gtklist.c 61.8 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10
 * 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
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * 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/. 
 */

27 28
#undef GTK_DISABLE_DEPRECATED

29
#include <config.h>
30 31
#include <string.h> /* memset */

Elliot Lee's avatar
Elliot Lee committed
32 33 34 35
#include "gtklist.h"
#include "gtklistitem.h"
#include "gtkmain.h"
#include "gtksignal.h"
36
#include "gtklabel.h"
37
#include "gtkmarshalers.h"
Matthias Clasen's avatar
Matthias Clasen committed
38
#include "gtkintl.h"
39
#include "gtkalias.h"
Elliot Lee's avatar
Elliot Lee committed
40 41 42 43 44 45 46 47

enum {
  SELECTION_CHANGED,
  SELECT_CHILD,
  UNSELECT_CHILD,
  LAST_SIGNAL
};

48 49 50 51 52
enum {
  ARG_0,
  ARG_SELECTION_MODE
};

53 54
#define SCROLL_TIME  100

55
/*** GtkList Methods ***/
56 57
static void gtk_list_class_init	     (GtkListClass   *klass);
static void gtk_list_init	     (GtkList	     *list);
58 59 60 61 62 63
static void gtk_list_set_arg         (GtkObject      *object,
				      GtkArg         *arg,
				      guint           arg_id);
static void gtk_list_get_arg         (GtkObject      *object,
				      GtkArg         *arg,
				      guint           arg_id);
64
/*** GtkObject Methods ***/
Tim Janik's avatar
Tim Janik committed
65
static void gtk_list_dispose	     (GObject	     *object);
66

67
/*** GtkWidget Methods ***/
68 69 70 71 72
static void gtk_list_size_request    (GtkWidget	     *widget,
				      GtkRequisition *requisition);
static void gtk_list_size_allocate   (GtkWidget	     *widget,
				      GtkAllocation  *allocation);
static void gtk_list_realize	     (GtkWidget	     *widget);
73
static void gtk_list_unmap	     (GtkWidget	     *widget);
Owen Taylor's avatar
Owen Taylor committed
74 75
static void gtk_list_style_set	     (GtkWidget      *widget,
				      GtkStyle       *previous_style);
76 77
static gint gtk_list_motion_notify   (GtkWidget      *widget,
				      GdkEventMotion *event);
Elliot Lee's avatar
Elliot Lee committed
78 79
static gint gtk_list_button_press    (GtkWidget      *widget,
				      GdkEventButton *event);
80
static gint gtk_list_button_release  (GtkWidget	     *widget,
Elliot Lee's avatar
Elliot Lee committed
81
				      GdkEventButton *event);
82

83 84 85
static gboolean gtk_list_focus       (GtkWidget        *widget,
                                      GtkDirectionType  direction);

86
/*** GtkContainer Methods ***/
87 88 89 90 91 92 93 94 95 96 97 98
static void gtk_list_add	     (GtkContainer     *container,
				      GtkWidget        *widget);
static void gtk_list_remove	     (GtkContainer     *container,
				      GtkWidget        *widget);
static void gtk_list_forall	     (GtkContainer     *container,
				      gboolean          include_internals,
				      GtkCallback       callback,
				      gpointer          callback_data);
static GtkType gtk_list_child_type   (GtkContainer     *container);
static void gtk_list_set_focus_child (GtkContainer     *container,
				      GtkWidget        *widget);

99
/*** GtkList Private Functions ***/
100 101 102 103 104 105 106 107 108
static void gtk_list_move_focus_child      (GtkList       *list,
					    GtkScrollType  scroll_type,
					    gfloat         position);
static gint gtk_list_horizontal_timeout    (GtkWidget     *list);
static gint gtk_list_vertical_timeout      (GtkWidget     *list);
static void gtk_list_remove_items_internal (GtkList       *list,
					    GList         *items,
					    gboolean       no_unref);

109
/*** GtkList Selection Methods ***/
110 111 112 113 114
static void gtk_real_list_select_child	        (GtkList   *list,
						 GtkWidget *child);
static void gtk_real_list_unselect_child        (GtkList   *list,
						 GtkWidget *child);

115
/*** GtkList Selection Functions ***/
116 117 118 119 120 121 122 123 124 125
static void gtk_list_set_anchor                 (GtkList   *list,
					         gboolean   add_mode,
					         gint       anchor,
					         GtkWidget *undo_focus_child);
static void gtk_list_fake_unselect_all          (GtkList   *list,
			                         GtkWidget *item);
static void gtk_list_fake_toggle_row            (GtkList   *list,
					         GtkWidget *item);
static void gtk_list_update_extended_selection  (GtkList   *list,
					         gint       row);
126
static void gtk_list_reset_extended_selection   (GtkList   *list);
127

128
/*** GtkListItem Signal Functions ***/
129 130 131
static void gtk_list_signal_drag_begin         (GtkWidget      *widget,
						GdkDragContext *context,
						GtkList        *list);
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 162 163 164
static void gtk_list_signal_toggle_focus_row   (GtkListItem   *list_item,
						GtkList       *list);
static void gtk_list_signal_select_all         (GtkListItem   *list_item,
						GtkList       *list);
static void gtk_list_signal_unselect_all       (GtkListItem   *list_item,
						GtkList       *list);
static void gtk_list_signal_undo_selection     (GtkListItem   *list_item,
						GtkList       *list);
static void gtk_list_signal_start_selection    (GtkListItem   *list_item,
						GtkList       *list);
