nautilus-icon-text-item.c 31.7 KB
Newer Older
Darin Adler's avatar
Darin Adler committed
1 2
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

3 4 5 6 7 8 9 10 11 12
/* nautilus-icon-text-item:  an editable text block with word wrapping for the
 * GNOME canvas.
 *
 * Copyright (C) 1998, 1999 The Free Software Foundation
 *
 * Authors: Miguel de Icaza <miguel@gnu.org>
 *          Federico Mena <federico@gimp.org>
 *
 */

13 14
#include <config.h>
#include "nautilus-icon-text-item.h"
15

16
#include "nautilus-entry.h"
17
#include <libnautilus/nautilus-undo.h>
18

19
#include <math.h>
20
#include <stdio.h>
21 22 23 24
#include <gdk/gdkkeysyms.h>
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkwindow.h>
Gene Z. Ragan's avatar
CVS:  
Gene Z. Ragan committed
25
#include <libgnome/gnome-i18n.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

/* Margins used to display the information */
#define MARGIN_X 2
#define MARGIN_Y 2

/* Default fontset to be used if the user specified fontset is not found */
#define DEFAULT_FONT_NAME "-adobe-helvetica-medium-r-normal--*-100-*-*-*-*-*-*,"	\
			  "-*-*-medium-r-normal--10-*-*-*-*-*-*-*,*"

/* Separators for text layout */
#define DEFAULT_SEPARATORS " \t-.[]#"

/* Aliases to minimize screen use in my laptop */
#define ITI(x)       NAUTILUS_ICON_TEXT_ITEM (x)
#define ITI_CLASS(x) NAUTILUS_ICON_TEXT_ITEM_CLASS (x)
#define IS_ITI(x)    NAUTILUS_IS_ICON_TEXT_ITEM (x)

typedef NautilusIconTextItem Iti;

45
/* Signal callbacks */
Darin Adler's avatar
Darin Adler committed
46 47
static void register_rename_undo (NautilusIconTextItem *item);
static void restore_from_undo_snapshot_callback (GtkObject *target, gpointer callback_data);
48 49


50 51 52 53 54
/* Private part of the NautilusIconTextItem structure */
typedef struct {
	/* Font */
	GdkFont *font;

55 56
	/* Create an offscreen window and place an entry inside it */
	NautilusEntry *entry;
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	GtkWidget *entry_top;

	/* Whether the user pressed the mouse while the item was unselected */
	guint unselected_click : 1;

	/* Whether we need to update the position */
	guint need_pos_update : 1;

	/* Whether we need to update the font */
	guint need_font_update : 1;

	/* Whether we need to update the text */
	guint need_text_update : 1;

	/* Whether we need to update because the editing/selected state changed */
	guint need_state_update : 1;

74 75
	/* Store min width and height.  These are used when the text entry is empty. */
	guint min_width;
76 77 78 79
	guint min_height;

	gboolean undo_registered;
		
80 81 82 83 84 85 86 87 88 89 90
} ItiPrivate;


static GnomeCanvasItemClass *parent_class;

enum {
	ARG_0,
};

enum {
	TEXT_CHANGED,
91
	TEXT_EDITED,
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	HEIGHT_CHANGED,
	WIDTH_CHANGED,
	EDITING_STARTED,
	EDITING_STOPPED,
	SELECTION_STARTED,
	SELECTION_STOPPED,
	LAST_SIGNAL
};
static guint iti_signals [LAST_SIGNAL] = { 0 };

/* Stops the editing state of an icon text item */
static void
iti_stop_editing (Iti *iti)
{
	ItiPrivate *priv;

	priv = iti->priv;

	iti->editing = FALSE;

	gtk_widget_destroy (priv->entry_top);
	priv->entry = NULL;
	priv->entry_top = NULL;

	priv->need_state_update = TRUE;
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (iti));

	gtk_signal_emit (GTK_OBJECT (iti), iti_signals[EDITING_STOPPED]);
}

/* Lays out the text in an icon item */
static void
layout_text (Iti *iti)
{
	ItiPrivate *priv;
	char *text;
	int old_width, old_height;
	int width, height;

	priv = iti->priv;

	/* Save old size */
	if (iti->ti) {
		old_width = iti->ti->width + 2 * MARGIN_X;
		old_height = iti->ti->height + 2 * MARGIN_Y;

		gnome_icon_text_info_free (iti->ti);
	} else {
		old_width = 2 * MARGIN_X;
		old_height = 2 * MARGIN_Y;
	}

	/* Change the text layout */
	if (iti->editing)
146
		text = gtk_entry_get_text (GTK_ENTRY(priv->entry));
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	else
		text = iti->text;

	iti->ti = gnome_icon_layout_text (priv->font,
					  text,
					  DEFAULT_SEPARATORS,
					  iti->width - 2 * MARGIN_X,
					  TRUE);

	/* Check the sizes and see if we need to emit any signals */
	width = iti->ti->width + 2 * MARGIN_X;
	height = iti->ti->height + 2 * MARGIN_Y;

	if (width != old_width)
		gtk_signal_emit (GTK_OBJECT (iti), iti_signals[WIDTH_CHANGED]);

	if (height != old_height)
		gtk_signal_emit (GTK_OBJECT (iti), iti_signals[HEIGHT_CHANGED]);
165
	
166
	gtk_signal_emit (GTK_OBJECT (iti), iti_signals [TEXT_EDITED]);		
167 168
}

