]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-cell-renderer-activatable.c
individual_view_drag_end: remove the auto scroll
[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     const GdkRectangle *background_area,
116     const 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     cairo_t *cr,
150     GtkWidget *widget,
151     const GdkRectangle *background_area,
152     const GdkRectangle *cell_area,
153     GtkCellRendererState flags)
154 {
155   EmpathyCellRendererActivatablePriv *priv = GET_PRIV (cell);
156
157   if (priv->show_on_select && !(flags & (GTK_CELL_RENDERER_SELECTED)))
158     return;
159
160   GTK_CELL_RENDERER_CLASS
161     (empathy_cell_renderer_activatable_parent_class)->render (
162         cell, cr, widget, background_area, cell_area, flags);
163 }
164
165 static void
166 empathy_cell_renderer_activatable_class_init (
167     EmpathyCellRendererActivatableClass *klass)
168 {
169   GtkCellRendererClass *cell_class;
170   GObjectClass *oclass;
171
172   oclass = G_OBJECT_CLASS (klass);
173   oclass->get_property = cell_renderer_activatable_get_property;
174   oclass->set_property = cell_renderer_activatable_set_property;
175
176   cell_class = GTK_CELL_RENDERER_CLASS (klass);
177   cell_class->activate = cell_renderer_activatable_activate;
178   cell_class->render = cell_renderer_activatable_render;
179
180   signals[PATH_ACTIVATED] =
181     g_signal_new ("path-activated",
182         G_TYPE_FROM_CLASS (klass),
183         G_SIGNAL_RUN_LAST,
184         0,
185         NULL, NULL,
186         g_cclosure_marshal_generic,
187         G_TYPE_NONE,
188         1, G_TYPE_STRING);
189
190   g_object_class_install_property (oclass, PROP_SHOW_ON_SELECT,
191       g_param_spec_boolean ("show-on-select",
192           "Show on select",
193           "Whether the cell renderer should be shown only when it's selected",
194           FALSE,
195           G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
196
197   g_type_class_add_private (klass,
198       sizeof (EmpathyCellRendererActivatablePriv));
199 }