Search code examples
gtkmenubar

GTK+ menu bar looks terrible


I'm writing a Gtk+ menu bar following the instruction here.

When packing the bar onto a VBox with

// "FALSE, TRUE" and "FALSE, FALSE" actually makes no difference
gtk_box_pack_start(GTK_BOX(main_vbox), menu_bar,
                   TRUE, FALSE, 0);

the menu bar looks terrible, like this:

slim menu bar

And when I changed to:

 gtk_box_pack_start(GTK_BOX(main_vbox), menu_bar,
                    TRUE, TRUE, 0);

it looks like:

enter image description here

So, how to make the toolbar get a smaller space allocated?


Solution

  • If I understand your question correctly, you want to:

    1. Get rid of the light gray padding around the menu bar.
    2. Do the above without expanding the menu bar to fill the available space.

    Therefore, the packing mode of your other widget (namely the button bar below your menu bar) comes into play (for clarity, I'll refer to them as button_hbox and menu_bar, respectively, since both can qualify as "toolbars").

    Understanding the boolean layout arguments passed to gtk_box_pack_start() is paramount here:

    • The first one, expand, is TRUE if the widget should consume the empty space left in its container after layout is computed. Widgets packed this way compete equally for the remaining space.

    • The second one, fill, is TRUE if the widget should fill the layout space it consumes instead of being centered within it (there comes the light gray padding).

    The idea is that you have one (or more, but let's stick to one for now) main widget in main_vbox, and that widget is packed with both expand and fill set to TRUE. The satellite widgets around it are packed with expand set to FALSE and fill set to TRUE. For instance:

    +-------------------------------------------+
    |  Menu bar: expand = FALSE, fill = TRUE    |
    +-------------------------------------------+
    |  Toolbar: expand = FALSE, fill = TRUE     |
    +-------------------------------------------+  ^
    |                                           |  |
    |                                           |  |  The height of this widget
    |  Main widget: expand = TRUE, fill = TRUE  |  |  varies depending on the 
    |                                           |  |  height of its container.
    |                                           |  |
    +-------------------------------------------+  v
    |  Status bar: expand = FALSE, fill = TRUE  |
    +-------------------------------------------+
    

    In your case, since you don't want menu_bar to fill the available space, button_hbox should do so:

    // Menu bar: satellite widget, expand = FALSE, fill = TRUE
    gtk_box_pack_start(GTK_BOX(main_vbox), menu_bar, FALSE, TRUE, 0);
    
    // Button bar: main widget, expand = TRUE, fill = TRUE
    gtk_box_pack_start(GTK_BOX(main_vbox), button_hbox, TRUE, TRUE, 0);