How to allow a form to expand only horizontally?

How to allow a form to expand only horizontally?
edited Oct 12 at 8:47 pm

You can use API::track_window_size to set limits for the window size. For example, the following code disables vertical resizing, and allows horizontal resizing in the range [600px, screen width]:

// nana::form fm {...};
auto h_limit {fm.size().height}; // restrict the form height to its current value
auto scr_width {nana::screen{}.from_window(fm).area().dimension().width};

nana::API::track_window_size(fm, {scr_width, h_limit}, true); // set maximum limits
nana::API::track_window_size(fm, {600, h_limit}, false); // set minimum limits
You can use [API::track_window_size](https://errorflynn.github.io/nana-docs/#api_form::track_window_size) to set limits for the window size. For example, the following code disables vertical resizing, and allows horizontal resizing in the range [600px, screen width]: ```` // nana::form fm {...}; auto h_limit {fm.size().height}; // restrict the form height to its current value auto scr_width {nana::screen{}.from_window(fm).area().dimension().width}; nana::API::track_window_size(fm, {scr_width, h_limit}, true); // set maximum limits nana::API::track_window_size(fm, {600, h_limit}, false); // set minimum limits ````
edited Oct 13 at 4:22 am

use resizing event of the form.

using namespace nana;
form fm;

auto fix_height = fm.size().height;
fm.events().resizing([&](const arg_resizing& arg){
   arg.height = fix_height;
});

fm.show();

exec();

return 0;
use resizing event of the form. ```` using namespace nana; form fm; auto fix_height = fm.size().height; fm.events().resizing([&](const arg_resizing& arg){ arg.height = fix_height; }); fm.show(); exec(); return 0; ````

Thanks Error Flynn, the code that you sent is working fine.

Now, I can resize the form, but how can I make the nana::button and nana::textbox in the form to stretch or move when I resize the form?

Basically I have two textbox inside two nana::group that I want to stretch horizontally with the form. Also I have two button at the right lower corner of the form that want them to move with their original size horizontally when I stretch the form.

Thanks Error Flynn, the code that you sent is working fine. Now, I can resize the form, but how can I make the nana::button and nana::textbox in the form to stretch or move when I resize the form? Basically I have two textbox inside two nana::group that I want to stretch horizontally with the form. Also I have two button at the right lower corner of the form that want them to move with their original size horizontally when I stretch the form.
edited Oct 13 at 6:22 pm

First I should say that Jinhao's article about the div text format is pretty good, I use it myself. https://github.com/cnjinhao/nana/wiki/Div-Text

The basic principle that's relevant here is that a field occupies as much of the space of its parent as possible, unless something restricts it. So the default behavior of a field is to stretch when the form is resized. If you don't want it to stretch, give it an absolute weight value. If you want it to move, put a sibling field (that stretches) before it, and it will be pushed by the sibling field.

Here's some code that hopefully shows this principle:

#include <nana/gui.hpp>
#include <nana/gui/widgets/label.hpp>

int main()
{
    nana::form fm {nana::API::make_center(1280, 720)};
    fm.bgcolor(nana::colors::white);
    fm.div("vert margin=15" // implicitly defined field (not enclosed in angle brackets), parent to all
        "< <stretch_1> <weight=15> <stretch_2> >" // this field stretches in both directions
        "<weight=15>" // spacer field, 15px height
        "< weight=30 <this_field_pushes_its_siblings> <button_1 weight=160> <weight=15> <button_2 weight=160> >" // 30px height
    );

    nana::label l1 {fm};
    l1.bgcolor(nana::colors::light_coral);
    fm["stretch_1"] << l1;

    nana::label l2 {fm};
    l2.bgcolor(nana::colors::light_goldenrod_yellow);
    fm["stretch_2"] << l2;

    nana::label l3 {fm};
    l3.bgcolor(nana::colors::light_green);
    fm["button_1"] << l3;

    nana::label l4 {fm};
    l4.bgcolor(nana::colors::light_blue);
    fm["button_2"] << l4;

    fm.collocate();
    fm.show();
    nana::exec();
}

Edit: corrected comment for "vert margin=15".

First I should say that Jinhao's article about the div text format is pretty good, I use it myself. https://github.com/cnjinhao/nana/wiki/Div-Text The basic principle that's relevant here is that a field occupies as much of the space of its parent as possible, unless something restricts it. So the default behavior of a field is to stretch when the form is resized. If you don't want it to stretch, give it an absolute `weight` value. If you want it to move, put a sibling field (that stretches) before it, and it will be pushed by the sibling field. Here's some code that hopefully shows this principle: ```` #include <nana/gui.hpp> #include <nana/gui/widgets/label.hpp> int main() { nana::form fm {nana::API::make_center(1280, 720)}; fm.bgcolor(nana::colors::white); fm.div("vert margin=15" // implicitly defined field (not enclosed in angle brackets), parent to all "< <stretch_1> <weight=15> <stretch_2> >" // this field stretches in both directions "<weight=15>" // spacer field, 15px height "< weight=30 <this_field_pushes_its_siblings> <button_1 weight=160> <weight=15> <button_2 weight=160> >" // 30px height ); nana::label l1 {fm}; l1.bgcolor(nana::colors::light_coral); fm["stretch_1"] << l1; nana::label l2 {fm}; l2.bgcolor(nana::colors::light_goldenrod_yellow); fm["stretch_2"] << l2; nana::label l3 {fm}; l3.bgcolor(nana::colors::light_green); fm["button_1"] << l3; nana::label l4 {fm}; l4.bgcolor(nana::colors::light_blue); fm["button_2"] << l4; fm.collocate(); fm.show(); nana::exec(); } ```` Edit: corrected comment for `"vert margin=15"`.
edited Oct 18 at 2:54 am
36
views
4
replies
3
followers
live preview
enter atleast 10 characters
WARNING: You mentioned %MENTIONS%, but they cannot see this message and will not be notified
Saving...
Saved
All posts under this topic will be deleted ?
Pending draft ... Click to resume editing
Discard draft