gtktreeviewcolumn.c 111 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.
 */

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

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

enum
{
59 60 61 62
  CLICKED,
  LAST_SIGNAL
};

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

79
/* Type methods */
80
static void gtk_tree_view_column_cell_layout_init              (GtkCellLayoutIface      *iface);
81 82 83 84 85 86 87 88 89 90 91 92

/* 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);

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
/* 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);
Kristian Rietveld's avatar
Kristian Rietveld committed
112 113 114
static void gtk_tree_view_column_cell_layout_reorder            (GtkCellLayout         *cell_layout,
                                                                 GtkCellRenderer       *cell,
                                                                 gint                   position);
115
static GList *gtk_tree_view_column_cell_layout_get_cells        (GtkCellLayout         *cell_layout);
116

117
/* Button handling code */
118 119 120 121
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 */
122
static gint gtk_tree_view_column_button_event                  (GtkWidget               *widget,
123 124 125 126
								GdkEvent                *event,
								gpointer                 data);
static void gtk_tree_view_column_button_clicked                (GtkWidget               *widget,
								gpointer                 data);
127 128 129
static gboolean gtk_tree_view_column_mnemonic_activate         (GtkWidget *widget,
					                        gboolean   group_cycling,
								gpointer   data);
130 131 132 133 134 135 136 137 138 139

/* 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,
140
								GtkCellRenderer         *cell_renderer,
141
								va_list                  args);
142 143
static GtkTreeViewColumnCellInfo *gtk_tree_view_column_get_cell_info (GtkTreeViewColumn *tree_column,
								      GtkCellRenderer   *cell_renderer);
144

145 146 147 148 149 150 151
/* 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);
152 153
static void gtk_tree_view_column_clear_attributes_by_info      (GtkTreeViewColumn      *tree_column,
					                        GtkTreeViewColumnCellInfo *info);
Johan Dahlin's avatar
Johan Dahlin committed
154 155
/* GtkBuildable implementation */
static void gtk_tree_view_column_buildable_init                 (GtkBuildableIface     *iface);
156 157 158

static guint tree_column_signals[LAST_SIGNAL] = { 0 };

Matthias Clasen's avatar
Matthias Clasen committed
159 160
G_DEFINE_TYPE_WITH_CODE (GtkTreeViewColumn, gtk_tree_view_column, GTK_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
Johan Dahlin's avatar
Johan Dahlin committed
161 162 163 164
						gtk_tree_view_column_cell_layout_init)
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
						gtk_tree_view_column_buildable_init))

165 166 167 168

static void
gtk_tree_view_column_class_init (GtkTreeViewColumnClass *class)
{
169
  GObjectClass *object_class;
170

171
  object_class = (GObjectClass*) class;
172

173
  class->clicked = NULL;
174

175
  object_class->finalize = gtk_tree_view_column_finalize;
176 177 178
  object_class->set_property = gtk_tree_view_column_set_property;
  object_class->get_property = gtk_tree_view_column_get_property;
  
179
  tree_column_signals[CLICKED] =
Matthias Clasen's avatar
Matthias Clasen committed
180
    g_signal_new (I_("clicked"),
Manish Singh's avatar
Manish Singh committed
181
                  G_OBJECT_CLASS_TYPE (object_class),
182 183 184
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTreeViewColumnClass, clicked),
                  NULL, NULL,
185
                  _gtk_marshal_VOID__VOID,
Manish Singh's avatar
Manish Singh committed
186
                  G_TYPE_NONE, 0);
187 188 189 190

  g_object_class_install_property (object_class,
                                   PROP_VISIBLE,
                                   g_param_spec_boolean ("visible",
191 192
                                                        P_("Visible"),
                                                        P_("Whether to display the column"),
193
                                                         TRUE,
194
                                                         GTK_PARAM_READWRITE));
195
  
196 197 198
  g_object_class_install_property (object_class,
                                   PROP_RESIZABLE,
                                   g_param_spec_boolean ("resizable",
199 200
							 P_("Resizable"),
							 P_("Column is user-resizable"),
201
                                                         FALSE,
202
                                                         GTK_PARAM_READWRITE));
203
  
204 205 206
  g_object_class_install_property (object_class,
                                   PROP_WIDTH,
                                   g_param_spec_int ("width",
207 208
						     P_("Width"),
						     P_("Current width of the column"),
209 210 211
						     0,
						     G_MAXINT,
						     0,
212
						     GTK_PARAM_READABLE));
