gtklist.c 67.4 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/. 
 */

Elliot Lee's avatar
Elliot Lee committed
27 28 29 30
#include "gtklist.h"
#include "gtklistitem.h"
#include "gtkmain.h"
#include "gtksignal.h"
31
#include "gtklabel.h"
Elliot Lee's avatar
Elliot Lee committed
32 33 34 35 36 37 38 39

enum {
  SELECTION_CHANGED,
  SELECT_CHILD,
  UNSELECT_CHILD,
  LAST_SIGNAL
};

40 41 42 43 44
enum {
  ARG_0,
  ARG_SELECTION_MODE
};

45 46
#define SCROLL_TIME  100

47
/*** GtkList Methods ***/
48 49
static void gtk_list_class_init	     (GtkListClass   *klass);
static void gtk_list_init	     (GtkList	     *list);
50 51 52 53 54 55
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);
56
/*** GtkObject Methods ***/
57
static void gtk_list_shutdown	     (GObject	     *object);
58

59
/*** GtkWidget Methods ***/
60 61 62 63 64
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);
65 66
static void gtk_list_map	     (GtkWidget	     *widget);
static void gtk_list_unmap	     (GtkWidget	     *widget);
Owen Taylor's avatar
Owen Taylor committed
67 68
static void gtk_list_style_set	     (GtkWidget      *widget,
				      GtkStyle       *previous_style);
69
static gint gtk_list_expose	     (GtkWidget	     *widget,
Elliot Lee's avatar
Elliot Lee committed
70
				      GdkEventExpose *event);
71 72
static gint gtk_list_motion_notify   (GtkWidget      *widget,
				      GdkEventMotion *event);
Elliot Lee's avatar
Elliot Lee committed
73 74
static gint gtk_list_button_press    (GtkWidget      *widget,
				      GdkEventButton *event);
75
static gint gtk_list_button_release  (GtkWidget	     *widget,
Elliot Lee's avatar
Elliot Lee committed
76
				      GdkEventButton *event);
77

78
/*** GtkContainer Methods ***/
79 80 81 82 83 84 85 86 87 88 89 90 91 92
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);
static gint gtk_list_focus           (GtkContainer     *container,
				      GtkDirectionType  direction);

93
/*** GtkList Private Functions ***/
94 95 96 97 98 99 100 101 102
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);

103
/*** GtkList Selection Methods ***/
104 105 106 107 108
static void gtk_real_list_select_child	        (GtkList   *list,
						 GtkWidget *child);
static void gtk_real_list_unselect_child        (GtkList   *list,
						 GtkWidget *child);

109
/*** GtkList Selection Functions ***/
110 111 112 113 114 115 116 117 118 119
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);
120
static void gtk_list_reset_extended_selection   (GtkList   *list);
121

122
/*** GtkListItem Signal Functions ***/
123 124 125
static void gtk_list_signal_drag_begin         (GtkWidget      *widget,
						GdkDragContext *context,
						GtkList        *list);
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 154 155 156 157 158
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
159 160


161 162 163 164
static void gtk_list_drag_begin (GtkWidget      *widget,
				 GdkDragContext *context);


Elliot Lee's avatar
Elliot Lee committed
165
static GtkContainerClass *parent_class = NULL;
166
static guint list_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
167

168 169 170 171
static const gchar *vadjustment_key = "gtk-vadjustment";
static guint        vadjustment_key_id = 0;
static const gchar *hadjustment_key = "gtk-hadjustment";
static guint        hadjustment_key_id = 0;
Elliot Lee's avatar
Elliot Lee committed
172

