GCC Code Coverage Report


Directory: ./
File: panels/wacom/cc-wacom-stylus-action-dialog.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 85 0.0%
Functions: 0 12 0.0%
Branches: 0 21 0.0%

Line Branch Exec Source
1 /* cc-wacom-stylus-action-dialog.c
2 *
3 * Copyright © 2024 Red Hat, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-2.0-or-later
19 */
20
21 #include <config.h>
22 #include <glib/gi18n.h>
23 #include <adwaita.h>
24
25 #include <gdesktop-enums.h>
26
27 #include "cc-wacom-stylus-action-dialog.h"
28 #include "cc-wacom-panel.h"
29
30 struct _CcWacomStylusActionDialog
31 {
32 AdwWindow parent_instance;
33
34 AdwPreferencesGroup *preferences_group;
35
36 AdwActionRow *left_button_row;
37 AdwActionRow *right_button_row;
38 AdwActionRow *middle_button_row;
39 AdwActionRow *back_row;
40 AdwActionRow *forward_row;
41
42 GSettings *settings;
43 char *key;
44 };
45
46 G_DEFINE_TYPE (CcWacomStylusActionDialog, cc_wacom_stylus_action_dialog, ADW_TYPE_WINDOW)
47
48 static void
49 cc_wacom_stylus_action_dialog_finalize (GObject *object)
50 {
51 CcWacomStylusActionDialog *self = CC_WACOM_STYLUS_ACTION_DIALOG (object);
52
53 g_clear_pointer (&self->key, free);
54
55 G_OBJECT_CLASS (cc_wacom_stylus_action_dialog_parent_class)->finalize (object);
56 }
57
58 static void
59 left_button_activated (CcWacomStylusActionDialog *self)
60 {
61 g_settings_set_enum (self->settings, self->key, G_DESKTOP_STYLUS_BUTTON_ACTION_DEFAULT);
62 }
63
64 static void
65 right_button_activated (CcWacomStylusActionDialog *self)
66 {
67 g_settings_set_enum (self->settings, self->key, G_DESKTOP_STYLUS_BUTTON_ACTION_RIGHT);
68 }
69
70 static void
71 middle_button_activated (CcWacomStylusActionDialog *self)
72 {
73 g_settings_set_enum (self->settings, self->key, G_DESKTOP_STYLUS_BUTTON_ACTION_MIDDLE);
74 }
75
76 static void
77 back_activated (CcWacomStylusActionDialog *self)
78 {
79 g_settings_set_enum (self->settings, self->key, G_DESKTOP_STYLUS_BUTTON_ACTION_BACK);
80 }
81
82 static void
83 forward_activated (CcWacomStylusActionDialog *self)
84 {
85 g_settings_set_enum (self->settings, self->key, G_DESKTOP_STYLUS_BUTTON_ACTION_FORWARD);
86 }
87
88 static void
89 cc_wacom_stylus_action_dialog_class_init (CcWacomStylusActionDialogClass *klass)
90 {
91 GObjectClass *object_class = G_OBJECT_CLASS (klass);
92 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
93
94 object_class->finalize = cc_wacom_stylus_action_dialog_finalize;
95
96 gtk_widget_class_add_binding_action (widget_class, GDK_KEY_Escape, 0, "window.close", NULL);
97
98 gtk_widget_class_set_template_from_resource (widget_class,
99 "/org/gnome/control-center/"
100 "wacom/cc-wacom-stylus-action-dialog.ui");
101
102 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusActionDialog, preferences_group);
103
104 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusActionDialog, left_button_row);
105 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusActionDialog, right_button_row);
106 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusActionDialog, middle_button_row);
107 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusActionDialog, back_row);
108 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusActionDialog, forward_row);
109
110 gtk_widget_class_bind_template_callback (widget_class, left_button_activated);
111 gtk_widget_class_bind_template_callback (widget_class, middle_button_activated);
112 gtk_widget_class_bind_template_callback (widget_class, right_button_activated);
113 gtk_widget_class_bind_template_callback (widget_class, forward_activated);
114 gtk_widget_class_bind_template_callback (widget_class, back_activated);
115 }
116
117 static void
118 cc_wacom_stylus_action_dialog_init (CcWacomStylusActionDialog *self)
119 {
120 const char *text;
121
122 gtk_widget_init_template (GTK_WIDGET (self));
123
124 text = cc_wacom_panel_get_stylus_button_action_label (G_DESKTOP_STYLUS_BUTTON_ACTION_DEFAULT);
125 adw_preferences_row_set_title (ADW_PREFERENCES_ROW (self->left_button_row), text);
126 text = cc_wacom_panel_get_stylus_button_action_label (G_DESKTOP_STYLUS_BUTTON_ACTION_MIDDLE);
127 adw_preferences_row_set_title (ADW_PREFERENCES_ROW (self->middle_button_row), text);
128 text = cc_wacom_panel_get_stylus_button_action_label (G_DESKTOP_STYLUS_BUTTON_ACTION_RIGHT);
129 adw_preferences_row_set_title (ADW_PREFERENCES_ROW (self->right_button_row), text);
130 text = cc_wacom_panel_get_stylus_button_action_label (G_DESKTOP_STYLUS_BUTTON_ACTION_BACK);
131 adw_preferences_row_set_title (ADW_PREFERENCES_ROW (self->back_row), text);
132 text = cc_wacom_panel_get_stylus_button_action_label (G_DESKTOP_STYLUS_BUTTON_ACTION_FORWARD);
133 adw_preferences_row_set_title (ADW_PREFERENCES_ROW (self->forward_row), text);
134 }
135
136 GtkWidget*
137 cc_wacom_stylus_action_dialog_new (GSettings *settings,
138 const char *stylus_name,
139 guint button,
140 const char *key)
141 {
142 CcWacomStylusActionDialog *dialog = g_object_new (CC_TYPE_WACOM_STYLUS_ACTION_DIALOG, NULL);
143 GDesktopStylusButtonAction action;
144 AdwActionRow *row = NULL;
145 g_autofree char *text = NULL;
146 g_autofree char *title = NULL;
147
148 g_return_val_if_fail(button > 0 && button <= 3, NULL);
149
150 dialog->settings = settings;
151 dialog->key = g_strdup (key);
152
153 gtk_window_set_title (GTK_WINDOW (dialog), stylus_name);
154
155 text = g_strdup_printf (_("Choose an action when button %d on the stylus is pressed"), button);
156 adw_preferences_group_set_description (dialog->preferences_group, text);
157
158 title = g_strdup_printf (_("Button %d Mapping"), button);
159 adw_preferences_group_set_title (dialog->preferences_group, title);
160
161 action = g_settings_get_enum (settings, key);
162 switch (action) {
163 case G_DESKTOP_STYLUS_BUTTON_ACTION_DEFAULT:
164 row = dialog->left_button_row;
165 break;
166 case G_DESKTOP_STYLUS_BUTTON_ACTION_MIDDLE:
167 row = dialog->middle_button_row;
168 break;
169 case G_DESKTOP_STYLUS_BUTTON_ACTION_RIGHT:
170 row = dialog->right_button_row;
171 break;
172 case G_DESKTOP_STYLUS_BUTTON_ACTION_BACK:
173 row = dialog->back_row;
174 break;
175 case G_DESKTOP_STYLUS_BUTTON_ACTION_FORWARD:
176 row = dialog->forward_row;
177 break;
178 }
179
180 if (row)
181 adw_action_row_activate (row);
182
183 return GTK_WIDGET (dialog);
184 }
185