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


#include "gtktreeview.h"
#include "gtkrbtree.h"
Havoc Pennington's avatar
Havoc Pennington committed
23
#include "gtktreednd.h"
24 25 26 27 28 29 30
#include "gtktreeprivate.h"
#include "gtkcellrenderer.h"
#include "gtksignal.h"
#include "gtkmain.h"
#include "gtkbutton.h"
#include "gtkalignment.h"
#include "gtklabel.h"
Havoc Pennington's avatar
Havoc Pennington committed
31 32
#include "gtkhbox.h"
#include "gtkarrow.h"
33
#include "gtkintl.h"
34
#include "gtkbindings.h"
35 36 37

#include <gdk/gdkkeysyms.h>

Jonathan Blandford's avatar
Jonathan Blandford committed
38 39 40 41 42 43 44 45 46 47
#if defined (GDK_WINDOWING_X11)
#include "x11/gdkx.h"
#elif defined (GDK_WINDOWING_WIN32)
#include "win32/gdkwin32.h"
#elif defined(GDK_WINDOWING_FB)
#include "linux-fb/gdkfb.h"
#elif defined (GDK_WINDOWING_NANOX)
#include "nanox/gdkprivate-nanox.h"
#endif

48

49
#define SCROLL_EDGE_SIZE 15
50 51


Havoc Pennington's avatar
Havoc Pennington committed
52 53 54 55 56 57
/* The "background" areas of all rows/cells add up to cover the entire tree.
 * The background includes all inter-row and inter-cell spacing.
 * The "cell" areas are the cell_area passed in to gtk_cell_renderer_render(),
 * i.e. just the cells, no spacing.
 */
#define BACKGROUND_FIRST_PIXEL(tree_view,tree,node) (_gtk_rbtree_node_find_offset ((tree), (node)) + TREE_VIEW_HEADER_HEIGHT ((tree_view)))
58
#define CELL_FIRST_PIXEL(tree_view,tree,node,separator) (BACKGROUND_FIRST_PIXEL (tree_view,tree,node) + separator/2)
Havoc Pennington's avatar
Havoc Pennington committed
59 60

#define BACKGROUND_HEIGHT(node) (GTK_RBNODE_GET_HEIGHT (node))
61
#define CELL_HEIGHT(node, separator) (BACKGROUND_HEIGHT (node) - separator);
Havoc Pennington's avatar
Havoc Pennington committed
62 63 64

#define TREE_WINDOW_Y_TO_RBTREE_Y(tree_view,y) ((y) - TREE_VIEW_HEADER_HEIGHT (tree_view))
#define RBTREE_Y_TO_TREE_WINDOW_Y(tree_view,y) ((y) + TREE_VIEW_HEADER_HEIGHT (tree_view))
65 66


67
typedef struct _GtkTreeViewChild GtkTreeViewChild;
68 69 70 71 72 73 74
struct _GtkTreeViewChild
{
  GtkWidget *widget;
  gint x;
  gint y;
};

Jonathan Blandford's avatar
Jonathan Blandford committed
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
typedef struct _TreeViewDragInfo TreeViewDragInfo;
struct _TreeViewDragInfo
{
  GdkModifierType start_button_mask;
  GtkTargetList *source_target_list;
  GdkDragAction source_actions;
  GClosure *row_draggable_closure;

  GtkTargetList *dest_target_list;
  GClosure *location_droppable_closure;

  guint source_set : 1;
  guint dest_set : 1;
};


/* Signals */
93 94 95
enum
{
  ROW_ACTIVATED,
96 97 98 99
  TEST_EXPAND_ROW,
  TEST_COLLAPSE_ROW,
  ROW_EXPANDED,
  ROW_COLLAPSED,
100
  COLUMNS_CHANGED,
101 102 103 104
  BEGIN_EXTENDED_SELECTION,
  END_EXTENDED_SELECTION,
  BEGIN_FREE_MOTION,
  END_FREE_MOTION,
105
  MOVE_CURSOR,
106
  SELECT_CURSOR_ROW,
107 108 109
  TOGGLE_CURSOR_ROW,
  EXPAND_COLLAPSE_CURSOR_ROW,
  SELECT_CURSOR_PARENT,
110 111 112
  LAST_SIGNAL
};

113
/* Properties */
114 115 116 117 118 119 120 121
enum {
  PROP_0,
  PROP_MODEL,
  PROP_HADJUSTMENT,
  PROP_VADJUSTMENT,
  PROP_HEADERS_VISIBLE,
  PROP_HEADERS_CLICKABLE,
  PROP_EXPANDER_COLUMN,
122
  PROP_REORDERABLE,
123 124
  PROP_RULES_HINT
};
125 126

static void     gtk_tree_view_class_init           (GtkTreeViewClass *klass);
127 128 129 130
static void     gtk_tree_view_init                 (GtkTreeView      *tree_view);

/* object signals */
static void     gtk_tree_view_finalize             (GObject          *object);
131 132 133
static void     gtk_tree_view_set_property         (GObject         *object,
						    guint            prop_id,
						    const GValue    *value,
Jonathan Blandford's avatar
Jonathan Blandford committed
134
						    GParamSpec      *pspec);
135 136 137
static void     gtk_tree_view_get_property         (GObject         *object,
						    guint            prop_id,
						    GValue          *value,
Jonathan Blandford's avatar
Jonathan Blandford committed
138
						    GParamSpec      *pspec);
139

140
/* gtkobject signals */
Jonathan Blandford's avatar
Jonathan Blandford committed
141 142
static void     gtk_tree_view_destroy              (GtkObject        *object);

143
/* gtkwidget signals */
144
static void     gtk_tree_view_setup_model          (GtkTreeView      *tree_view);
145 146 147 148 149 150 151 152 153
static void     gtk_tree_view_realize              (GtkWidget        *widget);
static void     gtk_tree_view_unrealize            (GtkWidget        *widget);
static void     gtk_tree_view_map                  (GtkWidget        *widget);
static void     gtk_tree_view_size_request         (GtkWidget        *widget,
						    GtkRequisition   *requisition);
static void     gtk_tree_view_size_allocate        (GtkWidget        *widget,
						    GtkAllocation    *allocation);
static gboolean gtk_tree_view_expose               (GtkWidget        *widget,
						    GdkEventExpose   *event);
154 155
static gboolean gtk_tree_view_key_press            (GtkWidget        *widget,
						    GdkEventKey      *event);
156 157 158 159 160 161 162 163 164 165
static gboolean gtk_tree_view_motion               (GtkWidget        *widget,
						    GdkEventMotion   *event);
static gboolean gtk_tree_view_enter_notify         (GtkWidget        *widget,
						    GdkEventCrossing *event);
static gboolean gtk_tree_view_leave_notify         (GtkWidget        *widget,
						    GdkEventCrossing *event);
static gboolean gtk_tree_view_button_press         (GtkWidget        *widget,
						    GdkEventButton   *event);