169

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/* Accepts the text in the off-screen entry of an icon text item */
static void
iti_edition_accept (Iti *iti)
{
	ItiPrivate *priv;
	gboolean accept;

	priv = iti->priv;
	accept = TRUE;

	gtk_signal_emit (GTK_OBJECT (iti), iti_signals [TEXT_CHANGED], &accept);

	if (iti->editing){
		if (accept) {
			if (iti->is_text_allocated)
				g_free (iti->text);

187
			iti->text = g_strdup (gtk_entry_get_text (GTK_ENTRY(priv->entry)));
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
			iti->is_text_allocated = 1;
		}

		iti_stop_editing (iti);
	}
	layout_text (iti);

	priv->need_text_update = TRUE;
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (iti));
}

/* Callback used when the off-screen entry of an icon text item is activated.
 * When this happens, we have to accept edition.
 */
static void
iti_entry_activate (GtkWidget *entry, Iti *iti)
{
	iti_edition_accept (iti);
}

/* Starts the editing state of an icon text item */
static void
iti_start_editing (Iti *iti)
{
	ItiPrivate *priv;

	priv = iti->priv;

	if (iti->editing)
		return;

219
	/* Trick: The actual edition of the entry takes place in a NautilusEntry
220
	 * which is placed offscreen.  That way we get all of the advantages
221
	 * from NautilusEntry without duplicating code.  Yes, this is a hack.
222
	 */
223 224
	priv->entry = (NautilusEntry *) nautilus_entry_new ();
	gtk_entry_set_text (GTK_ENTRY(priv->entry), iti->text);
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 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
	gtk_signal_connect (GTK_OBJECT (priv->entry), "activate",
			    GTK_SIGNAL_FUNC (iti_entry_activate), iti);

	priv->entry_top = gtk_window_new (GTK_WINDOW_POPUP);
	gtk_container_add (GTK_CONTAINER (priv->entry_top), GTK_WIDGET (priv->entry));
	gtk_widget_set_uposition (priv->entry_top, 20000, 20000);
	gtk_widget_show_all (priv->entry_top);

	gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);

	iti->editing = TRUE;

	priv->need_state_update = TRUE;
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (iti));

	gtk_signal_emit (GTK_OBJECT (iti), iti_signals[EDITING_STARTED]);
}

/* Destroy method handler for the icon text item */
static void
iti_destroy (GtkObject *object)
{
	Iti *iti;
	ItiPrivate *priv;
	GnomeCanvasItem *item;

	g_return_if_fail (object != NULL);
	g_return_if_fail (IS_ITI (object));

	iti = ITI (object);
	priv = iti->priv;
	item = GNOME_CANVAS_ITEM (object);

	/* Queue redraw of bounding box */
	gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);

	/* Free everything */
	if (iti->font)
		gdk_font_unref (iti->font);

	if (iti->text && iti->is_text_allocated)
		g_free (iti->text);

	if (iti->ti)
		gnome_icon_text_info_free (iti->ti);

	if (priv->font)
		gdk_font_unref (priv->font);

	if (priv->entry_top)
		gtk_widget_destroy (priv->entry_top);

	g_free (priv);

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

/* set_arg handler for the icon text item */
static void
iti_set_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
	Iti *iti;
	ItiPrivate *priv;

	iti = ITI (object);
	priv = iti->priv;

	switch (arg_id) {
	default:
		break;
	}
}

/* Recomputes the bounding box of an icon text item */
static void
recompute_bounding_box (Iti *iti)
{
	GnomeCanvasItem *item;
	double affine[6];
	ArtPoint p, q;
	int x1, y1, x2, y2;
	int width, height;
308
	ItiPrivate *priv;
309 310 311

	item = GNOME_CANVAS_ITEM (iti);

312
	priv = iti->priv;
313

314
	/* Compute width, height, position */
315 316 317
	width = iti->ti->width + 2 * MARGIN_X;
	height = iti->ti->height + 2 * MARGIN_Y;

318 319 320 321 322 323
	/* Verify we are not smaller than default settings */
	if (width < priv->min_width)
		width = priv->min_width;
	if (height < priv->min_height)
		height = priv->min_height;

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
	x1 = iti->x + (iti->width - width) / 2;
	y1 = iti->y;
	x2 = x1 + width;
	y2 = y1 + height;

	/* Translate to world coordinates */
	gnome_canvas_item_i2w_affine (item, affine);

	p.x = x1;
	p.y = y1;
	art_affine_point (&q, &p, affine);
	item->x1 = q.x;
	item->y1 = q.y;

	p.x = x2;
	p.y = y2;
	art_affine_point (&q, &p, affine);
	item->x2 = q.x;
	item->y2 = q.y;
}

