gnome-icon-container.c 67.8 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
   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>
*/

#include <config.h>
26
#include "gnome-icon-container.h"
Ettore Perazzoli's avatar
Ettore Perazzoli committed
27

28
#include <string.h>
Ettore Perazzoli's avatar
Ettore Perazzoli committed
29

30 31 32 33
#include <gdk/gdkkeysyms.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkmain.h>
#include <libgnomeui/gnome-canvas-rect-ellipse.h>
34 35
#include <gdk-pixbuf/gnome-canvas-pixbuf.h>

36
#include "nautilus-glib-extensions.h"
37
#include "nautilus-gtk-macros.h"
Ettore Perazzoli's avatar
Ettore Perazzoli committed
38

39
#include "gnome-icon-container-private.h"
40

Ettore Perazzoli's avatar
Ettore Perazzoli committed
41 42 43 44
/* Interval for updating the rubberband selection, in milliseconds.  */
#define RUBBERBAND_TIMEOUT_INTERVAL 10

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

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

55 56 57
/* Maximum amount of milliseconds the mouse button is allowed to stay down
 * and still be considered a click.
 */
58 59
#define MAX_CLICK_TIME 1500

60 61 62 63 64 65 66 67 68
/* Distance you have to move before it becomes a drag. */
#define SNAP_RESISTANCE 2 /* GMC has this set to 3, but it's too much for my (Ettore's?) taste. */

/* Button assignments. */
#define DRAG_BUTTON 1
#define RUBBERBAND_BUTTON 1
#define STRETCH_BUTTON 2
#define CONTEXTUAL_MENU_BUTTON 3

69 70
static void gnome_icon_container_initialize_class (GnomeIconContainerClass *class);
static void gnome_icon_container_initialize (GnomeIconContainer *container);
71 72
static void request_update_one (GnomeIconContainer *container, 
			        GnomeIconContainerIcon *icon);
73 74 75

NAUTILUS_DEFINE_CLASS_BOILERPLATE (GnomeIconContainer, gnome_icon_container, GNOME_TYPE_CANVAS)

Ettore Perazzoli's avatar
Ettore Perazzoli committed
76 77 78


/* The GnomeIconContainer signals.  */
79
enum {
Ettore Perazzoli's avatar
Ettore Perazzoli committed
80 81 82
	SELECTION_CHANGED,
	BUTTON_PRESS,
	ACTIVATE,
83 84 85
	CONTEXT_CLICK_ICON,
	CONTEXT_CLICK_BACKGROUND,
	ICON_MOVED,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
86 87
	LAST_SIGNAL
};
88
static guint signals[LAST_SIGNAL];
Ettore Perazzoli's avatar
Ettore Perazzoli committed
89 90 91 92 93

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

94

Ettore Perazzoli's avatar
Ettore Perazzoli committed
95 96 97 98 99 100 101 102 103 104
/* Functions dealing with GnomeIconContainerIcons.  */

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

static GnomeIconContainerIcon *
icon_new (GnomeIconContainer *container,
105
	  NautilusControllerIcon *data)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
106 107
{
	GnomeIconContainerIcon *new;
108 109
        GnomeCanvas *canvas;
        
110
	canvas = GNOME_CANVAS(container);
111
	
112 113
	new = g_new0 (GnomeIconContainerIcon, 1);
	
Ettore Perazzoli's avatar
Ettore Perazzoli committed
114
	new->layout_done = TRUE;
115
	
Ettore Perazzoli's avatar
Ettore Perazzoli committed
116
	new->data = data;
117

118 119 120 121
        new->item = NAUTILUS_ICONS_VIEW_ICON_ITEM
		(gnome_canvas_item_new (GNOME_CANVAS_GROUP (canvas->root),
					nautilus_icons_view_icon_item_get_type (),
					NULL));
122

123
	request_update_one (container, new);
124
	
Ettore Perazzoli's avatar
Ettore Perazzoli committed
125 126 127 128 129
	return new;
}

static void
icon_position (GnomeIconContainerIcon *icon,
130
	       double x, double y)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
