gtkctree.c 160 KB
Newer Older
Tim Janik's avatar
Tim Janik committed
1 2 3 4 5 6 7 8
/* 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
9
 * modify it under the terms of the GNU Lesser General Public
Tim Janik's avatar
Tim Janik committed
10 11 12 13 14 15
 * 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
16
 * Lesser General Public License for more details.
Tim Janik's avatar
Tim Janik committed
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
Tim Janik's avatar
Tim Janik committed
19 20 21 22 23
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

24
/*
25
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
26 27 28 29 30
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

Manish Singh's avatar
Manish Singh committed
31
#undef GDK_DISABLE_DEPRECATED
32
#undef GTK_DISABLE_DEPRECATED
Manish Singh's avatar
Manish Singh committed
33

34
#include <config.h>
Tim Janik's avatar
Tim Janik committed
35 36
#include <stdlib.h>
#include "gtkctree.h"
37
#include "gtkbindings.h"
Lars Hamann's avatar
Lars Hamann committed
38
#include "gtkmain.h"
39
#include "gtkmarshalers.h"
40
#include "gtkdnd.h"
Matthias Clasen's avatar
Matthias Clasen committed
41
#include "gtkintl.h"
42
#include <gdk/gdkkeysyms.h>
43 44

#define GTK_DISABLE_DEPRECATED
45
#include "gtkalias.h"
Tim Janik's avatar
Tim Janik committed
46 47

#define PM_SIZE                    8
48
#define TAB_SIZE                   (PM_SIZE + 6)
Tim Janik's avatar
Tim Janik committed
49
#define CELL_SPACING               1
50
#define CLIST_OPTIMUM_SIZE         64
51 52
#define COLUMN_INSET               3
#define DRAG_WIDTH                 6
Tim Janik's avatar
Tim Janik committed
53 54 55 56 57 58 59 60

#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)
61
#define COLUMN_LEFT(clist, column) ((clist)->column[(column)].area.x)
Tim Janik's avatar
Tim Janik committed
62

63 64 65 66 67
static inline gint
COLUMN_FROM_XPIXEL (GtkCList * clist,
		    gint x)
{
  gint i, cx;
68

69 70 71 72
  for (i = 0; i < clist->columns; i++)
    if (clist->column[i].visible)
      {
	cx = clist->column[i].area.x + clist->hoffset;
73

74 75 76 77
	if (x >= (cx - (COLUMN_INSET + CELL_SPACING)) &&
	    x <= (cx + clist->column[i].area.width + COLUMN_INSET))
	  return i;
      }
78

79 80 81 82
  /* no match */
  return -1;
}

83 84 85
#define CLIST_UNFROZEN(clist)     (((GtkCList*) (clist))->freeze_count == 0)
#define CLIST_REFRESH(clist)    G_STMT_START { \
  if (CLIST_UNFROZEN (clist)) \
86
    GTK_CLIST_GET_CLASS (clist)->refresh ((GtkCList*) (clist)); \
87 88
} G_STMT_END

89

90 91 92 93 94 95 96 97 98 99 100
enum {
  ARG_0,
  ARG_N_COLUMNS,
  ARG_TREE_COLUMN,
  ARG_INDENT,
  ARG_SPACING,
  ARG_SHOW_STUB,
  ARG_LINE_STYLE,
  ARG_EXPANDER_STYLE
};

101

102 103 104 105 106
static void     gtk_ctree_class_init    (GtkCTreeClass         *klass);
static void     gtk_ctree_init          (GtkCTree              *ctree);
static GObject* gtk_ctree_constructor   (GType                  type,
				         guint                  n_construct_properties,
				         GObjectConstructParam *construct_params);
107 108 109 110 111 112
static void gtk_ctree_set_arg		(GtkObject      *object,
					 GtkArg         *arg,
					 guint           arg_id);
static void gtk_ctree_get_arg      	(GtkObject      *object,
					 GtkArg         *arg,
					 guint           arg_id);
113 114
static void gtk_ctree_realize           (GtkWidget      *widget);
static void gtk_ctree_unrealize         (GtkWidget      *widget);
Tim Janik's avatar
Tim Janik committed
115 116
static gint gtk_ctree_button_press      (GtkWidget      *widget,
					 GdkEventButton *event);
117 118 119 120 121 122 123 124 125 126 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
static void ctree_attach_styles         (GtkCTree       *ctree,
					 GtkCTreeNode   *node,
					 gpointer        data);
static void ctree_detach_styles         (GtkCTree       *ctree,
					 GtkCTreeNode   *node, 
					 gpointer        data);
static gint draw_cell_pixmap            (GdkWindow      *window,
					 GdkRectangle   *clip_rectangle,
					 GdkGC          *fg_gc,
					 GdkPixmap      *pixmap,
					 GdkBitmap      *mask,
					 gint            x,
					 gint            y,
					 gint            width,
					 gint            height);
static void get_cell_style              (GtkCList       *clist,
					 GtkCListRow    *clist_row,
					 gint            state,
					 gint            column,
					 GtkStyle      **style,
					 GdkGC         **fg_gc,
					 GdkGC         **bg_gc);