static gboolean gtk_tree_view_button_release       (GtkWidget        *widget,
						    GdkEventButton   *event);
166 167
static void     gtk_tree_view_set_focus_child      (GtkContainer     *container,
						    GtkWidget        *child);
168 169 170 171 172
static void     gtk_tree_view_draw_focus           (GtkWidget        *widget);
static gint     gtk_tree_view_focus_in             (GtkWidget        *widget,
						    GdkEventFocus    *event);
static gint     gtk_tree_view_focus_out            (GtkWidget        *widget,
						    GdkEventFocus    *event);
173
static gint     gtk_tree_view_focus                (GtkWidget        *widget,
174 175 176 177 178 179 180 181 182 183
						    GtkDirectionType  direction);

/* container signals */
static void     gtk_tree_view_remove               (GtkContainer     *container,
						    GtkWidget        *widget);
static void     gtk_tree_view_forall               (GtkContainer     *container,
						    gboolean          include_internals,
						    GtkCallback       callback,
						    gpointer          callback_data);

Havoc Pennington's avatar
Havoc Pennington committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/* Source side drag signals */
static void gtk_tree_view_drag_begin       (GtkWidget        *widget,
                                            GdkDragContext   *context);
static void gtk_tree_view_drag_end         (GtkWidget        *widget,
                                            GdkDragContext   *context);
static void gtk_tree_view_drag_data_get    (GtkWidget        *widget,
                                            GdkDragContext   *context,
                                            GtkSelectionData *selection_data,
                                            guint             info,
                                            guint             time);
static void gtk_tree_view_drag_data_delete (GtkWidget        *widget,
                                            GdkDragContext   *context);

/* Target side drag signals */
static void     gtk_tree_view_drag_leave         (GtkWidget        *widget,
                                                  GdkDragContext   *context,
                                                  guint             time);
static gboolean gtk_tree_view_drag_motion        (GtkWidget        *widget,
                                                  GdkDragContext   *context,
                                                  gint              x,
                                                  gint              y,
                                                  guint             time);
static gboolean gtk_tree_view_drag_drop          (GtkWidget        *widget,
                                                  GdkDragContext   *context,
                                                  gint              x,
                                                  gint              y,
                                                  guint             time);
static void     gtk_tree_view_drag_data_received (GtkWidget        *widget,
                                                  GdkDragContext   *context,
                                                  gint              x,
                                                  gint              y,
                                                  GtkSelectionData *selection_data,
                                                  guint             info,
                                                  guint             time);

219
/* tree_model signals */
220 221 222
static void gtk_tree_view_set_adjustments                 (GtkTreeView     *tree_view,
							   GtkAdjustment   *hadj,
							   GtkAdjustment   *vadj);
223 224 225 226
static void gtk_tree_view_real_begin_extended_selection   (GtkTreeView     *tree_view);
static void gtk_tree_view_real_end_extended_selection     (GtkTreeView     *tree_view);
static void gtk_tree_view_real_begin_free_motion          (GtkTreeView     *tree_view);
static void gtk_tree_view_real_end_free_motion            (GtkTreeView     *tree_view);
227 228
static void gtk_tree_view_real_move_cursor                (GtkTreeView     *tree_view,
							   GtkMovementStep  step,
229
							   gint             count);
230
static void gtk_tree_view_real_select_cursor_row          (GtkTreeView     *tree_view);
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
static void gtk_tree_view_real_toggle_cursor_row          (GtkTreeView     *tree_view);
static void gtk_tree_view_real_expand_collapse_cursor_row (GtkTreeView     *tree_view,
							   gboolean         logical,
							   gboolean         expand,
							   gboolean         open_all);
static void gtk_tree_view_real_select_cursor_parent       (GtkTreeView     *tree_view);
static void gtk_tree_view_range_changed                   (GtkTreeModel    *model,
							   GtkTreePath     *path,
							   GtkTreeIter     *iter,
							   GtkTreePath     *end_path,
							   GtkTreeIter     *end_iter,
							   gpointer         data);
static void gtk_tree_view_inserted                        (GtkTreeModel    *model,
							   GtkTreePath     *path,
							   GtkTreeIter     *iter,
							   gpointer         data);
static void gtk_tree_view_has_child_toggled               (GtkTreeModel    *model,
							   GtkTreePath     *path,
							   GtkTreeIter     *iter,
							   gpointer         data);
static void gtk_tree_view_deleted                         (GtkTreeModel    *model,
							   GtkTreePath     *path,
							   gpointer         data);
static void gtk_tree_view_reordered                       (GtkTreeModel    *model,
							   GtkTreePath     *parent,
							   GtkTreeIter     *iter,
							   gint            *new_order,
							   gpointer         data);



262
/* Internal functions */
263 264
static gboolean gtk_tree_view_is_expander_column   (GtkTreeView      *tree_view,
						    GtkTreeViewColumn*column);
265 266 267 268 269
static void     gtk_tree_view_add_move_binding     (GtkBindingSet    *binding_set,
						    guint             keyval,
						    guint             modmask,
						    GtkMovementStep   step,
						    gint              count);
270 271
static gint     gtk_tree_view_unref_and_check_selection_tree (GtkTreeView      *tree_view,
							      GtkRBTree        *tree);
272 273 274 275
static void     gtk_tree_view_queue_draw_node      (GtkTreeView      *tree_view,
						    GtkRBTree        *tree,
						    GtkRBNode        *node,
						    GdkRectangle     *clip_rect);
Havoc Pennington's avatar
Havoc Pennington committed
276 277 278
static void     gtk_tree_view_queue_draw_path      (GtkTreeView      *tree_view,
                                                    GtkTreePath      *path,
                                                    GdkRectangle     *clip_rect);
279
static void     gtk_tree_view_draw_arrow           (GtkTreeView      *tree_view,
Havoc Pennington's avatar
Havoc Pennington committed
280
                                                    GtkRBTree        *tree,
281 282 283
						    GtkRBNode        *node,
						    gint              x,
						    gint              y);
Havoc Pennington's avatar
Havoc Pennington committed
284 285
static void     gtk_tree_view_get_arrow_xrange     (GtkTreeView      *tree_view,
                                                    gint              *x1,
Havoc Pennington's avatar
Havoc Pennington committed
286
                                                    gint              *x2);
287 288 289
static gint     gtk_tree_view_new_column_width     (GtkTreeView      *tree_view,
						    gint              i,
						    gint             *x);
290 291
static void     gtk_tree_view_ensure_scroll_timeout(GtkTreeView      *tree_view,
						    GFunc             func);
292 293
static void     gtk_tree_view_adjustment_changed   (GtkAdjustment    *adjustment,
						    GtkTreeView      *tree_view);
294
static gint     gtk_tree_view_insert_iter_height   (GtkTreeView      *tree_view,
295
						    GtkRBTree        *tree,
296
						    GtkTreeIter      *iter,
297 298 299
						    gint              depth);