static void gtk_list_signal_end_selection      (GtkListItem   *list_item,
						GtkList       *list);
static void gtk_list_signal_extend_selection   (GtkListItem   *list_item,
						GtkScrollType  scroll_type,
						gfloat         position,
						gboolean       auto_start_selection,
						GtkList       *list);
static void gtk_list_signal_scroll_horizontal  (GtkListItem   *list_item,
						GtkScrollType  scroll_type,
						gfloat         position,
						GtkList       *list);
static void gtk_list_signal_scroll_vertical    (GtkListItem   *list_item,
						GtkScrollType  scroll_type,
						gfloat         position,
						GtkList       *list);
static void gtk_list_signal_toggle_add_mode    (GtkListItem   *list_item,
						GtkList       *list);
static void gtk_list_signal_item_select        (GtkListItem   *list_item,
						GtkList       *list);
static void gtk_list_signal_item_deselect      (GtkListItem   *list_item,
						GtkList       *list);
static void gtk_list_signal_item_toggle        (GtkListItem   *list_item,
						GtkList       *list);
Elliot Lee's avatar
Elliot Lee committed
165 166


167 168 169 170
static void gtk_list_drag_begin (GtkWidget      *widget,
				 GdkDragContext *context);


Elliot Lee's avatar
Elliot Lee committed
171
static GtkContainerClass *parent_class = NULL;
172
static guint list_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
173

174
static const gchar vadjustment_key[] = "gtk-vadjustment";
175
static guint        vadjustment_key_id = 0;
176
static const gchar hadjustment_key[] = "gtk-hadjustment";
177
static guint        hadjustment_key_id = 0;
Elliot Lee's avatar
Elliot Lee committed
178