173
GtkType
174
gtk_list_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
175
{
176
  static GtkType list_type = 0;
Elliot Lee's avatar
Elliot Lee committed
177 178 179

  if (!list_type)
    {
180
      static const GtkTypeInfo list_info =
Elliot Lee's avatar
Elliot Lee committed
181 182 183 184 185 186
      {
	"GtkList",
	sizeof (GtkList),
	sizeof (GtkListClass),
	(GtkClassInitFunc) gtk_list_class_init,
	(GtkObjectInitFunc) gtk_list_init,
187 188
	/* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
189
        (GtkClassInitFunc) NULL,
Elliot Lee's avatar
Elliot Lee committed
190 191
      };

192
      list_type = gtk_type_unique (GTK_TYPE_CONTAINER, &list_info);
Elliot Lee's avatar
Elliot Lee committed
193 194 195 196 197 198 199 200
    }

  return list_type;
}

static void
gtk_list_class_init (GtkListClass *class)
{
201
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
202 203 204 205 206 207 208 209
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

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

210
  parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
Elliot Lee's avatar
Elliot Lee committed
211

212 213 214
  vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
  hadjustment_key_id = g_quark_from_static_string (hadjustment_key);

215 216
  gobject_class->shutdown = gtk_list_shutdown;

Elliot Lee's avatar
Elliot Lee committed
217

218 219
  object_class->set_arg = gtk_list_set_arg;
  object_class->get_arg = gtk_list_get_arg;
Elliot Lee's avatar
Elliot Lee committed
220 221 222

  widget_class->map = gtk_list_map;
  widget_class->unmap = gtk_list_unmap;
Owen Taylor's avatar
Owen Taylor committed
223
  widget_class->style_set = gtk_list_style_set;
Elliot Lee's avatar
Elliot Lee committed
224 225 226 227
  widget_class->realize = gtk_list_realize;
  widget_class->expose_event = gtk_list_expose;
  widget_class->button_press_event = gtk_list_button_press;
  widget_class->button_release_event = gtk_list_button_release;
228
  widget_class->motion_notify_event = gtk_list_motion_notify;
Elliot Lee's avatar
Elliot Lee committed
229 230
  widget_class->size_request = gtk_list_size_request;
  widget_class->size_allocate = gtk_list_size_allocate;
231
  widget_class->drag_begin = gtk_list_drag_begin;
Elliot Lee's avatar
Elliot Lee committed
232 233 234

  container_class->add = gtk_list_add;
  container_class->remove = gtk_list_remove;
235
  container_class->forall = gtk_list_forall;
236
  container_class->child_type = gtk_list_child_type;
237 238
  container_class->set_focus_child = gtk_list_set_focus_child;
  container_class->focus = gtk_list_focus;
Elliot Lee's avatar
Elliot Lee committed
239 240 241 242

  class->selection_changed = NULL;
  class->select_child = gtk_real_list_select_child;
  class->unselect_child = gtk_real_list_unselect_child;
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

  list_signals[SELECTION_CHANGED] =
    gtk_signal_new ("selection_changed",
		    GTK_RUN_FIRST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkListClass, selection_changed),
		    gtk_marshal_VOID__VOID,
		    GTK_TYPE_NONE, 0);
  list_signals[SELECT_CHILD] =
    gtk_signal_new ("select_child",
		    GTK_RUN_FIRST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkListClass, select_child),
		    gtk_marshal_VOID__POINTER,
		    GTK_TYPE_NONE, 1,
		    GTK_TYPE_WIDGET);
  list_signals[UNSELECT_CHILD] =
    gtk_signal_new ("unselect_child",
		    GTK_RUN_FIRST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkListClass, unselect_child),
		    gtk_marshal_VOID__POINTER,
		    GTK_TYPE_NONE, 1,
		    GTK_TYPE_WIDGET);
267 268 269 270
  
  gtk_object_add_arg_type ("GtkList::selection_mode",
			   GTK_TYPE_SELECTION_MODE, GTK_ARG_READWRITE,
			   ARG_SELECTION_MODE);
Elliot Lee's avatar
Elliot Lee committed
271 272 273 274 275 276 277
}

static void
gtk_list_init (GtkList *list)
{
  list->children = NULL;
  list->selection = NULL;
278 279 280 281 282 283 284 285 286 287 288 289 290 291

  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
292
  list->selection_mode = GTK_SELECTION_SINGLE;
293 294
  list->drag_selection = FALSE;
  list->add_mode = FALSE;
Elliot Lee's avatar
Elliot Lee committed
295 296
}

297 298 299 300 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
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
330
GtkWidget*
331
gtk_list_new (void)
Elliot Lee's avatar
Elliot Lee committed
332
{
333
  return GTK_WIDGET (gtk_type_new (GTK_TYPE_LIST));
Elliot Lee's avatar
Elliot Lee committed
334 335
}

336 337 338 339 340

/* Private GtkObject Methods :
 * 
 * gtk_list_shutdown
 */
341
static void
342
gtk_list_shutdown (GObject *object)
343 344
{
  gtk_list_clear_items (GTK_LIST (object), 0, -1);
345 346

  G_OBJECT_CLASS (parent_class)->shutdown (object);
347 348
}

349 350 351 352 353 354 355 356 357 358 359 360

/* Private GtkWidget Methods :
 * 
 * gtk_list_size_request
 * gtk_list_size_allocate
 * gtk_list_realize
 * gtk_list_map
 * gtk_list_unmap
 * gtk_list_motion_notify
 * gtk_list_button_press
 * gtk_list_button_release
 */
361
static void
362 363
gtk_list_size_request (GtkWidget      *widget,
		       GtkRequisition *requisition)
364
{
365 366 367
  GtkList *list;
  GtkWidget *child;
  GList *children;
368

369 370 371
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_LIST (widget));
  g_return_if_fail (requisition != NULL);
372

373 374 375
  list = GTK_LIST (widget);
  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 407 408 409
  GtkList *list;
  GtkWidget *child;
  GtkAllocation child_allocation;
  GList *children;
Elliot Lee's avatar
Elliot Lee committed
410

411 412 413
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_LIST (widget));
  g_return_if_fail (allocation != NULL);
Elliot Lee's avatar
Elliot Lee committed
414

415
  list = GTK_LIST (widget);
Elliot Lee's avatar
Elliot Lee committed
416

417 418 419 420 421
  widget->allocation = *allocation;
  if (GTK_WIDGET_REALIZED (widget))
    gdk_window_move_resize (widget->window,
			    allocation->x, allocation->y,
			    allocation->width, allocation->height);
422

423
  if (list->children)
Elliot Lee's avatar
Elliot Lee committed
424
    {
425 426
      child_allocation.x = GTK_CONTAINER (list)->border_width;
      child_allocation.y = GTK_CONTAINER (list)->border_width;
427
      child_allocation.width = MAX (1, (gint)allocation->width -
428
				    child_allocation.x * 2);
Elliot Lee's avatar
Elliot Lee committed
429

430
      children = list->children;
Elliot Lee's avatar
Elliot Lee committed
431

432
      while (children)
Elliot Lee's avatar
Elliot Lee committed
433
	{
434 435
	  child = children->data;
	  children = children->next;
Elliot Lee's avatar
Elliot Lee committed
436

437 438
	  if (GTK_WIDGET_VISIBLE (child))
	    {
439 440 441 442
	      GtkRequisition child_requisition;
	      gtk_widget_get_child_requisition (child, &child_requisition);
	      
	      child_allocation.height = child_requisition.height;
Elliot Lee's avatar
Elliot Lee committed
443

444
	      gtk_widget_size_allocate (child, &child_allocation);
Elliot Lee's avatar
Elliot Lee committed
445

446 447
	      child_allocation.y += child_allocation.height;
	    }
Elliot Lee's avatar
Elliot Lee committed
448 449
	}
    }
450
}
Elliot Lee's avatar
Elliot Lee committed
451

452 453 454 455 456
static void
gtk_list_realize (GtkWidget *widget)
{
  GdkWindowAttr attributes;
  gint attributes_mask;
Elliot Lee's avatar
Elliot Lee committed
457

458 459
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_LIST (widget));
Elliot Lee's avatar
Elliot Lee committed
460

461
  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
Elliot Lee's avatar
Elliot Lee committed
462

463 464 465 466 467 468 469 470 471
  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
472

473
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
Elliot Lee's avatar
Elliot Lee committed
474

475 476 477
  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
478

479 480 481
  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
482 483
}

