gnome-icon-container.c 66 KB
Newer Older
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1
2
3
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gnome-icon-container.c - Icon container widget.

Elliot Lee's avatar
Elliot Lee committed
4
   Copyright (C) 1999, 2000 Free Software Foundation
5
6
   Copyright (C) 2000 Eazel, Inc.
   
Ettore Perazzoli's avatar
Ettore Perazzoli committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Ettore Perazzoli <ettore@gnu.org>
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

29
#include "gnome-icon-container.h"
Ettore Perazzoli's avatar
Ettore Perazzoli committed
30

31
#include <string.h>
Ettore Perazzoli's avatar
Ettore Perazzoli committed
32

33
34
35
36
#include <gdk/gdkkeysyms.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkmain.h>
#include <libgnomeui/gnome-canvas-rect-ellipse.h>
37
38
#include <gdk-pixbuf/gnome-canvas-pixbuf.h>

39
#include "nautilus-gtk-macros.h"
Ettore Perazzoli's avatar
Ettore Perazzoli committed
40

41
42
43
#include "gnome-icon-container-private.h"
#include "gnome-icon-container-dnd.h"
#include "nautilus-icons-view-icon-item.h"
44

Ettore Perazzoli's avatar
Ettore Perazzoli committed
45
46
47
48
49
/* Interval for updating the rubberband selection, in milliseconds.  */
#define RUBBERBAND_TIMEOUT_INTERVAL 10

/* Timeout for making the icon currently selected for keyboard operation
   visible.  FIXME: This *must* be higher than the double-click time in GDK,
50
51
   but there is no way to access its value from outside.
*/
Ettore Perazzoli's avatar
Ettore Perazzoli committed
52
53
#define KBD_ICON_VISIBILITY_TIMEOUT 300

54
55
56
57
/* Timeout for selecting an icon in "linger-select" mode (i.e. by just placing the
   pointer over the icon, without pressing any button).
*/
#define LINGER_SELECTION_MODE_TIMEOUT 800
Ettore Perazzoli's avatar
Ettore Perazzoli committed
58

59
60
61
/* maximum amount of milliseconds the mouse button is allowed to stay down and still be considered a click */
#define MAX_CLICK_TIME 1500

62
63
64
65
66
static void gnome_icon_container_initialize_class (GnomeIconContainerClass *class);
static void gnome_icon_container_initialize (GnomeIconContainer *container);

NAUTILUS_DEFINE_CLASS_BOILERPLATE (GnomeIconContainer, gnome_icon_container, GNOME_TYPE_CANVAS)

Ettore Perazzoli's avatar
Ettore Perazzoli committed
67
68
69
70
71
72
73


/* The GnomeIconContainer signals.  */
enum _GnomeIconContainerSignalNumber {
	SELECTION_CHANGED,
	BUTTON_PRESS,
	ACTIVATE,
74
75
76
	CONTEXT_CLICK_ICON,
	CONTEXT_CLICK_BACKGROUND,
	ICON_MOVED,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
77
78
79
80
81
82
83
84
85
	LAST_SIGNAL
};
typedef enum _GnomeIconContainerSignalNumber GnomeIconContainerSignalNumber;
static guint signals[LAST_SIGNAL] = { 0 };

/* Bitmap for stippled selection rectangles.  */
static GdkBitmap *stipple;
static char stipple_bits[] = { 0x02, 0x01 };

86

Ettore Perazzoli's avatar
Ettore Perazzoli committed
87
88
89
90
91
92
93
94
95
96
/* Functions dealing with GnomeIconContainerIcons.  */

static void
icon_destroy (GnomeIconContainerIcon *icon)
{
	gtk_object_destroy (GTK_OBJECT (icon->item));
}

static GnomeIconContainerIcon *
icon_new (GnomeIconContainer *container,
97
	  NautilusControllerIcon *data)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
98
99
{
	GnomeCanvas *canvas;
100
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
101
102
103
	GnomeIconContainerIcon *new;

	canvas = GNOME_CANVAS (container);
104
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
105

106
107
	new = g_new0 (GnomeIconContainerIcon, 1);
	
Ettore Perazzoli's avatar
Ettore Perazzoli committed
108
109
110
111
	new->layout_done = TRUE;

	new->data = data;

112
        new->item = NULL;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
113
114
115
116
117
118
119
120

	new->width = GNOME_ICON_CONTAINER_CELL_WIDTH (container);
	new->height = GNOME_ICON_CONTAINER_CELL_HEIGHT (container);

	return new;
}

static GnomeIconContainerIcon *
121
icon_new_pixbuf (GnomeIconContainer *container,
122
		 NautilusControllerIcon *data)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
123
{
124
	GnomeIconContainerDetails *details;
125
	char *name;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
126
	GnomeIconContainerIcon *new;
127
	GdkPixbuf *image;
128
129
        GnomeCanvas* canvas = GNOME_CANVAS(container);
        
130
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
131

132
	new = icon_new (container, data);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
133

134
	image = nautilus_icons_controller_get_icon_image (details->controller, data);
135
136
	name = nautilus_icons_controller_get_icon_name (details->controller, data);
	
137
        
138
139
140
141
142
143
144
145
        new->item = gnome_canvas_item_new
			(GNOME_CANVAS_GROUP (canvas->root),
			nautilus_icons_view_icon_item_get_type (),
	 		"pixbuf", image,    
			"label", name,
			"x", (gdouble) 0,
			"y", (gdouble) 0,	 
                        NULL);
146
                        
147
148
	g_free (name);
        
Ettore Perazzoli's avatar
Ettore Perazzoli committed
149
150
151
152
153
154
155
156
	return new;
}

static void
icon_position (GnomeIconContainerIcon *icon,
	       GnomeIconContainer *container,
	       gdouble x, gdouble y)
{
157
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
158

159
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

	icon->x = x;
	icon->y = y;

	gnome_canvas_item_set (GNOME_CANVAS_ITEM (icon->item),
			       "x", (gdouble) icon->x,
			       "y", (gdouble) icon->y,
			       NULL);
}

static void
icon_raise (GnomeIconContainerIcon *icon)
{
	gnome_canvas_item_raise_to_top (GNOME_CANVAS_ITEM (icon->item));
}

static void
icon_show (GnomeIconContainerIcon *icon)
{
	gnome_canvas_item_show (GNOME_CANVAS_ITEM (icon->item));
}

static void
183
icon_toggle_selected (GnomeIconContainerIcon *icon)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
184
{
185
	icon->is_selected = !icon->is_selected;
186
	gnome_canvas_item_set (GNOME_CANVAS_ITEM (icon->item),
187
			       "selected", (gboolean) icon->is_selected,
188
			       NULL);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
189
190
}

