gtktreeviewcolumn.c 106 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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.
 */

Manish Singh's avatar
Manish Singh committed
20
#include <string.h>
21
#include "gtktreeviewcolumn.h"
22
#include "gtktreeview.h"
23
#include "gtktreeprivate.h"
24
#include "gtkcelllayout.h"
25 26
#include "gtkbutton.h"
#include "gtkalignment.h"
27
#include "gtklabel.h"
Havoc Pennington's avatar
Havoc Pennington committed
28
#include "gtkhbox.h"
29
#include "gtkmarshalers.h"
Havoc Pennington's avatar
Havoc Pennington committed
30
#include "gtkarrow.h"
31
#include "gtkintl.h"
Jonathan Blandford's avatar
Jonathan Blandford committed
32

33 34 35 36
enum
{
  PROP_0,
  PROP_VISIBLE,
37
  PROP_RESIZABLE,
38
  PROP_WIDTH,
39 40
  PROP_SIZING,
  PROP_FIXED_WIDTH,
41 42 43
  PROP_MIN_WIDTH,
  PROP_MAX_WIDTH,
  PROP_TITLE,
Jonathan Blandford's avatar
Jonathan Blandford committed
44
  PROP_EXPAND,
45 46
  PROP_CLICKABLE,
  PROP_WIDGET,
Havoc Pennington's avatar
Havoc Pennington committed
47
  PROP_ALIGNMENT,
48
  PROP_REORDERABLE,
Havoc Pennington's avatar
Havoc Pennington committed
49 50
  PROP_SORT_INDICATOR,
  PROP_SORT_ORDER
51 52 53 54
};

enum
{
55 56 57 58
  CLICKED,
  LAST_SIGNAL
};

59 60 61 62 63 64 65 66
typedef struct _GtkTreeViewColumnCellInfo GtkTreeViewColumnCellInfo;
struct _GtkTreeViewColumnCellInfo
{
  GtkCellRenderer *cell;
  GSList *attributes;
  GtkTreeCellDataFunc func;
  gpointer func_data;
  GtkDestroyNotify destroy;
67
  gint requested_width;
68
  gint real_width;
69 70
  guint expand : 1;
  guint pack : 1;
71
  guint has_focus : 1;
72
  guint in_editing_mode : 1;
73
};
74

75 76 77
/* Type methods */
static void gtk_tree_view_column_init                          (GtkTreeViewColumn       *tree_column);
static void gtk_tree_view_column_class_init                    (GtkTreeViewColumnClass  *klass);
78
static void gtk_tree_view_column_cell_layout_init              (GtkCellLayoutIface      *iface);
79 80 81 82 83 84 85 86 87 88 89 90

/* GObject methods */
static void gtk_tree_view_column_set_property                  (GObject                 *object,
								guint                    prop_id,
								const GValue            *value,
								GParamSpec              *pspec);
static void gtk_tree_view_column_get_property                  (GObject                 *object,
								guint                    prop_id,
								GValue                  *value,
								GParamSpec              *pspec);
static void gtk_tree_view_column_finalize                      (GObject                 *object);

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/* GtkCellLayout implementation */
static void gtk_tree_view_column_cell_layout_pack_start         (GtkCellLayout         *cell_layout,
                                                                 GtkCellRenderer       *cell,
                                                                 gboolean               expand);
static void gtk_tree_view_column_cell_layout_pack_end           (GtkCellLayout         *cell_layout,
                                                                 GtkCellRenderer       *cell,
                                                                 gboolean               expand);
static void gtk_tree_view_column_cell_layout_clear              (GtkCellLayout         *cell_layout);
static void gtk_tree_view_column_cell_layout_add_attribute      (GtkCellLayout         *cell_layout,
                                                                 GtkCellRenderer       *cell,
                                                                 const gchar           *attribute,
                                                                 gint                   column);
static void gtk_tree_view_column_cell_layout_set_cell_data_func (GtkCellLayout         *cell_layout,
                                                                 GtkCellRenderer       *cell,
                                                                 GtkCellLayoutDataFunc  func,
                                                                 gpointer               func_data,
                                                                 GDestroyNotify         destroy);
static void gtk_tree_view_column_cell_layout_clear_attributes   (GtkCellLayout         *cell_layout,
                                                                 GtkCellRenderer       *cell);

111
/* Button handling code */
112 113 114 115
static void gtk_tree_view_column_create_button                 (GtkTreeViewColumn       *tree_column);
static void gtk_tree_view_column_update_button                 (GtkTreeViewColumn       *tree_column);

/* Button signal handlers */
116
static gint gtk_tree_view_column_button_event                  (GtkWidget               *widget,
117 118 119 120
								GdkEvent                *event,
								gpointer                 data);
static void gtk_tree_view_column_button_clicked                (GtkWidget               *widget,
								gpointer                 data);
121 122 123
static gboolean gtk_tree_view_column_mnemonic_activate         (GtkWidget *widget,
					                        gboolean   group_cycling,
								gpointer   data);
124 125 126 127 128 129 130 131 132 133

/* Property handlers */
static void gtk_tree_view_model_sort_column_changed            (GtkTreeSortable         *sortable,
								GtkTreeViewColumn       *tree_column);

/* Internal functions */
static void gtk_tree_view_column_sort                          (GtkTreeViewColumn       *tree_column,
								gpointer                 data);
static void gtk_tree_view_column_setup_sort_column_id_callback (GtkTreeViewColumn       *tree_column);
static void gtk_tree_view_column_set_attributesv               (GtkTreeViewColumn       *tree_column,
134
								GtkCellRenderer         *cell_renderer,
135
								va_list                  args);
136 137
static GtkTreeViewColumnCellInfo *gtk_tree_view_column_get_cell_info (GtkTreeViewColumn *tree_column,
								      GtkCellRenderer   *cell_renderer);
138

139 140 141 142 143 144 145
/* cell list manipulation */
static GList *gtk_tree_view_column_cell_first                  (GtkTreeViewColumn      *tree_column);
static GList *gtk_tree_view_column_cell_last                   (GtkTreeViewColumn      *tree_column);
static GList *gtk_tree_view_column_cell_next                   (GtkTreeViewColumn      *tree_column,
								GList                  *current);
static GList *gtk_tree_view_column_cell_prev                   (GtkTreeViewColumn      *tree_column,
								GList                  *current);