484
static void
485
gtk_list_map (GtkWidget *widget)
Elliot Lee's avatar
Elliot Lee committed
486
{
487 488 489
  GtkList *list;
  GtkWidget *child;
  GList *children;
490

491 492
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_LIST (widget));
493

494 495
  GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
  list = GTK_LIST (widget);
496

497 498
  children = list->children;
  while (children)
Elliot Lee's avatar
Elliot Lee committed
499
    {
500 501
      child = children->data;
      children = children->next;
502

503 504 505
      if (GTK_WIDGET_VISIBLE (child) &&
	  !GTK_WIDGET_MAPPED (child))
	gtk_widget_map (child);
Elliot Lee's avatar
Elliot Lee committed
506
    }
507 508

  gdk_window_show (widget->window);
Elliot Lee's avatar
Elliot Lee committed
509 510
}

511 512
static void
gtk_list_unmap (GtkWidget *widget)
513
{
514 515
  GtkList *list;

516 517
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_LIST (widget));
518

519 520 521 522 523
  if (!GTK_WIDGET_MAPPED (widget))
    return;

  list = GTK_LIST (widget);

524
  GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
525 526 527 528 529 530 531 532 533

  if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (list))
    {
      gtk_list_end_drag_selection (list);

      if (list->anchor != -1 && list->selection_mode == GTK_SELECTION_EXTENDED)
	gtk_list_end_selection (list);
    }