213 214 215 216 217 218 219 220
  g_object_class_install_property (object_class,
                                   PROP_SPACING,
                                   g_param_spec_int ("spacing",
						     P_("Spacing"),
						     P_("Space which is inserted between cells"),
						     0,
						     G_MAXINT,
						     0,
221
						     GTK_PARAM_READWRITE));
222 223 224
  g_object_class_install_property (object_class,
                                   PROP_SIZING,
                                   g_param_spec_enum ("sizing",
225 226
                                                      P_("Sizing"),
                                                      P_("Resize mode of the column"),
227
                                                      GTK_TYPE_TREE_VIEW_COLUMN_SIZING,
228
                                                      GTK_TREE_VIEW_COLUMN_GROW_ONLY,
229
                                                      GTK_PARAM_READWRITE));
230 231
  
  g_object_class_install_property (object_class,
232
                                   PROP_FIXED_WIDTH,
Matthias Clasen's avatar
x  
Matthias Clasen committed
233
                                   g_param_spec_int ("fixed-width",
234 235
                                                     P_("Fixed Width"),
                                                     P_("Current fixed width of the column"),
236 237 238
                                                     1,
                                                     G_MAXINT,
                                                     1, /* not useful */
239
                                                     GTK_PARAM_READWRITE));
240 241 242

  g_object_class_install_property (object_class,
                                   PROP_MIN_WIDTH,
Matthias Clasen's avatar
x  
Matthias Clasen committed
243
                                   g_param_spec_int ("min-width",
244 245
                                                     P_("Minimum Width"),
                                                     P_("Minimum allowed width of the column"),
246 247
                                                     -1,
                                                     G_MAXINT,
248
                                                     -1,
249
                                                     GTK_PARAM_READWRITE));
250 251 252

  g_object_class_install_property (object_class,
                                   PROP_MAX_WIDTH,
Matthias Clasen's avatar
x  
Matthias Clasen committed
253
                                   g_param_spec_int ("max-width",
254 255
                                                     P_("Maximum Width"),
                                                     P_("Maximum allowed width of the column"),
256 257
                                                     -1,
                                                     G_MAXINT,
258
                                                     -1,
259
                                                     GTK_PARAM_READWRITE));
260 261 262 263

  g_object_class_install_property (object_class,
                                   PROP_TITLE,
                                   g_param_spec_string ("title",
264 265
                                                        P_("Title"),
                                                        P_("Title to appear in column header"),
266
                                                        "",
267
                                                        GTK_PARAM_READWRITE));
268
  
Jonathan Blandford's avatar
Jonathan Blandford committed
269 270 271
  g_object_class_install_property (object_class,
                                   PROP_EXPAND,
                                   g_param_spec_boolean ("expand",
272 273
							 P_("Expand"),
							 P_("Column gets share of extra width allocated to the widget"),
Jonathan Blandford's avatar
Jonathan Blandford committed
274
							 FALSE,
275
							 GTK_PARAM_READWRITE));
Jonathan Blandford's avatar
Jonathan Blandford committed
276
  
277 278 279
  g_object_class_install_property (object_class,
                                   PROP_CLICKABLE,
                                   g_param_spec_boolean ("clickable",
280 281
                                                        P_("Clickable"),
                                                        P_("Whether the header can be clicked"),
282
                                                         FALSE,
283
                                                         GTK_PARAM_READWRITE));
284 285 286 287 288
  

  g_object_class_install_property (object_class,
                                   PROP_WIDGET,
                                   g_param_spec_object ("widget",
289 290
                                                        P_("Widget"),
                                                        P_("Widget to put in column header button instead of column title"),
291
                                                        GTK_TYPE_WIDGET,
292
                                                        GTK_PARAM_READWRITE));
293 294

  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