191
/* Select an icon. Return TRUE if selection has changed. */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
192
static gboolean
193
194
icon_select (GnomeIconContainerIcon *icon,
	     gboolean select)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
195
{
196
197
198
199
200
201
	/* Since is_selected is a bit field, we have to do the ! business
	 * to be sure we have either a 1 or a 0. Similarly, the caller
	 * might pass a value other than 1 or 0 so we have to pass do the
	 * same thing there.
	 */
	if (!select == !icon->is_selected)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
202
		return FALSE;
203
204

	icon_toggle_selected (icon);
205
	g_assert (!select == !icon->is_selected);
206
	return TRUE;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
}

static gboolean
icon_is_in_region (GnomeIconContainerIcon *icon,
		   gint x1, gint y1,
		   gint x2, gint y2)
{
	gint icon_x2, icon_y2;

	icon_x2 = icon->x + icon->width;
	icon_y2 = icon->y + icon->height;

	if (x1 == x2 && y1 == y2)
		return FALSE;

	if (x1 < icon_x2 && x2 >= icon->x && y1 < icon_y2 && y2 >= icon->y)
		return TRUE;
	else
		return FALSE;
}

static void
icon_get_text_bounding_box (GnomeIconContainerIcon *icon,
			    guint *x1_return, guint *y1_return,
			    guint *x2_return, guint *y2_return)
{

234
235
236
237
238
        /* FIXME: need to ask item for the text bounds */
	
        /*  
	double x1, y1, x2, y2;
        gnome_canvas_item_get_bounds (GNOME_CANVAS_ITEM (icon->text_item),        
Ettore Perazzoli's avatar
Ettore Perazzoli committed
239
240
241
242
	*x1_return = icon->x + x1;
	*y1_return = icon->y + y1;
	*x2_return = icon->x + x2;
	*y2_return = icon->y + y2;
243
244
245
246
247
248
249
        */
        
        *x1_return = 0;
        *y1_return = 0;
        *x2_return = 0;
        *y2_return = 0;
        
Ettore Perazzoli's avatar
Ettore Perazzoli committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
}

static void
icon_get_bounding_box (GnomeIconContainerIcon *icon,
		       guint *x1_return, guint *y1_return,
		       guint *x2_return, guint *y2_return)
{
	double x1, y1, x2, y2;

	gnome_canvas_item_get_bounds (GNOME_CANVAS_ITEM (icon->item),
				      &x1, &y1, &x2, &y2);

	*x1_return = x1;
	*y1_return = y1;
	*x2_return = x2;
	*y2_return = y2;
}


/* Functions for dealing with IconGrids.  */

static GnomeIconContainerIconGrid *
icon_grid_new (void)
{
	GnomeIconContainerIconGrid *new;

	new = g_new (GnomeIconContainerIconGrid, 1);

	new->width = new->height = 0;
	new->visible_width = 0;
	new->alloc_width = new->alloc_height = 0;

	new->elems = NULL;

	new->first_free_x = -1;
	new->first_free_y = -1;

	return new;
}

static void
icon_grid_clear (GnomeIconContainerIconGrid *grid)
{
	GList **p;
	guint i, j;

	p = grid->elems;
	for (j = 0; j < grid->height; j++) {
		for (i = 0; i < grid->width; i++) {
			if (p[i] != NULL) {
				g_list_free (p[i]);
				p[i] = NULL;
			}
		}

		p += grid->alloc_width;
	}

	grid->first_free_x = 0;
	grid->first_free_y = 0;
}

static void
icon_grid_destroy (GnomeIconContainerIconGrid *grid)
{
	icon_grid_clear (grid);
	g_free (grid->elems);
	g_free (grid);
}

inline static GList **
icon_grid_get_element_ptr (GnomeIconContainerIconGrid *grid,
			   guint x, guint y)
{
	return &grid->elems[y * grid->alloc_width + x];
}

inline static GList *
icon_grid_get_element (GnomeIconContainerIconGrid *grid,
		       guint x, guint y)
{
	return *icon_grid_get_element_ptr (grid, x, y);
}

/* This is admittedly a bit lame.

   Instead of re-allocating the grid from scratch and copying the values, we
   should just link grid chunks horizontally and vertically in lists;
   i.e. use a hybrid list/array representation.  */
static void
icon_grid_resize_allocation (GnomeIconContainerIconGrid *grid,
			     guint new_alloc_width,
			     guint new_alloc_height)
{
	GList **new_elems;
	guint i, j;
	guint new_alloc_size;

	if (new_alloc_width == 0 || new_alloc_height == 0) {
		g_free (grid->elems);
		grid->elems = NULL;
		grid->width = grid->height = 0;
		grid->alloc_width = new_alloc_width;
		grid->alloc_height = new_alloc_height;
		return;
	}

	new_alloc_size = new_alloc_width * new_alloc_height;
	new_elems = g_new (GList *, new_alloc_size);

	if (grid->elems == NULL || grid->width == 0 || grid->height == 0) {
		memset (new_elems, 0, sizeof (*new_elems) * new_alloc_size);
	} else {
		GList **sp, **dp;
		guint copy_width, copy_height;

		/* Copy existing elements into the new array.  */

		sp = grid->elems;
		dp = new_elems;
		copy_width = MIN (grid->width, new_alloc_width);
		copy_height = MIN (grid->height, new_alloc_height);
		
		for (i = 0; i < copy_height; i++) {
			for (j = 0; j < copy_width; j++)
				dp[j] = sp[j];

			for (j = copy_width; j < new_alloc_width; j++)
				dp[j] = NULL;

			for (j = copy_width; j < grid->width; j++)
				g_list_free (sp[j]);

			sp += grid->alloc_width;
			dp += new_alloc_width;
		}

		/* If there are other lines left, zero them as well.  */

		if (i < new_alloc_height) {
			guint elems_left;

			elems_left = new_alloc_size - (dp - new_elems);
			memset (dp, 0, sizeof (*new_elems) * elems_left);
		}
	}

	g_free (grid->elems);
	grid->elems = new_elems;

	grid->alloc_width = new_alloc_width;
	grid->alloc_height = new_alloc_height;
}

