testmenubutton.c 5.29 KB
Newer Older
1 2
#include <gtk/gtk.h>

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#define INITIAL_HALIGN          GTK_ALIGN_START
#define INITIAL_VALIGN          GTK_ALIGN_START

static GList *menubuttons = NULL;

static void
horizontal_alignment_changed (GtkComboBox *box)
{
	GtkAlign alignment = gtk_combo_box_get_active (box);
	GList *l;

	for (l = menubuttons; l != NULL; l = l->next) {
		GtkMenu *popup = gtk_menu_button_get_popup (GTK_MENU_BUTTON (l->data));
		if (popup != NULL)
			gtk_widget_set_halign (GTK_WIDGET (popup), alignment);
	}
}

static void
vertical_alignment_changed (GtkComboBox *box)
{
	GtkAlign alignment = gtk_combo_box_get_active (box);
	GList *l;

	for (l = menubuttons; l != NULL; l = l->next) {
		GtkMenu *popup = gtk_menu_button_get_popup (GTK_MENU_BUTTON (l->data));
		if (popup != NULL)
			gtk_widget_set_valign (GTK_WIDGET (popup), alignment);
	}
}

34 35 36 37 38 39
int main (int argc, char **argv)
{
	GtkWidget *window;
	GtkWidget *button;
	GtkWidget *grid;
	GtkWidget *entry;
40
	GtkWidget *label;
41
	GtkWidget *check;
42
	GtkWidget *combo;
43
	GtkWidget *menu_widget;
44
	GtkAccelGroup *accel_group;
45
	guint i;
46
	guint row = 0;
47 48 49 50 51 52 53 54
	GMenu *menu;

	gtk_init (&argc, &argv);

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_resize (GTK_WINDOW (window), 400, 300);

	grid = gtk_grid_new ();
55 56 57
	gtk_container_set_border_width (GTK_CONTAINER (grid), 12);
	gtk_grid_set_row_spacing (GTK_GRID (grid), 12);
	gtk_grid_set_column_spacing (GTK_GRID (grid), 12);
58 59
	gtk_container_add (GTK_CONTAINER (window), grid);

60 61 62
	accel_group = gtk_accel_group_new ();
	gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	/* horizontal alignment */
	label = gtk_label_new ("Horizontal Alignment:");
	gtk_widget_show (label);
	gtk_grid_attach (GTK_GRID (grid), label, 0, row++, 1, 1);

	combo = gtk_combo_box_text_new ();
	gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Fill");
	gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Start");
	gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "End");
	gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Center");
	gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Baseline");
	gtk_combo_box_set_active (GTK_COMBO_BOX (combo), INITIAL_HALIGN);
	gtk_widget_show (combo);
	gtk_grid_attach_next_to (GTK_GRID (grid), combo, label, GTK_POS_RIGHT, 1, 1);
	g_signal_connect (G_OBJECT (combo), "changed",
			  G_CALLBACK (horizontal_alignment_changed), menubuttons);

	/* vertical alignment */
	label = gtk_label_new ("Vertical Alignment:");
	gtk_widget_show (label);
	gtk_grid_attach (GTK_GRID (grid), label, 0, row++, 1, 1);

	combo = gtk_combo_box_text_new ();
	gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Fill");
	gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Start");
	gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "End");
	gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Center");
	gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Baseline");
	gtk_combo_box_set_active (GTK_COMBO_BOX (combo), INITIAL_HALIGN);
	gtk_widget_show (combo);
	gtk_grid_attach_next_to (GTK_GRID (grid), combo, label, GTK_POS_RIGHT, 1, 1);
	g_signal_connect (G_OBJECT (combo), "changed",
			  G_CALLBACK (vertical_alignment_changed), menubuttons);

97 98
	/* Button next to entry */
	entry = gtk_entry_new ();
99
	gtk_grid_attach (GTK_GRID (grid), entry, 0, row++, 1, 1);
100
	button = gtk_menu_button_new ();
101 102 103 104
	gtk_widget_set_halign (button, GTK_ALIGN_START);

	gtk_grid_attach_next_to (GTK_GRID (grid), button, entry, GTK_POS_RIGHT, 1, 1);
	menubuttons = g_list_prepend (menubuttons, button);
105 106 107 108 109 110

	/* Button with GtkMenu */
	menu_widget = gtk_menu_new ();
	for (i = 5; i > 0; i--) {
		GtkWidget *item;

111
		if (i == 3) {
112
			item = gtk_menu_item_new_with_mnemonic ("_Copy");
113 114 115 116 117 118 119 120
		} else {
			char *label;

			label = g_strdup_printf ("Item _%d", i);
			item = gtk_menu_item_new_with_mnemonic (label);
			g_free (label);
		}
		gtk_menu_item_set_use_underline (GTK_MENU_ITEM (item), TRUE);
121 122 123 124 125 126 127 128
		gtk_menu_attach (GTK_MENU (menu_widget),
				 item,
				 0, 1,
				 i - 1, i);
	}
	gtk_widget_show_all (menu_widget);

	button = gtk_menu_button_new ();
129 130
	gtk_widget_set_halign (button, GTK_ALIGN_START);
	menubuttons = g_list_prepend (menubuttons, button);
131
	gtk_menu_button_set_popup (GTK_MENU_BUTTON (button), menu_widget);
132
	gtk_grid_attach (GTK_GRID (grid), button, 1, row++, 1, 1);
133

134 135 136 137
        check = gtk_check_button_new_with_label ("Popover");
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), TRUE);
	gtk_grid_attach (GTK_GRID (grid), check, 0, row, 1, 1);

138 139 140 141
	/* Button with GMenuModel */
	menu = g_menu_new ();
	for (i = 5; i > 0; i--) {
		char *label;
142
                GMenuItem *item;
143
		label = g_strdup_printf ("Item _%d", i);
144 145 146
                item = g_menu_item_new (label, NULL);
                if (i == 3)
                  g_menu_item_set_attribute (item, "icon", "s", "preferences-desktop-locale-symbolic");
Matthias Clasen's avatar
Matthias Clasen committed
147
		g_menu_insert_item (menu, 0, item);
148
                g_object_unref (item);
149 150
		g_free (label);
	}
151

152
	button = gtk_menu_button_new ();
153 154
        g_object_bind_property (check, "active", button, "use-popover", G_BINDING_SYNC_CREATE);

155 156
	gtk_widget_set_halign (button, GTK_ALIGN_START);
	menubuttons = g_list_prepend (menubuttons, button);
157
	gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), G_MENU_MODEL (menu));
158
	gtk_grid_attach (GTK_GRID (grid), button, 1, row++, 1, 1);
159 160 161 162 163 164 165

	gtk_widget_show_all (window);

	gtk_main ();

	return 0;
}