]> Pileus Git - ~andy/gtk/blob - tests/cellareascaffold.c
Added test and scaffolding widget for GtkCellArea.
[~andy/gtk] / tests / cellareascaffold.c
1 /* cellareascaffold.c
2  *
3  * Copyright (C) 2010 Openismus GmbH
4  *
5  * Authors:
6  *      Tristan Van Berkom <tristanvb@openismus.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <string.h>
25 #include "cellareascaffold.h"
26
27 /* GObjectClass */
28 static void      cell_area_scaffold_finalize                       (GObject              *object);
29 static void      cell_area_scaffold_dispose                        (GObject              *object);
30 static void      cell_area_scaffold_set_property                   (GObject              *object,
31                                                                     guint                 prop_id,
32                                                                     const GValue         *value,
33                                                                     GParamSpec           *pspec);
34 static void      cell_area_scaffold_get_property                   (GObject              *object,
35                                                                     guint                 prop_id,
36                                                                     GValue               *value,
37                                                                     GParamSpec           *pspec);
38
39 /* GtkWidgetClass */
40 static gboolean  cell_area_scaffold_draw                           (GtkWidget       *widget,
41                                                                     cairo_t         *cr);
42 static void      cell_area_scaffold_size_allocate                  (GtkWidget       *widget,
43                                                                     GtkAllocation   *allocation);
44 static void      cell_area_scaffold_get_preferred_width            (GtkWidget       *widget,
45                                                                     gint            *minimum_size,
46                                                                     gint            *natural_size);
47 static void      cell_area_scaffold_get_preferred_height_for_width (GtkWidget       *widget,
48                                                                     gint             for_size,
49                                                                     gint            *minimum_size,
50                                                                     gint            *natural_size);
51 static void      cell_area_scaffold_get_preferred_height           (GtkWidget       *widget,
52                                                                     gint            *minimum_size,
53                                                                     gint            *natural_size);
54 static void      cell_area_scaffold_get_preferred_width_for_height (GtkWidget       *widget,
55                                                                     gint             for_size,
56                                                                     gint            *minimum_size,
57                                                                     gint            *natural_size);
58
59
60
61 typedef struct {
62   gint    size; /* The size of the row in the scaffold's opposing orientation */
63 } RowData;
64
65 struct _CellAreaScaffoldPrivate {
66
67   /* The model we're showing data for */
68   GtkTreeModel    *model;
69
70   /* The area rendering the data and a global iter */
71   GtkCellArea     *area;
72   GtkCellAreaIter *iter;
73
74   /* Cache some info about rows (hieghts etc) */
75   GArray          *row_data;
76 };
77
78
79 #define ROW_SPACING  2
80
81 enum {
82   PROP_0,
83   PROP_ORIENTATION
84 };
85
86 G_DEFINE_TYPE_WITH_CODE (CellAreaScaffold, cell_area_scaffold, GTK_TYPE_WIDGET,
87                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
88
89
90 static void
91 cell_area_scaffold_init (CellAreaScaffold *scaffold)
92 {
93   CellAreaScaffoldPrivate *priv;
94
95   scaffold->priv = G_TYPE_INSTANCE_GET_PRIVATE (scaffold,
96                                                 TYPE_CELL_AREA_SCAFFOLD,
97                                                 CellAreaScaffoldPrivate);
98   priv = scaffold->priv;
99
100   priv->area = gtk_cell_area_box_new ();
101   priv->iter = gtk_cell_area_create_iter (priv->area);
102
103   priv->row_data = g_array_new (FALSE, FALSE, sizeof (RowData));
104
105   gtk_widget_set_has_window (GTK_WIDGET (scaffold), FALSE);
106 }
107
108 static void
109 cell_area_scaffold_class_init (CellAreaScaffoldClass *class)
110 {
111   GObjectClass   *gobject_class;
112   GtkWidgetClass *widget_class;
113
114   gobject_class = G_OBJECT_CLASS(class);
115   gobject_class->dispose = cell_area_scaffold_dispose;
116   gobject_class->finalize = cell_area_scaffold_finalize;
117   gobject_class->get_property = cell_area_scaffold_get_property;
118   gobject_class->set_property = cell_area_scaffold_set_property;
119
120   widget_class = GTK_WIDGET_CLASS(class);
121   widget_class->draw = cell_area_scaffold_draw;
122   widget_class->size_allocate = cell_area_scaffold_size_allocate;
123   widget_class->get_preferred_width = cell_area_scaffold_get_preferred_width;
124   widget_class->get_preferred_height_for_width = cell_area_scaffold_get_preferred_height_for_width;
125   widget_class->get_preferred_height = cell_area_scaffold_get_preferred_height;
126   widget_class->get_preferred_width_for_height = cell_area_scaffold_get_preferred_width_for_height;
127
128   g_object_class_override_property (gobject_class, PROP_ORIENTATION, "orientation");
129
130   g_type_class_add_private (gobject_class, sizeof (CellAreaScaffoldPrivate));
131 }
132
133 /*********************************************************
134  *                    GObjectClass                       *
135  *********************************************************/
136 static void
137 cell_area_scaffold_finalize (GObject *object)
138 {
139   CellAreaScaffold        *scaffold = CELL_AREA_SCAFFOLD (object);
140   CellAreaScaffoldPrivate *priv;
141
142   priv = scaffold->priv;
143
144   g_array_free (priv->row_data, TRUE);
145
146   G_OBJECT_CLASS (cell_area_scaffold_parent_class)->finalize (object);
147 }
148
149 static void
150 cell_area_scaffold_dispose (GObject *object)
151 {
152   CellAreaScaffold        *scaffold = CELL_AREA_SCAFFOLD (object);
153   CellAreaScaffoldPrivate *priv;
154
155   priv = scaffold->priv;
156
157   cell_area_scaffold_set_model (scaffold, NULL);
158
159   if (priv->iter)
160     {
161       g_object_unref (priv->iter);
162       priv->iter = NULL;
163     }
164
165   if (priv->area)
166     {
167       g_object_unref (priv->area);
168       priv->area = NULL;
169     }
170
171   G_OBJECT_CLASS (cell_area_scaffold_parent_class)->dispose (object);  
172 }
173
174 static void
175 cell_area_scaffold_set_property (GObject      *object,
176                                  guint         prop_id,
177                                  const GValue *value,
178                                  GParamSpec   *pspec)
179 {
180   CellAreaScaffold        *scaffold = CELL_AREA_SCAFFOLD (object);
181   CellAreaScaffoldPrivate *priv;
182
183   priv = scaffold->priv;
184
185   switch (prop_id)
186     {
187     case PROP_ORIENTATION:
188       gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->area), 
189                                       g_value_get_enum (value));
190       break;
191     default:
192       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193       break;
194     }
195 }
196
197 static void      
198 cell_area_scaffold_get_property (GObject    *object,
199                                  guint       prop_id,
200                                  GValue     *value,
201                                  GParamSpec *pspec)
202 {
203   CellAreaScaffold        *scaffold = CELL_AREA_SCAFFOLD (object);
204   CellAreaScaffoldPrivate *priv;
205
206   priv = scaffold->priv;
207
208   switch (prop_id)
209     {
210     case PROP_ORIENTATION:
211       g_value_set_enum (value, 
212                         gtk_orientable_get_orientation (GTK_ORIENTABLE (priv->area)));
213       break;
214     default:
215       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216       break;
217     }
218 }
219
220
221 /*********************************************************
222  *                    GtkWidgetClass                     *
223  *********************************************************/
224 static gboolean
225 cell_area_scaffold_draw (GtkWidget       *widget,
226                          cairo_t         *cr)
227 {
228   CellAreaScaffold        *scaffold = CELL_AREA_SCAFFOLD (widget);
229   CellAreaScaffoldPrivate *priv     = scaffold->priv;
230   GtkOrientation           orientation;
231   GtkTreeIter              iter;
232   gboolean                 valid;
233   GdkRectangle             render_area;
234   GtkAllocation            allocation;
235   gint                     i = 0;
236
237   if (!priv->model)
238     return FALSE;
239
240   orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (priv->area));
241
242   gtk_widget_get_allocation (widget, &allocation);
243
244   render_area.x      = 0;
245   render_area.y      = 0;
246   render_area.width  = allocation.width;
247   render_area.height = allocation.height;
248
249   valid = gtk_tree_model_get_iter_first (priv->model, &iter);
250   while (valid)
251     {
252       RowData *data = &g_array_index (priv->row_data, RowData, i);
253
254       if (orientation == GTK_ORIENTATION_HORIZONTAL)
255         {
256           render_area.height = data->size;
257         }
258       else
259         {
260           render_area.width = data->size;
261         }
262
263       gtk_cell_area_apply_attributes (priv->area, priv->model, &iter, FALSE, FALSE);
264       gtk_cell_area_render (priv->area, priv->iter, widget, cr, &render_area, 0);
265
266       if (orientation == GTK_ORIENTATION_HORIZONTAL)
267         {
268           render_area.y += data->size;
269           render_area.y += ROW_SPACING;
270         }
271       else
272         {
273           render_area.x += data->size;
274           render_area.x += ROW_SPACING;
275         }
276
277       i++;
278       valid = gtk_tree_model_iter_next (priv->model, &iter);
279     }
280
281   return FALSE;
282 }
283
284 static void 
285 request_all_base (CellAreaScaffold *scaffold)
286 {
287   CellAreaScaffoldPrivate *priv = scaffold->priv;
288   GtkWidget               *widget = GTK_WIDGET (scaffold);
289   GtkOrientation           orientation;
290   GtkTreeIter              iter;
291   gboolean                 valid;
292
293   if (!priv->model)
294     return;
295
296   orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (priv->area));
297
298   valid = gtk_tree_model_get_iter_first (priv->model, &iter);
299   while (valid)
300     {
301       gint min, nat;
302
303       gtk_cell_area_apply_attributes (priv->area, priv->model, &iter, FALSE, FALSE);
304
305       if (orientation == GTK_ORIENTATION_HORIZONTAL)
306         gtk_cell_area_get_preferred_width (priv->area, priv->iter, widget, &min, &nat);
307       else
308         gtk_cell_area_get_preferred_height (priv->area, priv->iter, widget, &min, &nat);
309
310       valid = gtk_tree_model_iter_next (priv->model, &iter);
311     }
312
313   if (orientation == GTK_ORIENTATION_HORIZONTAL)
314     gtk_cell_area_iter_sum_preferred_width (priv->iter);
315   else
316     gtk_cell_area_iter_sum_preferred_height (priv->iter);
317 }
318
319 static void 
320 get_row_sizes (CellAreaScaffold *scaffold, 
321                GArray           *array,
322                gint              for_size)
323 {
324   CellAreaScaffoldPrivate *priv = scaffold->priv;
325   GtkWidget               *widget = GTK_WIDGET (scaffold);
326   GtkOrientation           orientation;
327   GtkTreeIter              iter;
328   gboolean                 valid;
329   gint                     i = 0;
330
331   if (!priv->model)
332     return;
333
334   orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (priv->area));
335
336   valid = gtk_tree_model_get_iter_first (priv->model, &iter);
337   while (valid)
338     {
339       RowData *data = &g_array_index (array, RowData, i);
340
341       gtk_cell_area_apply_attributes (priv->area, priv->model, &iter, FALSE, FALSE);
342
343       if (orientation == GTK_ORIENTATION_HORIZONTAL)
344         gtk_cell_area_get_preferred_height_for_width (priv->area, priv->iter, widget, 
345                                                       for_size, &data->size, NULL);
346       else
347         gtk_cell_area_get_preferred_width_for_height (priv->area, priv->iter, widget, 
348                                                       for_size, &data->size, NULL);
349
350       i++;
351       valid = gtk_tree_model_iter_next (priv->model, &iter);
352     }
353 }
354
355 static void
356 cell_area_scaffold_size_allocate (GtkWidget           *widget,
357                                   GtkAllocation       *allocation)
358 {
359   CellAreaScaffold        *scaffold = CELL_AREA_SCAFFOLD (widget);
360   CellAreaScaffoldPrivate *priv     = scaffold->priv;
361   GtkOrientation           orientation;
362
363   if (!priv->model)
364     return;
365
366   gtk_widget_set_allocation (widget, allocation);
367
368   orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (priv->area));
369
370   /* Cache the per-row sizes and allocate the iter */
371   if (orientation == GTK_ORIENTATION_HORIZONTAL)
372     {
373       get_row_sizes (scaffold, priv->row_data, allocation->width);
374       gtk_cell_area_iter_allocate_width (priv->iter, allocation->width);
375     }
376   else
377     {
378       get_row_sizes (scaffold, priv->row_data, allocation->height);
379       gtk_cell_area_iter_allocate_height (priv->iter, allocation->height);
380     }
381 }
382
383
384 static void
385 cell_area_scaffold_get_preferred_width (GtkWidget       *widget,
386                                         gint            *minimum_size,
387                                         gint            *natural_size)
388 {
389   CellAreaScaffold        *scaffold = CELL_AREA_SCAFFOLD (widget);
390   CellAreaScaffoldPrivate *priv     = scaffold->priv;
391   GtkOrientation           orientation;
392
393   if (!priv->model)
394     return;
395
396   orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (priv->area));
397
398   if (orientation == GTK_ORIENTATION_HORIZONTAL)
399     {
400       request_all_base (scaffold);
401
402       gtk_cell_area_iter_get_preferred_width (priv->iter, minimum_size, natural_size);
403     }
404   else
405     {
406       gint min_size, nat_size;
407
408       GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, &min_size, &nat_size);
409       GTK_WIDGET_GET_CLASS (widget)->get_preferred_width_for_height (widget, min_size, 
410                                                                      minimum_size, natural_size);
411     }
412 }
413
414 static void
415 cell_area_scaffold_get_preferred_height_for_width (GtkWidget       *widget,
416                                                    gint             for_size,
417                                                    gint            *minimum_size,
418                                                    gint            *natural_size)
419 {
420   CellAreaScaffold        *scaffold = CELL_AREA_SCAFFOLD (widget);
421   CellAreaScaffoldPrivate *priv     = scaffold->priv;
422   GtkOrientation           orientation;
423
424   if (!priv->model)
425     return;
426
427   orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (priv->area));
428
429   if (orientation == GTK_ORIENTATION_HORIZONTAL)
430     {
431       GArray *request_array;
432       gint    n_rows, i, full_size = 0;
433
434       n_rows = gtk_tree_model_iter_n_children (priv->model, NULL);
435
436       /* Get an array for the contextual request */
437       request_array = g_array_new (FALSE, FALSE, sizeof (RowData));
438       g_array_set_size (request_array, n_rows);
439       memset (request_array->data, 0x0, n_rows * sizeof (RowData));
440
441       /* Gather each contextual size into the request array */
442       get_row_sizes (scaffold, request_array, for_size);
443
444       /* Sum up the size and add some row spacing */
445       for (i = 0; i < n_rows; i++)
446         {
447           RowData *data = &g_array_index (request_array, RowData, i);
448
449           full_size += data->size;
450         }
451
452       full_size += MAX (0, n_rows -1) * ROW_SPACING;
453
454       g_array_free (request_array, TRUE);
455
456       *minimum_size = full_size;
457       *natural_size = full_size;
458     }
459   else
460     {
461       GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_size, natural_size);
462     }
463 }
464
465 static void
466 cell_area_scaffold_get_preferred_height (GtkWidget       *widget,
467                                          gint            *minimum_size,
468                                          gint            *natural_size)
469 {
470   CellAreaScaffold        *scaffold = CELL_AREA_SCAFFOLD (widget);
471   CellAreaScaffoldPrivate *priv     = scaffold->priv;
472   GtkOrientation           orientation;
473
474   if (!priv->model)
475     return;
476
477   orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (priv->area));
478
479   if (orientation == GTK_ORIENTATION_VERTICAL)
480     {
481       request_all_base (scaffold);
482
483       gtk_cell_area_iter_get_preferred_height (priv->iter, minimum_size, natural_size);
484     }
485   else
486     {
487       gint min_size, nat_size;
488
489       GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_size, &nat_size);
490       GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_size, 
491                                                                      minimum_size, natural_size);
492     }
493 }
494
495 static void
496 cell_area_scaffold_get_preferred_width_for_height (GtkWidget       *widget,
497                                                    gint             for_size,
498                                                    gint            *minimum_size,
499                                                    gint            *natural_size)
500 {
501   CellAreaScaffold        *scaffold = CELL_AREA_SCAFFOLD (widget);
502   CellAreaScaffoldPrivate *priv     = scaffold->priv;
503   GtkOrientation           orientation;
504
505   if (!priv->model)
506     return;
507
508   orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (priv->area));
509
510   if (orientation == GTK_ORIENTATION_VERTICAL)
511     {
512       GArray *request_array;
513       gint    n_rows, i, full_size = 0;
514
515       n_rows = gtk_tree_model_iter_n_children (priv->model, NULL);
516
517       /* Get an array for the contextual request */
518       request_array = g_array_new (FALSE, FALSE, sizeof (RowData));
519       g_array_set_size (request_array, n_rows);
520       memset (request_array->data, 0x0, n_rows * sizeof (RowData));
521
522       /* Gather each contextual size into the request array */
523       get_row_sizes (scaffold, request_array, for_size);
524
525       /* Sum up the size and add some row spacing */
526       for (i = 0; i < n_rows; i++)
527         {
528           RowData *data = &g_array_index (request_array, RowData, i);
529
530           full_size += data->size;
531         }
532
533       full_size += MAX (0, n_rows -1) * ROW_SPACING;
534
535       g_array_free (request_array, TRUE);
536
537       *minimum_size = full_size;
538       *natural_size = full_size;
539     }
540   else
541     {
542       GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
543     }
544 }
545
546
547
548 /*********************************************************
549  *                         API                           *
550  *********************************************************/
551 GtkWidget *
552 cell_area_scaffold_new (void)
553 {
554   return (GtkWidget *)g_object_new (TYPE_CELL_AREA_SCAFFOLD, NULL);
555 }
556
557 GtkCellArea  *
558 cell_area_scaffold_get_area (CellAreaScaffold *scaffold)
559 {
560   CellAreaScaffoldPrivate *priv;
561   
562   g_return_val_if_fail (IS_CELL_AREA_SCAFFOLD (scaffold), NULL);
563
564   priv = scaffold->priv;
565
566   return priv->area;
567 }
568
569 void
570 cell_area_scaffold_set_model (CellAreaScaffold *scaffold,
571                               GtkTreeModel     *model)
572 {
573   CellAreaScaffoldPrivate *priv;
574   
575   g_return_if_fail (IS_CELL_AREA_SCAFFOLD (scaffold));
576
577   priv = scaffold->priv;
578
579   if (priv->model != model)
580     {
581       if (priv->model)
582         {
583           /* XXX disconnect signals */
584           g_object_unref (priv->model);
585         }
586
587       priv->model = model;
588
589       if (priv->model)
590         {
591           gint n_rows;
592
593           /* XXX connect signals */
594           g_object_ref (priv->model);
595
596           n_rows = gtk_tree_model_iter_n_children (priv->model, NULL);
597
598           /* Clear/reset the array */
599           g_array_set_size (priv->row_data, n_rows);
600           memset (priv->row_data->data, 0x0, n_rows * sizeof (RowData));
601         }
602       else
603         {
604           g_array_set_size (priv->row_data, 0);
605         }
606
607       gtk_cell_area_iter_flush (priv->iter);
608
609       gtk_widget_queue_resize (GTK_WIDGET (scaffold));
610     }
611 }
612
613 GtkTreeModel *
614 cell_area_scaffold_get_model (CellAreaScaffold *scaffold)
615 {
616   CellAreaScaffoldPrivate *priv;
617   
618   g_return_val_if_fail (IS_CELL_AREA_SCAFFOLD (scaffold), NULL);
619
620   priv = scaffold->priv;
621
622   return priv->model;
623 }