static void
icon_grid_update_first_free_forward (GnomeIconContainerIconGrid *grid)
{
	GList **p;
	guint start_x, start_y;
	guint x, y;

	if (grid->first_free_x == -1) {
		start_x = start_y = 0;
		p = grid->elems;
	} else {
		start_x = grid->first_free_x;
		start_y = grid->first_free_y;
		p = icon_grid_get_element_ptr (grid, start_x, start_y);
	}

	x = start_x;
	y = start_y;
	while (y < grid->height) {
		if (*p == NULL) {
			grid->first_free_x = x;
			grid->first_free_y = y;
			return;
		}

		x++, p++;

		if (x >= grid->visible_width) {
			x = 0;
			y++;
			p += grid->alloc_width - grid->visible_width;
		}
	}

	/* No free cell found.  */

	grid->first_free_x = -1;
	grid->first_free_y = -1;
}

static void
icon_grid_set_visible_width (GnomeIconContainerIconGrid *grid,
			     guint visible_width)
{
	if (visible_width > grid->visible_width
	    && grid->height > 0
	    && grid->first_free_x == -1) {
		grid->first_free_x = visible_width;
		grid->first_free_y = 0;
	} else if (grid->first_free_x >= visible_width) {
		if (grid->first_free_y == grid->height - 1) {
			grid->first_free_x = -1;
			grid->first_free_y = -1;
		} else {
			grid->first_free_x = 0;
			grid->first_free_y++;
			icon_grid_update_first_free_forward (grid);
		}
	}

	grid->visible_width = visible_width;
}

static void
icon_grid_resize (GnomeIconContainerIconGrid *grid,
		  guint width, guint height)
{
	guint new_alloc_width, new_alloc_height;

	if (width > grid->alloc_width || height > grid->alloc_height) {
		if (grid->alloc_width > 0)
			new_alloc_width = grid->alloc_width;
		else
			new_alloc_width = INITIAL_GRID_WIDTH;
		while (new_alloc_width < width)
			new_alloc_width *= 2;

		if (grid->alloc_height > 0)
			new_alloc_height = grid->alloc_height;
		else
			new_alloc_height = INITIAL_GRID_HEIGHT;
		while (new_alloc_height < height)
			new_alloc_height *= 2;

		icon_grid_resize_allocation (grid, new_alloc_width,
					     new_alloc_height);
	}

	grid->width = width;
	grid->height = height;

495
	if (grid->visible_width != grid->width)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
		icon_grid_set_visible_width (grid, grid->width);
}

static void
icon_grid_maybe_resize (GnomeIconContainerIconGrid *grid,
			guint x, guint y)
{
	guint new_width, new_height;

	if (x < grid->width && y < grid->height)
		return;

	if (x >= grid->width)
		new_width = x + 1;
	else
		new_width = grid->width;

	if (y >= grid->height)
		new_height = y + 1;
	else
		new_height = grid->height;

	icon_grid_resize (grid, new_width, new_height);
}

static void
icon_grid_add (GnomeIconContainerIconGrid *grid,
	       GnomeIconContainerIcon *icon,
	       guint x, guint y)
{
	GList **elem_ptr;

	icon_grid_maybe_resize (grid, x, y);

	elem_ptr = icon_grid_get_element_ptr (grid, x, y);
	*elem_ptr = g_list_prepend (*elem_ptr, icon);

	if (x == grid->first_free_x && y == grid->first_free_y)
		icon_grid_update_first_free_forward (grid);
}

static void
icon_grid_remove (GnomeIconContainerIconGrid *grid,
		  GnomeIconContainerIcon *icon,
		  guint x, guint y)
{
	GList **elem_ptr;

	elem_ptr = icon_grid_get_element_ptr (grid, x, y);

	g_return_if_fail (*elem_ptr != NULL);

	*elem_ptr = g_list_remove (*elem_ptr, icon);

	if (*elem_ptr == NULL) {
		if ((grid->first_free_x == -1 && grid->first_free_y == -1)
		    || grid->first_free_y > y
		    || (grid->first_free_y == y && grid->first_free_x > x)) {
			grid->first_free_x = x;
			grid->first_free_y = y;
		}
	}
}

static void
icon_grid_add_auto (GnomeIconContainerIconGrid *grid,
		    GnomeIconContainerIcon *icon,
		    guint *x_return, guint *y_return)
{
	GList **empty_elem_ptr;

	if (grid->first_free_x < 0 || grid->first_free_y < 0
	    || grid->height == 0 || grid->width == 0) {
		/* No empty element: add a row.  */
		icon_grid_resize (grid, MAX (grid->width, 1), grid->height + 1);
		grid->first_free_x = 0;
		grid->first_free_y = grid->height - 1;
	}

	empty_elem_ptr = icon_grid_get_element_ptr (grid,
						    grid->first_free_x,
						    grid->first_free_y);

	*empty_elem_ptr = g_list_prepend (*empty_elem_ptr, icon);

	if (x_return != NULL)
		*x_return = grid->first_free_x;
	if (y_return != NULL)
		*y_return = grid->first_free_y;

	icon_grid_update_first_free_forward (grid);
}

static gint
icon_grid_cell_compare_by_x (gconstpointer ap,
			     gconstpointer bp)
{
	GnomeIconContainerIcon *a, *b;

	a = (GnomeIconContainerIcon *) ap;
	b = (GnomeIconContainerIcon *) bp;

	return (gint) a->x - b->x;
}


static void
world_to_grid (GnomeIconContainer *container,
	       gint world_x, gint world_y,
	       guint *grid_x_return, guint *grid_y_return)
{
607
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
608

609
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630

	if (grid_x_return != NULL) {
		if (world_x < 0)
			*grid_x_return = 0;
		else
			*grid_x_return = world_x / GNOME_ICON_CONTAINER_CELL_WIDTH (container);
	}

	if (grid_y_return != NULL) {
		if (world_y < 0)
			*grid_y_return = 0;
		else
			*grid_y_return = world_y / GNOME_ICON_CONTAINER_CELL_HEIGHT (container);
	}
}

static void
grid_to_world (GnomeIconContainer *container,
	       guint grid_x, guint grid_y,
	       gint *world_x_return, gint *world_y_return)
{
631
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
632

633
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650

	if (world_x_return != NULL)
		*world_x_return
			= grid_x * GNOME_ICON_CONTAINER_CELL_WIDTH (container);

	if (world_y_return != NULL)
		*world_y_return
			= grid_y * GNOME_ICON_CONTAINER_CELL_HEIGHT (container);
}


/* Utility functions for GnomeIconContainer.  */