179
GtkType
180
gtk_list_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
181
{
182
  static GtkType list_type = 0;
Elliot Lee's avatar
Elliot Lee committed
183 184 185

  if (!list_type)
    {
186
      static const GtkTypeInfo list_info =
Elliot Lee's avatar
Elliot Lee committed
187 188 189 190 191 192
      {
	"GtkList",
	sizeof (GtkList),
	sizeof (GtkListClass),
	(GtkClassInitFunc) gtk_list_class_init,
	(GtkObjectInitFunc) gtk_list_init,
193 194
	/* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
195
        (GtkClassInitFunc) NULL,
Elliot Lee's avatar
Elliot Lee committed
196 197
      };

Matthias Clasen's avatar
Matthias Clasen committed
198
      I_("GtkList");
199
      list_type = gtk_type_unique (GTK_TYPE_CONTAINER, &list_info);
Elliot Lee's avatar
Elliot Lee committed
200 201 202 203 204 205 206 207
    }

  return list_type;
}

static void
gtk_list_class_init (GtkListClass *class)
{
208
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
209 210 211 212 213 214 215 216
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

  object_class = (GtkObjectClass*) class;
  widget_class = (GtkWidgetClass*) class;
  container_class = (GtkContainerClass*) class;

217
  parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
Elliot Lee's avatar
Elliot Lee committed
218

219 220 221
  vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
  hadjustment_key_id = g_quark_from_static_string (hadjustment_key);

Tim Janik's avatar
Tim Janik committed
222
  gobject_class->dispose = gtk_list_dispose;
223

Elliot Lee's avatar
Elliot Lee committed
224

225 226
  object_class->set_arg = gtk_list_set_arg;
  object_class->get_arg = gtk_list_get_arg;
Elliot Lee's avatar
Elliot Lee committed
227 228

  widget_class->unmap = gtk_list_unmap;
Owen Taylor's avatar
Owen Taylor committed
229
  widget_class->style_set = gtk_list_style_set;
Elliot Lee's avatar
Elliot Lee committed
230 231 232
  widget_class->realize = gtk_list_realize;
  widget_class->button_press_event = gtk_list_button_press;
  widget_class->button_release_event = gtk_list_button_release;
233
  widget_class->motion_notify_event = gtk_list_motion_notify;
Elliot Lee's avatar
Elliot Lee committed
234 235
  widget_class->size_request = gtk_list_size_request;
  widget_class->size_allocate = gtk_list_size_allocate;
236
  widget_class->drag_begin = gtk_list_drag_begin;
237 238
  widget_class->focus = gtk_list_focus;
  
Elliot Lee's avatar
Elliot Lee committed
239 240
  container_class->add = gtk_list_add;
  container_class->remove = gtk_list_remove;
241
  container_class->forall = gtk_list_forall;
242
  container_class->child_type = gtk_list_child_type;
243
  container_class->set_focus_child = gtk_list_set_focus_child;
Elliot Lee's avatar
Elliot Lee committed
244 245 246 247

  class->selection_changed = NULL;
  class->select_child = gtk_real_list_select_child;
  class->unselect_child = gtk_real_list_unselect_child;
248 249

  list_signals[SELECTION_CHANGED] =
250
    gtk_signal_new (I_("selection-changed"),
251 252 253
		    GTK_RUN_FIRST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkListClass, selection_changed),
254
		    _gtk_marshal_VOID__VOID,
255 256
		    GTK_TYPE_NONE, 0);
  list_signals[SELECT_CHILD] =
Matthias Clasen's avatar
Matthias Clasen committed
257
    gtk_signal_new (I_("select_child"),
258 259 260
		    GTK_RUN_FIRST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkListClass, select_child),
261
		    _gtk_marshal_VOID__OBJECT,
262 263 264
		    GTK_TYPE_NONE, 1,
		    GTK_TYPE_WIDGET);
  list_signals[UNSELECT_CHILD] =
Matthias Clasen's avatar
Matthias Clasen committed
265
    gtk_signal_new (I_("unselect_child"),
266 267 268
		    GTK_RUN_FIRST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkListClass, unselect_child),
269
		    _gtk_marshal_VOID__OBJECT,
270 271
		    GTK_TYPE_NONE, 1,
		    GTK_TYPE_WIDGET);
272
  
273 274 275
  gtk_object_add_arg_type ("GtkList::selection-mode",
			   GTK_TYPE_SELECTION_MODE, 
			   GTK_ARG_READWRITE | G_PARAM_STATIC_NAME,
276
			   ARG_SELECTION_MODE);
Elliot Lee's avatar
Elliot Lee committed
277 278 279 280 281 282 283
}

static void
gtk_list_init (GtkList *list)
{
  list->children = NULL;
  list->selection = NULL;
284 285 286 287 288 289 290 291 292 293 294 295 296 297

  list->undo_selection = NULL;
  list->undo_unselection = NULL;

  list->last_focus_child = NULL;
  list->undo_focus_child = NULL;

  list->htimer = 0;
  list->vtimer = 0;

  list->anchor = -1;
  list->drag_pos = -1;
  list->anchor_state = GTK_STATE_SELECTED;

Elliot Lee's avatar
Elliot Lee committed
298
  list->selection_mode = GTK_SELECTION_SINGLE;
299 300
  list->drag_selection = FALSE;
  list->add_mode = FALSE;
Elliot Lee's avatar
Elliot Lee committed
301 302
}

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
static void
gtk_list_set_arg (GtkObject      *object,
		  GtkArg         *arg,
		  guint           arg_id)
{
  GtkList *list = GTK_LIST (object);
  
  switch (arg_id)
    {
    case ARG_SELECTION_MODE:
      gtk_list_set_selection_mode (list, GTK_VALUE_ENUM (*arg));
      break;
    }
}

static void
gtk_list_get_arg (GtkObject      *object,
		  GtkArg         *arg,
		  guint           arg_id)
{
  GtkList *list = GTK_LIST (object);
  
  switch (arg_id)
    {
    case ARG_SELECTION_MODE: 
      GTK_VALUE_ENUM (*arg) = list->selection_mode; 
      break;
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}

Elliot Lee's avatar
Elliot Lee committed
336
GtkWidget*
337
gtk_list_new (void)
Elliot Lee's avatar
Elliot Lee committed
338
{
339
  return GTK_WIDGET (gtk_type_new (GTK_TYPE_LIST));
Elliot Lee's avatar
Elliot Lee committed
340 341
}

342 343 344

/* Private GtkObject Methods :
 * 
Tim Janik's avatar
Tim Janik committed
345
 * gtk_list_dispose
346
 */
347
static void
Tim Janik's avatar
Tim Janik committed
348
gtk_list_dispose (GObject *object)
349 350
{
  gtk_list_clear_items (GTK_LIST (object), 0, -1);
351

Tim Janik's avatar
Tim Janik committed
352
  G_OBJECT_CLASS (parent_class)->dispose (object);
353 354
}

355 356 357 358 359 360 361 362 363 364 365

/* Private GtkWidget Methods :
 * 
 * gtk_list_size_request
 * gtk_list_size_allocate
 * gtk_list_realize
 * gtk_list_unmap
 * gtk_list_motion_notify
 * gtk_list_button_press
 * gtk_list_button_release
 */
366
static void
367 368
gtk_list_size_request (GtkWidget      *widget,
		       GtkRequisition *requisition)
369
{
370
  GtkList *list = GTK_LIST (widget);
371 372
  GtkWidget *child;
  GList *children;
373

374 375
  requisition->width = 0;
  requisition->height = 0;
376

377 378
  children = list->children;
  while (children)
379
    {
380 381
      child = children->data;
      children = children->next;
382

383 384
      if (GTK_WIDGET_VISIBLE (child))
	{
385 386 387
	  GtkRequisition child_requisition;
	  
	  gtk_widget_size_request (child, &child_requisition);
388

389
	  requisition->width = MAX (requisition->width,
390 391
				    child_requisition.width);
	  requisition->height += child_requisition.height;
392 393
	}
    }
394

395 396
  requisition->width += GTK_CONTAINER (list)->border_width * 2;
  requisition->height += GTK_CONTAINER (list)->border_width * 2;
397

398 399
  requisition->width = MAX (requisition->width, 1);
  requisition->height = MAX (requisition->height, 1);
400 401
}

402 403 404
static void
gtk_list_size_allocate (GtkWidget     *widget,
			GtkAllocation *allocation)
Elliot Lee's avatar
Elliot Lee committed
405
{
406
  GtkList *list = GTK_LIST (widget);
407 408 409
  GtkWidget *child;
  GtkAllocation child_allocation;
  GList *children;
Elliot Lee's avatar
Elliot Lee committed
410

411 412 413 414 415
  widget->allocation = *allocation;
  if (GTK_WIDGET_REALIZED (widget))
    gdk_window_move_resize (widget->window,
			    allocation->x, allocation->y,
			    allocation->width, allocation->height);
416

417
  if (list->children)
Elliot Lee's avatar
Elliot Lee committed
418
    {
419 420
      child_allocation.x = GTK_CONTAINER (list)->border_width;
      child_allocation.y = GTK_CONTAINER (list)->border_width;
421
      child_allocation.width = MAX (1, (gint)allocation->width -
422
				    child_allocation.x * 2);
Elliot Lee's avatar
Elliot Lee committed
423

424
      children = list->children;
Elliot Lee's avatar
Elliot Lee committed
425

426
      while (children)
Elliot Lee's avatar
Elliot Lee committed
427
	{
428 429
	  child = children->data;
	  children = children->next;
Elliot Lee's avatar
Elliot Lee committed
430

431 432
	  if (GTK_WIDGET_VISIBLE (child))
	    {
433 434 435 436
	      GtkRequisition child_requisition;
	      gtk_widget_get_child_requisition (child, &child_requisition);
	      
	      child_allocation.height = child_requisition.height;
Elliot Lee's avatar
Elliot Lee committed
437

438
	      gtk_widget_size_allocate (child, &child_allocation);
Elliot Lee's avatar
Elliot Lee committed
439

440 441
	      child_allocation.y += child_allocation.height;
	    }
Elliot Lee's avatar
Elliot Lee committed
442 443
	}
    }
444
}
Elliot Lee's avatar
Elliot Lee committed
445

446 447 448 449 450
static void
gtk_list_realize (GtkWidget *widget)
{
  GdkWindowAttr attributes;
  gint attributes_mask;
Elliot Lee's avatar
Elliot Lee committed
451

452
  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
Elliot Lee's avatar
Elliot Lee committed
453

454 455 456 457 458 459 460 461 462
  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 = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
Elliot Lee's avatar
Elliot Lee committed
463

464
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
Elliot Lee's avatar
Elliot Lee committed
465

466 467 468
  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
				   &attributes, attributes_mask);
  gdk_window_set_user_data (widget->window, widget);
Elliot Lee's avatar
Elliot Lee committed
469

470 471 472
  widget->style = gtk_style_attach (widget->style, widget->window);
  gdk_window_set_background (widget->window, 
			     &widget->style->base[GTK_STATE_NORMAL]);
Elliot Lee's avatar
Elliot Lee committed
473 474
}

475 476 477 478 479 480 481 482
static gboolean
list_has_grab (GtkList *list)
{
  return (GTK_WIDGET_HAS_GRAB (list) &&
	  gdk_display_pointer_is_grabbed (gtk_widget_get_display (GTK_WIDGET (list))));
	  
}

483 484
static void
gtk_list_unmap (GtkWidget *widget)
485
{
486
  GtkList *list = GTK_LIST (widget);
487

488 489 490
  if (!GTK_WIDGET_MAPPED (widget))
    return;

491
  GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
492

493
  if (list_has_grab (list))
494 495 496
    {
      gtk_list_end_drag_selection (list);

497
      if (list->anchor != -1 && list->selection_mode == GTK_SELECTION_MULTIPLE)
498 499 500
	gtk_list_end_selection (list);
    }

501
  gdk_window_hide (widget->window);
502 503
}

504 505 506
static gint
gtk_list_motion_notify (GtkWidget      *widget,
			GdkEventMotion *event)
Elliot Lee's avatar
Elliot Lee committed
507
{
508
  GtkList *list = GTK_LIST (widget);
509 510 511 512 513 514 515 516 517
  GtkWidget *item = NULL;
  GtkAdjustment *adj;
  GtkContainer *container;
  GList *work;
  gint x;
  gint y;
  gint row = -1;
  gint focus_row = 0;
  gint length = 0;
518

519 520
  if (!list->drag_selection || !list->children)
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
521

522
  container = GTK_CONTAINER (widget);
523

524 525
  if (event->is_hint || event->window != widget->window)
    gdk_window_get_pointer (widget->window, &x, &y, NULL);
526 527 528 529 530
  else
    {
      x = event->x;
      y = event->y;
    }
Elliot Lee's avatar
Elliot Lee committed
531

532
  adj = gtk_object_get_data_by_id (GTK_OBJECT (list), hadjustment_key_id);
Elliot Lee's avatar
Elliot Lee committed
533

534 535 536 537 538
  /* horizontal autoscrolling */
  if (adj && widget->allocation.width > adj->page_size &&
      (x < adj->value || x >= adj->value + adj->page_size))
    {
      if (list->htimer == 0)
Elliot Lee's avatar
Elliot Lee committed
539
	{
540
	  list->htimer = gdk_threads_add_timeout
541
	    (SCROLL_TIME, (GSourceFunc) gtk_list_horizontal_timeout, widget);
542 543 544 545
	  
	  if (!((x < adj->value && adj->value <= 0) ||
		(x > adj->value + adj->page_size &&
		 adj->value >= adj->upper - adj->page_size)))
Elliot Lee's avatar
Elliot Lee committed
546
	    {
547
	      gdouble value;
Elliot Lee's avatar
Elliot Lee committed
548

549 550 551 552
	      if (x < adj->value)
		value = adj->value + (x - adj->value) / 2 - 1;
	      else
		value = adj->value + 1 + (x - adj->value - adj->page_size) / 2;
Elliot Lee's avatar
Elliot Lee committed
553

554 555 556 557 558 559 560 561
	      gtk_adjustment_set_value (adj,
					CLAMP (value, 0.0,
					       adj->upper - adj->page_size));
	    }
	}
      else
	return FALSE;
    }
Tim Janik's avatar
Tim Janik committed
562

563 564 565 566 567
  
  /* vertical autoscrolling */
  for (work = list->children; work; length++, work = work->next)
    {
      if (row < 0)
Elliot Lee's avatar
Elliot Lee committed
568
	{
569 570 571 572 573
	  item = GTK_WIDGET (work->data);
	  if (item->allocation.y > y || 
	      (item->allocation.y <= y &&
	       item->allocation.y + item->allocation.height > y))
	    row = length;
Elliot Lee's avatar
Elliot Lee committed
574 575
	}

576 577
      if (work->data == container->focus_child)
	focus_row = length;
Elliot Lee's avatar
Elliot Lee committed
578
    }
579 580 581
  
  if (row < 0)
    row = length - 1;
Elliot Lee's avatar
Elliot Lee committed
582

583 584
  if (list->vtimer != 0)
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
585

586 587
  if (!((y < 0 && focus_row == 0) ||
	(y > widget->allocation.height && focus_row >= length - 1)))
588
    list->vtimer = gdk_threads_add_timeout (SCROLL_TIME,
589
				  (GSourceFunc) gtk_list_vertical_timeout,
590
				  list);
Elliot Lee's avatar
Elliot Lee committed
591

592 593
  if (row != focus_row)
    gtk_widget_grab_focus (item);
594

595 596 597 598 599
  switch (list->selection_mode)
    {
    case GTK_SELECTION_BROWSE:
      gtk_list_select_child (list, item);
      break;
600
    case GTK_SELECTION_MULTIPLE:
601 602 603 604 605
      gtk_list_update_extended_selection (list, row);
      break;
    default:
      break;
    }
Elliot Lee's avatar
Elliot Lee committed
606

607
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
608 609
}

610 611 612
static gint
gtk_list_button_press (GtkWidget      *widget,
		       GdkEventButton *event)
Elliot Lee's avatar
Elliot Lee committed
613
{
614
  GtkList *list = GTK_LIST (widget);
615
  GtkWidget *item;
Elliot Lee's avatar
Elliot Lee committed
616

617 618
  if (event->button != 1)
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
619

620
  item = gtk_get_event_widget ((GdkEvent*) event);
Elliot Lee's avatar
Elliot Lee committed
621

622 623
  while (item && !GTK_IS_LIST_ITEM (item))
    item = item->parent;
Elliot Lee's avatar
Elliot Lee committed
624

625
  if (item && (item->parent == widget))
Elliot Lee's avatar
Elliot Lee committed
626
    {
627 628
      gint last_focus_row;
      gint focus_row;
Elliot Lee's avatar
Elliot Lee committed
629

630 631 632
      if (event->type == GDK_BUTTON_PRESS)
	{
	  gtk_grab_add (widget);
633
	  list->drag_selection = TRUE;
634
	}
635
      else if (list_has_grab (list))
636 637 638 639
	gtk_list_end_drag_selection (list);
	  
      if (!GTK_WIDGET_HAS_FOCUS(item))
	gtk_widget_grab_focus (item);
Elliot Lee's avatar
Elliot Lee committed
640

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
      if (list->add_mode)
	{
	  list->add_mode = FALSE;
	  gtk_widget_queue_draw (item);
	}
      
      switch (list->selection_mode)
	{
	case GTK_SELECTION_SINGLE:
	  if (event->type != GDK_BUTTON_PRESS)
	    gtk_list_select_child (list, item);
	  else
	    list->undo_focus_child = item;
	  break;
	  
	case GTK_SELECTION_BROWSE:
	  break;
Elliot Lee's avatar
Elliot Lee committed
658

659
	case GTK_SELECTION_MULTIPLE:
660
	  focus_row = g_list_index (list->children, item);
Elliot Lee's avatar
Elliot Lee committed
661

662 663 664 665 666 667 668 669
	  if (list->last_focus_child)
	    last_focus_row = g_list_index (list->children,
					   list->last_focus_child);
	  else
	    {
	      last_focus_row = focus_row;
	      list->last_focus_child = item;
	    }
670

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
	  if (event->type != GDK_BUTTON_PRESS)
	    {
	      if (list->anchor >= 0)
		{
		  gtk_list_update_extended_selection (list, focus_row);
		  gtk_list_end_selection (list);
		}
	      gtk_list_select_child (list, item);
	      break;
	    }
	      
	  if (event->state & GDK_CONTROL_MASK)
	    {
	      if (event->state & GDK_SHIFT_MASK)
		{
		  if (list->anchor < 0)
		    {
		      g_list_free (list->undo_selection);
		      g_list_free (list->undo_unselection);
		      list->undo_selection = NULL;
		      list->undo_unselection = NULL;
692

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
		      list->anchor = last_focus_row;
		      list->drag_pos = last_focus_row;
		      list->undo_focus_child = list->last_focus_child;
		    }
		  gtk_list_update_extended_selection (list, focus_row);
		}
	      else
		{
		  if (list->anchor < 0)
		    gtk_list_set_anchor (list, TRUE,
					 focus_row, list->last_focus_child);
		  else
		    gtk_list_update_extended_selection (list, focus_row);
		}
	      break;
	    }
709

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
	  if (event->state & GDK_SHIFT_MASK)
	    {
	      gtk_list_set_anchor (list, FALSE,
				   last_focus_row, list->last_focus_child);
	      gtk_list_update_extended_selection (list, focus_row);
	      break;
	    }

	  if (list->anchor < 0)
	    gtk_list_set_anchor (list, FALSE, focus_row,
				 list->last_focus_child);
	  else
	    gtk_list_update_extended_selection (list, focus_row);
	  break;
	  
	default:
	  break;
	}
728 729

      return TRUE;
730
    }
Elliot Lee's avatar
Elliot Lee committed
731

732 733
  return FALSE;
}
Elliot Lee's avatar
Elliot Lee committed
734

735 736 737
static gint
gtk_list_button_release (GtkWidget	*widget,
			 GdkEventButton *event)
Elliot Lee's avatar
Elliot Lee committed
738
{
739
  GtkList *list = GTK_LIST (widget);
740
  GtkWidget *item;
Elliot Lee's avatar
Elliot Lee committed
741

742 743 744
  /* we don't handle button 2 and 3 */
  if (event->button != 1)
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
745

746
  if (list->drag_selection)
Elliot Lee's avatar
Elliot Lee committed
747
    {
748
      gtk_list_end_drag_selection (list);
Elliot Lee's avatar
Elliot Lee committed
749

750 751
      switch (list->selection_mode)
	{
752
	case GTK_SELECTION_MULTIPLE:
753 754 755
 	  if (!(event->state & GDK_SHIFT_MASK))
	    gtk_list_end_selection (list);
	  break;
Elliot Lee's avatar
Elliot Lee committed
756

757
	case GTK_SELECTION_SINGLE:
Elliot Lee's avatar
Elliot Lee committed
758

759 760 761 762 763 764 765 766 767 768 769 770
	  item = gtk_get_event_widget ((GdkEvent*) event);
  
	  while (item && !GTK_IS_LIST_ITEM (item))
	    item = item->parent;
	  
	  if (item && item->parent == widget)
	    {
	      if (list->undo_focus_child == item)
		gtk_list_toggle_row (list, item);
	    }
	  list->undo_focus_child = NULL;
	  break;
Elliot Lee's avatar
Elliot Lee committed
771

772 773 774
	default:
	  break;
	}
775 776

      return TRUE;
777 778 779
    }
  
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
780 781
}

Owen Taylor's avatar
Owen Taylor committed
782 783 784 785 786 787 788
static void 
gtk_list_style_set	(GtkWidget      *widget,
			 GtkStyle       *previous_style)
{
  if (previous_style && GTK_WIDGET_REALIZED (widget))
    gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
}
789 790 791 792 793 794 795 796 797

/* GtkContainer Methods :
 * gtk_list_add
 * gtk_list_remove
 * gtk_list_forall
 * gtk_list_child_type
 * gtk_list_set_focus_child
 * gtk_list_focus
 */
798
static void
799 800
gtk_list_add (GtkContainer *container,
	      GtkWidget	   *widget)
801
{
802
  GList *item_list;
803

804
  g_return_if_fail (GTK_IS_LIST_ITEM (widget));
805

806 807 808 809
  item_list = g_list_alloc ();
  item_list->data = widget;
  
  gtk_list_append_items (GTK_LIST (container), item_list);
810 811
}

812 813 814
static void
gtk_list_remove (GtkContainer *container,
		 GtkWidget    *widget)
815
{
816
  GList *item_list;
817

818 819 820 821 822 823 824 825 826
  g_return_if_fail (container == GTK_CONTAINER (widget->parent));
  
  item_list = g_list_alloc ();
  item_list->data = widget;
  
  gtk_list_remove_items (GTK_LIST (container), item_list);
  
  g_list_free (item_list);
}
827

828 829 830 831 832
static void
gtk_list_forall (GtkContainer  *container,
		 gboolean       include_internals,
		 GtkCallback	callback,
		 gpointer	callback_data)
833
{
834
  GtkList *list = GTK_LIST (container);
835 836
  GtkWidget *child;
  GList *children;
837

838
  children = list->children;
839

840 841 842 843
  while (children)
    {
      child = children->data;
      children = children->next;
844

845 846 847
      (* callback) (child, callback_data);
    }
}
848

849 850 851 852
static GtkType
gtk_list_child_type (GtkContainer *container)
{
  return GTK_TYPE_LIST_ITEM;
853 854
}

855 856 857
static void
gtk_list_set_focus_child (GtkContainer *container,
			  GtkWidget    *child)
Elliot Lee's avatar
Elliot Lee committed
858
{
859
  GtkList *list;
860

861
  g_return_if_fail (GTK_IS_LIST (container));
862
 
863 864
  if (child)
    g_return_if_fail (GTK_IS_WIDGET (child));
865

866
  list = GTK_LIST (container);
867

868
  if (child != container->focus_child)
869
    {
870
      if (container->focus_child)
871 872 873 874
	{
	  list->last_focus_child = container->focus_child;
	  gtk_widget_unref (container->focus_child);
	}
875 876 877
      container->focus_child = child;
      if (container->focus_child)
        gtk_widget_ref (container->focus_child);
878 879
    }

880 881
  /* check for v adjustment */
  if (container->focus_child)
882
    {
883
      GtkAdjustment *adjustment;
884

885 886 887 888 889 890 891
      adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container),
					      vadjustment_key_id);
      if (adjustment)
        gtk_adjustment_clamp_page (adjustment,
                                   container->focus_child->allocation.y,
                                   (container->focus_child->allocation.y +
                                    container->focus_child->allocation.height));
892 893 894 895 896
      switch (list->selection_mode)
	{
	case GTK_SELECTION_BROWSE:
	  gtk_list_select_child (list, child);
	  break;
897
	case GTK_SELECTION_MULTIPLE:
898 899 900 901 902 903 904 905 906 907
	  if (!list->last_focus_child && !list->add_mode)
	    {
	      list->undo_focus_child = list->last_focus_child;
	      gtk_list_unselect_all (list);
	      gtk_list_select_child (list, child);
	    }
	  break;
	default:
	  break;
	}
908
    }
Elliot Lee's avatar
Elliot Lee committed
909 910
}

911 912
static gboolean
gtk_list_focus (GtkWidget        *widget,
913
		GtkDirectionType  direction)
Elliot Lee's avatar
Elliot Lee committed
914
{
915
  gint return_val = FALSE;
916
  GtkContainer *container;
Elliot Lee's avatar
Elliot Lee committed
917

918 919
  container = GTK_CONTAINER (widget);
  
920
  if (container->focus_child == NULL ||
921 922
      !GTK_WIDGET_HAS_FOCUS (container->focus_child))
    {
923 924 925 926
      if (GTK_LIST (container)->last_focus_child)
	gtk_container_set_focus_child
	  (container, GTK_LIST (container)->last_focus_child);

927 928 929
      if (GTK_WIDGET_CLASS (parent_class)->focus)
	return_val = GTK_WIDGET_CLASS (parent_class)->focus (widget,
                                                             direction);
930
    }
931

932 933 934
  if (!return_val)
    {
      GtkList *list;
935

936
      list =  GTK_LIST (container);
937
      if (list->selection_mode == GTK_SELECTION_MULTIPLE && list->anchor >= 0)
938
	gtk_list_end_selection (list);
939 940 941

      if (container->focus_child)
	list->last_focus_child = container->focus_child;
942
    }
943

944 945
  return return_val;
}
Elliot Lee's avatar
Elliot Lee committed
946

947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974

/* Public GtkList Methods :
 *
 * gtk_list_insert_items
 * gtk_list_append_items
 * gtk_list_prepend_items
 * gtk_list_remove_items
 * gtk_list_remove_items_no_unref
 * gtk_list_clear_items
 *
 * gtk_list_child_position
 */
void
gtk_list_insert_items (GtkList *list,
		       GList   *items,
		       gint	position)
{
  GtkWidget *widget;
  GList *tmp_list;
  GList *last;
  gint nchildren;

  g_return_if_fail (GTK_IS_LIST (list));

  if (!items)
    return;

  gtk_list_end_drag_selection (list);
975
  if (list->selection_mode == GTK_SELECTION_MULTIPLE && list->anchor >= 0)
976 977 978 979
    gtk_list_end_selection (list);

  tmp_list = items;
  while (tmp_list)
980
    {
981 982
      widget = tmp_list->data;
      tmp_list = tmp_list->next;
983

984
      gtk_widget_set_parent (widget, GTK_WIDGET (list));
985 986 987
      gtk_signal_connect (GTK_OBJECT (widget), "drag_begin",
			  GTK_SIGNAL_FUNC (gtk_list_signal_drag_begin),
			  list);
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
      gtk_signal_connect (GTK_OBJECT (widget), "toggle_focus_row",
			  GTK_SIGNAL_FUNC (gtk_list_signal_toggle_focus_row),
			  list);
      gtk_signal_connect (GTK_OBJECT (widget), "select_all",
			  GTK_SIGNAL_FUNC (gtk_list_signal_select_all),
			  list);
      gtk_signal_connect (GTK_OBJECT (widget), "unselect_all",
			  GTK_SIGNAL_FUNC (gtk_list_signal_unselect_all),
			  list);
      gtk_signal_connect (GTK_OBJECT (widget), "undo_selection",
			  GTK_SIGNAL_FUNC (gtk_list_signal_undo_selection),
			  list);
      gtk_signal_connect (GTK_OBJECT (widget), "start_selection",
			  GTK_SIGNAL_FUNC (gtk_list_signal_start_selection),
			  list);
      gtk_signal_connect (GTK_OBJECT (widget), "end_selection",
			  GTK_SIGNAL_FUNC (gtk_list_signal_end_selection),
			  list);
      gtk_signal_connect (GTK_OBJECT (widget), "extend_selection",
			  GTK_SIGNAL_FUNC (gtk_list_signal_extend_selection),
			  list);
      gtk_signal_connect (GTK_OBJECT (widget), "scroll_horizontal",
			  GTK_SIGNAL_FUNC (gtk_list_signal_scroll_horizontal),
			  list);
      gtk_signal_connect (GTK_OBJECT (widget), "scroll_vertical",
			  GTK_SIGNAL_FUNC (gtk_list_signal_scroll_vertical),
			  list);
      gtk_signal_connect (GTK_OBJECT (widget), "toggle_add_mode",
			  GTK_SIGNAL_FUNC (gtk_list_signal_toggle_add_mode),
			  list);
      gtk_signal_connect (GTK_OBJECT (widget), "select",
			  GTK_SIGNAL_FUNC (gtk_list_signal_item_select),
			  list);
      gtk_signal_connect (GTK_OBJECT (widget), "deselect",
			  GTK_SIGNAL_FUNC (gtk_list_signal_item_deselect),
			  list);
      gtk_signal_connect (GTK_OBJECT (widget), "toggle",
			  GTK_SIGNAL_FUNC (gtk_list_signal_item_toggle),
			  list);
    }
1028

1029

1030 1031 1032 1033 1034 1035 1036
  nchildren = g_list_length (list->children);
  if ((position < 0) || (position > nchildren))
    position = nchildren;

  if (position == nchildren)
    {
      if (list->children)
1037
	{
1038 1039 1040
	  tmp_list = g_list_last (list->children);
	  tmp_list->next = items;
	  items->prev = tmp_list;
1041
	}
1042
      else
1043
	{
1044 1045 1046 1047 1048 1049 1050
	  list->children = items;
	}
    }
  else
    {
      tmp_list = g_list_nth (list->children, position);
      last = g_list_last (items);
1051

1052 1053 1054 1055 1056
      if (tmp_list->prev)
	tmp_list->prev->next = items;
      last->next = tmp_list;
      items->prev = tmp_list->prev;
      tmp_list->prev = last;
1057

1058 1059 1060
      if (tmp_list == list->children)
	list->children = items;
    }
1061
  
1062 1063 1064 1065 1066 1067 1068
  if (list->children && !list->selection &&
      (list->selection_mode == GTK_SELECTION_BROWSE))
    {
      widget = list->children->data;
      gtk_list_select_child (list, widget);
    }
}
1069