295 296
                                   PROP_ALIGNMENT,
                                   g_param_spec_float ("alignment",
297 298
                                                       P_("Alignment"),
                                                       P_("X Alignment of the column header text or widget"),
Havoc Pennington's avatar
Havoc Pennington committed
299 300
                                                       0.0,
                                                       1.0,
301
                                                       0.0,
302
                                                       GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
303

304 305 306
  g_object_class_install_property (object_class,
                                   PROP_REORDERABLE,
                                   g_param_spec_boolean ("reorderable",
307 308
							 P_("Reorderable"),
							 P_("Whether the column can be reordered around the headers"),
309
							 FALSE,
310
							 GTK_PARAM_READWRITE));
311

Havoc Pennington's avatar
Havoc Pennington committed
312 313
  g_object_class_install_property (object_class,
                                   PROP_SORT_INDICATOR,
Matthias Clasen's avatar
x  
Matthias Clasen committed
314
                                   g_param_spec_boolean ("sort-indicator",
315 316
                                                        P_("Sort indicator"),
                                                        P_("Whether to show a sort indicator"),
Havoc Pennington's avatar
Havoc Pennington committed
317
                                                         FALSE,
318
                                                         GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
319 320 321

  g_object_class_install_property (object_class,
                                   PROP_SORT_ORDER,
Matthias Clasen's avatar
x  
Matthias Clasen committed
322
                                   g_param_spec_enum ("sort-order",
323 324
                                                      P_("Sort order"),
                                                      P_("Sort direction the sort indicator should indicate"),
325 326
                                                      GTK_TYPE_SORT_TYPE,
                                                      GTK_SORT_ASCENDING,
327
                                                      GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
328
  
329 330
}

Johan Dahlin's avatar
Johan Dahlin committed
331 332 333
static void
gtk_tree_view_column_buildable_init (GtkBuildableIface *iface)
{
334
  iface->add_child = _gtk_cell_layout_buildable_add_child;
Johan Dahlin's avatar
Johan Dahlin committed
335 336 337 338
  iface->custom_tag_start = _gtk_cell_layout_buildable_custom_tag_start;
  iface->custom_tag_end = _gtk_cell_layout_buildable_custom_tag_end;
}

339 340 341 342 343 344 345 346 347
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;
Kristian Rietveld's avatar
Kristian Rietveld committed
348
  iface->reorder = gtk_tree_view_column_cell_layout_reorder;
349
  iface->get_cells = gtk_tree_view_column_cell_layout_get_cells;
350 351
}

352 353 354 355
static void
gtk_tree_view_column_init (GtkTreeViewColumn *tree_column)
{
  tree_column->button = NULL;
Havoc Pennington's avatar
Havoc Pennington committed
356
  tree_column->xalign = 0.0;
357
  tree_column->width = 0;
358
  tree_column->spacing = 0;
359
  tree_column->requested_width = -1;
360 361
  tree_column->min_width = -1;
  tree_column->max_width = -1;
Jonathan Blandford's avatar
Jonathan Blandford committed
362
  tree_column->resized_width = 0;
363
  tree_column->column_type = GTK_TREE_VIEW_COLUMN_GROW_ONLY;
364
  tree_column->visible = TRUE;
365
  tree_column->resizable = FALSE;
366
  tree_column->expand = FALSE;
367
  tree_column->clickable = FALSE;
368
  tree_column->dirty = TRUE;
369
  tree_column->sort_order = GTK_SORT_ASCENDING;
Havoc Pennington's avatar
Havoc Pennington committed
370
  tree_column->show_sort_indicator = FALSE;
371
  tree_column->property_changed_signal = 0;
372 373
  tree_column->sort_clicked_signal = 0;
  tree_column->sort_column_changed_signal = 0;
374
  tree_column->sort_column_id = -1;
375 376
  tree_column->reorderable = FALSE;
  tree_column->maybe_reordered = FALSE;
377
  tree_column->fixed_width = 1;
Jonathan Blandford's avatar
Jonathan Blandford committed
378
  tree_column->use_resized_width = FALSE;
379
  tree_column->title = g_strdup ("");
380 381 382 383 384 385
}

static void
gtk_tree_view_column_finalize (GObject *object)
{
  GtkTreeViewColumn *tree_column = (GtkTreeViewColumn *) object;
386 387 388 389 390
  GList *list;

  for (list = tree_column->cell_list; list; list = list->next)
    {
      GtkTreeViewColumnCellInfo *info = (GtkTreeViewColumnCellInfo *) list->data;
391 392 393 394 395 396 397 398

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

	  info->destroy = NULL;
	  d (info->func_data);
	}
399
      gtk_tree_view_column_clear_attributes_by_info (tree_column, info);
Manish Singh's avatar
Manish Singh committed
400
      g_object_unref (info->cell);
401 402
      g_free (info);
    }
403 404

  g_free (tree_column->title);
405 406 407
  g_list_free (tree_column->cell_list);

  if (tree_column->child)
408
    g_object_unref (tree_column->child);
409

Matthias Clasen's avatar
Matthias Clasen committed
410
  G_OBJECT_CLASS (gtk_tree_view_column_parent_class)->finalize (object);
411 412
}

413 414 415 416
static void
gtk_tree_view_column_set_property (GObject         *object,
                                   guint            prop_id,
                                   const GValue    *value,
Tim Janik's avatar
Tim Janik committed
417
                                   GParamSpec      *pspec)
418 419 420 421 422 423 424 425 426 427 428 429
{
  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
430 431 432 433 434
    case PROP_RESIZABLE:
      gtk_tree_view_column_set_resizable (tree_column,
					  g_value_get_boolean (value));
      break;

435 436 437 438 439
    case PROP_SIZING:
      gtk_tree_view_column_set_sizing (tree_column,
                                       g_value_get_enum (value));
      break;

440 441 442
    case PROP_FIXED_WIDTH:
      gtk_tree_view_column_set_fixed_width (tree_column,
					    g_value_get_int (value));
443 444 445 446 447 448 449 450 451 452 453 454
      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;

455 456 457 458 459
    case PROP_SPACING:
      gtk_tree_view_column_set_spacing (tree_column,
					g_value_get_int (value));
      break;

460 461 462 463 464
    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 534 535 536 537
    case PROP_SPACING:
      g_value_set_int (value,
                       gtk_tree_view_column_get_spacing (tree_column));
      break;

538
    case PROP_SIZING:
Havoc Pennington's avatar
Havoc Pennington committed
539 540
      g_value_set_enum (value,
                        gtk_tree_view_column_get_sizing (tree_column));
541 542
      break;

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

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

    case PROP_MAX_WIDTH:
Havoc Pennington's avatar
Havoc Pennington committed
554 555
      g_value_set_int (value,
                       gtk_tree_view_column_get_max_width (tree_column));
556 557 558
      break;

    case PROP_TITLE:
Havoc Pennington's avatar
Havoc Pennington committed
559 560
      g_value_set_string (value,
                          gtk_tree_view_column_get_title (tree_column));
561 562
      break;

Jonathan Blandford's avatar
Jonathan Blandford committed
563 564 565 566 567
    case PROP_EXPAND:
      g_value_set_boolean (value,
                          gtk_tree_view_column_get_expand (tree_column));
      break;

568
    case PROP_CLICKABLE:
Havoc Pennington's avatar
Havoc Pennington committed
569 570
      g_value_set_boolean (value,
                           gtk_tree_view_column_get_clickable (tree_column));
571 572 573
      break;

    case PROP_WIDGET:
Havoc Pennington's avatar
Havoc Pennington committed
574 575
      g_value_set_object (value,
                          (GObject*) gtk_tree_view_column_get_widget (tree_column));
576 577
      break;

Havoc Pennington's avatar
Havoc Pennington committed
578 579 580
    case PROP_ALIGNMENT:
      g_value_set_float (value,
                         gtk_tree_view_column_get_alignment (tree_column));
581 582
      break;

583 584 585 586 587
    case PROP_REORDERABLE:
      g_value_set_boolean (value,
			   gtk_tree_view_column_get_reorderable (tree_column));
      break;

Havoc Pennington's avatar
Havoc Pennington committed
588 589 590 591 592 593 594 595 596 597
    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;
      
598 599 600 601 602 603
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
/* 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));

619
  g_object_ref_sink (cell);
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642

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

643
  g_object_ref_sink (cell);
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662

  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)
{
  GtkTreeViewColumn *column;

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

663
  while (column->cell_list)
664
    {
665
      GtkTreeViewColumnCellInfo *info = (GtkTreeViewColumnCellInfo *)column->cell_list->data;
666

667
      gtk_tree_view_column_cell_layout_clear_attributes (cell_layout, info->cell);
668
      g_object_unref (info->cell);
669
      g_free (info);
670 671
      column->cell_list = g_list_delete_link (column->cell_list, 
					      column->cell_list);
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
    }
}

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);
740 741
  if (info)
    gtk_tree_view_column_clear_attributes_by_info (column, info);
742 743
}

Kristian Rietveld's avatar
Kristian Rietveld committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
static void
gtk_tree_view_column_cell_layout_reorder (GtkCellLayout   *cell_layout,
                                          GtkCellRenderer *cell,
                                          gint             position)
{
  GList *link;
  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);
  g_return_if_fail (position >= 0);

  link = g_list_find (column->cell_list, info);

  g_return_if_fail (link != NULL);

765
  column->cell_list = g_list_delete_link (column->cell_list, link);
Kristian Rietveld's avatar
Kristian Rietveld committed
766 767
  column->cell_list = g_list_insert (column->cell_list, info, position);

768 769
  if (column->tree_view)
    gtk_widget_queue_draw (column->tree_view);
Kristian Rietveld's avatar
Kristian Rietveld committed
770 771
}

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
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);
}

792 793 794 795 796
/* Helper functions
 */

/* Button handling code
 */
797
static void
798
gtk_tree_view_column_create_button (GtkTreeViewColumn *tree_column)
799
{
800 801 802
  GtkTreeView *tree_view;
  GtkWidget *child;
  GtkWidget *hbox;
803

804
  tree_view = (GtkTreeView *) tree_column->tree_view;
805

806 807 808 809 810
  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
811
  gtk_widget_add_events (tree_column->button, GDK_POINTER_MOTION_MASK);
812 813 814
  gtk_widget_pop_composite_child ();

  /* make sure we own a reference to it as well. */
815 816
  if (tree_view->priv->header_window)
    gtk_widget_set_parent_window (tree_column->button, tree_view->priv->header_window);
817
  gtk_widget_set_parent (tree_column->button, GTK_WIDGET (tree_view));
818

Manish Singh's avatar
Manish Singh committed
819
  g_signal_connect (tree_column->button, "event",
820
		    G_CALLBACK (gtk_tree_view_column_button_event),
Manish Singh's avatar
Manish Singh committed
821 822 823 824
		    tree_column);
  g_signal_connect (tree_column->button, "clicked",
		    G_CALLBACK (gtk_tree_view_column_button_clicked),
		    tree_column);
825 826 827 828 829 830 831 832 833 834 835 836 837 838

  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
839
  g_signal_connect (child, "mnemonic_activate",
840
		    G_CALLBACK (gtk_tree_view_column_mnemonic_activate),
Manish Singh's avatar
Manish Singh committed
841
		    tree_column);
842

843 844 845 846 847 848 849 850 851 852 853 854 855
  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);
856 857
}

858 859 860
static void 
gtk_tree_view_column_update_button (GtkTreeViewColumn *tree_column)
{
861
  gint sort_column_id = -1;
862 863 864 865
  GtkWidget *hbox;
  GtkWidget *alignment;
  GtkWidget *arrow;
  GtkWidget *current_child;
Kristian Rietveld's avatar
Kristian Rietveld committed
866
  GtkArrowType arrow_type = GTK_ARROW_NONE;
867 868 869 870 871 872
  GtkTreeModel *model;

  if (tree_column->tree_view)
    model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_column->tree_view));
  else
    model = NULL;
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915

  /* 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)
916 917
	gtk_label_set_text_with_mnemonic (GTK_LABEL (current_child),
					  tree_column->title);
918
      else
919 920
	gtk_label_set_text_with_mnemonic (GTK_LABEL (current_child),
					  "");
921 922
    }

923 924 925 926 927
  if (GTK_IS_TREE_SORTABLE (model))
    gtk_tree_sortable_get_sort_column_id (GTK_TREE_SORTABLE (model),
					  &sort_column_id,
					  NULL);

928
  if (tree_column->show_sort_indicator)
929
    {
930 931 932 933 934 935
      gboolean alternative;

      g_object_get (gtk_widget_get_settings (tree_column->tree_view),
		    "gtk-alternative-sort-arrows", &alternative,
		    NULL);

936 937 938
      switch (tree_column->sort_order)
        {
	  case GTK_SORT_ASCENDING:
939
	    arrow_type = alternative ? GTK_ARROW_UP : GTK_ARROW_DOWN;
940 941 942
	    break;

	  case GTK_SORT_DESCENDING:
943
	    arrow_type = alternative ? GTK_ARROW_DOWN : GTK_ARROW_UP;
944 945 946 947 948
	    break;

	  default:
	    g_warning (G_STRLOC": bad sort order");
	    break;
Kristian Rietveld's avatar
Kristian Rietveld committed
949
	}
950 951
    }

Kristian Rietveld's avatar
Kristian Rietveld committed
952 953 954 955
  gtk_arrow_set (GTK_ARROW (arrow),
		 arrow_type,
		 GTK_SHADOW_IN);

956 957 958 959
  /* 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
960
  g_object_ref (arrow);
961 962 963 964 965 966 967 968 969 970 971 972
  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
973
  g_object_unref (arrow);
974

975 976
  if (tree_column->show_sort_indicator
      || (GTK_IS_TREE_SORTABLE (model) && tree_column->sort_column_id >= 0))
977 978 979 980
    gtk_widget_show (arrow);
  else
    gtk_widget_hide (arrow);

981 982
  /* 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. */
983 984 985 986 987 988
  if (tree_column->button &&
      tree_column->tree_view != NULL &&
      GTK_WIDGET_REALIZED (tree_column->tree_view))
    {
      if (tree_column->visible)
	{
989
	  gtk_widget_show_now (tree_column->button);
990 991
	  if (tree_column->window)
	    {
992
	      if (tree_column->resizable)
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
		{
		  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);
	}
    }
1010 1011
  
  if (tree_column->reorderable || tree_column->clickable)
1012 1013 1014
    {
      GTK_WIDGET_SET_FLAGS (tree_column->button, GTK_CAN_FOCUS);
    }
1015
  else
1016 1017 1018
    {
      GTK_WIDGET_UNSET_FLAGS (tree_column->button, GTK_CAN_FOCUS);
      if (GTK_WIDGET_HAS_FOCUS (tree_column->button))
1019 1020 1021
	{
	  GtkWidget *toplevel = gtk_widget_get_toplevel (tree_column->tree_view);
	  if (GTK_WIDGET_TOPLEVEL (toplevel))
1022 1023 1024
	    {
	      gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
	    }
1025
	}
1026
    }
1027 1028 1029 1030 1031 1032
  /* 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);

1033 1034 1035 1036
}

/* Button signal handlers
 */
1037 1038

static gint
1039 1040 1041
gtk_tree_view_column_button_event (GtkWidget *widget,
				   GdkEvent  *event,
				   gpointer   data)
1042
{
1043 1044
  GtkTreeViewColumn *column = (GtkTreeViewColumn *) data;

1045 1046
  g_return_val_if_fail (event != NULL, FALSE);

1047
  if (event->type == GDK_BUTTON_PRESS &&
1048 1049
      column->reorderable &&
      ((GdkEventButton *)event)->button == 1)
1050 1051
    {
      column->maybe_reordered = TRUE;
1052
      gdk_window_get_pointer (GTK_BUTTON (widget)->event_window,
1053 1054 1055 1056 1057 1058
			      &column->drag_x,
			      &column->drag_y,
			      NULL);
      gtk_widget_grab_focus (widget);
    }

1059 1060
  if (event->type == GDK_BUTTON_RELEASE ||
      event->type == GDK_LEAVE_NOTIFY)
1061
    column->maybe_reordered = FALSE;
1062
  
1063
  if (event->type == GDK_MOTION_NOTIFY &&
1064
      column->maybe_reordered &&
1065 1066 1067 1068 1069
      (gtk_drag_check_threshold (widget,
				 column->drag_x,
				 column->drag_y,
				 (gint) ((GdkEventMotion *)event)->x,
				 (gint) ((GdkEventMotion *)event)->y)))
1070
    {
1071 1072
      column->maybe_reordered = FALSE;
      _gtk_tree_view_column_start_drag (GTK_TREE_VIEW (column->tree_view), column);
1073
      return TRUE;
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
    }
  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;
	}
1090 1091 1092 1093 1094
    }
  return FALSE;
}