534
  gdk_window_hide (widget->window);
535 536
}

537 538 539
static gint
gtk_list_motion_notify (GtkWidget      *widget,
			GdkEventMotion *event)
Elliot Lee's avatar
Elliot Lee committed
540
{
541 542 543 544 545 546 547 548 549 550
  GtkList *list;
  GtkWidget *item = NULL;
  GtkAdjustment *adj;
  GtkContainer *container;
  GList *work;
  gint x;
  gint y;
  gint row = -1;
  gint focus_row = 0;
  gint length = 0;
551

552 553 554
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_LIST (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
Elliot Lee's avatar
Elliot Lee committed
555

556
  list = GTK_LIST (widget);
Elliot Lee's avatar
Elliot Lee committed
557

558 559
  if (!list->drag_selection || !list->children)
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
560

561
  container = GTK_CONTAINER (widget);
562

563 564
  if (event->is_hint || event->window != widget->window)
    gdk_window_get_pointer (widget->window, &x, &y, NULL);
565 566 567 568 569
  else
    {
      x = event->x;
      y = event->y;
    }
Elliot Lee's avatar
Elliot Lee committed
570

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

573 574 575 576 577
  /* 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
578
	{
579 580 581 582 583 584
	  list->htimer = gtk_timeout_add
	    (SCROLL_TIME, (GtkFunction) gtk_list_horizontal_timeout, widget);
	  
	  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
585
	    {
586
	      gfloat value;
Elliot Lee's avatar
Elliot Lee committed
587

588 589 590 591
	      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
592

593 594 595 596 597 598 599 600
	      gtk_adjustment_set_value (adj,
					CLAMP (value, 0.0,
					       adj->upper - adj->page_size));
	    }
	}
      else
	return FALSE;
    }
Tim Janik's avatar
Tim Janik committed
601

602 603 604 605 606
  
  /* vertical autoscrolling */
  for (work = list->children; work; length++, work = work->next)
    {
      if (row < 0)
Elliot Lee's avatar
Elliot Lee committed
607
	{
608 609 610 611 612
	  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
613 614
	}

615 616
      if (work->data == container->focus_child)
	focus_row = length;
Elliot Lee's avatar
Elliot Lee committed
617
    }
618 619 620
  
  if (row < 0)
    row = length - 1;
Elliot Lee's avatar
Elliot Lee committed
621

622 623
  if (list->vtimer != 0)
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
624

625 626 627 628 629
  if (!((y < 0 && focus_row == 0) ||
	(y > widget->allocation.height && focus_row >= length - 1)))
    list->vtimer = gtk_timeout_add (SCROLL_TIME,
				    (GtkFunction) gtk_list_vertical_timeout,
				    list);
Elliot Lee's avatar
Elliot Lee committed
630

631 632
  if (row != focus_row)
    gtk_widget_grab_focus (item);
633

634 635 636 637 638 639 640 641 642 643 644
  switch (list->selection_mode)
    {
    case GTK_SELECTION_BROWSE:
      gtk_list_select_child (list, item);
      break;
    case GTK_SELECTION_EXTENDED:
      gtk_list_update_extended_selection (list, row);
      break;
    default:
      break;
    }
Elliot Lee's avatar
Elliot Lee committed
645

646
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
647 648
}

649 650 651
static gint
gtk_list_button_press (GtkWidget      *widget,
		       GdkEventButton *event)
Elliot Lee's avatar
Elliot Lee committed
652
{
653 654
  GtkList *list;
  GtkWidget *item;
Elliot Lee's avatar
Elliot Lee committed
655

656 657 658
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_LIST (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
Elliot Lee's avatar
Elliot Lee committed
659

660 661
  if (event->button != 1)
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
662

663 664
  list = GTK_LIST (widget);
  item = gtk_get_event_widget ((GdkEvent*) event);
Elliot Lee's avatar
Elliot Lee committed
665

666 667
  while (item && !GTK_IS_LIST_ITEM (item))
    item = item->parent;
Elliot Lee's avatar
Elliot Lee committed
668

669
  if (item && (item->parent == widget))
Elliot Lee's avatar
Elliot Lee committed
670
    {
671 672
      gint last_focus_row;
      gint focus_row;
Elliot Lee's avatar
Elliot Lee committed
673

674 675
      if (event->type == GDK_BUTTON_PRESS)
	{
676 677 678 679 680 681 682
	  if (gdk_pointer_grab (widget->window, TRUE,
				GDK_POINTER_MOTION_HINT_MASK |
				GDK_BUTTON1_MOTION_MASK |
				GDK_BUTTON_RELEASE_MASK,
				NULL, NULL, event->time))
	    return FALSE;
	  
683
	  gtk_grab_add (widget);
684
	  list->drag_selection = TRUE;
685 686 687 688 689 690
	}
      else if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (list))
	gtk_list_end_drag_selection (list);
	  
      if (!GTK_WIDGET_HAS_FOCUS(item))
	gtk_widget_grab_focus (item);
Elliot Lee's avatar
Elliot Lee committed
691

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
      if (list->add_mode)
	{
	  list->add_mode = FALSE;
	  gtk_widget_queue_draw (item);
	}
      
      switch (list->selection_mode)
	{
	case GTK_SELECTION_SINGLE:
	case GTK_SELECTION_MULTIPLE:
	  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
710

711 712
	case GTK_SELECTION_EXTENDED:
	  focus_row = g_list_index (list->children, item);
Elliot Lee's avatar
Elliot Lee committed
713

714 715 716 717 718 719 720 721
	  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;
	    }
722

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
	  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;
744

745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
		      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;
	    }
761

762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
	  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;
	}
780
    }
