Resizing a window with native titlebar and fixed aspect ratio

gui

#1

What is the correct way to create a resizable window that uses a native titlebar and has a fixed aspect ratio?
My Problem is that for the intended layout to work I actually need the aspect ratio to be constant for the bare window area excluding borders and titlebar.
And of course I would prefer to use the native edge and corner handles to resize the
window.

Should I use the constraint on a main component? how would I make the surrounding window snap to the component? Or is there another recommended way to achieve this?

Thanks!


#2

I just finished a way to do this so I thought I’d share. I overrode the “checkBounds” function of the ComponentBoundsConstrainer to also factor in a border. It’s pretty simple. Just one overriden function:

void BorderBoundsConstrainer::checkBounds(Rectangle<int>& bounds, const Rectangle<int>& previous,
                                          const Rectangle<int>& limits,
                                          bool stretching_top, bool stretching_left,
                                          bool stretching_bottom, bool stretching_right) {
  border_.subtractFrom(bounds);
  ComponentBoundsConstrainer::checkBounds(bounds, previous, limits,
                                          stretching_top, stretching_left,
                                          stretching_bottom, stretching_right);
  border_.addTo(bounds);
}

And here’s the class declaration:

class BorderBoundsConstrainer : public ComponentBoundsConstrainer {
  public:
    BorderBoundsConstrainer() : ComponentBoundsConstrainer() { }

    virtual void checkBounds(Rectangle<int>& bounds, const Rectangle<int>& previous,
                             const Rectangle<int>& limits,
                             bool stretching_top, bool stretching_left,
                             bool stretching_bottom, bool stretching_right) override;

    void setBorder(BorderSize<int> border) { border_ = border; }

  protected:
    BorderSize<int> border_;
};

Then in your window (e.g. DocumentWindow) when you’re setting the constrainer up, you pass in the border of the window like so:

constrainer_->setBorder(getPeer()->getFrameSize());

That’s it. Hope it helps.


#3

I just noticed that you answered already a while ago.
Thanks so much. I’ll check that out rightaway. :slight_smile: