gtktreeview.c 178 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 35 36 37

#include <gdk/gdkkeysyms.h>


Havoc Pennington's avatar
Havoc Pennington committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51
/* 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)))
#define CELL_FIRST_PIXEL(tree_view,tree,node) (BACKGROUND_FIRST_PIXEL (tree_view,tree,node) + TREE_VIEW_VERTICAL_SEPARATOR/2)

#define BACKGROUND_HEIGHT(node) (GTK_RBNODE_GET_HEIGHT (node))
#define CELL_HEIGHT(node) (BACKGROUND_HEIGHT (node) - TREE_VIEW_VERTICAL_SEPARATOR);

#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))
52 53 54 55 56 57 58 59 60 61

typedef struct _GtkTreeViewChild GtkTreeViewChild;

struct _GtkTreeViewChild
{
  GtkWidget *widget;
  gint x;
  gint y;
};

62 63 64 65 66 67 68 69 70 71 72
enum {
  PROP_0,

  PROP_MODEL,
  PROP_HADJUSTMENT,
  PROP_VADJUSTMENT,
  PROP_HEADERS_VISIBLE,
  PROP_HEADERS_CLICKABLE,
  PROP_EXPANDER_COLUMN,
  PROP_RULES_HINT
};
73 74 75

static void     gtk_tree_view_init                 (GtkTreeView      *tree_view);
static void     gtk_tree_view_class_init           (GtkTreeViewClass *klass);
76 77 78
static void     gtk_tree_view_set_property         (GObject         *object,
						    guint            prop_id,
						    const GValue    *value,
Jonathan Blandford's avatar
Jonathan Blandford committed
79
						    GParamSpec      *pspec);
80 81 82
static void     gtk_tree_view_get_property         (GObject         *object,
						    guint            prop_id,
						    GValue          *value,
Jonathan Blandford's avatar
Jonathan Blandford committed
83 84
						    GParamSpec      *pspec);
/* o signals */
85 86
static void     gtk_tree_view_finalize             (GObject          *object);

Jonathan Blandford's avatar
Jonathan Blandford committed
87 88 89
/* object signals */
static void     gtk_tree_view_destroy              (GtkObject        *object);

90
/* widget signals */
91
static void     gtk_tree_view_setup_model          (GtkTreeView      *tree_view);
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
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);
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);
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);
static gint     gtk_tree_view_focus                (GtkContainer     *container,
						    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
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
/* 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);

162 163 164 165
/* tree_model signals */
static void     gtk_tree_view_set_adjustments      (GtkTreeView      *tree_view,
						    GtkAdjustment    *hadj,
						    GtkAdjustment    *vadj);
166
static void     gtk_tree_view_changed              (GtkTreeModel     *model,
167
						    GtkTreePath      *path,
168
						    GtkTreeIter      *iter,
169
						    gpointer          data);
170
static void     gtk_tree_view_inserted             (GtkTreeModel     *model,
171
						    GtkTreePath      *path,
172
						    GtkTreeIter      *iter,
173
						    gpointer          data);
174
static void     gtk_tree_view_has_child_toggled    (GtkTreeModel     *model,
175
						    GtkTreePath      *path,
176
						    GtkTreeIter      *iter,
177
						    gpointer          data);
178
static void     gtk_tree_view_deleted              (GtkTreeModel     *model,
179 180 181 182
						    GtkTreePath      *path,
						    gpointer          data);

/* Internal functions */
183 184
static void     gtk_tree_view_unref_tree           (GtkTreeView      *tree_view,
						    GtkRBTree        *tree);
185 186 187 188
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
189 190 191
static void     gtk_tree_view_queue_draw_path      (GtkTreeView      *tree_view,
                                                    GtkTreePath      *path,
                                                    GdkRectangle     *clip_rect);
192
static void     gtk_tree_view_draw_arrow           (GtkTreeView      *tree_view,
Havoc Pennington's avatar
Havoc Pennington committed
193
                                                    GtkRBTree        *tree,
194 195 196
						    GtkRBNode        *node,
						    gint              x,
						    gint              y);
Havoc Pennington's avatar
Havoc Pennington committed
197 198
static void     gtk_tree_view_get_arrow_xrange     (GtkTreeView      *tree_view,
                                                    gint              *x1,
Havoc Pennington's avatar
Havoc Pennington committed
199
                                                    gint              *x2);
200 201 202 203 204
static gint     gtk_tree_view_new_column_width     (GtkTreeView      *tree_view,
						    gint              i,
						    gint             *x);
static void     gtk_tree_view_adjustment_changed   (GtkAdjustment    *adjustment,
						    GtkTreeView      *tree_view);
205
static gint     gtk_tree_view_insert_iter_height   (GtkTreeView      *tree_view,
206
						    GtkRBTree        *tree,
207
						    GtkTreeIter      *iter,
208 209 210
						    gint              depth);
static void     gtk_tree_view_build_tree           (GtkTreeView      *tree_view,
						    GtkRBTree        *tree,
211
						    GtkTreeIter      *iter,
212 213 214 215 216
						    gint              depth,
						    gboolean          recurse,
						    gboolean          calc_bounds);
static void     gtk_tree_view_calc_size            (GtkTreeView      *priv,
						    GtkRBTree        *tree,
217
						    GtkTreeIter      *iter,
218
						    gint              depth);
219 220
static gboolean gtk_tree_view_discover_dirty_iter  (GtkTreeView      *tree_view,
						    GtkTreeIter      *iter,
221 222 223 224
						    gint              depth,
						    gint             *height);
static void     gtk_tree_view_discover_dirty       (GtkTreeView      *tree_view,
						    GtkRBTree        *tree,
225
						    GtkTreeIter      *iter,
226 227
						    gint              depth);
static void     gtk_tree_view_check_dirty          (GtkTreeView      *tree_view);
228
#if 0
229 230 231
static void     gtk_tree_view_create_button        (GtkTreeView      *tree_view,
						    gint              i);
static void     gtk_tree_view_create_buttons       (GtkTreeView      *tree_view);
232
#endif
233 234 235
static void     gtk_tree_view_clamp_node_visible   (GtkTreeView      *tree_view,
						    GtkRBTree        *tree,
						    GtkRBNode        *node);
Havoc Pennington's avatar
Havoc Pennington committed
236 237
static gboolean gtk_tree_view_maybe_begin_dragging_row (GtkTreeView      *tree_view,
                                                        GdkEventMotion   *event);
238 239
static void     _gtk_tree_view_update_col_width    (GtkTreeView      *tree_view);

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

static GtkContainerClass *parent_class = NULL;


/* Class Functions */
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
      };