static void     gtk_tree_view_build_tree           (GtkTreeView      *tree_view,
						    GtkRBTree        *tree,
300
						    GtkTreeIter      *iter,
301 302 303 304 305
						    gint              depth,
						    gboolean          recurse,
						    gboolean          calc_bounds);
static void     gtk_tree_view_calc_size            (GtkTreeView      *priv,
						    GtkRBTree        *tree,
306
						    GtkTreeIter      *iter,
307
						    gint              depth);
308 309
static gboolean gtk_tree_view_discover_dirty_iter  (GtkTreeView      *tree_view,
						    GtkTreeIter      *iter,
310 311 312 313
						    gint              depth,
						    gint             *height);
static void     gtk_tree_view_discover_dirty       (GtkTreeView      *tree_view,
						    GtkRBTree        *tree,
314
						    GtkTreeIter      *iter,
315 316 317 318 319
						    gint              depth);
static void     gtk_tree_view_check_dirty          (GtkTreeView      *tree_view);
static void     gtk_tree_view_clamp_node_visible   (GtkTreeView      *tree_view,
						    GtkRBTree        *tree,
						    GtkRBNode        *node);
Havoc Pennington's avatar
Havoc Pennington committed
320 321
static gboolean gtk_tree_view_maybe_begin_dragging_row (GtkTreeView      *tree_view,
                                                        GdkEventMotion   *event);
322
static void     _gtk_tree_view_update_col_width    (GtkTreeView      *tree_view);
323 324


325
static void gtk_tree_view_focus_to_cursor          (GtkTreeView *tree_view);
326
static void gtk_tree_view_move_cursor_up_down      (GtkTreeView *tree_view,
327
						    gint         count);
328
static void gtk_tree_view_move_cursor_page_up_down (GtkTreeView *tree_view,
329
						    gint         count);
330 331 332
static void gtk_tree_view_move_cursor_left_right   (GtkTreeView *tree_view,
						    gint         count);
static void gtk_tree_view_move_cursor_start_end    (GtkTreeView *tree_view,
333
						    gint         count);
334 335 336 337 338 339 340 341 342
static gboolean gtk_tree_view_real_collapse_row (GtkTreeView *tree_view,
						 GtkTreePath *path,
						 GtkRBTree   *tree,
						 GtkRBNode   *node);
static gboolean gtk_tree_view_real_expand_row (GtkTreeView *tree_view,
					       GtkTreePath *path,
					       GtkRBTree   *tree,
					       GtkRBNode   *node,
					       gboolean     open_all);
343 344
static void gtk_tree_view_real_set_cursor (GtkTreeView     *tree_view,
					   GtkTreePath     *path,
345
					   gboolean         clear_and_select);
346

347 348

static GtkContainerClass *parent_class = NULL;
349
static guint tree_view_signals[LAST_SIGNAL] = { 0 };
350

351 352 353 354 355


/* GType Methods
 */

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
GtkType
gtk_tree_view_get_type (void)
{
  static GtkType tree_view_type = 0;

  if (!tree_view_type)
    {
      static const GTypeInfo tree_view_info =
      {
        sizeof (GtkTreeViewClass),
	NULL,		/* base_init */
	NULL,		/* base_finalize */
        (GClassInitFunc) gtk_tree_view_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
        sizeof (GtkTreeView),
	0,              /* n_preallocs */
        (GInstanceInitFunc) gtk_tree_view_init
      };

376
      tree_view_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkTreeView", &tree_view_info, 0);
377 378 379 380 381 382 383 384
    }

  return tree_view_type;
}

