gtktreeviewcolumn.c 47 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* gtktreeviewcolumn.c
 * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "gtktreeviewcolumn.h"
21
#include "gtktreeview.h"
22
23
24
25
#include "gtktreeprivate.h"
#include "gtksignal.h"
#include "gtkbutton.h"
#include "gtkalignment.h"
26
#include "gtklabel.h"
Havoc Pennington's avatar
Havoc Pennington committed
27
28
#include "gtkhbox.h"
#include "gtkarrow.h"
29
#include "gtkintl.h"
30

31
32
33
34
35
36
37
38
39
40
41
42
43
enum
{
  PROP_0,
  
  PROP_CELL_RENDERER,
  PROP_VISIBLE,
  PROP_SIZING,
  PROP_WIDTH,
  PROP_MIN_WIDTH,
  PROP_MAX_WIDTH,
  PROP_TITLE,
  PROP_CLICKABLE,
  PROP_WIDGET,
Havoc Pennington's avatar
Havoc Pennington committed
44
45
46
  PROP_ALIGNMENT,
  PROP_SORT_INDICATOR,
  PROP_SORT_ORDER
47
48
49
50
};

enum
{
51
52
53
54
55
56
57
58
59
60
  CLICKED,
  LAST_SIGNAL
};


static void gtk_tree_view_column_init            (GtkTreeViewColumn      *tree_column);
static void gtk_tree_view_column_class_init      (GtkTreeViewColumnClass *klass);
static void gtk_tree_view_column_set_attributesv (GtkTreeViewColumn      *tree_column,
						  va_list                 args);
static void gtk_real_tree_column_clicked         (GtkTreeViewColumn      *tree_column);
61
62
63
64
65
66
67
68
69
70
71
static void gtk_tree_view_column_set_property    (GObject                *object,
						  guint                   prop_id,
						  const GValue           *value,
						  GParamSpec             *pspec,
						  const gchar            *trailer);
static void gtk_tree_view_column_get_property    (GObject                *object,
						  guint                   prop_id,
						  GValue                 *value,
						  GParamSpec             *pspec,
						  const gchar            *trailer);
static void gtk_tree_view_column_finalize        (GObject                *object);
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97


static GtkObjectClass *parent_class = NULL;
static guint tree_column_signals[LAST_SIGNAL] = { 0 };


GtkType
gtk_tree_view_column_get_type (void)
{
  static GtkType tree_column_type = 0;

  if (!tree_column_type)
    {
      static const GTypeInfo tree_column_info =
      {
	sizeof (GtkTreeViewColumnClass),
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_tree_view_column_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkTreeViewColumn),
	0,
	(GInstanceInitFunc) gtk_tree_view_column_init,
      };

98
      tree_column_type = g_type_register_static (GTK_TYPE_OBJECT, "GtkTreeViewColumn", &tree_column_info, 0);
99
100
101
102
103
104
105
106
    }

  return tree_column_type;
}