static gint gtk_ctree_draw_expander     (GtkCTree       *ctree,
					 GtkCTreeRow    *ctree_row,
					 GtkStyle       *style,
					 GdkRectangle   *clip_rectangle,
					 gint            x);
static gint gtk_ctree_draw_lines        (GtkCTree       *ctree,
					 GtkCTreeRow    *ctree_row,
					 gint            row,
					 gint            column,
					 gint            state,
					 GdkRectangle   *clip_rectangle,
					 GdkRectangle   *cell_rectangle,
					 GdkRectangle   *crect,
					 GdkRectangle   *area,
					 GtkStyle       *style);
154 155 156
static void draw_row                    (GtkCList       *clist,
					 GdkRectangle   *area,
					 gint            row,
157 158 159 160 161
					 GtkCListRow    *clist_row);
static void draw_drag_highlight         (GtkCList        *clist,
					 GtkCListRow     *dest_row,
					 gint             dest_row_number,
					 GtkCListDragPos  drag_pos);
162 163
static void tree_draw_node              (GtkCTree      *ctree,
					 GtkCTreeNode  *node);
164
static void set_cell_contents           (GtkCList      *clist,
Tim Janik's avatar
Tim Janik committed
165 166
					 GtkCListRow   *clist_row,
					 gint           column,
167
					 GtkCellType    type,
Tim Janik's avatar
Tim Janik committed
168
					 const gchar   *text,
Tim Janik's avatar
Tim Janik committed
169 170 171
					 guint8         spacing,
					 GdkPixmap     *pixmap,
					 GdkBitmap     *mask);
172
static void set_node_info               (GtkCTree      *ctree,
173
					 GtkCTreeNode  *node,
Tim Janik's avatar
Tim Janik committed
174
					 const gchar   *text,
175 176 177 178 179 180 181
					 guint8         spacing,
					 GdkPixmap     *pixmap_closed,
					 GdkBitmap     *mask_closed,
					 GdkPixmap     *pixmap_opened,
					 GdkBitmap     *mask_opened,
					 gboolean       is_leaf,
					 gboolean       expanded);
182 183 184 185
static GtkCTreeRow *row_new             (GtkCTree      *ctree);
static void row_delete                  (GtkCTree      *ctree,
				 	 GtkCTreeRow   *ctree_row);
static void tree_delete                 (GtkCTree      *ctree, 
186
					 GtkCTreeNode  *node, 
187 188
					 gpointer       data);
static void tree_delete_row             (GtkCTree      *ctree, 
189
					 GtkCTreeNode  *node, 
190
					 gpointer       data);
191
static void real_clear                  (GtkCList      *clist);
192
static void tree_update_level           (GtkCTree      *ctree, 
193
					 GtkCTreeNode  *node, 
194 195
					 gpointer       data);
static void tree_select                 (GtkCTree      *ctree, 
196
					 GtkCTreeNode  *node, 
197 198
					 gpointer       data);
static void tree_unselect               (GtkCTree      *ctree, 
199
					 GtkCTreeNode  *node, 
200
				         gpointer       data);
201 202
static void real_select_all             (GtkCList      *clist);
static void real_unselect_all           (GtkCList      *clist);
203
static void tree_expand                 (GtkCTree      *ctree, 
204
					 GtkCTreeNode  *node,
205 206
					 gpointer       data);
static void tree_collapse               (GtkCTree      *ctree, 
207
					 GtkCTreeNode  *node,
208
					 gpointer       data);
209
static void tree_collapse_to_depth      (GtkCTree      *ctree, 
210
					 GtkCTreeNode  *node, 
211
					 gint           depth);
212
static void tree_toggle_expansion       (GtkCTree      *ctree,
213
					 GtkCTreeNode  *node,
214 215
					 gpointer       data);
static void change_focus_row_expansion  (GtkCTree      *ctree,
216
				         GtkCTreeExpansionType expansion);
217 218 219 220 221 222 223 224
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);
225
static void real_tree_select            (GtkCTree      *ctree,
226
					 GtkCTreeNode  *node,
227 228
					 gint           column);
static void real_tree_unselect          (GtkCTree      *ctree,
229
					 GtkCTreeNode  *node,
230 231
					 gint           column);
static void real_tree_expand            (GtkCTree      *ctree,
232
					 GtkCTreeNode  *node);
233
static void real_tree_collapse          (GtkCTree      *ctree,
234
					 GtkCTreeNode  *node);
235
static void real_tree_move              (GtkCTree      *ctree,
236 237 238
					 GtkCTreeNode  *node,
					 GtkCTreeNode  *new_parent, 
					 GtkCTreeNode  *new_sibling);
239 240 241
static void real_row_move               (GtkCList      *clist,
					 gint           source_row,
					 gint           dest_row);
242
static void gtk_ctree_link              (GtkCTree      *ctree,
243 244 245
					 GtkCTreeNode  *node,
					 GtkCTreeNode  *parent,
					 GtkCTreeNode  *sibling,
246
					 gboolean       update_focus_row);
247
static void gtk_ctree_unlink            (GtkCTree      *ctree, 
248
					 GtkCTreeNode  *node,
249
					 gboolean       update_focus_row);
250 251
static GtkCTreeNode * gtk_ctree_last_visible (GtkCTree     *ctree,
					      GtkCTreeNode *node);