static void
scroll (GnomeIconContainer *container,
	gint delta_x, gint delta_y)
{
651
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
652
653
654
655
656
	GtkAdjustment *hadj, *vadj;
	GtkAllocation *allocation;
	gfloat vnew, hnew;
	gfloat hmax, vmax;

657
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
658
659
660
661
662
663

	hadj = GTK_LAYOUT (container)->hadjustment;
	vadj = GTK_LAYOUT (container)->vadjustment;

	allocation = &GTK_WIDGET (container)->allocation;

664
665
	if (container->details->width > allocation->width)
		hmax = (gfloat) (container->details->width - allocation->width);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
666
667
668
	else
		hmax = 0.0;

669
670
	if (container->details->height > allocation->height)
		vmax = (gfloat) (container->details->height - allocation->height);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
	else
		vmax = 0.0;

	hnew = CLAMP (hadj->value + (gfloat) delta_x, 0.0, hmax);
	vnew = CLAMP (vadj->value + (gfloat) delta_y, 0.0, vmax);

	if (hnew != hadj->value) {
		hadj->value = hnew;
		gtk_signal_emit_by_name (GTK_OBJECT (hadj), "value_changed");
	}
	if (vnew != vadj->value) {
		vadj->value = vnew;
		gtk_signal_emit_by_name (GTK_OBJECT (vadj), "value_changed");
	}
}

static void
make_icon_visible (GnomeIconContainer *container,
		   GnomeIconContainerIcon *icon)
{
691
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
692
693
694
695
	GtkAllocation *allocation;
	GtkAdjustment *hadj, *vadj;
	gint x1, y1, x2, y2;

696
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
697
698
	allocation = &GTK_WIDGET (container)->allocation;

699
700
	if (details->height < allocation->height
	    && details->width < allocation->width)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
		return;

	hadj = GTK_LAYOUT (container)->hadjustment;
	vadj = GTK_LAYOUT (container)->vadjustment;

	icon_get_bounding_box (icon, &x1, &y1, &x2, &y2);

	if (y1 < vadj->value)
		gtk_adjustment_set_value (vadj, y1);
	else if (y2 > vadj->value + allocation->height)
		gtk_adjustment_set_value (vadj, y2 - allocation->height);

	if (x1 < hadj->value)
		gtk_adjustment_set_value (hadj, x1);
	else if (x2 > hadj->value + allocation->width)
		gtk_adjustment_set_value (hadj, x2 - allocation->width);
}

static gint
kbd_icon_visibility_timeout_cb (gpointer data)
{
	GnomeIconContainer *container;

	GDK_THREADS_ENTER ();

	container = GNOME_ICON_CONTAINER (data);

728
729
730
	if (container->details->kbd_current != NULL)
		make_icon_visible (container, container->details->kbd_current);
	container->details->kbd_icon_visibility_timer_tag = -1;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
731
732
733
734
735
736
737
738
739

	GDK_THREADS_LEAVE ();

	return FALSE;
}

static void
unschedule_kbd_icon_visibility (GnomeIconContainer *container)
{
740
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
741

742
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
743

744
745
	if (details->kbd_icon_visibility_timer_tag != -1)
		gtk_timeout_remove (details->kbd_icon_visibility_timer_tag);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
746
747
748
749
750
}

static void
schedule_kbd_icon_visibility (GnomeIconContainer *container)
{
751
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
752

753
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
754
755
756

	unschedule_kbd_icon_visibility (container);

757
	details->kbd_icon_visibility_timer_tag
Ettore Perazzoli's avatar
Ettore Perazzoli committed
758
759
760
761
762
763
764
765
		= gtk_timeout_add (KBD_ICON_VISIBILITY_TIMEOUT,
				   kbd_icon_visibility_timeout_cb,
				   container);
}

static void
prepare_for_layout (GnomeIconContainer *container)
{
766
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
767
768
	GList *p;

769
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
770

771
	for (p = details->icons; p != NULL; p = p->next) {
Ettore Perazzoli's avatar
Ettore Perazzoli committed
772
773
774
775
776
777
778
779
780
781
782
783
		GnomeIconContainerIcon *icon;

		icon = p->data;
		icon->layout_done = FALSE;
	}
}

/* Find the "first" icon (in left-to-right, top-to-bottom order) in
   `container'.  */
static GnomeIconContainerIcon *
find_first (GnomeIconContainer *container)
{
784
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
785
786
787
788
789
	GnomeIconContainerIconGrid *grid;
	GnomeIconContainerIcon *first;
	GList **p;
	guint i, j;

790
791
	details = container->details;
	grid = details->grid;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822

	if (grid->width == 0 || grid->height == 0)
		return NULL;

	first = NULL;
	p = grid->elems;
	for (i = 0; i < grid->height; i++) {
		for (j = 0; j < grid->width; j++) {
			GList *q;

			for (q = p[j]; q != NULL; q = q->next) {
				GnomeIconContainerIcon *icon;

				icon = q->data;
				if (first == NULL
				    || icon->y < first->y
				    || (icon->y == first->y
					&& icon->x < first->x))
					first = icon;
			}
		}

		p += grid->alloc_width;
	}

	return first;
}

static GnomeIconContainerIcon *
find_last (GnomeIconContainer *container)
{
823
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
824
825
826
827
828
	GnomeIconContainerIconGrid *grid;
	GnomeIconContainerIcon *last;
	GList **p;
	gint i, j;

829
830
	details = container->details;
	grid = details->grid;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
831
832
833
834
835
836
837
838
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

	last = NULL;

	if (grid->height == 0 || grid->width == 0)
		return NULL;

	p = icon_grid_get_element_ptr (grid, 0, grid->height - 1);

	for (i = grid->height - 1; i >= 0; i--) {
		for (j = grid->width - 1; j >= 0; j--) {
			GList *q;

			for (q = p[j]; q != NULL; q = q->next) {
				GnomeIconContainerIcon *icon;

				icon = q->data;
				if (last == NULL
				    || icon->y > last->y
				    || (icon->y == last->y
					&& icon->x > last->x))
					last = icon;
			}
		}

		p -= grid->alloc_width;
	}

	return last;
}

/* Set `icon' as the icon currently selected for keyboard operations.  */
static void
set_kbd_current (GnomeIconContainer *container,
		 GnomeIconContainerIcon *icon,
		 gboolean schedule_visibility)
{
867
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
868
869
	gint x1, y1, x2, y2;

870
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
871

872
	details->kbd_current = icon;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
873

874
	if (details->kbd_current == NULL)
875
876
		return;
	
Ettore Perazzoli's avatar
Ettore Perazzoli committed
877
878
	icon_get_text_bounding_box (icon, &x1, &y1, &x2, &y2);

879
	gnome_canvas_item_set (details->kbd_navigation_rectangle,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
880
881
882
883
884
			       "x1", (gdouble) x1 - 1,
			       "y1", (gdouble) y1 - 1,
			       "x2", (gdouble) x2,
			       "y2", (gdouble) y2,
			       NULL);
885
	gnome_canvas_item_show (details->kbd_navigation_rectangle);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
886
887

	icon_raise (icon);
888
	gnome_canvas_item_raise_to_top (details->kbd_navigation_rectangle);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
889
890
891
892
893
894
895
896
897
898

	if (schedule_visibility)
		schedule_kbd_icon_visibility (container);
	else
		unschedule_kbd_icon_visibility (container);
}

