GCC Code Coverage Report


Directory: ./
File: panels/wacom/cc-wacom-button-row.c
Date: 2024-05-03 09:46:52
Exec Total Coverage
Lines: 0 96 0.0%
Functions: 0 15 0.0%
Branches: 0 29 0.0%

Line Branch Exec Source
1 /*
2 * Copyright © 2013 Red Hat, Inc.
3 *
4 * Authors: Joaquim Rocha <jrocha@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21 #include <glib/gi18n-lib.h>
22
23 #include "gsd-wacom-key-shortcut-button.h"
24 #include "cc-wacom-button-row.h"
25
26 #define ACTION_KEY "action"
27 #define KEYBINDING_KEY "keybinding"
28
29 #define WACOM_C(x) g_dpgettext2(NULL, "Wacom action-type", x)
30
31 enum {
32 ACTION_NAME_COLUMN,
33 ACTION_TYPE_COLUMN,
34 ACTION_N_COLUMNS
35 };
36
37 struct _CcWacomButtonRow {
38 GtkListBoxRow parent_instance;
39 guint button;
40 GSettings *settings;
41 GtkDirectionType direction;
42 GtkComboBox *action_combo;
43 GsdWacomKeyShortcutButton *key_shortcut_button;
44 };
45
46 G_DEFINE_TYPE (CcWacomButtonRow, cc_wacom_button_row, GTK_TYPE_LIST_BOX_ROW)
47
48 static GtkWidget *
49 create_actions_combo (void)
50 {
51 GtkListStore *model;
52 GtkTreeIter iter;
53 GtkWidget *combo;
54 GtkCellRenderer *renderer;
55 gint i;
56
57 model = gtk_list_store_new (ACTION_N_COLUMNS, G_TYPE_STRING, G_TYPE_INT);
58
59 for (i = 0; i < G_N_ELEMENTS (action_table); i++)
60 {
61 gtk_list_store_append (model, &iter);
62 gtk_list_store_set (model, &iter,
63 ACTION_NAME_COLUMN, WACOM_C(action_table[i].action_name),
64 ACTION_TYPE_COLUMN, action_table[i].action_type, -1);
65 }
66
67 combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (model));
68
69 renderer = gtk_cell_renderer_text_new ();
70 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
71 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
72 "text", ACTION_NAME_COLUMN, NULL);
73
74
75 return combo;
76 }
77
78 static void
79 cc_wacom_button_row_update_shortcut (CcWacomButtonRow *row,
80 GDesktopPadButtonAction action_type)
81 {
82 guint keyval;
83 GdkModifierType mask;
84 g_autofree gchar *shortcut = NULL;
85
86 if (action_type != G_DESKTOP_PAD_BUTTON_ACTION_KEYBINDING)
87 return;
88
89 shortcut = g_settings_get_string (row->settings, KEYBINDING_KEY);
90
91 if (shortcut != NULL)
92 {
93 gtk_accelerator_parse (shortcut, &keyval, &mask);
94
95 g_object_set (row->key_shortcut_button,
96 "key-value", keyval,
97 "key-mods", mask,
98 NULL);
99 }
100 }
101
102 static void
103 cc_wacom_button_row_update_action (CcWacomButtonRow *row,
104 GDesktopPadButtonAction action_type)
105 {
106 GtkTreeIter iter;
107 gboolean iter_valid;
108 GDesktopPadButtonAction current_action_type, real_action_type;
109 GtkTreeModel *model;
110
111 model = gtk_combo_box_get_model (row->action_combo);
112 real_action_type = action_type;
113
114 for (iter_valid = gtk_tree_model_get_iter_first (model, &iter); iter_valid;
115 iter_valid = gtk_tree_model_iter_next (model, &iter))
116 {
117 gtk_tree_model_get (model, &iter,
118 ACTION_TYPE_COLUMN, &current_action_type,
119 -1);
120
121 if (current_action_type == real_action_type)
122 {
123 gtk_combo_box_set_active_iter (row->action_combo, &iter);
124 break;
125 }
126 }
127 }
128
129 static void
130 cc_wacom_button_row_update (CcWacomButtonRow *row)
131 {
132 GDesktopPadButtonAction current_action_type;
133
134 current_action_type = g_settings_get_enum (row->settings, ACTION_KEY);
135
136 cc_wacom_button_row_update_shortcut (row, current_action_type);
137
138 cc_wacom_button_row_update_action (row, current_action_type);
139
140 gtk_widget_set_sensitive (GTK_WIDGET (row->key_shortcut_button),
141 current_action_type == G_DESKTOP_PAD_BUTTON_ACTION_KEYBINDING);
142 }
143
144 static void
145 change_button_action_type (CcWacomButtonRow *row,
146 GDesktopPadButtonAction type)
147 {
148 g_settings_set_enum (row->settings, ACTION_KEY, type);
149 gtk_widget_set_sensitive (GTK_WIDGET (row->key_shortcut_button),
150 type == G_DESKTOP_PAD_BUTTON_ACTION_KEYBINDING);
151 }
152
153 static void
154 on_key_shortcut_edited (CcWacomButtonRow *row)
155 {
156 g_autofree gchar *custom_key = NULL;
157 guint keyval;
158 GdkModifierType mask;
159
160 change_button_action_type (row, G_DESKTOP_PAD_BUTTON_ACTION_KEYBINDING);
161
162 g_object_get (row->key_shortcut_button,
163 "key-value", &keyval,
164 "key-mods", &mask,
165 NULL);
166
167 mask &= ~GDK_LOCK_MASK;
168
169 custom_key = gtk_accelerator_name (keyval, mask);
170
171 g_settings_set_string (row->settings, KEYBINDING_KEY, custom_key);
172 }
173
174 static void
175 on_key_shortcut_cleared (CcWacomButtonRow *row)
176 {
177 change_button_action_type (row, G_DESKTOP_PAD_BUTTON_ACTION_NONE);
178 cc_wacom_button_row_update_action (row, G_DESKTOP_PAD_BUTTON_ACTION_NONE);
179 }
180
181 static void
182 on_row_action_combo_box_changed (CcWacomButtonRow *row)
183 {
184 GDesktopPadButtonAction type;
185 GtkTreeModel *model;
186 GtkListBox *list_box;
187 GtkTreeIter iter;
188
189 if (!gtk_combo_box_get_active_iter (row->action_combo, &iter))
190 return;
191
192 /* Select the row where we changed the combo box (if not yet selected) */
193 list_box = GTK_LIST_BOX (gtk_widget_get_parent (GTK_WIDGET (row)));
194 if (list_box && gtk_list_box_get_selected_row (list_box) != GTK_LIST_BOX_ROW (row))
195 gtk_list_box_select_row (list_box, GTK_LIST_BOX_ROW (row));
196
197 model = gtk_combo_box_get_model (row->action_combo);
198 gtk_tree_model_get (model, &iter, ACTION_TYPE_COLUMN, &type, -1);
199
200 change_button_action_type (row, type);
201 }
202
203 static gboolean
204 on_key_shortcut_button_press_event (CcWacomButtonRow *row)
205 {
206 GtkListBox *list_box;
207
208 /* Select the row where we pressed the button (if not yet selected) */
209 list_box = GTK_LIST_BOX (gtk_widget_get_parent (GTK_WIDGET (row)));
210 if (list_box && gtk_list_box_get_selected_row (list_box) != GTK_LIST_BOX_ROW (row))
211 gtk_list_box_select_row (list_box, GTK_LIST_BOX_ROW (row));
212
213 return FALSE;
214 }
215
216 static void
217 cc_wacom_button_row_class_init (CcWacomButtonRowClass *button_row_class)
218 {
219 }
220
221 static void
222 cc_wacom_button_row_init (CcWacomButtonRow *button_row)
223 {
224 }
225
226 GtkWidget *
227 cc_wacom_button_row_new (guint button,
228 GSettings *settings)
229 {
230 CcWacomButtonRow *row;
231 GtkWidget *grid, *combo, *label, *shortcut_button;
232 g_autofree gchar *name = NULL;
233
234 row = CC_WACOM_BUTTON_ROW (g_object_new (CC_WACOM_TYPE_BUTTON_ROW, NULL));
235
236 row->button = button;
237 row->settings = g_object_ref (settings);
238
239 grid = gtk_grid_new ();
240 gtk_grid_set_row_homogeneous (GTK_GRID (grid), TRUE);
241 gtk_grid_set_column_homogeneous (GTK_GRID (grid), TRUE);
242
243 name = g_strdup_printf (_("Button %d"), button);
244 label = gtk_label_new (name);
245 g_object_set (label, "halign", GTK_ALIGN_START, NULL);
246 gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
247
248 combo = create_actions_combo ();
249 gtk_grid_attach (GTK_GRID (grid), combo, 1, 0, 1, 1);
250 row->action_combo = GTK_COMBO_BOX (combo);
251 g_signal_connect_object (combo, "changed",
252 G_CALLBACK (on_row_action_combo_box_changed), row, G_CONNECT_SWAPPED);
253
254 shortcut_button = gsd_wacom_key_shortcut_button_new ();
255 g_object_set (shortcut_button, "mode", GSD_WACOM_KEY_SHORTCUT_BUTTON_MODE_ALL, NULL);
256 gtk_grid_attach (GTK_GRID (grid), shortcut_button, 2, 0, 1, 1);
257 row->key_shortcut_button = GSD_WACOM_KEY_SHORTCUT_BUTTON (shortcut_button);
258 g_signal_connect_object (shortcut_button, "key-shortcut-cleared",
259 G_CALLBACK (on_key_shortcut_cleared),
260 row,
261 G_CONNECT_SWAPPED);
262 g_signal_connect_object (shortcut_button, "key-shortcut-edited",
263 G_CALLBACK (on_key_shortcut_edited),
264 row,
265 G_CONNECT_SWAPPED);
266 g_signal_connect_object (shortcut_button, "button-press-event",
267 G_CALLBACK (on_key_shortcut_button_press_event),
268 row,
269 G_CONNECT_SWAPPED);
270
271 gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (row), grid);
272
273 cc_wacom_button_row_update (CC_WACOM_BUTTON_ROW (row));
274
275 return GTK_WIDGET (row);
276 }
277