252
static gboolean ctree_is_hot_spot       (GtkCTree      *ctree, 
253
					 GtkCTreeNode  *node,
254 255 256 257
					 gint           row, 
					 gint           x, 
					 gint           y);
static void tree_sort                   (GtkCTree      *ctree,
258
					 GtkCTreeNode  *node,
259
					 gpointer       data);
260 261
static void fake_unselect_all           (GtkCList      *clist,
					 gint           row);
262
static GList * selection_find           (GtkCList      *clist,
263 264 265 266 267 268
					 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, 
269
					 GtkCTreeNode  *node, 
270
					 gpointer       data);
271 272 273 274 275 276
static gint real_insert_row             (GtkCList      *clist,
					 gint           row,
					 gchar         *text[]);
static void real_remove_row             (GtkCList      *clist,
					 gint           row);
static void real_sort_list              (GtkCList      *clist);
277 278 279 280 281 282 283 284
static void cell_size_request           (GtkCList       *clist,
					 GtkCListRow    *clist_row,
					 gint            column,
					 GtkRequisition *requisition);
static void column_auto_resize          (GtkCList       *clist,
					 GtkCListRow    *clist_row,
					 gint            column,
					 gint            old_width);
Lars Hamann's avatar
Lars Hamann committed
285
static void auto_resize_columns         (GtkCList       *clist);
286

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

static gboolean check_drag               (GtkCTree         *ctree,
			                  GtkCTreeNode     *drag_source,
					  GtkCTreeNode     *drag_target,
					  GtkCListDragPos   insert_pos);
static void gtk_ctree_drag_begin         (GtkWidget        *widget,
					  GdkDragContext   *context);
static gint gtk_ctree_drag_motion        (GtkWidget        *widget,
					  GdkDragContext   *context,
					  gint              x,
					  gint              y,
					  guint             time);
static void gtk_ctree_drag_data_received (GtkWidget        *widget,
					  GdkDragContext   *context,
					  gint              x,
					  gint              y,
					  GtkSelectionData *selection_data,
					  guint             info,
					  guint32           time);
306
static void remove_grab                  (GtkCList         *clist);
Lars Hamann's avatar
Lars Hamann committed
307 308 309 310
static void drag_dest_cell               (GtkCList         *clist,
					  gint              x,
					  gint              y,
					  GtkCListDestInfo *dest_info);
311 312


Tim Janik's avatar
Tim Janik committed
313 314 315 316 317 318 319
enum
{
  TREE_SELECT_ROW,
  TREE_UNSELECT_ROW,
  TREE_EXPAND,
  TREE_COLLAPSE,
  TREE_MOVE,
320
  CHANGE_FOCUS_ROW_EXPANSION,
Tim Janik's avatar
Tim Janik committed
321 322 323 324 325 326 327 328 329
  LAST_SIGNAL
};

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