static void
gtk_tree_view_column_class_init (GtkTreeViewColumnClass *class)
{
107
  GObjectClass *object_class;
108

109
  object_class = (GObjectClass*) class;
110
111
112

  parent_class = g_type_class_peek_parent (class);

113
114
  class->clicked = gtk_real_tree_column_clicked;

115
  object_class->finalize = gtk_tree_view_column_finalize;
116
117
118
  object_class->set_property = gtk_tree_view_column_set_property;
  object_class->get_property = gtk_tree_view_column_get_property;
  
119
  tree_column_signals[CLICKED] =
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    g_signal_newc ("clicked",
                   GTK_CLASS_TYPE (object_class),
                   G_SIGNAL_RUN_LAST,
                   G_STRUCT_OFFSET (GtkTreeViewColumnClass, clicked),
                   NULL,
                   gtk_marshal_VOID__VOID,
                   GTK_TYPE_NONE, 0);

  g_object_class_install_property (object_class,
                                   PROP_CELL_RENDERER,
                                   g_param_spec_object ("cell_renderer",
                                                        _("Cell renderer"),
                                                        _("Cell renderer object to use for rendering the cell"),
                                                        GTK_TYPE_CELL_RENDERER,
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
  
  g_object_class_install_property (object_class,
                                   PROP_VISIBLE,
                                   g_param_spec_boolean ("visible",
                                                        _("Visible"),
                                                        _("Whether to display the colomn"),
                                                         TRUE,
                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
  
  g_object_class_install_property (object_class,
                                   PROP_SIZING,
                                   g_param_spec_enum ("sizing",
                                                      _("Sizing"),
                                                      _("Resize mode of the column"),
                                                      GTK_TYPE_TREE_VIEW_COLUMN_SIZING,
                                                      GTK_TREE_VIEW_COLUMN_AUTOSIZE,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
  
  g_object_class_install_property (object_class,
                                   PROP_WIDTH,
                                   g_param_spec_int ("width",
                                                     _("Width"),
                                                     _("Current width of the column"),
                                                     1,
                                                     G_MAXINT,
                                                     1, /* not useful */
                                                     G_PARAM_READABLE | G_PARAM_WRITABLE));

  g_object_class_install_property (object_class,
                                   PROP_MIN_WIDTH,
                                   g_param_spec_int ("min_width",
                                                     _("Minimum Width"),
                                                     _("Minimum allowed width of the column"),
                                                     -1,
                                                     G_MAXINT,
                                                     1,
                                                     G_PARAM_READABLE | G_PARAM_WRITABLE));

  g_object_class_install_property (object_class,
                                   PROP_MAX_WIDTH,
                                   g_param_spec_int ("max_width",
                                                     _("Maximum Width"),
                                                     _("Maximum allowed width of the column"),
                                                     -1,
                                                     G_MAXINT,
                                                     G_MAXINT,
                                                     G_PARAM_READABLE | G_PARAM_WRITABLE));

  g_object_class_install_property (object_class,
                                   PROP_TITLE,
                                   g_param_spec_string ("title",
                                                        _("Title"),
                                                        _("Title to appear in column header"),
                                                        "",
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
  
  g_object_class_install_property (object_class,
                                   PROP_CLICKABLE,
                                   g_param_spec_boolean ("clickable",
                                                        _("Clickable"),
                                                        _("Whether the header can be clicked"),
                                                         TRUE,
                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
  

  g_object_class_install_property (object_class,
                                   PROP_WIDGET,
                                   g_param_spec_object ("widget",
                                                        _("Widget"),
                                                        _("Widget to put in column header button instead of column title"),
                                                        GTK_TYPE_WIDGET,
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));

  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
                                   PROP_ALIGNMENT,
                                   g_param_spec_float ("alignment",
                                                       _("Alignment"),
                                                       _("Alignment of the column header text or widget"),
                                                       0.0,
                                                       1.0,
                                                       0.5,
                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));

  g_object_class_install_property (object_class,
                                   PROP_SORT_INDICATOR,
                                   g_param_spec_boolean ("sort_indicator",
                                                        _("Sort indicator"),
                                                        _("Whether to show a sort indicator"),
                                                         FALSE,
                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));

  g_object_class_install_property (object_class,
                                   PROP_SORT_ORDER,
                                   g_param_spec_enum ("sort_order",
                                                      _("Sort order"),
                                                      _("Sort direction the sort indicator should indicate"),
                                                      GTK_TYPE_TREE_SORT_ORDER,
                                                      GTK_TREE_SORT_ASCENDING,
233
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
Havoc Pennington's avatar
Havoc Pennington committed
234
  
235
236
237
238
239
}

static void
gtk_tree_view_column_init (GtkTreeViewColumn *tree_column)
{
Havoc Pennington's avatar
Havoc Pennington committed
240
241
242
243
  /* FIXME remove on port to GtkObject */
  gtk_object_ref (GTK_OBJECT (tree_column));
  gtk_object_sink (GTK_OBJECT (tree_column));
  
244
  tree_column->button = NULL;
Havoc Pennington's avatar
Havoc Pennington committed
245
  tree_column->xalign = 0.0;
246
  tree_column->width = 1;
247
248
249
250
251
252
253
254
  tree_column->min_width = -1;
  tree_column->max_width = -1;
  tree_column->cell = NULL;
  tree_column->attributes = NULL;
  tree_column->column_type = GTK_TREE_VIEW_COLUMN_AUTOSIZE;
  tree_column->visible = TRUE;
  tree_column->button_active = FALSE;
  tree_column->dirty = TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
255
256
  tree_column->sort_order = GTK_TREE_SORT_ASCENDING;
  tree_column->show_sort_indicator = FALSE;
257
258
}

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
static void
gtk_tree_view_column_set_property (GObject         *object,
                                   guint            prop_id,
                                   const GValue    *value,
                                   GParamSpec      *pspec,
                                   const gchar     *trailer)
{
  GtkTreeViewColumn *tree_column;

  tree_column = GTK_TREE_VIEW_COLUMN (object);

  switch (prop_id)
    {
    case PROP_CELL_RENDERER:
      gtk_tree_view_column_set_cell_renderer (tree_column,
                                              (GtkCellRenderer*)
                                              g_value_get_object (value));
      break;

    case PROP_VISIBLE:
      gtk_tree_view_column_set_visible (tree_column,
                                        g_value_get_boolean (value));
      break;

    case PROP_SIZING:
      gtk_tree_view_column_set_sizing (tree_column,
                                       g_value_get_enum (value));
      break;

    case PROP_WIDTH:
      gtk_tree_view_column_set_width (tree_column,
                                      g_value_get_int (value));
      break;

    case PROP_MIN_WIDTH:
      gtk_tree_view_column_set_min_width (tree_column,
                                          g_value_get_int (value));
      break;

    case PROP_MAX_WIDTH:
      gtk_tree_view_column_set_max_width (tree_column,
                                          g_value_get_int (value));
      break;

    case PROP_TITLE:
      gtk_tree_view_column_set_title (tree_column,
                                      g_value_get_string (value));
      break;

    case PROP_CLICKABLE:
      gtk_tree_view_column_set_clickable (tree_column,
                                          g_value_get_boolean (value));
      break;

    case PROP_WIDGET:
      gtk_tree_view_column_set_widget (tree_column,
                                       (GtkWidget*) g_value_get_object (value));
      break;

Havoc Pennington's avatar
Havoc Pennington committed
318
319
320
321
322
323
324
325
    case PROP_ALIGNMENT:
      gtk_tree_view_column_set_alignment (tree_column,
                                          g_value_get_float (value));
      break;

    case PROP_SORT_INDICATOR:
      gtk_tree_view_column_set_sort_indicator (tree_column,
                                               g_value_get_boolean (value));
326
327
      break;

Havoc Pennington's avatar
Havoc Pennington committed
328
329
330
331
332
    case PROP_SORT_ORDER:
      gtk_tree_view_column_set_sort_order (tree_column,
                                           g_value_get_enum (value));
      break;
      
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_tree_view_column_get_property (GObject         *object,
                                   guint            prop_id,
                                   GValue          *value,
                                   GParamSpec      *pspec,
                                   const gchar     *trailer)
{
  GtkTreeViewColumn *tree_column;

  tree_column = GTK_TREE_VIEW_COLUMN (object);

  switch (prop_id)
    {
    case PROP_CELL_RENDERER:
Havoc Pennington's avatar
Havoc Pennington committed
353
354
      g_value_set_object (value,
                          (GObject*) gtk_tree_view_column_get_cell_renderer (tree_column));
355
356
357
      break;

    case PROP_VISIBLE:
Havoc Pennington's avatar
Havoc Pennington committed
358
359
      g_value_set_boolean (value,
                           gtk_tree_view_column_get_visible (tree_column));
360
361
362
      break;

    case PROP_SIZING:
Havoc Pennington's avatar
Havoc Pennington committed
363
364
      g_value_set_enum (value,
                        gtk_tree_view_column_get_sizing (tree_column));
365
366
367
      break;

    case PROP_WIDTH:
Havoc Pennington's avatar
Havoc Pennington committed
368
369
      g_value_set_int (value,
                       gtk_tree_view_column_get_width (tree_column));
370
371
372
      break;

    case PROP_MIN_WIDTH:
Havoc Pennington's avatar
Havoc Pennington committed
373
374
      g_value_set_int (value,
                       gtk_tree_view_column_get_min_width (tree_column));
375
376
377
      break;

    case PROP_MAX_WIDTH:
Havoc Pennington's avatar
Havoc Pennington committed
378
379
      g_value_set_int (value,
                       gtk_tree_view_column_get_max_width (tree_column));
380
381
382
      break;

    case PROP_TITLE:
Havoc Pennington's avatar
Havoc Pennington committed
383
384
      g_value_set_string (value,
                          gtk_tree_view_column_get_title (tree_column));
385
386
387
      break;

    case PROP_CLICKABLE:
Havoc Pennington's avatar
Havoc Pennington committed
388
389
      g_value_set_boolean (value,
                           gtk_tree_view_column_get_clickable (tree_column));
390
391
392
      break;

    case PROP_WIDGET:
Havoc Pennington's avatar
Havoc Pennington committed
393
394
      g_value_set_object (value,
                          (GObject*) gtk_tree_view_column_get_widget (tree_column));
395
396
      break;

Havoc Pennington's avatar
Havoc Pennington committed
397
398
399
    case PROP_ALIGNMENT:
      g_value_set_float (value,
                         gtk_tree_view_column_get_alignment (tree_column));
400
401
      break;

Havoc Pennington's avatar
Havoc Pennington committed
402
403
404
405
406
407
408
409
410
411
    case PROP_SORT_INDICATOR:
      g_value_set_boolean (value,
                           gtk_tree_view_column_get_sort_indicator (tree_column));
      break;

    case PROP_SORT_ORDER:
      g_value_set_enum (value,
                        gtk_tree_view_column_get_sort_order (tree_column));
      break;
      
412
413
414
415
416
417
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

418
419
420
421
422
423
424
425
426
427
428
429
static void
gtk_tree_view_column_finalize (GObject *object)
{
  GtkTreeViewColumn *tree_column = (GtkTreeViewColumn *) object;

  if (tree_column->func_data && tree_column->destroy)
    (tree_column->destroy) (tree_column->func_data);

  g_slist_free (tree_column->attributes);
  g_free (tree_column->title);
}

430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* used to make the buttons 'unclickable' */

static gint
gtk_tree_view_passive_func (GtkWidget *widget,
			    GdkEvent  *event,
			    gpointer   data)
{
  g_return_val_if_fail (event != NULL, FALSE);

  switch (event->type)
    {
    case GDK_MOTION_NOTIFY:
    case GDK_BUTTON_PRESS:
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
    case GDK_ENTER_NOTIFY:
    case GDK_LEAVE_NOTIFY:
      return TRUE;
    default:
      break;
    }
  return FALSE;
}

static void
gtk_real_tree_column_clicked (GtkTreeViewColumn *tree_column)
{
  g_return_if_fail (tree_column != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));

}

463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596



void
_gtk_tree_view_column_create_button (GtkTreeViewColumn *column)
{
  GtkTreeView *tree_view;
  GtkWidget *child;
  GtkWidget *hbox;

  tree_view = (GtkTreeView *) column->tree_view;

  g_return_if_fail (tree_view != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
  g_return_if_fail (column->button == NULL);

  gtk_widget_push_composite_child ();
  column->button = gtk_button_new ();
  gtk_widget_pop_composite_child ();

  /* make sure we own a reference to it as well. */
  gtk_widget_set_parent (column->button, GTK_WIDGET (tree_view));
  
  /*  gtk_signal_connect (GTK_OBJECT (button), "clicked",
      (GtkSignalFunc) gtk_tree_view_button_clicked,
      (gpointer) tree_view);*/

  column->alignment = gtk_alignment_new (column->xalign, 0.5, 0.0, 0.0);

  hbox = gtk_hbox_new (FALSE, 2);
  column->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_IN);

  if (column->child)
    child = column->child;
  else
    {
      child = gtk_label_new (column->title);
      gtk_widget_show (child);
    }

  if (column->xalign <= 0.5)
    gtk_box_pack_end (GTK_BOX (hbox), column->arrow, FALSE, FALSE, 0);
  else
    gtk_box_pack_start (GTK_BOX (hbox), column->arrow, FALSE, FALSE, 0);

  gtk_box_pack_start (GTK_BOX (hbox), column->alignment, TRUE, TRUE, 0);
        
  gtk_container_add (GTK_CONTAINER (column->alignment), child);
  gtk_container_add (GTK_CONTAINER (column->button), hbox);

  if (column->visible)
    gtk_widget_show (column->button);

  gtk_widget_show (hbox);
  gtk_widget_show (column->alignment);
}

void
_gtk_tree_view_column_realize_button (GtkTreeViewColumn *column)
{
  GtkTreeView *tree_view;
  GdkWindowAttr attr;
  guint attributes_mask;

  tree_view = (GtkTreeView *)column->tree_view;

  g_return_if_fail (tree_view != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
  g_return_if_fail (GTK_WIDGET_REALIZED (tree_view));
  g_return_if_fail (tree_view->priv->header_window != NULL);
  g_return_if_fail (column->button != NULL);

  attr.window_type = GDK_WINDOW_CHILD;
  attr.wclass = GDK_INPUT_ONLY;
  attr.visual = gtk_widget_get_visual (GTK_WIDGET (tree_view));
  attr.colormap = gtk_widget_get_colormap (GTK_WIDGET (tree_view));
  attr.event_mask = gtk_widget_get_events (GTK_WIDGET (tree_view));
  attr.event_mask = (GDK_BUTTON_PRESS_MASK |
		     GDK_BUTTON_RELEASE_MASK |
		     GDK_POINTER_MOTION_MASK |
		     GDK_POINTER_MOTION_HINT_MASK |
		     GDK_KEY_PRESS_MASK);
  attributes_mask = GDK_WA_CURSOR | GDK_WA_X | GDK_WA_Y;
  attr.cursor = gdk_cursor_new (GDK_SB_H_DOUBLE_ARROW);
  tree_view->priv->cursor_drag = attr.cursor;

  attr.y = 0;
  attr.width = TREE_VIEW_DRAG_WIDTH;
  attr.height = tree_view->priv->header_height;

  gtk_widget_set_parent_window (column->button, tree_view->priv->header_window);

  attr.x = (column->button->allocation.x + column->button->allocation.width) - 3;
          
  column->window = gdk_window_new (tree_view->priv->header_window,
				   &attr, attributes_mask);
  gdk_window_set_user_data (column->window, tree_view);
}

void
_gtk_tree_view_column_unrealize_button (GtkTreeViewColumn *column)
{
  g_return_if_fail (column != NULL);
  g_return_if_fail (column->window != NULL);

  gdk_window_set_user_data (column->window, NULL);
  gdk_window_destroy (column->window);
  column->window = NULL;
}

void
_gtk_tree_view_column_set_tree_view (GtkTreeViewColumn *column,
				     GtkTreeView       *tree_view)
{
  column->tree_view = GTK_WIDGET (tree_view);
}

void
_gtk_tree_view_column_unset_tree_view (GtkTreeViewColumn *column)
{
  if (column->tree_view && column->button)
    {
      gtk_container_remove (GTK_CONTAINER (column->tree_view), column->button);
    }

  column->tree_view = NULL;
  column->button = NULL;
}



/* Public Functions */


597
598
599
600
601
602
603
/**
 * gtk_tree_view_column_new:
 * 
 * Creates a new #GtkTreeViewColumn.
 * 
 * Return value: A newly created #GtkTreeViewColumn.
 **/
604
GtkTreeViewColumn *
605
606
gtk_tree_view_column_new (void)
{
607
  GtkTreeViewColumn *retval;
608

609
  retval = GTK_TREE_VIEW_COLUMN (gtk_type_new (GTK_TYPE_TREE_COLUMN));
610
611
612
613

  return retval;
}

614
615
616
617
/**
 * gtk_tree_view_column_new_with_attributes:
 * @title: The title to set the header to.
 * @cell: The #GtkCellRenderer.
Jonathan Blandford's avatar
Jonathan Blandford committed
618
 * @Varargs: A NULL terminated list of attributes.
619
620
621
622
623
624
625
626
 * 
 * Creates a new #GtkTreeViewColumn with a number of default values.  This is
 * equivalent to calling @gtk_tree_view_column_set_title,
 * @gtk_tree_view_column_set_cell_renderer, and
 * @gtk_tree_view_column_set_attributes on the newly created #GtkTreeViewColumn.
 * 
 * Return value: A newly created #GtkTreeViewColumn.
 **/
627
GtkTreeViewColumn *
628
gtk_tree_view_column_new_with_attributes (const gchar     *title,
629
630
631
					  GtkCellRenderer *cell,
					  ...)
{
632
  GtkTreeViewColumn *retval;
633
634
635
636
  va_list args;

  retval = gtk_tree_view_column_new ();

637
638
  gtk_tree_view_column_set_title (retval, title);
  gtk_tree_view_column_set_cell_renderer (retval, cell);
639
640

  va_start (args, cell);
641
  gtk_tree_view_column_set_attributesv (retval, args);
642
643
644
645
646
  va_end (args);

  return retval;
}

647
648
649
650
651
/**
 * gtk_tree_view_column_set_cell_renderer:
 * @tree_column: A #GtkTreeViewColumn.
 * @cell: The #GtkCellRenderer, or NULL.
 * 
652
653
654
 * Sets the cell renderer of the @tree_column.  If there is a cell
 * renderer already set, then it is removed.  If @cell is NULL, then
 * the cell renderer is unset.
655
 **/
656
657
658
659
660
void
gtk_tree_view_column_set_cell_renderer (GtkTreeViewColumn *tree_column,
					GtkCellRenderer   *cell)
{
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
Havoc Pennington's avatar
Havoc Pennington committed
661

662
663
664
665
666
667
668
669
670
671
  if (cell)
    g_return_if_fail (GTK_IS_CELL_RENDERER (cell));

  if (cell)
    g_object_ref (G_OBJECT (cell));

  if (tree_column->cell)
    g_object_unref (G_OBJECT (tree_column->cell));

  tree_column->cell = cell;
672

673
  g_object_notify (G_OBJECT (tree_column), "cell_renderer");
674
675
}

Havoc Pennington's avatar
Havoc Pennington committed
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/**
 * gtk_tree_view_column_get_cell_renderer:
 * @tree_column: a #GtkTreeViewColumn
 * 
 * Gets the value set with gtk_tree_view_column_set_cell_renderer().
 * 
 * Return value: cell renderer for the column, or %NULL if unset
 **/
GtkCellRenderer*
gtk_tree_view_column_get_cell_renderer (GtkTreeViewColumn *tree_column)
{
  g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), NULL);
  
  return tree_column->cell;
}

692
693
694
695
696
697
698
699
700
701
/**
 * gtk_tree_view_column_add_attribute:
 * @tree_column: A #GtkTreeViewColumn.
 * @attribute: An attribute on the columns cell
 * @column: The column position on the model to get the attribute from.
 * 
 * Adds an attribute mapping to the list in @tree_column.  The @column is the
 * column position on the model to get a value from, and the @attribute is the
 * parameter on the @tree_column's cell renderer to set that value to.
 **/
702
703
void
gtk_tree_view_column_add_attribute (GtkTreeViewColumn *tree_column,
704
				    const gchar       *attribute,
705
706
707
708
709
				    gint               column)
{
  g_return_if_fail (tree_column != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));

710
711
712
713
  tree_column->attributes = g_slist_prepend (tree_column->attributes,
					     GINT_TO_POINTER (column));
  tree_column->attributes = g_slist_prepend (tree_column->attributes,
					     g_strdup (attribute));
714
715
716
717
718
719
720
721
722
723
724
725
726
727
}

static void
gtk_tree_view_column_set_attributesv (GtkTreeViewColumn *tree_column,
				      va_list            args)
{
  gchar *attribute;
  gint column;

  g_return_if_fail (tree_column != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));

  attribute = va_arg (args, gchar *);

728
729
  gtk_tree_view_column_clear_attributes (tree_column);
  
730
731
732
733
734
735
736
737
738
739
  while (attribute != NULL)
    {
      column = va_arg (args, gint);
      gtk_tree_view_column_add_attribute (tree_column,
					  attribute,
					  column);
      attribute = va_arg (args, gchar *);
    }
}

740
741
742
/**
 * gtk_tree_view_column_set_attributes:
 * @tree_column: A #GtkTreeViewColumn.
743
 * @Varargs: A NULL terminated listing of attributes.
744
 * 
745
746
747
748
 * Sets the attributes in the list as the attributes of @tree_column.
 * The attributes should be in attribute/column order, as in
 * @gtk_tree_view_column_add_attribute. All existing attributes
 * are removed, and replaced with the new attributes.
749
 **/
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
void
gtk_tree_view_column_set_attributes (GtkTreeViewColumn *tree_column,
				     ...)
{
  va_list args;

  g_return_if_fail (tree_column != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));

  va_start (args, tree_column);

  gtk_tree_view_column_set_attributesv (tree_column, args);

  va_end (args);
}

766
767

/**
768
 * gtk_tree_view_column_set_cell_data_func:
769
770
771
772
773
774
775
776
777
778
779
 * @tree_column: A #GtkTreeViewColumn
 * @func: The #GtkTreeViewColumnFunc to use. 
 * @func_data: The user data for @func.
 * @destroy: The destroy notification for @func_data
 * 
 * Sets the #GtkTreeViewColumnFunc to use for the column.  This function is used
 * instead of the standard attributes mapping for setting the column value, and
 * should set the value of @tree_column 's cell renderer as appropriate.  @func
 * may be NULL to remove an older one.
 **/
void
780
781
782
783
gtk_tree_view_column_set_cell_data_func (GtkTreeViewColumn *tree_column,
					 CellDataFunc       func,
					 gpointer           func_data,
					 GtkDestroyNotify   destroy)
784
785
786
787
788
789
790
791
792
793
794
795
796
{
  g_return_if_fail (tree_column != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));

  if (tree_column->func_data && tree_column->destroy)
    (tree_column->destroy) (tree_column->func_data);

  tree_column->func = func;
  tree_column->func_data = func_data;
  tree_column->destroy = destroy;
}


797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/**
 * gtk_tree_view_column_clear_attributes:
 * @tree_column: a #GtkTreeViewColumn
 * 
 * Clears all existing attributes previously set with
 * gtk_tree_view_column_set_attributes().
 **/
void
gtk_tree_view_column_clear_attributes (GtkTreeViewColumn *tree_column)
{
  GSList *list;
  
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));

  list = tree_column->attributes;

  while (list && list->next)
    {
      g_free (list->data);
      list = list->next->next;
    }
  g_slist_free (tree_column->attributes);
  tree_column->attributes = NULL;
}

822
823
824
825
/**
 * gtk_tree_view_column_set_cell_data:
 * @tree_column: A #GtkTreeViewColumn.
 * @tree_model: The #GtkTreeModel to to get the cell renderers attributes from.
Jonathan Blandford's avatar
Jonathan Blandford committed
826
 * @iter: The #GtkTreeIter to to get the cell renderer's attributes from.
827
828
829
830
831
832
 * 
 * Sets the cell renderer based on the @tree_model and @tree_node.  That is, for
 * every attribute mapping in @tree_column, it will get a value from the set
 * column on the @tree_node, and use that value to set the attribute on the cell
 * renderer.
 **/
833
834
835
void
gtk_tree_view_column_set_cell_data (GtkTreeViewColumn *tree_column,
				    GtkTreeModel      *tree_model,
836
				    GtkTreeIter       *iter)
837
838
839
840
841
842
843
844
845
{
  GSList *list;
  GValue value = { 0, };
  GObject *cell;

  g_return_if_fail (tree_column != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
  g_return_if_fail (tree_column->cell != NULL);

846
847
848
  if (tree_model == NULL)
    return;

849
850
851
  cell = (GObject *) tree_column->cell;
  list = tree_column->attributes;

852
853
  g_object_freeze_notify (cell);

854
855
  while (list && list->next)
    {
856
857
858
      gtk_tree_model_get_value (tree_model, iter,
				GPOINTER_TO_INT (list->next->data),
				&value);
859
      g_object_set_property (cell, (gchar *) list->data, &value);
860
861
      g_value_unset (&value);
      list = list->next->next;
862

863
    }
864
865
866
867

  if (tree_column->func)
    (* tree_column->func) (tree_column, tree_column->cell, tree_model, iter, tree_column->func_data);

868
  g_object_thaw_notify (cell);
869
870
871
872
}

/* Options for manipulating the columns */

873
874
875
876
877
878
879
/**
 * gtk_tree_view_column_set_visible:
 * @tree_column: A #GtkTreeViewColumn.
 * @visible: TRUE if the @tree_column is visible.
 * 
 * Sets the visibility of @tree_column.
 **/
880
881
882
883
884
885
886
void
gtk_tree_view_column_set_visible (GtkTreeViewColumn *tree_column,
				  gboolean           visible)
{
  g_return_if_fail (tree_column != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));

887
888
  visible = !! visible;
  
889
890
891
892
893
  if (tree_column->visible == visible)
    return;

  tree_column->visible = visible;

894
  if (tree_column->tree_view != NULL)
895
    {
896
897
898
      if (visible)
	{
	  gtk_widget_show (tree_column->button);
899
	  if (GTK_WIDGET_REALIZED (tree_column->tree_view) && tree_column->window)
900
901
902
903
904
	    gdk_window_show (tree_column->window);
	}
      else
	{
	  gtk_widget_hide (tree_column->button);
905
	  if (GTK_WIDGET_REALIZED (tree_column->tree_view) && tree_column->window)
906
907
908
	    gdk_window_hide (tree_column->window);
	}

909
      if (GTK_WIDGET_REALIZED (tree_column->tree_view))
910
	_gtk_tree_view_update_size (GTK_TREE_VIEW (tree_column->tree_view));
911
912
    }

913
  g_object_notify (G_OBJECT (tree_column), "visible");
914
915
}

916
917
918
919
920
921
922
923
924
/**
 * gtk_tree_view_column_get_visible:
 * @tree_column: A #GtkTreeViewColumn.
 * 
 * Returns TRUE if @tree_column is visible.
 * 
 * Return value: whether the column is visible or not.  If it is visible, then
 * the tree will show the column.
 **/
925
926
927
928
929
930
931
932
933
gboolean
gtk_tree_view_column_get_visible (GtkTreeViewColumn *tree_column)
{
  g_return_val_if_fail (tree_column != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), FALSE);

  return tree_column->visible;
}

934
/**
Havoc Pennington's avatar
Havoc Pennington committed
935
 * gtk_tree_view_column_set_sizing:
936
 * @tree_column: A #GtkTreeViewColumn.
Havoc Pennington's avatar
Havoc Pennington committed
937
 * @type: The #GtkTreeViewColumnSizing.
938
939
940
 * 
 * Sets the growth behavior of @tree_column to @type.
 **/
941
void
Havoc Pennington's avatar
Havoc Pennington committed
942
gtk_tree_view_column_set_sizing (GtkTreeViewColumn     *tree_column,
943
                                 GtkTreeViewColumnSizing  type)
944
945
946
947
948
949
950
951
{
  g_return_if_fail (tree_column != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));

  if (type == tree_column->column_type)
    return;

  tree_column->column_type = type;
952
953
954
955
956
957

  g_object_notify (G_OBJECT (tree_column), "sizing");

  if (tree_column->tree_view == NULL)
    return;

958
959
960
961
962
  switch (type)
    {
    case GTK_TREE_VIEW_COLUMN_AUTOSIZE:
      tree_column->dirty = TRUE;
    case GTK_TREE_VIEW_COLUMN_FIXED:
963
964
      if (GTK_WIDGET_REALIZED (tree_column->tree_view))
        gdk_window_hide (tree_column->window);
965
966
      break;
    default:
967
968
969
970
971
      if (GTK_WIDGET_REALIZED (tree_column->tree_view))
        {
          gdk_window_show (tree_column->window);
          gdk_window_raise (tree_column->window);
        }
972
973
974
975
976
977
      break;
    }

  gtk_widget_queue_resize (tree_column->tree_view);
}

978
/**
979
 * gtk_tree_view_column_get_sizing:
980
981
982
983
984
985
 * @tree_column: A #GtkTreeViewColumn.
 * 
 * Returns the current type of @tree_column.
 * 
 * Return value: The type of @tree_column.
 **/
986
gint
987
gtk_tree_view_column_get_sizing (GtkTreeViewColumn *tree_column)
988
989
990
991
992
993
994
{
  g_return_val_if_fail (tree_column != NULL, 0);
  g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), 0);

  return tree_column->column_type;
}

995
996
997
998
999
1000
1001
1002
/**
 * gtk_tree_view_column_get_size:
 * @tree_column: A #GtkTreeViewColumn.
 * 
 * Returns the current size of the @tree_column in pixels.
 * 
 * Return value: The current size of the @tree_column.
 **/
1003
1004
1005
1006
1007
1008
gint
gtk_tree_view_column_get_size (GtkTreeViewColumn *tree_column)
{
  g_return_val_if_fail (tree_column != NULL, 0);
  g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), 0);

Havoc Pennington's avatar
Havoc Pennington committed
1009
  return tree_column->width;
1010
1011
}

1012
/**
Havoc Pennington's avatar
Havoc Pennington committed
1013
 * gtk_tree_view_column_set_width:
1014
1015
1016
1017
1018
 * @tree_column: A #GtkTreeViewColumn.
 * @size: The size to set the @tree_column to.
 * 
 * Sets the size of the column in pixels, unless the the column type is
 * #GTK_TREE_VIEW_COLUMN_AUTOSIZE.  In this case, the value is discarded as the
1019
1020
 * size of the column is based on the calculated width of the column. The
 * width is clamped to the min/max width for the column.
1021
 **/
1022
void
Havoc Pennington's avatar
Havoc Pennington committed
1023
gtk_tree_view_column_set_width (GtkTreeViewColumn *tree_column,
1024
                                gint               size)
1025
1026
1027
1028
{
  g_return_if_fail (tree_column != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
  g_return_if_fail (size > 0);
1029
1030
  
  if (tree_column->min_width != -1 &&
1031
      size <= tree_column->min_width)
1032
1033
1034
1035
1036
1037
    size = tree_column->min_width;
  else if (tree_column->max_width != -1 &&
           size > tree_column->max_width)
    size = tree_column->max_width;
  
  if (tree_column->width == size)
1038
    return;
1039
  
Havoc Pennington's avatar
Havoc Pennington committed
1040
  tree_column->width = size;
1041

1042
  
1043
1044
1045
1046
1047
  g_object_notify (G_OBJECT (tree_column), "width");

  if (tree_column->tree_view == NULL)
    return;

1048
1049
1050
1051
  if (GTK_WIDGET_REALIZED (tree_column->tree_view))
    gtk_widget_queue_resize (tree_column->tree_view);
}

Havoc Pennington's avatar
Havoc Pennington committed
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
/**
 * gtk_tree_view_column_get_width:
 * @tree_column: a #GtkTreeViewColumn
 * 
 * Gets the value set by gtk_tree_view_column_set_width().
 * 
 * Return value: the width of the column
 **/
gint
gtk_tree_view_column_get_width (GtkTreeViewColumn *tree_column)
{
  g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), 0);

  return tree_column->width;
}

