Create one single GAction to join rooms
[empathy-mirror.git] / libempathy-gtk / empathy-cell-renderer-activatable.c
blobbdbf1cb095ed0a67149aa4bd20946b059c312cd0
1 /*
2 * Copyright (C) 2007 Raphael Slinckx <raphael@slinckx.net>
3 * Copyright (C) 2007-2009 Collabora Ltd.
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.
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.
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
19 * Authors: Raphael Slinckx <raphael@slinckx.net>
20 * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
23 #include "config.h"
24 #include "empathy-cell-renderer-activatable.h"
26 #include "empathy-utils.h"
28 enum {
29 PATH_ACTIVATED,
30 LAST_SIGNAL
33 enum {
34 PROP_SHOW_ON_SELECT = 1
37 typedef struct {
38 gboolean show_on_select;
39 } EmpathyCellRendererActivatablePriv;
41 static guint signals[LAST_SIGNAL];
43 G_DEFINE_TYPE (EmpathyCellRendererActivatable,
44 empathy_cell_renderer_activatable, GTK_TYPE_CELL_RENDERER_PIXBUF)
46 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCellRendererActivatable)
48 static void
49 empathy_cell_renderer_activatable_init (EmpathyCellRendererActivatable *cell)
51 cell->priv = G_TYPE_INSTANCE_GET_PRIVATE (cell,
52 EMPATHY_TYPE_CELL_RENDERER_ACTIVATABLE,
53 EmpathyCellRendererActivatablePriv);
55 g_object_set (cell,
56 "xpad", 0,
57 "ypad", 0,
58 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE,
59 "follow-state", TRUE,
60 NULL);
63 static void
64 cell_renderer_activatable_get_property (GObject *object,
65 guint prop_id,
66 GValue *value,
67 GParamSpec *pspec)
69 EmpathyCellRendererActivatablePriv *priv = GET_PRIV (object);
71 switch (prop_id)
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;
82 static void
83 cell_renderer_activatable_set_property (GObject *object,
84 guint prop_id,
85 const GValue *value,
86 GParamSpec *pspec)
88 EmpathyCellRendererActivatablePriv *priv = GET_PRIV (object);
90 switch (prop_id)
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;
101 GtkCellRenderer *
102 empathy_cell_renderer_activatable_new (void)
104 return g_object_new (EMPATHY_TYPE_CELL_RENDERER_ACTIVATABLE, NULL);
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)
116 EmpathyCellRendererActivatable *activatable;
117 gint ex, ey, bx, by, bw, bh;
119 activatable = EMPATHY_CELL_RENDERER_ACTIVATABLE (cell);
121 if (!GTK_IS_TREE_VIEW (widget) || event == NULL ||
122 event->type != GDK_BUTTON_PRESS) {
123 return FALSE;
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;
133 if (ex < bx || ex > (bx+bw) || ey < by || ey > (by+bh)){
134 /* Click wasn't on the icon */
135 return FALSE;
138 g_signal_emit (activatable, signals[PATH_ACTIVATED], 0, path_string);
140 return TRUE;
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)
152 EmpathyCellRendererActivatablePriv *priv = GET_PRIV (cell);
154 if (priv->show_on_select && !(flags & (GTK_CELL_RENDERER_SELECTED)))
155 return;
157 GTK_CELL_RENDERER_CLASS
158 (empathy_cell_renderer_activatable_parent_class)->render (
159 cell, cr, widget, background_area, cell_area, flags);
162 static void
163 empathy_cell_renderer_activatable_class_init (
164 EmpathyCellRendererActivatableClass *klass)
166 GtkCellRendererClass *cell_class;
167 GObjectClass *oclass;
169 oclass = G_OBJECT_CLASS (klass);
170 oclass->get_property = cell_renderer_activatable_get_property;
171 oclass->set_property = cell_renderer_activatable_set_property;
173 cell_class = GTK_CELL_RENDERER_CLASS (klass);
174 cell_class->activate = cell_renderer_activatable_activate;
175 cell_class->render = cell_renderer_activatable_render;
177 signals[PATH_ACTIVATED] =
178 g_signal_new ("path-activated",
179 G_TYPE_FROM_CLASS (klass),
180 G_SIGNAL_RUN_LAST,
182 NULL, NULL,
183 g_cclosure_marshal_generic,
184 G_TYPE_NONE,
185 1, G_TYPE_STRING);
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));
194 g_type_class_add_private (klass,
195 sizeof (EmpathyCellRendererActivatablePriv));