/* Update method for the icon text item */
static void
iti_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
{
	Iti *iti;
	ItiPrivate *priv;

	iti = ITI (item);
	priv = iti->priv;

	if (parent_class->update)
		(* parent_class->update) (item, affine, clip_path, flags);

	/* If necessary, queue a redraw of the old bounding box */
	if ((flags & GNOME_CANVAS_UPDATE_VISIBILITY)
	    || (flags & GNOME_CANVAS_UPDATE_AFFINE)
	    || priv->need_pos_update
	    || priv->need_font_update
	    || priv->need_text_update)
		gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);

	/* Compute new bounds */
	if (priv->need_pos_update
	    || priv->need_font_update
	    || priv->need_text_update)
		recompute_bounding_box (iti);

	/* Queue redraw */
	gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);

	priv->need_pos_update = FALSE;
	priv->need_font_update = FALSE;
	priv->need_text_update = FALSE;
	priv->need_state_update = FALSE;
}

/* Draw the icon text item's text when it is being edited */
static void
iti_paint_text (Iti *iti, GdkDrawable *drawable, int x, int y)
{
	ItiPrivate *priv;
	GnomeIconTextInfoRow *row;
	GnomeIconTextInfo *ti;
	GtkStyle *style;
	GdkGC *fg_gc, *bg_gc;
	GdkGC *gc, *bgc, *sgc, *bsgc;
391 392 393
	GList *item;
	int xpos, len;
	int cursor, offset, i;
394 395 396 397 398 399

	priv = iti->priv;
	style = GTK_WIDGET (GNOME_CANVAS_ITEM (iti)->canvas)->style;

	ti = iti->ti;
	len = 0;
400
	y += ti->font->ascent;
401

402 403 404
	cursor = 0;
	i = -1;
	
405 406 407 408 409 410 411 412
	/*
	 * Pointers to all of the GCs we use
	 */
	gc = style->fg_gc [GTK_STATE_NORMAL];
	bgc = style->bg_gc [GTK_STATE_NORMAL];
	sgc = style->fg_gc [GTK_STATE_SELECTED];
	bsgc = style->bg_gc [GTK_STATE_SELECTED];

413
	for (item = ti->rows; item; item = item->next, len += (row ? row->text_length : 0)) {
414
		GdkWChar *text_wc;
415
		int text_length;		
416 417 418 419
		int sel_start, sel_end;

		row = item->data;

420
		if (!row) {
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
			y += ti->baseline_skip / 2;
			continue;
		}

		text_wc = row->text_wc;
		text_length = row->text_length;

		xpos = (ti->width - row->width) / 2;

		sel_start = GTK_EDITABLE (priv->entry)->selection_start_pos - len;
		sel_end = GTK_EDITABLE (priv->entry)->selection_end_pos - len;
		offset = 0;
		cursor = GTK_EDITABLE (priv->entry)->current_pos - len;

		for (i = 0; *text_wc; text_wc++, i++) {
			int size, px;

			size = gdk_text_width_wc (ti->font, text_wc, 1);

			if (i >= sel_start && i < sel_end) {
				fg_gc = sgc;
				bg_gc = bsgc;
			} else {
				fg_gc = gc;
				bg_gc = bgc;
			}

			px = x + xpos + offset;
			gdk_draw_rectangle (drawable,
					    bg_gc,
					    TRUE,
					    px,
					    y - ti->font->ascent,
					    size, ti->baseline_skip);

			gdk_draw_text_wc (drawable,
					  ti->font,
					  fg_gc,
					  px, y,
					  text_wc, 1);

			if (cursor == i)
				gdk_draw_line (drawable,
					       gc,
465
					       px,
466
					       y - ti->font->ascent,
467
					       px,
468 469 470 471
					       y + ti->font->descent - 1);

			offset += size;
		}
472 473 474
		
		y += ti->baseline_skip;
	}
475

476
	/* The i-beam should only be drawn at the end of a line of text if that line is the
477
	 * only or last line of text in a label.  We subtract one from the x position
478 479 480 481 482 483 484 485 486 487 488 489
	 * so the i-beam is not visually jammed against the edge of the bounding rect. */
	if (cursor == i){
		int px = x + xpos + offset;
		y -= ti->baseline_skip;
		
		gdk_draw_line (drawable,
			       gc,
			       px - 1,
			       y - ti->font->ascent,
			       px - 1,
			       y + ti->font->descent - 1);
	}
490 491 492 493 494 495 496 497 498 499 500

}