265
      tree_view_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkTreeView", &tree_view_info, 0);
266 267 268 269 270 271 272 273
    }

  return tree_view_type;
}

static void
gtk_tree_view_class_init (GtkTreeViewClass *class)
{
274
  GObjectClass *o_class;
275 276 277 278
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

279 280 281 282 283
  o_class = (GObjectClass *) class;
  object_class = (GtkObjectClass *) class;
  widget_class = (GtkWidgetClass *) class;
  container_class = (GtkContainerClass *) class;

284 285
  parent_class = g_type_class_peek_parent (class);

286
  o_class->finalize = gtk_tree_view_finalize;
287 288
  o_class->set_property = gtk_tree_view_set_property;
  o_class->get_property = gtk_tree_view_get_property;
289

Jonathan Blandford's avatar
Jonathan Blandford committed
290 291
  object_class->destroy = gtk_tree_view_destroy;
  
292 293 294 295 296 297 298 299 300 301 302 303 304 305
  widget_class->realize = gtk_tree_view_realize;
  widget_class->unrealize = gtk_tree_view_unrealize;
  widget_class->map = gtk_tree_view_map;
  widget_class->size_request = gtk_tree_view_size_request;
  widget_class->size_allocate = gtk_tree_view_size_allocate;
  widget_class->expose_event = gtk_tree_view_expose;
  widget_class->motion_notify_event = gtk_tree_view_motion;
  widget_class->enter_notify_event = gtk_tree_view_enter_notify;
  widget_class->leave_notify_event = gtk_tree_view_leave_notify;
  widget_class->button_press_event = gtk_tree_view_button_press;
  widget_class->button_release_event = gtk_tree_view_button_release;
  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
306 307 308 309 310 311 312 313 314
  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;
Jonathan Blandford's avatar
Jonathan Blandford committed
315

316 317 318 319 320 321
  container_class->forall = gtk_tree_view_forall;
  container_class->remove = gtk_tree_view_remove;
  container_class->focus = gtk_tree_view_focus;

  class->set_scroll_adjustments = gtk_tree_view_set_adjustments;

322 323
  g_object_class_install_property (o_class,
                                   PROP_MODEL,
Tim Janik's avatar
Tim Janik committed
324 325 326 327 328
                                   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
329

330 331 332 333
  g_object_class_install_property (o_class,
                                   PROP_HADJUSTMENT,
                                   g_param_spec_object ("hadjustment",
							_("Horizontal Adjustment"),
Jonathan Blandford's avatar
Jonathan Blandford committed
334
                                                        _("Horizontal Adjustment for the widget"),
335 336 337 338 339 340 341
                                                        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
342
                                                        _("Vertical Adjustment for the widget"),
343 344 345 346 347 348 349
                                                        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
350
							 _("Show the column header buttons"),
351 352 353 354 355 356 357
							 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
358
							 _("Column headers respond to click events"),
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
							 FALSE,
							 G_PARAM_READWRITE));

  g_object_class_install_property (o_class,
                                   PROP_EXPANDER_COLUMN,
                                   g_param_spec_uint ("expander_column",
						      _("Expand Column"),
						      _("Set the column number for the expander column"),
						      0,
						      G_MAXINT,
						      0,
						      G_PARAM_READWRITE));

  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
376
							 _("Set a hint to the theme engine to draw rows in alternating colors"),
377 378 379
							 FALSE,
							 G_PARAM_READWRITE));