1068
1069
1070
1071
1072
1073
1074
1075
/**
 * gtk_tree_view_column_set_min_width:
 * @tree_column: A #GtkTreeViewColumn.
 * @min_width: The minimum width of the column in pixels, or -1.
 * 
 * Sets the minimum width of the @tree_column.  If @min_width is -1, then the
 * minimum width is unset.
 **/
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
void
gtk_tree_view_column_set_min_width (GtkTreeViewColumn *tree_column,
				    gint               min_width)
{
  gint real_min_width;

  g_return_if_fail (tree_column != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
  g_return_if_fail (min_width >= -1);

  if (min_width == tree_column->min_width)
    return;

  real_min_width = (tree_column->min_width == -1) ?
    tree_column->button->requisition.width : tree_column->min_width;

  /* We want to queue a resize if the either the old min_size or the
   * new min_size determined the size of the column */
  if (GTK_WIDGET_REALIZED (tree_column->tree_view) &&
Havoc Pennington's avatar
Havoc Pennington committed
1095
      ((tree_column->min_width > tree_column->width) ||
1096
       (tree_column->min_width == -1 &&
Havoc Pennington's avatar
Havoc Pennington committed
1097
1098
	tree_column->button->requisition.width > tree_column->width) ||
       (min_width > tree_column->width) ||
1099
       (min_width == -1 &&
Havoc Pennington's avatar
Havoc Pennington committed
1100
	tree_column->button->requisition.width > tree_column->width)))
1101
1102
1103
1104
1105
1106
1107
    gtk_widget_queue_resize (tree_column->tree_view);

  if (tree_column->max_width != -1 &&
      tree_column->max_width < real_min_width)
    tree_column->max_width = real_min_width;

  tree_column->min_width = min_width;
1108
1109

  g_object_notify (G_OBJECT (tree_column), "min_width");
1110
1111
}

1112
1113
1114
1115
1116
1117
1118
1119
1120
/**
 * gtk_tree_view_column_get_min_width:
 * @tree_column: A #GtkTreeViewColumn.
 * 
 * Returns the minimum width in pixels of the @tree_column, or -1 if no minimum
 * width is set.
 * 
 * Return value: The minimum width of the @tree_column.
 **/
1121
1122
1123
1124
1125
1126
1127
1128
1129
gint
gtk_tree_view_column_get_min_width (GtkTreeViewColumn *tree_column)
{
  g_return_val_if_fail (tree_column != NULL, -1);
  g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), -1);

  return tree_column->min_width;
}