static void
gtk_tree_view_class_init (GtkTreeViewClass *class)
{
385
  GObjectClass *o_class;
386 387 388
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;
389
  GtkBindingSet *binding_set;
390

391
  parent_class = g_type_class_peek_parent (class);
392
  binding_set = gtk_binding_set_by_class (class);
393

394 395 396 397 398
  o_class = (GObjectClass *) class;
  object_class = (GtkObjectClass *) class;
  widget_class = (GtkWidgetClass *) class;
  container_class = (GtkContainerClass *) class;

399
  /* GObject signals */
400 401
  o_class->set_property = gtk_tree_view_set_property;
  o_class->get_property = gtk_tree_view_get_property;
402
  o_class->finalize = gtk_tree_view_finalize;
403

404
  /* GtkObject signals */
Jonathan Blandford's avatar
Jonathan Blandford committed
405
  object_class->destroy = gtk_tree_view_destroy;
406

407 408
  /* GtkWidget signals */
  widget_class->map = gtk_tree_view_map;
409 410 411 412
  widget_class->realize = gtk_tree_view_realize;
  widget_class->unrealize = gtk_tree_view_unrealize;
  widget_class->size_request = gtk_tree_view_size_request;
  widget_class->size_allocate = gtk_tree_view_size_allocate;
413 414
  widget_class->button_press_event = gtk_tree_view_button_press;
  widget_class->button_release_event = gtk_tree_view_button_release;
415
  widget_class->motion_notify_event = gtk_tree_view_motion;
416
  widget_class->expose_event = gtk_tree_view_expose;
417
  widget_class->key_press_event = gtk_tree_view_key_press;
418 419 420 421
  widget_class->enter_notify_event = gtk_tree_view_enter_notify;
  widget_class->leave_notify_event = gtk_tree_view_leave_notify;
  widget_class->focus_in_event = gtk_tree_view_focus_in;
  widget_class->focus_out_event = gtk_tree_view_focus_out;
Havoc Pennington's avatar
Havoc Pennington committed
422 423 424 425 426 427 428 429
  widget_class->drag_begin = gtk_tree_view_drag_begin;
  widget_class->drag_end = gtk_tree_view_drag_end;
  widget_class->drag_data_get = gtk_tree_view_drag_data_get;
  widget_class->drag_data_delete = gtk_tree_view_drag_data_delete;
  widget_class->drag_leave = gtk_tree_view_drag_leave;
  widget_class->drag_motion = gtk_tree_view_drag_motion;
  widget_class->drag_drop = gtk_tree_view_drag_drop;
  widget_class->drag_data_received = gtk_tree_view_drag_data_received;
430
  widget_class->focus = gtk_tree_view_focus;
431

432
  /* GtkContainer signals */
433
  container_class->remove = gtk_tree_view_remove;
434 435
  container_class->forall = gtk_tree_view_forall;
  container_class->set_focus_child = gtk_tree_view_set_focus_child;
436 437

  class->set_scroll_adjustments = gtk_tree_view_set_adjustments;
438 439 440 441
  class->begin_extended_selection = gtk_tree_view_real_begin_extended_selection;
  class->end_extended_selection = gtk_tree_view_real_end_extended_selection;
  class->begin_free_motion = gtk_tree_view_real_begin_free_motion;
  class->end_free_motion = gtk_tree_view_real_end_free_motion;
442
  class->move_cursor = gtk_tree_view_real_move_cursor;
443
  class->select_cursor_row = gtk_tree_view_real_select_cursor_row;
444 445 446 447
  class->toggle_cursor_row = gtk_tree_view_real_toggle_cursor_row;
  class->expand_collapse_cursor_row = gtk_tree_view_real_expand_collapse_cursor_row;
  class->select_cursor_parent = gtk_tree_view_real_select_cursor_parent;

448
  /* Properties */
449

450 451
  g_object_class_install_property (o_class,
                                   PROP_MODEL,
Tim Janik's avatar
Tim Janik committed
452 453 454 455 456
                                   g_param_spec_object ("model",
							_("TreeView Model"),
							_("The model for the tree view"),
							GTK_TYPE_TREE_MODEL,
							G_PARAM_READWRITE));
Jonathan Blandford's avatar
Jonathan Blandford committed
457

458 459 460 461
  g_object_class_install_property (o_class,
                                   PROP_HADJUSTMENT,
                                   g_param_spec_object ("hadjustment",
							_("Horizontal Adjustment"),
Jonathan Blandford's avatar
Jonathan Blandford committed
462
                                                        _("Horizontal Adjustment for the widget"),
463 464 465 466 467 468 469
                                                        GTK_TYPE_ADJUSTMENT,
                                                        G_PARAM_READWRITE));

  g_object_class_install_property (o_class,
                                   PROP_VADJUSTMENT,
                                   g_param_spec_object ("vadjustment",
							_("Vertical Adjustment"),
Jonathan Blandford's avatar
Jonathan Blandford committed
470
                                                        _("Vertical Adjustment for the widget"),
471 472 473 474 475 476 477
                                                        GTK_TYPE_ADJUSTMENT,
                                                        G_PARAM_READWRITE));

  g_object_class_install_property (o_class,
                                   PROP_HEADERS_VISIBLE,
                                   g_param_spec_boolean ("headers_visible",
							 _("Visible"),
Jonathan Blandford's avatar
Jonathan Blandford committed
478
							 _("Show the column header buttons"),
479 480 481 482 483 484 485
							 FALSE,
							 G_PARAM_READWRITE));

  g_object_class_install_property (o_class,
                                   PROP_HEADERS_CLICKABLE,
                                   g_param_spec_boolean ("headers_clickable",
							 _("Headers Clickable"),
Jonathan Blandford's avatar
Jonathan Blandford committed
486
							 _("Column headers respond to click events"),
487
							 FALSE,
488
							 G_PARAM_WRITABLE));
489 490 491

  g_object_class_install_property (o_class,
                                   PROP_EXPANDER_COLUMN,
492 493 494 495 496
                                   g_param_spec_object ("expander_column",
							_("Expander Column"),
							_("Set the column for the expander column"),
							GTK_TYPE_TREE_VIEW_COLUMN,
							G_PARAM_READWRITE));
497

498 499 500 501 502 503 504 505
  g_object_class_install_property (o_class,
                                   PROP_REORDERABLE,
                                   g_param_spec_boolean ("reorderable",
							 _("Reorderable"),
							 _("View is reorderable"),
							 FALSE,
							 G_PARAM_READWRITE));

506 507 508 509
  g_object_class_install_property (o_class,
                                   PROP_RULES_HINT,
                                   g_param_spec_boolean ("rules_hint",
							 _("Rules Hint"),
Jonathan Blandford's avatar
Jonathan Blandford committed
510
							 _("Set a hint to the theme engine to draw rows in alternating colors"),
511 512 513
							 FALSE,
							 G_PARAM_READWRITE));

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
  /* Style properties */
  /* the width of the column resize windows */
#define _TREE_VIEW_EXPANDER_WIDTH 14
#define _TREE_VIEW_EXPANDER_HEIGHT 14
#define _TREE_VIEW_VERTICAL_SEPARATOR 2
#define _TREE_VIEW_HORIZONTAL_SEPARATOR 0

  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("expander_width",
							     _("Expander Width"),
							     _("Width of the expander arrow"),
							     0,
							     G_MAXINT,
							     _TREE_VIEW_EXPANDER_WIDTH,
							     G_PARAM_READABLE));

  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("expander_height",
							     _("Expander Height"),
							     _("Height of the expander arrow"),
							     4,
							     G_MAXINT,
							     _TREE_VIEW_EXPANDER_HEIGHT,
							     G_PARAM_READABLE));

    gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("vertical_separator",
							     _("Vertical Separator Width"),
							     _("Vertical space between cells"),
							     0,
							     G_MAXINT,
							     _TREE_VIEW_VERTICAL_SEPARATOR,
							     G_PARAM_READABLE));

    gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("horizontal_separator",
							     _("Horizontal Separator Width"),
							     _("Horizontal space between cells"),
							     0,
							     G_MAXINT,
							     _TREE_VIEW_HORIZONTAL_SEPARATOR,
							     G_PARAM_READABLE));

  /* Signals */
558 559 560 561 562
  widget_class->set_scroll_adjustments_signal =
    gtk_signal_new ("set_scroll_adjustments",
		    GTK_RUN_LAST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkTreeViewClass, set_scroll_adjustments),
Tim Janik's avatar
Tim Janik committed
563
		    gtk_marshal_VOID__OBJECT_OBJECT,
564
		    GTK_TYPE_NONE, 2,
565
		    GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
566

567 568 569 570 571 572 573 574 575
  tree_view_signals[ROW_ACTIVATED] =
    gtk_signal_new ("row_activated",
		    GTK_RUN_LAST | GTK_RUN_ACTION,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkTreeViewClass, row_activated),
		    gtk_marshal_VOID__BOXED_OBJECT,
		    GTK_TYPE_NONE, 2,
		    GTK_TYPE_TREE_PATH,
		    GTK_TYPE_TREE_VIEW_COLUMN);
576

577
  tree_view_signals[TEST_EXPAND_ROW] =
578 579 580 581 582 583 584 585 586
    g_signal_new ("test_expand_row",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTreeViewClass, test_expand_row),
                  _gtk_boolean_handled_accumulator, NULL,
                  gtk_marshal_BOOLEAN__BOXED_BOXED,
                  G_TYPE_BOOLEAN, 2,
                  GTK_TYPE_TREE_ITER,
                  GTK_TYPE_TREE_PATH);
587

588
  tree_view_signals[TEST_COLLAPSE_ROW] =
589 590 591 592 593 594 595 596 597
    g_signal_new ("test_collapse_row",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTreeViewClass, test_collapse_row),
                  _gtk_boolean_handled_accumulator, NULL,
                  gtk_marshal_BOOLEAN__BOXED_BOXED,
                  G_TYPE_BOOLEAN, 2,
                  GTK_TYPE_TREE_ITER,
                  GTK_TYPE_TREE_PATH);
598

599
  tree_view_signals[ROW_EXPANDED] =