380 381 382 383 384
  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
385
		    gtk_marshal_VOID__OBJECT_OBJECT,
386
		    GTK_TYPE_NONE, 2,
387
		    GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
388 389 390 391 392 393 394 395 396
}

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

397
  tree_view->priv->model = NULL;
398 399
  tree_view->priv->flags = GTK_TREE_VIEW_IS_LIST | GTK_TREE_VIEW_SHOW_EXPANDERS | GTK_TREE_VIEW_DRAW_KEYFOCUS | GTK_TREE_VIEW_HEADERS_VISIBLE;
  tree_view->priv->tab_offset = TREE_VIEW_EXPANDER_WIDTH;
400 401
  tree_view->priv->n_columns = 0;
  tree_view->priv->columns = NULL;
402 403 404 405 406 407 408 409 410
  tree_view->priv->button_pressed_node = NULL;
  tree_view->priv->button_pressed_tree = NULL;
  tree_view->priv->prelight_node = NULL;
  tree_view->priv->header_height = 1;
  tree_view->priv->x_drag = 0;
  tree_view->priv->drag_pos = -1;
  tree_view->priv->selection = NULL;
  tree_view->priv->anchor = NULL;
  tree_view->priv->cursor = NULL;
411
  tree_view->priv->header_has_focus = FALSE;
Havoc Pennington's avatar
Havoc Pennington committed
412 413 414
  tree_view->priv->pressed_button = -1;
  tree_view->priv->press_start_x = -1;
  tree_view->priv->press_start_y = -1;
Jonathan Blandford's avatar
Jonathan Blandford committed
415

416
  gtk_tree_view_set_adjustments (tree_view, NULL, NULL);
417
  _gtk_tree_view_update_size (tree_view);
418 419
}

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

/* Object methods
 */

static void
gtk_tree_view_finalize (GObject *object)
{
  GtkTreeView *tree_view = (GtkTreeView *) object;

  if (tree_view->priv->tree)
    _gtk_rbtree_free (tree_view->priv->tree);

  if (tree_view->priv->scroll_to_path != NULL)
    gtk_tree_path_free (tree_view->priv->scroll_to_path);

  if (tree_view->priv->drag_dest_row)
436
    gtk_tree_row_reference_free (tree_view->priv->drag_dest_row);
Jonathan Blandford's avatar
Jonathan Blandford committed
437 438 439 440 441 442 443

  if (tree_view->priv->anchor)
    gtk_tree_row_reference_free (tree_view->priv->anchor);

  if (tree_view->priv->cursor)
    gtk_tree_row_reference_free (tree_view->priv->cursor);

444 445 446 447 448
  g_free (tree_view->priv);
  if (G_OBJECT_CLASS (parent_class)->finalize)
    (* G_OBJECT_CLASS (parent_class)->finalize) (object);
}

Jonathan Blandford's avatar
Jonathan Blandford committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
static void
gtk_tree_view_destroy (GtkObject *object)
{
  GtkTreeView *tree_view = (GtkTreeView *) object;
  GList *list;

  gtk_tree_view_unref_tree (tree_view, tree_view->priv->tree);

  for (list = tree_view->priv->columns; list; list = list->next)
    g_object_unref (G_OBJECT (list->data));

  _gtk_tree_selection_set_tree_view (tree_view->priv->selection, NULL);
  g_object_unref (tree_view->priv->selection);
}

464 465 466 467 468 469 470
/* Property handlers
 */

static void
gtk_tree_view_set_property (GObject         *object,
			    guint            prop_id,
			    const GValue    *value,
Jonathan Blandford's avatar
Jonathan Blandford committed
471
			    GParamSpec      *pspec)