GtkType
330
gtk_ctree_get_type (void)
Tim Janik's avatar
Tim Janik committed
331 332
{
  static GtkType ctree_type = 0;
333

Tim Janik's avatar
Tim Janik committed
334 335
  if (!ctree_type)
    {
336
      static const GtkTypeInfo ctree_info =
Tim Janik's avatar
Tim Janik committed
337 338 339 340 341 342
      {
	"GtkCTree",
	sizeof (GtkCTree),
	sizeof (GtkCTreeClass),
	(GtkClassInitFunc) gtk_ctree_class_init,
	(GtkObjectInitFunc) gtk_ctree_init,
343 344
	/* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
345
        (GtkClassInitFunc) NULL,
Tim Janik's avatar
Tim Janik committed
346
      };
347

Matthias Clasen's avatar
Matthias Clasen committed
348
      I_("GtkCTree");
349
      ctree_type = gtk_type_unique (GTK_TYPE_CLIST, &ctree_info);
Tim Janik's avatar
Tim Janik committed
350
    }
351

Tim Janik's avatar
Tim Janik committed
352 353 354 355 356 357
  return ctree_type;
}

static void
gtk_ctree_class_init (GtkCTreeClass *klass)
{
358
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Tim Janik's avatar
Tim Janik committed
359 360 361
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkCListClass *clist_class;
362
  GtkBindingSet *binding_set;
363

364 365
  gobject_class->constructor = gtk_ctree_constructor;

Tim Janik's avatar
Tim Janik committed
366 367 368 369
  object_class = (GtkObjectClass *) klass;
  widget_class = (GtkWidgetClass *) klass;
  container_class = (GtkContainerClass *) klass;
  clist_class = (GtkCListClass *) klass;
370

371 372
  parent_class = gtk_type_class (GTK_TYPE_CLIST);
  container_class = gtk_type_class (GTK_TYPE_CONTAINER);
373

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
  object_class->set_arg = gtk_ctree_set_arg;
  object_class->get_arg = gtk_ctree_get_arg;

  widget_class->realize = gtk_ctree_realize;
  widget_class->unrealize = gtk_ctree_unrealize;
  widget_class->button_press_event = gtk_ctree_button_press;

  widget_class->drag_begin = gtk_ctree_drag_begin;
  widget_class->drag_motion = gtk_ctree_drag_motion;
  widget_class->drag_data_received = gtk_ctree_drag_data_received;

  clist_class->select_row = real_select_row;
  clist_class->unselect_row = real_unselect_row;
  clist_class->row_move = real_row_move;
  clist_class->undo_selection = real_undo_selection;
  clist_class->resync_selection = resync_selection;
  clist_class->selection_find = selection_find;
  clist_class->click_column = NULL;
  clist_class->draw_row = draw_row;
  clist_class->draw_drag_highlight = draw_drag_highlight;
  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;
  clist_class->insert_row = real_insert_row;
  clist_class->remove_row = real_remove_row;
  clist_class->sort_list = real_sort_list;
  clist_class->set_cell_contents = set_cell_contents;
  clist_class->cell_size_request = cell_size_request;

  klass->tree_select_row = real_tree_select;
  klass->tree_unselect_row = real_tree_unselect;
  klass->tree_expand = real_tree_expand;
  klass->tree_collapse = real_tree_collapse;
  klass->tree_move = real_tree_move;
  klass->change_focus_row_expansion = change_focus_row_expansion;

411
  gtk_object_add_arg_type ("GtkCTree::n-columns", /* overrides GtkCList::n_columns!! */
412
			   GTK_TYPE_UINT,
413
			   GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME,
414
			   ARG_N_COLUMNS);
415
  gtk_object_add_arg_type ("GtkCTree::tree-column",
416
			   GTK_TYPE_UINT,
417
			   GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME,
418 419 420
			   ARG_TREE_COLUMN);
  gtk_object_add_arg_type ("GtkCTree::indent",
			   GTK_TYPE_UINT,
421
			   GTK_ARG_READWRITE | G_PARAM_STATIC_NAME,
422 423 424
			   ARG_INDENT);
  gtk_object_add_arg_type ("GtkCTree::spacing",
			   GTK_TYPE_UINT,
425
			   GTK_ARG_READWRITE | G_PARAM_STATIC_NAME,
426
			   ARG_SPACING);
427
  gtk_object_add_arg_type ("GtkCTree::show-stub",
428
			   GTK_TYPE_BOOL,
429
			   GTK_ARG_READWRITE | G_PARAM_STATIC_NAME,
430
			   ARG_SHOW_STUB);
431
  gtk_object_add_arg_type ("GtkCTree::line-style",
432
			   GTK_TYPE_CTREE_LINE_STYLE,
433
			   GTK_ARG_READWRITE | G_PARAM_STATIC_NAME,
434
			   ARG_LINE_STYLE);
435
  gtk_object_add_arg_type ("GtkCTree::expander-style",
436
			   GTK_TYPE_CTREE_EXPANDER_STYLE,
437
			   GTK_ARG_READWRITE | G_PARAM_STATIC_NAME,
438
			   ARG_EXPANDER_STYLE);
439

Tim Janik's avatar
Tim Janik committed
440
  ctree_signals[TREE_SELECT_ROW] =
Matthias Clasen's avatar
Matthias Clasen committed
441
    gtk_signal_new (I_("tree_select_row"),
Tim Janik's avatar
Tim Janik committed
442
		    GTK_RUN_FIRST,
443
		    GTK_CLASS_TYPE (object_class),
Tim Janik's avatar
Tim Janik committed
444
		    GTK_SIGNAL_OFFSET (GtkCTreeClass, tree_select_row),
445
		    _gtk_marshal_VOID__POINTER_INT,
James Henstridge's avatar
James Henstridge committed
446
		    GTK_TYPE_NONE, 2,
447
		    GTK_TYPE_CTREE_NODE,
James Henstridge's avatar
James Henstridge committed
448
		    GTK_TYPE_INT);
Tim Janik's avatar
Tim Janik committed
449
  ctree_signals[TREE_UNSELECT_ROW] =
Matthias Clasen's avatar
Matthias Clasen committed
450
    gtk_signal_new (I_("tree_unselect_row"),
Tim Janik's avatar
Tim Janik committed
451
		    GTK_RUN_FIRST,
452
		    GTK_CLASS_TYPE (object_class),
Tim Janik's avatar
Tim Janik committed
453
		    GTK_SIGNAL_OFFSET (GtkCTreeClass, tree_unselect_row),
454
		    _gtk_marshal_VOID__POINTER_INT,
James Henstridge's avatar
James Henstridge committed
455
		    GTK_TYPE_NONE, 2,
456
		    GTK_TYPE_CTREE_NODE,
James Henstridge's avatar
James Henstridge committed
457
		    GTK_TYPE_INT);
Tim Janik's avatar
Tim Janik committed
458
  ctree_signals[TREE_EXPAND] =
Matthias Clasen's avatar
Matthias Clasen committed
459
    gtk_signal_new (I_("tree_expand"),
Tim Janik's avatar
Tim Janik committed
460
		    GTK_RUN_LAST,
461
		    GTK_CLASS_TYPE (object_class),
Tim Janik's avatar
Tim Janik committed
462
		    GTK_SIGNAL_OFFSET (GtkCTreeClass, tree_expand),
463
		    _gtk_marshal_VOID__POINTER,
James Henstridge's avatar
James Henstridge committed
464
		    GTK_TYPE_NONE, 1,
465
		    GTK_TYPE_CTREE_NODE);
Tim Janik's avatar
Tim Janik committed
466
  ctree_signals[TREE_COLLAPSE] =
Matthias Clasen's avatar
Matthias Clasen committed
467
    gtk_signal_new (I_("tree_collapse"),
Tim Janik's avatar
Tim Janik committed
468
		    GTK_RUN_LAST,
469
		    GTK_CLASS_TYPE (object_class),
Tim Janik's avatar
Tim Janik committed
470
		    GTK_SIGNAL_OFFSET (GtkCTreeClass, tree_collapse),
471
		    _gtk_marshal_VOID__POINTER,
James Henstridge's avatar
James Henstridge committed
472
		    GTK_TYPE_NONE, 1,
473
		    GTK_TYPE_CTREE_NODE);
Tim Janik's avatar
Tim Janik committed
474
  ctree_signals[TREE_MOVE] =
Matthias Clasen's avatar
Matthias Clasen committed
475
    gtk_signal_new (I_("tree_move"),
Tim Janik's avatar
Tim Janik committed
476
		    GTK_RUN_LAST,
477
		    GTK_CLASS_TYPE (object_class),
Tim Janik's avatar
Tim Janik committed
478
		    GTK_SIGNAL_OFFSET (GtkCTreeClass, tree_move),
479
		    _gtk_marshal_VOID__POINTER_POINTER_POINTER,
James Henstridge's avatar
James Henstridge committed
480
		    GTK_TYPE_NONE, 3,
481 482 483
		    GTK_TYPE_CTREE_NODE,
		    GTK_TYPE_CTREE_NODE,
		    GTK_TYPE_CTREE_NODE);
484
  ctree_signals[CHANGE_FOCUS_ROW_EXPANSION] =
Matthias Clasen's avatar
Matthias Clasen committed
485
    gtk_signal_new (I_("change_focus_row_expansion"),
486
		    GTK_RUN_LAST | GTK_RUN_ACTION,
487
		    GTK_CLASS_TYPE (object_class),
488 489
		    GTK_SIGNAL_OFFSET (GtkCTreeClass,
				       change_focus_row_expansion),
490
		    _gtk_marshal_VOID__ENUM,
491
		    GTK_TYPE_NONE, 1, GTK_TYPE_CTREE_EXPANSION_TYPE);
492

493
  binding_set = gtk_binding_set_by_class (klass);
494
  gtk_binding_entry_add_signal (binding_set,
495
				GDK_plus, 0,
496 497 498
				"change_focus_row_expansion", 1,
				GTK_TYPE_ENUM, GTK_CTREE_EXPANSION_EXPAND);
  gtk_binding_entry_add_signal (binding_set,
499
				GDK_plus, GDK_CONTROL_MASK,
500 501
				"change_focus_row_expansion", 1,
				GTK_TYPE_ENUM, GTK_CTREE_EXPANSION_EXPAND_RECURSIVE);
502

503 504 505 506 507 508 509
  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,
510 511
				GTK_TYPE_ENUM, GTK_CTREE_EXPANSION_EXPAND_RECURSIVE);
  
512
  gtk_binding_entry_add_signal (binding_set,
513
				GDK_minus, 0,
514 515
				"change_focus_row_expansion", 1,
				GTK_TYPE_ENUM, GTK_CTREE_EXPANSION_COLLAPSE);
516
  gtk_binding_entry_add_signal (binding_set,
517
                                GDK_minus, GDK_CONTROL_MASK,
518 519 520
				"change_focus_row_expansion", 1,
				GTK_TYPE_ENUM,
				GTK_CTREE_EXPANSION_COLLAPSE_RECURSIVE);
521 522 523 524 525 526 527 528 529 530
  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,
531
				GDK_equal, 0,
532 533
				"change_focus_row_expansion", 1,
				GTK_TYPE_ENUM, GTK_CTREE_EXPANSION_TOGGLE);
