gtkctree.c 132 KB
Newer Older
Tim Janik's avatar
Tim Janik committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball, Josh MacDonald, 
 * Copyright (C) 1997-1998 Jay Painter <jpaint@serv.net><jpaint@gimp.org>  
 *
 * GtkCTree widget for GTK+
 * Copyright (C) 1998 Lars Hamann and Stefan Jeske
 *
 * 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 <stdlib.h>
#include "gtkctree.h"
26
#include "gtkbindings.h"
Tim Janik's avatar
Tim Janik committed
27
#include <gdk/gdkx.h>
28
#include <gdk/gdkkeysyms.h>
Tim Janik's avatar
Tim Janik committed
29 30

#define PM_SIZE                    8
31
#define TAB_SIZE                   (PM_SIZE + 6)
Tim Janik's avatar
Tim Janik committed
32 33
#define CELL_SPACING               1
#define CLIST_OPTIMUM_SIZE         512
34 35
#define COLUMN_INSET               3
#define DRAG_WIDTH                 6
Tim Janik's avatar
Tim Janik committed
36 37 38 39 40 41 42 43

#define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
				    (((row) + 1) * CELL_SPACING) + \
				    (clist)->voffset)
#define ROW_FROM_YPIXEL(clist, y)  (((y) - (clist)->voffset) / \
                                    ((clist)->row_height + CELL_SPACING))
#define COLUMN_LEFT_XPIXEL(clist, col)  ((clist)->column[(col)].area.x \
                                    + (clist)->hoffset)
44
#define COLUMN_LEFT(clist, column) ((clist)->column[(column)].area.x)
Tim Janik's avatar
Tim Janik committed
45

46 47 48
#define GTK_CLIST_CLASS_FW(_widget_) GTK_CLIST_CLASS (GTK_OBJECT (_widget_)->klass)


Tim Janik's avatar
Tim Janik committed
49 50 51 52
static void gtk_ctree_class_init        (GtkCTreeClass  *klass);
static void gtk_ctree_init              (GtkCTree       *ctree);
static void gtk_ctree_destroy           (GtkObject      *object);

53 54
static void gtk_ctree_realize           (GtkWidget      *widget);
static void gtk_ctree_unrealize         (GtkWidget      *widget);
Tim Janik's avatar
Tim Janik committed
55 56 57 58 59 60
static gint gtk_ctree_button_press      (GtkWidget      *widget,
					 GdkEventButton *event);
static gint gtk_ctree_button_release    (GtkWidget      *widget,
					 GdkEventButton *event);
static gint gtk_ctree_button_motion     (GtkWidget      *widget, 
					 GdkEventMotion *event);
61 62 63 64
static void draw_row                    (GtkCList       *clist,
					 GdkRectangle   *area,
					 gint            row,
					 GtkCListRow   *clist_row);
Tim Janik's avatar
Tim Janik committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
static void create_xor_gc               (GtkCTree       *ctree);
static void draw_xor_line               (GtkCTree       *ctree);
static void draw_xor_rect               (GtkCTree       *ctree);
static void create_drag_icon            (GtkCTree       *ctree,
					 GtkCTreeRow    *row);
static void tree_draw_row               (GtkCTree      *ctree,
					 GList         *row);
static void cell_empty                  (GtkCList      *clist,
					 GtkCListRow   *clist_row,
					 gint           column);
static void cell_set_text               (GtkCList      *clist,
					 GtkCListRow   *clist_row,
					 gint           column,
					 gchar         *text);
static void cell_set_pixmap             (GtkCList      *clist,
				   	 GtkCListRow   *clist_row,
					 gint           column,
					 GdkPixmap     *pixmap,
					 GdkBitmap     *mask);
static void cell_set_pixtext            (GtkCList      *clist,
				 	 GtkCListRow   *clist_row,
					 gint           column,
					 gchar         *text,
					 guint8         spacing,
					 GdkPixmap     *pixmap,
					 GdkBitmap     *mask);
91 92 93 94 95 96 97 98 99 100
static void set_node_info               (GtkCTree      *ctree,
					 GList         *node,
					 gchar         *text,
					 guint8         spacing,
					 GdkPixmap     *pixmap_closed,
					 GdkBitmap     *mask_closed,
					 GdkPixmap     *pixmap_opened,
					 GdkBitmap     *mask_opened,
					 gboolean       is_leaf,
					 gboolean       expanded);
101 102 103 104 105 106 107 108 109
static GtkCTreeRow *row_new             (GtkCTree      *ctree);
static void row_delete                  (GtkCTree      *ctree,
				 	 GtkCTreeRow   *ctree_row);
static void tree_delete                 (GtkCTree      *ctree, 
					 GList         *node, 
					 gpointer       data);
static void tree_delete_row             (GtkCTree      *ctree, 
					 GList         *node, 
					 gpointer       data);
110
static void real_clear                  (GtkCList      *clist);
111 112 113 114 115 116 117 118 119
static void tree_update_level           (GtkCTree      *ctree, 
					 GList         *node, 
					 gpointer       data);
static void tree_select                 (GtkCTree      *ctree, 
					 GList         *node, 
					 gpointer       data);
static void tree_unselect               (GtkCTree      *ctree, 
					 GList         *node, 
				         gpointer       data);
120 121
static void real_select_all             (GtkCList      *clist);
static void real_unselect_all           (GtkCList      *clist);
122 123 124 125 126 127
static void tree_expand                 (GtkCTree      *ctree, 
					 GList         *node,
					 gpointer       data);
static void tree_collapse               (GtkCTree      *ctree, 
					 GList         *node,
					 gpointer       data);
128 129 130
static void tree_collapse_to_depth      (GtkCTree      *ctree, 
					 GList         *node, 
					 gint           depth);
131 132 133 134 135 136 137 138 139 140 141 142 143
static void tree_toggle_expansion       (GtkCTree      *ctree,
					 GList         *node,
					 gpointer       data);
static void change_focus_row_expansion  (GtkCTree      *ctree,
				         GtkCTreeExpansion expansion);
static void real_select_row             (GtkCList      *clist,
					 gint           row,
					 gint           column,
					 GdkEvent      *event);
static void real_unselect_row           (GtkCList      *clist,
					 gint           row,
					 gint           column,
					 GdkEvent      *event);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
static void real_tree_select            (GtkCTree      *ctree,
					 GList         *node,
					 gint           column);
static void real_tree_unselect          (GtkCTree      *ctree,
					 GList         *node,
					 gint           column);
static void tree_toggle_selection       (GtkCTree      *ctree, 
					 GList         *node, 
				         gint           column);
static void real_tree_expand            (GtkCTree      *ctree,
					 GList         *node);
static void real_tree_collapse          (GtkCTree      *ctree,
					 GList         *node);
static void real_tree_move              (GtkCTree      *ctree,
					 GList         *node,
					 GList         *new_parent, 
					 GList         *new_sibling);
static void gtk_ctree_link              (GtkCTree      *ctree,
					 GList         *node,
					 GList         *parent,
164 165
					 GList         *sibling,
					 gboolean       update_focus_row);
166
static void gtk_ctree_unlink            (GtkCTree      *ctree, 
167 168
					 GList         *node,
					 gboolean       update_focus_row);
169 170 171 172 173 174 175 176 177 178 179 180 181
static GList * gtk_ctree_last_visible    (GtkCTree     *ctree,
					  GList        *node);
static gboolean ctree_is_hot_spot       (GtkCTree      *ctree, 
					 GList         *node,
					 gint           row, 
					 gint           x, 
					 gint           y);
