1 /*
2  * gui/accelerators.h - accel
3  *
4  * Copyright (C) 2018 Alexandros Theodotou
5  *
6  * This file is part of Zrythm
7  *
8  * Zrythm is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Zrythm is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with Zrythm.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include "gui/accel.h"
23 #include "zrythm.h"
24 #include "zrythm_app.h"
25 
26 #include <gtk/gtk.h>
27 
28 /**
29  * Adds all global accelerators.
30  *
31  * Note: not used, doesn't work.
32  */
33 /*void*/
34 /*accel_add_all ()*/
35 /*{*/
36   /*gtk_accel_map_add_entry ("<MainWindow>/File/Save",*/
37                            /*GDK_KEY_s,*/
38                            /*GDK_CONTROL_MASK);*/
39   /*gtk_accel_map_add_entry ("<MainWindow>/File/Save As",*/
40                            /*GDK_KEY_s,*/
41                            /*GDK_CONTROL_MASK & GDK_SHIFT_MASK);*/
42   /*gtk_accel_map_add_entry ("<MainWindow>/File/Export As",*/
43                            /*GDK_KEY_e,*/
44                            /*GDK_CONTROL_MASK);*/
45   /*gtk_accel_map_add_entry ("<MainWindow>/File/Quit",*/
46                            /*GDK_KEY_q,*/
47                            /*GDK_CONTROL_MASK);*/
48 /*}*/
49 
50 /**
51  * Install accelerator for an action.
52  */
53 void
accel_install_action_accelerator(const char * primary,const char * secondary,const char * action_name)54 accel_install_action_accelerator (
55   const char *     primary,
56   const char *     secondary,
57   const char *     action_name)
58 {
59   g_warn_if_fail (zrythm_app);
60   const char *accels[] =
61     { primary, secondary, NULL };
62   gtk_application_set_accels_for_action (
63     GTK_APPLICATION (zrythm_app),
64     action_name, accels);
65 }
66 
67 char *
accel_get_primary_accel_for_action(const char * action_name)68 accel_get_primary_accel_for_action (
69   const char * action_name)
70 {
71   g_warn_if_fail (zrythm_app);
72   guint accel_key;
73   GdkModifierType accel_mods;
74   gchar ** accels =
75     gtk_application_get_accels_for_action (
76       GTK_APPLICATION (zrythm_app),
77       action_name);
78   char * ret = NULL;
79   if (accels[0] != NULL)
80     {
81       gtk_accelerator_parse (
82         accels[0],
83         &accel_key,
84         &accel_mods);
85       ret =
86         gtk_accelerator_get_label (
87           accel_key, accel_mods);
88     }
89   g_strfreev (accels);
90   return ret;
91 }
92 
93 void
accel_set_accel_label_from_action(GtkAccelLabel * accel_label,const char * action_name)94 accel_set_accel_label_from_action (
95   GtkAccelLabel * accel_label,
96   const char *    action_name)
97 {
98   g_warn_if_fail (zrythm_app);
99   guint accel_key;
100   GdkModifierType accel_mods;
101   gchar ** accels =
102     gtk_application_get_accels_for_action (
103       GTK_APPLICATION (zrythm_app),
104       action_name);
105   if (accels[0] != NULL)
106     {
107       gtk_accelerator_parse (
108         accels[0],
109         &accel_key,
110         &accel_mods);
111       gtk_accel_label_set_accel (
112         accel_label,
113         accel_key,
114         accel_mods);
115     }
116   g_strfreev (accels);
117 }
118