empathy-cell-renderer-expander.c 10.3 KB
Newer Older
1 2 3
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2006-2007 Imendio AB
Guillaume Desmottes's avatar
Guillaume Desmottes committed
4
 * Copyright (C) 2007-2011 Collabora Ltd.
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
18 19 20
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 *
21 22 23
 * Authors: Kristian Rietveld <kris@imendio.com>
 */

24
#include <gtk/gtk.h>
25

26
#include <libempathy/empathy-utils.h>
27
#include "empathy-cell-renderer-expander.h"
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42
#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCellRendererExpander)
typedef struct {
	GtkExpanderStyle     expander_style;
	gint                 expander_size;

	guint                activatable : 1;
} EmpathyCellRendererExpanderPriv;

enum {
	PROP_0,
	PROP_EXPANDER_STYLE,
	PROP_EXPANDER_SIZE,
	PROP_ACTIVATABLE
};
43

44
static void     empathy_cell_renderer_expander_get_property (GObject                         *object,
45 46 47
							    guint                            param_id,
							    GValue                          *value,
							    GParamSpec                      *pspec);
48
static void     empathy_cell_renderer_expander_set_property (GObject                         *object,
49 50 51
							    guint                            param_id,
							    const GValue                    *value,
							    GParamSpec                      *pspec);
52 53
static void     empathy_cell_renderer_expander_finalize     (GObject                         *object);
static void     empathy_cell_renderer_expander_get_size     (GtkCellRenderer                 *cell,
54
							    GtkWidget                       *widget,
55
							    const GdkRectangle              *cell_area,
56 57 58 59
							    gint                            *x_offset,
							    gint                            *y_offset,
							    gint                            *width,
							    gint                            *height);
60
static void     empathy_cell_renderer_expander_render       (GtkCellRenderer                 *cell,
61
							    cairo_t *cr,
62
							    GtkWidget                       *widget,
63 64
							    const GdkRectangle              *background_area,
							    const GdkRectangle              *cell_area,
65
							    GtkCellRendererState             flags);
66
static gboolean empathy_cell_renderer_expander_activate     (GtkCellRenderer                 *cell,
67 68 69
							    GdkEvent                        *event,
							    GtkWidget                       *widget,
							    const gchar                     *path,
70 71
							    const GdkRectangle              *background_area,
							    const GdkRectangle              *cell_area,
72 73
							    GtkCellRendererState             flags);

74
G_DEFINE_TYPE (EmpathyCellRendererExpander, empathy_cell_renderer_expander, GTK_TYPE_CELL_RENDERER)
75 76

static void
77
empathy_cell_renderer_expander_init (EmpathyCellRendererExpander *expander)
78
{
79 80
	EmpathyCellRendererExpanderPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (expander,
		EMPATHY_TYPE_CELL_RENDERER_EXPANDER, EmpathyCellRendererExpanderPriv);
81

82
	expander->priv = priv;
83 84 85 86
	priv->expander_style = GTK_EXPANDER_COLLAPSED;
	priv->expander_size = 12;
	priv->activatable = TRUE;

87 88 89 90 91
	g_object_set (expander,
		      "xpad", 2,
		      "ypad", 2,
		      "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE,
		      NULL);
92 93 94
}