534
  gtk_binding_entry_add_signal (binding_set,
535 536 537
				GDK_KP_Equal, 0,
				"change_focus_row_expansion", 1,
				GTK_TYPE_ENUM, GTK_CTREE_EXPANSION_TOGGLE);
538 539 540 541
  gtk_binding_entry_add_signal (binding_set,
				GDK_KP_Multiply, 0,
				"change_focus_row_expansion", 1,
				GTK_TYPE_ENUM, GTK_CTREE_EXPANSION_TOGGLE);
542 543 544 545
  gtk_binding_entry_add_signal (binding_set,
				GDK_asterisk, 0,
				"change_focus_row_expansion", 1,
				GTK_TYPE_ENUM, GTK_CTREE_EXPANSION_TOGGLE);
546 547 548 549 550
  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);
551 552 553 554 555
  gtk_binding_entry_add_signal (binding_set,
				GDK_asterisk, GDK_CONTROL_MASK,
				"change_focus_row_expansion", 1,
				GTK_TYPE_ENUM,
				GTK_CTREE_EXPANSION_TOGGLE_RECURSIVE);  
Tim Janik's avatar
Tim Janik committed
556 557
}

558 559 560 561 562 563
static void
gtk_ctree_set_arg (GtkObject      *object,
		   GtkArg         *arg,
		   guint           arg_id)
{
  GtkCTree *ctree;
564
  GtkCList *clist;
565

566
  ctree = GTK_CTREE (object);
567
  clist = GTK_CLIST (ctree);
568

569 570
  switch (arg_id)
    {
571 572 573
    case ARG_N_COLUMNS: /* construct-only arg, only set at construction time */
      clist->columns = MAX (1, GTK_VALUE_UINT (*arg));
      ctree->tree_column = CLAMP (ctree->tree_column, 0, clist->columns);
574
      break;
575 576
    case ARG_TREE_COLUMN: /* construct-only arg, only set at construction time */
      ctree->tree_column = GTK_VALUE_UINT (*arg);
577
      ctree->tree_column = CLAMP (ctree->tree_column, 0, clist->columns);
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
      break;
    case ARG_INDENT:
      gtk_ctree_set_indent (ctree, GTK_VALUE_UINT (*arg));
      break;
    case ARG_SPACING:
      gtk_ctree_set_spacing (ctree, GTK_VALUE_UINT (*arg));
      break;
    case ARG_SHOW_STUB:
      gtk_ctree_set_show_stub (ctree, GTK_VALUE_BOOL (*arg));
      break;
    case ARG_LINE_STYLE:
      gtk_ctree_set_line_style (ctree, GTK_VALUE_ENUM (*arg));
      break;
    case ARG_EXPANDER_STYLE:
      gtk_ctree_set_expander_style (ctree, GTK_VALUE_ENUM (*arg));
      break;
    default:
      break;
    }
}