static void tree_sort                   (GtkCTree      *ctree,
					 GList         *node,
					 gpointer       data);
static gint default_compare             (GtkCTree      *ctree,
					 const GList   *node1,
					 const GList   *node2);
Tim Janik's avatar
Tim Janik committed
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199


static void fake_unselect_all           (GtkCList      *clist,
					 gint           row);
static GList*  selection_find           (GtkCList      *clist,
					 gint           row_number,
					 GList         *row_list_element);
static void resync_selection            (GtkCList      *clist,
					 GdkEvent      *event);
static void real_undo_selection         (GtkCList      *clist);
static void select_row_recursive        (GtkCTree      *ctree, 
					 GList         *node, 
					 gpointer       data);




Tim Janik's avatar
Tim Janik committed
200 201 202 203 204 205 206
enum
{
  TREE_SELECT_ROW,
  TREE_UNSELECT_ROW,
  TREE_EXPAND,
  TREE_COLLAPSE,
  TREE_MOVE,
207
  CHANGE_FOCUS_ROW_EXPANSION,
Tim Janik's avatar
Tim Janik committed
208 209 210 211 212 213 214
  LAST_SIGNAL
};

typedef void (*GtkCTreeSignal1) (GtkObject *object,
				 GList     *arg1,
				 gint       arg2,
				 gpointer   data);
215

Tim Janik's avatar
Tim Janik committed
216 217 218 219 220
typedef void (*GtkCTreeSignal2) (GtkObject *object,
				 GList     *arg1,
				 GList     *arg2,
				 GList     *arg3,
				 gpointer   data);
221

Tim Janik's avatar
Tim Janik committed
222 223 224 225
typedef void (*GtkCTreeSignal3) (GtkObject *object,
				 GList     *arg1,
				 gpointer   data);

226 227 228 229
typedef void (*GtkCTreeSignal4) (GtkObject         *object,
				 GtkCTreeExpansion  arg1,
				 gpointer           data);

Tim Janik's avatar
Tim Janik committed
230 231 232 233 234 235 236

static GtkCListClass *parent_class = NULL;
static GtkContainerClass *container_class = NULL;
static guint ctree_signals[LAST_SIGNAL] = {0};