static void
95
empathy_cell_renderer_expander_class_init (EmpathyCellRendererExpanderClass *klass)
96 97 98 99 100 101 102
{
	GObjectClass         *object_class;
	GtkCellRendererClass *cell_class;

	object_class  = G_OBJECT_CLASS (klass);
	cell_class = GTK_CELL_RENDERER_CLASS (klass);

103
	object_class->finalize = empathy_cell_renderer_expander_finalize;
104

105 106
	object_class->get_property = empathy_cell_renderer_expander_get_property;
	object_class->set_property = empathy_cell_renderer_expander_set_property;
107

108 109 110
	cell_class->get_size = empathy_cell_renderer_expander_get_size;
	cell_class->render = empathy_cell_renderer_expander_render;
	cell_class->activate = empathy_cell_renderer_expander_activate;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

	g_object_class_install_property (object_class,
					 PROP_EXPANDER_STYLE,
					 g_param_spec_enum ("expander-style",
							    "Expander Style",
							    "Style to use when painting the expander",
							    GTK_TYPE_EXPANDER_STYLE,
							    GTK_EXPANDER_COLLAPSED,
							    G_PARAM_READWRITE));

	g_object_class_install_property (object_class,
					 PROP_EXPANDER_SIZE,
					 g_param_spec_int ("expander-size",
							   "Expander Size",
							   "The size of the expander",
							   0,
							   G_MAXINT,
							   12,
							   G_PARAM_READWRITE));

	g_object_class_install_property (object_class,
					 PROP_ACTIVATABLE,
					 g_param_spec_boolean ("activatable",
							       "Activatable",
							       "The expander can be activated",
							       TRUE,
							       G_PARAM_READWRITE));

139
	g_type_class_add_private (object_class, sizeof (EmpathyCellRendererExpanderPriv));
140 141 142
}

static void
143
empathy_cell_renderer_expander_get_property (GObject    *object,
144 145 146 147
					    guint       param_id,
					    GValue     *value,
					    GParamSpec *pspec)
{
148 149
	EmpathyCellRendererExpander     *expander;
	EmpathyCellRendererExpanderPriv *priv;
150

151
	expander = EMPATHY_CELL_RENDERER_EXPANDER (object);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	priv = GET_PRIV (expander);

	switch (param_id) {
	case PROP_EXPANDER_STYLE:
		g_value_set_enum (value, priv->expander_style);
		break;

	case PROP_EXPANDER_SIZE:
		g_value_set_int (value, priv->expander_size);
		break;

	case PROP_ACTIVATABLE:
		g_value_set_boolean (value, priv->activatable);
		break;

	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	}
}

static void
174
empathy_cell_renderer_expander_set_property (GObject      *object,
175 176 177 178
					    guint         param_id,
					    const GValue *value,
					    GParamSpec   *pspec)
{
179 180
	EmpathyCellRendererExpander     *expander;
	EmpathyCellRendererExpanderPriv *priv;
181

182
	expander = EMPATHY_CELL_RENDERER_EXPANDER (object);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	priv = GET_PRIV (expander);

	switch (param_id) {
	case PROP_EXPANDER_STYLE:
		priv->expander_style = g_value_get_enum (value);
		break;

	case PROP_EXPANDER_SIZE:
		priv->expander_size = g_value_get_int (value);
		break;

	case PROP_ACTIVATABLE:
		priv->activatable = g_value_get_boolean (value);
		break;

	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	}
}

static void
205
empathy_cell_renderer_expander_finalize (GObject *object)
206
{
207
	(* G_OBJECT_CLASS (empathy_cell_renderer_expander_parent_class)->finalize) (object);
208 209 210
}

GtkCellRenderer *
211
empathy_cell_renderer_expander_new (void)
212
{
213
	return g_object_new (EMPATHY_TYPE_CELL_RENDERER_EXPANDER, NULL);
214 215 216
}

static void
217 218 219 220 221 222 223
empathy_cell_renderer_expander_get_size (GtkCellRenderer    *cell,
					GtkWidget          *widget,
					const GdkRectangle *cell_area,
					gint               *x_offset,
					gint               *y_offset,
					gint               *width,
					gint               *height)