static void
unset_kbd_current (GnomeIconContainer *container)
{
899
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
900

901
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
902

903
904
	details->kbd_current = NULL;
	gnome_canvas_item_hide (details->kbd_navigation_rectangle);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
905
906
907
908
909
910
911
912
913
914

	unschedule_kbd_icon_visibility (container);
}


/* Idle operation handler.  */

static void
set_scroll_region (GnomeIconContainer *container)
{
915
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
916
917
918
919
920
921
	GnomeIconContainerIconGrid *grid;
	GtkAllocation *allocation;
	GtkAdjustment *vadj, *hadj;
	gdouble x1, y1, x2, y2;
	guint scroll_width, scroll_height;

922
923
	details = container->details;
	grid = details->grid;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
924
925
926
927
928
929
930
931
	allocation = &(GTK_WIDGET (container)->allocation);
	hadj = GTK_LAYOUT (container)->hadjustment;
	vadj = GTK_LAYOUT (container)->vadjustment;

	/* FIXME: We can do this more efficiently.  */
	gnome_canvas_item_get_bounds (GNOME_CANVAS (container)->root,
				      &x1, &y1, &x2, &y2);

932
933
	details->width = x2 + GNOME_ICON_CONTAINER_CELL_SPACING (container);
	details->height = y2 + GNOME_ICON_CONTAINER_CELL_SPACING (container);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
934

935
936
	scroll_width = MAX (details->width, allocation->width);
	scroll_height = MAX (details->height, allocation->height);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
937

Elliot Lee's avatar
Elliot Lee committed
938
939
940
	scroll_width--;
	scroll_height--;

Ettore Perazzoli's avatar
Ettore Perazzoli committed
941
942
943
944
945
	gnome_canvas_set_scroll_region (GNOME_CANVAS (container),
					0.0, 0.0,
					(gdouble) scroll_width,
					(gdouble) scroll_height);

946
	if (details->width <= allocation->width)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
947
		gtk_adjustment_set_value (hadj, 0.0);
948
	if (details->height <= allocation->height)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
949
950
951
952
953
954
955
		gtk_adjustment_set_value (vadj, 0.0);
}

static gint
idle_handler (gpointer data)
{
	GnomeIconContainer *container;
956
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
957
958
959
960

	GDK_THREADS_ENTER ();

	container = GNOME_ICON_CONTAINER (data);
961
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
962
963
964

	set_scroll_region (container);

965
	if (details->icons != NULL && details->kbd_current == NULL)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
966
967
		set_kbd_current (container, find_first (container), FALSE);

968
	details->idle_id = 0;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
969
970
971
972
973
974
975
976
977

	GDK_THREADS_LEAVE ();

	return FALSE;
}

static void
add_idle (GnomeIconContainer *container)
{
978
	if (container->details->idle_id != 0)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
979
980
		return;

981
	container->details->idle_id = gtk_idle_add (idle_handler, container);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
982
983
984
985
986
}


/* Container-level icon handling functions.  */

987
988
989
990
991
992
static gboolean
button_event_modifies_selection (GdkEventButton *event)
{
	return event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK);
}

Ettore Perazzoli's avatar
Ettore Perazzoli committed
993
static gboolean
994
995
select_one_unselect_others (GnomeIconContainer *container,
			    GnomeIconContainerIcon *icon_to_select)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
996
{
997
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
998
999
1000
	GList *p;
	gboolean selection_changed;

1001
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1002
	selection_changed = FALSE;
1003
	
1004
	for (p = details->icons; p != NULL; p = p->next) {
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1005
1006
1007
		GnomeIconContainerIcon *icon;

		icon = p->data;
1008
		selection_changed |= icon_select (icon, icon == icon_to_select);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1009
	}
1010
	
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1011
1012
1013
1014
1015
1016
	return selection_changed;
}

static gboolean
unselect_all (GnomeIconContainer *container)
{
1017
	return select_one_unselect_others (container, NULL);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1018
1019
1020
}

/* FIXME: This could be optimized a bit.  */
1021
1022
void
gnome_icon_container_move_icon (GnomeIconContainer *container,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1023
	   GnomeIconContainerIcon *icon,
1024
	   int x, int y, gboolean raise)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1025
{
1026
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1027
1028
1029
1030
1031
1032
	gint old_x, old_y;
	guint old_grid_x, old_grid_y;
	gint old_x_offset, old_y_offset;
	guint new_grid_x, new_grid_y;
	gint new_x_offset, new_y_offset;

1033
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045

	old_x = icon->x;
	old_y = icon->y;

	world_to_grid (container, old_x, old_y, &old_grid_x, &old_grid_y);
	old_x_offset = old_x % GNOME_ICON_CONTAINER_CELL_WIDTH (container);
	old_y_offset = old_y % GNOME_ICON_CONTAINER_CELL_HEIGHT (container);

	world_to_grid (container, x, y, &new_grid_x, &new_grid_y);
	new_x_offset = x % GNOME_ICON_CONTAINER_CELL_WIDTH (container);
	new_y_offset = y % GNOME_ICON_CONTAINER_CELL_HEIGHT (container);

1046
	icon_grid_remove (details->grid, icon, old_grid_x, old_grid_y);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1047
	if (old_x_offset > 0)
1048
		icon_grid_remove (details->grid, icon,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1049
1050
				  old_grid_x + 1, old_grid_y);
	if (old_y_offset > 0)
1051
		icon_grid_remove (details->grid, icon,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1052
1053
				  old_grid_x, old_grid_y + 1);
	if (old_x_offset > 0 && old_y_offset > 0)
1054
		icon_grid_remove (details->grid, icon,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1055
1056
				  old_grid_x + 1, old_grid_y + 1);

1057
	icon_grid_add (details->grid, icon, new_grid_x, new_grid_y);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1058
	if (new_x_offset > 0)
1059
		icon_grid_add (details->grid, icon, new_grid_x + 1, new_grid_y);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1060
	if (new_y_offset > 0)
1061
		icon_grid_add (details->grid, icon, new_grid_x, new_grid_y + 1);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1062
	if (new_x_offset > 0 && new_y_offset > 0)
1063
		icon_grid_add (details->grid, icon, new_grid_x + 1, new_grid_y + 1);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1064
1065

	icon_position (icon, container, x, y);
1066
1067
1068
1069
1070
1071
	if (raise)
		icon_raise (icon);

	/* Update the keyboard selection indicator.  */
	if (details->kbd_current == icon)
		set_kbd_current (container, icon, FALSE);
1072
1073

	gtk_signal_emit (GTK_OBJECT (container), signals[ICON_MOVED],
1074
			 icon->data, x, y);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
}