146 147
static void gtk_tree_view_column_clear_attributes_by_info      (GtkTreeViewColumn      *tree_column,
					                        GtkTreeViewColumnCellInfo *info);
148 149 150 151 152

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


Manish Singh's avatar
Manish Singh committed
153
GType
154 155
gtk_tree_view_column_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
156
  static GType tree_column_type = 0;
157 158 159 160 161 162 163 164 165 166 167 168 169

  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,
170 171 172 173 174 175 176 177
	(GInstanceInitFunc) gtk_tree_view_column_init
      };

      static const GInterfaceInfo cell_layout_info =
      {
        (GInterfaceInitFunc) gtk_tree_view_column_cell_layout_init,
        NULL,
        NULL
178 179
      };

Manish Singh's avatar
Manish Singh committed
180 181 182
      tree_column_type =
	g_type_register_static (GTK_TYPE_OBJECT, "GtkTreeViewColumn",
				&tree_column_info, 0);
183 184 185 186

      g_type_add_interface_static (tree_column_type,
                                   GTK_TYPE_CELL_LAYOUT,
                                   &cell_layout_info);
187 188 189 190 191 192 193 194
    }

  return tree_column_type;
}

static void
gtk_tree_view_column_class_init (GtkTreeViewColumnClass *class)
{
195
  GObjectClass *object_class;
196

197
  object_class = (GObjectClass*) class;
198 199 200

  parent_class = g_type_class_peek_parent (class);

201
  class->clicked = NULL;
202

203
  object_class->finalize = gtk_tree_view_column_finalize;
204 205 206
  object_class->set_property = gtk_tree_view_column_set_property;
  object_class->get_property = gtk_tree_view_column_get_property;
  
207
  tree_column_signals[CLICKED] =
208
    g_signal_new ("clicked",
Manish Singh's avatar
Manish Singh committed
209
                  G_OBJECT_CLASS_TYPE (object_class),
210 211 212
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTreeViewColumnClass, clicked),
                  NULL, NULL,
213
                  _gtk_marshal_VOID__VOID,
Manish Singh's avatar
Manish Singh committed
214
                  G_TYPE_NONE, 0);