1130
1131
1132
1133
1134
1135
1136
1137
/**
 * gtk_tree_view_column_set_max_width:
 * @tree_column: A #GtkTreeViewColumn.
 * @max_width: The maximum width of the column in pixels, or -1.
 * 
 * Sets the maximum width of the @tree_column.  If @max_width is -1, then the
 * maximum width is unset.
 **/
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
void
gtk_tree_view_column_set_max_width (GtkTreeViewColumn *tree_column,
				    gint               max_width)
{
  gint real_min_width;

  g_return_if_fail (tree_column != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
  g_return_if_fail (max_width >= -1);

  if (max_width == tree_column->max_width)
    return;

  real_min_width = tree_column->min_width == -1 ?
    tree_column->button->requisition.width : tree_column->min_width;

  if (GTK_WIDGET_REALIZED (tree_column->tree_view) &&
Havoc Pennington's avatar
Havoc Pennington committed
1155
1156
      ((tree_column->max_width < tree_column->width) ||
       (max_width != -1 && max_width < tree_column->width)))
1157
1158
1159
1160
1161
1162
    gtk_widget_queue_resize (tree_column->tree_view);

  tree_column->max_width = max_width;

  if (real_min_width > max_width)
    tree_column->min_width = max_width;
1163
1164

  g_object_notify (G_OBJECT (tree_column), "max_width");
1165
1166
}

1167
1168
1169
1170
1171
1172
1173
1174
1175
/**
 * gtk_tree_view_column_get_max_width:
 * @tree_column: A #GtkTreeViewColumn.
 * 
 * Returns the maximum width in pixels of the @tree_column, or -1 if no maximum
 * width is set.
 * 
 * Return value: The maximum width of the @tree_column.
 **/
1176
1177
1178
1179
1180
1181
1182
1183
1184
gint
gtk_tree_view_column_get_max_width (GtkTreeViewColumn *tree_column)
{
  g_return_val_if_fail (tree_column != NULL, -1);
  g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), -1);

  return tree_column->max_width;
}