600 601 602 603 604 605 606 607 608
    g_signal_new ("row_expanded",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTreeViewClass, row_expanded),
                  NULL, NULL,
                  gtk_marshal_VOID__BOXED_BOXED,
                  GTK_TYPE_NONE, 2,
                  GTK_TYPE_TREE_ITER,
                  GTK_TYPE_TREE_PATH);
609 610

  tree_view_signals[ROW_COLLAPSED] =
611 612 613 614 615 616 617 618 619
    g_signal_new ("row_collapsed",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTreeViewClass, row_collapsed),
                  NULL, NULL,
                  gtk_marshal_VOID__BOXED_BOXED,
                  GTK_TYPE_NONE, 2,
                  GTK_TYPE_TREE_ITER,
                  GTK_TYPE_TREE_PATH);
620

621
  tree_view_signals[COLUMNS_CHANGED] =
622 623 624 625 626 627 628
    g_signal_new ("columns_changed",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTreeViewClass, columns_changed),
                  NULL, NULL,
                  gtk_marshal_NONE__NONE,
                  G_TYPE_NONE, 0);
629

630
  tree_view_signals[BEGIN_EXTENDED_SELECTION] =
631 632 633 634 635 636 637
    g_signal_new ("begin_extended_selection",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST | GTK_RUN_ACTION,
                  G_STRUCT_OFFSET (GtkTreeViewClass, begin_extended_selection),
                  NULL, NULL,
                  gtk_marshal_NONE__NONE,
                  G_TYPE_NONE, 0);
638 639

  tree_view_signals[END_EXTENDED_SELECTION] =
640 641 642 643 644 645 646
    g_signal_new ("end_extended_selection",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST | GTK_RUN_ACTION,
                  G_STRUCT_OFFSET (GtkTreeViewClass, end_extended_selection),
                  NULL, NULL,
                  gtk_marshal_NONE__NONE,
                  G_TYPE_NONE, 0);
647 648

  tree_view_signals[BEGIN_FREE_MOTION] =
649 650 651 652 653 654 655
    g_signal_new ("begin_free_motion",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST | GTK_RUN_ACTION,
                  G_STRUCT_OFFSET (GtkTreeViewClass, begin_free_motion),
                  NULL, NULL,
                  gtk_marshal_NONE__NONE,
                  G_TYPE_NONE, 0);
656 657

  tree_view_signals[END_FREE_MOTION] =
658 659 660 661 662 663 664
    g_signal_new ("end_free_motion",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST | GTK_RUN_ACTION,
                  G_STRUCT_OFFSET (GtkTreeViewClass, end_free_motion),
                  NULL, NULL,
                  gtk_marshal_NONE__NONE,
                  G_TYPE_NONE, 0);
665

666
  tree_view_signals[MOVE_CURSOR] =
667 668 669 670 671 672 673
    g_signal_new ("move_cursor",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST | GTK_RUN_ACTION,
                  G_STRUCT_OFFSET (GtkTreeViewClass, move_cursor),
                  NULL, NULL,
                  gtk_marshal_VOID__ENUM_INT,
                  GTK_TYPE_NONE, 2, GTK_TYPE_MOVEMENT_STEP, GTK_TYPE_INT);
674

675
  tree_view_signals[SELECT_CURSOR_ROW] =
676 677 678 679 680 681 682
    g_signal_new ("select_cursor_row",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST | GTK_RUN_ACTION,
                  G_STRUCT_OFFSET (GtkTreeViewClass, select_cursor_row),
                  NULL, NULL,
                  gtk_marshal_NONE__NONE,
                  GTK_TYPE_NONE, 0);
683

684
  tree_view_signals[TOGGLE_CURSOR_ROW] =
685 686 687 688 689 690 691
    g_signal_new ("toggle_cursor_row",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST | GTK_RUN_ACTION,
                  G_STRUCT_OFFSET (GtkTreeViewClass, toggle_cursor_row),
                  NULL, NULL,
                  gtk_marshal_NONE__NONE,
                  GTK_TYPE_NONE, 0);
692

693
  tree_view_signals[EXPAND_COLLAPSE_CURSOR_ROW] =
694 695 696 697 698 699 700
    g_signal_new ("expand_collapse_cursor_row",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST | GTK_RUN_ACTION,
                  G_STRUCT_OFFSET (GtkTreeViewClass, expand_collapse_cursor_row),
                  NULL, NULL,
                  gtk_marshal_VOID__BOOLEAN_BOOLEAN_BOOLEAN,
                  GTK_TYPE_NONE, 3, GTK_TYPE_BOOL, GTK_TYPE_BOOL, GTK_TYPE_BOOL);
701

702
  tree_view_signals[SELECT_CURSOR_PARENT] =
703 704 705 706 707 708 709
    g_signal_new ("select_cursor_parent",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST | GTK_RUN_ACTION,
                  G_STRUCT_OFFSET (GtkTreeViewClass, select_cursor_parent),
                  NULL, NULL,
                  gtk_marshal_NONE__NONE,
                  GTK_TYPE_NONE, 0);
710

711
  /* Key bindings */
712 713 714 715 716 717 718 719 720
  gtk_binding_entry_add_signal (binding_set, GDK_Shift_L, 0, "begin_extended_selection", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_Shift_L, GDK_SHIFT_MASK | GDK_RELEASE_MASK, "end_extended_selection", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_Control_L, 0, "begin_free_motion", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_Control_L, GDK_CONTROL_MASK | GDK_RELEASE_MASK, "end_free_motion", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_Shift_R, 0, "begin_extended_selection", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_Shift_R, GDK_SHIFT_MASK | GDK_RELEASE_MASK, "end_extended_selection", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_Control_R, 0, "begin_free_motion", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_Control_R, GDK_CONTROL_MASK | GDK_RELEASE_MASK, "end_free_motion", 0);

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
  gtk_tree_view_add_move_binding (binding_set, GDK_Up, 0,
				  GTK_MOVEMENT_DISPLAY_LINES, -1);

  gtk_tree_view_add_move_binding (binding_set, GDK_Down, 0,
				  GTK_MOVEMENT_DISPLAY_LINES, 1);

  gtk_tree_view_add_move_binding (binding_set, GDK_p, GDK_CONTROL_MASK,
				  GTK_MOVEMENT_DISPLAY_LINES, -1);

  gtk_tree_view_add_move_binding (binding_set, GDK_n, GDK_CONTROL_MASK,
				  GTK_MOVEMENT_DISPLAY_LINES, 1);

  gtk_tree_view_add_move_binding (binding_set, GDK_Home, 0,
				  GTK_MOVEMENT_BUFFER_ENDS, -1);

  gtk_tree_view_add_move_binding (binding_set, GDK_End, 0,
				  GTK_MOVEMENT_BUFFER_ENDS, 1);

  gtk_tree_view_add_move_binding (binding_set, GDK_Page_Up, 0,
				  GTK_MOVEMENT_PAGES, -1);

  gtk_tree_view_add_move_binding (binding_set, GDK_Page_Down, 0,
				  GTK_MOVEMENT_PAGES, 1);
