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>
26 #include <libempathy/empathy-utils.h>
27 #include "empathy-cell-renderer-expander.h"
29 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCellRendererExpander)
31 GtkExpanderStyle expander_style;
34 guint activatable : 1;
35 } EmpathyCellRendererExpanderPriv;
44 static void empathy_cell_renderer_expander_get_property (GObject *object,
48 static void empathy_cell_renderer_expander_set_property (GObject *object,
52 static void empathy_cell_renderer_expander_finalize (GObject *object);
53 static void empathy_cell_renderer_expander_get_size (GtkCellRenderer *cell,
55 const GdkRectangle *cell_area,
60 static void empathy_cell_renderer_expander_render (GtkCellRenderer *cell,
63 const GdkRectangle *background_area,
64 const GdkRectangle *cell_area,
65 GtkCellRendererState flags);
66 static gboolean empathy_cell_renderer_expander_activate (GtkCellRenderer *cell,
70 const GdkRectangle *background_area,
71 const GdkRectangle *cell_area,
72 GtkCellRendererState flags);
74 G_DEFINE_TYPE (EmpathyCellRendererExpander, empathy_cell_renderer_expander, GTK_TYPE_CELL_RENDERER)
77 empathy_cell_renderer_expander_init (EmpathyCellRendererExpander *expander)
79 EmpathyCellRendererExpanderPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (expander,
80 EMPATHY_TYPE_CELL_RENDERER_EXPANDER, EmpathyCellRendererExpanderPriv);
82 expander->priv = priv;
83 priv->expander_style = GTK_EXPANDER_COLLAPSED;
84 priv->expander_size = 12;
85 priv->activatable = TRUE;
87 g_object_set (expander,
90 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE,
95 empathy_cell_renderer_expander_class_init (EmpathyCellRendererExpanderClass *klass)
97 GObjectClass *object_class;
98 GtkCellRendererClass *cell_class;
100 object_class = G_OBJECT_CLASS (klass);
101 cell_class = GTK_CELL_RENDERER_CLASS (klass);
103 object_class->finalize = empathy_cell_renderer_expander_finalize;
105 object_class->get_property = empathy_cell_renderer_expander_get_property;
106 object_class->set_property = empathy_cell_renderer_expander_set_property;
108 cell_class->get_size = empathy_cell_renderer_expander_get_size;
109 cell_class->render = empathy_cell_renderer_expander_render;
110 cell_class->activate = empathy_cell_renderer_expander_activate;
112 g_object_class_install_property (object_class,
114 g_param_spec_enum ("expander-style",
116 "Style to use when painting the expander",
117 GTK_TYPE_EXPANDER_STYLE,
118 GTK_EXPANDER_COLLAPSED,
121 g_object_class_install_property (object_class,
123 g_param_spec_int ("expander-size",
125 "The size of the expander",
131 g_object_class_install_property (object_class,
133 g_param_spec_boolean ("activatable",
135 "The expander can be activated",
139 g_type_class_add_private (object_class, sizeof (EmpathyCellRendererExpanderPriv));
143 empathy_cell_renderer_expander_get_property (GObject *object,
148 EmpathyCellRendererExpander *expander;
149 EmpathyCellRendererExpanderPriv *priv;
151 expander = EMPATHY_CELL_RENDERER_EXPANDER (object);
152 priv = GET_PRIV (expander);
155 case PROP_EXPANDER_STYLE:
156 g_value_set_enum (value, priv->expander_style);
159 case PROP_EXPANDER_SIZE:
160 g_value_set_int (value, priv->expander_size);
163 case PROP_ACTIVATABLE:
164 g_value_set_boolean (value, priv->activatable);
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
174 empathy_cell_renderer_expander_set_property (GObject *object,
179 EmpathyCellRendererExpander *expander;
180 EmpathyCellRendererExpanderPriv *priv;
182 expander = EMPATHY_CELL_RENDERER_EXPANDER (object);
183 priv = GET_PRIV (expander);
186 case PROP_EXPANDER_STYLE:
187 priv->expander_style = g_value_get_enum (value);
190 case PROP_EXPANDER_SIZE:
191 priv->expander_size = g_value_get_int (value);
194 case PROP_ACTIVATABLE:
195 priv->activatable = g_value_get_boolean (value);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
205 empathy_cell_renderer_expander_finalize (GObject *object)
207 (* G_OBJECT_CLASS (empathy_cell_renderer_expander_parent_class)->finalize) (object);
211 empathy_cell_renderer_expander_new (void)
213 return g_object_new (EMPATHY_TYPE_CELL_RENDERER_EXPANDER, NULL);
217 empathy_cell_renderer_expander_get_size (GtkCellRenderer *cell,
219 const GdkRectangle *cell_area,
225 EmpathyCellRendererExpander *expander;
226 EmpathyCellRendererExpanderPriv *priv;
227 gfloat xalign, yalign;
230 expander = (EmpathyCellRendererExpander *) cell;
231 priv = GET_PRIV (expander);
242 *x_offset = xalign * (cell_area->width - (priv->expander_size + (2 * xpad)));
243 *x_offset = MAX (*x_offset, 0);
247 *y_offset = yalign * (cell_area->height - (priv->expander_size + (2 * ypad)));
248 *y_offset = MAX (*y_offset, 0);
259 *width = xpad * 2 + priv->expander_size;
262 *height = ypad * 2 + priv->expander_size;
266 empathy_cell_renderer_expander_render (GtkCellRenderer *cell,
269 const GdkRectangle *background_area,
270 const GdkRectangle *cell_area,
271 GtkCellRendererState flags)
273 EmpathyCellRendererExpander *expander;
274 EmpathyCellRendererExpanderPriv *priv;
275 gint x_offset, y_offset;
277 GtkStyleContext *style;
280 expander = (EmpathyCellRendererExpander *) cell;
281 priv = GET_PRIV (expander);
283 empathy_cell_renderer_expander_get_size (cell, widget,
284 (GdkRectangle *) cell_area,
285 &x_offset, &y_offset,
293 style = gtk_widget_get_style_context (widget);
295 gtk_style_context_save (style);
296 gtk_style_context_add_class (style, GTK_STYLE_CLASS_EXPANDER);
298 state = gtk_cell_renderer_get_state (cell, widget, flags);
300 if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
301 state |= GTK_STATE_FLAG_NORMAL;
303 state |= GTK_STATE_FLAG_ACTIVE;
305 gtk_style_context_set_state (style, state);
307 gtk_render_expander (style,
309 cell_area->x + x_offset + xpad,
310 cell_area->y + y_offset + ypad,
312 priv->expander_size);
314 gtk_style_context_restore (style);
318 empathy_cell_renderer_expander_activate (GtkCellRenderer *cell,
321 const gchar *path_string,
322 const GdkRectangle *background_area,
323 const GdkRectangle *cell_area,
324 GtkCellRendererState flags)
326 EmpathyCellRendererExpanderPriv *priv;
329 priv = GET_PRIV (cell);
331 if (!GTK_IS_TREE_VIEW (widget) || !priv->activatable)
334 path = gtk_tree_path_new_from_string (path_string);
336 if (gtk_tree_path_get_depth (path) > 1) {
337 gtk_tree_path_free (path);
341 if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
342 gtk_tree_view_collapse_row (GTK_TREE_VIEW (widget), path);
344 gtk_tree_view_expand_row (GTK_TREE_VIEW (widget), path, FALSE);
347 gtk_tree_path_free (path);