215 216 217 218 219

  g_object_class_install_property (object_class,
                                   PROP_VISIBLE,
                                   g_param_spec_boolean ("visible",
                                                        _("Visible"),
Kjartan Maraas's avatar
Kjartan Maraas committed
220
                                                        _("Whether to display the column"),
221 222 223
                                                         TRUE,
                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
  
224 225 226 227 228 229 230 231
  g_object_class_install_property (object_class,
                                   PROP_RESIZABLE,
                                   g_param_spec_boolean ("resizable",
							 _("Resizable"),
							 _("Column is user-resizable"),
                                                         FALSE,
                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
  
232 233 234 235 236 237 238 239 240
  g_object_class_install_property (object_class,
                                   PROP_WIDTH,
                                   g_param_spec_int ("width",
						     _("Width"),
						     _("Current width of the column"),
						     0,
						     G_MAXINT,
						     0,
						     G_PARAM_READABLE));
241 242 243 244 245 246 247 248 249 250
  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,
251 252 253 254
                                   PROP_FIXED_WIDTH,
                                   g_param_spec_int ("fixed_width",
                                                     _("Fixed Width"),
                                                     _("Current fixed width of the column"),
255 256 257 258 259 260 261 262 263 264 265 266
                                                     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,
267
                                                     -1,
268 269 270 271 272 273 274 275 276
                                                     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,
277
                                                     -1,
278 279 280 281 282 283 284 285 286 287
                                                     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));
  
Jonathan Blandford's avatar
Jonathan Blandford committed
288 289 290 291 292 293 294 295
  g_object_class_install_property (object_class,
                                   PROP_EXPAND,
                                   g_param_spec_boolean ("expand",
							 _("Expand"),
							 _("Column gets share of extra width allocated to the widget"),
							 FALSE,
							 G_PARAM_READABLE | G_PARAM_WRITABLE));
  
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
  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
314 315 316
                                   PROP_ALIGNMENT,
                                   g_param_spec_float ("alignment",
                                                       _("Alignment"),
317
                                                       _("X Alignment of the column header text or widget"),
Havoc Pennington's avatar
Havoc Pennington committed
318 319 320 321 322
                                                       0.0,
                                                       1.0,
                                                       0.5,
                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));

323 324 325 326
  g_object_class_install_property (object_class,
                                   PROP_REORDERABLE,
                                   g_param_spec_boolean ("reorderable",
							 _("Reorderable"),
327
							 _("Whether the column can be reordered around the headers"),
328 329 330
							 FALSE,
							 G_PARAM_READABLE | G_PARAM_WRITABLE));

Havoc Pennington's avatar
Havoc Pennington committed
331 332 333 334 335 336 337 338 339 340 341 342 343
  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"),
344 345
                                                      GTK_TYPE_SORT_TYPE,
                                                      GTK_SORT_ASCENDING,
346
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
Havoc Pennington's avatar
Havoc Pennington committed
347
  
348 349
}

350 351 352 353 354 355 356 357 358 359 360
static void
gtk_tree_view_column_cell_layout_init (GtkCellLayoutIface *iface)
{
  iface->pack_start = gtk_tree_view_column_cell_layout_pack_start;
  iface->pack_end = gtk_tree_view_column_cell_layout_pack_end;
  iface->clear = gtk_tree_view_column_cell_layout_clear;
  iface->add_attribute = gtk_tree_view_column_cell_layout_add_attribute;
  iface->set_cell_data_func = gtk_tree_view_column_cell_layout_set_cell_data_func;
  iface->clear_attributes = gtk_tree_view_column_cell_layout_clear_attributes;
}

361 362 363 364
static void
gtk_tree_view_column_init (GtkTreeViewColumn *tree_column)
{
  tree_column->button = NULL;
Havoc Pennington's avatar
Havoc Pennington committed
365
  tree_column->xalign = 0.0;
366
  tree_column->width = 0;
367
  tree_column->requested_width = -1;
368 369
  tree_column->min_width = -1;
  tree_column->max_width = -1;
Jonathan Blandford's avatar
Jonathan Blandford committed
370
  tree_column->resized_width = 0;
371
  tree_column->column_type = GTK_TREE_VIEW_COLUMN_GROW_ONLY;
372
  tree_column->visible = TRUE;
373
  tree_column->resizable = FALSE;
374
  tree_column->clickable = FALSE;
375
  tree_column->dirty = TRUE;
376
  tree_column->sort_order = GTK_SORT_ASCENDING;
Havoc Pennington's avatar
Havoc Pennington committed
377
  tree_column->show_sort_indicator = FALSE;
378
  tree_column->property_changed_signal = 0;
379 380
  tree_column->sort_clicked_signal = 0;
  tree_column->sort_column_changed_signal = 0;
381
  tree_column->sort_column_id = -1;
382 383
  tree_column->reorderable = FALSE;
  tree_column->maybe_reordered = FALSE;
Jonathan Blandford's avatar
Jonathan Blandford committed
384
  tree_column->use_resized_width = FALSE;
385 386 387 388 389 390
}

static void
gtk_tree_view_column_finalize (GObject *object)
{
  GtkTreeViewColumn *tree_column = (GtkTreeViewColumn *) object;
391 392 393 394 395
  GList *list;

  for (list = tree_column->cell_list; list; list = list->next)
    {
      GtkTreeViewColumnCellInfo *info = (GtkTreeViewColumnCellInfo *) list->data;
396 397 398 399 400 401 402 403

      if (info->destroy)
	{
	  GtkDestroyNotify d = info->destroy;

	  info->destroy = NULL;
	  d (info->func_data);
	}
404
      gtk_tree_view_column_clear_attributes_by_info (tree_column, info);
Manish Singh's avatar
Manish Singh committed
405
      g_object_unref (info->cell);
406 407
      g_free (info);
    }
408 409

  g_free (tree_column->title);
410 411 412
  g_list_free (tree_column->cell_list);

  if (tree_column->child)
413
    g_object_unref (tree_column->child);
414 415

  G_OBJECT_CLASS (parent_class)->finalize (object);
416 417
}

418 419 420 421
static void
gtk_tree_view_column_set_property (GObject         *object,
                                   guint            prop_id,
                                   const GValue    *value,
Tim Janik's avatar
Tim Janik committed
422
                                   GParamSpec      *pspec)
423 424 425 426 427 428 429 430 431 432 433 434
{
  GtkTreeViewColumn *tree_column;

  tree_column = GTK_TREE_VIEW_COLUMN (object);

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

Jonathan Blandford's avatar
Jonathan Blandford committed
435 436 437 438 439
    case PROP_RESIZABLE:
      gtk_tree_view_column_set_resizable (tree_column,
					  g_value_get_boolean (value));
      break;

440 441 442 443 444
    case PROP_SIZING:
      gtk_tree_view_column_set_sizing (tree_column,
                                       g_value_get_enum (value));
      break;

445 446 447
    case PROP_FIXED_WIDTH:
      gtk_tree_view_column_set_fixed_width (tree_column,
					    g_value_get_int (value));
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
      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;

Jonathan Blandford's avatar
Jonathan Blandford committed
465 466 467 468 469
    case PROP_EXPAND:
      gtk_tree_view_column_set_expand (tree_column,
				       g_value_get_boolean (value));
      break;

470 471 472 473 474 475 476 477 478 479
    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
480 481 482 483 484
    case PROP_ALIGNMENT:
      gtk_tree_view_column_set_alignment (tree_column,
                                          g_value_get_float (value));
      break;

485 486 487 488 489
    case PROP_REORDERABLE:
      gtk_tree_view_column_set_reorderable (tree_column,
					    g_value_get_boolean (value));
      break;

Havoc Pennington's avatar
Havoc Pennington committed
490 491 492
    case PROP_SORT_INDICATOR:
      gtk_tree_view_column_set_sort_indicator (tree_column,
                                               g_value_get_boolean (value));
493 494
      break;

Havoc Pennington's avatar
Havoc Pennington committed
495 496 497 498 499
    case PROP_SORT_ORDER:
      gtk_tree_view_column_set_sort_order (tree_column,
                                           g_value_get_enum (value));
      break;
      
500 501 502 503 504 505 506 507 508 509
    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,
Tim Janik's avatar
Tim Janik committed
510
                                   GParamSpec      *pspec)
511 512 513 514 515 516 517 518
{
  GtkTreeViewColumn *tree_column;

  tree_column = GTK_TREE_VIEW_COLUMN (object);

  switch (prop_id)
    {
    case PROP_VISIBLE:
Havoc Pennington's avatar
Havoc Pennington committed
519 520
      g_value_set_boolean (value,
                           gtk_tree_view_column_get_visible (tree_column));
521 522
      break;

Jonathan Blandford's avatar
Jonathan Blandford committed
523 524 525 526 527
    case PROP_RESIZABLE:
      g_value_set_boolean (value,
                           gtk_tree_view_column_get_resizable (tree_column));
      break;

528 529 530 531 532
    case PROP_WIDTH:
      g_value_set_int (value,
                       gtk_tree_view_column_get_width (tree_column));
      break;

533
    case PROP_SIZING:
Havoc Pennington's avatar
Havoc Pennington committed
534 535
      g_value_set_enum (value,
                        gtk_tree_view_column_get_sizing (tree_column));
536 537
      break;

538
    case PROP_FIXED_WIDTH:
Havoc Pennington's avatar
Havoc Pennington committed
539
      g_value_set_int (value,
540
                       gtk_tree_view_column_get_fixed_width (tree_column));
541 542 543
      break;

    case PROP_MIN_WIDTH:
Havoc Pennington's avatar
Havoc Pennington committed
544 545
      g_value_set_int (value,
                       gtk_tree_view_column_get_min_width (tree_column));
546 547 548
      break;

    case PROP_MAX_WIDTH:
Havoc Pennington's avatar
Havoc Pennington committed
549 550
      g_value_set_int (value,
                       gtk_tree_view_column_get_max_width (tree_column));
551 552 553
      break;

    case PROP_TITLE:
Havoc Pennington's avatar
Havoc Pennington committed
554 555
      g_value_set_string (value,
                          gtk_tree_view_column_get_title (tree_column));
556 557
      break;

Jonathan Blandford's avatar
Jonathan Blandford committed
558 559 560 561 562
    case PROP_EXPAND:
      g_value_set_boolean (value,
                          gtk_tree_view_column_get_expand (tree_column));
      break;

563
    case PROP_CLICKABLE:
Havoc Pennington's avatar
Havoc Pennington committed
564 565
      g_value_set_boolean (value,
                           gtk_tree_view_column_get_clickable (tree_column));
566 567 568
      break;

    case PROP_WIDGET:
Havoc Pennington's avatar
Havoc Pennington committed
569 570
      g_value_set_object (value,
                          (GObject*) gtk_tree_view_column_get_widget (tree_column));
571 572
      break;

Havoc Pennington's avatar
Havoc Pennington committed
573 574 575
    case PROP_ALIGNMENT:
      g_value_set_float (value,
                         gtk_tree_view_column_get_alignment (tree_column));
576 577
      break;

578 579 580 581 582
    case PROP_REORDERABLE:
      g_value_set_boolean (value,
			   gtk_tree_view_column_get_reorderable (tree_column));
      break;

Havoc Pennington's avatar
Havoc Pennington committed
583 584 585 586 587 588 589 590 591 592
    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;
      
593 594 595 596 597 598
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
/* Implementation of GtkCellLayout interface
 */

static void
gtk_tree_view_column_cell_layout_pack_start (GtkCellLayout   *cell_layout,
                                             GtkCellRenderer *cell,
                                             gboolean         expand)
{
  GtkTreeViewColumn *column;
  GtkTreeViewColumnCellInfo *cell_info;

  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (cell_layout));
  column = GTK_TREE_VIEW_COLUMN (cell_layout);
  g_return_if_fail (! gtk_tree_view_column_get_cell_info (column, cell));

  g_object_ref (cell);
  gtk_object_sink (GTK_OBJECT (cell));

  cell_info = g_new0 (GtkTreeViewColumnCellInfo, 1);
  cell_info->cell = cell;
  cell_info->expand = expand ? TRUE : FALSE;
  cell_info->pack = GTK_PACK_START;
  cell_info->has_focus = 0;
  cell_info->attributes = NULL;

  column->cell_list = g_list_append (column->cell_list, cell_info);
}

static void
gtk_tree_view_column_cell_layout_pack_end (GtkCellLayout   *cell_layout,
                                           GtkCellRenderer *cell,
                                           gboolean         expand)
{
  GtkTreeViewColumn *column;
  GtkTreeViewColumnCellInfo *cell_info;

  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (cell_layout));
  column = GTK_TREE_VIEW_COLUMN (cell_layout);
  g_return_if_fail (! gtk_tree_view_column_get_cell_info (column, cell));

  g_object_ref (cell);
  gtk_object_sink (GTK_OBJECT (cell));

  cell_info = g_new0 (GtkTreeViewColumnCellInfo, 1);
  cell_info->cell = cell;
  cell_info->expand = expand ? TRUE : FALSE;
  cell_info->pack = GTK_PACK_END;
  cell_info->has_focus = 0;
  cell_info->attributes = NULL;

  column->cell_list = g_list_append (column->cell_list, cell_info);
}

static void
gtk_tree_view_column_cell_layout_clear (GtkCellLayout *cell_layout)
{
  GList *list;
  GtkTreeViewColumn *column;

  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (cell_layout));
  column = GTK_TREE_VIEW_COLUMN (cell_layout);

  for (list = column->cell_list; list; list = list->next)
    {
      GtkTreeViewColumnCellInfo *info = (GtkTreeViewColumnCellInfo *)list->data;

      gtk_tree_view_column_clear_attributes (column, info->cell);
      g_object_unref (info->cell);
      g_free (info);
    }

  g_list_free (column->cell_list);
  column->cell_list = NULL;
}

