]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-cell-renderer-activatable.c
6a8fe72eeed55e3d72a0b03b8f9a20bb605372f3
[empathy.git] / libempathy-gtk / empathy-cell-renderer-activatable.c
1 /*
2  * Copyright (C) 2007 Raphael Slinckx <raphael@slinckx.net>
3  * Copyright (C) 2007-2009 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Raphael Slinckx <raphael@slinckx.net>
20  *          Cosimo Cecchi   <cosimo.cecchi@collabora.co.uk>
21  */
22
23 #include "config.h"
24
25 #include "empathy-utils.h"
26
27 #include "empathy-cell-renderer-activatable.h"
28
29 enum {
30   PATH_ACTIVATED,
31   LAST_SIGNAL
32 };
33
34 enum {
35   PROP_SHOW_ON_SELECT = 1
36 };
37
38 typedef struct {
39   gboolean show_on_select;
40 } EmpathyCellRendererActivatablePriv;
41
42 static guint signals[LAST_SIGNAL];
43
44 G_DEFINE_TYPE (EmpathyCellRendererActivatable,
45     empathy_cell_renderer_activatable, GTK_TYPE_CELL_RENDERER_PIXBUF)
46
47 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCellRendererActivatable)
48
49 static void
50 empathy_cell_renderer_activatable_init (EmpathyCellRendererActivatable *cell)
51 {
52   cell->priv = G_TYPE_INSTANCE_GET_PRIVATE (cell,
53       EMPATHY_TYPE_CELL_RENDERER_ACTIVATABLE,
54       EmpathyCellRendererActivatablePriv);
55
56   g_object_set (cell,
57       "xpad", 0,
58       "ypad", 0,
59       "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE,
60       "follow-state", TRUE,
61       NULL);
62 }
63
64 static void
65 cell_renderer_activatable_get_property (GObject *object,
66     guint prop_id,
67     GValue *value,
68     GParamSpec *pspec)
69 {
70   EmpathyCellRendererActivatablePriv *priv = GET_PRIV (object);
71
72   switch (prop_id)
73     {
74       case PROP_SHOW_ON_SELECT:
75         g_value_set_boolean (value, priv->show_on_select);
76         break;
77       default:
78         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
79         break;
80     }
81 }
82
83 static void
84 cell_renderer_activatable_set_property (GObject *object,
85     guint prop_id,
86     const GValue *value,
87     GParamSpec *pspec)
88 {
89   EmpathyCellRendererActivatablePriv *priv = GET_PRIV (object);
90
91   switch (prop_id)
92     {
93       case PROP_SHOW_ON_SELECT:
94         priv->show_on_select = g_value_get_boolean (value);
95         break;
96       default:
97         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
98         break;
99     }
100 }
101
102 GtkCellRenderer *
103 empathy_cell_renderer_activatable_new (void)
104 {
105   return g_object_new (EMPATHY_TYPE_CELL_RENDERER_ACTIVATABLE, NULL);
106 }
107
108 static gboolean
109 cell_renderer_activatable_activate (GtkCellRenderer      *cell,
110     GdkEvent *event,
111     GtkWidget *widget,
112     const gchar *path_string,
113     const GdkRectangle *background_area,
114     const GdkRectangle *cell_area,
115     GtkCellRendererState  flags)
116 {
117   EmpathyCellRendererActivatable *activatable;
118   gint ex, ey, bx, by, bw, bh;
119
120   activatable = EMPATHY_CELL_RENDERER_ACTIVATABLE (cell);
121
122   if (!GTK_IS_TREE_VIEW (widget) || event == NULL ||
123       event->type != GDK_BUTTON_PRESS) {
124     return FALSE;
125   }
126
127   ex  = (gint) ((GdkEventButton *) event)->x;
128   ey  = (gint) ((GdkEventButton *) event)->y;
129   bx = background_area->x;
130   by = background_area->y;
131   bw = background_area->width;
132   bh = background_area->height;
133
134   if (ex < bx || ex > (bx+bw) || ey < by || ey > (by+bh)){
135     /* Click wasn't on the icon */
136     return FALSE;
137   }
138
139   g_signal_emit (activatable, signals[PATH_ACTIVATED], 0, path_string);
140
141   return TRUE;
142 }
143
144 static void
145 cell_renderer_activatable_render (
146     GtkCellRenderer *cell,
147     cairo_t *cr,
148     GtkWidget *widget,
149     const GdkRectangle *background_area,
150     const GdkRectangle *cell_area,
151     GtkCellRendererState flags)
152 {
153   EmpathyCellRendererActivatablePriv *priv = GET_PRIV (cell);
154
155   if (priv->show_on_select && !(flags & (GTK_CELL_RENDERER_SELECTED)))
156     return;
157
158   GTK_CELL_RENDERER_CLASS
159     (empathy_cell_renderer_activatable_parent_class)->render (
160         cell, cr, widget, background_area, cell_area, flags);
161 }
162
163 static void
164 empathy_cell_renderer_activatable_class_init (
165     EmpathyCellRendererActivatableClass *klass)
166 {
167   GtkCellRendererClass *cell_class;
168   GObjectClass *oclass;
169
170   oclass = G_OBJECT_CLASS (klass);
171   oclass->get_property = cell_renderer_activatable_get_property;
172   oclass->set_property = cell_renderer_activatable_set_property;
173
174   cell_class = GTK_CELL_RENDERER_CLASS (klass);
175   cell_class->activate = cell_renderer_activatable_activate;
176   cell_class->render = cell_renderer_activatable_render;
177
178   signals[PATH_ACTIVATED] =
179     g_signal_new ("path-activated",
180         G_TYPE_FROM_CLASS (klass),
181         G_SIGNAL_RUN_LAST,
182         0,
183         NULL, NULL,
184         g_cclosure_marshal_generic,
185         G_TYPE_NONE,
186         1, G_TYPE_STRING);
187
188   g_object_class_install_property (oclass, PROP_SHOW_ON_SELECT,
189       g_param_spec_boolean ("show-on-select",
190           "Show on select",
191           "Whether the cell renderer should be shown only when it's selected",
192           FALSE,
193           G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
194
195   g_type_class_add_private (klass,
196       sizeof (EmpathyCellRendererActivatablePriv));
197 }