]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-cell-renderer-expander.c
Merge commit 'jtellier/video-call-button-sensitivity'
[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                                                             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                                                             GdkWindow                       *window,
77                                                             GtkWidget                       *widget,
78                                                             GdkRectangle                    *background_area,
79                                                             GdkRectangle                    *cell_area,
80                                                             GdkRectangle                    *expose_area,
81                                                             GtkCellRendererState             flags);
82 static gboolean empathy_cell_renderer_expander_activate     (GtkCellRenderer                 *cell,
83                                                             GdkEvent                        *event,
84                                                             GtkWidget                       *widget,
85                                                             const gchar                     *path,
86                                                             GdkRectangle                    *background_area,
87                                                             GdkRectangle                    *cell_area,
88                                                             GtkCellRendererState             flags);
89
90 G_DEFINE_TYPE (EmpathyCellRendererExpander, empathy_cell_renderer_expander, GTK_TYPE_CELL_RENDERER)
91
92 static void
93 empathy_cell_renderer_expander_init (EmpathyCellRendererExpander *expander)
94 {
95         EmpathyCellRendererExpanderPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (expander,
96                 EMPATHY_TYPE_CELL_RENDERER_EXPANDER, EmpathyCellRendererExpanderPriv);
97
98         expander->priv = priv;
99         priv->expander_style = GTK_EXPANDER_COLLAPSED;
100         priv->expander_size = 12;
101         priv->activatable = TRUE;
102         priv->animation_node = NULL;
103
104         g_object_set (expander,
105                       "xpad", 2,
106                       "ypad", 2,
107                       "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE,
108                       NULL);
109 }
110
111 static void
112 empathy_cell_renderer_expander_class_init (EmpathyCellRendererExpanderClass *klass)
113 {
114         GObjectClass         *object_class;
115         GtkCellRendererClass *cell_class;
116
117         object_class  = G_OBJECT_CLASS (klass);
118         cell_class = GTK_CELL_RENDERER_CLASS (klass);
119
120         object_class->finalize = empathy_cell_renderer_expander_finalize;
121
122         object_class->get_property = empathy_cell_renderer_expander_get_property;
123         object_class->set_property = empathy_cell_renderer_expander_set_property;
124
125         cell_class->get_size = empathy_cell_renderer_expander_get_size;
126         cell_class->render = empathy_cell_renderer_expander_render;
127         cell_class->activate = empathy_cell_renderer_expander_activate;
128
129         g_object_class_install_property (object_class,
130                                          PROP_EXPANDER_STYLE,
131                                          g_param_spec_enum ("expander-style",
132                                                             "Expander Style",
133                                                             "Style to use when painting the expander",
134                                                             GTK_TYPE_EXPANDER_STYLE,
135                                                             GTK_EXPANDER_COLLAPSED,
136                                                             G_PARAM_READWRITE));
137
138         g_object_class_install_property (object_class,
139                                          PROP_EXPANDER_SIZE,
140                                          g_param_spec_int ("expander-size",
141                                                            "Expander Size",
142                                                            "The size of the expander",
143                                                            0,
144                                                            G_MAXINT,
145                                                            12,
146                                                            G_PARAM_READWRITE));
147
148         g_object_class_install_property (object_class,
149                                          PROP_ACTIVATABLE,
150                                          g_param_spec_boolean ("activatable",
151                                                                "Activatable",
152                                                                "The expander can be activated",
153                                                                TRUE,
154                                                                G_PARAM_READWRITE));
155
156         g_type_class_add_private (object_class, sizeof (EmpathyCellRendererExpanderPriv));
157 }
158
159 static void
160 empathy_cell_renderer_expander_get_property (GObject    *object,
161                                             guint       param_id,
162                                             GValue     *value,
163                                             GParamSpec *pspec)
164 {
165         EmpathyCellRendererExpander     *expander;
166         EmpathyCellRendererExpanderPriv *priv;
167
168         expander = EMPATHY_CELL_RENDERER_EXPANDER (object);
169         priv = GET_PRIV (expander);
170
171         switch (param_id) {
172         case PROP_EXPANDER_STYLE:
173                 g_value_set_enum (value, priv->expander_style);
174                 break;
175
176         case PROP_EXPANDER_SIZE:
177                 g_value_set_int (value, priv->expander_size);
178                 break;
179
180         case PROP_ACTIVATABLE:
181                 g_value_set_boolean (value, priv->activatable);
182                 break;
183
184         default:
185                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
186                 break;
187         }
188 }
189
190 static void
191 empathy_cell_renderer_expander_set_property (GObject      *object,
192                                             guint         param_id,
193                                             const GValue *value,
194                                             GParamSpec   *pspec)
195 {
196         EmpathyCellRendererExpander     *expander;
197         EmpathyCellRendererExpanderPriv *priv;
198
199         expander = EMPATHY_CELL_RENDERER_EXPANDER (object);
200         priv = GET_PRIV (expander);
201
202         switch (param_id) {
203         case PROP_EXPANDER_STYLE:
204                 priv->expander_style = g_value_get_enum (value);
205                 break;
206
207         case PROP_EXPANDER_SIZE:
208                 priv->expander_size = g_value_get_int (value);
209                 break;
210
211         case PROP_ACTIVATABLE:
212                 priv->activatable = g_value_get_boolean (value);
213                 break;
214
215         default:
216                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
217                 break;
218         }
219 }
220
221 static void
222 empathy_cell_renderer_expander_finalize (GObject *object)
223 {
224         EmpathyCellRendererExpanderPriv *priv;
225
226         priv = GET_PRIV (object);
227
228         if (priv->animation_timeout) {
229                 g_source_remove (priv->animation_timeout);
230                 priv->animation_timeout = 0;
231         }
232
233         if (priv->animation_node) {
234                 gtk_tree_row_reference_free (priv->animation_node);
235         }
236
237         (* G_OBJECT_CLASS (empathy_cell_renderer_expander_parent_class)->finalize) (object);
238 }
239
240 GtkCellRenderer *
241 empathy_cell_renderer_expander_new (void)
242 {
243         return g_object_new (EMPATHY_TYPE_CELL_RENDERER_EXPANDER, NULL);
244 }
245
246 static void
247 empathy_cell_renderer_expander_get_size (GtkCellRenderer *cell,
248                                         GtkWidget       *widget,
249                                         GdkRectangle    *cell_area,
250                                         gint            *x_offset,
251                                         gint            *y_offset,
252                                         gint            *width,
253                                         gint            *height)
254 {
255         EmpathyCellRendererExpander     *expander;
256         EmpathyCellRendererExpanderPriv *priv;
257         gfloat xalign, yalign;
258         guint xpad, ypad;
259
260         expander = (EmpathyCellRendererExpander *) cell;
261         priv = GET_PRIV (expander);
262
263         g_object_get (cell,
264                       "xalign", &xalign,
265                       "yalign", &yalign,
266                       "xpad", &xpad,
267                       "ypad", &ypad,
268                       NULL);
269
270         if (cell_area) {
271                 if (x_offset) {
272                         *x_offset = xalign * (cell_area->width - (priv->expander_size + (2 * xpad)));
273                         *x_offset = MAX (*x_offset, 0);
274                 }
275
276                 if (y_offset) {
277                         *y_offset = yalign * (cell_area->height - (priv->expander_size + (2 * ypad)));
278                         *y_offset = MAX (*y_offset, 0);
279                 }
280         } else {
281                 if (x_offset)
282                         *x_offset = 0;
283
284                 if (y_offset)
285                         *y_offset = 0;
286         }
287
288         if (width)
289                 *width = xpad * 2 + priv->expander_size;
290
291         if (height)
292                 *height = ypad * 2 + priv->expander_size;
293 }
294
295 static void
296 empathy_cell_renderer_expander_render (GtkCellRenderer      *cell,
297                                       GdkWindow            *window,
298                                       GtkWidget            *widget,
299                                       GdkRectangle         *background_area,
300                                       GdkRectangle         *cell_area,
301                                       GdkRectangle         *expose_area,
302                                       GtkCellRendererState  flags)
303 {
304         EmpathyCellRendererExpander     *expander;
305         EmpathyCellRendererExpanderPriv *priv;
306         GtkExpanderStyle                expander_style;
307         gint                            x_offset, y_offset;
308         guint                           xpad, ypad;
309
310
311         expander = (EmpathyCellRendererExpander *) cell;
312         priv = GET_PRIV (expander);
313
314         if (priv->animation_node) {
315                 GtkTreePath *path;
316                 GdkRectangle rect;
317
318                 /* Not sure if I like this ... */
319                 path = gtk_tree_row_reference_get_path (priv->animation_node);
320                 gtk_tree_view_get_background_area (priv->animation_view, path,
321                                                    NULL, &rect);
322                 gtk_tree_path_free (path);
323
324                 if (background_area->y == rect.y)
325                         expander_style = priv->animation_style;
326                 else
327                         expander_style = priv->expander_style;
328         } else
329                 expander_style = priv->expander_style;
330
331         empathy_cell_renderer_expander_get_size (cell, widget, cell_area,
332                                                 &x_offset, &y_offset,
333                                                 NULL, NULL);
334
335         g_object_get (cell,
336                       "xpad", &xpad,
337                       "ypad", &ypad,
338                       NULL);
339
340         gtk_paint_expander (gtk_widget_get_style (widget),
341                             window,
342                             GTK_STATE_NORMAL,
343                             expose_area,
344                             widget,
345                             "treeview",
346                             cell_area->x + x_offset + xpad + priv->expander_size / 2,
347                             cell_area->y + y_offset + ypad + priv->expander_size / 2,
348                             expander_style);
349 }
350
351 static void
352 invalidate_node (GtkTreeView *tree_view,
353                  GtkTreePath *path)
354 {
355        GdkWindow    *bin_window;
356        GdkRectangle  rect;
357
358        bin_window = gtk_tree_view_get_bin_window (tree_view);
359
360        gtk_tree_view_get_background_area (tree_view, path, NULL, &rect);
361
362        rect.x = 0;
363        rect.width = GTK_WIDGET (tree_view)->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                                                GdkRectangle               *background_area)
426 {
427         EmpathyCellRendererExpanderPriv *priv;
428
429         priv = GET_PRIV (expander);
430
431         if (expanding) {
432                 priv->animation_style = GTK_EXPANDER_SEMI_COLLAPSED;
433         } else {
434                 priv->animation_style = GTK_EXPANDER_SEMI_EXPANDED;
435         }
436
437         invalidate_node (tree_view, path);
438
439         priv->animation_expanding = expanding;
440         priv->animation_view = tree_view;
441         priv->animation_node = gtk_tree_row_reference_new (gtk_tree_view_get_model (tree_view), path);
442         priv->animation_timeout = g_timeout_add (50, animation_timeout, expander);
443 }
444
445 static gboolean
446 empathy_cell_renderer_expander_activate (GtkCellRenderer      *cell,
447                                         GdkEvent             *event,
448                                         GtkWidget            *widget,
449                                         const gchar          *path_string,
450                                         GdkRectangle         *background_area,
451                                         GdkRectangle         *cell_area,
452                                         GtkCellRendererState  flags)
453 {
454         EmpathyCellRendererExpander     *expander;
455         EmpathyCellRendererExpanderPriv *priv;
456         GtkTreePath                    *path;
457         gboolean                        animate;
458         gboolean                        expanding;
459
460         expander = EMPATHY_CELL_RENDERER_EXPANDER (cell);
461         priv = GET_PRIV (cell);
462
463         if (!GTK_IS_TREE_VIEW (widget) || !priv->activatable)
464                 return FALSE;
465
466         path = gtk_tree_path_new_from_string (path_string);
467
468         if (gtk_tree_path_get_depth (path) > 1) {
469                 gtk_tree_path_free (path);
470                 return TRUE;
471         }
472
473         g_object_get (gtk_widget_get_settings (GTK_WIDGET (widget)),
474                       "gtk-enable-animations", &animate,
475                       NULL);
476
477         if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
478                 gtk_tree_view_collapse_row (GTK_TREE_VIEW (widget), path);
479                 expanding = FALSE;
480         } else {
481                 gtk_tree_view_expand_row (GTK_TREE_VIEW (widget), path, FALSE);
482                 expanding = TRUE;
483         }
484
485         if (animate) {
486                 empathy_cell_renderer_expander_start_animation (expander,
487                                                                GTK_TREE_VIEW (widget),
488                                                                path,
489                                                                expanding,
490                                                                background_area);
491         }
492
493         gtk_tree_path_free (path);
494
495         return TRUE;
496 }