static void
gtk_tree_view_column_cell_layout_add_attribute (GtkCellLayout   *cell_layout,
                                                GtkCellRenderer *cell,
                                                const gchar     *attribute,
                                                gint             column)
{
  GtkTreeViewColumn *tree_column;
  GtkTreeViewColumnCellInfo *info;

  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (cell_layout));
  tree_column = GTK_TREE_VIEW_COLUMN (cell_layout);

  info = gtk_tree_view_column_get_cell_info (tree_column, cell);
  g_return_if_fail (info != NULL);

  info->attributes = g_slist_prepend (info->attributes, GINT_TO_POINTER (column));
  info->attributes = g_slist_prepend (info->attributes, g_strdup (attribute));

  if (tree_column->tree_view)
    _gtk_tree_view_column_cell_set_dirty (tree_column, TRUE);
}

static void
gtk_tree_view_column_cell_layout_set_cell_data_func (GtkCellLayout         *cell_layout,
                                                     GtkCellRenderer       *cell,
                                                     GtkCellLayoutDataFunc  func,
                                                     gpointer               func_data,
                                                     GDestroyNotify         destroy)
{
  GtkTreeViewColumn *column;
  GtkTreeViewColumnCellInfo *info;

  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (cell_layout));
  column = GTK_TREE_VIEW_COLUMN (cell_layout);

  info = gtk_tree_view_column_get_cell_info (column, cell);
  g_return_if_fail (info != NULL);

  if (info->destroy)
    {
      GDestroyNotify d = info->destroy;

      info->destroy = NULL;
      d (info->func_data);
    }

  info->func = (GtkTreeCellDataFunc)func;
  info->func_data = func_data;
  info->destroy = destroy;

  if (column->tree_view)
    _gtk_tree_view_column_cell_set_dirty (column, TRUE);
}

static void
gtk_tree_view_column_cell_layout_clear_attributes (GtkCellLayout    *cell_layout,
                                                   GtkCellRenderer  *cell_renderer)
{
  GtkTreeViewColumn *column;
  GtkTreeViewColumnCellInfo *info;

  g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (cell_layout));
  column = GTK_TREE_VIEW_COLUMN (cell_layout);

  info = gtk_tree_view_column_get_cell_info (column, cell_renderer);
  gtk_tree_view_column_clear_attributes_by_info (column, info);
}

static void
gtk_tree_view_column_clear_attributes_by_info (GtkTreeViewColumn *tree_column,
					       GtkTreeViewColumnCellInfo *info)
{
  GSList *list;

  list = info->attributes;

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

  if (tree_column->tree_view)
    _gtk_tree_view_column_cell_set_dirty (tree_column, TRUE);
}

762 763 764 765 766
/* Helper functions
 */