Elliot Lee's avatar
Elliot Lee committed
781

782 783
  return FALSE;
}
Elliot Lee's avatar
Elliot Lee committed
784

785 786 787
static gint
gtk_list_button_release (GtkWidget	*widget,
			 GdkEventButton *event)
Elliot Lee's avatar
Elliot Lee committed
788 789
{
  GtkList *list;
790
  GtkWidget *item;
Elliot Lee's avatar
Elliot Lee committed
791

792 793 794
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_LIST (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
Elliot Lee's avatar
Elliot Lee committed
795 796 797

  list = GTK_LIST (widget);

798 799 800
  /* we don't handle button 2 and 3 */
  if (event->button != 1)
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
801

802
  if (list->drag_selection)
Elliot Lee's avatar
Elliot Lee committed
803
    {
804
      gtk_list_end_drag_selection (list);
Elliot Lee's avatar
Elliot Lee committed
805

806 807 808 809 810 811
      switch (list->selection_mode)
	{
	case GTK_SELECTION_EXTENDED:
 	  if (!(event->state & GDK_SHIFT_MASK))
	    gtk_list_end_selection (list);
	  break;
Elliot Lee's avatar
Elliot Lee committed
812

813 814
	case GTK_SELECTION_SINGLE:
	case GTK_SELECTION_MULTIPLE:
Elliot Lee's avatar
Elliot Lee committed
815

816 817 818 819 820 821 822 823 824 825 826 827
	  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
828

829 830 831 832 833 834
	default:
	  break;
	}
    }
  
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
835 836 837
}

static gint
838
gtk_list_expose (GtkWidget	*widget,
Elliot Lee's avatar
Elliot Lee committed
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
		 GdkEventExpose *event)
{
  GtkList *list;
  GtkWidget *child;
  GdkEventExpose child_event;
  GList *children;

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

  if (GTK_WIDGET_DRAWABLE (widget))
    {
      list = GTK_LIST (widget);

      child_event = *event;

      children = list->children;
      while (children)
	{
	  child = children->data;
	  children = children->next;

	  if (GTK_WIDGET_NO_WINDOW (child) &&
	      gtk_widget_intersect (child, &event->area, &child_event.area))
	    gtk_widget_event (child, (GdkEvent*) &child_event);
	}
    }

  return FALSE;
}

Owen Taylor's avatar
Owen Taylor committed
871 872 873 874 875 876 877 878 879
static void 
gtk_list_style_set	(GtkWidget      *widget,
			 GtkStyle       *previous_style)
{
  g_return_if_fail (widget != NULL);

  if (previous_style && GTK_WIDGET_REALIZED (widget))
    gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
}
880 881 882 883 884 885 886 887 888

/* GtkContainer Methods :
 * gtk_list_add
 * gtk_list_remove
 * gtk_list_forall
 * gtk_list_child_type
 * gtk_list_set_focus_child
 * gtk_list_focus
 */
889
static void
890 891
gtk_list_add (GtkContainer *container,
	      GtkWidget	   *widget)
892
{
893
  GList *item_list;
894

895 896 897 898
  g_return_if_fail (container != NULL);
  g_return_if_fail (GTK_IS_LIST (container));
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_LIST_ITEM (widget));
899

900 901 902 903
  item_list = g_list_alloc ();
  item_list->data = widget;
  
  gtk_list_append_items (GTK_LIST (container), item_list);
904 905
}