static void
gtk_ctree_get_arg (GtkObject      *object,
		   GtkArg         *arg,
		   guint           arg_id)
{
  GtkCTree *ctree;
605

606
  ctree = GTK_CTREE (object);
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
  switch (arg_id)
    {
    case ARG_N_COLUMNS:
      GTK_VALUE_UINT (*arg) = GTK_CLIST (ctree)->columns;
      break;
    case ARG_TREE_COLUMN:
      GTK_VALUE_UINT (*arg) = ctree->tree_column;
      break;
    case ARG_INDENT:
      GTK_VALUE_UINT (*arg) = ctree->tree_indent;
      break;
    case ARG_SPACING:
      GTK_VALUE_UINT (*arg) = ctree->tree_spacing;
      break;
    case ARG_SHOW_STUB:
      GTK_VALUE_BOOL (*arg) = ctree->show_stub;
      break;
    case ARG_LINE_STYLE:
      GTK_VALUE_ENUM (*arg) = ctree->line_style;
      break;
    case ARG_EXPANDER_STYLE:
      GTK_VALUE_ENUM (*arg) = ctree->expander_style;
      break;
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}

Tim Janik's avatar
Tim Janik committed
637 638 639
static void
gtk_ctree_init (GtkCTree *ctree)
{
640
  GtkCList *clist;
641

642 643
  GTK_CLIST_SET_FLAG (ctree, CLIST_DRAW_DRAG_RECT);
  GTK_CLIST_SET_FLAG (ctree, CLIST_DRAW_DRAG_LINE);
644

645
  clist = GTK_CLIST (ctree);
646

Tim Janik's avatar
Tim Janik committed
647
  ctree->tree_indent    = 20;
648
  ctree->tree_spacing   = 5;
Tim Janik's avatar
Tim Janik committed
649
  ctree->tree_column    = 0;
650
  ctree->line_style     = GTK_CTREE_LINES_SOLID;
651
  ctree->expander_style = GTK_CTREE_EXPANDER_SQUARE;
652
  ctree->drag_compare   = NULL;
653
  ctree->show_stub      = TRUE;
654

655
  clist->button_actions[0] |= GTK_BUTTON_EXPANDS;
Tim Janik's avatar
Tim Janik committed
656 657
}

658 659 660 661 662 663 664
static void
ctree_attach_styles (GtkCTree     *ctree,
		     GtkCTreeNode *node,
		     gpointer      data)
{
  GtkCList *clist;
  gint i;
665

666
  clist = GTK_CLIST (ctree);
667

668 669 670
  if (GTK_CTREE_ROW (node)->row.style)
    GTK_CTREE_ROW (node)->row.style =
      gtk_style_attach (GTK_CTREE_ROW (node)->row.style, clist->clist_window);
671

672 673 674
  if (GTK_CTREE_ROW (node)->row.fg_set || GTK_CTREE_ROW (node)->row.bg_set)
    {
      GdkColormap *colormap;
675

676 677 678 679 680 681
      colormap = gtk_widget_get_colormap (GTK_WIDGET (ctree));
      if (GTK_CTREE_ROW (node)->row.fg_set)
	gdk_color_alloc (colormap, &(GTK_CTREE_ROW (node)->row.foreground));
      if (GTK_CTREE_ROW (node)->row.bg_set)
	gdk_color_alloc (colormap, &(GTK_CTREE_ROW (node)->row.background));
    }
682

683 684 685 686 687 688 689 690 691 692 693 694 695 696
  for (i = 0; i < clist->columns; i++)
    if  (GTK_CTREE_ROW (node)->row.cell[i].style)
      GTK_CTREE_ROW (node)->row.cell[i].style =
	gtk_style_attach (GTK_CTREE_ROW (node)->row.cell[i].style,
			  clist->clist_window);
}

static void
ctree_detach_styles (GtkCTree     *ctree,
		     GtkCTreeNode *node,
		     gpointer      data)
{
  GtkCList *clist;
  gint i;
697

698
  clist = GTK_CLIST (ctree);
699

700 701 702 703 704 705 706
  if (GTK_CTREE_ROW (node)->row.style)
    gtk_style_detach (GTK_CTREE_ROW (node)->row.style);
  for (i = 0; i < clist->columns; i++)
    if  (GTK_CTREE_ROW (node)->row.cell[i].style)
      gtk_style_detach (GTK_CTREE_ROW (node)->row.cell[i].style);
}

Tim Janik's avatar
Tim Janik committed
707 708 709 710
static void
gtk_ctree_realize (GtkWidget *widget)
{
  GtkCTree *ctree;
711
  GtkCList *clist;
Tim Janik's avatar
Tim Janik committed
712
  GdkGCValues values;
713 714 715
  GtkCTreeNode *node;
  GtkCTreeNode *child;
  gint i;
716

717
  g_return_if_fail (GTK_IS_CTREE (widget));
718

719
  GTK_WIDGET_CLASS (parent_class)->realize (widget);
720

721
  ctree = GTK_CTREE (widget);
722
  clist = GTK_CLIST (widget);
723

724 725 726 727 728 729 730 731 732
  node = GTK_CTREE_NODE (clist->row_list);
  for (i = 0; i < clist->rows; i++)
    {
      if (GTK_CTREE_ROW (node)->children && !GTK_CTREE_ROW (node)->expanded)
	for (child = GTK_CTREE_ROW (node)->children; child;
	     child = GTK_CTREE_ROW (child)->sibling)
	  gtk_ctree_pre_recursive (ctree, child, ctree_attach_styles, NULL);
      node = GTK_CTREE_NODE_NEXT (node);
    }
733

Tim Janik's avatar
Tim Janik committed
734
  values.foreground = widget->style->fg[GTK_STATE_NORMAL];
735
  values.background = widget->style->base[GTK_STATE_NORMAL];
Tim Janik's avatar
Tim Janik committed
736 737 738 739 740 741 742 743
  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);
744

Tim Janik's avatar
Tim Janik committed
745 746 747
  if (ctree->line_style == GTK_CTREE_LINES_DOTTED)
    {
      gdk_gc_set_line_attributes (ctree->lines_gc, 1, 
748
				  GDK_LINE_ON_OFF_DASH, GDK_CAP_BUTT, GDK_JOIN_MITER);
749
      gdk_gc_set_dashes (ctree->lines_gc, 0, "\1\1", 2);
Tim Janik's avatar
Tim Janik committed
750 751 752 753 754 755 756
    }
}