744

745 746 747
  gtk_binding_entry_add_signal (binding_set, GDK_Right, 0, "move_cursor", 2,
				GTK_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
				GTK_TYPE_INT, 1);
748

749 750 751
  gtk_binding_entry_add_signal (binding_set, GDK_Left, 0, "move_cursor", 2,
				GTK_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
				GTK_TYPE_INT, -1);
752

753
  gtk_binding_entry_add_signal (binding_set, GDK_f, GDK_CONTROL_MASK, "move_cursor", 2,
754
				GTK_TYPE_ENUM, GTK_MOVEMENT_LOGICAL_POSITIONS,
755
				GTK_TYPE_INT, 1);
756

757
  gtk_binding_entry_add_signal (binding_set, GDK_b, GDK_CONTROL_MASK, "move_cursor", 2,
758
				GTK_TYPE_ENUM, GTK_MOVEMENT_LOGICAL_POSITIONS,
759
				GTK_TYPE_INT, -1);
760 761 762

  gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_CONTROL_MASK, "toggle_cursor_row", 0);

763 764
  gtk_binding_entry_add_signal (binding_set, GDK_space, 0, "select_cursor_row", 0);

765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
  /* expand and collapse rows */
  gtk_binding_entry_add_signal (binding_set, GDK_plus, 0, "expand_collapse_cursor_row", 3,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, TRUE,
				GTK_TYPE_BOOL, FALSE);
  /* Not doable on US keyboards */
  gtk_binding_entry_add_signal (binding_set, GDK_plus, GDK_SHIFT_MASK, "expand_collapse_cursor_row", 3,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, TRUE,
				GTK_TYPE_BOOL, TRUE);
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Add, 0, "expand_collapse_cursor_row", 3,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, TRUE,
				GTK_TYPE_BOOL, FALSE);
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Add, GDK_SHIFT_MASK, "expand_collapse_cursor_row", 3,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, TRUE,
				GTK_TYPE_BOOL, TRUE);
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Add, GDK_SHIFT_MASK, "expand_collapse_cursor_row", 3,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, TRUE,
				GTK_TYPE_BOOL, TRUE);
  gtk_binding_entry_add_signal (binding_set, GDK_Right, GDK_SHIFT_MASK, "expand_collapse_cursor_row", 3,
				GTK_TYPE_BOOL, TRUE,
				GTK_TYPE_BOOL, TRUE,
				GTK_TYPE_BOOL, TRUE);

  gtk_binding_entry_add_signal (binding_set, GDK_minus, 0, "expand_collapse_cursor_row", 3,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, FALSE);
  gtk_binding_entry_add_signal (binding_set, GDK_minus, GDK_SHIFT_MASK, "expand_collapse_cursor_row", 3,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, TRUE);
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Subtract, 0, "expand_collapse_cursor_row", 3,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, FALSE);
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Subtract, GDK_SHIFT_MASK, "expand_collapse_cursor_row", 3,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, TRUE);
  gtk_binding_entry_add_signal (binding_set, GDK_Left, GDK_SHIFT_MASK, "expand_collapse_cursor_row", 3,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, FALSE,
				GTK_TYPE_BOOL, TRUE);

  gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0, "select_cursor_parent", 0);

815 816 817 818 819 820 821 822 823 824
}

static void
gtk_tree_view_init (GtkTreeView *tree_view)
{
  tree_view->priv = g_new0 (GtkTreeViewPrivate, 1);

  GTK_WIDGET_SET_FLAGS (tree_view, GTK_CAN_FOCUS);

  tree_view->priv->flags = GTK_TREE_VIEW_IS_LIST | GTK_TREE_VIEW_SHOW_EXPANDERS | GTK_TREE_VIEW_DRAW_KEYFOCUS | GTK_TREE_VIEW_HEADERS_VISIBLE;
825
  gtk_widget_style_get (GTK_WIDGET (tree_view), "expander_width", &tree_view->priv->tab_offset, NULL);
826
  tree_view->priv->n_columns = 0;
827 828 829
  tree_view->priv->header_height = 1;
  tree_view->priv->x_drag = 0;
  tree_view->priv->drag_pos = -1;
830
  tree_view->priv->header_has_focus = FALSE;
Havoc Pennington's avatar
Havoc Pennington committed
831 832 833
  tree_view->priv->pressed_button = -1;
  tree_view->priv->press_start_x = -1;
  tree_view->priv->press_start_y = -1;
834
  tree_view->priv->reorderable = FALSE;
835
  gtk_tree_view_set_adjustments (tree_view, NULL, NULL);
836
  tree_view->priv->selection = _gtk_tree_selection_new_with_tree_view (tree_view);
837
  _gtk_tree_view_update_size (tree_view);
838 839
}

840

841

842
/* GObject Methods
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
static void
gtk_tree_view_set_property (GObject         *object,
			    guint            prop_id,
			    const GValue    *value,
			    GParamSpec      *pspec)
{
  GtkTreeView *tree_view;

  tree_view = GTK_TREE_VIEW (object);

  switch (prop_id)
    {
    case PROP_MODEL:
      gtk_tree_view_set_model (tree_view, GTK_TREE_MODEL (g_value_get_object (value)));
      break;
    case PROP_HADJUSTMENT:
      gtk_tree_view_set_hadjustment (tree_view, GTK_ADJUSTMENT (g_value_get_object (value)));
      break;
    case PROP_VADJUSTMENT:
      gtk_tree_view_set_vadjustment (tree_view, GTK_ADJUSTMENT (g_value_get_object (value)));
      break;
    case PROP_HEADERS_VISIBLE:
      gtk_tree_view_set_headers_visible (tree_view, g_value_get_boolean (value));
      break;
    case PROP_HEADERS_CLICKABLE:
      gtk_tree_view_set_headers_clickable (tree_view, g_value_get_boolean (value));
      break;
    case PROP_EXPANDER_COLUMN:
873
      gtk_tree_view_set_expander_column (tree_view, GTK_TREE_VIEW_COLUMN (g_value_get_object (value)));
874
      break;
875 876 877
    case PROP_REORDERABLE:
      gtk_tree_view_set_reorderable (tree_view, g_value_get_boolean (value));
      break;
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
    case PROP_RULES_HINT:
      gtk_tree_view_set_rules_hint (tree_view, g_value_get_boolean (value));
      break;
    default:
      break;
    }
}

static void
gtk_tree_view_get_property (GObject         *object,
			    guint            prop_id,
			    GValue          *value,
			    GParamSpec      *pspec)
{
  GtkTreeView *tree_view;

  tree_view = GTK_TREE_VIEW (object);

  switch (prop_id)
    {
    case PROP_MODEL:
      g_value_set_object (value, G_OBJECT (tree_view->priv->model));
      break;
    case PROP_HADJUSTMENT:
      g_value_set_object (value, G_OBJECT (tree_view->priv->hadjustment));
      break;
    case PROP_VADJUSTMENT:
      g_value_set_object (value, G_OBJECT (tree_view->priv->vadjustment));
      break;
    case PROP_HEADERS_VISIBLE:
      g_value_set_boolean (value, gtk_tree_view_get_headers_visible (tree_view));
      break;
    case PROP_EXPANDER_COLUMN:
911
      g_value_set_object (value, G_OBJECT (tree_view->priv->expander_column));
912
      break;
913 914 915
    case PROP_REORDERABLE:
      g_value_set_boolean (value, tree_view->priv->reorderable);
      break;
916 917 918 919 920 921 922 923 924
    case PROP_RULES_HINT:
      g_value_set_boolean (value, tree_view->priv->has_rules);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

925 926 927 928 929 930
static void
gtk_tree_view_finalize (GObject *object)
{
  GtkTreeView *tree_view = (GtkTreeView *) object;

  g_free (tree_view->priv);
931

932 933 934 935
  if (G_OBJECT_CLASS (parent_class)->finalize)
    (* G_OBJECT_CLASS (parent_class)->finalize) (object);
}

936 937 938 939 940


/* GtkObject Methods
 */