131
{
132 133
	if (icon->x == x && icon->y == y)
		return;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
134

135 136 137
	gnome_canvas_item_move (GNOME_CANVAS_ITEM (icon->item),
				x - icon->x,
				y - icon->y);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

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

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
156
icon_toggle_selected (GnomeIconContainerIcon *icon)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
157
{
158
	icon->is_selected = !icon->is_selected;
159
	gnome_canvas_item_set (GNOME_CANVAS_ITEM (icon->item),
160
			       "highlighted_for_selection", (gboolean) icon->is_selected,
161
			       NULL);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
162 163
}

164
/* Select an icon. Return TRUE if selection has changed. */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
165
static gboolean
166 167
icon_select (GnomeIconContainerIcon *icon,
	     gboolean select)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
168
{
169 170 171 172 173 174
	/* 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
175
		return FALSE;
176 177

	icon_toggle_selected (icon);
178
	g_assert (!select == !icon->is_selected);
179
	return TRUE;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
180 181 182
}

static gboolean
183 184
icon_is_in_region (GnomeIconContainer *container,
		   GnomeIconContainerIcon *icon,
185 186
		   int x0, int y0,
		   int x1, int y1)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
187
{
188
	ArtDRect rect;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
189

190
	if (x0 >= x1 || y0 >= y1)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
191 192
		return FALSE;

193 194 195
	nautilus_icons_view_icon_item_get_icon_world_rectangle
		(icon->item, &rect);
	return x0 < rect.x1 && x1 >= rect.x0 && y0 < rect.y1 && y1 >= rect.y0;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
196 197 198 199
}

static void
icon_get_bounding_box (GnomeIconContainerIcon *icon,
200 201
		       int *x1_return, int *y1_return,
		       int *x2_return, int *y2_return)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
202 203 204 205 206 207 208 209 210 211 212 213 214
{
	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;
}


215

Ettore Perazzoli's avatar
Ettore Perazzoli committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 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
/* 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.
282 283 284 285 286
 *
 * 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.
 */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
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
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;

443
	if (grid->visible_width != grid->width)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
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 495 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
		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);
}

537
static int
Ettore Perazzoli's avatar
Ettore Perazzoli committed
538 539 540 541 542 543 544 545
icon_grid_cell_compare_by_x (gconstpointer ap,
			     gconstpointer bp)
{
	GnomeIconContainerIcon *a, *b;

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

546
	return (int) a->x - b->x;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
547 548 549
}


550

Ettore Perazzoli's avatar
Ettore Perazzoli committed
551 552
static void
world_to_grid (GnomeIconContainer *container,
553
	       int world_x, int world_y,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
554 555
	       guint *grid_x_return, guint *grid_y_return)
{
556
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
557

558
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577

	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,
578
	       int *world_x_return, int *world_y_return)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
579
{
580
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
581

582
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
583 584 585 586 587 588 589 590 591 592 593

	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);
}


594

Ettore Perazzoli's avatar
Ettore Perazzoli committed
595 596 597 598
/* Utility functions for GnomeIconContainer.  */

static void
scroll (GnomeIconContainer *container,
599
	int delta_x, int delta_y)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
600
{
601
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
602 603 604 605 606
	GtkAdjustment *hadj, *vadj;
	GtkAllocation *allocation;
	gfloat vnew, hnew;
	gfloat hmax, vmax;

607
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
608 609 610 611 612 613

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

	allocation = &GTK_WIDGET (container)->allocation;

614 615
	if (container->details->width > allocation->width)
		hmax = (gfloat) (container->details->width - allocation->width);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
616 617 618
	else
		hmax = 0.0;

619 620
	if (container->details->height > allocation->height)
		vmax = (gfloat) (container->details->height - allocation->height);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
	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)
{
641
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
642 643
	GtkAllocation *allocation;
	GtkAdjustment *hadj, *vadj;
644
	int x1, y1, x2, y2;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
645

646
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
647 648
	allocation = &GTK_WIDGET (container)->allocation;

649 650
	if (details->height < allocation->height
	    && details->width < allocation->width)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
		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);
}

669
static gboolean
Ettore Perazzoli's avatar
Ettore Perazzoli committed
670 671 672 673 674 675 676 677
kbd_icon_visibility_timeout_cb (gpointer data)
{
	GnomeIconContainer *container;

	GDK_THREADS_ENTER ();

	container = GNOME_ICON_CONTAINER (data);

678 679
	if (container->details->kbd_current != NULL)
		make_icon_visible (container, container->details->kbd_current);
680
	container->details->kbd_icon_visibility_timer_id = 0;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
681 682 683 684 685 686 687 688 689

	GDK_THREADS_LEAVE ();

	return FALSE;
}

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

692
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
693

694 695
	if (details->kbd_icon_visibility_timer_id != 0)
		gtk_timeout_remove (details->kbd_icon_visibility_timer_id);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
696 697 698 699 700
}

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

703
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
704 705 706

	unschedule_kbd_icon_visibility (container);

707
	details->kbd_icon_visibility_timer_id
Ettore Perazzoli's avatar
Ettore Perazzoli committed
708 709 710 711 712 713 714 715
		= gtk_timeout_add (KBD_ICON_VISIBILITY_TIMEOUT,
				   kbd_icon_visibility_timeout_cb,
				   container);
}