/* Implementation of rubberband selection.  */

static gboolean
rubberband_select_in_cell (GList *cell,
			   gdouble curr_x1, gdouble curr_y1,
			   gdouble curr_x2, gdouble curr_y2,
			   gdouble prev_x1, gdouble prev_y1,
			   gdouble prev_x2, gdouble prev_y2)
{
	GList *p;
	gboolean selection_changed;

	selection_changed = FALSE;

	for (p = cell; p != NULL; p = p->next) {
		GnomeIconContainerIcon *icon;
		gboolean in_curr_region;
		gboolean in_prev_region;

		icon = p->data;

		in_curr_region = icon_is_in_region (icon,
						    curr_x1, curr_y1,
						    curr_x2, curr_y2);

		in_prev_region = icon_is_in_region (icon,
						    prev_x1, prev_y1,
						    prev_x2, prev_y2);

1107
1108
1109
1110
1111
1112
		if (in_curr_region && ! in_prev_region)
			selection_changed |= icon_select (icon,
							  !icon->was_selected_before_rubberband);
		else if (in_prev_region && ! in_curr_region)
			selection_changed |= icon_select (icon,
							  icon->was_selected_before_rubberband);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
	}

	return selection_changed;
}

static void
rubberband_select (GnomeIconContainer *container,
		   gdouble curr_x1, gdouble curr_y1,
		   gdouble curr_x2, gdouble curr_y2,
		   gdouble prev_x1, gdouble prev_y1,
		   gdouble prev_x2, gdouble prev_y2)
{
	GList **p;
	GnomeIconContainerIconGrid *grid;
	guint curr_grid_x1, curr_grid_y1;
	guint curr_grid_x2, curr_grid_y2;
	guint prev_grid_x1, prev_grid_y1;
	guint prev_grid_x2, prev_grid_y2;
	guint grid_x1, grid_y1;
	guint grid_x2, grid_y2;
	guint i, j;
	gboolean selection_changed;

1136
	grid = container->details->grid;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183

	world_to_grid (container, curr_x1, curr_y1, &curr_grid_x1, &curr_grid_y1);
	world_to_grid (container, curr_x2, curr_y2, &curr_grid_x2, &curr_grid_y2);
	world_to_grid (container, prev_x1, prev_y1, &prev_grid_x1, &prev_grid_y1);
	world_to_grid (container, prev_x2, prev_y2, &prev_grid_x2, &prev_grid_y2);

	grid_x1 = MIN (curr_grid_x1, prev_grid_x1);
	grid_x2 = MAX (curr_grid_x2, prev_grid_x2);
	grid_y1 = MIN (curr_grid_y1, prev_grid_y1);
	grid_y2 = MAX (curr_grid_y2, prev_grid_y2);

	selection_changed = FALSE;

	p = icon_grid_get_element_ptr (grid, grid_x1, grid_y1);
	for (i = 0; i <= grid_y2 - grid_y1; i++) {
		for (j = 0; j <= grid_x2 - grid_x1; j++) {
			if (rubberband_select_in_cell (p[j],
						       curr_x1, curr_y1,
						       curr_x2, curr_y2,
						       prev_x1, prev_y1,
						       prev_x2, prev_y2))
				selection_changed = TRUE;
		}

		p += grid->alloc_width;
	}

	if (selection_changed)
		gtk_signal_emit (GTK_OBJECT (container),
				 signals[SELECTION_CHANGED]);
}

static gint
rubberband_timeout_cb (gpointer data)
{
	GnomeIconContainer *container;
	GtkWidget *widget;
	GnomeIconContainerRubberbandInfo *rinfo;
	gint x, y;
	gdouble x1, y1, x2, y2;
	gdouble world_x, world_y;
	gint x_scroll, y_scroll;

	GDK_THREADS_ENTER ();

	widget = GTK_WIDGET (data);
	container = GNOME_ICON_CONTAINER (data);
1184
	rinfo = &container->details->rubberband_info;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262

	gdk_window_get_pointer (widget->window, &x, &y, NULL);

	if (x < 0) {
		x_scroll = x;
		x = 0;
	} else if (x >= widget->allocation.width) {
		x_scroll = x - widget->allocation.width + 1;
		x = widget->allocation.width - 1;
	} else {
		x_scroll = 0;
	}

	if (y < 0) {
		y_scroll = y;
		y = 0;
	} else if (y >= widget->allocation.height) {
		y_scroll = y - widget->allocation.height + 1;
		y = widget->allocation.height - 1;
	} else {
		y_scroll = 0;
	}

	if (y_scroll == 0 && x_scroll == 0
	    && rinfo->prev_x == x && rinfo->prev_y == y) {
		GDK_THREADS_LEAVE ();
		return TRUE;
	}

	scroll (container, x_scroll, y_scroll);

	gnome_canvas_window_to_world (GNOME_CANVAS (container),
				      x, y, &world_x, &world_y);

	if (world_x < rinfo->start_x) {
		x1 = world_x;
		x2 = rinfo->start_x;
	} else {
		x1 = rinfo->start_x;
		x2 = world_x;
	}

	if (world_y < rinfo->start_y) {
		y1 = world_y;
		y2 = rinfo->start_y;
	} else {
		y1 = rinfo->start_y;
		y2 = world_y;
	}

	gnome_canvas_item_set (rinfo->selection_rectangle,
			       "x1", (gdouble) x1,
			       "y1", (gdouble) y1,
			       "x2", (gdouble) x2,
			       "y2", (gdouble) y2,
			       NULL);

	rubberband_select (container,
			   x1, y1, x2, y2,
			   rinfo->prev_x1, rinfo->prev_y1,
			   rinfo->prev_x2, rinfo->prev_y2);

	rinfo->prev_x = x;
	rinfo->prev_y = y;
	rinfo->prev_x1 = x1;
	rinfo->prev_y1 = y1;
	rinfo->prev_x2 = x2;
	rinfo->prev_y2 = y2;	

	GDK_THREADS_LEAVE ();

	return TRUE;
}