/* Draw method handler for the icon text item */
static void
iti_draw (GnomeCanvasItem *item, GdkDrawable *drawable, int x, int y, int width, int height)
{
	Iti *iti;
	GtkStyle *style;
	int w, h;
	int xofs, yofs;
501
	ItiPrivate *priv;
502 503

	iti = ITI (item);
504
	priv = iti->priv;
505 506 507 508

	if (iti->ti) {
		w = iti->ti->width + 2 * MARGIN_X;
		h = iti->ti->height + 2 * MARGIN_Y;
509 510 511 512 513 514

		/* Make sure we aren't smaller than default settings */
		if (w < priv->min_width)
			w = priv->min_width;
		if (h < priv->min_height)
			h = priv->min_height;			
515 516 517 518
	} else {
		w = 2 * MARGIN_X;
		h = 2 * MARGIN_Y;
	}
519
	
520 521 522 523 524 525 526 527 528 529 530 531 532
	xofs = item->x1 - x;
	yofs = item->y1 - y;

	style = GTK_WIDGET (item->canvas)->style;

	if (iti->editing) {
		gdk_draw_rectangle (drawable,
				    style->fg_gc[GTK_STATE_NORMAL],
				    FALSE,
				    xofs, yofs,
				    w - 1, h - 1);

		iti_paint_text (iti, drawable, xofs + MARGIN_X, yofs + MARGIN_Y);
533 534 535 536 537 538 539 540 541 542
	} else {

		if (iti->selected) {
			gdk_draw_rectangle (drawable,
				    style->bg_gc[GTK_STATE_SELECTED],
				    TRUE,
				    xofs, yofs,
				    w, h);
		}
		
543 544 545 546 547 548 549 550
		gnome_icon_paint_text (iti->ti,
				       drawable,
				       style->fg_gc[(iti->selected
						     ? GTK_STATE_SELECTED
						     : GTK_STATE_NORMAL)],
				       xofs + MARGIN_X,
				       yofs + MARGIN_Y,
				       GTK_JUSTIFY_CENTER);
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 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
}

/* Point method handler for the icon text item */
static double
iti_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
{
	double dx, dy;

	*actual_item = item;

	if (cx < item->x1)
		dx = item->x1 - cx;
	else if (cx > item->x2)
		dx = cx - item->x2;
	else
		dx = 0.0;

	if (cy < item->y1)
		dy = item->y1 - cy;
	else if (cy > item->y2)
		dy = cy - item->y2;
	else
		dy = 0.0;

	return sqrt (dx * dx + dy * dy);
}

/* Given X, Y, a mouse position, return a valid index inside the edited text */
static int
iti_idx_from_x_y (Iti *iti, int x, int y)
{
	ItiPrivate *priv;
	GnomeIconTextInfoRow *row;
	int lines;
	int line, col, i, idx;
	GList *l;

	priv = iti->priv;

	if (iti->ti->rows == NULL)
		return 0;

	lines = g_list_length (iti->ti->rows);
	line = y / iti->ti->baseline_skip;

	if (line < 0)
		line = 0;
	else if (lines < line + 1)
		line = lines - 1;

	/* Compute the base index for this line */
	for (l = iti->ti->rows, idx = i = 0; i < line; l = l->next, i++) {
		row = l->data;
		idx += row->text_length;
	}

	row = g_list_nth (iti->ti->rows, line)->data;
	col = 0;
	if (row != NULL) {
		int first_char;
		int last_char;

		first_char = (iti->ti->width - row->width) / 2;
		last_char = first_char + row->width;

		if (x < first_char) {
			/* nothing */
		} else if (x > last_char) {
			col = row->text_length;
		} else {
			GdkWChar *s = row->text_wc;
			int pos = first_char;

			while (pos < last_char) {
				pos += gdk_text_width_wc (iti->ti->font, s, 1);
				if (pos > x)
					break;
				col++;
				s++;
			}
		}
	}

	idx += col;

637
	g_assert (idx <= GTK_ENTRY(priv->entry)->text_size);
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

	return idx;
}

/* Starts the selection state in the icon text item */
static void
iti_start_selecting (Iti *iti, int idx, guint32 event_time)
{
	ItiPrivate *priv;
	GtkEditable *e;
	GdkCursor *ibeam;

	priv = iti->priv;
	e = GTK_EDITABLE (priv->entry);

	gtk_editable_select_region (e, idx, idx);
	gtk_editable_set_position (e, idx);
	ibeam = gdk_cursor_new (GDK_XTERM);
	gnome_canvas_item_grab (GNOME_CANVAS_ITEM (iti),
				GDK_BUTTON_RELEASE_MASK |
				GDK_POINTER_MOTION_MASK,
				ibeam, event_time);
	gdk_cursor_destroy (ibeam);

	gtk_editable_select_region (e, idx, idx);
	e->current_pos = e->selection_start_pos;
	e->has_selection = TRUE;
	iti->selecting = TRUE;

	priv->need_state_update = TRUE;
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (iti));

	gtk_signal_emit (GTK_OBJECT (iti), iti_signals[SELECTION_STARTED]);
}

/* Stops the selection state in the icon text item */
static void
iti_stop_selecting (Iti *iti, guint32 event_time)
{
	ItiPrivate *priv;
	GnomeCanvasItem *item;
	GtkEditable *e;

	priv = iti->priv;
	item = GNOME_CANVAS_ITEM (iti);
	e = GTK_EDITABLE (priv->entry);

	gnome_canvas_item_ungrab (item, event_time);
	e->has_selection = FALSE;
	iti->selecting = FALSE;

	priv->need_state_update = TRUE;
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (iti));
	gtk_signal_emit (GTK_OBJECT (iti), iti_signals[SELECTION_STOPPED]);
}

/* Handles selection range changes on the icon text item */
static void
iti_selection_motion (Iti *iti, int idx)
{
	ItiPrivate *priv;
	GtkEditable *e;

	priv = iti->priv;
	e = GTK_EDITABLE (priv->entry);

	if (idx < e->current_pos) {
		e->selection_start_pos = idx;
		e->selection_end_pos   = e->current_pos;
	} else {
		e->selection_start_pos = e->current_pos;
		e->selection_end_pos  = idx;
	}

	priv->need_state_update = TRUE;
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (iti));
}

