1 | #include <gtk/gtk.h> |
2 | |
3 | typedef struct |
4 | { |
5 | GtkApplication parent_instance; |
6 | } TestApp; |
7 | |
8 | typedef GtkApplicationClass TestAppClass; |
9 | |
10 | static GType test_app_get_type (void); |
11 | G_DEFINE_TYPE (TestApp, test_app, GTK_TYPE_APPLICATION) |
12 | |
13 | static GtkWidget *create_row (const char *label); |
14 | |
15 | static void |
16 | activate_first_row (GSimpleAction *simple, |
17 | GVariant *parameter, |
18 | gpointer user_data) |
19 | { |
20 | const char *text = "First row activated (no parameter action)" ; |
21 | |
22 | g_print (format: "%s\n" , text); |
23 | gtk_label_set_label (GTK_LABEL (user_data), str: text); |
24 | } |
25 | |
26 | static void |
27 | activate_print_string (GSimpleAction *simple, |
28 | GVariant *parameter, |
29 | gpointer user_data) |
30 | { |
31 | const char *text = g_variant_get_string (value: parameter, NULL); |
32 | |
33 | g_print (format: "%s\n" , text); |
34 | gtk_label_set_label (GTK_LABEL (user_data), str: text); |
35 | } |
36 | |
37 | static void |
38 | activate_print_int (GSimpleAction *simple, |
39 | GVariant *parameter, |
40 | gpointer user_data) |
41 | { |
42 | const int value = g_variant_get_int32 (value: parameter); |
43 | char *text; |
44 | |
45 | text = g_strdup_printf (format: "Row %d activated (int action)" , value); |
46 | |
47 | g_print (format: "%s\n" , text); |
48 | gtk_label_set_label (GTK_LABEL (user_data), str: text); |
49 | } |
50 | |
51 | static void |
52 | row_without_gaction_activated_cb (GtkListBox *list, |
53 | GtkListBoxRow *row, |
54 | gpointer user_data) |
55 | { |
56 | int index = gtk_list_box_row_get_index (row); |
57 | char *text; |
58 | |
59 | text = g_strdup_printf (format: "Row %d activated (signal based)" , index); |
60 | |
61 | g_print (format: "%s\n" , text); |
62 | gtk_label_set_label (GTK_LABEL (user_data), str: text); |
63 | } |
64 | |
65 | static void |
66 | add_separator (GtkListBoxRow *row, GtkListBoxRow *before, gpointer data) |
67 | { |
68 | if (!before) |
69 | return; |
70 | |
71 | gtk_list_box_row_set_header (row, header: gtk_separator_new (orientation: GTK_ORIENTATION_HORIZONTAL)); |
72 | } |
73 | |
74 | static GtkWidget * |
75 | create_row (const char *text) |
76 | { |
77 | GtkWidget *row_content, *label; |
78 | |
79 | row_content = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 10); |
80 | |
81 | label = gtk_label_new (str: text); |
82 | gtk_box_append (GTK_BOX (row_content), child: label); |
83 | |
84 | return row_content; |
85 | } |
86 | |
87 | static void |
88 | new_window (GApplication *app) |
89 | { |
90 | GtkWidget *window, *grid, *sw, *list, *label; |
91 | GSimpleAction *action; |
92 | |
93 | GtkWidget *row_content; |
94 | GtkListBoxRow *row; |
95 | |
96 | int i; |
97 | char *text, *text2; |
98 | |
99 | window = gtk_application_window_new (GTK_APPLICATION (app)); |
100 | gtk_window_set_default_size (GTK_WINDOW (window), width: 300, height: 300); |
101 | |
102 | /* widget creation */ |
103 | grid = gtk_grid_new (); |
104 | gtk_window_set_child (GTK_WINDOW (window), child: grid); |
105 | sw = gtk_scrolled_window_new (); |
106 | gtk_widget_set_hexpand (GTK_WIDGET (sw), true); |
107 | gtk_widget_set_vexpand (GTK_WIDGET (sw), true); |
108 | gtk_grid_attach (GTK_GRID (grid), child: sw, column: 0, row: 0, width: 1, height: 1); |
109 | |
110 | list = gtk_list_box_new (); |
111 | gtk_list_box_set_selection_mode (GTK_LIST_BOX (list), mode: GTK_SELECTION_NONE); |
112 | gtk_list_box_set_header_func (GTK_LIST_BOX (list), update_header: add_separator, NULL, NULL); |
113 | gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), child: list); |
114 | |
115 | label = gtk_label_new (str: "No row activated" ); |
116 | gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: 1, width: 1, height: 1); |
117 | |
118 | /* no parameter action row */ |
119 | action = g_simple_action_new (name: "first-row-action" , NULL); |
120 | g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (action)); |
121 | |
122 | row_content = create_row (text: "First row (no parameter action)" ); |
123 | gtk_list_box_insert (GTK_LIST_BOX (list), child: row_content, position: -1); |
124 | |
125 | row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (list), index_: 0); |
126 | gtk_actionable_set_action_name (GTK_ACTIONABLE (row), action_name: "win.first-row-action" ); |
127 | |
128 | g_signal_connect (action, "activate" , (GCallback) activate_first_row, label); |
129 | |
130 | /* string action rows */ |
131 | action = g_simple_action_new (name: "print-string" , G_VARIANT_TYPE_STRING); |
132 | g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (action)); |
133 | |
134 | for (i = 1; i < 3; i++) |
135 | { |
136 | text = g_strdup_printf (format: "Row %d (string action)" , i); |
137 | row_content = create_row (text); |
138 | gtk_list_box_insert (GTK_LIST_BOX (list), child: row_content, position: -1); |
139 | |
140 | row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (list), index_: i); |
141 | text2 = g_strdup_printf (format: "Row %d activated (string action)" , i); |
142 | gtk_actionable_set_action_target (GTK_ACTIONABLE (row), format_string: "s" , text2); |
143 | gtk_actionable_set_action_name (GTK_ACTIONABLE (row), action_name: "win.print-string" ); |
144 | } |
145 | |
146 | g_signal_connect (action, "activate" , (GCallback) activate_print_string, label); |
147 | |
148 | /* int action rows */ |
149 | action = g_simple_action_new (name: "print-int" , G_VARIANT_TYPE_INT32); |
150 | g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (action)); |
151 | |
152 | for (i = 3; i < 5; i++) |
153 | { |
154 | text = g_strdup_printf (format: "Row %d (int action)" , i); |
155 | row_content = create_row (text); |
156 | gtk_list_box_insert (GTK_LIST_BOX (list), child: row_content, position: -1); |
157 | |
158 | row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (list), index_: i); |
159 | gtk_actionable_set_action_target (GTK_ACTIONABLE (row), format_string: "i" , i); |
160 | gtk_actionable_set_action_name (GTK_ACTIONABLE (row), action_name: "win.print-int" ); |
161 | } |
162 | |
163 | g_signal_connect (action, "activate" , (GCallback) activate_print_int, label); |
164 | |
165 | /* signal based row */ |
166 | for (i = 5; i < 7; i++) |
167 | { |
168 | text = g_strdup_printf (format: "Row %d (signal based)" , i); |
169 | row_content = create_row (text); |
170 | gtk_list_box_insert (GTK_LIST_BOX (list), child: row_content, position: -1); |
171 | } |
172 | |
173 | g_signal_connect (list, "row-activated" , |
174 | G_CALLBACK (row_without_gaction_activated_cb), label); |
175 | |
176 | /* let the show begin */ |
177 | gtk_widget_show (GTK_WIDGET (window)); |
178 | } |
179 | |
180 | static void |
181 | test_app_activate (GApplication *application) |
182 | { |
183 | new_window (app: application); |
184 | } |
185 | |
186 | static void |
187 | test_app_init (TestApp *app) |
188 | { |
189 | } |
190 | |
191 | static void |
192 | test_app_class_init (TestAppClass *class) |
193 | { |
194 | G_APPLICATION_CLASS (class)->activate = test_app_activate; |
195 | } |
196 | |
197 | static TestApp * |
198 | test_app_new (void) |
199 | { |
200 | TestApp *test_app; |
201 | |
202 | g_set_application_name (application_name: "Test List 4" ); |
203 | |
204 | test_app = g_object_new (object_type: test_app_get_type (), |
205 | first_property_name: "application-id" , "org.gtk.testlist4" , |
206 | "flags" , G_APPLICATION_FLAGS_NONE, |
207 | NULL); |
208 | |
209 | return test_app; |
210 | } |
211 | |
212 | int |
213 | main (int argc, char **argv) |
214 | { |
215 | TestApp *test_app; |
216 | int status; |
217 | |
218 | test_app = test_app_new (); |
219 | status = g_application_run (G_APPLICATION (test_app), argc, argv); |
220 | |
221 | g_object_unref (object: test_app); |
222 | return status; |
223 | } |
224 | |