]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-cell-renderer-expander.c
Merge branch 'sasl'
[empathy.git] / libempathy-gtk / empathy-cell-renderer-expander.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2006-2007 Imendio AB
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program 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  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors: Kristian Rietveld <kris@imendio.com>
21  */
22
23 /* To do:
24  *  - should probably cancel animation if model changes
25  *  - need to handle case where node-in-animation is removed
26  *  - it only handles a single animation at a time; but I guess users
27  *    aren't fast enough to trigger two or more animations at once anyway :P
28  *    (could guard for this by just cancelling the "old" animation, and
29  *     start the new one).
30  */
31
32 #include <gtk/gtk.h>
33
34 #include <libempathy/empathy-utils.h>
35 #include "empathy-cell-renderer-expander.h"
36
37 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCellRendererExpander)
38 typedef struct {
39         GtkExpanderStyle     expander_style;
40         gint                 expander_size;
41
42         GtkTreeView         *animation_view;
43         GtkTreeRowReference *animation_node;
44         GtkExpanderStyle     animation_style;
45         guint                animation_timeout;
46         GdkRectangle         animation_area;
47
48         guint                activatable : 1;
49         guint                animation_expanding : 1;
50 } EmpathyCellRendererExpanderPriv;
51
52 enum {
53         PROP_0,
54         PROP_EXPANDER_STYLE,
55         PROP_EXPANDER_SIZE,
56         PROP_ACTIVATABLE
57 };
58
59 static void     empathy_cell_renderer_expander_get_property (GObject                         *object,
60                                                             guint                            param_id,
61                                                             GValue                          *value,
62                                                             GParamSpec                      *pspec);
63 static void     empathy_cell_renderer_expander_set_property (GObject                         *object,
64                                                             guint                            param_id,
65                                                             const GValue                    *value,
66                                                             GParamSpec                      *pspec);
67 static void     empathy_cell_renderer_expander_finalize     (GObject                         *object);
68 static void     empathy_cell_renderer_expander_get_size     (GtkCellRenderer                 *cell,
69                                                             GtkWidget                       *widget,
70                                                             const GdkRectangle              *cell_area,
71                                                             gint                            *x_offset,
72                                                             gint                            *y_offset,
73                                                             gint                            *width,
74                                                             gint                            *height);
75 static void     empathy_cell_renderer_expander_render       (GtkCellRenderer                 *cell,
76                                                             cairo_t *cr,
77                                                             GtkWidget                       *widget,
78                                                             const GdkRectangle              *background_area,
79                                                             const GdkRectangle              *cell_area,
80                                                             GtkCellRendererState             flags);
81 static gboolean empathy_cell_renderer_expander_activate     (GtkCellRenderer                 *cell,
82                                                             GdkEvent                        *event,
83                                                             GtkWidget                       *widget,
84                                                             const gchar                     *path,
85                                                             const GdkRectangle              *background_area,
86                                                             const GdkRectangle              *cell_area,
87                                                             GtkCellRendererState             flags);
88
89 G_DEFINE_TYPE (EmpathyCellRendererExpander, empathy_cell_renderer_expander, GTK_TYPE_CELL_RENDERER)
90
91 static void
92 empathy_cell_renderer_expander_init (EmpathyCellRendererExpander *expander)
93 {
94         EmpathyCellRendererExpanderPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (expander,
95                 EMPATHY_TYPE_CELL_RENDERER_EXPANDER, EmpathyCellRendererExpanderPriv);
96
97         expander->priv = priv;
98         priv->expander_style = GTK_EXPANDER_COLLAPSED;
99         priv->expander_size = 12;
100         priv->activatable = TRUE;
101         priv->animation_node = NULL;
102
103         g_object_set (expander,
104                       "xpad", 2,
105                       "ypad", 2,
106                       "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE,
107                       NULL);
108 }
109
110 static void
111 empathy_cell_renderer_expander_class_init (EmpathyCellRendererExpanderClass *klass)
112 {
113         GObjectClass         *object_class;
114         GtkCellRendererClass *cell_class;
115
116         object_class  = G_OBJECT_CLASS (klass);
117         cell_class = GTK_CELL_RENDERER_CLASS (klass);
118
119         object_class->finalize = empathy_cell_renderer_expander_finalize;
120
121         object_class->get_property = empathy_cell_renderer_expander_get_property;
122         object_class->set_property = empathy_cell_renderer_expander_set_property;
123
124         cell_class->get_size = empathy_cell_renderer_expander_get_size;
125         cell_class->render = empathy_cell_renderer_expander_render;
126         cell_class->activate = empathy_cell_renderer_expander_activate;
127
128         g_object_class_install_property (object_class,
129                                          PROP_EXPANDER_STYLE,
130                                          g_param_spec_enum ("expander-style",
131                                                             "Expander Style",
132                                                             "Style to use when painting the expander",
133                                                             GTK_TYPE_EXPANDER_STYLE,
134                                                             GTK_EXPANDER_COLLAPSED,
135                                                             G_PARAM_READWRITE));
136
137         g_object_class_install_property (object_class,
138                                          PROP_EXPANDER_SIZE,
139                                          g_param_spec_int ("expander-size",
140                                                            "Expander Size",
141                                                            "The size of the expander",
142                                                            0,
143                                                            G_MAXINT,
144                                                            12,
145                                                            G_PARAM_READWRITE));
146
147         g_object_class_install_property (object_class,
148                                          PROP_ACTIVATABLE,
149                                          g_param_spec_boolean ("activatable",
150                                                                "Activatable",
151                                                                "The expander can be activated",
152                                                                TRUE,
153                                                                G_PARAM_READWRITE));
154
155         g_type_class_add_private (object_class, sizeof (EmpathyCellRendererExpanderPriv));
156 }
157
158 static void
159 empathy_cell_renderer_expander_get_property (GObject    *object,
160                                             guint       param_id,
161                                             GValue     *value,
162                                             GParamSpec *pspec)
163 {
164         EmpathyCellRendererExpander     *expander;
165         EmpathyCellRendererExpanderPriv *priv;
166
167         expander = EMPATHY_CELL_RENDERER_EXPANDER (object);
168         priv = GET_PRIV (expander);
169
170         switch (param_id) {
171         case PROP_EXPANDER_STYLE:
172                 g_value_set_enum (value, priv->expander_style);
173                 break;
174
175         case PROP_EXPANDER_SIZE:
176                 g_value_set_int (value, priv->expander_size);
177                 break;
178
179         case PROP_ACTIVATABLE:
180                 g_value_set_boolean (value, priv->activatable);
181                 break;
182
183         default:
184                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
185                 break;
186         }
187 }
188
189 static void
190 empathy_cell_renderer_expander_set_property (GObject      *object,
191                                             guint         param_id,
192                                             const GValue *value,
193                                             GParamSpec   *pspec)
194 {
195         EmpathyCellRendererExpander     *expander;
196         EmpathyCellRendererExpanderPriv *priv;
197
198         expander = EMPATHY_CELL_RENDERER_EXPANDER (object);
199         priv = GET_PRIV (expander);
200
201         switch (param_id) {
202         case PROP_EXPANDER_STYLE:
203                 priv->expander_style = g_value_get_enum (value);
204                 break;
205
206         case PROP_EXPANDER_SIZE:
207                 priv->expander_size = g_value_get_int (value);
208                 break;
209
210         case PROP_ACTIVATABLE:
211                 priv->activatable = g_value_get_boolean (value);
212                 break;
213
214         default:
215                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
216                 break;
217         }
218 }
219
220 static void
221 empathy_cell_renderer_expander_finalize (GObject *object)
222 {
223         EmpathyCellRendererExpanderPriv *priv;
224
225         priv = GET_PRIV (object);
226
227         if (priv->animation_timeout) {
228                 g_source_remove (priv->animation_timeout);
229                 priv->animation_timeout = 0;
230         }
231
232         if (priv->animation_node) {
233                 gtk_tree_row_reference_free (priv->animation_node);
234         }
235
236         (* G_OBJECT_CLASS (empathy_cell_renderer_expander_parent_class)->finalize) (object);
237 }
238
239 GtkCellRenderer *
240 empathy_cell_renderer_expander_new (void)
241 {
242         return g_object_new (EMPATHY_TYPE_CELL_RENDERER_EXPANDER, NULL);
243 }
244
245 static void
246 empathy_cell_renderer_expander_get_size (GtkCellRenderer    *cell,
247                                         GtkWidget          *widget,
248                                         const GdkRectangle *cell_area,
249                                         gint               *x_offset,
250                                         gint               *y_offset,
251                                         gint               *width,
252                                         gint               *height)
253 {
254         EmpathyCellRendererExpander     *expander;
255         EmpathyCellRendererExpanderPriv *priv;
256         gfloat xalign, yalign;
257         guint xpad, ypad;
258
259         expander = (EmpathyCellRendererExpander *) cell;
260         priv = GET_PRIV (expander);
261
262         g_object_get (cell,
263                       "xalign", &xalign,
264                       "yalign", &yalign,
265                       "xpad", &xpad,
266                       "ypad", &ypad,
267                       NULL);
268
269         if (cell_area) {
270                 if (x_offset) {
271                         *x_offset = xalign * (cell_area->width - (priv->expander_size + (2 * xpad)));
272                         *x_offset = MAX (*x_offset, 0);
273                 }
274
275                 if (y_offset) {
276                         *y_offset = yalign * (cell_area->height - (priv->expander_size + (2 * ypad)));
277                         *y_offset = MAX (*y_offset, 0);
278                 }
279         } else {
280                 if (x_offset)
281                         *x_offset = 0;
282
283                 if (y_offset)
284                         *y_offset = 0;
285         }
286
287         if (width)
288                 *width = xpad * 2 + priv->expander_size;
289
290         if (height)
291                 *height = ypad * 2 + priv->expander_size;
292 }
293
294 static void
295 empathy_cell_renderer_expander_render (GtkCellRenderer      *cell,
296                                       cairo_t *cr,
297                                       GtkWidget            *widget,
298                                       const GdkRectangle   *background_area,
299                                       const GdkRectangle   *cell_area,
300                                       GtkCellRendererState  flags)
301 {
302         EmpathyCellRendererExpander     *expander;
303         EmpathyCellRendererExpanderPriv *priv;
304         GtkExpanderStyle                expander_style;
305         gint                            x_offset, y_offset;
306         guint                           xpad, ypad;
307
308
309         expander = (EmpathyCellRendererExpander *) cell;
310         priv = GET_PRIV (expander);
311
312         if (priv->animation_node) {
313                 GtkTreePath *path;
314                 GdkRectangle rect;
315
316                 /* Not sure if I like this ... */
317                 path = gtk_tree_row_reference_get_path (priv->animation_node);
318                 gtk_tree_view_get_background_area (priv->animation_view, path,
319                                                    NULL, &rect);
320                 gtk_tree_path_free (path);
321
322                 if (background_area->y == rect.y)
323                         expander_style = priv->animation_style;
324                 else
325                         expander_style = priv->expander_style;
326         } else
327                 expander_style = priv->expander_style;
328
329         empathy_cell_renderer_expander_get_size (cell, widget,
330                                                 (GdkRectangle *) cell_area,
331                                                 &x_offset, &y_offset,
332                                                 NULL, NULL);
333
334         g_object_get (cell,
335                       "xpad", &xpad,
336                       "ypad", &ypad,
337                       NULL);
338
339         gtk_paint_expander (gtk_widget_get_style (widget),
340                             cr,
341                             GTK_STATE_NORMAL,
342                             widget,
343                             "treeview",
344                             cell_area->x + x_offset + xpad + priv->expander_size / 2,
345                             cell_area->y + y_offset + ypad + priv->expander_size / 2,
346                             expander_style);
347 }
348
349 static void
350 invalidate_node (GtkTreeView *tree_view,
351                  GtkTreePath *path)
352 {
353        GdkWindow    *bin_window;
354        GdkRectangle  rect;
355        GtkAllocation allocation;
356
357        bin_window = gtk_tree_view_get_bin_window (tree_view);
358
359        gtk_tree_view_get_background_area (tree_view, path, NULL, &rect);
360
361        rect.x = 0;
362        gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
363        rect.width = allocation.width;
364
365        gdk_window_invalidate_rect (bin_window, &rect, TRUE);
366 }
367
368 static gboolean
369 do_animation (EmpathyCellRendererExpander *expander)
370 {
371         EmpathyCellRendererExpanderPriv *priv;
372         GtkTreePath                    *path;
373         gboolean                        done = FALSE;
374
375         priv = GET_PRIV (expander);
376
377         if (priv->animation_expanding) {
378                 if (priv->animation_style == GTK_EXPANDER_SEMI_COLLAPSED)
379                         priv->animation_style = GTK_EXPANDER_SEMI_EXPANDED;
380                 else if (priv->animation_style == GTK_EXPANDER_SEMI_EXPANDED) {
381                         priv->animation_style = GTK_EXPANDER_EXPANDED;
382                         done = TRUE;
383                 }
384         } else {
385                 if (priv->animation_style == GTK_EXPANDER_SEMI_EXPANDED)
386                         priv->animation_style = GTK_EXPANDER_SEMI_COLLAPSED;
387                 else if (priv->animation_style == GTK_EXPANDER_SEMI_COLLAPSED) {
388                         priv->animation_style = GTK_EXPANDER_COLLAPSED;
389                         done = TRUE;
390                 }
391         }
392
393         path = gtk_tree_row_reference_get_path (priv->animation_node);
394         invalidate_node (priv->animation_view, path);
395         gtk_tree_path_free (path);
396
397         if (done) {
398                 gtk_tree_row_reference_free (priv->animation_node);
399                 priv->animation_node = NULL;
400                 priv->animation_timeout = 0;
401         }
402
403         return !done;
404 }
405
406 static gboolean
407 animation_timeout (gpointer data)
408 {
409         gboolean retval;
410
411         GDK_THREADS_ENTER ();
412
413         retval = do_animation (data);
414
415         GDK_THREADS_LEAVE ();
416
417         return retval;
418 }
419
420 static void
421 empathy_cell_renderer_expander_start_animation (EmpathyCellRendererExpander *expander,
422                                                GtkTreeView                *tree_view,
423                                                GtkTreePath                *path,
424                                                gboolean                    expanding,
425                                                const GdkRectangle         *background_area)
426 {
427         EmpathyCellRendererExpanderPriv *priv;
428
429         priv = GET_PRIV (expander);
430
431         if (priv->animation_timeout != 0) {
432                 g_source_remove (priv->animation_timeout);
433                 priv->animation_timeout = 0;
434                 gtk_tree_row_reference_free (priv->animation_node);
435                 priv->animation_node = NULL;
436         }
437
438         if (expanding) {
439                 priv->animation_style = GTK_EXPANDER_SEMI_COLLAPSED;
440         } else {
441                 priv->animation_style = GTK_EXPANDER_SEMI_EXPANDED;
442         }
443
444         invalidate_node (tree_view, path);
445
446         priv->animation_expanding = expanding;
447         priv->animation_view = tree_view;
448         priv->animation_node = gtk_tree_row_reference_new (gtk_tree_view_get_model (tree_view), path);
449         priv->animation_timeout = g_timeout_add (50, animation_timeout, expander);
450 }
451
452 static gboolean
453 empathy_cell_renderer_expander_activate (GtkCellRenderer      *cell,
454                                         GdkEvent             *event,
455                                         GtkWidget            *widget,
456                                         const gchar          *path_string,
457                                         const GdkRectangle   *background_area,
458                                         const GdkRectangle   *cell_area,
459                                         GtkCellRendererState  flags)
460 {
461         EmpathyCellRendererExpander     *expander;
462         EmpathyCellRendererExpanderPriv *priv;
463         GtkTreePath                    *path;
464         gboolean                        animate;
465         gboolean                        expanding;
466
467         expander = EMPATHY_CELL_RENDERER_EXPANDER (cell);
468         priv = GET_PRIV (cell);
469
470         if (!GTK_IS_TREE_VIEW (widget) || !priv->activatable)
471                 return FALSE;
472
473         path = gtk_tree_path_new_from_string (path_string);
474
475         if (gtk_tree_path_get_depth (path) > 1) {
476                 gtk_tree_path_free (path);
477                 return TRUE;
478         }
479
480         g_object_get (gtk_widget_get_settings (GTK_WIDGET (widget)),
481                       "gtk-enable-animations", &animate,
482                       NULL);
483
484         if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
485                 gtk_tree_view_collapse_row (GTK_TREE_VIEW (widget), path);
486                 expanding = FALSE;
487         } else {
488                 gtk_tree_view_expand_row (GTK_TREE_VIEW (widget), path, FALSE);
489                 expanding = TRUE;
490         }
491
492         if (animate) {
493                 empathy_cell_renderer_expander_start_animation (expander,
494                                                                GTK_TREE_VIEW (widget),
495                                                                path,
496                                                                expanding,
497                                                                background_area);
498         }
499
500         gtk_tree_path_free (path);
501
502         return TRUE;
503 }