1 /*
2  * Copyright (c) 2016, 2017 Red Hat, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
12  * License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Author: Debarshi Ray <debarshir@gnome.org>
19  *
20  */
21 
22 #include "gd-main-box-child.h"
23 #include "gd-main-icon-box-child.h"
24 #include "gd-main-icon-box-icon.h"
25 
26 #include <gio/gio.h>
27 #include <glib.h>
28 
29 typedef struct _GdMainIconBoxChildPrivate GdMainIconBoxChildPrivate;
30 
31 struct _GdMainIconBoxChildPrivate
32 {
33   GdMainBoxItem *item;
34   GtkWidget *check_button;
35   gboolean selection_mode;
36   gboolean show_primary_text;
37   gboolean show_secondary_text;
38 };
39 
40 enum
41 {
42   PROP_ITEM = 1,
43   PROP_SELECTION_MODE,
44   PROP_SHOW_PRIMARY_TEXT,
45   PROP_SHOW_SECONDARY_TEXT,
46   NUM_PROPERTIES
47 };
48 
49 static void gd_main_box_child_interface_init (GdMainBoxChildInterface *iface);
G_DEFINE_TYPE_WITH_CODE(GdMainIconBoxChild,gd_main_icon_box_child,GTK_TYPE_FLOW_BOX_CHILD,G_ADD_PRIVATE (GdMainIconBoxChild)G_IMPLEMENT_INTERFACE (GD_TYPE_MAIN_BOX_CHILD,gd_main_box_child_interface_init))50 G_DEFINE_TYPE_WITH_CODE (GdMainIconBoxChild, gd_main_icon_box_child, GTK_TYPE_FLOW_BOX_CHILD,
51                          G_ADD_PRIVATE (GdMainIconBoxChild)
52                          G_IMPLEMENT_INTERFACE (GD_TYPE_MAIN_BOX_CHILD, gd_main_box_child_interface_init))
53 
54 static gboolean
55 gd_main_icon_box_child_bind_text_to_visible (GBinding *binding,
56                                              const GValue *from_value,
57                                              GValue *to_value,
58                                              gpointer user_data)
59 {
60   gboolean to_visible;
61   const gchar *from_text;
62 
63   from_text = g_value_get_string (from_value);
64   to_visible = from_text != NULL && from_text[0] != '\0' ? TRUE : FALSE;
65   g_value_set_boolean (to_value, to_visible);
66   return TRUE;
67 }
68 
69 static void
gd_main_icon_box_check_button_toggled(GdMainIconBoxChild * self)70 gd_main_icon_box_check_button_toggled (GdMainIconBoxChild *self)
71 {
72   GdMainIconBoxChildPrivate *priv;
73   GtkWidget *parent;
74 
75   priv = gd_main_icon_box_child_get_instance_private (self);
76 
77   parent = gtk_widget_get_parent (GTK_WIDGET (self));
78   if (!GTK_IS_FLOW_BOX (parent))
79     return;
80 
81   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->check_button)))
82     gtk_flow_box_select_child (GTK_FLOW_BOX (parent), GTK_FLOW_BOX_CHILD (self));
83   else
84     gtk_flow_box_unselect_child (GTK_FLOW_BOX (parent), GTK_FLOW_BOX_CHILD (self));
85 }
86 
87 static void
gd_main_icon_box_child_update_layout(GdMainIconBoxChild * self)88 gd_main_icon_box_child_update_layout (GdMainIconBoxChild *self)
89 {
90   GdMainIconBoxChildPrivate *priv;
91   GtkWidget *grid;
92   GtkWidget *icon;
93   GtkWidget *overlay;
94 
95   priv = gd_main_icon_box_child_get_instance_private (self);
96 
97   gtk_container_foreach (GTK_CONTAINER (self), (GtkCallback) gtk_widget_destroy, NULL);
98 
99   grid = gtk_grid_new ();
100   gtk_widget_set_valign (grid, GTK_ALIGN_CENTER);
101   gtk_orientable_set_orientation (GTK_ORIENTABLE (grid), GTK_ORIENTATION_VERTICAL);
102   gtk_container_add (GTK_CONTAINER (self), grid);
103 
104   overlay = gtk_overlay_new ();
105   gtk_container_add (GTK_CONTAINER (grid), overlay);
106 
107   icon = gd_main_icon_box_icon_new (priv->item);
108   gtk_widget_set_hexpand (icon, TRUE);
109   gtk_container_add (GTK_CONTAINER (overlay), icon);
110 
111   if (gd_main_box_item_get_pulse (priv->item))
112     {
113       GtkWidget *spinner;
114 
115       spinner = gtk_spinner_new ();
116       gtk_widget_set_halign (spinner, GTK_ALIGN_CENTER);
117       gtk_widget_set_size_request (spinner, 32, 32);
118       gtk_widget_set_valign (spinner, GTK_ALIGN_CENTER);
119       gtk_spinner_start (GTK_SPINNER (spinner));
120       gtk_overlay_add_overlay (GTK_OVERLAY (overlay), spinner);
121     }
122 
123   priv->check_button = gtk_check_button_new ();
124   gtk_widget_set_can_focus (priv->check_button, FALSE);
125   gtk_widget_set_halign (priv->check_button, GTK_ALIGN_END);
126   gtk_widget_set_valign (priv->check_button, GTK_ALIGN_END);
127   gtk_widget_set_no_show_all (priv->check_button, TRUE);
128   g_object_bind_property (self, "selection-mode", priv->check_button, "visible", G_BINDING_SYNC_CREATE);
129   gtk_overlay_add_overlay (GTK_OVERLAY (overlay), priv->check_button);
130   g_signal_connect_swapped (priv->check_button,
131                             "toggled",
132                             G_CALLBACK (gd_main_icon_box_check_button_toggled),
133                             self);
134 
135   if (priv->show_primary_text)
136     {
137       GtkWidget *primary_label;
138 
139       primary_label = gtk_label_new (NULL);
140       gtk_widget_set_no_show_all (primary_label, TRUE);
141       gtk_label_set_ellipsize (GTK_LABEL (primary_label), PANGO_ELLIPSIZE_MIDDLE);
142       g_object_bind_property (priv->item, "primary-text", primary_label, "label", G_BINDING_SYNC_CREATE);
143       g_object_bind_property_full (priv->item,
144                                    "primary-text",
145                                    primary_label,
146                                    "visible",
147                                    G_BINDING_SYNC_CREATE,
148                                    gd_main_icon_box_child_bind_text_to_visible,
149                                    NULL,
150                                    NULL,
151                                    NULL);
152       gtk_container_add (GTK_CONTAINER (grid), primary_label);
153     }
154 
155   if (priv->show_secondary_text)
156     {
157       GtkStyleContext *context;
158       GtkWidget *secondary_label;
159 
160       secondary_label = gtk_label_new (NULL);
161       gtk_widget_set_no_show_all (secondary_label, TRUE);
162       gtk_label_set_ellipsize (GTK_LABEL (secondary_label), PANGO_ELLIPSIZE_END);
163       context = gtk_widget_get_style_context (secondary_label);
164       gtk_style_context_add_class (context, "dim-label");
165       g_object_bind_property (priv->item, "secondary-text", secondary_label, "label", G_BINDING_SYNC_CREATE);
166       g_object_bind_property_full (priv->item,
167                                    "secondary-text",
168                                    secondary_label,
169                                    "visible",
170                                    G_BINDING_SYNC_CREATE,
171                                    gd_main_icon_box_child_bind_text_to_visible,
172                                    NULL,
173                                    NULL,
174                                    NULL);
175       gtk_container_add (GTK_CONTAINER (grid), secondary_label);
176     }
177 
178   gtk_widget_show_all (grid);
179 }
180 
181 static void
gd_main_icon_box_child_notify_pulse(GdMainIconBoxChild * self)182 gd_main_icon_box_child_notify_pulse (GdMainIconBoxChild *self)
183 {
184   gd_main_icon_box_child_update_layout (self);
185 }
186 
187 static GdMainBoxItem *
gd_main_icon_box_child_get_item(GdMainBoxChild * child)188 gd_main_icon_box_child_get_item (GdMainBoxChild *child)
189 {
190   GdMainIconBoxChild *self = GD_MAIN_ICON_BOX_CHILD (child);
191   GdMainIconBoxChildPrivate *priv;
192 
193   priv = gd_main_icon_box_child_get_instance_private (self);
194   return priv->item;
195 }
196 
197 static gint
gd_main_icon_box_child_get_index(GdMainBoxChild * child)198 gd_main_icon_box_child_get_index (GdMainBoxChild *child)
199 {
200   GdMainIconBoxChild *self = GD_MAIN_ICON_BOX_CHILD (child);
201   gint index;
202 
203   index = gtk_flow_box_child_get_index (GTK_FLOW_BOX_CHILD (self));
204   return index;
205 }
206 
207 static gboolean
gd_main_icon_box_child_get_selected(GdMainBoxChild * child)208 gd_main_icon_box_child_get_selected (GdMainBoxChild *child)
209 {
210   GdMainIconBoxChild *self = GD_MAIN_ICON_BOX_CHILD (child);
211   gboolean selected;
212 
213   selected = gtk_flow_box_child_is_selected (GTK_FLOW_BOX_CHILD (self));
214   return selected;
215 }
216 
217 static gboolean
gd_main_icon_box_child_get_selection_mode(GdMainIconBoxChild * self)218 gd_main_icon_box_child_get_selection_mode (GdMainIconBoxChild *self)
219 {
220   GdMainIconBoxChildPrivate *priv;
221 
222   priv = gd_main_icon_box_child_get_instance_private (self);
223   return priv->selection_mode;
224 }
225 
226 static gboolean
gd_main_icon_box_child_get_show_primary_text(GdMainIconBoxChild * self)227 gd_main_icon_box_child_get_show_primary_text (GdMainIconBoxChild *self)
228 {
229   GdMainIconBoxChildPrivate *priv;
230 
231   priv = gd_main_icon_box_child_get_instance_private (self);
232   return priv->show_primary_text;
233 }
234 
235 static gboolean
gd_main_icon_box_child_get_show_secondary_text(GdMainIconBoxChild * self)236 gd_main_icon_box_child_get_show_secondary_text (GdMainIconBoxChild *self)
237 {
238   GdMainIconBoxChildPrivate *priv;
239 
240   priv = gd_main_icon_box_child_get_instance_private (self);
241   return priv->show_secondary_text;
242 }
243 
244 static void
gd_main_icon_box_child_set_item(GdMainIconBoxChild * self,GdMainBoxItem * item)245 gd_main_icon_box_child_set_item (GdMainIconBoxChild *self, GdMainBoxItem *item)
246 {
247   GdMainIconBoxChildPrivate *priv;
248 
249   priv = gd_main_icon_box_child_get_instance_private (self);
250 
251   if (priv->item != NULL)
252     g_signal_handlers_disconnect_by_func (priv->item, gd_main_icon_box_child_notify_pulse, self);
253 
254   if (!g_set_object (&priv->item, item))
255     return;
256 
257   if (priv->item != NULL)
258     {
259       g_signal_connect_object (priv->item,
260                                "notify::pulse",
261                                G_CALLBACK (gd_main_icon_box_child_notify_pulse),
262                                self,
263                                G_CONNECT_SWAPPED);
264     }
265 
266   g_object_notify (G_OBJECT (self), "item");
267   gtk_widget_queue_draw (GTK_WIDGET (self));
268 }
269 
270 static void
gd_main_icon_box_child_set_selected(GdMainBoxChild * child,gboolean selected)271 gd_main_icon_box_child_set_selected (GdMainBoxChild *child, gboolean selected)
272 {
273   GdMainIconBoxChild *self = GD_MAIN_ICON_BOX_CHILD (child);
274   GdMainIconBoxChildPrivate *priv;
275 
276   priv = gd_main_icon_box_child_get_instance_private (self);
277   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->check_button), selected);
278 }
279 
280 static void
gd_main_icon_box_child_set_selection_mode(GdMainIconBoxChild * self,gboolean selection_mode)281 gd_main_icon_box_child_set_selection_mode (GdMainIconBoxChild *self, gboolean selection_mode)
282 {
283   GdMainIconBoxChildPrivate *priv;
284 
285   priv = gd_main_icon_box_child_get_instance_private (self);
286 
287   if (priv->selection_mode == selection_mode)
288     return;
289 
290   priv->selection_mode = selection_mode;
291   g_object_notify (G_OBJECT (self), "selection-mode");
292   gtk_widget_queue_draw (GTK_WIDGET (self));
293 }
294 
295 static void
gd_main_icon_box_child_set_show_primary_text(GdMainIconBoxChild * self,gboolean show_primary_text)296 gd_main_icon_box_child_set_show_primary_text (GdMainIconBoxChild *self, gboolean show_primary_text)
297 {
298   GdMainIconBoxChildPrivate *priv;
299 
300   priv = gd_main_icon_box_child_get_instance_private (self);
301 
302   if (priv->show_primary_text == show_primary_text)
303     return;
304 
305   priv->show_primary_text = show_primary_text;
306   gd_main_icon_box_child_update_layout (self);
307   g_object_notify (G_OBJECT (self), "show-primary-text");
308 }
309 
310 static void
gd_main_icon_box_child_set_show_secondary_text(GdMainIconBoxChild * self,gboolean show_secondary_text)311 gd_main_icon_box_child_set_show_secondary_text (GdMainIconBoxChild *self, gboolean show_secondary_text)
312 {
313   GdMainIconBoxChildPrivate *priv;
314 
315   priv = gd_main_icon_box_child_get_instance_private (self);
316 
317   if (priv->show_secondary_text == show_secondary_text)
318     return;
319 
320   priv->show_secondary_text = show_secondary_text;
321   gd_main_icon_box_child_update_layout (self);
322   g_object_notify (G_OBJECT (self), "show-secondary-text");
323 }
324 
325 static void
gd_main_icon_box_child_constructed(GObject * obj)326 gd_main_icon_box_child_constructed (GObject *obj)
327 {
328   GdMainIconBoxChild *self = GD_MAIN_ICON_BOX_CHILD (obj);
329 
330   G_OBJECT_CLASS (gd_main_icon_box_child_parent_class)->constructed (obj);
331 
332   gd_main_icon_box_child_update_layout (self);
333 }
334 
335 static void
gd_main_icon_box_child_dispose(GObject * obj)336 gd_main_icon_box_child_dispose (GObject *obj)
337 {
338   GdMainIconBoxChild *self = GD_MAIN_ICON_BOX_CHILD (obj);
339   GdMainIconBoxChildPrivate *priv;
340 
341   priv = gd_main_icon_box_child_get_instance_private (self);
342 
343   g_clear_object (&priv->item);
344 
345   G_OBJECT_CLASS (gd_main_icon_box_child_parent_class)->dispose (obj);
346 }
347 
348 static void
gd_main_icon_box_child_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)349 gd_main_icon_box_child_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
350 {
351   GdMainIconBoxChild *self = GD_MAIN_ICON_BOX_CHILD (object);
352 
353   switch (property_id)
354     {
355     case PROP_ITEM:
356       g_value_set_object (value, gd_main_icon_box_child_get_item (GD_MAIN_BOX_CHILD (self)));
357       break;
358     case PROP_SELECTION_MODE:
359       g_value_set_boolean (value, gd_main_icon_box_child_get_selection_mode (self));
360       break;
361     case PROP_SHOW_PRIMARY_TEXT:
362       g_value_set_boolean (value, gd_main_icon_box_child_get_show_primary_text (self));
363       break;
364     case PROP_SHOW_SECONDARY_TEXT:
365       g_value_set_boolean (value, gd_main_icon_box_child_get_show_secondary_text (self));
366       break;
367     default:
368       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
369       break;
370     }
371 }
372 
373 static void
gd_main_icon_box_child_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)374 gd_main_icon_box_child_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
375 {
376   GdMainIconBoxChild *self = GD_MAIN_ICON_BOX_CHILD (object);
377 
378   switch (property_id)
379     {
380     case PROP_ITEM:
381       gd_main_icon_box_child_set_item (self, g_value_get_object (value));
382       break;
383     case PROP_SELECTION_MODE:
384       gd_main_icon_box_child_set_selection_mode (self, g_value_get_boolean (value));
385       break;
386     case PROP_SHOW_PRIMARY_TEXT:
387       gd_main_icon_box_child_set_show_primary_text (self, g_value_get_boolean (value));
388       break;
389     case PROP_SHOW_SECONDARY_TEXT:
390       gd_main_icon_box_child_set_show_secondary_text (self, g_value_get_boolean (value));
391       break;
392     default:
393       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
394       break;
395     }
396 }
397 
398 static void
gd_main_icon_box_child_init(GdMainIconBoxChild * self)399 gd_main_icon_box_child_init (GdMainIconBoxChild *self)
400 {
401   GtkStyleContext *context;
402 
403   context = gtk_widget_get_style_context (GTK_WIDGET (self));
404   gtk_style_context_add_class (context, "tile");
405 }
406 
407 static void
gd_main_icon_box_child_class_init(GdMainIconBoxChildClass * klass)408 gd_main_icon_box_child_class_init (GdMainIconBoxChildClass *klass)
409 {
410   GObjectClass *oclass = G_OBJECT_CLASS (klass);
411 
412   oclass->constructed = gd_main_icon_box_child_constructed;
413   oclass->dispose = gd_main_icon_box_child_dispose;
414   oclass->get_property = gd_main_icon_box_child_get_property;
415   oclass->set_property = gd_main_icon_box_child_set_property;
416 
417   g_object_class_override_property (oclass, PROP_ITEM, "item");
418   g_object_class_override_property (oclass, PROP_SELECTION_MODE, "selection-mode");
419   g_object_class_override_property (oclass, PROP_SHOW_PRIMARY_TEXT, "show-primary-text");
420   g_object_class_override_property (oclass, PROP_SHOW_SECONDARY_TEXT, "show-secondary-text");
421 }
422 
423 static void
gd_main_box_child_interface_init(GdMainBoxChildInterface * iface)424 gd_main_box_child_interface_init (GdMainBoxChildInterface *iface)
425 {
426   iface->get_index = gd_main_icon_box_child_get_index;
427   iface->get_item = gd_main_icon_box_child_get_item;
428   iface->get_selected = gd_main_icon_box_child_get_selected;
429   iface->set_selected = gd_main_icon_box_child_set_selected;
430 }
431 
432 GtkWidget *
gd_main_icon_box_child_new(GdMainBoxItem * item,gboolean selection_mode)433 gd_main_icon_box_child_new (GdMainBoxItem *item, gboolean selection_mode)
434 {
435   return g_object_new (GD_TYPE_MAIN_ICON_BOX_CHILD,
436                        "item", item,
437                        "selection-mode", selection_mode,
438                        NULL);
439 }
440