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