/* Button handling code
 */
767
static void
768
gtk_tree_view_column_create_button (GtkTreeViewColumn *tree_column)
769
{
770 771 772
  GtkTreeView *tree_view;
  GtkWidget *child;
  GtkWidget *hbox;
773

774
  tree_view = (GtkTreeView *) tree_column->tree_view;
775

776 777 778 779 780
  g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
  g_return_if_fail (tree_column->button == NULL);

  gtk_widget_push_composite_child ();
  tree_column->button = gtk_button_new ();
Jonathan Blandford's avatar
Jonathan Blandford committed
781
  gtk_widget_add_events (tree_column->button, GDK_POINTER_MOTION_MASK);
782 783 784
  gtk_widget_pop_composite_child ();

  /* make sure we own a reference to it as well. */
785 786
  if (tree_view->priv->header_window)
    gtk_widget_set_parent_window (tree_column->button, tree_view->priv->header_window);
787
  gtk_widget_set_parent (tree_column->button, GTK_WIDGET (tree_view));
788

Manish Singh's avatar
Manish Singh committed
789
  g_signal_connect (tree_column->button, "event",
790
		    G_CALLBACK (gtk_tree_view_column_button_event),
Manish Singh's avatar
Manish Singh committed
791 792 793 794
		    tree_column);
  g_signal_connect (tree_column->button, "clicked",
		    G_CALLBACK (gtk_tree_view_column_button_clicked),
		    tree_column);
795 796 797 798 799 800 801 802 803 804 805 806 807 808

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

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

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

Manish Singh's avatar
Manish Singh committed
809
  g_signal_connect (child, "mnemonic_activate",
810
		    G_CALLBACK (gtk_tree_view_column_mnemonic_activate),
Manish Singh's avatar
Manish Singh committed
811
		    tree_column);
812

813 814 815 816 817 818 819 820 821 822 823 824 825
  if (tree_column->xalign <= 0.5)
    gtk_box_pack_end (GTK_BOX (hbox), tree_column->arrow, FALSE, FALSE, 0);
  else
    gtk_box_pack_start (GTK_BOX (hbox), tree_column->arrow, FALSE, FALSE, 0);

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

  gtk_widget_show (hbox);
  gtk_widget_show (tree_column->alignment);
  gtk_tree_view_column_update_button (tree_column);
826 827
}

828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
static void 
gtk_tree_view_column_update_button (GtkTreeViewColumn *tree_column)
{
  GtkWidget *hbox;
  GtkWidget *alignment;
  GtkWidget *arrow;
  GtkWidget *current_child;

  /* Create a button if necessary */
  if (tree_column->visible &&
      tree_column->button == NULL &&
      tree_column->tree_view &&
      GTK_WIDGET_REALIZED (tree_column->tree_view))
    gtk_tree_view_column_create_button (tree_column);
  
  if (! tree_column->button)
    return;

  hbox = GTK_BIN (tree_column->button)->child;
  alignment = tree_column->alignment;
  arrow = tree_column->arrow;
  current_child = GTK_BIN (alignment)->child;

  /* Set up the actual button */
  gtk_alignment_set (GTK_ALIGNMENT (alignment), tree_column->xalign,
		     0.5, 0.0, 0.0);
      
  if (tree_column->child)
    {
      if (current_child != tree_column->child)
	{
	  gtk_container_remove (GTK_CONTAINER (alignment),
				current_child);
	  gtk_container_add (GTK_CONTAINER (alignment),
			     tree_column->child);
	}
    }
  else 
    {
      if (current_child == NULL)
	{
	  current_child = gtk_label_new (NULL);
	  gtk_widget_show (current_child);
	  gtk_container_add (GTK_CONTAINER (alignment),
			     current_child);
	}

      g_return_if_fail (GTK_IS_LABEL (current_child));

      if (tree_column->title)
878 879
	gtk_label_set_text_with_mnemonic (GTK_LABEL (current_child),
					  tree_column->title);
880
      else
881 882
	gtk_label_set_text_with_mnemonic (GTK_LABEL (current_child),
					  "");
883 884 885 886
    }

  switch (tree_column->sort_order)
    {
887
    case GTK_SORT_ASCENDING:
888 889 890 891 892
      gtk_arrow_set (GTK_ARROW (arrow),
		     GTK_ARROW_DOWN,
		     GTK_SHADOW_IN);
      break;

893
    case GTK_SORT_DESCENDING:
894 895 896 897 898 899 900 901 902 903
      gtk_arrow_set (GTK_ARROW (arrow),
		     GTK_ARROW_UP,
		     GTK_SHADOW_IN);
      break;
          
    default:
      g_warning (G_STRLOC": bad sort order");
      break;
    }

904 905 906 907
  /* 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
   */
Manish Singh's avatar
Manish Singh committed
908
  g_object_ref (arrow);
909 910 911 912 913 914 915 916 917 918 919 920
  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);
    }
Manish Singh's avatar
Manish Singh committed
921
  g_object_unref (arrow);
922 923 924 925 926 927

  if (tree_column->show_sort_indicator)
    gtk_widget_show (arrow);
  else
    gtk_widget_hide (arrow);

928 929
  /* It's always safe to hide the button.  It isn't always safe to show it, as
   * if you show it before it's realized, it'll get the wrong window. */
930 931 932 933 934 935
  if (tree_column->button &&
      tree_column->tree_view != NULL &&
      GTK_WIDGET_REALIZED (tree_column->tree_view))
    {
      if (tree_column->visible)
	{
936
	  gtk_widget_show_now (tree_column->button);
937 938
	  if (tree_column->window)
	    {
939
	      if (tree_column->resizable)
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
		{
		  gdk_window_show (tree_column->window);
		  gdk_window_raise (tree_column->window);
		}
	      else
		{
		  gdk_window_hide (tree_column->window);
		}
	    }
	}
      else
	{
	  gtk_widget_hide (tree_column->button);
	  if (tree_column->window)
	    gdk_window_hide (tree_column->window);
	}
    }
957 958
  
  if (tree_column->reorderable || tree_column->clickable)
959 960 961
    {
      GTK_WIDGET_SET_FLAGS (tree_column->button, GTK_CAN_FOCUS);
    }
962
  else