472 473 474 475 476 477 478 479
{
  GtkTreeView *tree_view;

  tree_view = GTK_TREE_VIEW (object);

  switch (prop_id)
    {
    case PROP_MODEL:
Jonathan Blandford's avatar
Jonathan Blandford committed
480
      gtk_tree_view_set_model (tree_view, GTK_TREE_MODEL (g_value_get_object (value)));
481 482
      break;
    case PROP_HADJUSTMENT:
Jonathan Blandford's avatar
Jonathan Blandford committed
483
      gtk_tree_view_set_hadjustment (tree_view, GTK_ADJUSTMENT (g_value_get_object (value)));
484 485
      break;
    case PROP_VADJUSTMENT:
Jonathan Blandford's avatar
Jonathan Blandford committed
486
      gtk_tree_view_set_vadjustment (tree_view, GTK_ADJUSTMENT (g_value_get_object (value)));
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
      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:
      gtk_tree_view_set_expander_column (tree_view, g_value_get_uint (value));
      break;
    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,
Jonathan Blandford's avatar
Jonathan Blandford committed
509
			    GParamSpec      *pspec)
510 511 512 513
{
  GtkTreeView *tree_view;

  tree_view = GTK_TREE_VIEW (object);
Jonathan Blandford's avatar
Jonathan Blandford committed
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
  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_HEADERS_CLICKABLE:
      /* g_value_set_boolean (value, gtk_tree_view_get_headers_clickable (tree_view)); */
      break;
    case PROP_EXPANDER_COLUMN:
      g_value_set_uint (value, tree_view->priv->expander_column);
      break;
    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;
    }
}

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
/* Widget methods
 */

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);
    }
640 641 642 643

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

644
  _gtk_tree_view_update_size (GTK_TREE_VIEW (widget));
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

  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;
    }
661 662 663 664 665 666
}

static void
gtk_tree_view_unrealize (GtkWidget *widget)
{
  GtkTreeView *tree_view;
667
  GList *list;
668 669 670 671 672 673

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

  tree_view = GTK_TREE_VIEW (widget);

Havoc Pennington's avatar
Havoc Pennington committed
674 675 676 677 678 679 680 681 682 683 684
  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;
    }
685 686 687 688

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

689 690 691 692 693 694 695 696
  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;

697
  gdk_cursor_destroy (tree_view->priv->cursor_drag);
Jonathan Blandford's avatar
Jonathan Blandford committed
698 699
  gdk_gc_destroy (tree_view->priv->xor_gc);

700
  /* GtkWidget::unrealize destroys children and widget->window */
Jonathan Blandford's avatar
Jonathan Blandford committed
701

702 703 704 705
  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
    (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
}

706 707 708 709 710 711
static void
gtk_tree_view_map_buttons (GtkTreeView *tree_view)
{
  GList *list;

  g_return_if_fail (GTK_WIDGET_MAPPED (tree_view));
Jonathan Blandford's avatar
Jonathan Blandford committed
712

713 714 715
  if (GTK_TREE_VIEW_FLAG_SET (tree_view, GTK_TREE_VIEW_HEADERS_VISIBLE))
    {
      GtkTreeViewColumn *column;
Jonathan Blandford's avatar
Jonathan Blandford committed
716

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
      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;
	  if (column->column_type == GTK_TREE_VIEW_COLUMN_RESIZEABLE)
	    {
	      gdk_window_raise (column->window);
	      gdk_window_show (column->window);
	    }
	  else
	    gdk_window_hide (column->window);
	}
      gdk_window_show (tree_view->priv->header_window);
    }
}

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
static void
gtk_tree_view_map (GtkWidget *widget)
{
  GList *tmp_list;
  GtkTreeView *tree_view;

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

  tree_view = GTK_TREE_VIEW (widget);

  GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);

  tmp_list = tree_view->priv->children;
  while (tmp_list)
    {
      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);
	}
    }
  gdk_window_show (tree_view->priv->bin_window);
767 768

  gtk_tree_view_map_buttons (tree_view);
Jonathan Blandford's avatar
Jonathan Blandford committed
769

770 771 772 773 774 775 776
  gdk_window_show (widget->window);
}

static void
gtk_tree_view_size_request_buttons (GtkTreeView *tree_view)
{
  GList *list;
Jonathan Blandford's avatar
Jonathan Blandford committed
777

778 779 780
  tree_view->priv->header_height = 1;

  if (GTK_TREE_VIEW_FLAG_SET (tree_view, GTK_TREE_VIEW_MODEL_SETUP))
781
    {
782 783 784 785
      for (list = tree_view->priv->columns; list; list = list->next)
        {
          GtkRequisition requisition;
          GtkTreeViewColumn *column;
Jonathan Blandford's avatar
Jonathan Blandford committed
786

787
          column = list->data;
Jonathan Blandford's avatar
Jonathan Blandford committed
788

789
          gtk_widget_size_request (column->button, &requisition);
Jonathan Blandford's avatar
Jonathan Blandford committed
790

791
          gtk_tree_view_column_set_width (column, MAX (column->width, requisition.width));
792 793
          tree_view->priv->header_height = MAX (tree_view->priv->header_height, requisition.height);
        }
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
    }
}