1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
/**
 * gtk_tree_view_column_clicked:
 * @tree_column: a #GtkTreeViewColumn
 * 
 * Emits the "clicked" signal on the column.
 **/
void
gtk_tree_view_column_clicked (GtkTreeViewColumn *tree_column)
{
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));

  gtk_signal_emit (GTK_OBJECT (tree_column), tree_column_signals[CLICKED]);
}

Havoc Pennington's avatar
Havoc Pennington committed
1199
1200
1201
1202
1203
static void 
update_button_contents (GtkTreeViewColumn *tree_column)
{
  if (tree_column->button)
    {
Havoc Pennington's avatar
Havoc Pennington committed
1204
1205
1206
      GtkWidget *hbox = GTK_BIN (tree_column->button)->child;
      GtkWidget *alignment = tree_column->alignment;
      GtkWidget *arrow = tree_column->arrow;
Havoc Pennington's avatar
Havoc Pennington committed
1207
      GtkWidget *current_child = GTK_BIN (alignment)->child;
Havoc Pennington's avatar
Havoc Pennington committed
1208
1209
1210

      gtk_alignment_set (GTK_ALIGNMENT (alignment), tree_column->xalign,
                         0.5, 0.0, 0.0);
Havoc Pennington's avatar
Havoc Pennington committed
1211
1212
1213
1214
1215
      
      if (tree_column->child)
        {
          if (current_child != tree_column->child)
            {
Havoc Pennington's avatar
Havoc Pennington committed
1216
              gtk_container_remove (GTK_CONTAINER (alignment),
Havoc Pennington's avatar
Havoc Pennington committed
1217
1218
                                    current_child);

Havoc Pennington's avatar
Havoc Pennington committed
1219
1220
              gtk_container_add (GTK_CONTAINER (alignment),
                                 tree_column->child);
Havoc Pennington's avatar
Havoc Pennington committed
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
            }
        }
      else 
        {
          if (current_child == NULL)
            {
              current_child = gtk_label_new (NULL);

              gtk_widget_show (current_child);
              
Havoc Pennington's avatar
Havoc Pennington committed
1231
              gtk_container_add (GTK_CONTAINER (alignment),
Havoc Pennington's avatar
Havoc Pennington committed
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
                                 current_child);
            }

          g_return_if_fail (GTK_IS_LABEL (current_child));

          if (tree_column->title)
            gtk_label_set_text (GTK_LABEL (current_child),
                                tree_column->title);
          else
            gtk_label_set_text (GTK_LABEL (current_child),
                                "");
        }
Havoc Pennington's avatar
Havoc Pennington committed
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287

      switch (tree_column->sort_order)
        {
        case GTK_TREE_SORT_ASCENDING:
          gtk_arrow_set (GTK_ARROW (arrow),
                         GTK_ARROW_DOWN,
                         GTK_SHADOW_IN);
          break;

        case GTK_TREE_SORT_DESCENDING:
          gtk_arrow_set (GTK_ARROW (arrow),
                         GTK_ARROW_UP,
                         GTK_SHADOW_IN);
          break;
          
        default:
          g_warning (G_STRLOC": bad sort order");
          break;
        }

      /* Put arrow on the right if the text is left-or-center justified,
       * and on the left otherwise; do this by packing boxes, so flipping
       * text direction will reverse things
       */
      gtk_widget_ref (arrow);
      gtk_container_remove (GTK_CONTAINER (hbox), arrow);

      if (tree_column->xalign <= 0.5)
        {
          gtk_box_pack_end (GTK_BOX (hbox), arrow, FALSE, FALSE, 0);
        }
      else
        {
          gtk_box_pack_start (GTK_BOX (hbox), arrow, FALSE, FALSE, 0);
          /* move it to the front */
          gtk_box_reorder_child (GTK_BOX (hbox), arrow, 0);
        }

      gtk_widget_unref (arrow);

      if (tree_column->show_sort_indicator)
        gtk_widget_show (arrow);
      else
        gtk_widget_hide (arrow);
Havoc Pennington's avatar
Havoc Pennington committed
1288
1289
1290
    }
}