963 964 965
    {
      GTK_WIDGET_UNSET_FLAGS (tree_column->button, GTK_CAN_FOCUS);
      if (GTK_WIDGET_HAS_FOCUS (tree_column->button))
966 967 968
	{
	  GtkWidget *toplevel = gtk_widget_get_toplevel (tree_column->tree_view);
	  if (GTK_WIDGET_TOPLEVEL (toplevel))
969 970 971
	    {
	      gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
	    }
972
	}
973
    }
974 975 976 977 978 979
  /* Queue a resize on the assumption that we always want to catch all changes
   * and columns don't change all that often.
   */
  if (GTK_WIDGET_REALIZED (tree_column->tree_view))
     gtk_widget_queue_resize (tree_column->tree_view);

980 981 982 983
}

/* Button signal handlers
 */
984 985

static gint
986 987 988
gtk_tree_view_column_button_event (GtkWidget *widget,
				   GdkEvent  *event,
				   gpointer   data)
989
{
990 991
  GtkTreeViewColumn *column = (GtkTreeViewColumn *) data;

992 993
  g_return_val_if_fail (event != NULL, FALSE);

994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
  if (event->type == GDK_BUTTON_PRESS &&
      column->reorderable)
    {
      column->maybe_reordered = TRUE;
      gdk_window_get_pointer (widget->window,
			      &column->drag_x,
			      &column->drag_y,
			      NULL);
      gtk_widget_grab_focus (widget);
    }

  if (event->type == GDK_BUTTON_RELEASE &&
      column->maybe_reordered)
    column->maybe_reordered = FALSE;

  if (event->type == GDK_MOTION_NOTIFY &&
      (column->maybe_reordered) &&
      (gtk_drag_check_threshold (widget,
				 column->drag_x,
				 column->drag_y,
				 (gint) ((GdkEventMotion *)event)->x,
				 (gint) ((GdkEventMotion *)event)->y)))
1016
    {
1017
      column->maybe_reordered = FALSE;
1018 1019 1020 1021
      /* this is to change our drag_x to be relative to
       * tree_view->priv->bin_window, instead of our window.
       */
      column->drag_x -= column->button->allocation.x;
1022
      _gtk_tree_view_column_start_drag (GTK_TREE_VIEW (column->tree_view), column);
1023
      return TRUE;
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
    }
  if (column->clickable == FALSE)
    {
      switch (event->type)
	{
	case GDK_BUTTON_PRESS:
	case GDK_2BUTTON_PRESS:
	case GDK_3BUTTON_PRESS:
	case GDK_MOTION_NOTIFY:
	case GDK_BUTTON_RELEASE:
	case GDK_ENTER_NOTIFY:
	case GDK_LEAVE_NOTIFY:
	  return TRUE;
	default:
	  return FALSE;
	}
1040 1041 1042 1043 1044
    }
  return FALSE;
}


1045 1046 1047
static void
gtk_tree_view_column_button_clicked (GtkWidget *widget, gpointer data)
{
Manish Singh's avatar
Manish Singh committed
1048
  g_signal_emit_by_name (data, "clicked");
Jonathan Blandford's avatar
Jonathan Blandford committed
1049
}
1050

1051 1052 1053 1054 1055 1056 1057 1058 1059
static gboolean
gtk_tree_view_column_mnemonic_activate (GtkWidget *widget,
					gboolean   group_cycling,
					gpointer   data)
{
  GtkTreeViewColumn *column = (GtkTreeViewColumn *)data;

  g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (column), FALSE);

1060
  GTK_TREE_VIEW (column->tree_view)->priv->focus_column = column;
1061 1062 1063 1064 1065
  if (column->clickable)
    gtk_button_clicked (GTK_BUTTON (column->button));
  else if (GTK_WIDGET_CAN_FOCUS (column->button))
    gtk_widget_grab_focus (column->button);
  else
1066
    gtk_widget_grab_focus (column->tree_view);
1067 1068 1069 1070

  return TRUE;
}

Jonathan Blandford's avatar
Jonathan Blandford committed
1071
static void
1072
gtk_tree_view_model_sort_column_changed (GtkTreeSortable   *sortable,
Jonathan Blandford's avatar
Jonathan Blandford committed
1073
					 GtkTreeViewColumn *column)
Jonathan Blandford's avatar
Jonathan Blandford committed
1074
{
1075
  gint sort_column_id;
1076
  GtkSortType order;
Jonathan Blandford's avatar
Jonathan Blandford committed
1077

1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
  if (gtk_tree_sortable_get_sort_column_id (sortable,
					    &sort_column_id,
					    &order))
    {
      if (sort_column_id == column->sort_column_id)
	{
	  gtk_tree_view_column_set_sort_indicator (column, TRUE);
	  gtk_tree_view_column_set_sort_order (column, order);
	}
      else
	{
	  gtk_tree_view_column_set_sort_indicator (column, FALSE);
	}
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
1092 1093 1094 1095
  else
    {
      gtk_tree_view_column_set_sort_indicator (column, FALSE);
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
1096 1097
}

1098 1099 1100
static void
gtk_tree_view_column_sort (GtkTreeViewColumn *tree_column,
			   gpointer           data)
1101
{
Jonathan Blandford's avatar
Jonathan Blandford committed
1102 1103 1104 1105
  gint sort_column_id;
  GtkSortType order;
  gboolean has_sort_column;
  gboolean has_default_sort_func;
1106

1107
  g_return_if_fail (tree_column->tree_view != NULL);
1108

Jonathan Blandford's avatar
Jonathan Blandford committed
1109 1110 1111 1112 1113 1114 1115 1116 1117
  has_sort_column =
    gtk_tree_sortable_get_sort_column_id (GTK_TREE_SORTABLE (GTK_TREE_VIEW (tree_column->tree_view)->priv->model),
					  &sort_column_id,
					  &order);
  has_default_sort_func =
    gtk_tree_sortable_has_default_sort_func (GTK_TREE_SORTABLE (GTK_TREE_VIEW (tree_column->tree_view)->priv->model));

  if (has_sort_column &&
      sort_column_id == tree_column->sort_column_id)
1118
    {
Jonathan Blandford's avatar
Jonathan Blandford committed
1119 1120 1121 1122 1123 1124 1125 1126
      if (order == GTK_SORT_ASCENDING)
	gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (GTK_TREE_VIEW (tree_column->tree_view)->priv->model),
					      tree_column->sort_column_id,
					      GTK_SORT_DESCENDING);
      else if (order == GTK_SORT_DESCENDING && has_default_sort_func)
	gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (GTK_TREE_VIEW (tree_column->tree_view)->priv->model),
					      GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
					      GTK_SORT_ASCENDING);
1127
      else
Jonathan Blandford's avatar
Jonathan Blandford committed
1128 1129 1130
	gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (GTK_TREE_VIEW (tree_column->tree_view)->priv->model),
					      tree_column->sort_column_id,
					      GTK_SORT_ASCENDING);
1131 1132 1133
    }
  else
    {
Jonathan Blandford's avatar
Jonathan Blandford committed
1134 1135 1136
      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (GTK_TREE_VIEW (tree_column->tree_view)->priv->model),
					    tree_column->sort_column_id,
					    GTK_SORT_ASCENDING);