static void
gtk_tree_view_size_request (GtkWidget      *widget,
			    GtkRequisition *requisition)
{
  GtkTreeView *tree_view;
  GList *tmp_list;

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

  tree_view = GTK_TREE_VIEW (widget);

  requisition->width = 200;
  requisition->height = 200;

  tmp_list = tree_view->priv->children;

  while (tmp_list)
    {
      GtkTreeViewChild *child = tmp_list->data;
      GtkRequisition child_requisition;

      tmp_list = tmp_list->next;

821 822
      if (GTK_WIDGET_VISIBLE (child->widget))
        gtk_widget_size_request (child->widget, &child_requisition);
823
    }
824 825

  gtk_tree_view_size_request_buttons (tree_view);
826 827 828 829 830 831 832 833 834 835
}

static void
gtk_tree_view_size_allocate_buttons (GtkWidget *widget)
{
  GtkTreeView *tree_view;
  GList *list;
  GtkTreeViewColumn *column;
  GtkAllocation allocation;
  gint width = 0;
Jonathan Blandford's avatar
Jonathan Blandford committed
836

837 838 839 840 841
  tree_view = GTK_TREE_VIEW (widget);

  allocation.y = 0;
  allocation.height = tree_view->priv->header_height;

842
  for (list = tree_view->priv->columns; list != NULL; list = list->next)
843 844 845 846 847 848 849
    {
      column = list->data;

      if (!column->visible)
	continue;

      allocation.x = width;
850
      allocation.width = column->displayed_width;
Havoc Pennington's avatar
Havoc Pennington committed
851
      width += column->width;
852 853 854
      gtk_widget_size_allocate (column->button, &allocation);

      if (column->window)
855
	gdk_window_move_resize (column->window,
856 857
                                allocation.x + allocation.width - TREE_VIEW_DRAG_WIDTH/2,
				allocation.y,
858
                                TREE_VIEW_DRAG_WIDTH, allocation.height);
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
    }
}

static void
gtk_tree_view_size_allocate (GtkWidget     *widget,
			     GtkAllocation *allocation)
{
  GList *tmp_list;
  GtkTreeView *tree_view;

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

  widget->allocation = *allocation;

  tree_view = GTK_TREE_VIEW (widget);
Jonathan Blandford's avatar
Jonathan Blandford committed
875

Havoc Pennington's avatar
Havoc Pennington committed
876
  gtk_tree_view_check_dirty (tree_view);
Jonathan Blandford's avatar
Jonathan Blandford committed
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
  tmp_list = tree_view->priv->children;

  while (tmp_list)
    {
      GtkAllocation allocation;
      GtkRequisition requisition;

      GtkTreeViewChild *child = tmp_list->data;
      tmp_list = tmp_list->next;

      allocation.x = child->x;
      allocation.y = child->y;
      gtk_widget_get_child_requisition (child->widget, &requisition);
      allocation.width = requisition.width;
      allocation.height = requisition.height;

      gtk_widget_size_allocate (child->widget, &allocation);
    }

  if (GTK_WIDGET_REALIZED (widget))
    {
      gdk_window_move_resize (widget->window,
			      allocation->x, allocation->y,
			      allocation->width, allocation->height);
Havoc Pennington's avatar
Havoc Pennington committed
902

903 904 905 906
      gdk_window_move_resize (tree_view->priv->header_window,
			      0, 0,
			      MAX (tree_view->priv->width, allocation->width),
			      tree_view->priv->header_height);
907 908 909 910 911 912 913

      if (tree_view->priv->width < allocation->width)
	gdk_window_resize (tree_view->priv->bin_window,
			   allocation->width,
			   tree_view->priv->height + TREE_VIEW_HEADER_HEIGHT (tree_view));

      _gtk_tree_view_update_col_width (tree_view);
914
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
915

916 917
  gtk_tree_view_size_allocate_buttons (widget);

918 919 920 921
  tree_view->priv->hadjustment->page_size = allocation->width;
  tree_view->priv->hadjustment->page_increment = allocation->width / 2;
  tree_view->priv->hadjustment->lower = 0;
  tree_view->priv->hadjustment->upper = tree_view->priv->width;
Havoc Pennington's avatar
Havoc Pennington committed
922

923 924 925 926 927 928 929 930
  if (tree_view->priv->hadjustment->value + allocation->width > tree_view->priv->width)
    tree_view->priv->hadjustment->value = MAX (tree_view->priv->width - allocation->width, 0);
  gtk_signal_emit_by_name (GTK_OBJECT (tree_view->priv->hadjustment), "changed");

  tree_view->priv->vadjustment->page_size = allocation->height - TREE_VIEW_HEADER_HEIGHT (tree_view);
  tree_view->priv->vadjustment->page_increment = (allocation->height - TREE_VIEW_HEADER_HEIGHT (tree_view)) / 2;
  tree_view->priv->vadjustment->lower = 0;
  tree_view->priv->vadjustment->upper = tree_view->priv->height;
Havoc Pennington's avatar
Havoc Pennington committed
931

932 933 934
  if (tree_view->priv->vadjustment->value + allocation->height > tree_view->priv->height)
    gtk_adjustment_set_value (tree_view->priv->vadjustment,
			      (gfloat) MAX (tree_view->priv->height - allocation->height, 0));
Havoc Pennington's avatar
Havoc Pennington committed
935

936
  gtk_signal_emit_by_name (GTK_OBJECT (tree_view->priv->vadjustment), "changed");
937

938 939
}

Havoc Pennington's avatar
Havoc Pennington committed
940 941 942 943 944 945 946 947
static void
gtk_tree_view_draw_node_focus_rect (GtkWidget   *widget,
                                    GtkTreePath *path)
{
  GtkTreeView *tree_view;
  GtkRBTree *tree = NULL;
  GtkRBNode *node = NULL;
  gint bin_window_width = 0;
Jonathan Blandford's avatar
Jonathan Blandford committed
948

Havoc Pennington's avatar
Havoc Pennington committed
949 950 951 952 953 954 955 956 957 958 959 960
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_TREE_VIEW (widget));

  tree_view = GTK_TREE_VIEW (widget);

  _gtk_tree_view_find_node (tree_view, path, &tree, &node);

  if (tree == NULL)
    return;

  gdk_drawable_get_size (tree_view->priv->bin_window,
                         &bin_window_width, NULL);
Jonathan Blandford's avatar
Jonathan Blandford committed
961

Havoc Pennington's avatar
Havoc Pennington committed
962 963 964 965 966
  /* FIXME need a style function appropriate for this */
  gdk_draw_rectangle (tree_view->priv->bin_window,
		      widget->style->fg_gc[GTK_STATE_NORMAL],
		      FALSE,
		      0,
Havoc Pennington's avatar
Havoc Pennington committed
967
                      BACKGROUND_FIRST_PIXEL (tree_view, tree, node),
Havoc Pennington's avatar
Havoc Pennington committed
968
                      bin_window_width - 2,
Havoc Pennington's avatar
Havoc Pennington committed
969
		      BACKGROUND_HEIGHT (node) - 1);
Havoc Pennington's avatar
Havoc Pennington committed
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
}

