1#include <gtk/gtk.h>
2
3#define INITIAL_HALIGN GTK_ALIGN_START
4#define INITIAL_VALIGN GTK_ALIGN_START
5
6static GList *menubuttons = NULL;
7
8static void
9horizontal_alignment_changed (GtkComboBox *box)
10{
11 GtkAlign alignment = gtk_combo_box_get_active (combo_box: box);
12 GList *l;
13
14 for (l = menubuttons; l != NULL; l = l->next) {
15 GtkPopover *popup = gtk_menu_button_get_popover (GTK_MENU_BUTTON (l->data));
16 if (popup != NULL)
17 gtk_widget_set_halign (GTK_WIDGET (popup), align: alignment);
18 }
19}
20
21static void
22vertical_alignment_changed (GtkComboBox *box)
23{
24 GtkAlign alignment = gtk_combo_box_get_active (combo_box: box);
25 GList *l;
26
27 for (l = menubuttons; l != NULL; l = l->next) {
28 GtkPopover *popup = gtk_menu_button_get_popover (GTK_MENU_BUTTON (l->data));
29 if (popup != NULL)
30 gtk_widget_set_valign (GTK_WIDGET (popup), align: alignment);
31 }
32}
33
34int main (int argc, char **argv)
35{
36 GtkWidget *window;
37 GtkWidget *button;
38 GtkWidget *grid;
39 GtkWidget *entry;
40 GtkWidget *label;
41 GtkWidget *combo;
42 guint i;
43 guint row = 0;
44 GMenu *menu;
45
46 gtk_init ();
47
48 window = gtk_window_new ();
49 gtk_window_set_default_size (GTK_WINDOW (window), width: 400, height: 300);
50
51 grid = gtk_grid_new ();
52 gtk_grid_set_row_spacing (GTK_GRID (grid), spacing: 12);
53 gtk_grid_set_column_spacing (GTK_GRID (grid), spacing: 12);
54 gtk_window_set_child (GTK_WINDOW (window), child: grid);
55
56 /* horizontal alignment */
57 label = gtk_label_new (str: "Horizontal Alignment:");
58 gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: row++, width: 1, height: 1);
59
60 combo = gtk_combo_box_text_new ();
61 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), text: "Fill");
62 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), text: "Start");
63 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), text: "End");
64 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), text: "Center");
65 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), text: "Baseline");
66 gtk_combo_box_set_active (GTK_COMBO_BOX (combo), INITIAL_HALIGN);
67 gtk_grid_attach_next_to (GTK_GRID (grid), child: combo, sibling: label, side: GTK_POS_RIGHT, width: 1, height: 1);
68 g_signal_connect (G_OBJECT (combo), "changed",
69 G_CALLBACK (horizontal_alignment_changed), menubuttons);
70
71 /* vertical alignment */
72 label = gtk_label_new (str: "Vertical Alignment:");
73 gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: row++, width: 1, height: 1);
74
75 combo = gtk_combo_box_text_new ();
76 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), text: "Fill");
77 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), text: "Start");
78 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), text: "End");
79 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), text: "Center");
80 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), text: "Baseline");
81 gtk_combo_box_set_active (GTK_COMBO_BOX (combo), INITIAL_HALIGN);
82 gtk_grid_attach_next_to (GTK_GRID (grid), child: combo, sibling: label, side: GTK_POS_RIGHT, width: 1, height: 1);
83 g_signal_connect (G_OBJECT (combo), "changed",
84 G_CALLBACK (vertical_alignment_changed), menubuttons);
85
86 /* Button next to entry */
87 entry = gtk_entry_new ();
88 gtk_grid_attach (GTK_GRID (grid), child: entry, column: 0, row: row++, width: 1, height: 1);
89 button = gtk_menu_button_new ();
90 gtk_widget_set_halign (widget: button, align: GTK_ALIGN_START);
91
92 gtk_grid_attach_next_to (GTK_GRID (grid), child: button, sibling: entry, side: GTK_POS_RIGHT, width: 1, height: 1);
93 menubuttons = g_list_prepend (list: menubuttons, data: button);
94
95 /* Button with GMenuModel */
96 menu = g_menu_new ();
97 for (i = 5; i > 0; i--) {
98 char *item_label;
99 GMenuItem *item;
100 item_label = g_strdup_printf (format: "Item _%d", i);
101 item = g_menu_item_new (label: item_label, NULL);
102 if (i == 3)
103 g_menu_item_set_attribute (menu_item: item, attribute: "icon", format_string: "s", "preferences-desktop-locale-symbolic");
104 g_menu_insert_item (menu, position: 0, item);
105 g_object_unref (object: item);
106 g_free (mem: item_label);
107 }
108
109 button = gtk_menu_button_new ();
110
111 gtk_widget_set_halign (widget: button, align: GTK_ALIGN_START);
112 menubuttons = g_list_prepend (list: menubuttons, data: button);
113 gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), G_MENU_MODEL (menu));
114 gtk_grid_attach (GTK_GRID (grid), child: button, column: 1, row: row++, width: 1, height: 1);
115
116 gtk_widget_show (widget: window);
117
118 while (TRUE)
119 g_main_context_iteration (NULL, TRUE);
120
121 return 0;
122}
123

source code of gtk/tests/testmenubutton.c