224
{
225 226
	EmpathyCellRendererExpander     *expander;
	EmpathyCellRendererExpanderPriv *priv;
227 228
	gfloat xalign, yalign;
	guint xpad, ypad;
229

230
	expander = (EmpathyCellRendererExpander *) cell;
231 232
	priv = GET_PRIV (expander);

233 234 235 236 237 238 239
	g_object_get (cell,
		      "xalign", &xalign,
		      "yalign", &yalign,
		      "xpad", &xpad,
		      "ypad", &ypad,
		      NULL);

240 241
	if (cell_area) {
		if (x_offset) {
242
			*x_offset = xalign * (cell_area->width - (priv->expander_size + (2 * xpad)));
243 244 245 246
			*x_offset = MAX (*x_offset, 0);
		}

		if (y_offset) {
247
			*y_offset = yalign * (cell_area->height - (priv->expander_size + (2 * ypad)));
248 249 250 251 252 253 254 255 256 257 258
			*y_offset = MAX (*y_offset, 0);
		}
	} else {
		if (x_offset)
			*x_offset = 0;

		if (y_offset)
			*y_offset = 0;
	}

	if (width)
259
		*width = xpad * 2 + priv->expander_size;
260 261

	if (height)
262
		*height = ypad * 2 + priv->expander_size;
263 264 265
}

static void
266
empathy_cell_renderer_expander_render (GtkCellRenderer      *cell,
267
				      cairo_t *cr,
268
				      GtkWidget            *widget,
269 270
				      const GdkRectangle   *background_area,
				      const GdkRectangle   *cell_area,
271 272
				      GtkCellRendererState  flags)
{
273 274
	EmpathyCellRendererExpander     *expander;
	EmpathyCellRendererExpanderPriv *priv;
275
	gint                            x_offset, y_offset;
276
	guint                           xpad, ypad;
277
	GtkStyleContext                 *style;
278
	GtkStateFlags                    state;
279

280
	expander = (EmpathyCellRendererExpander *) cell;
281 282
	priv = GET_PRIV (expander);

283 284
	empathy_cell_renderer_expander_get_size (cell, widget,
						(GdkRectangle *) cell_area,
285 286 287
						&x_offset, &y_offset,
						NULL, NULL);

288 289 290 291 292
	g_object_get (cell,
		      "xpad", &xpad,
		      "ypad", &ypad,
		      NULL);

293 294 295
	style = gtk_widget_get_style_context (widget);

	gtk_style_context_save (style);
296 297 298
	gtk_style_context_add_class (style, GTK_STYLE_CLASS_EXPANDER);

	state = gtk_cell_renderer_get_state (cell, widget, flags);
299 300

	if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
301
		state |= GTK_STATE_FLAG_NORMAL;
302
	else
303 304 305
		state |= GTK_STATE_FLAG_ACTIVE;

	gtk_style_context_set_state (style, state);
306 307

	gtk_render_expander (style,
308 309 310 311 312
			     cr,
			     cell_area->x + x_offset + xpad,
			     cell_area->y + y_offset + ypad,
			     priv->expander_size,
			     priv->expander_size);
313 314

	gtk_style_context_restore (style);
315 316 317
}

static gboolean
318
empathy_cell_renderer_expander_activate (GtkCellRenderer      *cell,
319 320 321
					GdkEvent             *event,
					GtkWidget            *widget,
					const gchar          *path_string,
322 323
					const GdkRectangle   *background_area,
					const GdkRectangle   *cell_area,
324 325
					GtkCellRendererState  flags)
{
326
	EmpathyCellRendererExpanderPriv *priv;
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	GtkTreePath                    *path;

	priv = GET_PRIV (cell);

	if (!GTK_IS_TREE_VIEW (widget) || !priv->activatable)
		return FALSE;

	path = gtk_tree_path_new_from_string (path_string);

	if (gtk_tree_path_get_depth (path) > 1) {
		gtk_tree_path_free (path);
		return TRUE;
	}

	if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
		gtk_tree_view_collapse_row (GTK_TREE_VIEW (widget), path);
	} else {
		gtk_tree_view_expand_row (GTK_TREE_VIEW (widget), path, FALSE);
	}

	gtk_tree_path_free (path);

	return TRUE;
}