GdkPixmap*
gtk_tree_view_create_row_drag_icon (GtkTreeView  *tree_view,
                                    GtkTreePath  *path)
{
  GtkTreeIter   iter;
  GtkRBTree    *tree;
  GtkRBNode    *node;
  GtkCellRenderer *cell;
  gint i;
  gint cell_offset;
  GList *list;
  GdkRectangle background_area;
  GtkWidget *widget;
  gint depth;
  /* start drawing inside the black outline */
  gint x = 1, y = 1;
  GdkDrawable *drawable;
  gint bin_window_width;
Jonathan Blandford's avatar
Jonathan Blandford committed
990

Havoc Pennington's avatar
Havoc Pennington committed
991 992 993 994
  widget = GTK_WIDGET (tree_view);

  depth = gtk_tree_path_get_depth (path);

Havoc Pennington's avatar
Havoc Pennington committed
995 996 997 998
  _gtk_tree_view_find_node (tree_view,
                            path,
                            &tree,
                            &node);
Havoc Pennington's avatar
Havoc Pennington committed
999

Havoc Pennington's avatar
Havoc Pennington committed
1000 1001
  if (tree == NULL)
    return NULL;
Jonathan Blandford's avatar
Jonathan Blandford committed
1002

Havoc Pennington's avatar
Havoc Pennington committed
1003 1004 1005 1006
  if (!gtk_tree_model_get_iter (tree_view->priv->model,
                                &iter,
                                path))
    return NULL;
Jonathan Blandford's avatar
Jonathan Blandford committed
1007

Havoc Pennington's avatar
Havoc Pennington committed
1008 1009
  cell_offset = x;

Havoc Pennington's avatar
Havoc Pennington committed
1010 1011
  background_area.y = y;
  background_area.height = BACKGROUND_HEIGHT (node);
Havoc Pennington's avatar
Havoc Pennington committed
1012 1013 1014

  gdk_drawable_get_size (tree_view->priv->bin_window,
                         &bin_window_width, NULL);
Jonathan Blandford's avatar
Jonathan Blandford committed
1015

Havoc Pennington's avatar
Havoc Pennington committed
1016 1017
  drawable = gdk_pixmap_new (tree_view->priv->bin_window,
                             bin_window_width + 2,
Havoc Pennington's avatar
Havoc Pennington committed
1018
                             background_area.height + 2,
Havoc Pennington's avatar
Havoc Pennington committed
1019 1020 1021 1022 1023 1024 1025
                             -1);

  gdk_draw_rectangle (drawable,
                      widget->style->base_gc[GTK_WIDGET_STATE (widget)],
                      TRUE,
                      0, 0,
                      bin_window_width + 2,
Havoc Pennington's avatar
Havoc Pennington committed
1026
                      background_area.height + 2);
Havoc Pennington's avatar
Havoc Pennington committed
1027 1028 1029 1030 1031 1032

  gdk_draw_rectangle (drawable,
                      widget->style->black_gc,
                      FALSE,
                      0, 0,
                      bin_window_width + 1,
Havoc Pennington's avatar
Havoc Pennington committed
1033
                      background_area.height + 1);
Jonathan Blandford's avatar
Jonathan Blandford committed
1034

Havoc Pennington's avatar
Havoc Pennington committed
1035 1036 1037 1038
  for (i = 0, list = tree_view->priv->columns; i < tree_view->priv->n_columns; i++, list = list->next)
    {
      GtkTreeViewColumn *column = list->data;
      GdkRectangle cell_area;
Jonathan Blandford's avatar
Jonathan Blandford committed
1039

Havoc Pennington's avatar
Havoc Pennington committed
1040 1041 1042 1043 1044 1045 1046 1047 1048
      if (!column->visible)
        continue;

      cell = column->cell;
      gtk_tree_view_column_set_cell_data (column,
                                          tree_view->priv->model,
                                          &iter);

      background_area.x = cell_offset;
1049
      background_area.width = column->displayed_width;
Havoc Pennington's avatar
Havoc Pennington committed
1050 1051

      cell_area = background_area;
Havoc Pennington's avatar
Havoc Pennington committed
1052 1053 1054

      cell_area.y += TREE_VIEW_VERTICAL_SEPARATOR / 2;
      cell_area.height -= TREE_VIEW_VERTICAL_SEPARATOR;
Jonathan Blandford's avatar
Jonathan Blandford committed
1055

Havoc Pennington's avatar
Havoc Pennington committed
1056 1057 1058 1059 1060 1061
      if (i == tree_view->priv->expander_column &&
          TREE_VIEW_DRAW_EXPANDERS(tree_view))
        {
          cell_area.x += depth * tree_view->priv->tab_offset;
          cell_area.width -= depth * tree_view->priv->tab_offset;
        }
1062 1063 1064 1065 1066 1067 1068 1069
      if (cell->visible)
	gtk_cell_renderer_render (cell,
				  drawable,
				  widget,
				  &background_area,
				  &cell_area,
				  NULL,
				  0);
Jonathan Blandford's avatar
Jonathan Blandford committed
1070

1071
      cell_offset += column->displayed_width;
Havoc Pennington's avatar
Havoc Pennington committed
1072 1073 1074 1075 1076
    }

  return drawable;
}

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
/* Warning: Very scary function.
 * Modify at your own risk
 */