716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 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 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
/* Position insertion point based on arrow key event */
static void
iti_handle_arrow_key_event (NautilusIconTextItem *iti, GdkEvent *event)
{
	ItiPrivate *priv;
	GnomeIconTextInfoRow *row;
	GList *list;
	GtkEditable *editable;
	int index, lines, line;
	int position, pos_count, new_position;
	int cur_count, prev_count, next_count;
	float scale;
	
	/* Get number of lines.  Do nothing if we have only one line */
	lines = g_list_length (iti->ti->rows);
	if (lines <= 1) {
		return;
	}

	/* Figure out which line the current insertion point is on */
	priv = iti->priv;
	editable = GTK_EDITABLE (priv->entry);
	pos_count = position = gtk_editable_get_position (editable);
	for (list = iti->ti->rows, line = -1, index = 1; index <= lines; index++) {				
		if (list != NULL) {
			row = list->data;			
			if (pos_count > row->text_length) {		
				list = list->next;
				pos_count -= row->text_length;
			}
			else {
				line = index;				
				break;
			}
		}
	}
	
	/* Calculate new position of insertion point */
	switch (event->key.keyval) {

		case GDK_Up:
			/* Try to set insertion point to previous line */
			if (line > 1) {
				list = g_list_nth(iti->ti->rows, line - 1);
				row = list->data;
				cur_count = row->text_length;
				list = g_list_nth(iti->ti->rows, line - 2);
				row = list->data;				
				prev_count = row->text_length;
				scale = (float)prev_count / (float)cur_count;
				new_position = pos_count * scale;
				position -= prev_count + pos_count;
				position += new_position;
				gtk_editable_set_position (editable, position);
			}
			break;
			
		case GDK_Down:
			/* Try to set insertion point to next line */
			if (line < lines) {
				int new_position;
				list = g_list_nth(iti->ti->rows, line - 1);
				row = list->data;
				cur_count = row->text_length;
				list = g_list_nth(iti->ti->rows, line);
				row = list->data;				
				next_count = row->text_length;
				scale = (float)next_count / (float)cur_count;
				new_position = pos_count * scale;
				new_position += (position + (cur_count - pos_count));
				gtk_editable_set_position (editable, new_position);
			}
			break;

		default:
			break;

	}

}

797 798 799 800 801 802 803 804 805
/* Event handler for icon text items */
static gint
iti_event (GnomeCanvasItem *item, GdkEvent *event)
{
	Iti *iti;
	ItiPrivate *priv;
	int idx;
	double x, y;
	int cx, cy;
Gene Z. Ragan's avatar
CVS:  
Gene Z. Ragan committed
806
	
807 808 809 810 811 812 813
	iti = ITI (item);
	priv = iti->priv;

	switch (event->type) {
	case GDK_KEY_PRESS:
		if (!iti->editing)
			break;
814
		
815
		switch(event->key.keyval) {
816 817
		
		/* Pass these events back to parent */		
818 819
		case GDK_Escape:
		case GDK_Return:
820
		case GDK_KP_Enter:
821
			return FALSE;
822 823 824 825 826 827 828 829

		/* Handle up and down arrow keys.  GdkEntry does not know 
		 * how to handle multi line items */
		case GDK_Up:		
		case GDK_Down:
			iti_handle_arrow_key_event(iti, event);
			break;
					
830 831 832 833 834
		default:			
			/* Check for control key operations */
			if (event->key.state & GDK_CONTROL_MASK) {
				return FALSE;
			}
835

836 837 838
			/* Register undo transaction if neccessary */	
			if (!priv->undo_registered) {
				priv->undo_registered = TRUE;
Darin Adler's avatar
Darin Adler committed
839
				register_rename_undo (iti);
840 841
			}

842 843
			/* Handle any events that reach us */
			gtk_widget_event (GTK_WIDGET (priv->entry), event);
844 845
			break;
		}
846
		
847
		/* Update text item to reflect changes */
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
		layout_text (iti);
		priv->need_text_update = TRUE;
		gnome_canvas_item_request_update (item);
		return TRUE;

	case GDK_BUTTON_PRESS:
		if (!iti->editing)
			break;

		if (iti->editing && event->button.button == 1) {
			gnome_canvas_w2c(item->canvas, event->button.x, event->button.y, &cx, &cy);			
			x = cx - (item->x1 + MARGIN_X);
			y = cy - (item->y1 + MARGIN_Y);
			idx = iti_idx_from_x_y (iti, x, y);

			iti_start_selecting (iti, idx, event->button.time);
		}
		return TRUE;

	case GDK_MOTION_NOTIFY:
		if (!iti->selecting)
			break;

		gnome_canvas_w2c(item->canvas, event->button.x, event->button.y, &cx, &cy);			
		x = cx - (item->x1 + MARGIN_X);
		y = cy - (item->y1 + MARGIN_Y);
		idx = iti_idx_from_x_y (iti, x, y);
		iti_selection_motion (iti, idx);
		return TRUE;

	case GDK_BUTTON_RELEASE:
		if (iti->selecting && event->button.button == 1)
			iti_stop_selecting (iti, event->button.time);
		else
			break;

		return TRUE;

	case GDK_FOCUS_CHANGE:
		if (iti->editing && event->focus_change.in == FALSE)
			iti_edition_accept (iti);

		return TRUE;

	default:
		break;
	}

	return FALSE;
}

