Debian Codegraph @

src/window-configuration.c

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2022-2025 Geoffrey D. Bennett <[email protected]>
2 // SPDX-License-Identifier: GPL-3.0-or-later
3
4 #include <gtk/gtk.h>
5
6 #include "alsa.h"
7 #include "gtkhelper.h"
8 #include "optional-controls.h"
9 #include "optional-state.h"
10 #include "custom-names.h"
11 #include "device-port-names.h"
12 #include "port-enable.h"
13 #include "widget-text-entry.h"
14 #include "window-configuration.h"
15 #include "config-autogain.h"
16 #include "config-device-name.h"
17 #include "config-device-settings.h"
18 #include "config-front-panel.h"
19 #include "config-io.h"
20 #include "config-monitor-groups.h"
21
22 struct configuration_window {
23 struct alsa_card *card;
24 GtkWidget *top;
25 };
26
27 static void on_destroy(
28 struct configuration_window *data,
29 GtkWidget *widget
30 ) {
31 g_free(data);
32 }
33
34 // Keys used to store the configuration window tabs in the state file
35 #define CONFIG_TAB_KEY "configuration-tab"
36 #define CONFIG_IO_TAB_KEY "configuration-io-tab"
37
38 // Data for notebook tab persistence
39 struct notebook_tab_data {
40 struct alsa_card *card;
41 const char *key;
42 };
43
44 // Callback when a notebook tab changes
45 static void on_tab_changed(
46 GtkNotebook *notebook,
47 GtkWidget *page,
48 guint page_num,
49 gpointer user_data
50 ) {
51 // During window destruction, the notebook is detached from the
52 // hierarchy before tabs are removed, triggering this callback.
53 if (!gtk_widget_get_root(GTK_WIDGET(notebook)))
54 return;
55
56 struct notebook_tab_data *data = user_data;
57
58 const char *page_id = g_object_get_data(G_OBJECT(page), PAGE_ID_KEY);
59 if (page_id)
60 optional_state_save(data->card, CONFIG_SECTION_UI, data->key, page_id);
61 }
62
63 // Free notebook tab data
64 static void free_notebook_tab_data(gpointer data) {
65 g_free(data);
66 }
67
68 // Find page index by ID, returns -1 if not found
69 static int find_page_by_id(GtkNotebook *notebook, const char *page_id) {
70 int n_pages = gtk_notebook_get_n_pages(notebook);
71
72 for (int i = 0; i < n_pages; i++) {
73 GtkWidget *page = gtk_notebook_get_nth_page(notebook, i);
74 const char *id = g_object_get_data(G_OBJECT(page), PAGE_ID_KEY);
75 if (id && strcmp(id, page_id) == 0)
76 return i;
77 }
78
79 return -1;
80 }
81
82 // Restore the saved tab selection and connect signal to save changes
83 void setup_notebook_tab_persistence(
84 GtkNotebook *notebook,
85 struct alsa_card *card,
86 const char *key
87 ) {
88 // Restore saved tab from [ui] section
89 GHashTable *state = optional_state_load(card, CONFIG_SECTION_UI);
90 if (state) {
91 const char *value = g_hash_table_lookup(state, key);
92 if (value) {
93 int page_num = find_page_by_id(notebook, value);
94 if (page_num >= 0)
95 gtk_notebook_set_current_page(notebook, page_num);
96 }
97 g_hash_table_destroy(state);
98 }
99
100 // Connect signal to save tab changes
101 struct notebook_tab_data *data = g_malloc(sizeof(struct notebook_tab_data));
102 data->card = card;
103 data->key = key;
104
105 g_signal_connect_data(
106 notebook, "switch-page", G_CALLBACK(on_tab_changed), data,
107 (GClosureNotify)free_notebook_tab_data, 0
108 );
109 }
110
111 // Wrap tab content in a scrolled window for vertical scrolling
112 GtkWidget *wrap_tab_content_scrolled(GtkWidget *content) {
113 GtkWidget *scrolled = gtk_scrolled_window_new();
114 gtk_scrolled_window_set_policy(
115 GTK_SCROLLED_WINDOW(scrolled),
116 GTK_POLICY_NEVER,
117 GTK_POLICY_AUTOMATIC
118 );
119 gtk_scrolled_window_set_propagate_natural_height(
120 GTK_SCROLLED_WINDOW(scrolled), TRUE
121 );
122 gtk_widget_set_vexpand(scrolled, TRUE);
123 gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scrolled), content);
124 return scrolled;
125 }
126
127 GtkWidget *create_configuration_controls(struct alsa_card *card) {
128 struct configuration_window *data =
129 g_malloc0(sizeof(struct configuration_window));
130 data->card = card;
131
132 // create main container
133 GtkWidget *top = gtk_box_new(GTK_ORIENTATION_VERTICAL, 20);
134 gtk_widget_add_css_class(top, "window-frame");
135 GtkWidget *vbox = top;
136
137 data->top = top;
138
139 // Create top-level notebook for configuration sections
140 GtkWidget *top_notebook = gtk_notebook_new();
141 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(top_notebook), GTK_POS_TOP);
142 gtk_widget_add_css_class(top_notebook, "outer-notebook");
143
144 // Device Name tab
145 add_device_name_tab(top_notebook, card);
146
147 // Device Settings tab
148 add_device_settings_tab(top_notebook, card);
149
150 // Front Panel tab
151 add_front_panel_tab(top_notebook, card);
152
153 // I/O Configuration tab
154 add_io_tab(top_notebook, card);
155
156 // Autogain tab
157 add_autogain_tab(top_notebook, card);
158
159 // Monitor Groups tab
160 add_monitor_groups_tab(top_notebook, card);
161
162 // Add top notebook to main container (only if it has pages)
163 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(top_notebook)) > 0) {
164 gtk_widget_set_vexpand(top_notebook, TRUE);
165 gtk_box_append(GTK_BOX(vbox), top_notebook);
166
167 // Restore saved tab and connect signal to save tab changes
168 setup_notebook_tab_persistence(
169 GTK_NOTEBOOK(top_notebook), card, CONFIG_TAB_KEY
170 );
171 }
172
173 // cleanup on destroy
174 g_object_weak_ref(
175 G_OBJECT(data->top),
176 (GWeakNotify)on_destroy,
177 data
178 );
179
180 return data->top;
181 }