1291
1292
1293
1294
1295
1296
1297
1298
/**
 * gtk_tree_view_column_set_title:
 * @tree_column: A #GtkTreeViewColumn.
 * @title: The title of the @tree_column.
 * 
 * Sets the title of the @tree_column.  If a custom widget has been set, then
 * this value is ignored.
 **/
1299
1300
void
gtk_tree_view_column_set_title (GtkTreeViewColumn *tree_column,
1301
				const gchar       *title)
1302
1303
1304
1305
1306
1307
1308
1309
1310
{
  g_return_if_fail (tree_column != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));

  g_free (tree_column->title);
  if (title)
    tree_column->title = g_strdup (title);
  else
    tree_column->title = NULL;
1311

Havoc Pennington's avatar
Havoc Pennington committed
1312
  update_button_contents (tree_column);
1313
1314

  g_object_notify (G_OBJECT (tree_column), "title");
1315
1316
}

1317
1318
1319
1320
1321
1322
1323
1324
/**
 * gtk_tree_view_column_get_title:
 * @tree_column: A #GtkTreeViewColumn.
 * 
 * Returns the title of the widget.  This value should not be modified.
 * 
 * Return value: the title of the column.
 **/
Havoc Pennington's avatar
Havoc Pennington committed
1325
G_CONST_RETURN gchar *
1326
1327
1328
1329
1330
1331
1332
1333
gtk_tree_view_column_get_title (GtkTreeViewColumn *tree_column)
{
  g_return_val_if_fail (tree_column != NULL, NULL);
  g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), NULL);

  return tree_column->title;
}