/* Bounds method handler for the icon text item */
static void
iti_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
{
	Iti *iti;
	int width, height;

	iti = ITI (item);

	if (iti->ti) {
		width = iti->ti->width + 2 * MARGIN_X;
		height = iti->ti->height + 2 * MARGIN_Y;
	} else {
		width = 2 * MARGIN_X;
		height = 2 * MARGIN_Y;
	}

	*x1 = iti->x + (iti->width - width) / 2;
	*y1 = iti->y;
	*x2 = *x1 + width;
	*y2 = *y1 + height;
}

/* Class initialization function for the icon text item */
static void
iti_class_init (NautilusIconTextItemClass *text_item_class)
{
	GtkObjectClass  *object_class;
	GnomeCanvasItemClass *item_class;

	object_class = (GtkObjectClass *) text_item_class;
	item_class   = (GnomeCanvasItemClass *) text_item_class;

	parent_class = gtk_type_class (gnome_canvas_item_get_type ());

	iti_signals [TEXT_CHANGED] =
		gtk_signal_new (
			"text_changed",
			GTK_RUN_LAST,
			object_class->type,
			GTK_SIGNAL_OFFSET (NautilusIconTextItemClass, text_changed),
			gtk_marshal_BOOL__NONE,
			GTK_TYPE_BOOL, 0);

943 944 945 946 947 948 949 950 951
	iti_signals [TEXT_EDITED] =
		gtk_signal_new (
			"text_edited",
			GTK_RUN_LAST,
			object_class->type,
			GTK_SIGNAL_OFFSET (NautilusIconTextItemClass, text_edited),
			gtk_marshal_NONE__NONE,
			GTK_TYPE_NONE, 0);

952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
	iti_signals [HEIGHT_CHANGED] =
		gtk_signal_new (
			"height_changed",
			GTK_RUN_LAST,
			object_class->type,
			GTK_SIGNAL_OFFSET (NautilusIconTextItemClass, height_changed),
			gtk_marshal_NONE__NONE,
			GTK_TYPE_NONE, 0);

	iti_signals [WIDTH_CHANGED] =
		gtk_signal_new (
			"width_changed",
			GTK_RUN_LAST,
			object_class->type,
			GTK_SIGNAL_OFFSET (NautilusIconTextItemClass, width_changed),
			gtk_marshal_NONE__NONE,
			GTK_TYPE_NONE, 0);

	iti_signals[EDITING_STARTED] =
		gtk_signal_new (
			"editing_started",
			GTK_RUN_LAST,
			object_class->type,
			GTK_SIGNAL_OFFSET (NautilusIconTextItemClass, editing_started),
			gtk_marshal_NONE__NONE,
			GTK_TYPE_NONE, 0);

	iti_signals[EDITING_STOPPED] =
		gtk_signal_new (
			"editing_stopped",
			GTK_RUN_LAST,
			object_class->type,
			GTK_SIGNAL_OFFSET (NautilusIconTextItemClass, editing_stopped),
			gtk_marshal_NONE__NONE,
			GTK_TYPE_NONE, 0);

	iti_signals[SELECTION_STARTED] =
		gtk_signal_new (
			"selection_started",
			GTK_RUN_FIRST,
			object_class->type,
			GTK_SIGNAL_OFFSET (NautilusIconTextItemClass, selection_started),
			gtk_marshal_NONE__NONE,
			GTK_TYPE_NONE, 0);

	iti_signals[SELECTION_STOPPED] =
		gtk_signal_new (
			"selection_stopped",
			GTK_RUN_FIRST,
			object_class->type,
			GTK_SIGNAL_OFFSET (NautilusIconTextItemClass, selection_stopped),
			gtk_marshal_NONE__NONE,
			GTK_TYPE_NONE, 0);

	gtk_object_class_add_signals (object_class, iti_signals, LAST_SIGNAL);

	object_class->destroy = iti_destroy;
	object_class->set_arg = iti_set_arg;

	item_class->update = iti_update;
	item_class->draw = iti_draw;
	item_class->point = iti_point;
	item_class->bounds = iti_bounds;
	item_class->event = iti_event;
}

/* Object initialization function for the icon text item */
static void
iti_init (NautilusIconTextItem *iti)
{
	ItiPrivate *priv;

	priv = g_new0 (ItiPrivate, 1);
	iti->priv = priv;
}

1028 1029 1030



