]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-cell-renderer-activatable.c
Merge branch 'gnome-3-8'
[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 #include "empathy-cell-renderer-activatable.h"
25
26 #include "empathy-utils.h"
27
28 enum {
29   PATH_ACTIVATED,
30   LAST_SIGNAL
31 };
32
33 enum {
34   PROP_SHOW_ON_SELECT = 1
35 };
36
37 typedef struct {
38   gboolean show_on_select;
39 } EmpathyCellRendererActivatablePriv;
40
41 static guint signals[LAST_SIGNAL];
42
43 G_DEFINE_TYPE (EmpathyCellRendererActivatable,
44     empathy_cell_renderer_activatable, GTK_TYPE_CELL_RENDERER_PIXBUF)
45
46 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCellRendererActivatable)
47
48 static void
49 empathy_cell_renderer_activatable_init (EmpathyCellRendererActivatable *cell)
50 {
51   cell->priv = G_TYPE_INSTANCE_GET_PRIVATE (cell,
52       EMPATHY_TYPE_CELL_RENDERER_ACTIVATABLE,
53       EmpathyCellRendererActivatablePriv);
54
55   g_object_set (cell,
56       "xpad", 0,
57       "ypad", 0,
58       "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE,
59       "follow-state", TRUE,
60       NULL);
61 }
62
63 static void
64 cell_renderer_activatable_get_property (GObject *object,
65     guint prop_id,
66     GValue *value,
67     GParamSpec *pspec)
68 {
69   EmpathyCellRendererActivatablePriv *priv = GET_PRIV (object);
70
71   switch (prop_id)
72     {
73       case PROP_SHOW_ON_SELECT:
74         g_value_set_boolean (value, priv->show_on_select);
75         break;
76       default:
77         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
78         break;
79     }
80 }
81
82 static void
83 cell_renderer_activatable_set_property (GObject *object,
84     guint prop_id,
85     const GValue *value,
86     GParamSpec *pspec)
87 {
88   EmpathyCellRendererActivatablePriv *priv = GET_PRIV (object);
89
90   switch (prop_id)
91     {
92       case PROP_SHOW_ON_SELECT:
93         priv->show_on_select = g_value_get_boolean (value);
94         break;
95       default:
96         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97         break;
98     }
99 }
100
101 GtkCellRenderer *
102 empathy_cell_renderer_activatable_new (void)
103 {
104   return g_object_new (EMPATHY_TYPE_CELL_RENDERER_ACTIVATABLE, NULL);
105 }
106
107 static gboolean
108 cell_renderer_activatable_activate (GtkCellRenderer      *cell,
109     GdkEvent *event,
110     GtkWidget *widget,
111     const gchar *path_string,
112     const GdkRectangle *background_area,
113     const GdkRectangle *cell_area,
114     GtkCellRendererState  flags)
115 {
116   EmpathyCellRendererActivatable *activatable;
117   gint ex, ey, bx, by, bw, bh;
118
119   activatable = EMPATHY_CELL_RENDERER_ACTIVATABLE (cell);
120
121   if (!GTK_IS_TREE_VIEW (widget) || event == NULL ||
122       event->type != GDK_BUTTON_PRESS) {
123     return FALSE;
124   }
125
126   ex  = (gint) ((GdkEventButton *) event)->x;
127   ey  = (gint) ((GdkEventButton *) event)->y;
128   bx = background_area->x;
129   by = background_area->y;
130   bw = background_area->width;
131   bh = background_area->height;
132
133   if (ex < bx || ex > (bx+bw) || ey < by || ey > (by+bh)){
134     /* Click wasn't on the icon */
135     return FALSE;
136   }
137
138   g_signal_emit (activatable, signals[PATH_ACTIVATED], 0, path_string);
139
140   return TRUE;
141 }
142
143 static void
144 cell_renderer_activatable_render (
145     GtkCellRenderer *cell,
146     cairo_t *cr,
147     GtkWidget *widget,
148     const GdkRectangle *background_area,
149     const GdkRectangle *cell_area,
150     GtkCellRendererState flags)
151 {
152   EmpathyCellRendererActivatablePriv *priv = GET_PRIV (cell);
153
154   if (priv->show_on_select && !(flags & (GTK_CELL_RENDERER_SELECTED)))
155     return;
156
157   GTK_CELL_RENDERER_CLASS
158     (empathy_cell_renderer_activatable_parent_class)->render (
159         cell, cr, widget, background_area, cell_area, flags);
160 }
161
162 static void
163 empathy_cell_renderer_activatable_class_init (
164     EmpathyCellRendererActivatableClass *klass)
165 {
166   GtkCellRendererClass *cell_class;
167   GObjectClass *oclass;
168
169   oclass = G_OBJECT_CLASS (klass);
170   oclass->get_property = cell_renderer_activatable_get_property;
171   oclass->set_property = cell_renderer_activatable_set_property;
172
173   cell_class = GTK_CELL_RENDERER_CLASS (klass);
174   cell_class->activate = cell_renderer_activatable_activate;
175   cell_class->render = cell_renderer_activatable_render;
176
177   signals[PATH_ACTIVATED] =
178     g_signal_new ("path-activated",
179         G_TYPE_FROM_CLASS (klass),
180         G_SIGNAL_RUN_LAST,
181         0,
182         NULL, NULL,
183         g_cclosure_marshal_generic,
184         G_TYPE_NONE,
185         1, G_TYPE_STRING);
186
187   g_object_class_install_property (oclass, PROP_SHOW_ON_SELECT,
188       g_param_spec_boolean ("show-on-select",
189           "Show on select",
190           "Whether the cell renderer should be shown only when it's selected",
191           FALSE,
192           G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
193
194   g_type_class_add_private (klass,
195       sizeof (EmpathyCellRendererActivatablePriv));
196 }