static void
start_rubberbanding (GnomeIconContainer *container,
		     GdkEventButton *event)
{
1263
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1264
1265
1266
	GnomeIconContainerRubberbandInfo *rinfo;
	GList *p;

1267
1268
	details = container->details;
	rinfo = &details->rubberband_info;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1269

1270
	for (p = details->icons; p != NULL; p = p->next) {
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
		GnomeIconContainerIcon *icon;

		icon = p->data;
		icon->was_selected_before_rubberband = icon->is_selected;
	}

	gnome_canvas_window_to_world (GNOME_CANVAS (container),
				      event->x, event->y,
				      &rinfo->start_x, &rinfo->start_y);

	rinfo->selection_rectangle
		= gnome_canvas_item_new (gnome_canvas_root
					 (GNOME_CANVAS (container)),
					 gnome_canvas_rect_get_type (),
					 "x1", rinfo->start_x,
					 "y1", rinfo->start_y,
					 "x2", rinfo->start_x,
					 "y2", rinfo->start_y,
					 "outline_color", "black",
					 "outline_stipple", stipple,
1291
					 "width_pixels", 2,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
					 NULL);

	rinfo->prev_x = rinfo->prev_x1 = rinfo->prev_x2 = event->x;
	rinfo->prev_y = rinfo->prev_y1 = rinfo->prev_y2 = event->y;

	rinfo->active = TRUE;

	rinfo->timer_tag = gtk_timeout_add (RUBBERBAND_TIMEOUT_INTERVAL,
					    rubberband_timeout_cb,
					    container);

	gnome_canvas_item_grab (rinfo->selection_rectangle,
				(GDK_POINTER_MOTION_MASK
				 | GDK_BUTTON_RELEASE_MASK),
				NULL, event->time);
}

static void
stop_rubberbanding (GnomeIconContainer *container,
		    GdkEventButton *event)
{
	GnomeIconContainerRubberbandInfo *rinfo;

1315
	rinfo = &container->details->rubberband_info;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331

	gtk_timeout_remove (rinfo->timer_tag);
	rinfo->active = FALSE;

	gnome_canvas_item_ungrab (rinfo->selection_rectangle, event->time);
	gtk_object_destroy (GTK_OBJECT (rinfo->selection_rectangle));
}


/* Keyboard navigation.  */

static void
kbd_move_to (GnomeIconContainer *container,
	     GnomeIconContainerIcon *icon,
	     GdkEventKey *event)
{
1332
1333
1334
	/* Control key causes keyboard selection and "selected icon" to move separately.
	 * This seems like a confusing and bad idea. 
	 */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1335
1336
1337
1338
	if (! (event->state & GDK_CONTROL_MASK)) {
		gboolean selection_changed;

		selection_changed = unselect_all (container);
1339
		selection_changed |= icon_select (icon, TRUE);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375

		if (selection_changed)
			gtk_signal_emit (GTK_OBJECT (container),
					 signals[SELECTION_CHANGED]);
	}

	set_kbd_current (container, icon, FALSE);
	make_icon_visible (container, icon);
}

static void
kbd_home (GnomeIconContainer *container,
	  GdkEventKey *event)
{
	GnomeIconContainerIcon *first;

	first = find_first (container);
	if (first != NULL)
		kbd_move_to (container, first, event);
}

static void
kbd_end (GnomeIconContainer *container,
	 GdkEventKey *event)
{
	GnomeIconContainerIcon *last;

	last = find_last (container);
	if (last != NULL)
		kbd_move_to (container, last, event);
}

static void
kbd_left (GnomeIconContainer *container,
	  GdkEventKey *event)
{
1376
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1377
1378
1379
1380
1381
1382
1383
	GnomeIconContainerIconGrid *grid;
	GnomeIconContainerIcon *nearmost;
	GList **e;
	guint grid_x, grid_y;
	gint x, y;
	gint max_x;

1384
1385
	details = container->details;
	grid = details->grid;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1386

1387
	if (details->kbd_current == NULL)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1388
1389
		return;

1390
	world_to_grid (container, details->kbd_current->x, details->kbd_current->y,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1391
1392
1393
1394
1395
1396
		       &grid_x, &grid_y);
	grid_to_world (container, grid_x, grid_y, &x, &y);

	e = icon_grid_get_element_ptr (grid, 0, grid_y);
	nearmost = NULL;

1397
	max_x = details->kbd_current->x;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1398
1399
1400
1401
1402
1403
1404
1405
1406

	while (1) {
		while (1) {
			GList *p;

			for (p = e[grid_x]; p != NULL; p = p->next) {
				GnomeIconContainerIcon *icon;

				icon = p->data;
1407
				if (icon == details->kbd_current
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
				    || icon->x < x
				    || icon->y < y)
					continue;

				if (icon->x <= max_x
				    && (nearmost == NULL
					|| icon->x > nearmost->x))
					nearmost = icon;
			}
 
			if (nearmost != NULL) {
				kbd_move_to (container, nearmost, event);
				return;
			}

			if (grid_x == 0)
				break;

			grid_x--;
			x -= GNOME_ICON_CONTAINER_CELL_WIDTH (container);
		}

		if (grid_y == 0)
			break;

		grid_x = grid->width - 1;
		max_x = G_MAXINT;
		grid_to_world (container, grid_x, 0, &x, NULL);

		e -= grid->alloc_width;
		grid_y--;
		y -= GNOME_ICON_CONTAINER_CELL_HEIGHT (container);
	}
}

static void
kbd_up (GnomeIconContainer *container,
	GdkEventKey *event)
{
1447
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1448
1449
1450
1451
1452
1453
	GnomeIconContainerIconGrid *grid;
	GnomeIconContainerIcon *nearmost;
	GList **e;
	guint grid_x, grid_y;
	gint x, y;

1454
1455
	details = container->details;
	grid = details->grid;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1456

1457
	if (details->kbd_current == NULL)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1458
1459
		return;

1460
	world_to_grid (container, details->kbd_current->x, details->kbd_current->y,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
		       &grid_x, &grid_y);
	grid_to_world (container, grid_x, grid_y, &x, &y);

	e = icon_grid_get_element_ptr (grid, grid_x, grid_y);
	nearmost = NULL;

	while (1) {
		GList *p;

		p = *e;

		for (; p != NULL; p = p->next) {
			GnomeIconContainerIcon *icon;

			icon = p->data;
1476
			if (icon == details->kbd_current
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1477
1478
1479
1480
			    || icon->x < x
			    || icon->y < y)
				continue;

1481
			if (icon->y <= details->kbd_current->y
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
			    && (nearmost == NULL || icon->y > nearmost->y))
				nearmost = icon;
		}

		if (nearmost != NULL)
			break;

		if (grid_y == 0)
			break;

		e -= grid->alloc_width;
		grid_y--;
		y -= GNOME_ICON_CONTAINER_CELL_HEIGHT (container);
	}

	if (nearmost != NULL)
		kbd_move_to (container, nearmost, event);
}