1334
/**
1335
 * gtk_tree_view_column_set_clickable:
1336
1337
1338
1339
1340
1341
 * @tree_column: A #GtkTreeViewColumn.
 * @active: TRUE if the header is active.
 * 
 * Sets the header to be active if @active is TRUE.  When the header is active,
 * then it can take keyboard focus, and can be clicked.
 **/
1342
void
1343
1344
gtk_tree_view_column_set_clickable (GtkTreeViewColumn *tree_column,
                                    gboolean           active)
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
{
  g_return_if_fail (tree_column != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));

  if (!tree_column->button)
    return;

  if (tree_column->button_active == active)
    return;

  tree_column->button_active = active;
  if (active)
    {
      gtk_signal_disconnect_by_func (GTK_OBJECT (tree_column->button),
				     (GtkSignalFunc) gtk_tree_view_passive_func,
				     NULL);

      GTK_WIDGET_SET_FLAGS (tree_column->button, GTK_CAN_FOCUS);

      if (GTK_WIDGET_VISIBLE (tree_column->tree_view))
	gtk_widget_queue_draw (tree_column->button);
    }
  else
    {
      gtk_signal_connect (GTK_OBJECT (tree_column->button),
			  "event",
			  (GtkSignalFunc) gtk_tree_view_passive_func,
			  NULL);

      GTK_WIDGET_UNSET_FLAGS (tree_column->button, GTK_CAN_FOCUS);

      if (GTK_WIDGET_VISIBLE (tree_column->tree_view))
	gtk_widget_queue_draw (tree_column->button);
    }
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396

  g_object_notify (G_OBJECT (tree_column), "clickable");
}

/**
 * gtk_tree_view_column_get_clickable:
 * @tree_column: a #GtkTreeViewColumn
 * 
 * Returns %TRUE if the user can click on the header for the column.
 * 
 * Return value: whether the user can click the column header
 **/