1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
/**
 * nautilus_icon_text_item_configure:
 * @iti: An icon text item.
 * @x: X position in which to place the item.
 * @y: Y position in which to place the item.
 * @width: Maximum width allowed for this item, to be used for word wrapping.
 * @font: Name of the fontset that should be used to display the text.
 * @text: Text that is going to be displayed.
 * @is_editable: Deprecated.
 * @is_static: Whether @text points to a static string or not.
 *
 * This routine is used to configure a &NautilusIconTextItem.
 *
 * @x and @y specify the cordinates where the item is placed inside the canvas.
 * The @x coordinate should be the leftmost position that the icon text item can
 * assume at any one time, that is, the left margin of the column in which the
 * icon is to be placed.  The @y coordinate specifies the top of the icon text
 * item.
 *
 * @width is the maximum width allowed for this icon text item.  The coordinates
 * define the upper-left corner of an icon text item with maximum width; this may
 * actually be outside the bounding box of the item if the text is narrower than
 * the maximum width.
 *
 * If @is_static is true, it means that there is no need for the item to
 * allocate memory for the string (it is a guarantee that the text is allocated
 * by the caller and it will not be deallocated during the lifetime of this
 * item).  This is an optimization to reduce memory usage for large icon sets.
 */
void
nautilus_icon_text_item_configure (NautilusIconTextItem *iti, int x, int y,
				int width, GdkFont *font,
1063
				const char *text, gboolean is_static)
1064 1065
{
	ItiPrivate *priv;
1066
	GnomeIconTextInfo *min_text_info;
1067 1068 1069 1070 1071 1072 1073 1074 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

	g_return_if_fail (iti != NULL);
	g_return_if_fail (IS_ITI (iti));
	g_return_if_fail (width > 2 * MARGIN_X);
	g_return_if_fail (text != NULL);

	priv = iti->priv;

	iti->x = x;
	iti->y = y;
	iti->width = width;

	if (iti->text && iti->is_text_allocated)
		g_free (iti->text);

	iti->is_text_allocated = !is_static;

	/* This cast is to shut up the compiler */
	if (is_static)
		iti->text = (char *) text;
	else
		iti->text = g_strdup (text);

	if (iti->font)
		gdk_font_unref (iti->font);

	iti->font = gdk_font_ref (font);

	if (priv->font)
		gdk_font_unref (priv->font);

	priv->font = NULL;
	if (font)
		priv->font = gdk_font_ref (iti->font);
	if (!priv->font)
1102
		priv->font = gdk_fontset_load (DEFAULT_FONT_NAME);
1103 1104 1105

	layout_text (iti);

1106 1107 1108 1109 1110 1111 1112 1113 1114
	/* Calculate and store min and max dimensions */	
	min_text_info = gnome_icon_layout_text (priv->font,
					  " ",
					  DEFAULT_SEPARATORS,
					  iti->width - 2 * MARGIN_X,
					  TRUE);
	priv->min_width = min_text_info->width + 2 * MARGIN_X;
	priv->min_height = min_text_info->height + 2 * MARGIN_Y;
	gnome_icon_text_info_free(min_text_info);
1115

1116 1117
	priv->undo_registered = FALSE;

1118
	/* Request update */
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 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 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
	priv->need_pos_update = TRUE;
	priv->need_font_update = TRUE;
	priv->need_text_update = TRUE;
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (iti));
}

/**
 * nautilus_icon_text_item_setxy:
 * @iti:  An icon text item.
 * @x: X position.
 * @y: Y position.
 *
 * Sets the coordinates at which the icon text item should be placed.
 *
 * See also: nautilus_icon_text_item_configure().
 */
void
nautilus_icon_text_item_setxy (NautilusIconTextItem *iti, int x, int y)
{
	ItiPrivate *priv;

	g_return_if_fail (iti != NULL);
	g_return_if_fail (IS_ITI (iti));

	priv = iti->priv;

	iti->x = x;
	iti->y = y;

	priv->need_pos_update = TRUE;
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (iti));
}

/**
 * nautilus_icon_text_item_select:
 * @iti: An icon text item
 * @sel: Whether the icon text item should be displayed as selected.
 *
 * This function is used to control whether an icon text item is displayed as
 * selected or not.  Mouse events are ignored by the item when it is unselected;
 * when the user clicks on a selected icon text item, it will start the text
 * editing process.
 */
void
nautilus_icon_text_item_select (NautilusIconTextItem *iti, int sel)
{
	ItiPrivate *priv;

	g_return_if_fail (iti != NULL);
	g_return_if_fail (IS_ITI (iti));

	priv = iti->priv;

	if (!iti->selected == !sel)
		return;

	iti->selected = sel ? TRUE : FALSE;

	if (!iti->selected && iti->editing)
		iti_edition_accept (iti);

	priv->need_state_update = TRUE;
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (iti));
}

/**
 * nautilus_icon_text_item_set_text:
 * @iti: An icon text item.
 *
 * Returns the current text string in an icon text item.  The client should not
 * free this string, as it is internal to the icon text item.
 */
void
nautilus_icon_text_item_set_text (NautilusIconTextItem *iti, const char *text)
{
	ItiPrivate *priv;

	g_return_if_fail (iti != NULL);
	g_return_if_fail (IS_ITI (iti));

	priv = iti->priv;

1201 1202 1203 1204
	gtk_entry_set_text (GTK_ENTRY(priv->entry), text);
	gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);

	layout_text (iti);
1205

1206
	priv->need_text_update = TRUE;
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (iti));
}

/**
 * nautilus_icon_text_item_get_text:
 * @iti: An icon text item.
 *
 * Returns the current text string in an icon text item.  The client should not
 * free this string, as it is internal to the icon text item.
 */