static void
kbd_right (GnomeIconContainer *container,
	   GdkEventKey *event)
{
1505
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1506
1507
1508
1509
1510
1511
1512
	GnomeIconContainerIconGrid *grid;
	GnomeIconContainerIcon *nearmost;
	GList **e;
	guint grid_x, grid_y;
	gint x, y;
	gint min_x;

1513
1514
	details = container->details;
	grid = details->grid;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1515

1516
	if (details->kbd_current == NULL)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1517
1518
		return;

1519
	world_to_grid (container, details->kbd_current->x, details->kbd_current->y,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1520
1521
1522
1523
1524
1525
		       &grid_x, &grid_y);
	grid_to_world (container, grid_x, grid_y, &x, &y);

	e = icon_grid_get_element_ptr (grid, 0, grid_y);
	nearmost = NULL;

1526
	min_x = details->kbd_current->x;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1527
1528
1529
1530
1531
1532
1533
1534
1535

	while (grid_y < grid->height) {
		while (grid_x < grid->width) {
			GList *p;

			for (p = e[grid_x]; p != NULL; p = p->next) {
				GnomeIconContainerIcon *icon;

				icon = p->data;
1536
				if (icon == details->kbd_current
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
				    || icon->x < x
				    || icon->y < y)
					continue;

				if (icon->x >= min_x
				    && (nearmost == NULL
					|| icon->x < nearmost->x))
					nearmost = icon;
			}
 
			if (nearmost != NULL) {
				kbd_move_to (container, nearmost, event);
				return;
			}

			grid_x++;
			x += GNOME_ICON_CONTAINER_CELL_WIDTH (container);
		}

		grid_x = 0;
		min_x = 0;
		x = 0;

		e += grid->alloc_width;
		grid_y++;
		y += GNOME_ICON_CONTAINER_CELL_HEIGHT (container);
	}
}

static void
kbd_down (GnomeIconContainer *container,
	  GdkEventKey *event)
{
1570
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1571
1572
1573
1574
1575
1576
	GnomeIconContainerIconGrid *grid;
	GnomeIconContainerIcon *nearmost;
	GList **e;
	guint grid_x, grid_y;
	gint x, y;

1577
1578
	details = container->details;
	grid = details->grid;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1579

1580
	if (details->kbd_current == NULL)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1581
1582
		return;

1583
	world_to_grid (container, details->kbd_current->x, details->kbd_current->y,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
		       &grid_x, &grid_y);
	grid_to_world (container, grid_x, grid_y, &x, &y);

	e = icon_grid_get_element_ptr (grid, grid_x, grid_y);
	nearmost = NULL;

	while (grid_y < grid->height) {
		GList *p;

		p = *e;

		for (; p != NULL; p = p->next) {
			GnomeIconContainerIcon *icon;

			icon = p->data;
1599
			if (icon == details->kbd_current
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1600
1601
1602
1603
			    || icon->x < x
			    || icon->y < y)
				continue;

1604
			if (icon->y >= details->kbd_current->y
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
			    && (nearmost == NULL || icon->y < nearmost->y))
				nearmost = icon;
		}

		if (nearmost != NULL)
			break;

		e += grid->alloc_width;
		grid_y++;
		y += GNOME_ICON_CONTAINER_CELL_HEIGHT (container);
	}

	if (nearmost != NULL)
		kbd_move_to (container, nearmost, event);
}

static void
kbd_space (GnomeIconContainer *container,
	   GdkEventKey *event)
{
1625
	if (container->details->kbd_current != NULL) {
1626
		if (icon_select (container->details->kbd_current, TRUE))
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
			gtk_signal_emit (GTK_OBJECT (container),
					 signals[SELECTION_CHANGED]);
	}
}


/* GtkObject methods.  */

static void
destroy (GtkObject *object)
{
	GnomeIconContainer *container;

	container = GNOME_ICON_CONTAINER (object);

	gnome_icon_container_dnd_fini (container);

1644
	icon_grid_destroy (container->details->grid);
1645
	g_hash_table_destroy (container->details->canvas_item_to_icon);
1646
	unschedule_kbd_icon_visibility (container);
1647
1648
	
        if (container->details->idle_id != 0)
1649
1650
1651
		gtk_idle_remove (container->details->idle_id);
	if (container->details->linger_selection_mode_timer_tag != -1)
		gtk_timeout_remove (container->details->linger_selection_mode_timer_tag);
1652
1653
1654
        if (container->details->label_font)
                gdk_font_unref(container->details->label_font);
                
1655
	g_free (container->details);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684

	if (GTK_OBJECT_CLASS (parent_class)->destroy != NULL)
		(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}


/* GtkWidget methods.  */

static void
size_request (GtkWidget *widget,
	      GtkRequisition *requisition)
{
	requisition->width = 1;
	requisition->height = 1;
}

static void
size_allocate (GtkWidget *widget,
	       GtkAllocation *allocation)
{
	GnomeIconContainer *container;
	GnomeIconContainerIconGrid *grid;
	guint visible_width, visible_height;

	if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
		(* GTK_WIDGET_CLASS (parent_class)->size_allocate)
			(widget, allocation);

	container = GNOME_ICON_CONTAINER (widget);
1685
	grid = container->details->grid;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1686
1687
1688
1689
1690
1691
1692
1693

	world_to_grid (container,
		       allocation->width, 0,
		       &visible_width, &visible_height);

	if (visible_width == 0)
		visible_width = 1;

1694
#if 0
Elliot Lee's avatar
Elliot Lee committed
1695
	grid->visible_width = visible_width;
Elliot Lee's avatar
Elliot Lee committed
1696
1697
1698
	grid->height = MAX(visible_height, grid->height);
	gnome_icon_container_relayout(container);
#else
1699
	/*
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1700
1701
1702
1703
	if (visible_width > grid->width || visible_height > grid->height)
		icon_grid_resize (grid,
				  MAX (visible_width, grid->width),
				  MAX (visible_height, grid->height));
1704
	*/
Elliot Lee's avatar
Elliot Lee committed
1705
	/* icon_grid_resize(grid, visible_width, visible_height); */
Elliot Lee's avatar