Debian Codegraph @

src/widget-drop-down.c

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2023-2026 Geoffrey D. Bennett <[email protected]>
2 // SPDX-License-Identifier: GPL-3.0-or-later
3
4 #include <ctype.h>
5
6 #include "gtkhelper.h"
7 #include "widget-drop-down.h"
8
9 // Two construction modes share this state:
10 // Enum mode (ops == NULL): model is built from alsa_get_item_name
11 // and the ALSA value is the row index.
12 // Value-mapped mode (ops != NULL): caller-supplied model, ops map
13 // between ALSA values and row indices.
14 struct drop_down {
15 struct alsa_elem *elem;
16 GtkWidget *button;
17 GtkWidget *popover;
18 GtkWidget *listview;
19 GtkStringList *model;
20 GtkSingleSelection *selection;
21 int fixed_text;
22
23 // Latest ALSA value seen by drop_down_updated. Read by
24 // list_item_activated to skip redundant writes when the user picks a
25 // row whose value hasn't changed.
26 int current_value;
27
28 // Per-row tick icons. icon_count tracks the allocated length so the
29 // value-mapped path can grow the array when the model expands.
30 GtkWidget **icons;
31 int icon_count;
32 int ticked_row;
33
34 const struct drop_down_value_ops *ops;
35 void *user_data;
36 GDestroyNotify user_data_free;
37 };
38
39 static int is_value_mapped(struct drop_down *data) {
40 return data->ops != NULL;
41 }
42
43 static void sanitise_class_name(char *s) {
44 char *dst = s;
45
46 while (*s) {
47 if (isalnum(*s) || *s == '-')
48 *dst++ = tolower(*s);
49 s++;
50 }
51
52 *dst = '\0';
53 }
54
55 static void add_class(GtkWidget *widget, const char *class) {
56 char *class_name = g_strdup_printf("selected-%s", class);
57
58 sanitise_class_name(class_name);
59 gtk_widget_add_css_class(widget, class_name);
60 g_free(class_name);
61 }
62
63 static void resize_icons(struct drop_down *data, int new_count) {
64 if (new_count == data->icon_count)
65 return;
66 data->icons = g_realloc(data->icons, new_count * sizeof(GtkWidget *));
67 for (int i = data->icon_count; i < new_count; i++)
68 data->icons[i] = NULL;
69 data->icon_count = new_count;
70 }
71
72 static void update_icons(struct drop_down *data) {
73 for (int i = 0; i < data->icon_count; i++)
74 if (data->icons[i])
75 gtk_widget_set_opacity(
76 data->icons[i],
77 i == data->ticked_row ? 1.0 : 0.0
78 );
79 }
80
81 static void list_item_activated(
82 GtkListView *list,
83 guint index,
84 gpointer user_data
85 ) {
86 struct drop_down *data = user_data;
87
88 if (is_value_mapped(data)) {
89 int value = data->ops->row_to_value(
90 (int)index, data->current_value, data->user_data
91 );
92
93 // ops can return the current value to mean "no-op", which is how
94 // the sleep dropdown ignores clicks on the synthetic "Custom (N s)"
95 // row. Skip the write so the element doesn't see a redundant set.
96 if (value != data->current_value)
97 alsa_set_elem_value(data->elem, value);
98 } else {
99 data->current_value = (int)index;
100 data->ticked_row = (int)index;
101 update_icons(data);
102 alsa_set_elem_value(data->elem, (int)index);
103 }
104
105 gtk_popover_popdown(GTK_POPOVER(data->popover));
106 }
107
108 static void toggle_button_clicked(GtkWidget *widget, gpointer user_data) {
109 struct drop_down *data = user_data;
110
111 gtk_popover_popup(GTK_POPOVER(data->popover));
112 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(data->button), FALSE);
113
114 // Land focus on the currently-ticked row so Enter accepts the
115 // current value and arrow keys adjust from there.
116 if (data->ticked_row >= 0) {
117 gtk_single_selection_set_selected(data->selection, data->ticked_row);
118 gtk_list_view_scroll_to(
119 GTK_LIST_VIEW(data->listview),
120 data->ticked_row,
121 GTK_LIST_SCROLL_FOCUS,
122 NULL
123 );
124 }
125 }
126
127 static void setup_factory(
128 GtkListItemFactory *factory,
129 GtkListItem *list_item,
130 gpointer user_data
131 ) {
132 GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
133
134 GtkWidget *label = gtk_label_new(NULL);
135 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
136 gtk_widget_set_hexpand(label, TRUE);
137 gtk_box_append(GTK_BOX(box), label);
138
139 GtkWidget *icon = gtk_image_new_from_icon_name("object-select-symbolic");
140 gtk_box_append(GTK_BOX(box), icon);
141
142 gtk_list_item_set_child(list_item, box);
143 }
144
145 static void bind_factory(
146 GtkListItemFactory *factory,
147 GtkListItem *list_item,
148 gpointer user_data
149 ) {
150 struct drop_down *data = user_data;
151
152 GtkWidget *box = gtk_list_item_get_child(list_item);
153 GtkWidget *label = gtk_widget_get_first_child(box);
154 GtkWidget *icon = gtk_widget_get_last_child(box);
155
156 int index = gtk_list_item_get_position(list_item);
157
158 if (is_value_mapped(data)) {
159 GtkStringObject *so = gtk_list_item_get_item(list_item);
160 gtk_label_set_text(GTK_LABEL(label), gtk_string_object_get_string(so));
161 } else {
162 gtk_label_set_text(GTK_LABEL(label), alsa_get_item_name(data->elem, index));
163 }
164
165 if (index < data->icon_count)
166 data->icons[index] = icon;
167 gtk_widget_set_opacity(icon, index == data->ticked_row ? 1.0 : 0.0);
168 }
169
170 static void drop_down_updated(
171 struct alsa_elem *elem,
172 void *private
173 ) {
174 struct drop_down *data = private;
175
176 int is_writable = alsa_get_elem_writable(elem);
177 gtk_widget_set_sensitive(data->button, is_writable);
178
179 int value = alsa_get_elem_value(elem);
180 data->current_value = value;
181
182 if (is_value_mapped(data)) {
183 if (data->ops->refresh_model)
184 data->ops->refresh_model(data->model, value, data->user_data);
185
186 int row = data->ops->value_to_row(value, data->user_data);
187
188 // Keep the icon array sized to the (possibly grown) model so
189 // bind_factory has somewhere to record icons for new rows.
190 int model_n = (int)g_list_model_get_n_items(G_LIST_MODEL(data->model));
191 if (model_n > data->icon_count)
192 resize_icons(data, model_n);
193
194 data->ticked_row = row;
195 update_icons(data);
196 gtk_single_selection_set_selected(data->selection, row);
197
198 char *label = data->ops->button_label(value, data->user_data);
199 gtk_button_set_label(GTK_BUTTON(data->button), label);
200 g_free(label);
201 } else {
202 data->ticked_row = value;
203 update_icons(data);
204 gtk_single_selection_set_selected(data->selection, value);
205
206 gtk_widget_remove_css_classes_by_prefix(data->button, "selected-");
207 add_class(data->button, alsa_get_item_name(elem, value));
208
209 if (!data->fixed_text)
210 gtk_button_set_label(
211 GTK_BUTTON(data->button),
212 alsa_get_item_name(elem, value)
213 );
214 }
215 }
216
217 static void drop_down_destroy(GtkWidget *widget, gpointer user_data) {
218 struct drop_down *data = user_data;
219
220 // Detach from the element so a late event can't dereference us.
221 alsa_elem_remove_callbacks_by_data(data->elem, data);
222
223 gtk_widget_unparent(data->popover);
224 g_free(data->icons);
225 if (data->user_data_free)
226 data->user_data_free(data->user_data);
227 g_free(data);
228 }
229
230 // Build the popover/listview/button infrastructure shared by both
231 // dropdown variants. The caller has already populated data->elem,
232 // data->model, data->fixed_text, and any value-mapped fields.
233 static void build_drop_down(struct drop_down *data, const char *label_text) {
234 data->button = gtk_toggle_button_new_with_label(label_text);
235 gtk_widget_add_css_class(data->button, "drop-down");
236
237 data->popover = gtk_popover_new();
238 gtk_popover_set_has_arrow(GTK_POPOVER(data->popover), FALSE);
239 gtk_widget_set_parent(
240 data->popover,
241 gtk_widget_get_first_child(data->button)
242 );
243 g_signal_connect(
244 gtk_widget_get_first_child(data->button),
245 "destroy", G_CALLBACK(drop_down_destroy), data
246 );
247
248 GtkListItemFactory *factory = gtk_signal_list_item_factory_new();
249 g_signal_connect(factory, "setup", G_CALLBACK(setup_factory), data);
250 g_signal_connect(factory, "bind", G_CALLBACK(bind_factory), data);
251
252 data->selection = gtk_single_selection_new(G_LIST_MODEL(data->model));
253 data->listview = gtk_list_view_new(
254 GTK_SELECTION_MODEL(data->selection), factory
255 );
256 gtk_list_view_set_single_click_activate(GTK_LIST_VIEW(data->listview), TRUE);
257 gtk_widget_add_css_class(data->listview, "drop-down-list");
258
259 gtk_popover_set_child(GTK_POPOVER(data->popover), data->listview);
260
261 g_signal_connect(
262 data->button, "clicked", G_CALLBACK(toggle_button_clicked), data
263 );
264 g_signal_connect(
265 data->listview, "activate", G_CALLBACK(list_item_activated), data
266 );
267
268 drop_down_updated(data->elem, data);
269
270 alsa_elem_add_callback(data->elem, drop_down_updated, data, NULL);
271 }
272
273 GtkWidget *make_drop_down_alsa_elem(
274 struct alsa_elem *elem,
275 const char *label_text
276 ) {
277 struct drop_down *data = g_malloc0(sizeof(struct drop_down));
278 data->elem = elem;
279 data->fixed_text = !!label_text;
280 data->ticked_row = -1;
281
282 data->model = gtk_string_list_new(NULL);
283
284 data->icon_count = alsa_get_item_count(elem);
285 data->icons = g_malloc0(data->icon_count * sizeof(GtkWidget *));
286 for (int i = 0; i < data->icon_count; i++)
287 gtk_string_list_append(data->model, alsa_get_item_name(elem, i));
288
289 build_drop_down(data, label_text);
290
291 return data->button;
292 }
293
294 GtkWidget *make_value_mapped_drop_down_alsa_elem(
295 struct alsa_elem *elem,
296 GtkStringList *model,
297 const struct drop_down_value_ops *ops,
298 void *user_data,
299 GDestroyNotify user_data_free
300 ) {
301 struct drop_down *data = g_malloc0(sizeof(struct drop_down));
302 data->elem = elem;
303 data->ticked_row = -1;
304 data->model = model;
305 data->ops = ops;
306 data->user_data = user_data;
307 data->user_data_free = user_data_free;
308
309 int n = (int)g_list_model_get_n_items(G_LIST_MODEL(model));
310 data->icons = g_malloc0(n * sizeof(GtkWidget *));
311 data->icon_count = n;
312
313 build_drop_down(data, "");
314
315 return data->button;
316 }