1 /* dzl-shortcut-simple-label.c
2  *
3  * Copyright (C) 2017 Christian Hergert <chergert@redhat.com>
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 3 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 
19 #define G_LOG_DOMAIN "dzl-shortcut-simple-label"
20 
21 #include "config.h"
22 
23 #include "shortcuts/dzl-shortcut-chord.h"
24 #include "shortcuts/dzl-shortcut-simple-label.h"
25 #include "util/dzl-gtk.h"
26 
27 struct _DzlShortcutSimpleLabel
28 {
29   GtkBox       parent_instance;
30 
31   GtkLabel    *accel_label;
32   GtkLabel    *title;
33 
34   const gchar *accel;
35   const gchar *action;
36   const gchar *command;
37 };
38 
39 enum {
40   PROP_0,
41   PROP_ACCEL,
42   PROP_ACTION,
43   PROP_COMMAND,
44   PROP_SHOW_ACCEL,
45   PROP_TITLE,
46   N_PROPS
47 };
48 
G_DEFINE_TYPE(DzlShortcutSimpleLabel,dzl_shortcut_simple_label,GTK_TYPE_BOX)49 G_DEFINE_TYPE (DzlShortcutSimpleLabel, dzl_shortcut_simple_label, GTK_TYPE_BOX)
50 
51 static GParamSpec *properties [N_PROPS];
52 
53 static void
54 dzl_shortcut_simple_label_get_property (GObject    *object,
55                                         guint       prop_id,
56                                         GValue     *value,
57                                         GParamSpec *pspec)
58 {
59   DzlShortcutSimpleLabel *self = DZL_SHORTCUT_SIMPLE_LABEL (object);
60 
61   switch (prop_id)
62     {
63     case PROP_ACCEL:
64       g_value_set_static_string (value, dzl_shortcut_simple_label_get_accel (self));
65       break;
66 
67     case PROP_ACTION:
68       g_value_set_static_string (value, dzl_shortcut_simple_label_get_action (self));
69       break;
70 
71     case PROP_COMMAND:
72       g_value_set_static_string (value, dzl_shortcut_simple_label_get_command (self));
73       break;
74 
75     case PROP_SHOW_ACCEL:
76       g_object_get_property (G_OBJECT (self->accel_label), "visible", value);
77       break;
78 
79     case PROP_TITLE:
80       g_value_set_string (value, dzl_shortcut_simple_label_get_title (self));
81       break;
82 
83     default:
84       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
85     }
86 }
87 
88 static void
dzl_shortcut_simple_label_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)89 dzl_shortcut_simple_label_set_property (GObject      *object,
90                                         guint         prop_id,
91                                         const GValue *value,
92                                         GParamSpec   *pspec)
93 {
94   DzlShortcutSimpleLabel *self = DZL_SHORTCUT_SIMPLE_LABEL (object);
95 
96   switch (prop_id)
97     {
98     case PROP_ACCEL:
99       dzl_shortcut_simple_label_set_accel (self, g_value_get_string (value));
100       break;
101 
102     case PROP_ACTION:
103       dzl_shortcut_simple_label_set_action (self, g_value_get_string (value));
104       break;
105 
106     case PROP_COMMAND:
107       dzl_shortcut_simple_label_set_command (self, g_value_get_string (value));
108       break;
109 
110     case PROP_SHOW_ACCEL:
111       g_object_set_property (G_OBJECT (self->accel_label), "visible", value);
112       break;
113 
114     case PROP_TITLE:
115       dzl_shortcut_simple_label_set_title (self, g_value_get_string (value));
116       break;
117 
118     default:
119       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
120     }
121 }
122 
123 static void
dzl_shortcut_simple_label_class_init(DzlShortcutSimpleLabelClass * klass)124 dzl_shortcut_simple_label_class_init (DzlShortcutSimpleLabelClass *klass)
125 {
126   GObjectClass *object_class = G_OBJECT_CLASS (klass);
127 
128   object_class->get_property = dzl_shortcut_simple_label_get_property;
129   object_class->set_property = dzl_shortcut_simple_label_set_property;
130 
131   properties [PROP_ACTION] =
132     g_param_spec_string ("action",
133                          "Action",
134                          "Action",
135                          NULL,
136                          (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
137 
138   properties [PROP_ACCEL] =
139     g_param_spec_string ("accel",
140                          "Accel",
141                          "The accel label to override the discovered accel",
142                          NULL,
143                          (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
144 
145   properties [PROP_COMMAND] =
146     g_param_spec_string ("command",
147                          "Command",
148                          "Command",
149                          NULL,
150                          (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
151 
152   properties [PROP_SHOW_ACCEL] =
153     g_param_spec_boolean ("show-accel", NULL, NULL,
154                           TRUE,
155                           (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
156 
157   properties [PROP_TITLE] =
158     g_param_spec_string ("title",
159                          "Title",
160                          "Title",
161                          NULL,
162                          (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
163 
164   g_object_class_install_properties (object_class, N_PROPS, properties);
165 }
166 
167 static void
dzl_shortcut_simple_label_init(DzlShortcutSimpleLabel * self)168 dzl_shortcut_simple_label_init (DzlShortcutSimpleLabel *self)
169 {
170   self->title = g_object_new (GTK_TYPE_LABEL,
171                               "visible", TRUE,
172                               "use-underline", TRUE,
173                               "xalign", 0.0f,
174                               NULL);
175   dzl_gtk_widget_add_style_class (GTK_WIDGET (self->title), "title");
176   gtk_container_add_with_properties (GTK_CONTAINER (self), GTK_WIDGET (self->title),
177                                      "fill", TRUE,
178                                      "pack-type", GTK_PACK_START,
179                                      NULL);
180 
181   self->accel_label = g_object_new (GTK_TYPE_LABEL,
182                                     "hexpand", TRUE,
183                                     "halign", GTK_ALIGN_END,
184                                     "margin-start", 12,
185                                     "visible", TRUE,
186                                     "xalign", 1.0f,
187                                     NULL);
188   dzl_gtk_widget_add_style_class (GTK_WIDGET (self->accel_label), "dim-label");
189   gtk_container_add_with_properties (GTK_CONTAINER (self), GTK_WIDGET (self->accel_label),
190                                      "fill", TRUE,
191                                      "pack-type", GTK_PACK_END,
192                                      NULL);
193 }
194 
195 GtkWidget *
dzl_shortcut_simple_label_new(void)196 dzl_shortcut_simple_label_new (void)
197 {
198   return g_object_new (DZL_TYPE_SHORTCUT_SIMPLE_LABEL, NULL);
199 }
200 
201 const gchar *
dzl_shortcut_simple_label_get_accel(DzlShortcutSimpleLabel * self)202 dzl_shortcut_simple_label_get_accel (DzlShortcutSimpleLabel *self)
203 {
204   g_return_val_if_fail (DZL_IS_SHORTCUT_SIMPLE_LABEL (self), NULL);
205 
206   return self->accel;
207 }
208 
209 const gchar *
dzl_shortcut_simple_label_get_action(DzlShortcutSimpleLabel * self)210 dzl_shortcut_simple_label_get_action (DzlShortcutSimpleLabel *self)
211 {
212   g_return_val_if_fail (DZL_IS_SHORTCUT_SIMPLE_LABEL (self), NULL);
213 
214   return self->action;
215 }
216 
217 const gchar *
dzl_shortcut_simple_label_get_command(DzlShortcutSimpleLabel * self)218 dzl_shortcut_simple_label_get_command (DzlShortcutSimpleLabel *self)
219 {
220   g_return_val_if_fail (DZL_IS_SHORTCUT_SIMPLE_LABEL (self), NULL);
221 
222   return self->command;
223 }
224 
225 const gchar *
dzl_shortcut_simple_label_get_title(DzlShortcutSimpleLabel * self)226 dzl_shortcut_simple_label_get_title (DzlShortcutSimpleLabel *self)
227 {
228   g_return_val_if_fail (DZL_IS_SHORTCUT_SIMPLE_LABEL (self), NULL);
229 
230   return gtk_label_get_label (self->title);
231 }
232 
233 void
dzl_shortcut_simple_label_set_accel(DzlShortcutSimpleLabel * self,const gchar * accel)234 dzl_shortcut_simple_label_set_accel (DzlShortcutSimpleLabel *self,
235                                      const gchar            *accel)
236 {
237   g_return_if_fail (DZL_IS_SHORTCUT_SIMPLE_LABEL (self));
238 
239   accel = g_intern_string (accel);
240 
241   if (accel != self->accel)
242     {
243       g_autofree gchar *label = NULL;
244 
245       self->accel = accel;
246 
247       if (accel != NULL)
248         {
249           g_autoptr(DzlShortcutChord) chord = NULL;
250 
251           chord = dzl_shortcut_chord_new_from_string (accel);
252           label = dzl_shortcut_chord_get_label (chord);
253         }
254 
255       gtk_label_set_label (self->accel_label, label);
256       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ACCEL]);
257     }
258 }
259 
260 void
dzl_shortcut_simple_label_set_action(DzlShortcutSimpleLabel * self,const gchar * action)261 dzl_shortcut_simple_label_set_action (DzlShortcutSimpleLabel *self,
262                                       const gchar            *action)
263 {
264   g_return_if_fail (DZL_IS_SHORTCUT_SIMPLE_LABEL (self));
265 
266   action = g_intern_string (action);
267 
268   if (action != self->action)
269     {
270       self->action = action;
271       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ACTION]);
272     }
273 }
274 
275 void
dzl_shortcut_simple_label_set_command(DzlShortcutSimpleLabel * self,const gchar * command)276 dzl_shortcut_simple_label_set_command (DzlShortcutSimpleLabel *self,
277                                        const gchar            *command)
278 {
279   g_return_if_fail (DZL_IS_SHORTCUT_SIMPLE_LABEL (self));
280 
281   command = g_intern_string (command);
282 
283   if (command != self->command)
284     {
285       self->command = command;
286       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_COMMAND]);
287     }
288 }
289 
290 void
dzl_shortcut_simple_label_set_title(DzlShortcutSimpleLabel * self,const gchar * title)291 dzl_shortcut_simple_label_set_title (DzlShortcutSimpleLabel *self,
292                                      const gchar            *title)
293 {
294   g_return_if_fail (DZL_IS_SHORTCUT_SIMPLE_LABEL (self));
295 
296   gtk_label_set_label (self->title, title);
297   g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_TITLE]);
298 }
299 
300 void
_dzl_shortcut_simple_label_set_size_group(DzlShortcutSimpleLabel * self,GtkSizeGroup * size_group)301 _dzl_shortcut_simple_label_set_size_group(DzlShortcutSimpleLabel *self,
302                                           GtkSizeGroup           *size_group)
303 {
304   g_return_if_fail (DZL_IS_SHORTCUT_SIMPLE_LABEL (self));
305 
306   if (size_group != NULL)
307     gtk_size_group_add_widget (size_group, GTK_WIDGET (self->title));
308 }
309