1095 1096 1097
static void
gtk_tree_view_column_button_clicked (GtkWidget *widget, gpointer data)
{
Manish Singh's avatar
Manish Singh committed
1098
  g_signal_emit_by_name (data, "clicked");
Jonathan Blandford's avatar
Jonathan Blandford committed
1099
}
1100

1101 1102 1103 1104 1105 1106 1107 1108 1109
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);

1110
  GTK_TREE_VIEW (column->tree_view)->priv->focus_column = column;
1111 1112 1113 1114 1115
  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
1116
    gtk_widget_grab_focus (column->tree_view);
1117 1118 1119 1120

  return TRUE;
}

Jonathan Blandford's avatar
Jonathan Blandford committed
1121
static void
1122
gtk_tree_view_model_sort_column_changed (GtkTreeSortable   *sortable,
Jonathan Blandford's avatar
Jonathan Blandford committed
1123
					 GtkTreeViewColumn *column)
Jonathan Blandford's avatar
Jonathan Blandford committed
1124
{
1125
  gint sort_column_id;
1126
  GtkSortType order;
Jonathan Blandford's avatar
Jonathan Blandford committed
1127

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
  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
1142 1143 1144 1145
  else
    {
      gtk_tree_view_column_set_sort_indicator (column, FALSE);
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
1146 1147
}

1148 1149 1150
static void
gtk_tree_view_column_sort (GtkTreeViewColumn *tree_column,
			   gpointer           data)
1151
{
Jonathan Blandford's avatar
Jonathan Blandford committed
1152 1153 1154 1155
  gint sort_column_id;
  GtkSortType order;
  gboolean has_sort_column;
  gboolean has_default_sort_func;
1156

1157
  g_return_if_fail (tree_column->tree_view != NULL);
1158

Jonathan Blandford's avatar
Jonathan Blandford committed
1159 1160 1161 1162 1163 1164 1165 1166 1167
  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)
1168
    {
Jonathan Blandford's avatar
Jonathan Blandford committed
1169 1170 1171 1172 1173 1174 1175 1176
      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);
1177
      else
Jonathan Blandford's avatar
Jonathan Blandford committed
1178 1179 1180
	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);