static void
gtk_ctree_unrealize (GtkWidget *widget)
{
  GtkCTree *ctree;
757
  GtkCList *clist;
758

759
  g_return_if_fail (GTK_IS_CTREE (widget));
760

761
  GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
762

763
  ctree = GTK_CTREE (widget);
764
  clist = GTK_CLIST (widget);
765

766 767 768 769 770
  if (GTK_WIDGET_REALIZED (widget))
    {
      GtkCTreeNode *node;
      GtkCTreeNode *child;
      gint i;
771

772 773 774 775 776 777 778 779 780 781 782
      node = GTK_CTREE_NODE (clist->row_list);
      for (i = 0; i < clist->rows; i++)
	{
	  if (GTK_CTREE_ROW (node)->children &&
	      !GTK_CTREE_ROW (node)->expanded)
	    for (child = GTK_CTREE_ROW (node)->children; child;
		 child = GTK_CTREE_ROW (child)->sibling)
	      gtk_ctree_pre_recursive(ctree, child, ctree_detach_styles, NULL);
	  node = GTK_CTREE_NODE_NEXT (node);
	}
    }
783

Tim Janik's avatar
Tim Janik committed
784 785 786 787 788 789 790 791 792
  gdk_gc_destroy (ctree->lines_gc);
}

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