static gboolean
gtk_tree_view_bin_expose (GtkWidget      *widget,
			  GdkEventExpose *event)
{
  GtkTreeView *tree_view;
  GtkTreePath *path;
  GtkRBTree *tree;
  GList *list;
Havoc Pennington's avatar
Havoc Pennington committed
1088
  GtkRBNode *node;
1089
  GtkRBNode *cursor = NULL;
Havoc Pennington's avatar
Havoc Pennington committed
1090
  GtkRBTree *cursor_tree = NULL;
Havoc Pennington's avatar
Havoc Pennington committed
1091 1092
  GtkRBNode *drag_highlight = NULL;
  GtkRBTree *drag_highlight_tree = NULL;
1093
  GtkTreeIter iter;
1094 1095 1096 1097 1098 1099 1100 1101
  GtkCellRenderer *cell;
  gint new_y;
  gint y_offset, x_offset, cell_offset;
  gint i, max_height;
  gint depth;
  GdkRectangle background_area;
  GdkRectangle cell_area;
  guint flags;
Havoc Pennington's avatar
Havoc Pennington committed
1102 1103
  gint highlight_x;
  gint bin_window_width;
1104 1105
  GtkTreePath *cursor_path;
  GtkTreePath *drag_dest_path;
1106 1107
  GList *last_column;

1108 1109 1110 1111 1112 1113 1114
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_TREE_VIEW (widget), FALSE);

  tree_view = GTK_TREE_VIEW (widget);

  if (tree_view->priv->tree == NULL)
    return TRUE;
Jonathan Blandford's avatar
Jonathan Blandford committed
1115

1116 1117 1118 1119 1120 1121
  gtk_tree_view_check_dirty (GTK_TREE_VIEW (widget));
  /* we want to account for a potential HEADER offset.
   * That is, if the header exists, we want to offset our event by its
   * height to find the right node.
   */
  new_y = (event->area.y<TREE_VIEW_HEADER_HEIGHT (tree_view))?TREE_VIEW_HEADER_HEIGHT (tree_view):event->area.y;
