1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2006-2007 Imendio AB
4 * Copyright (C) 2007-2011 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Kristian Rietveld <kris@imendio.com>
24 #include "libempathy/empathy-utils.h"
25 #include "empathy-cell-renderer-expander.h"
27 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCellRendererExpander)
29 GtkExpanderStyle expander_style;
32 guint activatable : 1;
33 } EmpathyCellRendererExpanderPriv;
42 static void empathy_cell_renderer_expander_get_property (GObject *object,
46 static void empathy_cell_renderer_expander_set_property (GObject *object,
50 static void empathy_cell_renderer_expander_finalize (GObject *object);
51 static void empathy_cell_renderer_expander_get_size (GtkCellRenderer *cell,
53 const GdkRectangle *cell_area,
58 static void empathy_cell_renderer_expander_render (GtkCellRenderer *cell,
61 const GdkRectangle *background_area,
62 const GdkRectangle *cell_area,
63 GtkCellRendererState flags);
64 static gboolean empathy_cell_renderer_expander_activate (GtkCellRenderer *cell,
68 const GdkRectangle *background_area,
69 const GdkRectangle *cell_area,
70 GtkCellRendererState flags);
72 G_DEFINE_TYPE (EmpathyCellRendererExpander, empathy_cell_renderer_expander, GTK_TYPE_CELL_RENDERER)
75 empathy_cell_renderer_expander_init (EmpathyCellRendererExpander *expander)
77 EmpathyCellRendererExpanderPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (expander,
78 EMPATHY_TYPE_CELL_RENDERER_EXPANDER, EmpathyCellRendererExpanderPriv);
80 expander->priv = priv;
81 priv->expander_style = GTK_EXPANDER_COLLAPSED;
82 priv->expander_size = 12;
83 priv->activatable = TRUE;
85 g_object_set (expander,
88 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE,
93 empathy_cell_renderer_expander_class_init (EmpathyCellRendererExpanderClass *klass)
95 GObjectClass *object_class;
96 GtkCellRendererClass *cell_class;
98 object_class = G_OBJECT_CLASS (klass);
99 cell_class = GTK_CELL_RENDERER_CLASS (klass);
101 object_class->finalize = empathy_cell_renderer_expander_finalize;
103 object_class->get_property = empathy_cell_renderer_expander_get_property;
104 object_class->set_property = empathy_cell_renderer_expander_set_property;
106 cell_class->get_size = empathy_cell_renderer_expander_get_size;
107 cell_class->render = empathy_cell_renderer_expander_render;
108 cell_class->activate = empathy_cell_renderer_expander_activate;
110 g_object_class_install_property (object_class,
112 g_param_spec_enum ("expander-style",
114 "Style to use when painting the expander",
115 GTK_TYPE_EXPANDER_STYLE,
116 GTK_EXPANDER_COLLAPSED,
119 g_object_class_install_property (object_class,
121 g_param_spec_int ("expander-size",
123 "The size of the expander",
129 g_object_class_install_property (object_class,
131 g_param_spec_boolean ("activatable",
133 "The expander can be activated",
137 g_type_class_add_private (object_class, sizeof (EmpathyCellRendererExpanderPriv));
141 empathy_cell_renderer_expander_get_property (GObject *object,
146 EmpathyCellRendererExpander *expander;
147 EmpathyCellRendererExpanderPriv *priv;
149 expander = EMPATHY_CELL_RENDERER_EXPANDER (object);
150 priv = GET_PRIV (expander);
153 case PROP_EXPANDER_STYLE:
154 g_value_set_enum (value, priv->expander_style);
157 case PROP_EXPANDER_SIZE:
158 g_value_set_int (value, priv->expander_size);
161 case PROP_ACTIVATABLE:
162 g_value_set_boolean (value, priv->activatable);
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
172 empathy_cell_renderer_expander_set_property (GObject *object,
177 EmpathyCellRendererExpander *expander;
178 EmpathyCellRendererExpanderPriv *priv;
180 expander = EMPATHY_CELL_RENDERER_EXPANDER (object);
181 priv = GET_PRIV (expander);
184 case PROP_EXPANDER_STYLE:
185 priv->expander_style = g_value_get_enum (value);
188 case PROP_EXPANDER_SIZE:
189 priv->expander_size = g_value_get_int (value);
192 case PROP_ACTIVATABLE:
193 priv->activatable = g_value_get_boolean (value);
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
203 empathy_cell_renderer_expander_finalize (GObject *object)
205 (* G_OBJECT_CLASS (empathy_cell_renderer_expander_parent_class)->finalize) (object);
209 empathy_cell_renderer_expander_new (void)
211 return g_object_new (EMPATHY_TYPE_CELL_RENDERER_EXPANDER, NULL);
215 empathy_cell_renderer_expander_get_size (GtkCellRenderer *cell,
217 const GdkRectangle *cell_area,
223 EmpathyCellRendererExpander *expander;
224 EmpathyCellRendererExpanderPriv *priv;
225 gfloat xalign, yalign;
228 expander = (EmpathyCellRendererExpander *) cell;
229 priv = GET_PRIV (expander);
240 *x_offset = xalign * (cell_area->width - (priv->expander_size + (2 * xpad)));
241 *x_offset = MAX (*x_offset, 0);
245 *y_offset = yalign * (cell_area->height - (priv->expander_size + (2 * ypad)));
246 *y_offset = MAX (*y_offset, 0);
257 *width = xpad * 2 + priv->expander_size;
260 *height = ypad * 2 + priv->expander_size;
264 empathy_cell_renderer_expander_render (GtkCellRenderer *cell,
267 const GdkRectangle *background_area,
268 const GdkRectangle *cell_area,
269 GtkCellRendererState flags)
271 EmpathyCellRendererExpander *expander;
272 EmpathyCellRendererExpanderPriv *priv;
273 gint x_offset, y_offset;
275 GtkStyleContext *style;
278 expander = (EmpathyCellRendererExpander *) cell;
279 priv = GET_PRIV (expander);
281 empathy_cell_renderer_expander_get_size (cell, widget,
282 (GdkRectangle *) cell_area,
283 &x_offset, &y_offset,
291 style = gtk_widget_get_style_context (widget);
293 gtk_style_context_save (style);
294 gtk_style_context_add_class (style, GTK_STYLE_CLASS_EXPANDER);
296 state = gtk_cell_renderer_get_state (cell, widget, flags);
298 if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
299 state |= GTK_STATE_FLAG_NORMAL;
301 state |= GTK_STATE_FLAG_ACTIVE;
303 gtk_style_context_set_state (style, state);
305 gtk_render_expander (style,
307 cell_area->x + x_offset + xpad,
308 cell_area->y + y_offset + ypad,
310 priv->expander_size);
312 gtk_style_context_restore (style);
316 empathy_cell_renderer_expander_activate (GtkCellRenderer *cell,
319 const gchar *path_string,
320 const GdkRectangle *background_area,
321 const GdkRectangle *cell_area,
322 GtkCellRendererState flags)
324 EmpathyCellRendererExpanderPriv *priv;
327 priv = GET_PRIV (cell);
329 if (!GTK_IS_TREE_VIEW (widget) || !priv->activatable)
332 path = gtk_tree_path_new_from_string (path_string);
334 if (gtk_tree_path_get_depth (path) > 1) {
335 gtk_tree_path_free (path);
339 if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
340 gtk_tree_view_collapse_row (GTK_TREE_VIEW (widget), path);
342 gtk_tree_view_expand_row (GTK_TREE_VIEW (widget), path, FALSE);
345 gtk_tree_path_free (path);