]> Pileus Git - ~andy/gtk/blob - gtk/gtktreeviewcolumn.c
1d7dab0eee279e0defbb16242558359675b7f9c7
[~andy/gtk] / gtk / gtktreeviewcolumn.c
1 /* gtktreeviewcolumn.c
2  * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "gtktreeviewcolumn.h"
21 #include "gtktreeprivate.h"
22 #include "gtksignal.h"
23 #include "gtkbutton.h"
24 #include "gtkalignment.h"
25 #include "gtklabel.h"
26 #include "gtkintl.h"
27
28 enum
29 {
30   PROP_0,
31   
32   PROP_CELL_RENDERER,
33   PROP_VISIBLE,
34   PROP_SIZING,
35   PROP_WIDTH,
36   PROP_MIN_WIDTH,
37   PROP_MAX_WIDTH,
38   PROP_TITLE,
39   PROP_CLICKABLE,
40   PROP_WIDGET,
41   PROP_JUSTIFICATION
42 };
43
44 enum
45 {
46   CLICKED,
47   LAST_SIGNAL
48 };
49
50
51 static void gtk_tree_view_column_init            (GtkTreeViewColumn      *tree_column);
52 static void gtk_tree_view_column_class_init      (GtkTreeViewColumnClass *klass);
53 static void gtk_tree_view_column_set_attributesv (GtkTreeViewColumn      *tree_column,
54                                                   va_list                 args);
55 static void gtk_real_tree_column_clicked         (GtkTreeViewColumn      *tree_column);
56 static void gtk_tree_view_column_set_property    (GObject         *object,
57                                                   guint            prop_id,
58                                                   const GValue    *value,
59                                                   GParamSpec      *pspec,
60                                                   const gchar     *trailer);
61 static void gtk_tree_view_column_get_property    (GObject         *object,
62                                                   guint            prop_id,
63                                                   GValue          *value,
64                                                   GParamSpec      *pspec,
65                                                   const gchar     *trailer);
66
67
68
69 static GtkObjectClass *parent_class = NULL;
70 static guint tree_column_signals[LAST_SIGNAL] = { 0 };
71
72
73 GtkType
74 gtk_tree_view_column_get_type (void)
75 {
76   static GtkType tree_column_type = 0;
77
78   if (!tree_column_type)
79     {
80       static const GTypeInfo tree_column_info =
81       {
82         sizeof (GtkTreeViewColumnClass),
83         NULL,           /* base_init */
84         NULL,           /* base_finalize */
85         (GClassInitFunc) gtk_tree_view_column_class_init,
86         NULL,           /* class_finalize */
87         NULL,           /* class_data */
88         sizeof (GtkTreeViewColumn),
89         0,
90         (GInstanceInitFunc) gtk_tree_view_column_init,
91       };
92
93       tree_column_type = g_type_register_static (GTK_TYPE_OBJECT, "GtkTreeViewColumn", &tree_column_info, 0);
94     }
95
96   return tree_column_type;
97 }
98
99 static void
100 gtk_tree_view_column_class_init (GtkTreeViewColumnClass *class)
101 {
102   GObjectClass *object_class;
103
104   object_class = (GObjectClass*) class;
105
106   parent_class = g_type_class_peek_parent (class);
107
108   class->clicked = gtk_real_tree_column_clicked;
109
110   object_class->set_property = gtk_tree_view_column_set_property;
111   object_class->get_property = gtk_tree_view_column_get_property;
112   
113   tree_column_signals[CLICKED] =
114     g_signal_newc ("clicked",
115                    GTK_CLASS_TYPE (object_class),
116                    G_SIGNAL_RUN_LAST,
117                    G_STRUCT_OFFSET (GtkTreeViewColumnClass, clicked),
118                    NULL,
119                    gtk_marshal_VOID__VOID,
120                    GTK_TYPE_NONE, 0);
121
122   g_object_class_install_property (object_class,
123                                    PROP_CELL_RENDERER,
124                                    g_param_spec_object ("cell_renderer",
125                                                         _("Cell renderer"),
126                                                         _("Cell renderer object to use for rendering the cell"),
127                                                         GTK_TYPE_CELL_RENDERER,
128                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
129   
130   g_object_class_install_property (object_class,
131                                    PROP_VISIBLE,
132                                    g_param_spec_boolean ("visible",
133                                                         _("Visible"),
134                                                         _("Whether to display the colomn"),
135                                                          TRUE,
136                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
137   
138   g_object_class_install_property (object_class,
139                                    PROP_SIZING,
140                                    g_param_spec_enum ("sizing",
141                                                       _("Sizing"),
142                                                       _("Resize mode of the column"),
143                                                       GTK_TYPE_TREE_VIEW_COLUMN_SIZING,
144                                                       GTK_TREE_VIEW_COLUMN_AUTOSIZE,
145                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
146   
147   g_object_class_install_property (object_class,
148                                    PROP_WIDTH,
149                                    g_param_spec_int ("width",
150                                                      _("Width"),
151                                                      _("Current width of the column"),
152                                                      1,
153                                                      G_MAXINT,
154                                                      1, /* not useful */
155                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
156
157   g_object_class_install_property (object_class,
158                                    PROP_MIN_WIDTH,
159                                    g_param_spec_int ("min_width",
160                                                      _("Minimum Width"),
161                                                      _("Minimum allowed width of the column"),
162                                                      -1,
163                                                      G_MAXINT,
164                                                      1,
165                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
166
167   g_object_class_install_property (object_class,
168                                    PROP_MAX_WIDTH,
169                                    g_param_spec_int ("max_width",
170                                                      _("Maximum Width"),
171                                                      _("Maximum allowed width of the column"),
172                                                      -1,
173                                                      G_MAXINT,
174                                                      G_MAXINT,
175                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
176
177   g_object_class_install_property (object_class,
178                                    PROP_TITLE,
179                                    g_param_spec_string ("title",
180                                                         _("Title"),
181                                                         _("Title to appear in column header"),
182                                                         "",
183                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
184   
185   g_object_class_install_property (object_class,
186                                    PROP_CLICKABLE,
187                                    g_param_spec_boolean ("clickable",
188                                                         _("Clickable"),
189                                                         _("Whether the header can be clicked"),
190                                                          TRUE,
191                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
192   
193
194   g_object_class_install_property (object_class,
195                                    PROP_WIDGET,
196                                    g_param_spec_object ("widget",
197                                                         _("Widget"),
198                                                         _("Widget to put in column header button instead of column title"),
199                                                         GTK_TYPE_WIDGET,
200                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
201
202   g_object_class_install_property (object_class,
203                                    PROP_JUSTIFICATION,
204                                    g_param_spec_enum ("justification",
205                                                       _("Justification"),
206                                                       _("Justification of the column"),
207                                                       GTK_TYPE_JUSTIFICATION,
208                                                       GTK_JUSTIFY_LEFT,
209                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
210 }
211
212 static void
213 gtk_tree_view_column_init (GtkTreeViewColumn *tree_column)
214 {
215   /* FIXME remove on port to GtkObject */
216   gtk_object_ref (GTK_OBJECT (tree_column));
217   gtk_object_sink (GTK_OBJECT (tree_column));
218   
219   tree_column->button = NULL;
220   tree_column->justification = GTK_JUSTIFY_LEFT;
221   tree_column->width = 1;
222   tree_column->min_width = -1;
223   tree_column->max_width = -1;
224   tree_column->cell = NULL;
225   tree_column->attributes = NULL;
226   tree_column->column_type = GTK_TREE_VIEW_COLUMN_AUTOSIZE;
227   tree_column->visible = TRUE;
228   tree_column->button_active = FALSE;
229   tree_column->dirty = TRUE;
230 }
231
232 static void
233 gtk_tree_view_column_set_property (GObject         *object,
234                                    guint            prop_id,
235                                    const GValue    *value,
236                                    GParamSpec      *pspec,
237                                    const gchar     *trailer)
238 {
239   GtkTreeViewColumn *tree_column;
240
241   tree_column = GTK_TREE_VIEW_COLUMN (object);
242
243   switch (prop_id)
244     {
245     case PROP_CELL_RENDERER:
246       gtk_tree_view_column_set_cell_renderer (tree_column,
247                                               (GtkCellRenderer*)
248                                               g_value_get_object (value));
249       break;
250
251     case PROP_VISIBLE:
252       gtk_tree_view_column_set_visible (tree_column,
253                                         g_value_get_boolean (value));
254       break;
255
256     case PROP_SIZING:
257       gtk_tree_view_column_set_sizing (tree_column,
258                                        g_value_get_enum (value));
259       break;
260
261     case PROP_WIDTH:
262       gtk_tree_view_column_set_width (tree_column,
263                                       g_value_get_int (value));
264       break;
265
266     case PROP_MIN_WIDTH:
267       gtk_tree_view_column_set_min_width (tree_column,
268                                           g_value_get_int (value));
269       break;
270
271     case PROP_MAX_WIDTH:
272       gtk_tree_view_column_set_max_width (tree_column,
273                                           g_value_get_int (value));
274       break;
275
276     case PROP_TITLE:
277       gtk_tree_view_column_set_title (tree_column,
278                                       g_value_get_string (value));
279       break;
280
281     case PROP_CLICKABLE:
282       gtk_tree_view_column_set_clickable (tree_column,
283                                           g_value_get_boolean (value));
284       break;
285
286     case PROP_WIDGET:
287       gtk_tree_view_column_set_widget (tree_column,
288                                        (GtkWidget*) g_value_get_object (value));
289       break;
290
291     case PROP_JUSTIFICATION:
292       gtk_tree_view_column_set_justification (tree_column,
293                                               g_value_get_enum (value));
294       break;
295
296     default:
297       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
298       break;
299     }
300 }
301
302 static void
303 gtk_tree_view_column_get_property (GObject         *object,
304                                    guint            prop_id,
305                                    GValue          *value,
306                                    GParamSpec      *pspec,
307                                    const gchar     *trailer)
308 {
309   GtkTreeViewColumn *tree_column;
310
311   tree_column = GTK_TREE_VIEW_COLUMN (object);
312
313   switch (prop_id)
314     {
315     case PROP_CELL_RENDERER:
316       g_value_set_object (value, (GObject*) tree_column->cell);
317       break;
318
319     case PROP_VISIBLE:
320       g_value_set_boolean (value, tree_column->visible);
321       break;
322
323     case PROP_SIZING:
324       g_value_set_enum (value, tree_column->column_type);
325       break;
326
327     case PROP_WIDTH:
328       g_value_set_int (value, tree_column->width);
329       break;
330
331     case PROP_MIN_WIDTH:
332       g_value_set_int (value, tree_column->min_width);
333       break;
334
335     case PROP_MAX_WIDTH:
336       g_value_set_int (value, tree_column->max_width);
337       break;
338
339     case PROP_TITLE:
340       g_value_set_string (value, tree_column->title);
341       break;
342
343     case PROP_CLICKABLE:
344       g_value_set_boolean (value, tree_column->button_active);
345       break;
346
347     case PROP_WIDGET:
348       g_warning ("FIXME");
349       break;
350
351     case PROP_JUSTIFICATION:
352       g_value_set_enum (value, tree_column->justification);
353       break;
354
355     default:
356       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
357       break;
358     }
359
360 }
361
362 /* used to make the buttons 'unclickable' */
363
364 static gint
365 gtk_tree_view_passive_func (GtkWidget *widget,
366                             GdkEvent  *event,
367                             gpointer   data)
368 {
369   g_return_val_if_fail (event != NULL, FALSE);
370
371   switch (event->type)
372     {
373     case GDK_MOTION_NOTIFY:
374     case GDK_BUTTON_PRESS:
375     case GDK_2BUTTON_PRESS:
376     case GDK_3BUTTON_PRESS:
377     case GDK_BUTTON_RELEASE:
378     case GDK_ENTER_NOTIFY:
379     case GDK_LEAVE_NOTIFY:
380       return TRUE;
381     default:
382       break;
383     }
384   return FALSE;
385 }
386
387 static void
388 gtk_real_tree_column_clicked (GtkTreeViewColumn *tree_column)
389 {
390   g_return_if_fail (tree_column != NULL);
391   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
392
393 }
394
395 /**
396  * gtk_tree_view_column_new:
397  * 
398  * Creates a new #GtkTreeViewColumn.
399  * 
400  * Return value: A newly created #GtkTreeViewColumn.
401  **/
402 GtkTreeViewColumn *
403 gtk_tree_view_column_new (void)
404 {
405   GtkTreeViewColumn *retval;
406
407   retval = GTK_TREE_VIEW_COLUMN (gtk_type_new (GTK_TYPE_TREE_COLUMN));
408
409   return retval;
410 }
411
412 /**
413  * gtk_tree_view_column_new_with_attributes:
414  * @title: The title to set the header to.
415  * @cell: The #GtkCellRenderer.
416  * @Varargs: A NULL terminated list of attributes.
417  * 
418  * Creates a new #GtkTreeViewColumn with a number of default values.  This is
419  * equivalent to calling @gtk_tree_view_column_set_title,
420  * @gtk_tree_view_column_set_cell_renderer, and
421  * @gtk_tree_view_column_set_attributes on the newly created #GtkTreeViewColumn.
422  * 
423  * Return value: A newly created #GtkTreeViewColumn.
424  **/
425 GtkTreeViewColumn *
426 gtk_tree_view_column_new_with_attributes (gchar           *title,
427                                           GtkCellRenderer *cell,
428                                           ...)
429 {
430   GtkTreeViewColumn *retval;
431   va_list args;
432
433   retval = gtk_tree_view_column_new ();
434
435   gtk_tree_view_column_set_title (retval, title);
436   gtk_tree_view_column_set_cell_renderer (retval, cell);
437
438   va_start (args, cell);
439   gtk_tree_view_column_set_attributesv (retval, args);
440   va_end (args);
441
442   return retval;
443 }
444
445 /**
446  * gtk_tree_view_column_set_cell_renderer:
447  * @tree_column: A #GtkTreeViewColumn.
448  * @cell: The #GtkCellRenderer, or NULL.
449  * 
450  * Sets the cell renderer of the @tree_column.  If there is a cell
451  * renderer already set, then it is removed.  If @cell is NULL, then
452  * the cell renderer is unset.
453  **/
454 void
455 gtk_tree_view_column_set_cell_renderer (GtkTreeViewColumn *tree_column,
456                                         GtkCellRenderer   *cell)
457 {
458   g_return_if_fail (tree_column != NULL);
459   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
460   if (cell)
461     g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
462
463   if (cell)
464     g_object_ref (G_OBJECT (cell));
465
466   if (tree_column->cell)
467     g_object_unref (G_OBJECT (tree_column->cell));
468
469   tree_column->cell = cell;
470
471   g_object_notify (G_OBJECT (tree_column), "cell_renderer");
472 }
473
474 /**
475  * gtk_tree_view_column_add_attribute:
476  * @tree_column: A #GtkTreeViewColumn.
477  * @attribute: An attribute on the columns cell
478  * @column: The column position on the model to get the attribute from.
479  * 
480  * Adds an attribute mapping to the list in @tree_column.  The @column is the
481  * column position on the model to get a value from, and the @attribute is the
482  * parameter on the @tree_column's cell renderer to set that value to.
483  **/
484 void
485 gtk_tree_view_column_add_attribute (GtkTreeViewColumn *tree_column,
486                                     gchar             *attribute,
487                                     gint               column)
488 {
489   g_return_if_fail (tree_column != NULL);
490   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
491
492   tree_column->attributes = g_slist_prepend (tree_column->attributes,
493                                              GINT_TO_POINTER (column));
494   tree_column->attributes = g_slist_prepend (tree_column->attributes,
495                                              g_strdup (attribute));
496 }
497
498 static void
499 gtk_tree_view_column_set_attributesv (GtkTreeViewColumn *tree_column,
500                                       va_list            args)
501 {
502   gchar *attribute;
503   gint column;
504
505   g_return_if_fail (tree_column != NULL);
506   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
507
508   attribute = va_arg (args, gchar *);
509
510   gtk_tree_view_column_clear_attributes (tree_column);
511   
512   while (attribute != NULL)
513     {
514       column = va_arg (args, gint);
515       gtk_tree_view_column_add_attribute (tree_column,
516                                           attribute,
517                                           column);
518       attribute = va_arg (args, gchar *);
519     }
520 }
521
522 /**
523  * gtk_tree_view_column_set_attributes:
524  * @tree_column: A #GtkTreeViewColumn.
525  * @Varargs: A NULL terminated listing of attributes.
526  * 
527  * Sets the attributes in the list as the attributes of @tree_column.
528  * The attributes should be in attribute/column order, as in
529  * @gtk_tree_view_column_add_attribute. All existing attributes
530  * are removed, and replaced with the new attributes.
531  **/
532 void
533 gtk_tree_view_column_set_attributes (GtkTreeViewColumn *tree_column,
534                                      ...)
535 {
536   va_list args;
537
538   g_return_if_fail (tree_column != NULL);
539   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
540
541   va_start (args, tree_column);
542
543   gtk_tree_view_column_set_attributesv (tree_column, args);
544
545   va_end (args);
546 }
547
548 /**
549  * gtk_tree_view_column_clear_attributes:
550  * @tree_column: a #GtkTreeViewColumn
551  * 
552  * Clears all existing attributes previously set with
553  * gtk_tree_view_column_set_attributes().
554  **/
555 void
556 gtk_tree_view_column_clear_attributes (GtkTreeViewColumn *tree_column)
557 {
558   GSList *list;
559   
560   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
561
562   list = tree_column->attributes;
563
564   while (list && list->next)
565     {
566       g_free (list->data);
567       list = list->next->next;
568     }
569   g_slist_free (tree_column->attributes);
570   tree_column->attributes = NULL;
571 }
572
573 /**
574  * gtk_tree_view_column_set_cell_data:
575  * @tree_column: A #GtkTreeViewColumn.
576  * @tree_model: The #GtkTreeModel to to get the cell renderers attributes from.
577  * @iter: The #GtkTreeIter to to get the cell renderer's attributes from.
578  * 
579  * Sets the cell renderer based on the @tree_model and @tree_node.  That is, for
580  * every attribute mapping in @tree_column, it will get a value from the set
581  * column on the @tree_node, and use that value to set the attribute on the cell
582  * renderer.
583  **/
584 void
585 gtk_tree_view_column_set_cell_data (GtkTreeViewColumn *tree_column,
586                                     GtkTreeModel      *tree_model,
587                                     GtkTreeIter       *iter)
588 {
589   GSList *list;
590   GValue value = { 0, };
591   GObject *cell;
592
593   g_return_if_fail (tree_column != NULL);
594   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
595   g_return_if_fail (tree_column->cell != NULL);
596
597   if (tree_model == NULL)
598     return;
599
600   if (tree_column->func && (* tree_column->func) (tree_column,
601                                                   tree_model,
602                                                   iter,
603                                                   tree_column->func_data))
604     return;
605
606   cell = (GObject *) tree_column->cell;
607   list = tree_column->attributes;
608
609   while (list && list->next)
610     {
611       gtk_tree_model_get_value (tree_model, iter,
612                                 GPOINTER_TO_INT (list->next->data),
613                                 &value);
614       g_object_set_property (cell, (gchar *) list->data, &value);
615       g_value_unset (&value);
616       list = list->next->next;
617     }
618 }
619
620 /* Options for manipulating the columns */
621
622 /**
623  * gtk_tree_view_column_set_visible:
624  * @tree_column: A #GtkTreeViewColumn.
625  * @visible: TRUE if the @tree_column is visible.
626  * 
627  * Sets the visibility of @tree_column.
628  **/
629 void
630 gtk_tree_view_column_set_visible (GtkTreeViewColumn *tree_column,
631                                   gboolean           visible)
632 {
633   g_return_if_fail (tree_column != NULL);
634   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
635
636   visible = !! visible;
637   
638   if (tree_column->visible == visible)
639     return;
640
641   tree_column->visible = visible;
642
643   if (visible)
644     {
645       gtk_widget_show (tree_column->button);
646       if (GTK_WIDGET_REALIZED (tree_column->tree_view))
647         gdk_window_show (tree_column->window);
648     }
649   else
650     {
651       gtk_widget_hide (tree_column->button);
652       if (GTK_WIDGET_REALIZED (tree_column->tree_view))
653         gdk_window_hide (tree_column->window);
654     }
655
656   if (GTK_WIDGET_REALIZED (tree_column->tree_view))
657     _gtk_tree_view_set_size (GTK_TREE_VIEW (tree_column->tree_view), -1, -1);
658
659   g_object_notify (G_OBJECT (tree_column), "visible");
660 }
661
662 /**
663  * gtk_tree_view_column_get_visible:
664  * @tree_column: A #GtkTreeViewColumn.
665  * 
666  * Returns TRUE if @tree_column is visible.
667  * 
668  * Return value: whether the column is visible or not.  If it is visible, then
669  * the tree will show the column.
670  **/
671 gboolean
672 gtk_tree_view_column_get_visible (GtkTreeViewColumn *tree_column)
673 {
674   g_return_val_if_fail (tree_column != NULL, FALSE);
675   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), FALSE);
676
677   return tree_column->visible;
678 }
679
680 /**
681  * gtk_tree_view_column_set_sizing:
682  * @tree_column: A #GtkTreeViewColumn.
683  * @type: The #GtkTreeViewColumnSizing.
684  * 
685  * Sets the growth behavior of @tree_column to @type.
686  **/
687 void
688 gtk_tree_view_column_set_sizing (GtkTreeViewColumn     *tree_column,
689                                  GtkTreeViewColumnSizing  type)
690 {
691   g_return_if_fail (tree_column != NULL);
692   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
693
694   if (type == tree_column->column_type)
695     return;
696
697   tree_column->column_type = type;
698   switch (type)
699     {
700     case GTK_TREE_VIEW_COLUMN_AUTOSIZE:
701       tree_column->dirty = TRUE;
702     case GTK_TREE_VIEW_COLUMN_FIXED:
703       if (GTK_WIDGET_REALIZED (tree_column->tree_view))
704         gdk_window_hide (tree_column->window);
705       break;
706     default:
707       if (GTK_WIDGET_REALIZED (tree_column->tree_view))
708         {
709           gdk_window_show (tree_column->window);
710           gdk_window_raise (tree_column->window);
711         }
712       break;
713     }
714
715   gtk_widget_queue_resize (tree_column->tree_view);
716
717   g_object_notify (G_OBJECT (tree_column), "sizing");
718 }
719
720 /**
721  * gtk_tree_view_column_get_sizing:
722  * @tree_column: A #GtkTreeViewColumn.
723  * 
724  * Returns the current type of @tree_column.
725  * 
726  * Return value: The type of @tree_column.
727  **/
728 gint
729 gtk_tree_view_column_get_sizing (GtkTreeViewColumn *tree_column)
730 {
731   g_return_val_if_fail (tree_column != NULL, 0);
732   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), 0);
733
734   return tree_column->column_type;
735 }
736
737 /**
738  * gtk_tree_view_column_get_size:
739  * @tree_column: A #GtkTreeViewColumn.
740  * 
741  * Returns the current size of the @tree_column in pixels.
742  * 
743  * Return value: The current size of the @tree_column.
744  **/
745 gint
746 gtk_tree_view_column_get_size (GtkTreeViewColumn *tree_column)
747 {
748   g_return_val_if_fail (tree_column != NULL, 0);
749   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), 0);
750
751   return tree_column->width;
752 }
753
754 /**
755  * gtk_tree_view_column_set_width:
756  * @tree_column: A #GtkTreeViewColumn.
757  * @size: The size to set the @tree_column to.
758  * 
759  * Sets the size of the column in pixels, unless the the column type is
760  * #GTK_TREE_VIEW_COLUMN_AUTOSIZE.  In this case, the value is discarded as the
761  * size of the column is based on the calculated width of the column. The
762  * width is clamped to the min/max width for the column.
763  **/
764 void
765 gtk_tree_view_column_set_width (GtkTreeViewColumn *tree_column,
766                                 gint               size)
767 {
768   g_return_if_fail (tree_column != NULL);
769   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
770   g_return_if_fail (size > 0);
771   
772   if (tree_column->min_width != -1 &&
773       size < tree_column->min_width)
774     size = tree_column->min_width;
775   else if (tree_column->max_width != -1 &&
776            size > tree_column->max_width)
777     size = tree_column->max_width;
778   
779   if (tree_column->width == size)
780     return;
781   
782   tree_column->width = size;
783   
784   if (GTK_WIDGET_REALIZED (tree_column->tree_view))
785     gtk_widget_queue_resize (tree_column->tree_view);
786   
787   g_object_notify (G_OBJECT (tree_column), "width");
788 }
789
790 /**
791  * gtk_tree_view_column_set_min_width:
792  * @tree_column: A #GtkTreeViewColumn.
793  * @min_width: The minimum width of the column in pixels, or -1.
794  * 
795  * Sets the minimum width of the @tree_column.  If @min_width is -1, then the
796  * minimum width is unset.
797  **/
798 void
799 gtk_tree_view_column_set_min_width (GtkTreeViewColumn *tree_column,
800                                     gint               min_width)
801 {
802   gint real_min_width;
803
804   g_return_if_fail (tree_column != NULL);
805   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
806   g_return_if_fail (min_width >= -1);
807
808   if (min_width == tree_column->min_width)
809     return;
810
811   real_min_width = (tree_column->min_width == -1) ?
812     tree_column->button->requisition.width : tree_column->min_width;
813
814   /* We want to queue a resize if the either the old min_size or the
815    * new min_size determined the size of the column */
816   if (GTK_WIDGET_REALIZED (tree_column->tree_view) &&
817       ((tree_column->min_width > tree_column->width) ||
818        (tree_column->min_width == -1 &&
819         tree_column->button->requisition.width > tree_column->width) ||
820        (min_width > tree_column->width) ||
821        (min_width == -1 &&
822         tree_column->button->requisition.width > tree_column->width)))
823     gtk_widget_queue_resize (tree_column->tree_view);
824
825   if (tree_column->max_width != -1 &&
826       tree_column->max_width < real_min_width)
827     tree_column->max_width = real_min_width;
828
829   tree_column->min_width = min_width;
830
831   g_object_notify (G_OBJECT (tree_column), "min_width");
832 }
833
834 /**
835  * gtk_tree_view_column_get_min_width:
836  * @tree_column: A #GtkTreeViewColumn.
837  * 
838  * Returns the minimum width in pixels of the @tree_column, or -1 if no minimum
839  * width is set.
840  * 
841  * Return value: The minimum width of the @tree_column.
842  **/
843 gint
844 gtk_tree_view_column_get_min_width (GtkTreeViewColumn *tree_column)
845 {
846   g_return_val_if_fail (tree_column != NULL, -1);
847   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), -1);
848
849   return tree_column->min_width;
850 }
851
852 /**
853  * gtk_tree_view_column_set_max_width:
854  * @tree_column: A #GtkTreeViewColumn.
855  * @max_width: The maximum width of the column in pixels, or -1.
856  * 
857  * Sets the maximum width of the @tree_column.  If @max_width is -1, then the
858  * maximum width is unset.
859  **/
860 void
861 gtk_tree_view_column_set_max_width (GtkTreeViewColumn *tree_column,
862                                     gint               max_width)
863 {
864   gint real_min_width;
865
866   g_return_if_fail (tree_column != NULL);
867   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
868   g_return_if_fail (max_width >= -1);
869
870   if (max_width == tree_column->max_width)
871     return;
872
873   real_min_width = tree_column->min_width == -1 ?
874     tree_column->button->requisition.width : tree_column->min_width;
875
876   if (GTK_WIDGET_REALIZED (tree_column->tree_view) &&
877       ((tree_column->max_width < tree_column->width) ||
878        (max_width != -1 && max_width < tree_column->width)))
879     gtk_widget_queue_resize (tree_column->tree_view);
880
881   tree_column->max_width = max_width;
882
883   if (real_min_width > max_width)
884     tree_column->min_width = max_width;
885
886   g_object_notify (G_OBJECT (tree_column), "max_width");
887 }
888
889 /**
890  * gtk_tree_view_column_get_max_width:
891  * @tree_column: A #GtkTreeViewColumn.
892  * 
893  * Returns the maximum width in pixels of the @tree_column, or -1 if no maximum
894  * width is set.
895  * 
896  * Return value: The maximum width of the @tree_column.
897  **/
898 gint
899 gtk_tree_view_column_get_max_width (GtkTreeViewColumn *tree_column)
900 {
901   g_return_val_if_fail (tree_column != NULL, -1);
902   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), -1);
903
904   return tree_column->max_width;
905 }
906
907 /**
908  * gtk_tree_view_column_clicked:
909  * @tree_column: a #GtkTreeViewColumn
910  * 
911  * Emits the "clicked" signal on the column.
912  **/
913 void
914 gtk_tree_view_column_clicked (GtkTreeViewColumn *tree_column)
915 {
916   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
917
918   gtk_signal_emit (GTK_OBJECT (tree_column), tree_column_signals[CLICKED]);
919 }
920
921 static void 
922 update_button_contents (GtkTreeViewColumn *tree_column)
923 {
924   if (tree_column->button)
925     {
926       GtkWidget *alignment = GTK_BIN (tree_column->button)->child;
927       GtkWidget *current_child = GTK_BIN (alignment)->child;
928       
929       if (tree_column->child)
930         {
931           if (current_child != tree_column->child)
932             {
933               gtk_container_remove (GTK_CONTAINER (alignment),
934                                     current_child);
935
936               gtk_container_add (GTK_CONTAINER (alignment),
937                                  tree_column->child);
938             }
939         }
940       else 
941         {
942           if (current_child == NULL)
943             {
944               current_child = gtk_label_new (NULL);
945
946               gtk_widget_show (current_child);
947               
948               gtk_container_add (GTK_CONTAINER (alignment),
949                                  current_child);
950             }
951
952           g_return_if_fail (GTK_IS_LABEL (current_child));
953
954           if (tree_column->title)
955             gtk_label_set_text (GTK_LABEL (current_child),
956                                 tree_column->title);
957           else
958             gtk_label_set_text (GTK_LABEL (current_child),
959                                 "");
960         }
961     }
962 }
963
964 /**
965  * gtk_tree_view_column_set_title:
966  * @tree_column: A #GtkTreeViewColumn.
967  * @title: The title of the @tree_column.
968  * 
969  * Sets the title of the @tree_column.  If a custom widget has been set, then
970  * this value is ignored.
971  **/
972 void
973 gtk_tree_view_column_set_title (GtkTreeViewColumn *tree_column,
974                                 gchar             *title)
975 {
976   g_return_if_fail (tree_column != NULL);
977   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
978
979   g_free (tree_column->title);
980   if (title)
981     tree_column->title = g_strdup (title);
982   else
983     tree_column->title = NULL;
984
985   update_button_contents (tree_column);
986
987   g_object_notify (G_OBJECT (tree_column), "title");
988 }
989
990 /**
991  * gtk_tree_view_column_get_title:
992  * @tree_column: A #GtkTreeViewColumn.
993  * 
994  * Returns the title of the widget.  This value should not be modified.
995  * 
996  * Return value: the title of the column.
997  **/
998 gchar *
999 gtk_tree_view_column_get_title (GtkTreeViewColumn *tree_column)
1000 {
1001   g_return_val_if_fail (tree_column != NULL, NULL);
1002   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), NULL);
1003
1004   return tree_column->title;
1005 }
1006
1007 /**
1008  * gtk_tree_view_column_set_clickable:
1009  * @tree_column: A #GtkTreeViewColumn.
1010  * @active: TRUE if the header is active.
1011  * 
1012  * Sets the header to be active if @active is TRUE.  When the header is active,
1013  * then it can take keyboard focus, and can be clicked.
1014  **/
1015 void
1016 gtk_tree_view_column_set_clickable (GtkTreeViewColumn *tree_column,
1017                                     gboolean           active)
1018 {
1019   g_return_if_fail (tree_column != NULL);
1020   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
1021
1022   if (!tree_column->button)
1023     return;
1024
1025   if (tree_column->button_active == active)
1026     return;
1027
1028   tree_column->button_active = active;
1029   if (active)
1030     {
1031       gtk_signal_disconnect_by_func (GTK_OBJECT (tree_column->button),
1032                                      (GtkSignalFunc) gtk_tree_view_passive_func,
1033                                      NULL);
1034
1035       GTK_WIDGET_SET_FLAGS (tree_column->button, GTK_CAN_FOCUS);
1036
1037       if (GTK_WIDGET_VISIBLE (tree_column->tree_view))
1038         gtk_widget_queue_draw (tree_column->button);
1039     }
1040   else
1041     {
1042       gtk_signal_connect (GTK_OBJECT (tree_column->button),
1043                           "event",
1044                           (GtkSignalFunc) gtk_tree_view_passive_func,
1045                           NULL);
1046
1047       GTK_WIDGET_UNSET_FLAGS (tree_column->button, GTK_CAN_FOCUS);
1048
1049       if (GTK_WIDGET_VISIBLE (tree_column->tree_view))
1050         gtk_widget_queue_draw (tree_column->button);
1051     }
1052
1053   g_object_notify (G_OBJECT (tree_column), "clickable");
1054 }
1055
1056 /**
1057  * gtk_tree_view_column_get_clickable:
1058  * @tree_column: a #GtkTreeViewColumn
1059  * 
1060  * Returns %TRUE if the user can click on the header for the column.
1061  * 
1062  * Return value: whether the user can click the column header
1063  **/
1064 gboolean
1065 gtk_tree_view_column_get_clickable (GtkTreeViewColumn *tree_column)
1066 {
1067   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), FALSE);
1068
1069   return tree_column->button_active;
1070 }
1071
1072 /**
1073  * gtk_tree_view_column_set_widget:
1074  * @tree_column: A #GtkTreeViewColumn.
1075  * @widget: A child #GtkWidget, or NULL.
1076  * 
1077  * Sets the widget in the header to be @widget.  If widget is NULL, then the
1078  * header button is set with a #GtkLabel set to the title of @tree_column.
1079  **/
1080 void
1081 gtk_tree_view_column_set_widget (GtkTreeViewColumn *tree_column,
1082                                  GtkWidget         *widget)
1083 {
1084   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
1085   g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
1086
1087   if (widget)
1088     {
1089       gtk_object_ref (GTK_OBJECT (widget));
1090       gtk_object_sink (GTK_OBJECT (widget));
1091     }
1092
1093   if (tree_column->child)      
1094     gtk_object_unref (GTK_OBJECT (tree_column->child));
1095
1096   tree_column->child = widget;
1097   
1098   update_button_contents (tree_column);
1099
1100   g_object_notify (G_OBJECT (tree_column), "widget");
1101 }
1102
1103 /**
1104  * gtk_tree_view_column_get_widget:
1105  * @tree_column: A #GtkTreeViewColumn.
1106  * 
1107  * Returns the #GtkWidget in the button in the column header.  If a custom
1108  * widget has not been set, then this will be a #GtkAlignment with a #GtkLabel
1109  * in it.
1110  * 
1111  * Return value: The #GtkWidget in the column header.
1112  **/
1113 GtkWidget *
1114 gtk_tree_view_column_get_widget (GtkTreeViewColumn *tree_column)
1115 {
1116   g_return_val_if_fail (tree_column != NULL, NULL);
1117   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), NULL);
1118
1119   return tree_column->child;
1120 }
1121
1122 /**
1123  * gtk_tree_view_column_set_justification:
1124  * @tree_column: A #GtkTreeViewColumn.
1125  * @justification: The justification of the title.
1126  * 
1127  * Sets the justification of the title inside the column header.  If a custom
1128  * widget has been set, then this value is discarded.
1129  **/
1130 void
1131 gtk_tree_view_column_set_justification (GtkTreeViewColumn *tree_column,
1132                                         GtkJustification   justification)
1133 {
1134   GtkWidget *alignment;
1135
1136   g_return_if_fail (tree_column != NULL);
1137   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
1138
1139   if (tree_column->justification == justification)
1140     return;
1141
1142   tree_column->justification = justification;
1143
1144   alignment = GTK_BIN (tree_column->button)->child;
1145
1146   if (GTK_IS_ALIGNMENT (alignment))
1147     {
1148       switch (tree_column->justification)
1149         {
1150         case GTK_JUSTIFY_LEFT:
1151           gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.0, 0.5, 0.0, 0.0);
1152           break;
1153
1154         case GTK_JUSTIFY_RIGHT:
1155           gtk_alignment_set (GTK_ALIGNMENT (alignment), 1.0, 0.5, 0.0, 0.0);
1156           break;
1157
1158         case GTK_JUSTIFY_CENTER:
1159           gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.5, 0.5, 0.0, 0.0);
1160           break;
1161
1162         case GTK_JUSTIFY_FILL:
1163           gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.5, 0.5, 0.0, 0.0);
1164           break;
1165
1166         default:
1167           break;
1168         }
1169     }
1170
1171   g_object_notify (G_OBJECT (tree_column), "justification");
1172 }
1173