Tim Janik's avatar
Tim Janik committed
795 796
  g_return_val_if_fail (GTK_IS_CTREE (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
797

Tim Janik's avatar
Tim Janik committed
798 799
  ctree = GTK_CTREE (widget);
  clist = GTK_CLIST (widget);
800

801
  button_actions = clist->button_actions[event->button - 1];
802

803 804
  if (button_actions == GTK_BUTTON_IGNORED)
    return FALSE;
805

Tim Janik's avatar
Tim Janik committed
806 807
  if (event->window == clist->clist_window)
    {
808
      GtkCTreeNode *work;
Tim Janik's avatar
Tim Janik committed
809 810 811 812
      gint x;
      gint y;
      gint row;
      gint column;
813

Tim Janik's avatar
Tim Janik committed
814 815
      x = event->x;
      y = event->y;
816

817 818
      if (!gtk_clist_get_selection_info (clist, x, y, &row, &column))
	return FALSE;
819

820
      work = GTK_CTREE_NODE (g_list_nth (clist->row_list, row));
821
	  
822 823 824 825
      if (button_actions & GTK_BUTTON_EXPANDS &&
	  (GTK_CTREE_ROW (work)->children && !GTK_CTREE_ROW (work)->is_leaf  &&
	   (event->type == GDK_2BUTTON_PRESS ||
	    ctree_is_hot_spot (ctree, work, row, x, y))))
826 827 828 829 830
	{
	  if (GTK_CTREE_ROW (work)->expanded)
	    gtk_ctree_collapse (ctree, work);
	  else
	    gtk_ctree_expand (ctree, work);
831

832
	  return TRUE;
Tim Janik's avatar
Tim Janik committed
833 834
	}
    }
835
  
836
  return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
Tim Janik's avatar
Tim Janik committed
837 838 839
}

static void
840 841 842 843
draw_drag_highlight (GtkCList        *clist,
		     GtkCListRow     *dest_row,
		     gint             dest_row_number,
		     GtkCListDragPos  drag_pos)
Tim Janik's avatar
Tim Janik committed
844
{
845 846
  GtkCTree *ctree;
  GdkPoint points[4];
Tim Janik's avatar
Tim Janik committed
847
  gint level;
848
  gint i;
Tim Janik's avatar
Tim Janik committed
849
  gint y = 0;
850

851
  g_return_if_fail (GTK_IS_CTREE (clist));
852

853
  ctree = GTK_CTREE (clist);
854

855
  level = ((GtkCTreeRow *)(dest_row))->level;
856

857
  y = ROW_TOP_YPIXEL (clist, dest_row_number) - 1;
858

859 860 861 862 863 864 865
  switch (drag_pos)
    {
    case GTK_CLIST_DRAG_NONE:
      break;
    case GTK_CLIST_DRAG_AFTER:
      y += clist->row_height + 1;
    case GTK_CLIST_DRAG_BEFORE:
866
      
867 868 869 870 871 872 873 874 875 876 877 878
      if (clist->column[ctree->tree_column].visible)
	switch (clist->column[ctree->tree_column].justification)
	  {
	  case GTK_JUSTIFY_CENTER:
	  case GTK_JUSTIFY_FILL:
	  case GTK_JUSTIFY_LEFT:
	    if (ctree->tree_column > 0)
	      gdk_draw_line (clist->clist_window, clist->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);
879

880 881 882 883 884 885 886 887 888 889 890 891 892
	    gdk_draw_line (clist->clist_window, clist->xor_gc, 
			   COLUMN_LEFT_XPIXEL(clist, ctree->tree_column) + 
			   ctree->tree_indent * level -
			   (ctree->tree_indent - PM_SIZE) / 2, y,
			   GTK_WIDGET (ctree)->allocation.width, y);
	    break;
	  case GTK_JUSTIFY_RIGHT:
	    if (ctree->tree_column < clist->columns - 1)
	      gdk_draw_line (clist->clist_window, clist->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);
893
      
894 895 896 897 898 899 900 901
	    gdk_draw_line (clist->clist_window, clist->xor_gc, 
			   0, y, COLUMN_LEFT_XPIXEL(clist, ctree->tree_column)
			   + clist->column[ctree->tree_column].area.width -
			   ctree->tree_indent * level +
			   (ctree->tree_indent - PM_SIZE) / 2, y);
	    break;
	  }
      else
902
	gdk_draw_line (clist->clist_window, clist->xor_gc, 
903 904 905 906
		       0, y, clist->clist_window_width, y);
      break;
    case GTK_CLIST_DRAG_INTO:
      y = ROW_TOP_YPIXEL (clist, dest_row_number) + clist->row_height;
907

908 909
      if (clist->column[ctree->tree_column].visible)
	switch (clist->column[ctree->tree_column].justification)
910
	  {
911 912 913 914 915
	  case GTK_JUSTIFY_CENTER:
	  case GTK_JUSTIFY_FILL:
	  case GTK_JUSTIFY_LEFT:
	    points[0].x =  COLUMN_LEFT_XPIXEL(clist, ctree->tree_column) + 
	      ctree->tree_indent * level - (ctree->tree_indent - PM_SIZE) / 2;
916 917 918
	    points[0].y = y;
	    points[3].x = points[0].x;
	    points[3].y = y - clist->row_height - 1;
919
	    points[1].x = clist->clist_window_width - 1;
920
	    points[1].y = points[0].y;
921
	    points[2].x = points[1].x;
922
	    points[2].y = points[3].y;
923

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

929 930 931 932 933 934 935 936 937 938 939 940
	    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;
941

942 943 944 945 946 947 948 949 950 951
		for (i = 0; i < 3; i++)
		  gdk_draw_line (clist->clist_window, clist->xor_gc,
				 points[i].x, points[i].y, points[i+1].x, 
				 points[i+1].y);
	      }
	    break;
	  case GTK_JUSTIFY_RIGHT:
	    points[0].x =  COLUMN_LEFT_XPIXEL(clist, ctree->tree_column) - 
	      ctree->tree_indent * level + (ctree->tree_indent - PM_SIZE) / 2 +
	      clist->column[ctree->tree_column].area.width;
952 953 954
	    points[0].y = y;
	    points[3].x = points[0].x;
	    points[3].y = y - clist->row_height - 1;
955
	    points[1].x = 0;
956
	    points[1].y = points[0].y;
957
	    points[2].x = 0;
958
	    points[2].y = points[3].y;