1137 1138
    }
}
1139

1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151

static void
gtk_tree_view_column_setup_sort_column_id_callback (GtkTreeViewColumn *tree_column)
{
  GtkTreeModel *model;

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

  model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_column->tree_view));

  if (model == NULL)
Jonathan Blandford's avatar
Jonathan Blandford committed
1152
    return;
1153

1154 1155 1156 1157
  if (GTK_IS_TREE_SORTABLE (model) &&
      tree_column->sort_column_id != -1)
    {
      gint real_sort_column_id;
1158
      GtkSortType real_order;
1159 1160

      if (tree_column->sort_column_changed_signal == 0)
1161
        tree_column->sort_column_changed_signal =
Manish Singh's avatar
Manish Singh committed
1162 1163 1164
	  g_signal_connect (model, "sort_column_changed",
			    G_CALLBACK (gtk_tree_view_model_sort_column_changed),
			    tree_column);
1165
      
1166 1167 1168 1169 1170 1171 1172
      if (gtk_tree_sortable_get_sort_column_id (GTK_TREE_SORTABLE (model),
						&real_sort_column_id,
						&real_order) &&
	  (real_sort_column_id == tree_column->sort_column_id))
	{
	  gtk_tree_view_column_set_sort_indicator (tree_column, TRUE);
	  gtk_tree_view_column_set_sort_order (tree_column, real_order);
1173

1174 1175 1176 1177
	  return;
	}
    }
}
1178 1179


1180 1181 1182
/* Exported Private Functions.
 * These should only be called by gtktreeview.c or gtktreeviewcolumn.c
 */
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197

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 (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);

1198 1199 1200 1201 1202
  gtk_widget_set_parent_window (column->button, tree_view->priv->header_window);

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

1203 1204 1205 1206
  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));
Kristian Rietveld's avatar
Kristian Rietveld committed
1207 1208
  attr.event_mask = gtk_widget_get_events (GTK_WIDGET (tree_view)) |
                    (GDK_BUTTON_PRESS_MASK |
1209 1210 1211 1212 1213
		     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;
1214 1215
  attr.cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (tree_view->priv->header_window),
					    GDK_SB_H_DOUBLE_ARROW);
1216 1217 1218 1219 1220 1221 1222 1223 1224
  attr.y = 0;
  attr.width = TREE_VIEW_DRAG_WIDTH;
  attr.height = tree_view->priv->header_height;

  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);
1225 1226

  gtk_tree_view_column_update_button (column);
1227 1228

  gdk_cursor_unref (attr.cursor);
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
}

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;
}

1242 1243 1244 1245 1246
void
_gtk_tree_view_column_unset_model (GtkTreeViewColumn *column,
				   GtkTreeModel      *old_model)
{
  if (column->sort_column_changed_signal)
Manish Singh's avatar
Manish Singh committed
1247 1248 1249 1250 1251
    {
      g_signal_handler_disconnect (old_model,
				   column->sort_column_changed_signal);
      column->sort_column_changed_signal = 0;
    }
1252 1253
}

1254 1255 1256 1257
void
_gtk_tree_view_column_set_tree_view (GtkTreeViewColumn *column,
				     GtkTreeView       *tree_view)
{
1258
  g_assert (column->tree_view == NULL);
1259

1260 1261
  column->tree_view = GTK_WIDGET (tree_view);
  gtk_tree_view_column_create_button (column);
1262

1263
  column->property_changed_signal =
Manish Singh's avatar
Manish Singh committed
1264
	  g_signal_connect_swapped (tree_view,
1265
				    "notify::model",
Manish Singh's avatar
Manish Singh committed
1266
				    G_CALLBACK (gtk_tree_view_column_setup_sort_column_id_callback),
1267
				    column);
1268

1269
  gtk_tree_view_column_setup_sort_column_id_callback (column);
1270 1271 1272 1273 1274 1275 1276 1277 1278
}

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);
    }
1279 1280
  if (column->property_changed_signal)
    {
Manish Singh's avatar
Manish Singh committed
1281
      g_signal_handler_disconnect (column->tree_view, column->property_changed_signal);
1282 1283 1284 1285 1286
      column->property_changed_signal = 0;
    }

  if (column->sort_column_changed_signal)
    {
Manish Singh's avatar
Manish Singh committed
1287
      g_signal_handler_disconnect (gtk_tree_view_get_model (GTK_TREE_VIEW (column->tree_view)),
Jonathan Blandford's avatar
Jonathan Blandford committed
1288
				   column->sort_column_changed_signal);
1289 1290 1291
      column->sort_column_changed_signal = 0;
    }

1292 1293 1294 1295
  column->tree_view = NULL;
  column->button = NULL;
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1296 1297 1298 1299 1300
gboolean
_gtk_tree_view_column_has_editable_cell (GtkTreeViewColumn *column)
{
  GList *list;

Kristian Rietveld's avatar
Kristian Rietveld committed
1301
  for (list = column->cell_list; list; list = list->next)
Kristian Rietveld's avatar
Kristian Rietveld committed
1302 1303 1304 1305 1306 1307 1308
    if (((GtkTreeViewColumnCellInfo *)list->data)->cell->mode ==
	GTK_CELL_RENDERER_MODE_EDITABLE)
      return TRUE;

  return FALSE;
}