static void
prepare_for_layout (GnomeIconContainer *container)
{
716
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
717 718
	GList *p;

719
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
720

721
	for (p = details->icons; p != NULL; p = p->next) {
Ettore Perazzoli's avatar
Ettore Perazzoli committed
722 723 724 725 726 727 728 729 730 731 732 733
		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)
{
734
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
735 736 737 738 739
	GnomeIconContainerIconGrid *grid;
	GnomeIconContainerIcon *first;
	GList **p;
	guint i, j;

740 741
	details = container->details;
	grid = details->grid;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

	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)
{
773
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
774 775 776
	GnomeIconContainerIconGrid *grid;
	GnomeIconContainerIcon *last;
	GList **p;
777
	int i, j;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
778

779 780
	details = container->details;
	grid = details->grid;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
781 782 783 784 785 786 787 788 789 790 791 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

	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)
{
817
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
818

819
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
820

821
        if (details->kbd_current != NULL)
822 823
		gnome_canvas_item_set (GNOME_CANVAS_ITEM (details->kbd_current->item),
				       "highlighted_for_keyboard_selection", 0,
824 825
				       NULL);
	
826
	details->kbd_current = icon;
827
	
828
	if (icon != NULL) {
829 830
		gnome_canvas_item_set (GNOME_CANVAS_ITEM (details->kbd_current->item),
				       "highlighted_for_keyboard_selection", 1,
831 832 833 834 835
				       NULL);
		icon_raise (icon);
	}
	
	if (icon != NULL && schedule_visibility)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
836 837 838 839 840 841
		schedule_kbd_icon_visibility (container);
	else
		unschedule_kbd_icon_visibility (container);
}


842

Ettore Perazzoli's avatar
Ettore Perazzoli committed
843 844 845 846 847
/* Idle operation handler.  */

static void
set_scroll_region (GnomeIconContainer *container)
{
848
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
849 850 851
	GnomeIconContainerIconGrid *grid;
	GtkAllocation *allocation;
	GtkAdjustment *vadj, *hadj;
852
	double x1, y1, x2, y2;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
853 854
	guint scroll_width, scroll_height;

855 856
	details = container->details;
	grid = details->grid;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
857 858 859 860 861 862 863 864
	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);

865 866
	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
867

868 869
	scroll_width = MAX (details->width, allocation->width);
	scroll_height = MAX (details->height, allocation->height);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
870

Elliot Lee's avatar
Elliot Lee committed
871 872 873
	scroll_width--;
	scroll_height--;

Ettore Perazzoli's avatar
Ettore Perazzoli committed
874 875
	gnome_canvas_set_scroll_region (GNOME_CANVAS (container),
					0.0, 0.0,
876 877
					(double) scroll_width,
					(double) scroll_height);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
878

879
	if (details->width <= allocation->width)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
880
		gtk_adjustment_set_value (hadj, 0.0);
881
	if (details->height <= allocation->height)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
882 883 884
		gtk_adjustment_set_value (vadj, 0.0);
}

885
static gboolean
Ettore Perazzoli's avatar
Ettore Perazzoli committed
886 887 888
idle_handler (gpointer data)
{
	GnomeIconContainer *container;
889
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
890 891 892 893

	GDK_THREADS_ENTER ();

	container = GNOME_ICON_CONTAINER (data);
894
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
895 896 897

	set_scroll_region (container);

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

901
	details->idle_id = 0;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
902 903 904 905 906 907 908 909 910

	GDK_THREADS_LEAVE ();

	return FALSE;
}

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

914
	container->details->idle_id = gtk_idle_add (idle_handler, container);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
915 916 917 918 919
}


/* Container-level icon handling functions.  */

920 921 922 923 924 925
static gboolean
button_event_modifies_selection (GdkEventButton *event)
{
	return event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK);
}

Ettore Perazzoli's avatar
Ettore Perazzoli committed
926
static gboolean
927 928
select_one_unselect_others (GnomeIconContainer *container,
			    GnomeIconContainerIcon *icon_to_select)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
929
{
930
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
931 932 933
	GList *p;
	gboolean selection_changed;

934
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
935
	selection_changed = FALSE;
936
	
937
	for (p = details->icons; p != NULL; p = p->next) {
Ettore Perazzoli's avatar
Ettore Perazzoli committed
938 939 940
		GnomeIconContainerIcon *icon;

		icon = p->data;
941
		selection_changed |= icon_select (icon, icon == icon_to_select);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
942
	}
943
	
Ettore Perazzoli's avatar
Ettore Perazzoli committed
944 945 946 947 948 949
	return selection_changed;
}

static gboolean
unselect_all (GnomeIconContainer *container)
{
950
	return select_one_unselect_others (container, NULL);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
951 952 953
}

/* FIXME: This could be optimized a bit.  */
954 955
void
gnome_icon_container_move_icon (GnomeIconContainer *container,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
956
	   GnomeIconContainerIcon *icon,
957
	   int x, int y, gboolean raise)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