Jonathan Blandford's avatar
Jonathan Blandford committed
941 942 943 944 945 946 947
static void
gtk_tree_view_destroy (GtkObject *object)
{
  GtkTreeView *tree_view = (GtkTreeView *) object;
  GList *list;


948 949
  if (tree_view->priv->tree != NULL)
    {
950
      gtk_tree_view_unref_and_check_selection_tree (tree_view, tree_view->priv->tree);
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
      _gtk_rbtree_free (tree_view->priv->tree);
      tree_view->priv->tree = NULL;
    }

  if (tree_view->priv->model != NULL)
    {
      g_object_unref (G_OBJECT (tree_view->priv->model));
      tree_view->priv->model = NULL;
    }

  if (tree_view->priv->columns != NULL)
    {
      for (list = tree_view->priv->columns; list; list = list->next)
	g_object_unref (G_OBJECT (list->data));
      g_list_free (tree_view->priv->columns);
      tree_view->priv->columns = NULL;
    }

  if (tree_view->priv->selection != NULL)
    {
      _gtk_tree_selection_set_tree_view (tree_view->priv->selection, NULL);
      g_object_unref (tree_view->priv->selection);
      tree_view->priv->selection = NULL;
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
975

976 977 978 979 980 981 982 983 984 985 986 987
  if (tree_view->priv->scroll_to_path != NULL)
    {
      gtk_tree_path_free (tree_view->priv->scroll_to_path);
      tree_view->priv->scroll_to_path = NULL;
    }

  if (tree_view->priv->drag_dest_row != NULL)
    {
      gtk_tree_row_reference_free (tree_view->priv->drag_dest_row);
      tree_view->priv->drag_dest_row = NULL;
    }

988
  
989 990 991 992 993 994 995
  if (tree_view->priv->column_drop_func_data &&
      tree_view->priv->column_drop_func_data_destroy)
    {
      (* tree_view->priv->column_drop_func_data_destroy) (tree_view->priv->column_drop_func_data);
      tree_view->priv->column_drop_func_data = NULL;
    }

996 997 998 999 1000 1001 1002
  if (tree_view->priv->destroy_count_destroy &&
      tree_view->priv->destroy_count_data)
    {
      (* tree_view->priv->destroy_count_destroy) (tree_view->priv->destroy_count_data);
      tree_view->priv->destroy_count_data = NULL;
    }

1003 1004 1005 1006 1007 1008
  gtk_tree_row_reference_free (tree_view->priv->cursor);
  tree_view->priv->cursor = NULL;

  gtk_tree_row_reference_free (tree_view->priv->anchor);
  tree_view->priv->anchor = NULL;

1009 1010
  if (GTK_OBJECT_CLASS (parent_class)->destroy)
    (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
Jonathan Blandford's avatar
Jonathan Blandford committed
1011 1012
}

1013 1014 1015


/* GtkWidget Methods
1016 1017
 */

1018
/* GtkWidget::map helper */
1019
static void
1020
gtk_tree_view_map_buttons (GtkTreeView *tree_view)
1021
{
1022
  GList *list;
1023

1024
  g_return_if_fail (GTK_WIDGET_MAPPED (tree_view));
1025

1026
  if (GTK_TREE_VIEW_FLAG_SET (tree_view, GTK_TREE_VIEW_HEADERS_VISIBLE))
1027
    {
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
      GtkTreeViewColumn *column;

      for (list = tree_view->priv->columns; list; list = list->next)
	{
	  column = list->data;
          if (GTK_WIDGET_VISIBLE (column->button) &&
              !GTK_WIDGET_MAPPED (column->button))
            gtk_widget_map (column->button);
	}
      for (list = tree_view->priv->columns; list; list = list->next)
	{
	  column = list->data;
	  if (column->visible == FALSE)
	    continue;
Havoc Pennington's avatar
Havoc Pennington committed
1042
	  if (column->column_type == GTK_TREE_VIEW_COLUMN_RESIZABLE)
1043 1044 1045 1046 1047 1048 1049 1050
	    {
	      gdk_window_raise (column->window);
	      gdk_window_show (column->window);
	    }
	  else
	    gdk_window_hide (column->window);
	}
      gdk_window_show (tree_view->priv->header_window);
1051 1052 1053 1054
    }
}

static void
1055
gtk_tree_view_map (GtkWidget *widget)
1056
{
1057
  GList *tmp_list;
1058 1059
  GtkTreeView *tree_view;

1060
  g_return_if_fail (GTK_IS_TREE_VIEW (widget));
Jonathan Blandford's avatar
Jonathan Blandford committed
1061

1062 1063 1064 1065 1066 1067
  tree_view = GTK_TREE_VIEW (widget);

  GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);

  tmp_list = tree_view->priv->children;
  while (tmp_list)
1068
    {
1069 1070 1071 1072 1073 1074 1075 1076
      GtkTreeViewChild *child = tmp_list->data;
      tmp_list = tmp_list->next;

      if (GTK_WIDGET_VISIBLE (child->widget))
	{
	  if (!GTK_WIDGET_MAPPED (child->widget))
	    gtk_widget_map (child->widget);
	}
1077
    }
1078
  gdk_window_show (tree_view->priv->bin_window);
1079

1080 1081 1082 1083
  gtk_tree_view_map_buttons (tree_view);

  gdk_window_show (widget->window);
}
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177

static void
gtk_tree_view_realize (GtkWidget *widget)
{
  GList *tmp_list;
  GtkTreeView *tree_view;
  GdkGCValues values;
  GdkWindowAttr attributes;
  gint attributes_mask;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW (widget));

  tree_view = GTK_TREE_VIEW (widget);

  gtk_tree_view_check_dirty (GTK_TREE_VIEW (widget));
  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

  /* Make the main, clipping window */
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.x = widget->allocation.x;
  attributes.y = widget->allocation.y;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;

  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
				   &attributes, attributes_mask);
  gdk_window_set_user_data (widget->window, widget);

  /* Make the window for the tree */
  attributes.x = 0;
  attributes.y = 0;
  attributes.width = tree_view->priv->width;
  attributes.height = tree_view->priv->height + TREE_VIEW_HEADER_HEIGHT (tree_view);
  attributes.event_mask = GDK_EXPOSURE_MASK |
    GDK_SCROLL_MASK |
    GDK_POINTER_MOTION_MASK |
    GDK_ENTER_NOTIFY_MASK |
    GDK_LEAVE_NOTIFY_MASK |
    GDK_BUTTON_PRESS_MASK |
    GDK_BUTTON_RELEASE_MASK |
    gtk_widget_get_events (widget);

  tree_view->priv->bin_window = gdk_window_new (widget->window,
						&attributes, attributes_mask);
  gdk_window_set_user_data (tree_view->priv->bin_window, widget);

  /* Make the column header window */
  attributes.x = 0;
  attributes.y = 0;
  attributes.width = MAX (tree_view->priv->width, widget->allocation.width);
  attributes.height = tree_view->priv->header_height;
  attributes.event_mask = (GDK_EXPOSURE_MASK |
			   GDK_SCROLL_MASK |
			   GDK_BUTTON_PRESS_MASK |
			   GDK_BUTTON_RELEASE_MASK |
			   GDK_KEY_PRESS_MASK |
			   GDK_KEY_RELEASE_MASK) |
    gtk_widget_get_events (widget);

  tree_view->priv->header_window = gdk_window_new (widget->window,
						   &attributes, attributes_mask);
  gdk_window_set_user_data (tree_view->priv->header_window, widget);


  values.foreground = (widget->style->white.pixel==0 ?
		       widget->style->black:widget->style->white);
  values.function = GDK_XOR;
  values.subwindow_mode = GDK_INCLUDE_INFERIORS;
  tree_view->priv->xor_gc = gdk_gc_new_with_values (widget->window,
						    &values,
						    GDK_GC_FOREGROUND |
						    GDK_GC_FUNCTION |
						    GDK_GC_SUBWINDOW);
  /* Add them all up. */
  widget->style = gtk_style_attach (widget->style, widget->window);
  gdk_window_set_background (widget->window, &widget->style->base[widget->state]);
  gdk_window_set_background (tree_view->priv->bin_window, &widget->style->base[widget->state]);
  gtk_style_set_background (widget->style, tree_view->priv->header_window, GTK_STATE_NORMAL);

  tmp_list = tree_view->priv->children;
  while (tmp_list)
    {
      GtkTreeViewChild *child = tmp_list->data;
      tmp_list = tmp_list->next;

      gtk_widget_set_parent_window (child->widget, tree_view->priv->bin_window);
    }
