What is the exactly purpose use of set_align_widget function from GtkMenuButton

Today I tried to work with the GtkMenuButton and I understand it 95%, execept the function:


The documentations say:

Sets the [GtkWidget](https://developer.gnome.org/gtk3/stable/GtkWidget.html) to use to line the menu with when popped up. Note that the  *`align_widget`*  must contain the [GtkMenuButton](https://developer.gnome.org/gtk3/stable/GtkMenuButton.html) itself.

After I read closely I noticed that I am not sure what is the purpose of this Function.

Here is the Code tested:

#include <gtk/gtk.h>

int main ( void )
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *menu_button;

    gtk_init ( NULL, NULL );

    /* Create the Window*/
    window = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
    gtk_window_set_default_size ( GTK_WINDOW ( window ), 300, 300 );
    g_signal_connect ( window, "delete-event", gtk_main_quit, NULL );
    g_signal_connect ( window, "destroy",      gtk_main_quit, NULL );

    /* Create the Box which holds the Menu Button*/
    box      = gtk_box_new ( GTK_ORIENTATION_HORIZONTAL, 5 );
    gtk_container_add ( GTK_CONTAINER ( window ), box );

    /* Create the Menu Button*/
    menu_button = gtk_menu_button_new();
    gtk_widget_set_sensitive( menu_button, TRUE );
    gtk_widget_set_valign ( menu_button, GTK_ALIGN_START );
    gtk_box_set_center_widget ( GTK_BOX ( box ), menu_button );

    /*  BOX holds the Menu Button*/
    /* Set the Box as align widget for Menu Button*/
    gtk_menu_button_set_align_widget ( GTK_MENU_BUTTON ( menu_button ), box );

    /* Set the Button Menu Direction*/
    gtk_menu_button_set_direction ( GTK_MENU_BUTTON ( menu_button ), GTK_ARROW_LEFT );

    /* Create the Image for Menu Button*/
    GtkWidget *img = gtk_image_new_from_icon_name ( "emblem-system-symbolic", GTK_ICON_SIZE_BUTTON );
    gtk_container_add ( GTK_CONTAINER ( menu_button ), img );

    /// ***
    gtk_widget_show_all ( window );
    gtk_main ();

At this point the Menu Button behaves just like its a GtktoggleButton.

That’s because you didn’t assign a GMenuModel, GtkMenu or GtkPopover - the menu button simply doesn’t have a menu it could show :slight_smile:

To be honest, I’m not sure either. It used to line up the menu with a parent instead of the button itself (for example: center the menu to the button’s toolbar regardless of the position of the button), but it’s hard to think of a use case for that behavior.

It actually looks like few people can, as the property hasn’t been doing anything since gtk@ 8701e34f749 4 years ago. IMHO the property should be removed for GTK4.

1 Like

Well I noticed already (hence the name “MEnuButton” :slight_smile: ) , but I was not sure whats the story with this Function at all.

I will remember me this. Thank you for your time.