906 907 908
static void
gtk_list_remove (GtkContainer *container,
		 GtkWidget    *widget)
909
{
910 911 912 913 914 915 916 917 918 919 920 921 922 923
  GList *item_list;
  
  g_return_if_fail (container != NULL);
  g_return_if_fail (GTK_IS_LIST (container));
  g_return_if_fail (widget != NULL);
  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);
}
924

925 926 927 928 929
static void
gtk_list_forall (GtkContainer  *container,
		 gboolean       include_internals,
		 GtkCallback	callback,
		 gpointer	callback_data)
930
{
931 932 933
  GtkList *list;
  GtkWidget *child;
  GList *children;
934

935 936 937
  g_return_if_fail (container != NULL);
  g_return_if_fail (GTK_IS_LIST (container));
  g_return_if_fail (callback != NULL);
938

939 940
  list = GTK_LIST (container);
  children = list->children;
941

942 943 944 945
  while (children)
    {
      child = children->data;
      children = children->next;
946

947 948 949
      (* callback) (child, callback_data);
    }
}
950

951 952 953 954
static GtkType
gtk_list_child_type (GtkContainer *container)
{
  return GTK_TYPE_LIST_ITEM;
955 956
}

957 958 959
static void
gtk_list_set_focus_child (GtkContainer *container,
			  GtkWidget    *child)
Elliot Lee's avatar
Elliot Lee committed
960
{
961
  GtkList *list;
962

963 964
  g_return_if_fail (container != NULL);
  g_return_if_fail (GTK_IS_LIST (container));
965
 
966 967
  if (child)
    g_return_if_fail (GTK_IS_WIDGET (child));
968

969
  list = GTK_LIST (container);
970

971
  if (child != container->focus_child)
972
    {
973
      if (container->focus_child)
974 975 976 977
	{
	  list->last_focus_child = container->focus_child;
	  gtk_widget_unref (container->focus_child);
	}
978 979 980
      container->focus_child = child;
      if (container->focus_child)
        gtk_widget_ref (container->focus_child);
981 982
    }

983 984
  /* check for v adjustment */
  if (container->focus_child)
985
    {
986
      GtkAdjustment *adjustment;
987

988 989 990 991 992 993 994
      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));
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
      switch (list->selection_mode)
	{
	case GTK_SELECTION_BROWSE:
	  gtk_list_select_child (list, child);
	  break;
	case GTK_SELECTION_EXTENDED:
	  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;
	}
1011
    }
Elliot Lee's avatar
Elliot Lee committed
1012 1013 1014
}

static gint
1015 1016
gtk_list_focus (GtkContainer     *container,
		GtkDirectionType  direction)
Elliot Lee's avatar
Elliot Lee committed
1017
{
1018
  gint return_val = FALSE;
Elliot Lee's avatar
Elliot Lee committed
1019

1020 1021
  g_return_val_if_fail (container != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_LIST (container), FALSE);
Elliot Lee's avatar
Elliot Lee committed
1022

1023
  if (container->focus_child == NULL ||
1024 1025
      !GTK_WIDGET_HAS_FOCUS (container->focus_child))
    {
1026 1027 1028 1029
      if (GTK_LIST (container)->last_focus_child)
	gtk_container_set_focus_child
	  (container, GTK_LIST (container)->last_focus_child);

1030
      if (GTK_CONTAINER_CLASS (parent_class)->focus)
1031 1032
	return_val = GTK_CONTAINER_CLASS (parent_class)->focus (container,
								direction);
1033
    }
1034

1035 1036 1037
  if (!return_val)
    {
      GtkList *list;
1038