gboolean
gtk_tree_view_column_get_clickable (GtkTreeViewColumn *tree_column)
{
  g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), FALSE);

  return tree_column->button_active;
1397
1398
}

1399
1400
1401
1402
1403
1404
1405
1406
/**
 * gtk_tree_view_column_set_widget:
 * @tree_column: A #GtkTreeViewColumn.
 * @widget: A child #GtkWidget, or NULL.
 * 
 * Sets the widget in the header to be @widget.  If widget is NULL, then the
 * header button is set with a #GtkLabel set to the title of @tree_column.
 **/
1407
1408
1409
1410
void
gtk_tree_view_column_set_widget (GtkTreeViewColumn *tree_column,
				 GtkWidget         *widget)
{
Havoc Pennington's avatar
Havoc Pennington committed
1411
1412
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
  g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
1413
1414
1415

  if (widget)
    {
Havoc Pennington's avatar
Havoc Pennington committed
1416
1417
      gtk_object_ref (GTK_OBJECT (widget));
      gtk_object_sink (GTK_OBJECT (widget));
1418
1419
    }

Havoc Pennington's avatar
Havoc Pennington committed
1420
1421
1422
1423
1424
1425
  if (tree_column->child)      
    gtk_object_unref (GTK_OBJECT (tree_column->child));

  tree_column->child = widget;
  
  update_button_contents (tree_column);
1426
1427

  g_object_notify (G_OBJECT (tree_column), "widget");
1428
1429
}

1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
/**
 * gtk_tree_view_column_get_widget:
 * @tree_column: A #GtkTreeViewColumn.
 * 
 * Returns the #GtkWidget in the button in the column header.  If a custom
 * widget has not been set, then this will be a #GtkAlignment with a #GtkLabel
 * in it.
 * 
 * Return value: The #GtkWidget in the column header.
 **/
1440
1441
1442
1443
1444
1445
GtkWidget *
gtk_tree_view_column_get_widget (GtkTreeViewColumn *tree_column)
{
  g_return_val_if_fail (tree_column != NULL, NULL);
  g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), NULL);

Havoc Pennington's avatar
Havoc Pennington committed
1446
  return tree_column->child;
1447
1448
}

1449
/**
Havoc Pennington's avatar
Havoc Pennington committed
1450
 * gtk_tree_view_column_set_alignment:
1451
 * @tree_column: A #GtkTreeViewColumn.
Havoc Pennington's avatar
Havoc Pennington committed
1452
 * @xalign: alignment (0.0 for left, 0.5 for center, 1.0 for right)
1453
 * 
Havoc Pennington's avatar
Havoc Pennington committed
1454
 * Sets the alignment of the title or custom widget inside the column header.
1455
 **/
1456
void
Havoc Pennington's avatar
Havoc Pennington committed
1457
1458
gtk_tree_view_column_set_alignment (GtkTreeViewColumn *tree_column,
                                    gfloat             xalign)
1459
1460
1461
1462
{
  g_return_if_fail (tree_column != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));

Havoc Pennington's avatar
Havoc Pennington committed
1463
  if (tree_column->xalign == xalign)
1464
1465
    return;

Havoc Pennington's avatar
Havoc Pennington committed
1466
1467
1468
1469
1470
1471
  tree_column->xalign = xalign;

  update_button_contents (tree_column);

  g_object_notify (G_OBJECT (tree_column), "alignment");
}
1472

Havoc Pennington's avatar
Havoc Pennington committed
1473
1474
1475
1476
gfloat
gtk_tree_view_column_get_alignment (GtkTreeViewColumn *tree_column)
{
  g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), 0.5);
1477

Havoc Pennington's avatar
Havoc Pennington committed
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
  return tree_column->xalign;
}

/**
 * gtk_tree_view_column_set_sort_indicator:
 * @tree_column: a #GtkTreeViewColumn
 * @setting: %TRUE to display an indicator that the column is sorted
 *
 * Call this function with a @setting of %TRUE to display an arrow in
 * the header button indicating the column is sorted. Call
 * gtk_tree_view_column_set_sort_order() to change the direction of
 * the arrow.
 * 
 **/
void
gtk_tree_view_column_set_sort_indicator (GtkTreeViewColumn     *tree_column,
                                         gboolean               setting)
{
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));

  setting = setting != FALSE;

  if (setting != tree_column->show_sort_indicator)
1501
    {
Havoc Pennington's avatar
Havoc Pennington committed
1502
1503
1504
1505
1506
      tree_column->show_sort_indicator = setting;

      update_button_contents (tree_column);
      
      g_object_notify (G_OBJECT (tree_column), "sort_indicator");
1507
    }
Havoc Pennington's avatar
Havoc Pennington committed
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
}

/**
 * gtk_tree_view_column_get_sort_indicator:
 * @tree_column: a #GtkTreeViewColumn
 * 
 * Gets the value set by gtk_tree_view_column_set_sort_indicator().
 * 
 * Return value: whether the sort indicator arrow is displayed
 **/
gboolean
gtk_tree_view_column_get_sort_indicator  (GtkTreeViewColumn     *tree_column)
{
  g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), FALSE);

  return tree_column->show_sort_indicator;
}

/**
 * gtk_tree_view_column_set_sort_order:
 * @tree_column: a #GtkTreeViewColumn
 * @order: sort order that the sort indicator should indicate
 *
 * Changes the appearance of the sort indicator. (This <emphasis>does
 * not</emphasis> actually sort the model - for the models shipped
 * with GTK+, use at gtk_tree_sortable_set_sort_column() to do
 * that. For custom models, the mechanism will vary.) The sort
 * indicator changes direction to indicate normal sort or reverse
 * sort. Note that you must have the sort indicator enabled to see
 * anything when calling this function; see
 * gtk_tree_view_column_set_sort_indicator().
 * 
 **/
void
gtk_tree_view_column_set_sort_order      (GtkTreeViewColumn     *tree_column,
                                          GtkTreeSortOrder       order)
{
  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));

  if (order != tree_column->sort_order)
    {
      tree_column->sort_order = order;

      update_button_contents (tree_column);
      
      g_object_notify (G_OBJECT (tree_column), "sort_order");
    }
}

/**
 * gtk_tree_view_column_get_sort_order:
 * @tree_column: a #GtkTreeViewColumn
 * 
 * Gets the value set by gtk_tree_view_column_set_sort_order().
 * 
 * Return value: the sort order the sort indicator is indicating
 **/
GtkTreeSortOrder
gtk_tree_view_column_get_sort_order      (GtkTreeViewColumn     *tree_column)
{
  g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), 0);
1569

Havoc Pennington's avatar
Havoc Pennington committed
1570
  return tree_column->sort_order;
1571
}
Havoc Pennington's avatar
Havoc Pennington committed
1572

1573