958
{
959
	GnomeIconContainerDetails *details;
960
	int old_x, old_y;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
961
	guint old_grid_x, old_grid_y;
962
	int old_x_offset, old_y_offset;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
963
	guint new_grid_x, new_grid_y;
964
	int new_x_offset, new_y_offset;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
965

966
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
967 968 969 970 971 972 973 974 975 976 977 978

	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);

979
	icon_grid_remove (details->grid, icon, old_grid_x, old_grid_y);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
980
	if (old_x_offset > 0)
981
		icon_grid_remove (details->grid, icon,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
982 983
				  old_grid_x + 1, old_grid_y);
	if (old_y_offset > 0)
984
		icon_grid_remove (details->grid, icon,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
985 986
				  old_grid_x, old_grid_y + 1);
	if (old_x_offset > 0 && old_y_offset > 0)
987
		icon_grid_remove (details->grid, icon,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
988 989
				  old_grid_x + 1, old_grid_y + 1);

990
	icon_grid_add (details->grid, icon, new_grid_x, new_grid_y);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
991
	if (new_x_offset > 0)
992
		icon_grid_add (details->grid, icon, new_grid_x + 1, new_grid_y);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
993
	if (new_y_offset > 0)
994
		icon_grid_add (details->grid, icon, new_grid_x, new_grid_y + 1);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
995
	if (new_x_offset > 0 && new_y_offset > 0)
996
		icon_grid_add (details->grid, icon, new_grid_x + 1, new_grid_y + 1);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
997

998
	icon_position (icon, x, y);
999 1000 1001 1002 1003 1004
	if (raise)
		icon_raise (icon);

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

	gtk_signal_emit (GTK_OBJECT (container), signals[ICON_MOVED],
1007
			 icon->data, x, y);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1008 1009 1010 1011 1012 1013
}


/* Implementation of rubberband selection.  */

static gboolean
1014 1015
rubberband_select_in_cell (GnomeIconContainer *container,
			   GList *cell,
1016 1017 1018 1019
			   double curr_x1, double curr_y1,
			   double curr_x2, double curr_y2,
			   double prev_x1, double prev_y1,
			   double prev_x2, double prev_y2)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
{
	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;

1033
		in_curr_region = icon_is_in_region (container, icon,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1034 1035 1036
						    curr_x1, curr_y1,
						    curr_x2, curr_y2);

1037
		in_prev_region = icon_is_in_region (container, icon,
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1038 1039 1040
						    prev_x1, prev_y1,
						    prev_x2, prev_y2);

1041 1042 1043 1044 1045 1046
		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
1047 1048 1049 1050 1051 1052 1053
	}

	return selection_changed;
}

static void
rubberband_select (GnomeIconContainer *container,
1054 1055 1056 1057
		   double curr_x1, double curr_y1,
		   double curr_x2, double curr_y2,
		   double prev_x1, double prev_y1,
		   double prev_x2, double prev_y2)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
{
	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;

1070
	grid = container->details->grid;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086

	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++) {
1087
			if (rubberband_select_in_cell (container, p[j],
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
						       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]);
}

1103
static int
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1104 1105 1106 1107
rubberband_timeout_cb (gpointer data)
{
	GnomeIconContainer *container;
	GtkWidget *widget;
1108 1109
	GnomeIconContainerRubberbandInfo *band_info;
	int x, y;
1110 1111
	double x1, y1, x2, y2;
	double world_x, world_y;
1112
	int x_scroll, y_scroll;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1113 1114 1115 1116 1117

	GDK_THREADS_ENTER ();

	widget = GTK_WIDGET (data);
	container = GNOME_ICON_CONTAINER (data);
1118
	band_info = &container->details->rubberband_info;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1119

1120 1121 1122
	g_assert (band_info->timer_id != 0);
	g_assert (GNOME_IS_CANVAS_RECT (band_info->selection_rectangle));

Ettore Perazzoli's avatar
Ettore Perazzoli committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
	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
1146
	    && band_info->prev_x == x && band_info->prev_y == y) {
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1147 1148 1149 1150 1151 1152 1153 1154 1155
		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);

1156
	if (world_x < band_info->start_x) {
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1157
		x1 = world_x;
1158
		x2 = band_info->start_x;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1159
	} else {
1160
		x1 = band_info->start_x;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1161 1162 1163
		x2 = world_x;
	}

1164
	if (world_y < band_info->start_y) {
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1165
		y1 = world_y;
1166
		y2 = band_info->start_y;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1167
	} else {
1168
		y1 = band_info->start_y;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1169 1170 1171
		y2 = world_y;
	}

1172
	gnome_canvas_item_set (band_info->selection_rectangle,
Darin Adler's avatar