1178 1179 1180 1181

  for (tmp_list = tree_view->priv->columns; tmp_list; tmp_list = tmp_list->next)
    _gtk_tree_view_column_realize_button (GTK_TREE_VIEW_COLUMN (tmp_list->data));

1182
  _gtk_tree_view_update_size (GTK_TREE_VIEW (widget));
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198

  if (tree_view->priv->scroll_to_path != NULL ||
      tree_view->priv->scroll_to_column != NULL)
    {
      gtk_tree_view_scroll_to_cell (tree_view,
				    tree_view->priv->scroll_to_path,
				    tree_view->priv->scroll_to_column,
				    tree_view->priv->scroll_to_row_align,
				    tree_view->priv->scroll_to_col_align);
      if (tree_view->priv->scroll_to_path)
	{
	  gtk_tree_path_free (tree_view->priv->scroll_to_path);
	  tree_view->priv->scroll_to_path = NULL;
	}
      tree_view->priv->scroll_to_column = NULL;
    }
1199 1200 1201 1202 1203 1204
}

static void
gtk_tree_view_unrealize (GtkWidget *widget)
{
  GtkTreeView *tree_view;
1205
  GList *list;
1206 1207 1208 1209 1210

  g_return_if_fail (GTK_IS_TREE_VIEW (widget));

  tree_view = GTK_TREE_VIEW (widget);

Havoc Pennington's avatar
Havoc Pennington committed
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
  if (tree_view->priv->scroll_timeout != 0)
    {
      gtk_timeout_remove (tree_view->priv->scroll_timeout);
      tree_view->priv->scroll_timeout = 0;
    }

  if (tree_view->priv->open_dest_timeout != 0)
    {
      gtk_timeout_remove (tree_view->priv->open_dest_timeout);
      tree_view->priv->open_dest_timeout = 0;
    }
1222 1223 1224 1225

  for (list = tree_view->priv->columns; list; list = list->next)
    _gtk_tree_view_column_unrealize_button (GTK_TREE_VIEW_COLUMN (list->data));

1226 1227 1228 1229 1230 1231 1232 1233
  gdk_window_set_user_data (tree_view->priv->bin_window, NULL);
  gdk_window_destroy (tree_view->priv->bin_window);
  tree_view->priv->bin_window = NULL;

  gdk_window_set_user_data (tree_view->priv->header_window, NULL);
  gdk_window_destroy (tree_view->priv->header_window);
  tree_view->priv->header_window = NULL;

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
  if (tree_view->priv->drag_window)
    {
      gdk_window_set_user_data (tree_view->priv->drag_window, NULL);
      gdk_window_destroy (tree_view->priv->drag_window);
      tree_view->priv->drag_window = NULL;
    }

  if (tree_view->priv->drag_highlight_window)
    {
      gdk_window_set_user_data (tree_view->priv->drag_highlight_window, NULL);
      gdk_window_destroy (tree_view->priv->drag_highlight_window);
      tree_view->priv->drag_highlight_window = NULL;
    }

1248 1249 1250 1251 1252
  if (tree_view->priv->cursor_drag)
    {
      gdk_cursor_destroy (tree_view->priv->cursor_drag);
      tree_view->priv->cursor_drag = NULL;
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
1253

1254 1255 1256 1257 1258
  if (tree_view->priv->xor_gc)
    {
      gdk_gc_destroy (tree_view->priv->xor_gc);
      tree_view->priv->xor_gc = NULL;
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
1259

1260
  /* GtkWidget::unrealize destroys children and widget->window */
1261 1262 1263 1264
  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
    (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
}

1265
/* GtkWidget::size_request helper */
1266
static void
1267
gtk_tree_view_size_request_buttons (GtkTreeView *tree_view)
1268 1269 1270
{
  GList *list;

1271
  tree_view->priv->header_height = 1;
Jonathan Blandford's avatar
Jonathan Blandford committed
1272

1273
  if (GTK_TREE_VIEW_FLAG_SET (tree_view, GTK_TREE_VIEW_MODEL_SETUP))
1274
    {