char *
nautilus_icon_text_item_get_text (NautilusIconTextItem *iti)
{
	ItiPrivate *priv;

	g_return_val_if_fail (iti != NULL, NULL);
	g_return_val_if_fail (IS_ITI (iti), NULL);

	priv = iti->priv;

	if (iti->editing)
1228
		return gtk_entry_get_text (GTK_ENTRY(priv->entry));
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
	else
		return iti->text;
}


/**
 * nautilus_icon_text_item_start_editing:
 * @iti: An icon text item.
 *
 * Starts the editing state of an icon text item.
 **/
void
nautilus_icon_text_item_start_editing (NautilusIconTextItem *iti)
{
1243
	ItiPrivate *priv;
1244

1245 1246 1247
	g_return_if_fail (iti != NULL);
	g_return_if_fail (IS_ITI (iti));

1248 1249
	priv = iti->priv;

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
	if (iti->editing)
		return;

	iti->selected = TRUE; /* Ensure that we are selected */
	gnome_canvas_item_grab_focus (GNOME_CANVAS_ITEM (iti));
	iti_start_editing (iti);
}

/**
 * nautilus_icon_text_item_stop_editing:
 * @iti: An icon text item.
 * @accept: Whether to accept the current text or to discard it.
 *
 * Terminates the editing state of an icon text item.  The @accept argument
 * controls whether the item's current text should be accepted or discarded.  If
 * it is discarded, then the icon's original text will be restored.
 **/
void
nautilus_icon_text_item_stop_editing (NautilusIconTextItem *iti,
				   gboolean accept)
1270
{		
1271
	ItiPrivate *priv;
1272
	
1273 1274
	g_return_if_fail (iti != NULL);
	g_return_if_fail (IS_ITI (iti));
1275 1276
	
	priv = iti->priv;
1277 1278 1279 1280

	if (!iti->editing)
		return;

1281
	if (accept) {
1282
		iti_edition_accept (iti);
1283
	} else {
1284
		iti_stop_editing (iti);
1285
	}
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
}


/**
 * nautilus_icon_text_item_get_type:
 * @void:
 *
 * Registers the &NautilusIconTextItem class if necessary, and returns the type ID
 * associated to it.
 *
 * Return value: the type ID of the &NautilusIconTextItem class.
 **/
GtkType
nautilus_icon_text_item_get_type (void)
{
	static GtkType iti_type = 0;

	if (!iti_type) {
		static const GtkTypeInfo iti_info = {
			"NautilusIconTextItem",
			sizeof (NautilusIconTextItem),
			sizeof (NautilusIconTextItemClass),
			(GtkClassInitFunc) iti_class_init,
			(GtkObjectInitFunc) iti_init,
			NULL, /* reserved_1 */
			NULL, /* reserved_2 */
			(GtkClassInitFunc) NULL
		};

		iti_type = gtk_type_unique (gnome_canvas_item_get_type (), &iti_info);
	}

	return iti_type;
}
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334


/**
 * nautilus_icon_text_item_get_margins:
 * @void:
 *
 * Return the x and y margins of th etext item
 **/
void
nautilus_icon_text_item_get_margins (int *x, int *y)
{
	*x = MARGIN_X;
	*y = MARGIN_Y;
}

1335

Darin Adler's avatar
Darin Adler committed
1336
/* register_undo
1337 1338 1339 1340 1341
 * 
 * Get text at start of edit operation and store in undo data as 
 * string with a key of "undo_text".
 */
static void
Darin Adler's avatar
Darin Adler committed
1342
register_rename_undo (NautilusIconTextItem *item)
1343
{
1344
	ItiPrivate *priv;
1345

Darin Adler's avatar
Darin Adler committed
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
	priv = item->priv;
	nautilus_undo_register
		(GTK_OBJECT (item),
		 restore_from_undo_snapshot_callback,
		 g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry))),
		 (GDestroyNotify) g_free,
		 _("Rename"),
		 _("Undo Rename"),
		 _("Restore the old name"),
		 _("Redo Rename"),
		 _("Restore the changed name"));
1357 1358 1359 1360 1361 1362 1363 1364 1365
}


/* restore_from_undo_snapshot_callback
 * 
 * Restore edited text to data stored in undoable.  Data is stored as 
 * a string with a key of "undo_text".
 */
static void
Darin Adler's avatar
Darin Adler committed
1366
restore_from_undo_snapshot_callback (GtkObject *target, gpointer callback_data)
1367
{
Darin Adler's avatar
Darin Adler committed
1368
	NautilusIconTextItem *item;
1369
	ItiPrivate *priv;
1370

Darin Adler's avatar
Darin Adler committed
1371 1372 1373 1374 1375
	item = NAUTILUS_ICON_TEXT_ITEM (target);
	priv = item->priv;
	
	/* Register a new undo transaction for redo. */
	register_rename_undo (item);
1376
		
Darin Adler's avatar
Darin Adler committed
1377 1378
	/* Restore the text. */
	nautilus_icon_text_item_set_text (item, callback_data);
1379

Darin Adler's avatar
Darin Adler committed
1380
	/* Reset the registered flag so we get a new item for future editing. */
1381
	priv->undo_registered = FALSE;
1382
}