GtkType
237
gtk_ctree_get_type (void)
Tim Janik's avatar
Tim Janik committed
238 239 240 241 242 243 244 245 246 247 248 249
{
  static GtkType ctree_type = 0;

  if (!ctree_type)
    {
      GtkTypeInfo ctree_info =
      {
	"GtkCTree",
	sizeof (GtkCTree),
	sizeof (GtkCTreeClass),
	(GtkClassInitFunc) gtk_ctree_class_init,
	(GtkObjectInitFunc) gtk_ctree_init,
250 251
	/* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
252
        (GtkClassInitFunc) NULL,
Tim Janik's avatar
Tim Janik committed
253 254
      };

255
      ctree_type = gtk_type_unique (GTK_TYPE_CLIST, &ctree_info);
Tim Janik's avatar
Tim Janik committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    }

  return ctree_type;
}

static void
gtk_ctree_class_init (GtkCTreeClass *klass)
{
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkCListClass *clist_class;

  object_class = (GtkObjectClass *) klass;
  widget_class = (GtkWidgetClass *) klass;
  container_class = (GtkContainerClass *) klass;
  clist_class = (GtkCListClass *) klass;

273 274
  parent_class = gtk_type_class (GTK_TYPE_CLIST);
  container_class = gtk_type_class (GTK_TYPE_CONTAINER);
Tim Janik's avatar
Tim Janik committed
275 276 277 278 279 280

  ctree_signals[TREE_SELECT_ROW] =
    gtk_signal_new ("tree_select_row",
		    GTK_RUN_FIRST,
		    object_class->type,
		    GTK_SIGNAL_OFFSET (GtkCTreeClass, tree_select_row),
281
		    gtk_marshal_NONE__POINTER_INT,
Tim Janik's avatar
Tim Janik committed
282 283 284 285 286 287
		    GTK_TYPE_NONE, 2, GTK_TYPE_POINTER, GTK_TYPE_INT);
  ctree_signals[TREE_UNSELECT_ROW] =
    gtk_signal_new ("tree_unselect_row",
		    GTK_RUN_FIRST,
		    object_class->type,
		    GTK_SIGNAL_OFFSET (GtkCTreeClass, tree_unselect_row),
288
		    gtk_marshal_NONE__POINTER_INT,
Tim Janik's avatar
Tim Janik committed
289 290 291 292 293 294
		    GTK_TYPE_NONE, 2, GTK_TYPE_POINTER, GTK_TYPE_INT);
  ctree_signals[TREE_EXPAND] =
    gtk_signal_new ("tree_expand",
		    GTK_RUN_LAST,
		    object_class->type,
		    GTK_SIGNAL_OFFSET (GtkCTreeClass, tree_expand),
295
		    gtk_marshal_NONE__POINTER,
Tim Janik's avatar
Tim Janik committed
296 297 298 299 300 301
		    GTK_TYPE_NONE, 1, GTK_TYPE_POINTER);
  ctree_signals[TREE_COLLAPSE] =
    gtk_signal_new ("tree_collapse",
		    GTK_RUN_LAST,
		    object_class->type,
		    GTK_SIGNAL_OFFSET (GtkCTreeClass, tree_collapse),
302
		    gtk_marshal_NONE__POINTER,
Tim Janik's avatar
Tim Janik committed
303 304 305 306 307 308
		    GTK_TYPE_NONE, 1, GTK_TYPE_POINTER);
  ctree_signals[TREE_MOVE] =
    gtk_signal_new ("tree_move",
		    GTK_RUN_LAST,
		    object_class->type,
		    GTK_SIGNAL_OFFSET (GtkCTreeClass, tree_move),
309
		    gtk_marshal_NONE__POINTER_POINTER_POINTER,
Tim Janik's avatar
Tim Janik committed
310 311
		    GTK_TYPE_NONE, 3, GTK_TYPE_POINTER, GTK_TYPE_POINTER, 
		    GTK_TYPE_POINTER);
312 313 314 315 316 317
  ctree_signals[CHANGE_FOCUS_ROW_EXPANSION] =
    gtk_signal_new ("change_focus_row_expansion",
		    GTK_RUN_LAST | GTK_RUN_ACTION,
		    object_class->type,
		    GTK_SIGNAL_OFFSET (GtkCTreeClass,
				       change_focus_row_expansion),
318
		    gtk_marshal_NONE__ENUM,
319
		    GTK_TYPE_NONE, 1, GTK_TYPE_ENUM);
Tim Janik's avatar
Tim Janik committed
320 321 322 323 324 325 326 327 328 329 330

  gtk_object_class_add_signals (object_class, ctree_signals, LAST_SIGNAL);

  object_class->destroy = gtk_ctree_destroy;

  widget_class->realize = gtk_ctree_realize;
  widget_class->unrealize = gtk_ctree_unrealize;
  widget_class->button_press_event = gtk_ctree_button_press;
  widget_class->button_release_event = gtk_ctree_button_release;
  widget_class->motion_notify_event = gtk_ctree_button_motion;

331 332 333 334 335
  clist_class->select_row = real_select_row;
  clist_class->unselect_row = real_unselect_row;
  clist_class->undo_selection = real_undo_selection;
  clist_class->resync_selection = resync_selection;
  clist_class->selection_find = selection_find;
Tim Janik's avatar
Tim Janik committed
336 337
  clist_class->click_column = NULL;
  clist_class->draw_row = draw_row;
338 339 340 341
  clist_class->clear = real_clear;
  clist_class->select_all = real_select_all;
  clist_class->unselect_all = real_unselect_all;
  clist_class->fake_unselect_all = fake_unselect_all;
Tim Janik's avatar
Tim Janik committed
342

343 344
  klass->tree_select_row = real_tree_select;
  klass->tree_unselect_row = real_tree_unselect;
Tim Janik's avatar
Tim Janik committed
345 346 347
  klass->tree_expand = real_tree_expand;
  klass->tree_collapse = real_tree_collapse;
  klass->tree_move = real_tree_move;
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  klass->change_focus_row_expansion = change_focus_row_expansion;

  {
    GtkBindingSet *binding_set;

    binding_set = gtk_binding_set_by_class (klass);
    gtk_binding_entry_add_signal (binding_set,
				  '+', GDK_SHIFT_MASK,
				  "change_focus_row_expansion", 1,
				  GTK_TYPE_ENUM, GTK_CTREE_EXPANSION_EXPAND);
    gtk_binding_entry_add_signal (binding_set,
				  GDK_KP_Add, 0,
				  "change_focus_row_expansion", 1,
				  GTK_TYPE_ENUM, GTK_CTREE_EXPANSION_EXPAND);
    gtk_binding_entry_add_signal (binding_set,
				  GDK_KP_Add, GDK_CONTROL_MASK,
				  "change_focus_row_expansion", 1,
				  GTK_TYPE_ENUM,
				  GTK_CTREE_EXPANSION_EXPAND_RECURSIVE);
    gtk_binding_entry_add_signal (binding_set,
				  '-', 0,
				  "change_focus_row_expansion", 1,
				  GTK_TYPE_ENUM, GTK_CTREE_EXPANSION_COLLAPSE);
    gtk_binding_entry_add_signal (binding_set,
				  GDK_KP_Subtract, 0,
				  "change_focus_row_expansion", 1,
				  GTK_TYPE_ENUM, GTK_CTREE_EXPANSION_COLLAPSE);
    gtk_binding_entry_add_signal (binding_set,
				  GDK_KP_Subtract, GDK_CONTROL_MASK,
				  "change_focus_row_expansion", 1,
				  GTK_TYPE_ENUM,
				  GTK_CTREE_EXPANSION_COLLAPSE_RECURSIVE);
    gtk_binding_entry_add_signal (binding_set,
				  '=', 0,
				  "change_focus_row_expansion", 1,
				  GTK_TYPE_ENUM, GTK_CTREE_EXPANSION_TOGGLE);
    gtk_binding_entry_add_signal (binding_set,
				  GDK_KP_Multiply, 0,
				  "change_focus_row_expansion", 1,
				  GTK_TYPE_ENUM, GTK_CTREE_EXPANSION_TOGGLE);
    gtk_binding_entry_add_signal (binding_set,
				  GDK_KP_Multiply, GDK_CONTROL_MASK,
				  "change_focus_row_expansion", 1,
				  GTK_TYPE_ENUM,
				  GTK_CTREE_EXPANSION_TOGGLE_RECURSIVE);
  }

Tim Janik's avatar
Tim Janik committed
395 396 397 398 399
}

static void
gtk_ctree_init (GtkCTree *ctree)
{
400 401
  ctree->xor_gc         = NULL;
  ctree->drag_icon      = NULL;
Tim Janik's avatar
Tim Janik committed
402 403 404 405 406 407 408
  ctree->tree_indent    = 20;
  ctree->tree_column    = 0;
  ctree->drag_row       = -1;
  ctree->drag_source    = NULL;
  ctree->drag_target    = NULL;
  ctree->insert_pos     = GTK_CTREE_POS_AS_CHILD;
  ctree->node_compare   = default_compare;
409 410 411 412 413 414
  ctree->auto_sort      = FALSE;
  ctree->reorderable    = FALSE;
  ctree->use_icons      = TRUE;
  ctree->in_drag        = FALSE;
  ctree->drag_rect      = FALSE;
  ctree->line_style     = GTK_CTREE_LINES_SOLID;
Tim Janik's avatar
Tim Janik committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
}

static void
gtk_ctree_destroy (GtkObject *object)
{
  gint i;
  GtkCList *clist;

  g_return_if_fail (object != NULL);
  g_return_if_fail (GTK_IS_CTREE (object));

  clist = GTK_CLIST (object);

  GTK_CLIST_SET_FLAG (clist, CLIST_FROZEN);

430
  gtk_clist_clear (GTK_CLIST (object));
Tim Janik's avatar
Tim Janik committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

  if (clist->vscrollbar)
    {
      gtk_widget_unparent (clist->vscrollbar);
      clist->vscrollbar = NULL;
    }
  if (clist->hscrollbar)
    {
      gtk_widget_unparent (clist->hscrollbar);
      clist->hscrollbar = NULL;
    }

  for (i = 0; i < clist->columns; i++)
    if (clist->column[i].button)
      {
        gtk_widget_unparent (clist->column[i].button);
        clist->column[i].button = NULL;
      }

  if (GTK_OBJECT_CLASS (container_class)->destroy)
    (*GTK_OBJECT_CLASS (container_class)->destroy) (object);
}

static void
gtk_ctree_realize (GtkWidget *widget)
{
  GtkCTree *ctree;
  GdkGCValues values;

460 461
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_CTREE (widget));
Tim Janik's avatar
Tim Janik committed
462 463 464

  (* GTK_WIDGET_CLASS (parent_class)->realize) (widget);

465 466
  ctree = GTK_CTREE (widget);

Tim Janik's avatar
Tim Janik committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
  values.foreground = widget->style->fg[GTK_STATE_NORMAL];
  values.background = widget->style->bg[GTK_STATE_NORMAL];
  values.subwindow_mode = GDK_INCLUDE_INFERIORS;
  values.line_style = GDK_LINE_SOLID;
  ctree->lines_gc = gdk_gc_new_with_values (GTK_CLIST(widget)->clist_window, 
					    &values,
					    GDK_GC_FOREGROUND |
					    GDK_GC_BACKGROUND |
					    GDK_GC_SUBWINDOW |
					    GDK_GC_LINE_STYLE);

  if (ctree->line_style == GTK_CTREE_LINES_DOTTED)
    {
      gdk_gc_set_line_attributes (ctree->lines_gc, 1, 
				  GDK_LINE_ON_OFF_DASH, None, None);
482
      gdk_gc_set_dashes (ctree->lines_gc, 0, "\1\1", 2);
Tim Janik's avatar
Tim Janik committed
483 484 485 486 487 488 489 490 491 492 493
    }

  if (ctree->reorderable)
    create_xor_gc (ctree);
}

static void
gtk_ctree_unrealize (GtkWidget *widget)
{
  GtkCTree *ctree;

494 495
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_CTREE (widget));
Tim Janik's avatar
Tim Janik committed
496 497 498

  (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);

499 500
  ctree = GTK_CTREE (widget);

Tim Janik's avatar
Tim Janik committed
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
  gdk_gc_destroy (ctree->lines_gc);

  if (ctree->reorderable)
    gdk_gc_destroy (ctree->xor_gc);
}

static gint
gtk_ctree_button_press (GtkWidget      *widget,
			GdkEventButton *event)
{
  GtkCTree *ctree;
  GtkCList *clist;

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_CTREE (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  ctree = GTK_CTREE (widget);
  clist = GTK_CLIST (widget);

  if (event->window == clist->clist_window)
    {
523
      gboolean collapse_expand = FALSE;
Tim Janik's avatar
Tim Janik committed
524 525 526 527 528 529 530 531 532
      GList *work;
      gint x;
      gint y;
      gint row;
      gint column;

      x = event->x;
      y = event->y;

533 534 535 536 537 538 539
      if (!gtk_clist_get_selection_info (clist, x, y, &row, &column))
	return FALSE;

      if (event->button == 2)
	ctree->drag_row = - 1 - ROW_FROM_YPIXEL (clist, y);

      work = g_list_nth (clist->row_list, row);
Tim Janik's avatar
Tim Janik committed
540
	  
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
      if (ctree->reorderable && event->button == 2 && !ctree->in_drag &&
	  clist->anchor == -1)
	{
	  gdk_pointer_grab (event->window, FALSE,
			    GDK_POINTER_MOTION_HINT_MASK |
			    GDK_BUTTON2_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
			    NULL, NULL, event->time);
	  ctree->in_drag = TRUE;
	  ctree->drag_source = work;
	  ctree->drag_target = NULL;
	  return FALSE;
	}
      else if (event->button == 1 &&
	       (GTK_CTREE_ROW (work)->children &&
		(event->type == GDK_2BUTTON_PRESS ||
		 ctree_is_hot_spot (ctree, work, row, x, y))))
	{
	  if (GTK_CTREE_ROW (work)->expanded)
	    gtk_ctree_collapse (ctree, work);
	  else
	    gtk_ctree_expand (ctree, work);

	  collapse_expand = TRUE;
	}
      if (event->button == 1)
	{
	  gint old_row = clist->focus_row;
	  gboolean no_focus_row = FALSE;

	  switch (clist->selection_mode)
Tim Janik's avatar
Tim Janik committed
571
	    {
572 573 574 575 576 577 578 579 580 581 582 583 584
	    case GTK_SELECTION_MULTIPLE:
	    case GTK_SELECTION_SINGLE:
	      if (!collapse_expand)
		break;

	      if (clist->focus_row == -1)
		{
		  old_row = row;
		  no_focus_row = TRUE;
		}
		  
	      GTK_CLIST_SET_FLAG (clist, CLIST_DRAG_SELECTION);
	      gdk_pointer_grab (clist->clist_window, FALSE,
Tim Janik's avatar
Tim Janik committed
585 586 587 588
				GDK_POINTER_MOTION_HINT_MASK |
				GDK_BUTTON1_MOTION_MASK |
				GDK_BUTTON_RELEASE_MASK,
				NULL, NULL, event->time);
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

	      if (GTK_CLIST_ADD_MODE (clist))
		{
		  GTK_CLIST_UNSET_FLAG (clist, CLIST_ADD_MODE);
		  if (GTK_WIDGET_HAS_FOCUS (widget))
		    {
		      gtk_widget_draw_focus (widget);
		      gdk_gc_set_line_attributes (clist->xor_gc, 1,
						  GDK_LINE_SOLID, 0, 0);
		      clist->focus_row = row;
		      gtk_widget_draw_focus (widget);
		    }
		  else
		    {
		      gdk_gc_set_line_attributes (clist->xor_gc, 1,
						  GDK_LINE_SOLID, 0, 0);
		      clist->focus_row = row;
		    }
		}
	      else if (row != clist->focus_row)
		{
		  if (GTK_WIDGET_HAS_FOCUS (widget))
		    {
		      gtk_widget_draw_focus (widget);
		      clist->focus_row = row;
		      gtk_widget_draw_focus (widget);
		    }
		  else
		    clist->focus_row = row;
		}

	      if (!GTK_WIDGET_HAS_FOCUS (widget))
		gtk_widget_grab_focus (widget);

Tim Janik's avatar
Tim Janik committed
623
	      return FALSE;
624 625 626

	    default:
	      break;
Tim Janik's avatar
Tim Janik committed
627 628 629
	    }
	}
    }
630
  return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
Tim Janik's avatar
Tim Janik committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
}

static gint
gtk_ctree_button_motion (GtkWidget      *widget, 
			 GdkEventMotion *event)
{
  GtkCTree *ctree;
  GtkCList *clist;
  gint x;
  gint y;
  gint row;
  gint insert_pos = GTK_CTREE_POS_AS_CHILD;

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_CTREE (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  ctree = GTK_CTREE (widget);
  clist = GTK_CLIST (widget);

651 652 653
  if (GTK_CLIST_IN_DRAG (clist))
    return  GTK_WIDGET_CLASS (parent_class)->motion_notify_event
      (widget, event);
Tim Janik's avatar
Tim Janik committed
654

655 656
  if (event->window == clist->clist_window && 
      ctree->in_drag && ctree->reorderable)
Tim Janik's avatar
Tim Janik committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
    {
      GdkModifierType modmask;
      gint root_x;
      gint root_y;

      x = event->x;
      y = event->y;
      if (event->is_hint)
	gdk_window_get_pointer (event->window, &x, &y, NULL);

      /* delayed drag start */
      if (!ctree->drag_target &&
	  y >= ROW_TOP_YPIXEL (clist, -ctree->drag_row-1) &&
	  y <= ROW_TOP_YPIXEL (clist, -ctree->drag_row-1) + clist->row_height)
	return 
672
	  GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
Tim Janik's avatar
Tim Janik committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708

      if (ctree->use_icons)
	{
	  if (!ctree->drag_icon)
	    create_drag_icon (ctree, GTK_CTREE_ROW (ctree->drag_source));
	  else
	    {
	      gdk_window_get_pointer (NULL, &root_x, &root_y, &modmask);
	      gdk_window_move (ctree->drag_icon, root_x - ctree->icon_width /2,
			       root_y - ctree->icon_height);
	    }
	}

      /* out of bounds check */
      if (x < 0 || y < -3 || x > clist->clist_window_width ||
	  y > clist->clist_window_height + 3 ||
	  y > ROW_TOP_YPIXEL (clist, clist->rows-1) + clist->row_height + 3)
	{
	  if (ctree->drag_row >= 0)
	    {
	      if (ctree->drag_rect)
		{
		  draw_xor_rect (ctree);
		  ctree->drag_rect = FALSE;
		}
	      else
		draw_xor_line (ctree);
	      ctree->drag_row = -1;
	    }
	  return 
	    (* GTK_WIDGET_CLASS (parent_class)->motion_notify_event) 
	    (widget, event);
	}

      row = ROW_FROM_YPIXEL (clist, y);

709
      /* re-calculate target (mouse left the window) */
Tim Janik's avatar
Tim Janik committed
710 711 712 713 714 715
      if (ctree->drag_target && ctree->drag_row == -1)
	ctree->drag_target = g_list_nth (clist->row_list, row);
      
      if (y < 0 || y > clist->clist_window_height || 
	  ROW_TOP_YPIXEL (clist, row + 1) > clist->clist_window_height
	  || row >= clist->rows)
716
	return GTK_WIDGET_CLASS (parent_class)->motion_notify_event
Tim Janik's avatar
Tim Janik committed
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
	  (widget, event);

      if (y - ROW_TOP_YPIXEL (clist, row) < clist->row_height / 4)
	insert_pos = GTK_CTREE_POS_BEFORE;
      else if (ROW_TOP_YPIXEL (clist, row) + clist->row_height - y 
	       < clist->row_height / 4)
	insert_pos = GTK_CTREE_POS_AFTER;

      if (row != ctree->drag_row || 
	  (row == ctree->drag_row && ctree->insert_pos != insert_pos))
	{
	  if (insert_pos != GTK_CTREE_POS_AS_CHILD)
	    {
	      if (ctree->drag_row >= 0)
		{
		  if (ctree->drag_rect)
		    {
		      draw_xor_rect (ctree);
		      ctree->drag_rect = FALSE;
		    }
		  else
		    draw_xor_line (ctree);
		}
	      ctree->insert_pos = insert_pos;
	      ctree->drag_target = g_list_nth (clist->row_list, row);
	      ctree->drag_row = row;
	      draw_xor_line (ctree);
	    }
	  else if (ctree->drag_target &&
		   !GTK_CTREE_ROW (ctree->drag_target)->is_leaf)
	    {
	      if (ctree->drag_row >= 0)
		{
		  if (ctree->drag_rect)
		    draw_xor_rect (ctree);
		  else
		    draw_xor_line (ctree);
		}
	      ctree->drag_rect = TRUE;
	      ctree->insert_pos = insert_pos;
	      ctree->drag_target = g_list_nth (clist->row_list, row);
	      ctree->drag_row = row;
	      draw_xor_rect (ctree);
	    }
	}
    }
763
  return GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
Tim Janik's avatar
Tim Janik committed
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
}

static gint
gtk_ctree_button_release (GtkWidget      *widget, 
			  GdkEventButton *event)
{
  GtkCTree *ctree;
  GtkCList *clist;

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_CTREE (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  ctree = GTK_CTREE (widget);
  clist = GTK_CLIST (widget);

780
  if (event->button == 2 && clist->anchor == -1)
Tim Janik's avatar
Tim Janik committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
    {
      gdk_pointer_ungrab (event->time);
      ctree->in_drag = FALSE;

      if (ctree->use_icons && ctree->drag_icon)
	{
	  gdk_window_destroy (ctree->drag_icon);
	  ctree->drag_icon = NULL;
	}

      if (ctree->drag_row >= 0)
	{
	  if (ctree->drag_rect)
	    {
	      draw_xor_rect (ctree);
	      ctree->drag_rect = FALSE;
	    }
	  else
	    draw_xor_line (ctree);
	  ctree->drag_row = -1;
	}

803
      /* nop if out of bounds / source == target */
Tim Janik's avatar
Tim Janik committed
804 805 806
      if (event->x < 0 || event->y < -3 ||
	  event->x > clist->clist_window_width ||
	  event->y > clist->clist_window_height + 3 ||
807 808
	  ctree->drag_target == ctree->drag_source ||
	  !ctree->drag_target)
809
	return GTK_WIDGET_CLASS (parent_class)->button_release_event
Tim Janik's avatar
Tim Janik committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
	  (widget, event);

      if (!GTK_CTREE_ROW (ctree->drag_source)->children ||
	  !gtk_ctree_is_ancestor (ctree, ctree->drag_source,
				  ctree->drag_target))
	{
	  if (ctree->insert_pos == GTK_CTREE_POS_AFTER)
	    {
	      if (GTK_CTREE_ROW (ctree->drag_target)->sibling != 
		  ctree->drag_source)
		gtk_signal_emit (GTK_OBJECT (ctree), 
				 ctree_signals[TREE_MOVE],
				 ctree->drag_source,
				 GTK_CTREE_ROW (ctree->drag_target)->parent,
				 GTK_CTREE_ROW (ctree->drag_target)->sibling);
	    }
	  else if (ctree->insert_pos == GTK_CTREE_POS_BEFORE)
	    {
	      if (GTK_CTREE_ROW (ctree->drag_source)->sibling != 
		  ctree->drag_target)
		gtk_signal_emit (GTK_OBJECT (ctree), 
				 ctree_signals[TREE_MOVE],
				 ctree->drag_source,
				 GTK_CTREE_ROW (ctree->drag_target)->parent,
				 ctree->drag_target);
	    }
	  else if (!GTK_CTREE_ROW (ctree->drag_target)->is_leaf)
	    {
	      if (GTK_CTREE_ROW (ctree->drag_target)->children !=
		  ctree->drag_source)
		gtk_signal_emit (GTK_OBJECT (ctree), 
				 ctree_signals[TREE_MOVE],
				 ctree->drag_source,
				 ctree->drag_target,
				 GTK_CTREE_ROW (ctree->drag_target)->children);
	    }
	}
      ctree->drag_source = NULL;
      ctree->drag_target = NULL;
    }
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
  else if (event->button == 1 && GTK_CLIST_DRAG_SELECTION (clist) &&
	   (clist->selection_mode == GTK_SELECTION_SINGLE ||
	    clist->selection_mode == GTK_SELECTION_MULTIPLE))
    {
      gint row;
      gint column;
      GList *work;

      if (gtk_clist_get_selection_info
	  (clist, event->x, event->y, &row, &column))
	{
	  if (clist->anchor == clist->focus_row &&
	      (work = g_list_nth (clist->row_list, row)))
	    tree_toggle_selection (ctree, work, column);
	}
      clist->anchor = -1;
    }
  return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
Tim Janik's avatar
Tim Janik committed
868 869 870 871 872 873
}

static void
create_drag_icon (GtkCTree    *ctree,
		  GtkCTreeRow *row)
{
874 875
  GtkCList *clist;
  GtkWidget *widget;
Tim Janik's avatar
Tim Janik committed
876 877 878 879 880 881 882 883 884 885 886 887
  GdkWindow *window = NULL;
  GdkWindowAttr attributes;
  gint attributes_mask;
  GdkPixmap *pixmap;
  GdkBitmap *mask;
  GdkModifierType modmask;
  gint root_x;
  gint root_y;

  clist  = GTK_CLIST (ctree);
  widget = GTK_WIDGET (ctree);

888
  if (!(pixmap = GTK_CELL_PIXTEXT (row->row.cell[ctree->tree_column])->pixmap))
Tim Janik's avatar
Tim Janik committed
889
    return;
890
  mask = GTK_CELL_PIXTEXT (row->row.cell[ctree->tree_column])->mask;
Tim Janik's avatar
Tim Janik committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932

  gdk_window_get_pointer (NULL, &root_x, &root_y, &modmask);
  gdk_window_get_size (pixmap, &ctree->icon_width, &ctree->icon_height);

  attributes.window_type = GDK_WINDOW_TEMP;
  attributes.x = root_x - ctree->icon_width / 2;
  attributes.y = root_y - ctree->icon_height;
  attributes.width = ctree->icon_width;
  attributes.height = ctree->icon_height;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  attributes.event_mask = gtk_widget_get_events (widget);

  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

  window = gdk_window_new (widget->window, &attributes, attributes_mask);
  gdk_window_set_back_pixmap (window, pixmap, FALSE);
  if (mask)
    gdk_window_shape_combine_mask (window, mask, 0, 0);
  gdk_window_show (window);

  ctree->drag_icon = window;
}

static void
create_xor_gc (GtkCTree *ctree)
{
  GtkCList *clist;
  GdkGCValues values;

  clist = GTK_CLIST (ctree);

  values.foreground = GTK_WIDGET (clist)->style->bg[GTK_STATE_NORMAL];
  values.function = GDK_XOR;
  values.subwindow_mode = GDK_INCLUDE_INFERIORS;
  ctree->xor_gc = gdk_gc_new_with_values (clist->clist_window, &values,
					  GDK_GC_FOREGROUND |
					  GDK_GC_FUNCTION |
					  GDK_GC_SUBWINDOW);
  gdk_gc_set_line_attributes (ctree->xor_gc, 1, GDK_LINE_ON_OFF_DASH, 
			      None, None);
933
  gdk_gc_set_dashes (ctree->xor_gc, 0, "\2\2", 2);
Tim Janik's avatar
Tim Janik committed
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
}

static void
draw_xor_line (GtkCTree *ctree)
{
  GtkCList *clist;
  gint level;
  gint y = 0;

  clist = GTK_CLIST (ctree);

  level = GTK_CTREE_ROW (ctree->drag_target)->level;

  if (ctree->insert_pos == GTK_CTREE_POS_AFTER)
    y = ROW_TOP_YPIXEL (clist, ctree->drag_row) + clist->row_height;
  else 
    y = ROW_TOP_YPIXEL (clist, ctree->drag_row) - 1;

952
  switch (clist->column[ctree->tree_column].justification)
Tim Janik's avatar
Tim Janik committed
953
    {
954 955 956
    case GTK_JUSTIFY_CENTER:
    case GTK_JUSTIFY_FILL:
    case GTK_JUSTIFY_LEFT:
Tim Janik's avatar
Tim Janik committed
957 958 959 960 961
      if (ctree->tree_column > 0)
	gdk_draw_line (clist->clist_window, ctree->xor_gc, 
		       COLUMN_LEFT_XPIXEL(clist, 0), y,
		       COLUMN_LEFT_XPIXEL(clist, ctree->tree_column - 1) +
		       clist->column[ctree->tree_column - 1].area.width, y);
962
      
Tim Janik's avatar
Tim Janik committed
963 964
      gdk_draw_line (clist->clist_window, ctree->xor_gc, 
		     COLUMN_LEFT_XPIXEL(clist, ctree->tree_column) + 
965 966
		     ctree->tree_indent * level -
		     (ctree->tree_indent - PM_SIZE) / 2, y,
Tim Janik's avatar
Tim Janik committed
967
		     GTK_WIDGET (ctree)->allocation.width, y);
968 969
      break;
    case GTK_JUSTIFY_RIGHT:
Tim Janik's avatar
Tim Janik committed
970 971 972 973 974
      if (ctree->tree_column < clist->columns - 1)
	gdk_draw_line (clist->clist_window, ctree->xor_gc, 
		       COLUMN_LEFT_XPIXEL(clist, ctree->tree_column + 1), y,
		       COLUMN_LEFT_XPIXEL(clist, clist->columns - 1) +
		       clist->column[clist->columns - 1].area.width, y);
975
      
Tim Janik's avatar
Tim Janik committed
976 977 978
      gdk_draw_line (clist->clist_window, ctree->xor_gc, 
		     0, y, COLUMN_LEFT_XPIXEL(clist, ctree->tree_column)
		     + clist->column[ctree->tree_column].area.width
979 980 981
		     - ctree->tree_indent * level +
		     (ctree->tree_indent - PM_SIZE) / 2, y);
      break;
Tim Janik's avatar
Tim Janik committed
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
    }
}

static void
draw_xor_rect (GtkCTree *ctree)
{
  GtkCList *clist;
  GdkPoint points[4];
  guint level;
  gint i;
  gint y;

  clist = GTK_CLIST (ctree);

  level = GTK_CTREE_ROW (ctree->drag_target)->level;

  y = ROW_TOP_YPIXEL (clist, ctree->drag_row) + clist->row_height;

1000
  switch (clist->column[ctree->tree_column].justification)
Tim Janik's avatar
Tim Janik committed
1001
    {
1002 1003 1004
    case GTK_JUSTIFY_CENTER:
    case GTK_JUSTIFY_FILL:
    case GTK_JUSTIFY_LEFT:
Tim Janik's avatar
Tim Janik committed
1005
      points[0].x =  COLUMN_LEFT_XPIXEL(clist, ctree->tree_column) + 
1006
	ctree->tree_indent * level - (ctree->tree_indent - PM_SIZE) / 2;
Tim Janik's avatar
Tim Janik committed
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
      points[0].y = y;
      points[3].x = points[0].x;
      points[3].y = y - clist->row_height - 1;
      points[1].x = clist->clist_window_width - 1;
      points[1].y = points[0].y;
      points[2].x = points[1].x;
      points[2].y = points[3].y;

      for (i = 0; i < 3; i++)
	gdk_draw_line (clist->clist_window, ctree->xor_gc,
		       points[i].x, points[i].y, points[i+1].x, points[i+1].y);

      if (ctree->tree_column > 0)
	{
	  points[0].x = COLUMN_LEFT_XPIXEL(clist, ctree->tree_column - 1) +
	    clist->column[ctree->tree_column - 1].area.width ;
	  points[0].y = y;
	  points[3].x = points[0].x;
	  points[3].y = y - clist->row_height - 1;
	  points[1].x = 0;
	  points[1].y = points[0].y;
	  points[2].x = 0;
	  points[2].y = points[3].y;

	  for (i = 0; i < 3; i++)
	    gdk_draw_line (clist->clist_window, ctree->xor_gc,
			   points[i].x, points[i].y, points[i+1].x, 
			   points[i+1].y);
	}
1036 1037
      break;
    case GTK_JUSTIFY_RIGHT:
Tim Janik's avatar
Tim Janik committed
1038
      points[0].x =  COLUMN_LEFT_XPIXEL(clist, ctree->tree_column) - 
1039
	ctree->tree_indent * level + (ctree->tree_indent - PM_SIZE) / 2  +
Tim Janik's avatar
Tim Janik committed
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
	clist->column[ctree->tree_column].area.width;
      points[0].y = y;
      points[3].x = points[0].x;
      points[3].y = y - clist->row_height - 1;
      points[1].x = 0;
      points[1].y = points[0].y;
      points[2].x = 0;
      points[2].y = points[3].y;

      for (i = 0; i < 3; i++)
	gdk_draw_line (clist->clist_window, ctree->xor_gc,
		       points[i].x, points[i].y, points[i+1].x, points[i+1].y);

      if (ctree->tree_column < clist->columns - 1)
	{
	  points[0].x = COLUMN_LEFT_XPIXEL(clist, ctree->tree_column + 1);
	  points[0].y = y;
	  points[3].x = points[0].x;
	  points[3].y = y - clist->row_height - 1;
	  points[1].x = clist->clist_window_width - 1;
	  points[1].y = points[0].y;
	  points[2].x = points[1].x;
	  points[2].y = points[3].y;

	  for (i = 0; i < 3; i++)
	    gdk_draw_line (clist->clist_window, ctree->xor_gc,
			   points[i].x, points[i].y, points[i+1].x, 
			   points[i+1].y);
	}
1069
      break;
Tim Janik's avatar
Tim Janik committed
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
    }      
}

static void
draw_row (GtkCList     *clist,
	  GdkRectangle *area,
	  gint          row,
	  GtkCListRow  *clist_row)
{
  GtkWidget *widget;
  GtkCTree  *ctree;
  GdkGC *fg_gc; 
  GdkGC *bg_gc;
  GdkRectangle row_rectangle;
  GdkRectangle cell_rectangle; 
  GdkRectangle clip_rectangle;
  GdkRectangle intersect_rectangle;
  GdkRectangle *rect;

  gint i, offset = 0, width, height, pixmap_width = 0, string_width = 0;
1090
  gint xsrc, ysrc, xdest = 0, ydest;
Tim Janik's avatar
Tim Janik committed
1091
  gboolean need_redraw = TRUE;
Lars Hamann's avatar
Lars Hamann committed
1092
  gboolean draw_pixmap = FALSE;
Tim Janik's avatar
Tim Janik committed
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

  g_return_if_fail (clist != NULL);

  /* bail now if we arn't drawable yet */
  if (!GTK_WIDGET_DRAWABLE (clist))
    return;

  if (row < 0 || row >= clist->rows)
    return;

  widget = GTK_WIDGET (clist);
  ctree  = GTK_CTREE  (clist);

  /* if the function is passed the pointer to the row instead of null,
   * it avoids this expensive lookup */
  if (!clist_row)
    clist_row = (g_list_nth (clist->row_list, row))->data;

  /* rectangle of the entire row */
  row_rectangle.x = 0;
  row_rectangle.y = ROW_TOP_YPIXEL (clist, row);
  row_rectangle.width = clist->clist_window_width;
  row_rectangle.height = clist->row_height;

  /* rectangle of the cell spacing above the row */
  cell_rectangle.x = 0;
  cell_rectangle.y = row_rectangle.y - CELL_SPACING;
  cell_rectangle.width = row_rectangle.width;
  cell_rectangle.height = CELL_SPACING;

  /* rectangle used to clip drawing operations, it's y and height
   * positions only need to be set once, so we set them once here. 
   * the x and width are set withing the drawing loop below once per
   * column */
  clip_rectangle.y = row_rectangle.y;
  clip_rectangle.height = row_rectangle.height;

  /* select GC for background rectangle */
  if (clist_row->state == GTK_STATE_SELECTED)
    {
      fg_gc = widget->style->fg_gc[GTK_STATE_SELECTED];
      bg_gc = widget->style->bg_gc[GTK_STATE_SELECTED];
    }
  else
    {
      if (clist_row->fg_set)
	{
	  gdk_gc_set_foreground (clist->fg_gc, &clist_row->foreground);
	  fg_gc = clist->fg_gc;
	}
      else
	fg_gc = widget->style->fg_gc[GTK_STATE_NORMAL];
	
      if (clist_row->bg_set)
	{
	  gdk_gc_set_foreground (clist->bg_gc, &clist_row->background);
	  bg_gc = clist->bg_gc;
	}
      else
	bg_gc = widget->style->bg_gc[GTK_STATE_PRELIGHT];

    }

  /* draw the cell borders and background */
  if (area)
    {
1159 1160
      if (gdk_rectangle_intersect (area, &cell_rectangle, 
				   &intersect_rectangle))
Tim Janik's avatar
Tim Janik committed
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
	gdk_draw_rectangle (clist->clist_window,
			    widget->style->base_gc[GTK_STATE_NORMAL],
			    TRUE,
			    intersect_rectangle.x,
			    intersect_rectangle.y,
			    intersect_rectangle.width,
			    intersect_rectangle.height);

      /* the last row has to clear it's bottom cell spacing too */
      if (clist_row == clist->row_list_end->data)
	{
	  cell_rectangle.y += clist->row_height + CELL_SPACING;

1174 1175
	  if (gdk_rectangle_intersect (area, &cell_rectangle, 
				       &intersect_rectangle))
Tim Janik's avatar
Tim Janik committed
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
	    gdk_draw_rectangle (clist->clist_window,
				widget->style->base_gc[GTK_STATE_NORMAL],
				TRUE,
				intersect_rectangle.x,
				intersect_rectangle.y,
				intersect_rectangle.width,
				intersect_rectangle.height);
	}

      if (gdk_rectangle_intersect 
	  (area, &row_rectangle, &intersect_rectangle))
	{
1188
	  if (clist_row->state == GTK_STATE_SELECTED || clist_row->bg_set)
Tim Janik's avatar
Tim Janik committed
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
	    gdk_draw_rectangle (clist->clist_window,
				bg_gc,
				TRUE,
				intersect_rectangle.x,
				intersect_rectangle.y,
				intersect_rectangle.width,
				intersect_rectangle.height);
	  else
	    gdk_window_clear_area (clist->clist_window,
				   intersect_rectangle.x,
				   intersect_rectangle.y,
				   intersect_rectangle.width,
				   intersect_rectangle.height);
	}
      else 
	need_redraw = FALSE;
    }
  else
    {
      gdk_draw_rectangle (clist->clist_window,
			  widget->style->base_gc[GTK_STATE_NORMAL],
			  TRUE,
			  cell_rectangle.x,
			  cell_rectangle.y,
			  cell_rectangle.width,
			  cell_rectangle.height);

      /* the last row has to clear it's bottom cell spacing too */
      if (clist_row == clist->row_list_end->data)
	{
	  cell_rectangle.y += clist->row_height + CELL_SPACING;

	  gdk_draw_rectangle (clist->clist_window,
			      widget->style->base_gc[GTK_STATE_NORMAL],
			      TRUE,
			      cell_rectangle.x,
			      cell_rectangle.y,
			      cell_rectangle.width,
			      cell_rectangle.height);     
	}	  

1230
      if (clist_row->state == GTK_STATE_SELECTED || clist_row->bg_set)
Tim Janik's avatar
Tim Janik committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	gdk_draw_rectangle (clist->clist_window,
			    bg_gc,
			    TRUE,
			    row_rectangle.x,
			    row_rectangle.y,
			    row_rectangle.width,
			    row_rectangle.height);
      else
	gdk_window_clear_area (clist->clist_window,
			       row_rectangle.x,
			       row_rectangle.y,
			       row_rectangle.width,
			       row_rectangle.height);
    }

  /* iterate and draw all the columns (row cells) and draw their contents */
  for (i = 0; i < clist->columns; i++)
    {
1249
	
Tim Janik's avatar
Tim Janik committed
1250 1251 1252 1253 1254 1255
      if (!need_redraw && ctree->tree_column != i)
	continue;

      clip_rectangle.x = clist->column[i].area.x + clist->hoffset;
      clip_rectangle.width = clist->column[i].area.width;

1256
      /* calculate clipping region */
Tim Janik's avatar
Tim Janik committed
1257 1258 1259 1260 1261 1262
      if (i == ctree->tree_column)
	{
	  clip_rectangle.y -= CELL_SPACING;
	  clip_rectangle.height += CELL_SPACING;
	}

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
      if (i == ctree->tree_column)
	{

	  if (clist_row->state == GTK_STATE_SELECTED)
	    {
	      gdk_gc_set_foreground (ctree->lines_gc, 
				     &GTK_WIDGET (ctree)->style->
				     fg[GTK_STATE_SELECTED]);
	      gdk_gc_set_background (ctree->lines_gc, 
				     &GTK_WIDGET (ctree)->style->
				     bg[GTK_STATE_SELECTED]);
	    }
	  else
	    {
	      gdk_gc_set_foreground (ctree->lines_gc, 
				     &GTK_WIDGET (ctree)->style->
				     fg[GTK_STATE_NORMAL]);
	      if (clist_row->bg_set)
		gdk_gc_set_background (ctree->lines_gc, 
				       &clist_row->background);
	    }

	  if (ctree->line_style == GTK_CTREE_LINES_TABBED)
	    {
	      if (clist->column[i].justification == GTK_JUSTIFY_RIGHT)
		{
		  xdest = clip_rectangle.x + clip_rectangle.width - 1 -
		    (((GtkCTreeRow *) clist_row)->level - 1) *
		    ctree->tree_indent;

		  gdk_draw_line (clist->clist_window,
				 ctree->lines_gc, 
				 -1,
				 row_rectangle.y - 1,
				 MAX (xdest - TAB_SIZE, clip_rectangle.x - 1),
				 row_rectangle.y - 1);

		  if (clist_row == clist->row_list_end->data)
		    gdk_draw_line
		      (clist->clist_window,
		       ctree->lines_gc, 
		       -1,
		       row_rectangle.y + clist->row_height,
		       MAX (clip_rectangle.x + clip_rectangle.width -
			    TAB_SIZE - 1 -
			    (((GtkCTreeRow *) clist_row)->level > 1) *
			    MIN (ctree->tree_indent / 2, TAB_SIZE),
			    clip_rectangle.x - 1),
		       row_rectangle.y + clist->row_height);

		  if (clist_row->state != GTK_STATE_SELECTED)
		    gdk_draw_rectangle
		      (clist->clist_window,
		       widget->style->bg_gc[GTK_STATE_PRELIGHT],
		       TRUE,
		       clip_rectangle.x + clip_rectangle.width,
		       row_rectangle.y,
		       CELL_SPACING + COLUMN_INSET,
		       row_rectangle.height);
		}
	      else
		{
		  xdest = clip_rectangle.x +
		    (((GtkCTreeRow *) clist_row)->level - 1) *
		    ctree->tree_indent;
		  
		  gdk_draw_line (clist->clist_window,
				 ctree->lines_gc, 
				 MIN (xdest + TAB_SIZE,
				      clip_rectangle.x + clip_rectangle.width),
				 row_rectangle.y - 1,
				 clist->clist_window_width,
				 row_rectangle.y - 1);

		  if (clist_row == clist->row_list_end->data)
		    gdk_draw_line
		      (clist->clist_window, ctree->lines_gc, 
		       MIN (clip_rectangle.x + TAB_SIZE +
			    (((GtkCTreeRow *) clist_row)->level > 1) *
			    MIN (ctree->tree_indent / 2, TAB_SIZE),
			    clip_rectangle.x + clip_rectangle.width),
		       row_rectangle.y + clist->row_height,
		       clist->clist_window_width,
		       row_rectangle.y + clist->row_height);

		  if (clist_row->state != GTK_STATE_SELECTED)
		    gdk_draw_rectangle
		      (clist->clist_window,
		       widget->style->bg_gc[GTK_STATE_PRELIGHT],
		       TRUE,
		       clip_rectangle.x - CELL_SPACING - COLUMN_INSET,
		       row_rectangle.y,
		       CELL_SPACING + COLUMN_INSET,
		       row_rectangle.height);
		}
	    }
	}

Tim Janik's avatar
Tim Janik committed
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
      if (!area)
	{
	  rect = &clip_rectangle;
	}
      else
	{

	  if (!gdk_rectangle_intersect (area, &clip_rectangle, 
					&intersect_rectangle))
	    continue;
	  rect = &intersect_rectangle;
	}

      /* calculate real width for column justification */
      switch (clist_row->cell[i].type)
	{
	case GTK_CELL_EMPTY:
	  continue;
	  break;

	case GTK_CELL_TEXT:
	  width = gdk_string_width (GTK_WIDGET (clist)->style->font,
				    GTK_CELL_TEXT (clist_row->cell[i])->text);
	  break;

	case GTK_CELL_PIXMAP:
1387 1388
	  gdk_window_get_size (GTK_CELL_PIXMAP (clist_row->cell[i])->pixmap, 
			       &width, &height);
Tim Janik's avatar
Tim Janik committed
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
	  pixmap_width = width;
	  break;

	case GTK_CELL_PIXTEXT:
	  if (i == ctree->tree_column)
	    {
	      string_width = 0;
	      width = 0;

	      if (GTK_CELL_PIXTEXT (clist_row->cell[i])->pixmap)
		gdk_window_get_size (GTK_CELL_PIXTEXT 
				     (clist_row->cell[i])->pixmap, 
				     &width, &height);

	      pixmap_width = width;
	      width += GTK_CELL_PIXTEXT (clist_row->cell[i])->spacing;

	      if (GTK_CELL_PIXTEXT (clist_row->cell[i])->text)
		string_width += gdk_string_width 
		  (GTK_WIDGET (clist)->style->font,
		   GTK_CELL_PIXTEXT(clist_row->cell[i])->text);
	      
	      width += string_width + 
		((GtkCTreeRow *)clist_row)->level * ctree->tree_indent;
	    }
	  else
	    {
	      gdk_window_get_size (GTK_CELL_PIXTEXT 
				   (clist_row->cell[i])->pixmap, 
				   &width, &height);
	      pixmap_width = width;
	      width += GTK_CELL_PIXTEXT (clist_row->cell[i])->spacing;
	      width += gdk_string_width (GTK_WIDGET (clist)->style->font,
					 GTK_CELL_PIXTEXT 
					 (clist_row->cell[i])->text);
	    }
	  break;

	case GTK_CELL_WIDGET:
	  /* unimplemented */
	  continue;
	  break;

	default:
	  continue;
	  break;
	}

      switch (clist->column[i].justification)
	{
	case GTK_JUSTIFY_LEFT:
	  offset = clip_rectangle.x;
	  break;

	case GTK_JUSTIFY_RIGHT:
	  offset = (clip_rectangle.x + clip_rectangle.width) - width;
	  break;

	case GTK_JUSTIFY_CENTER:
1448 1449
	  offset = (clip_rectangle.x + (clip_rectangle.width / 2)) 
	    - (width / 2);
Tim Janik's avatar
Tim Janik committed
1450 1451 1452
	  break;

	case GTK_JUSTIFY_FILL:
1453 1454
	  offset = (clip_rectangle.x + (clip_rectangle.width / 2)) 
	    - (width / 2);
Tim Janik's avatar
Tim Janik committed
1455 1456 1457 1458 1459 1460 1461
	  break;

	default:
	  offset = 0;
	  break;
	};

1462
      if (i == ctree->tree_column)
Tim Janik's avatar
Tim Janik committed
1463
	{
1464 1465
	  GdkGC *cgc;
	  GdkGC *tgc;
1466
	  GdkGC *mbg_gc;
Tim Janik's avatar
Tim Janik committed
1467
	  GList *work;
1468
	  GList *work2;
Tim Janik's avatar
Tim Janik committed
1469 1470 1471 1472
	  gint xoffset;
	  gint yoffset;
	  gint xcenter;
	  gint ycenter;
1473
	  gint offset_x;
Tim Janik's avatar
Tim Janik committed
1474
	  gint offset_y = 0; 
1475 1476 1477
	  gint next_level;
	  gint in;
	  GdkPoint points[6];
1478

Tim Janik's avatar
Tim Janik committed
1479 1480 1481
	  xsrc = 0;
	  ysrc = 0;

1482 1483 1484 1485
	  yoffset = (clip_rectangle.height - PM_SIZE) / 2;
	  xoffset = (ctree->tree_indent - PM_SIZE) / 2;
	  ycenter = clip_rectangle.y + (clip_rectangle.height / 2);
	  ydest = ycenter - height / 2 + clist_row->cell[i].vertical;
Tim Janik's avatar
Tim Janik committed
1486 1487 1488

	  gdk_gc_set_clip_origin (fg_gc, 0, 0