Havoc Pennington's avatar
Havoc Pennington committed
1122 1123

  /* y_offset is the */
Jonathan Blandford's avatar
Jonathan Blandford committed
1124

1125
  y_offset = -_gtk_rbtree_find_offset (tree_view->priv->tree,
Havoc Pennington's avatar
Havoc Pennington committed
1126
                                       TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, new_y),
1127 1128 1129 1130 1131 1132 1133 1134 1135
				       &tree,
				       &node) + new_y - event->area.y;
  if (node == NULL)
    return TRUE;

  /* find the path for the node */
  path = _gtk_tree_view_find_path ((GtkTreeView *)widget,
				   tree,
				   node);
1136 1137 1138
  gtk_tree_model_get_iter (tree_view->priv->model,
			   &iter,
			   path);
1139 1140 1141
  depth = gtk_tree_path_get_depth (path);
  gtk_tree_path_free (path);

1142 1143
  cursor_path = NULL;
  drag_dest_path = NULL;
Jonathan Blandford's avatar
Jonathan Blandford committed
1144

1145
  if (tree_view->priv->cursor)
1146 1147 1148 1149 1150
    cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);

  if (cursor_path)
    _gtk_tree_view_find_node (tree_view, cursor_path,
                              &cursor_tree, &cursor);
1151

Havoc Pennington's avatar
Havoc Pennington committed
1152
  if (tree_view->priv->drag_dest_row)
1153 1154 1155 1156
    drag_dest_path = gtk_tree_row_reference_get_path (tree_view->priv->drag_dest_row);

  if (drag_dest_path)
    _gtk_tree_view_find_node (tree_view, drag_dest_path,
Havoc Pennington's avatar
Havoc Pennington committed
1157 1158 1159 1160
                              &drag_highlight_tree, &drag_highlight);

  gdk_drawable_get_size (tree_view->priv->bin_window,
                         &bin_window_width, NULL);
1161 1162 1163 1164 1165 1166 1167 1168

  for (last_column = g_list_last (tree_view->priv->columns);
       last_column &&
	 !(GTK_TREE_VIEW_COLUMN (last_column->data)->visible) &&
	 GTK_WIDGET_CAN_FOCUS (GTK_TREE_VIEW_COLUMN (last_column->data)->button);
       last_column = last_column->prev)
    ;

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
  /* Actually process the expose event.  To do this, we want to
   * start at the first node of the event, and walk the tree in
   * order, drawing each successive node.
   */

  do
    {
      /* Need to think about this more.
	 if (tree_view->priv->show_expanders)
	 max_height = MAX (TREE_VIEW_EXPANDER_MIN_HEIGHT, GTK_RBNODE_GET_HEIGHT (node));
	 else
      */
Havoc Pennington's avatar
Havoc Pennington committed
1181
      gboolean parity;
Jonathan Blandford's avatar
Jonathan Blandford committed
1182

Havoc Pennington's avatar
Havoc Pennington committed
1183
      max_height = BACKGROUND_HEIGHT (node);
1184 1185 1186

      x_offset = -event->area.x;
      cell_offset = 0;
Havoc Pennington's avatar
Havoc Pennington committed
1187
      highlight_x = 0; /* should match x coord of first cell */
Jonathan Blandford's avatar
Jonathan Blandford committed
1188

Havoc Pennington's avatar
Havoc Pennington committed
1189 1190
      background_area.y = y_offset + event->area.y;
      background_area.height = max_height;
1191 1192 1193 1194 1195
      flags = 0;

      if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_PRELIT))
	flags |= GTK_CELL_RENDERER_PRELIT;

Havoc Pennington's avatar
Havoc Pennington committed
1196
      parity = _gtk_rbtree_node_find_parity (tree, node);
Jonathan Blandford's avatar
Jonathan Blandford committed
1197

1198
      if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
Havoc Pennington's avatar
Havoc Pennington committed
1199
        flags |= GTK_CELL_RENDERER_SELECTED;
Jonathan Blandford's avatar
Jonathan Blandford committed
1200

1201
      for (i = 0, list = tree_view->priv->columns; i < tree_view->priv->n_columns; i++, list = list->next)
1202 1203
	{
	  GtkTreeViewColumn *column = list->data;
1204
	  const gchar *detail = NULL;
Jonathan Blandford's avatar
Jonathan Blandford committed
1205

1206
	  if (!column->visible)
Havoc Pennington's avatar
Havoc Pennington committed
1207
            continue;
1208

1209 1210 1211 1212 1213 1214 1215
	  if (cell_offset > event->area.x + event->area.width ||
	      cell_offset + column->displayed_width < event->area.x)
	    {
	      cell_offset += column->displayed_width;
	      continue;
	    }

Havoc Pennington's avatar
Havoc Pennington committed
1216 1217