1309
/* gets cell being edited */
1310
GtkCellRenderer *
1311
_gtk_tree_view_column_get_edited_cell (GtkTreeViewColumn *column)
1312 1313 1314
{
  GList *list;

Kristian Rietveld's avatar
Kristian Rietveld committed
1315
  for (list = column->cell_list; list; list = list->next)
1316
    if (((GtkTreeViewColumnCellInfo *)list->data)->in_editing_mode)
1317 1318 1319 1320 1321
      return ((GtkTreeViewColumnCellInfo *)list->data)->cell;

  return NULL;
}

Kristian Rietveld's avatar
Kristian Rietveld committed
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
gint
_gtk_tree_view_column_count_special_cells (GtkTreeViewColumn *column)
{
  gint i = 0;
  GList *list;

  for (list = column->cell_list; list; list = list->next)
    {
      GtkTreeViewColumnCellInfo *cellinfo = list->data;

1332 1333 1334
      if ((cellinfo->cell->mode == GTK_CELL_RENDERER_MODE_EDITABLE ||
	  cellinfo->cell->mode == GTK_CELL_RENDERER_MODE_ACTIVATABLE) &&
	  cellinfo->cell->visible)
Kristian Rietveld's avatar
Kristian Rietveld committed
1335 1336 1337 1338 1339 1340
	i++;
    }

  return i;
}

1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
GtkCellRenderer *
_gtk_tree_view_column_get_cell_at_pos (GtkTreeViewColumn *column,
				       gint               x)
{
  GList *list;
  gint current_x = 0;

  list = gtk_tree_view_column_cell_first (column);
  for (; list; list = gtk_tree_view_column_cell_next (column, list))
   {
     GtkTreeViewColumnCellInfo *cellinfo = list->data;
     if (current_x <= x && x <= current_x + cellinfo->real_width)
       return cellinfo->cell;
     current_x += cellinfo->real_width;
   }

  return NULL;
}

1360 1361 1362
/* Public Functions */


1363 1364 1365 1366 1367 1368 1369
/**
 * gtk_tree_view_column_new:
 * 
 * Creates a new #GtkTreeViewColumn.
 * 
 * Return value: A newly created #GtkTreeViewColumn.
 **/
1370
GtkTreeViewColumn *
1371 1372
gtk_tree_view_column_new (void)
{
1373
  GtkTreeViewColumn *tree_column;
1374

Manish Singh's avatar
Manish Singh committed
1375
  tree_column = g_object_new (GTK_TYPE_TREE_VIEW_COLUMN, NULL);
1376

1377
  return tree_column;
1378 1379
}

1380 1381 1382 1383
/**
 * gtk_tree_view_column_new_with_attributes:
 * @title: The title to set the header to.
 * @cell: The #GtkCellRenderer.
Matthias Clasen's avatar
Matthias Clasen committed
1384
 * @Varargs: A %NULL-terminated list of attributes.
1385 1386
 * 
 * Creates a new #GtkTreeViewColumn with a number of default values.  This is
Matthias Clasen's avatar
Matthias Clasen committed
1387 1388 1389
 * equivalent to calling gtk_tree_view_column_set_title(),
 * gtk_tree_view_column_pack_start(), and
 * gtk_tree_view_column_set_attributes() on the newly created #GtkTreeViewColumn.
1390 1391 1392
 *
 * Here's a simple example:
 * <informalexample><programlisting>
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
 *  enum { TEXT_COLUMN, COLOR_COLUMN, N_COLUMNS };
 *  ...
 *  {
 *    GtkTreeViewColumn *column;
 *    GtkCellRenderer   *renderer = gtk_cell_renderer_text_new (<!-- -->);
 *  
 *    column = gtk_tree_view_column_new_with_attributes ("Title",
 *                                                       renderer,
 *                                                       "text", TEXT_COLUMN,
 *                                                       "foreground", COLOR_COLUMN,
 *                                                       NULL);
 *  }
1405
 * </programlisting></informalexample>
1406 1407 1408
 * 
 * Return value: A newly created #GtkTreeViewColumn.
 **/
1409
GtkTreeViewColumn *
1410
gtk_tree_view_column_new_with_attributes (const gchar     *title,
1411 1412 1413
					  GtkCellRenderer *cell,
					  ...)
{
1414
  GtkTreeViewColumn *retval;
1415 1416 1417 1418
  va_list args;

  retval = gtk_tree_view_column_new ();

1419
  gtk_tree_view_column_set_title (retval, title);
1420
  gtk_tree_view_column_pack_start (retval, cell, TRUE);
1421 1422

  va_start (args, cell);
1423
  gtk_tree_view_column_set_attributesv (retval, cell, args);
1424 1425 1426 1427 1428
  va_end (args);

  return retval;
}

1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
static GtkTreeViewColumnCellInfo *
gtk_tree_view_column_get_cell_info (GtkTreeViewColumn *tree_column,
				    GtkCellRenderer   *cell_renderer)
{
  GList *list;
  for (list = tree_column->cell_list; list; list = list->next)
    if (((GtkTreeViewColumnCellInfo *)list->data)->cell == cell_renderer)
      return (GtkTreeViewColumnCellInfo *) list->data;
  return NULL;
}

1440

1441
/**
1442
 * gtk_tree_view_column_pack_start:
1443
 * @tree_column: A #GtkTreeViewColumn.
Matthias Clasen's avatar
Matthias Clasen committed
1444
 * @cell: The #GtkCellRenderer. 
1445
 * @expand: %TRUE if @cell is to be given extra space allocated to @tree_column.
1446
 *
1447
 * Packs the @cell into the beginning of the column. If @expand is %FALSE, then
1448
 * the @cell is allocated no more space than it needs. Any unused space is divided
1449
 * evenly between cells for which @expand is %TRUE.
1450 1451
 **/
void
1452 1453
gtk_tree_view_column_pack_start (GtkTreeViewColumn *tree_column,
				 GtkCellRenderer   *cell,
1454
				 gboolean           expand)
1455
{
1456
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (tree_column), cell, expand);
1457
}
1458

1459 1460 1461 1462
/**
 * gtk_tree_view_column_pack_end:
 * @tree_column: A #GtkTreeViewColumn.
 * @cell: The #GtkCellRenderer. 
1463
 * @expand: %TRUE if @cell is to be given extra space allocated to @tree_column.
1464
 *
1465
 * Adds the @cell to end of the column. If @expand is %FALSE, then the @cell
1466
 * is allocated no more space than it needs. Any unused space is divided