How to keep window's width / height ratio better way

May 27, 2013 at 12:57 PM
Hi Jinhao!
I tried to keep the ratio by making size event but failed with it.
So is there another way to do it?

Thank you.
Coordinator
May 27, 2013 at 3:28 PM
Is it that dragging the border of window to resize it with a specified width/height ratio? Can u show a sample code of your size event handler.
May 27, 2013 at 4:19 PM
Edited May 27, 2013 at 5:12 PM
I am ashamed of this code,but I don't know another way:
int width = _window.size().width;
_window.make_event<nana::gui::events::size>([this,width](const nana::gui::eventinfo& info) mutable
{
    const double epsilon = 0.001;
    if(std::abs(static_cast<double>(info.size.width) / info.size.height - config::window::ratio) > epsilon)
        _window.size(width,width / config::window::ratio);
    width = info.size.width;
});
assumed that user draggs left or right border,but not top or bottom.
May 28, 2013 at 10:46 AM
Ideally I would like to achieve an effect that can be observed when resize solitaire under Windows7
Coordinator
May 28, 2013 at 9:09 PM
Hi,
There is a new event in the latest commit(ce03b33f6cff) for this demand.
_window.make_event<nana::gui::events::sizing>([this](const nana::gui::eventinfo& ei)
{
    const double ratio = 2.0;

    //Change the size
    if(ei.sizing.border == nana::gui::window_border::top || ei.sizing.border == nana::gui::window_border::bottom)
        ei.sizing.width = ei.sizing.height * ratio;
    else
        ei.sizing.height = ei.sizing.width / ratio;

    //Do not change the size in this way.
    _window.size(width, height); //Not allowed. 
    API::window_size(ei.window, width, height); // Not allowed.
});
The sizing event is used to monitor the size while the size of window is been changing, and change it if needed.
Notice, to change the size in the sizing event, only modifying the eventinfo.sizing.width and eventinfo.sizing.height is allowed.
May 28, 2013 at 9:20 PM
Cool.Thank you:)