1 /*
2  * Copyright (C) 2008 Tristan Van Berkom.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation; either version 2.1 of
7  * the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Authors:
19  *   Tristan Van Berkom <tvb@gnome.org>
20  */
21 
22 #include "config.h"
23 #include "glade-cell-renderer-icon.h"
24 #include "glade-marshallers.h"
25 
26 static void glade_cell_renderer_icon_get_property (GObject      *object,
27                                                    guint         param_id,
28                                                    GValue       *value,
29                                                    GParamSpec   *pspec);
30 static void glade_cell_renderer_icon_set_property (GObject      *object,
31                                                    guint         param_id,
32                                                    const GValue *value,
33                                                    GParamSpec   *pspec);
34 static gboolean glade_cell_renderer_icon_activate (GtkCellRenderer     *cell,
35                                                    GdkEvent            *event,
36                                                    GtkWidget           *widget,
37                                                    const gchar         *path,
38                                                    const GdkRectangle  *background_area,
39                                                    const GdkRectangle  *cell_area,
40                                                    GtkCellRendererState flags);
41 
42 struct _GladeCellRendererIconPrivate
43 {
44   guint active : 1;
45   guint activatable : 1;
46 };
47 
48 enum
49 {
50   ACTIVATE,
51   LAST_SIGNAL
52 };
53 
54 enum
55 {
56   PROP_0,
57   PROP_ACTIVATABLE,
58   PROP_ACTIVE,
59   N_PROPERTIES
60 };
61 
62 static GParamSpec *properties[N_PROPERTIES];
63 static guint icon_cell_signals[LAST_SIGNAL] = { 0 };
64 
65 
G_DEFINE_TYPE_WITH_PRIVATE(GladeCellRendererIcon,glade_cell_renderer_icon,GTK_TYPE_CELL_RENDERER_PIXBUF)66 G_DEFINE_TYPE_WITH_PRIVATE (GladeCellRendererIcon,
67                             glade_cell_renderer_icon,
68                             GTK_TYPE_CELL_RENDERER_PIXBUF)
69 
70 static void glade_cell_renderer_icon_init (GladeCellRendererIcon *cellicon)
71 {
72   cellicon->priv = glade_cell_renderer_icon_get_instance_private (cellicon);
73 
74   cellicon->priv->activatable = TRUE;
75   cellicon->priv->active = FALSE;
76 
77   g_object_set (G_OBJECT (cellicon), "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE,
78                 NULL);
79 }
80 
81 static void
glade_cell_renderer_icon_class_init(GladeCellRendererIconClass * class)82 glade_cell_renderer_icon_class_init (GladeCellRendererIconClass *class)
83 {
84   GObjectClass *object_class = G_OBJECT_CLASS (class);
85   GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
86 
87   object_class->get_property = glade_cell_renderer_icon_get_property;
88   object_class->set_property = glade_cell_renderer_icon_set_property;
89 
90   cell_class->activate = glade_cell_renderer_icon_activate;
91 
92   properties[PROP_ACTIVE] =
93     g_param_spec_boolean ("active", "Icon state",
94                           "The icon state of the button",
95                           FALSE,
96                           G_PARAM_READABLE | G_PARAM_WRITABLE);
97 
98   properties[PROP_ACTIVATABLE] =
99     g_param_spec_boolean ("activatable",
100                           "Activatable",
101                           "The icon button can be activated",
102                           TRUE,
103                           G_PARAM_READABLE | G_PARAM_WRITABLE);
104 
105   /* Install all properties */
106   g_object_class_install_properties (object_class, N_PROPERTIES, properties);
107 
108   icon_cell_signals[ACTIVATE] =
109       g_signal_new ("activate",
110                     G_OBJECT_CLASS_TYPE (object_class),
111                     G_SIGNAL_RUN_LAST,
112                     G_STRUCT_OFFSET (GladeCellRendererIconClass, activate),
113                     NULL, NULL,
114                     _glade_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
115 }
116 
117 static void
glade_cell_renderer_icon_get_property(GObject * object,guint param_id,GValue * value,GParamSpec * pspec)118 glade_cell_renderer_icon_get_property (GObject    *object,
119                                        guint       param_id,
120                                        GValue     *value,
121                                        GParamSpec *pspec)
122 {
123   GladeCellRendererIcon *cellicon = GLADE_CELL_RENDERER_ICON (object);
124 
125   switch (param_id)
126     {
127       case PROP_ACTIVE:
128         g_value_set_boolean (value, cellicon->priv->active);
129         break;
130       case PROP_ACTIVATABLE:
131         g_value_set_boolean (value, cellicon->priv->activatable);
132         break;
133       default:
134         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
135         break;
136     }
137 }
138 
139 
140 static void
glade_cell_renderer_icon_set_property(GObject * object,guint param_id,const GValue * value,GParamSpec * pspec)141 glade_cell_renderer_icon_set_property (GObject      *object,
142                                        guint         param_id,
143                                        const GValue *value,
144                                        GParamSpec   *pspec)
145 {
146   GladeCellRendererIcon *cellicon = GLADE_CELL_RENDERER_ICON (object);
147 
148   switch (param_id)
149     {
150       case PROP_ACTIVE:
151         cellicon->priv->active = g_value_get_boolean (value);
152         break;
153       case PROP_ACTIVATABLE:
154         cellicon->priv->activatable = g_value_get_boolean (value);
155         break;
156       default:
157         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
158         break;
159     }
160 }
161 
162 GtkCellRenderer *
glade_cell_renderer_icon_new(void)163 glade_cell_renderer_icon_new (void)
164 {
165   return g_object_new (GLADE_TYPE_CELL_RENDERER_ICON, NULL);
166 }
167 
168 static gint
glade_cell_renderer_icon_activate(GtkCellRenderer * cell,GdkEvent * event,GtkWidget * widget,const gchar * path,const GdkRectangle * background_area,const GdkRectangle * cell_area,GtkCellRendererState flags)169 glade_cell_renderer_icon_activate (GtkCellRenderer     *cell,
170                                    GdkEvent            *event,
171                                    GtkWidget           *widget,
172                                    const gchar         *path,
173                                    const GdkRectangle  *background_area,
174                                    const GdkRectangle  *cell_area,
175                                    GtkCellRendererState flags)
176 {
177   GladeCellRendererIcon *cellicon;
178 
179   cellicon = GLADE_CELL_RENDERER_ICON (cell);
180   if (cellicon->priv->activatable)
181     {
182       g_signal_emit (cell, icon_cell_signals[ACTIVATE], 0, path);
183       return TRUE;
184     }
185 
186   return FALSE;
187 }
188 
189 gboolean
glade_cell_renderer_icon_get_active(GladeCellRendererIcon * icon)190 glade_cell_renderer_icon_get_active (GladeCellRendererIcon *icon)
191 {
192   g_return_val_if_fail (GLADE_IS_CELL_RENDERER_ICON (icon), FALSE);
193 
194   return icon->priv->active;
195 }
196 
197 void
glade_cell_renderer_icon_set_active(GladeCellRendererIcon * icon,gboolean setting)198 glade_cell_renderer_icon_set_active (GladeCellRendererIcon *icon,
199                                      gboolean               setting)
200 {
201   g_return_if_fail (GLADE_IS_CELL_RENDERER_ICON (icon));
202 
203   if (icon->priv->active != setting)
204     {
205       icon->priv->active = setting ? TRUE : FALSE;
206       g_object_notify_by_pspec (G_OBJECT (icon), properties[PROP_ACTIVE]);
207     }
208 }
209 
210 gboolean
glade_cell_renderer_icon_get_activatable(GladeCellRendererIcon * icon)211 glade_cell_renderer_icon_get_activatable (GladeCellRendererIcon *icon)
212 {
213   g_return_val_if_fail (GLADE_IS_CELL_RENDERER_ICON (icon), FALSE);
214 
215   return icon->priv->activatable;
216 }
217 
218 void
glade_cell_renderer_icon_set_activatable(GladeCellRendererIcon * icon,gboolean setting)219 glade_cell_renderer_icon_set_activatable (GladeCellRendererIcon *icon,
220                                           gboolean               setting)
221 {
222   g_return_if_fail (GLADE_IS_CELL_RENDERER_ICON (icon));
223 
224   if (icon->priv->activatable != setting)
225     {
226       icon->priv->activatable = setting ? TRUE : FALSE;
227       g_object_notify_by_pspec (G_OBJECT (icon), properties[PROP_ACTIVATABLE]);
228     }
229 }
230