How to keep the height of a menubar fixed in a resizable window ?

16 views
Skip to first unread message

Lucas Sanner

unread,
Apr 15, 2023, 7:38:25 AM4/15/23
to fltk.general
Hi,
Here's a simple window with a menubar:

void Change_CB(Fl_Widget *w, void *) {
    Fl_Menu_Bar *menu = (Fl_Menu_Bar*)w;
    Fl_Menu_Item *p;
    // Change submenu name
    p = (Fl_Menu_Item*)menu->find_item("Edit/Submenu");
    if ( p ) p->label("New Submenu Name");
    // Change item name
    p = (Fl_Menu_Item*)menu->find_item("Edit/New Submenu Name/Aaa");
    if ( p ) p->label("New Aaa Name");
}
void Quit_CB(Fl_Widget *, void *) {
    exit(0);
}
int main() {
    Fl_Window *win = new Fl_Window(400,400);
    Fl_Menu_Bar *menu = new Fl_Menu_Bar(0,0,400,25);
    menu->add("File/Quit",   FL_CTRL+'q', Quit_CB);
    menu->add("Edit/Change", FL_CTRL+'c', Change_CB);
    menu->add("Edit/Submenu/Aaa");
    menu->add("Edit/Submenu/Bbb");
    win->end();
    win->resizable(win);
    win->show();
    return(Fl::run());
}

The problem here is when the window is resized, the height of the menubar is resized too.
Is there any way to keep it unchanged while resizing ?

Albrecht Schlosser

unread,
Apr 15, 2023, 8:39:15 AM4/15/23
to fltkg...@googlegroups.com
On 4/15/23 13:38 Lucas Sanner wrote:
Here's a simple window with a menubar:

[code removed]


The problem here is when the window is resized, the height of the menubar is resized too.
Is there any way to keep it unchanged while resizing ?

Sure. There's an entire chapter in the docs about resizing: "How Does Resizing Work?"
https://www.fltk.org/doc-1.4/resize.html

This will hopefully answer your question. Look at the "Practical examples" section for a start with simple demos. These are included in the test/ folder if you want to try.

The key is that you use another resizable() widget that does NOT cover (overlap) the top area of the menu bar.

See the changed code and comments below for an update of your main() function:

int main() {
Fl_Window *win = new Fl_Window(400,400);
Fl_Menu_Bar *menu = new Fl_Menu_Bar(0,0,400,25);
menu->add("File/Quit", FL_CTRL+'q', Quit_CB);
menu->add("Edit/Change", FL_CTRL+'c', Change_CB);
menu->add("Edit/Submenu/Aaa");
menu->add("Edit/Submenu/Bbb");
Fl_Group *g = new Fl_Group(0,25,400,375); // container for other widgets
// put other widgets here
g-end();
win->end();
win->resizable(g); // <<<<< make g the resizable widget <<<<<
win->show();
return(Fl::run());
}
This is untested but it should get you going.

Note: in general it is not necessary to make a new group for "other" widgets. However, it is important to select a widget that does not overlap the menu bar as the resizable() widget. It could even be a tiny invisible box that is only inserted to be used as the resizable widget.

Albrecht Schlosser

unread,
Apr 15, 2023, 8:45:55 AM4/15/23
to fltkg...@googlegroups.com
On 4/15/23 14:39 Albrecht Schlosser wrote:
Fl_Group *g = new Fl_Group(0,25,400,375); // container for other widgets
// put other widgets here
g-end();

The line above should read: g->end();

Lucas Sanner

unread,
Apr 15, 2023, 10:00:57 AM4/15/23
to fltk.general
Ok, I get it now.
Thanks for your help.
Reply all
Reply to author
Forward
0 new messages