1 | #include <gtk/gtk.h> |
2 | |
3 | GtkWidget *label; |
4 | |
5 | |
6 | static void |
7 | change_label_button (GSimpleAction *action, |
8 | GVariant *parameter, |
9 | gpointer user_data) |
10 | { |
11 | gtk_label_set_label (GTK_LABEL (label), str: "Text set from button" ); |
12 | } |
13 | |
14 | static void |
15 | (GSimpleAction *action, |
16 | GVariant *parameter, |
17 | gpointer user_data) |
18 | { |
19 | gtk_label_set_label (GTK_LABEL (label), str: "Text set from normal menu item" ); |
20 | } |
21 | |
22 | static void |
23 | (GSimpleAction *action, |
24 | GVariant *parameter, |
25 | gpointer user_data) |
26 | { |
27 | GVariant *state = g_action_get_state (G_ACTION (action)); |
28 | |
29 | gtk_label_set_label (GTK_LABEL (label), str: "Text set from toggle menu item" ); |
30 | |
31 | g_simple_action_set_state (simple: action, value: g_variant_new_boolean (value: !g_variant_get_boolean (value: state))); |
32 | |
33 | g_variant_unref (value: state); |
34 | } |
35 | |
36 | static void |
37 | (GSimpleAction *action, |
38 | GVariant *parameter, |
39 | gpointer user_data) |
40 | { |
41 | gtk_label_set_label (GTK_LABEL (label), str: "Text set from submenu item" ); |
42 | } |
43 | |
44 | static void |
45 | radio (GSimpleAction *action, GVariant *parameter, gpointer user_data) |
46 | { |
47 | char *str; |
48 | |
49 | str = g_strdup_printf (format: "From Radio menu item %s" , |
50 | g_variant_get_string (value: parameter, NULL)); |
51 | |
52 | gtk_label_set_label (GTK_LABEL (label), str); |
53 | |
54 | g_free (mem: str); |
55 | |
56 | g_simple_action_set_state (simple: action, value: parameter); |
57 | } |
58 | |
59 | |
60 | |
61 | static const GActionEntry win_actions[] = { |
62 | { "change-label-button" , change_label_button, NULL, NULL, NULL }, |
63 | { "normal-menu-item" , normal_menu_item, NULL, NULL, NULL }, |
64 | { "toggle-menu-item" , toggle_menu_item, NULL, "true" , NULL }, |
65 | { "submenu-item" , submenu_item, NULL, NULL, NULL }, |
66 | { "radio" , radio, "s" , "'1'" , NULL }, |
67 | }; |
68 | |
69 | |
70 | static const char * = |
71 | "<interface>" |
72 | " <menu id=\"menu_model\">" |
73 | " <section>" |
74 | " <item>" |
75 | " <attribute name=\"label\">Normal Menu Item</attribute>" |
76 | " <attribute name=\"action\">win.normal-menu-item</attribute>" |
77 | " </item>" |
78 | " <submenu>" |
79 | " <attribute name=\"label\">Submenu</attribute>" |
80 | " <item>" |
81 | " <attribute name=\"label\">Submenu Item</attribute>" |
82 | " <attribute name=\"action\">win.submenu-item</attribute>" |
83 | " </item>" |
84 | " </submenu>" |
85 | " <item>" |
86 | " <attribute name=\"label\">Toggle Menu Item</attribute>" |
87 | " <attribute name=\"action\">win.toggle-menu-item</attribute>" |
88 | " </item>" |
89 | " </section>" |
90 | " <section>" |
91 | " <item>" |
92 | " <attribute name=\"label\">Radio 1</attribute>" |
93 | " <attribute name=\"action\">win.radio</attribute>" |
94 | " <attribute name=\"target\">1</attribute>" |
95 | " </item>" |
96 | " <item>" |
97 | " <attribute name=\"label\">Radio 2</attribute>" |
98 | " <attribute name=\"action\">win.radio</attribute>" |
99 | " <attribute name=\"target\">2</attribute>" |
100 | " </item>" |
101 | " <item>" |
102 | " <attribute name=\"label\">Radio 3</attribute>" |
103 | " <attribute name=\"action\">win.radio</attribute>" |
104 | " <attribute name=\"target\">3</attribute>" |
105 | " </item>" |
106 | " </section>" |
107 | " </menu>" |
108 | "</interface>" |
109 | ; |
110 | |
111 | |
112 | int main (int argc, char **argv) |
113 | { |
114 | gtk_init (); |
115 | GtkWidget *window = gtk_window_new (); |
116 | GtkWidget *box = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 12); |
117 | GtkWidget * = gtk_menu_button_new (); |
118 | GtkWidget *button1 = gtk_button_new_with_label (label: "Change Label Text" ); |
119 | GtkWidget *; |
120 | GSimpleActionGroup *action_group; |
121 | GtkWidget *box1; |
122 | |
123 | |
124 | action_group = g_simple_action_group_new (); |
125 | g_action_map_add_action_entries (G_ACTION_MAP (action_group), |
126 | entries: win_actions, |
127 | G_N_ELEMENTS (win_actions), |
128 | NULL); |
129 | |
130 | gtk_widget_insert_action_group (widget: window, name: "win" , G_ACTION_GROUP (action_group)); |
131 | |
132 | |
133 | label = gtk_label_new (str: "Initial Text" ); |
134 | gtk_widget_set_margin_top (widget: label, margin: 12); |
135 | gtk_widget_set_margin_bottom (widget: label, margin: 12); |
136 | gtk_box_append (GTK_BOX (box), child: label); |
137 | gtk_widget_set_halign (widget: menubutton, align: GTK_ALIGN_CENTER); |
138 | { |
139 | GMenuModel *; |
140 | GtkBuilder *builder = gtk_builder_new_from_string (string: menu_data, length: -1); |
141 | menu_model = G_MENU_MODEL (gtk_builder_get_object (builder, "menu_model" )); |
142 | |
143 | menu = gtk_popover_menu_new_from_model (model: menu_model); |
144 | |
145 | } |
146 | gtk_menu_button_set_popover (GTK_MENU_BUTTON (menubutton), popover: menu); |
147 | gtk_box_append (GTK_BOX (box), child: menubutton); |
148 | gtk_widget_set_halign (widget: button1, align: GTK_ALIGN_CENTER); |
149 | gtk_actionable_set_action_name (GTK_ACTIONABLE (button1), action_name: "win.change-label-button" ); |
150 | gtk_box_append (GTK_BOX (box), child: button1); |
151 | |
152 | box1 = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 10); |
153 | |
154 | button1 = gtk_toggle_button_new_with_label (label: "Toggle" ); |
155 | gtk_actionable_set_action_name (GTK_ACTIONABLE (button1), action_name: "win.toggle-menu-item" ); |
156 | gtk_box_append (GTK_BOX (box1), child: button1); |
157 | |
158 | button1 = gtk_check_button_new_with_label (label: "Check" ); |
159 | gtk_actionable_set_action_name (GTK_ACTIONABLE (button1), action_name: "win.toggle-menu-item" ); |
160 | gtk_box_append (GTK_BOX (box1), child: button1); |
161 | |
162 | gtk_box_append (GTK_BOX (box), child: box1); |
163 | |
164 | box1 = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 10); |
165 | |
166 | button1 = gtk_toggle_button_new_with_label (label: "Radio 1" ); |
167 | gtk_actionable_set_detailed_action_name (GTK_ACTIONABLE (button1), detailed_action_name: "win.radio::1" ); |
168 | gtk_box_append (GTK_BOX (box1), child: button1); |
169 | |
170 | button1 = gtk_toggle_button_new_with_label (label: "Radio 2" ); |
171 | gtk_actionable_set_detailed_action_name (GTK_ACTIONABLE (button1), detailed_action_name: "win.radio::2" ); |
172 | gtk_box_append (GTK_BOX (box1), child: button1); |
173 | |
174 | button1 = gtk_toggle_button_new_with_label (label: "Radio 3" ); |
175 | gtk_actionable_set_detailed_action_name (GTK_ACTIONABLE (button1), detailed_action_name: "win.radio::3" ); |
176 | gtk_box_append (GTK_BOX (box1), child: button1); |
177 | |
178 | gtk_box_append (GTK_BOX (box), child: box1); |
179 | |
180 | box1 = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 10); |
181 | |
182 | button1 = gtk_check_button_new_with_label (label: "Radio 1" ); |
183 | gtk_actionable_set_detailed_action_name (GTK_ACTIONABLE (button1), detailed_action_name: "win.radio::1" ); |
184 | gtk_box_append (GTK_BOX (box1), child: button1); |
185 | |
186 | button1 = gtk_check_button_new_with_label (label: "Radio 2" ); |
187 | gtk_actionable_set_detailed_action_name (GTK_ACTIONABLE (button1), detailed_action_name: "win.radio::2" ); |
188 | gtk_box_append (GTK_BOX (box1), child: button1); |
189 | |
190 | button1 = gtk_check_button_new_with_label (label: "Radio 3" ); |
191 | gtk_actionable_set_detailed_action_name (GTK_ACTIONABLE (button1), detailed_action_name: "win.radio::3" ); |
192 | gtk_box_append (GTK_BOX (box1), child: button1); |
193 | |
194 | gtk_box_append (GTK_BOX (box), child: box1); |
195 | |
196 | gtk_window_set_child (GTK_WINDOW (window), child: box); |
197 | |
198 | gtk_widget_show (widget: window); |
199 | while (TRUE) |
200 | g_main_context_iteration (NULL, TRUE); |
201 | return 0; |
202 | } |
203 | |