1070 1071 1072 1073 1074
void
gtk_list_append_items (GtkList *list,
		       GList   *items)
{
  g_return_if_fail (GTK_IS_LIST (list));
1075

1076 1077
  gtk_list_insert_items (list, items, -1);
}
1078

1079 1080 1081 1082 1083 1084 1085
void
gtk_list_prepend_items (GtkList *list,
			GList	*items)
{
  g_return_if_fail (GTK_IS_LIST (list));

  gtk_list_insert_items (list, items, 0);
Elliot Lee's avatar
Elliot Lee committed
1086 1087
}

1088 1089 1090
void
gtk_list_remove_items (GtkList	*list,
		       GList	*items)
Elliot Lee's avatar
Elliot Lee committed
1091
{
1092 1093
  gtk_list_remove_items_internal (list, items, FALSE);
}
Elliot Lee's avatar
Elliot Lee committed
1094

1095 1096 1097 1098 1099 1100
void
gtk_list_remove_items_no_unref (GtkList	 *list,
				GList	 *items)
{
  gtk_list_remove_items_internal (list, items, TRUE);
}
Elliot Lee's avatar
Elliot Lee committed
1101

1102 1103 1104 1105 1106
void
gtk_list_clear_items (GtkList *list,
		      gint     start,
		      gint     end)
{
1107
  GtkContainer *container;
1108
  GtkWidget *widget;
1109
  GtkWidget *new_focus_child = NULL;
1110 1111 1112 1113
  GList *start_list;
  GList *end_list;
  GList *tmp_list;
  guint nchildren;
1114
  gboolean grab_focus = FALSE;
Elliot Lee's avatar
Elliot Lee committed
1115

1116
  g_return_if_fail (GTK_IS_LIST (list));
1117

1118 1119
  nchildren = g_list_length (list->children);

1120 1121
  if (nchildren == 0)
    return;
1122

1123 1124
  if ((end < 0) || (end > nchildren))
    end = nchildren;
1125

1126 1127
  if (start >= end)
    return;
1128

1129
  container = GTK_CONTAINER (list);
1130

1131
  gtk_list_end_drag_selection (list);
1132
  if (list->selection_mode == GTK_SELECTION_MULTIPLE)
1133 1134
    {
      if (list->anchor >= 0)
1135
	gtk_list_end_selection (list);
1136

1137 1138
      gtk_list_reset_extended_selection (list);
    }
1139

1140 1141
  start_list = g_list_nth (list->children, start);
  end_list = g_list_nth (list->children, end);
1142

1143 1144 1145 1146 1147 1148 1149 1150
  if (start_list->prev)
    start_list->prev->next = end_list;
  if (end_list && end_list->prev)
    end_list->prev->next = NULL;
  if (end_list)
    end_list->prev = start_list->prev;
  if (start_list == list->children)
    list->children = end_list;
1151

1152 1153 1154 1155 1156 1157 1158
  if (container->focus_child)
    {
      if (g_list_find (start_list, container->focus_child))
	{
	  if (start_list->prev)
	    new_focus_child = start_list->prev->data;
	  else if (list->children)
1159
	    new_focus_child = list->children->data;
1160

1161 1162
	  if (GTK_WIDGET_HAS_FOCUS (container->focus_child))
	    grab_focus = TRUE;
1163
	}
1164 1165 1166 1167 1168 1169 1170 1171
    }

  tmp_list = start_list;
  while (tmp_list)
    {
      widget = tmp_list->data;
      tmp_list = tmp_list->next;

1172 1173
      gtk_widget_ref (widget);

1174 1175 1176
      if (widget->state == GTK_STATE_SELECTED)
	gtk_list_unselect_child (list, widget);

1177 1178 1179
      gtk_signal_disconnect_by_data (GTK_OBJECT (widget), (gpointer) list);
      gtk_widget_unparent (widget);
      
1180 1181 1182 1183
      if (widget == list->undo_focus_child)
	list->undo_focus_child = NULL;
      if (widget == list->last_focus_child)
	list->last_focus_child = NULL;
1184

1185
      gtk_widget_unref (widget);
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
    }

  g_list_free (start_list);

  if (new_focus_child)
    {
      if (grab_focus)
	gtk_widget_grab_focus (new_focus_child);
      else if (container->focus_child)
	gtk_container_set_focus_child (container, new_focus_child);