1181 1182 1183
    }
  else
    {
Jonathan Blandford's avatar
Jonathan Blandford committed
1184 1185 1186
      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);
1187 1188
    }
}
1189

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201

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
1202
    return;
1203

1204 1205 1206 1207
  if (GTK_IS_TREE_SORTABLE (model) &&
      tree_column->sort_column_id != -1)
    {
      gint real_sort_column_id;
1208
      GtkSortType real_order;
1209 1210

      if (tree_column->sort_column_changed_signal == 0)
1211
        tree_column->sort_column_changed_signal =
Manish Singh's avatar
Manish Singh committed
1212 1213 1214
	  g_signal_connect (model, "sort_column_changed",
			    G_CALLBACK (gtk_tree_view_model_sort_column_changed),
			    tree_column);
1215
      
1216 1217 1218 1219 1220 1221 1222
      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);
1223 1224 1225 1226
 	}
      else 
	{
	  gtk_tree_view_column_set_sort_indicator (tree_column, FALSE);
1227
	}
1228
   }
1229
}
1230 1231


1232 1233 1234
/* Exported Private Functions.
 * These should only be called by gtktreeview.c or gtktreeviewcolumn.c
 */
1235 1236 1237 1238 1239 1240 1241

void
_gtk_tree_view_column_realize_button (GtkTreeViewColumn *column)
{
  GtkTreeView *tree_view;
  GdkWindowAttr attr;
  guint attributes_mask;
1242
  gboolean rtl;
1243 1244

  tree_view = (GtkTreeView *)column->tree_view;
1245
  rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
1246 1247 1248 1249 1250 1251

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

1252 1253 1254 1255 1256
  gtk_widget_set_parent_window (column->button, tree_view->priv->header_window);

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

1257 1258 1259 1260
  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
1261 1262
  attr.event_mask = gtk_widget_get_events (GTK_WIDGET (tree_view)) |
                    (GDK_BUTTON_PRESS_MASK |
1263 1264 1265 1266 1267
		     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;
1268 1269
  attr.cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (tree_view->priv->header_window),
					    GDK_SB_H_DOUBLE_ARROW);
1270 1271 1272 1273
  attr.y = 0;
  attr.width = TREE_VIEW_DRAG_WIDTH;
  attr.height = tree_view->priv->header_height;

1274
  attr.x = (column->button->allocation.x + (rtl ? 0 : column->button->allocation.width)) - TREE_VIEW_DRAG_WIDTH / 2;
1275 1276 1277
  column->window = gdk_window_new (tree_view->priv->header_window,
				   &attr, attributes_mask);
  gdk_window_set_user_data (column->window, tree_view);
1278 1279

  gtk_tree_view_column_update_button (column);
1280 1281

  gdk_cursor_unref (attr