Whitespace fix
[empathy-mirror.git] / libempathy-gtk / empathy-cell-renderer-activatable.c
blob6aedb03066fb203ed7bd9fd83ff525d0c04441b9
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>
25 #include <gtk/gtk.h>
27 #include <libempathy/empathy-utils.h>
29 #include "empathy-cell-renderer-activatable.h"
31 enum {
32 PATH_ACTIVATED,
33 LAST_SIGNAL
36 enum {
37 PROP_SHOW_ON_SELECT = 1
40 typedef struct {
41 gboolean show_on_select;
42 } EmpathyCellRendererActivatablePriv;
44 static guint signals[LAST_SIGNAL];
46 G_DEFINE_TYPE (EmpathyCellRendererActivatable,
47 empathy_cell_renderer_activatable, GTK_TYPE_CELL_RENDERER_PIXBUF)
49 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCellRendererActivatable)
51 static void
52 empathy_cell_renderer_activatable_init (EmpathyCellRendererActivatable *cell)
54 cell->priv = G_TYPE_INSTANCE_GET_PRIVATE (cell,
55 EMPATHY_TYPE_CELL_RENDERER_ACTIVATABLE,
56 EmpathyCellRendererActivatablePriv);
58 g_object_set (cell,
59 "xpad", 0,
60 "ypad", 0,
61 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE,
62 "follow-state", TRUE,
63 NULL);
66 static void
67 cell_renderer_activatable_get_property (GObject *object,
68 guint prop_id,
69 GValue *value,
70 GParamSpec *pspec)
72 EmpathyCellRendererActivatablePriv *priv = GET_PRIV (object);
74 switch (prop_id)
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;
85 static void
86 cell_renderer_activatable_set_property (GObject *object,
87 guint prop_id,
88 const GValue *value,
89 GParamSpec *pspec)
91 EmpathyCellRendererActivatablePriv *priv = GET_PRIV (object);
93 switch (prop_id)
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;
104 GtkCellRenderer *
105 empathy_cell_renderer_activatable_new (void)
107 return g_object_new (EMPATHY_TYPE_CELL_RENDERER_ACTIVATABLE, NULL);
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)
119 EmpathyCellRendererActivatable *activatable;
120 gint ex, ey, bx, by, bw, bh;
122 activatable = EMPATHY_CELL_RENDERER_ACTIVATABLE (cell);
124 if (!GTK_IS_TREE_VIEW (widget) || event == NULL ||
125 event->type != GDK_BUTTON_PRESS) {
126 return FALSE;
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;
136 if (ex < bx || ex > (bx+bw) || ey < by || ey > (by+bh)){
137 /* Click wasn't on the icon */
138 return FALSE;
141 g_signal_emit (activatable, signals[PATH_ACTIVATED], 0, path_string);
143 return TRUE;
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)
156 EmpathyCellRendererActivatablePriv *priv = GET_PRIV (cell);
158 if (priv->show_on_select && !(flags & (GTK_CELL_RENDERER_SELECTED)))
159 return;
161 GTK_CELL_RENDERER_CLASS
162 (empathy_cell_renderer_activatable_parent_class)->render (
163 cell, window, widget, background_area, cell_area, expose_area, flags);
166 static void
167 empathy_cell_renderer_activatable_class_init (
168 EmpathyCellRendererActivatableClass *klass)
170 GtkCellRendererClass *cell_class;
171 GObjectClass *oclass;
173 oclass = G_OBJECT_CLASS (klass);
174 oclass->get_property = cell_renderer_activatable_get_property;
175 oclass->set_property = cell_renderer_activatable_set_property;
177 cell_class = GTK_CELL_RENDERER_CLASS (klass);
178 cell_class->activate = cell_renderer_activatable_activate;
179 cell_class->render = cell_renderer_activatable_render;
181 signals[PATH_ACTIVATED] =
182 g_signal_new ("path-activated",
183 G_TYPE_FROM_CLASS (klass),
184 G_SIGNAL_RUN_LAST,
186 NULL, NULL,
187 g_cclosure_marshal_VOID__STRING,
188 G_TYPE_NONE,
189 1, G_TYPE_STRING);
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));
198 g_type_class_add_private (klass,
199 sizeof (EmpathyCellRendererActivatablePriv));