1#include <gtk/gtk.h>
2#include <glib/gstdio.h>
3
4static void
5activate (GSimpleAction *action,
6 GVariant *parameter,
7 gpointer user_data)
8{
9 g_print (format: "%s activated\n", g_action_get_name (G_ACTION (action)));
10}
11
12static GActionEntry entries[] = {
13 { "cut", activate, NULL, NULL, NULL },
14 { "copy", activate, NULL, NULL, NULL },
15 { "paste", activate, NULL, NULL, NULL },
16 { "bold", NULL, NULL, "false", NULL },
17 { "italic", NULL, NULL, "false", NULL },
18 { "strikethrough", NULL, NULL, "false", NULL },
19 { "underline", NULL, NULL, "false", NULL },
20 { "set-view", NULL, "s", "'list'", NULL },
21 { "action1", activate, NULL, NULL, NULL },
22 { "action2", NULL, NULL, "true", NULL },
23 { "action2a", NULL, NULL, "false", NULL },
24 { "action3", NULL, "s", "'three'", NULL },
25 { "action4", activate, NULL, NULL, NULL },
26 { "action5", activate, NULL, NULL, NULL },
27 { "action6", activate, NULL, NULL, NULL },
28 { "action7", activate, NULL, NULL, NULL },
29 { "action8", activate, NULL, NULL, NULL },
30 { "action9", activate, NULL, NULL, NULL },
31 { "action10", activate, NULL, NULL, NULL }
32};
33
34static void
35quit_cb (GtkWidget *widget,
36 gpointer data)
37{
38 gboolean *done = data;
39
40 *done = TRUE;
41
42 g_main_context_wakeup (NULL);
43}
44
45int
46main (int argc, char *argv[])
47{
48 GtkWidget *win;
49 GtkWidget *box;
50 GtkWidget *button;
51 GtkWidget *button1;
52 GtkWidget *button2;
53 GtkBuilder *builder;
54 GMenuModel *model;
55 GSimpleActionGroup *actions;
56 GtkWidget *overlay;
57 GtkWidget *grid;
58 GtkWidget *popover;
59 GtkWidget *popover1;
60 GtkWidget *popover2;
61 GtkWidget *label;
62 GtkWidget *check;
63 GtkWidget *combo;
64 GtkWidget *header_bar;
65 gboolean done = FALSE;
66
67#ifdef GTK_SRCDIR
68 g_chdir (GTK_SRCDIR);
69#endif
70
71 gtk_init ();
72
73 win = gtk_window_new ();
74 gtk_window_set_default_size (GTK_WINDOW (win), width: 400, height: 600);
75 header_bar = gtk_header_bar_new ();
76 gtk_window_set_titlebar (GTK_WINDOW (win), titlebar: header_bar);
77 gtk_window_set_title (GTK_WINDOW (win), title: "Test GtkPopover");
78 actions = g_simple_action_group_new ();
79 g_action_map_add_action_entries (G_ACTION_MAP (actions), entries, G_N_ELEMENTS (entries), NULL);
80
81 gtk_widget_insert_action_group (widget: win, name: "top", G_ACTION_GROUP (actions));
82
83 overlay = gtk_overlay_new ();
84 gtk_window_set_child (GTK_WINDOW (win), child: overlay);
85
86 grid = gtk_grid_new ();
87 gtk_widget_set_halign (widget: grid, align: GTK_ALIGN_FILL);
88 gtk_widget_set_valign (widget: grid, align: GTK_ALIGN_FILL);
89 gtk_grid_set_row_spacing (GTK_GRID (grid), spacing: 10);
90 gtk_grid_set_column_spacing (GTK_GRID (grid), spacing: 10);
91 gtk_overlay_set_child (GTK_OVERLAY (overlay), child: grid);
92
93 label = gtk_label_new (str: "");
94 gtk_widget_set_hexpand (widget: label, TRUE);
95 gtk_widget_set_vexpand (widget: label, TRUE);
96 gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: 0, width: 1, height: 1);
97
98 label = gtk_label_new (str: "");
99 gtk_widget_set_hexpand (widget: label, TRUE);
100 gtk_widget_set_vexpand (widget: label, TRUE);
101 gtk_grid_attach (GTK_GRID (grid), child: label, column: 3, row: 7, width: 1, height: 1);
102
103 builder = gtk_builder_new_from_file (filename: "popover.ui");
104 model = (GMenuModel *)gtk_builder_get_object (builder, name: "menu");
105
106 box = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 6);
107 button = gtk_menu_button_new ();
108 gtk_box_append (GTK_BOX (box), child: button);
109 button1 = gtk_menu_button_new ();
110 gtk_box_append (GTK_BOX (box), child: button1);
111 button2 = gtk_menu_button_new ();
112 gtk_box_append (GTK_BOX (box), child: button2);
113
114 gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), menu_model: model);
115 popover = GTK_WIDGET (gtk_menu_button_get_popover (GTK_MENU_BUTTON (button)));
116
117 popover1 = gtk_popover_menu_new_from_model_full (model, flags: GTK_POPOVER_MENU_NESTED);
118 gtk_menu_button_set_popover (GTK_MENU_BUTTON (button1), popover: popover1);
119
120 g_object_unref (object: builder);
121 builder = gtk_builder_new_from_file (filename: "popover2.ui");
122 popover2 = (GtkWidget *)gtk_builder_get_object (builder, name: "popover");
123 gtk_menu_button_set_popover (GTK_MENU_BUTTON (button2), popover: popover2);
124
125 gtk_widget_set_margin_start (widget: box, margin: 10);
126 gtk_widget_set_margin_end (widget: box, margin: 10);
127 gtk_widget_set_margin_top (widget: box, margin: 10);
128 gtk_widget_set_margin_bottom (widget: box, margin: 10);
129 gtk_overlay_add_overlay (GTK_OVERLAY (overlay), widget: box);
130
131 label = gtk_label_new (str: "Popover hexpand");
132 check = gtk_check_button_new ();
133 g_object_bind_property (source: check, source_property: "active", target: popover, target_property: "hexpand", flags: G_BINDING_SYNC_CREATE);
134 g_object_bind_property (source: check, source_property: "active", target: popover1, target_property: "hexpand", flags: G_BINDING_SYNC_CREATE);
135 g_object_bind_property (source: check, source_property: "active", target: popover2, target_property: "hexpand", flags: G_BINDING_SYNC_CREATE);
136 gtk_grid_attach (GTK_GRID (grid), child: label , column: 1, row: 1, width: 1, height: 1);
137 gtk_grid_attach (GTK_GRID (grid), child: check, column: 2, row: 1, width: 1, height: 1);
138
139 label = gtk_label_new (str: "Popover vexpand");
140 check = gtk_check_button_new ();
141 g_object_bind_property (source: check, source_property: "active", target: popover, target_property: "vexpand", flags: G_BINDING_SYNC_CREATE);
142 g_object_bind_property (source: check, source_property: "active", target: popover1, target_property: "vexpand", flags: G_BINDING_SYNC_CREATE);
143 g_object_bind_property (source: check, source_property: "active", target: popover2, target_property: "vexpand", flags: G_BINDING_SYNC_CREATE);
144 gtk_grid_attach (GTK_GRID (grid), child: label , column: 1, row: 2, width: 1, height: 1);
145 gtk_grid_attach (GTK_GRID (grid), child: check, column: 2, row: 2, width: 1, height: 1);
146
147
148 label = gtk_label_new (str: "Autohide");
149 check = gtk_check_button_new ();
150 g_object_bind_property (source: check, source_property: "active", target: popover, target_property: "autohide", flags: G_BINDING_SYNC_CREATE);
151 g_object_bind_property (source: check, source_property: "active", target: popover1, target_property: "autohide", flags: G_BINDING_SYNC_CREATE);
152 g_object_bind_property (source: check, source_property: "active", target: popover2, target_property: "autohide", flags: G_BINDING_SYNC_CREATE);
153 gtk_check_button_set_active (GTK_CHECK_BUTTON (check), TRUE);
154 gtk_grid_attach (GTK_GRID (grid), child: label , column: 1, row: 3, width: 1, height: 1);
155 gtk_grid_attach (GTK_GRID (grid), child: check, column: 2, row: 3, width: 1, height: 1);
156
157 label = gtk_label_new (str: "Button direction");
158 combo = gtk_combo_box_text_new ();
159 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), id: "up", text: "Up");
160 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), id: "down", text: "Down");
161 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), id: "left", text: "Left");
162 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), id: "right", text: "Right");
163 gtk_combo_box_set_active (GTK_COMBO_BOX (combo), index_: 1);
164 g_object_bind_property (source: combo, source_property: "active", target: button, target_property: "direction", flags: G_BINDING_SYNC_CREATE);
165 g_object_bind_property (source: combo, source_property: "active", target: button1, target_property: "direction", flags: G_BINDING_SYNC_CREATE);
166 g_object_bind_property (source: combo, source_property: "active", target: button2, target_property: "direction", flags: G_BINDING_SYNC_CREATE);
167 gtk_grid_attach (GTK_GRID (grid), child: label , column: 1, row: 4, width: 1, height: 1);
168 gtk_grid_attach (GTK_GRID (grid), child: combo, column: 2, row: 4, width: 1, height: 1);
169
170 label = gtk_label_new (str: "Button halign");
171 combo = gtk_combo_box_text_new ();
172 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), id: "fill", text: "Fill");
173 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), id: "start", text: "Start");
174 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), id: "end", text: "End");
175 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), id: "center", text: "Center");
176 gtk_combo_box_set_active (GTK_COMBO_BOX (combo), index_: 2);
177 g_object_bind_property (source: combo, source_property: "active", target: box, target_property: "halign", flags: G_BINDING_SYNC_CREATE);
178 gtk_grid_attach (GTK_GRID (grid), child: label , column: 1, row: 5, width: 1, height: 1);
179 gtk_grid_attach (GTK_GRID (grid), child: combo, column: 2, row: 5, width: 1, height: 1);
180
181 label = gtk_label_new (str: "Button valign");
182 combo = gtk_combo_box_text_new ();
183 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), id: "fill", text: "Fill");
184 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), id: "start", text: "Start");
185 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), id: "end", text: "End");
186 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), id: "center", text: "Center");
187 gtk_combo_box_set_active (GTK_COMBO_BOX (combo), index_: 1);
188 g_object_bind_property (source: combo, source_property: "active", target: box, target_property: "valign", flags: G_BINDING_SYNC_CREATE);
189 gtk_grid_attach (GTK_GRID (grid), child: label , column: 1, row: 6, width: 1, height: 1);
190 gtk_grid_attach (GTK_GRID (grid), child: combo, column: 2, row: 6, width: 1, height: 1);
191 g_object_unref (object: builder);
192
193
194 g_signal_connect (win, "destroy", G_CALLBACK (quit_cb), &done);
195 gtk_widget_show (widget: win);
196
197 while (!done)
198 g_main_context_iteration (NULL, TRUE);
199
200 return 0;
201}
202

source code of gtk/tests/testpopover.c