nautilus-icon-canvas-item.c 96.4 KB
Newer Older
1
2
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

3
/* Nautilus - Icon canvas item class for icon container.
4
5
6
 *
 * Copyright (C) 2000 Eazel, Inc
 *
7
 * Author: Andy Hertzfeld <andy@eazel.com>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * This 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.
 *
 * This 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 this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
26
#include <math.h>
27
#include "nautilus-icon-canvas-item.h"
28

29
#include <glib/gi18n.h>
30

31
32
33
#include "nautilus-file-utilities.h"
#include "nautilus-global-preferences.h"
#include "nautilus-icon-factory.h"
34
#include "nautilus-icon-private.h"
Ramiro Estrugo's avatar
Ramiro Estrugo committed
35
36
37
#include <eel/eel-art-extensions.h>
#include <eel/eel-gdk-extensions.h>
#include <eel/eel-gdk-pixbuf-extensions.h>
38
#include <eel/eel-glib-extensions.h>
Ramiro Estrugo's avatar
Ramiro Estrugo committed
39
40
#include <eel/eel-gnome-extensions.h>
#include <eel/eel-graphic-effects.h>
41
#include <eel/eel-gtk-macros.h>
Darin Adler's avatar
Darin Adler committed
42
#include <eel/eel-pango-extensions.h>
43
#include <eel/eel-string.h>
44
#include <eel/eel-accessibility.h>
45
46
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gtk/gtksignal.h>
47
#include <gdk/gdk.h>
48
49
50
51
#include <libart_lgpl/art_rgb.h>
#include <libart_lgpl/art_rgb_affine.h>
#include <libart_lgpl/art_rgb_rgba_affine.h>
#include <libart_lgpl/art_svp_vpath.h>
52
#include <librsvg/rsvg.h>
53
#include <glib/gi18n.h>
54
#include <eel/eel-canvas-util.h>
55
56
#include <atk/atkimage.h>
#include <atk/atkcomponent.h>
57
#include <atk/atknoopobject.h>
58
59
60
#include <stdio.h>
#include <string.h>

61
#define EMBLEM_SPACING 2
62

63
/* gap between bottom of icon and start of text box */
64
#define LABEL_OFFSET 1
65
#define LABEL_LINE_SPACING 0
66

67
68
#define MAX_TEXT_WIDTH_STANDARD 135
#define MAX_TEXT_WIDTH_TIGHTER 80
Dave Camp's avatar
Dave Camp committed
69
#define MAX_TEXT_WIDTH_BESIDE 90
70

71
72
/* Private part of the NautilusIconCanvasItem structure. */
struct NautilusIconCanvasItemDetails {
73
	/* The image, text, font. */
74
	double x, y;
75
	GdkPixbuf *pixbuf;
76
	GdkPixbuf *rendered_pixbuf;
77
	GList *emblem_pixbufs;
78
	char *editable_text;		/* Text that can be modified by a renaming function */
79
	char *additional_text;		/* Text that cannot be modifed, such as file size, etc. */
80
	NautilusEmblemAttachPoints *attach_points;
81
	
82
	/* Size of the text at current font. */
83
	int text_dx;
84
85
	int text_width;
	int text_height;
86
	
87
	/* preview state */
Darin Adler's avatar
Darin Adler committed
88
	guint is_active : 1;
89

90
91
    	/* Highlight state. */
   	guint is_highlighted_for_selection : 1;
92
	guint is_highlighted_as_keyboard_focus: 1;
93
   	guint is_highlighted_for_drop : 1;
94
	guint show_stretch_handles : 1;
95
	guint is_prelit : 1;
96
97
98
99
100

	guint rendered_is_active : 1;
	guint rendered_is_highlighted_for_selection : 1;
	guint rendered_is_highlighted_for_drop : 1;
	guint rendered_is_prelit : 1;
101
	guint rendered_is_focused : 1;
102
	
103
	guint is_renaming : 1;
104
105
106
	
	guint bounds_cached : 1;
	
107
108
109
110
111
112
	guint is_visible : 1;

	GdkRectangle embedded_text_rect;
	char *embedded_text;

	/* Cached PangoLayouts. Only used if the icon is visible */
113
114
	PangoLayout *editable_text_layout;
	PangoLayout *additional_text_layout;
115
116
	PangoLayout *embedded_text_layout;

117
118
119
120
	/* Cached rectangle in canvas coordinates */
	ArtIRect canvas_rect;
	ArtIRect text_rect;
	ArtIRect emblem_rect;
121

122
123
	ArtIRect bounds_cache;
	
124
125
	/* Accessibility bits */
	GailTextUtil *text_util;
126
127
};

128
/* Object argument IDs. */
129
enum {
130
131
132
133
134
	PROP_0,
	PROP_EDITABLE_TEXT,
	PROP_ADDITIONAL_TEXT,
    	PROP_HIGHLIGHTED_FOR_SELECTION,
    	PROP_HIGHLIGHTED_AS_KEYBOARD_FOCUS,
135
    	PROP_HIGHLIGHTED_FOR_DROP
136
137
};

138
139
140
141
142
143
144
typedef enum {
	RIGHT_SIDE,
	BOTTOM_SIDE,
	LEFT_SIDE,
	TOP_SIDE
} RectangleSide;

145
146
147
148
149
150
151
152
enum {
	ACTION_OPEN,
	ACTION_MENU,
	LAST_ACTION
};

typedef struct {
        char *action_descriptions[LAST_ACTION];
153
154
	char *image_description;
	char *description;
155
156
} NautilusIconCanvasItemAccessiblePrivate;

157
158
159
160
161
typedef struct {
	NautilusIconCanvasItem *item;
	gint action_number;
} NautilusIconCanvasItemAccessibleActionContext;

162
typedef struct {
163
	NautilusIconCanvasItem *icon_item;
164
165
166
	ArtIRect icon_rect;
	RectangleSide side;
	int position;
167
	int index;
168
169
170
	GList *emblem;
} EmblemLayout;

171
#define ANTIALIAS_SELECTION_RECTANGLE TRUE
172

173
174
static int click_policy_auto_value;

175
/* GtkObject */
176
177
static void     nautilus_icon_canvas_item_class_init (NautilusIconCanvasItemClass   *class);
static void     nautilus_icon_canvas_item_init       (NautilusIconCanvasItem        *item);
178

179
/* private */
180
181
static void     draw_or_measure_label_text           (NautilusIconCanvasItem        *item,
						      GdkDrawable                   *drawable,
182
						      gboolean                       create_mask,
Dave Camp's avatar
Dave Camp committed
183
						      ArtIRect                       icon_rect);
184
185
static void     draw_label_text                      (NautilusIconCanvasItem        *item,
						      GdkDrawable                   *drawable,
186
						      gboolean                       create_mask,
Dave Camp's avatar
Dave Camp committed
187
						      ArtIRect                       icon_rect);
188
189
190
191
192
193
194
195
196
197
198
199
200
static void     measure_label_text                   (NautilusIconCanvasItem        *item);
static void     get_icon_canvas_rectangle            (NautilusIconCanvasItem        *item,
						      ArtIRect                      *rect);
static void     emblem_layout_reset                  (EmblemLayout                  *layout,
						      NautilusIconCanvasItem        *icon_item,
						      ArtIRect                       icon_rect);
static gboolean emblem_layout_next                   (EmblemLayout                  *layout,
						      GdkPixbuf                    **emblem_pixbuf,
						      ArtIRect                      *emblem_rect);
static void     draw_pixbuf                          (GdkPixbuf                     *pixbuf,
						      GdkDrawable                   *drawable,
						      int                            x,
						      int                            y);
201
202
203
204
static void     draw_mask                            (GdkPixbuf                     *pixbuf,
						      GdkDrawable                   *drawable,
						      int                            x,
						      int                            y);
Anders Carlsson's avatar
Anders Carlsson committed
205
206
207
208
209
210
211
static PangoLayout *get_label_layout                 (PangoLayout                  **layout,
						      NautilusIconCanvasItem        *item,
						      const char                    *text);
static void     draw_label_layout                    (NautilusIconCanvasItem        *item,
						      GdkDrawable                   *drawable,
						      PangoLayout                   *layout,
						      gboolean                       highlight,
212
						      GdkColor                      *label_color,
Anders Carlsson's avatar
Anders Carlsson committed
213
214
215
216
						      int                            x,
						      int                            y,
						      GdkGC                         *gc);

217
static gboolean hit_test_stretch_handle              (NautilusIconCanvasItem        *item,
218
219
						      ArtIRect                       canvas_rect,
						      GtkCornerType *corner);
220
221
222
static void     clear_rounded_corners                (GdkPixbuf                     *destination_pixbuf,
						      GdkPixbuf                     *corner_pixbuf,
						      int                            corner_size);
223
224
225
226
static void      draw_embedded_text                  (NautilusIconCanvasItem        *icon_item,
						      GdkDrawable                   *drawable,
						      int                            x,
						      int                            y);
Anders Carlsson's avatar
Anders Carlsson committed
227

228
229
static GdkPixbuf *nautilus_icon_canvas_lighten_pixbuf (GdkPixbuf* src, guint lighten_value);

230
231

static NautilusIconCanvasItemClass *parent_class = NULL;
232
static gpointer accessible_parent_class = NULL;
233

234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
static GQuark accessible_private_data_quark = 0;

static const char *nautilus_icon_canvas_item_accessible_action_names[] = {
        "open",
        "menu",
        NULL
};

static const char *nautilus_icon_canvas_item_accessible_action_descriptions[] = {
        "Open item",
        "Popup context menu",
        NULL
};


249
/* Object initialization function for the icon item. */
250
static void
251
nautilus_icon_canvas_item_init (NautilusIconCanvasItem *icon_item)
252
{
253
	static gboolean setup_auto_enums = FALSE;
254

255
256
257
258
259
260
261
	if (!setup_auto_enums) {
		eel_preferences_add_auto_enum
			(NAUTILUS_PREFERENCES_CLICK_POLICY,
			 &click_policy_auto_value);
		setup_auto_enums = TRUE;
	}

262
	icon_item->details = G_TYPE_INSTANCE_GET_PRIVATE ((icon_item), NAUTILUS_TYPE_ICON_CANVAS_ITEM, NautilusIconCanvasItemDetails);
263
	nautilus_icon_canvas_item_invalidate_label_size (icon_item);
264
265
266
}

static void
267
nautilus_icon_canvas_item_finalize (GObject *object)
268
{
269
	NautilusIconCanvasItemDetails *details;
270

271
	g_return_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (object));
272

273
	details = NAUTILUS_ICON_CANVAS_ITEM (object)->details;
274

275
	if (details->pixbuf != NULL) {
276
		g_object_unref (details->pixbuf);
277
	}
278
279
280
281
282
	
	if (details->text_util != NULL) {
		g_object_unref (details->text_util);
	}

Ramiro Estrugo's avatar
Ramiro Estrugo committed
283
	eel_gdk_pixbuf_list_free (details->emblem_pixbufs);
284
285
	g_free (details->editable_text);
	g_free (details->additional_text);
286
	g_free (details->attach_points);
287
	
288
	if (details->rendered_pixbuf != NULL) {
289
		g_object_unref (details->rendered_pixbuf);
290
	}
291
292
293
294
295
296
297
298

	if (details->editable_text_layout != NULL) {
		g_object_unref (details->editable_text_layout);
	}

	if (details->additional_text_layout != NULL) {
		g_object_unref (details->additional_text_layout);
	}
299
300
301
302
303

	if (details->embedded_text_layout != NULL) {
		g_object_unref (details->embedded_text_layout);
	}

304
	g_free (details->embedded_text);
305
	
306
	EEL_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
307
308
}
 
309
310
311
312
313
314
315
/* Currently we require pixbufs in this format (for hit testing).
 * Perhaps gdk-pixbuf will be changed so it can do the hit testing
 * and we won't have this requirement any more.
 */
static gboolean
pixbuf_is_acceptable (GdkPixbuf *pixbuf)
{
316
	return gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB
317
318
319
320
		&& ((!gdk_pixbuf_get_has_alpha (pixbuf)
		     && gdk_pixbuf_get_n_channels (pixbuf) == 3)
		    || (gdk_pixbuf_get_has_alpha (pixbuf)
			&& gdk_pixbuf_get_n_channels (pixbuf) == 4))
321
		&& gdk_pixbuf_get_bits_per_sample (pixbuf) == 8;
322
323
}

324
325
326
327
328
329
static void
nautilus_icon_canvas_item_invalidate_bounds_cache (NautilusIconCanvasItem *item)
{
	item->details->bounds_cached = FALSE;
}

330
331
332
/* invalidate the text width and height cached in the item details. */
void
nautilus_icon_canvas_item_invalidate_label_size (NautilusIconCanvasItem *item)
333
{
334
	nautilus_icon_canvas_item_invalidate_bounds_cache (item);
335
336
	item->details->text_width = -1;
	item->details->text_height = -1;
337
338
339
340
341
342
343
344
	if (item->details->editable_text_layout != NULL) {
		g_object_unref (item->details->editable_text_layout);
		item->details->editable_text_layout = NULL;
	}
	if (item->details->additional_text_layout != NULL) {
		g_object_unref (item->details->additional_text_layout);
		item->details->additional_text_layout = NULL;
	}
345
346
}

347
/* Set property handler for the icon item. */
348
static void
349
350
351
352
nautilus_icon_canvas_item_set_property (GObject        *object,
					guint           property_id,
					const GValue   *value,
					GParamSpec     *pspec)
353
{
354
	NautilusIconCanvasItem *item;
355
	NautilusIconCanvasItemDetails *details;
356

357
	item = NAUTILUS_ICON_CANVAS_ITEM (object);
358
	details = item->details;
359

360
	switch (property_id) {
361

362
363
364
	case PROP_EDITABLE_TEXT:
		if (eel_strcmp (details->editable_text,
				g_value_get_string (value)) == 0) {
365
366
367
368
			return;
		}

		g_free (details->editable_text);
369
		details->editable_text = g_strdup (g_value_get_string (value));
370
		if (details->text_util) {
371
372
			AtkObject *accessible;

373
374
			gail_text_util_text_setup (details->text_util,
						   details->editable_text);
375
376
			accessible = eel_accessibility_get_atk_object (item); 
			g_object_notify (G_OBJECT(accessible), "accessible-name");
377
		}
378
		
379
		nautilus_icon_canvas_item_invalidate_label_size (item);
380
381
		break;

382
383
384
	case PROP_ADDITIONAL_TEXT:
		if (eel_strcmp (details->additional_text,
				g_value_get_string (value)) == 0) {
385
			return;
386
		}
387

388
		g_free (details->additional_text);
389
		details->additional_text = g_strdup (g_value_get_string (value));
390
		
391
		nautilus_icon_canvas_item_invalidate_label_size (item);		
392
393
		break;

394
395
	case PROP_HIGHLIGHTED_FOR_SELECTION:
		if (!details->is_highlighted_for_selection == !g_value_get_boolean (value)) {
396
			return;
397
		}
398
		details->is_highlighted_for_selection = g_value_get_boolean (value);
399
		break;
400
         
401
402
        case PROP_HIGHLIGHTED_AS_KEYBOARD_FOCUS:
		if (!details->is_highlighted_as_keyboard_focus == !g_value_get_boolean (value)) {
403
			return;
404
		}
405
		details->is_highlighted_as_keyboard_focus = g_value_get_boolean (value);
406
407
408
409
410

		if (details->is_highlighted_as_keyboard_focus) {
			AtkObject *atk_object = eel_accessibility_for_object (object);
			atk_focus_tracker_notify (atk_object);
		}
411
		break;
412
		
413
414
        case PROP_HIGHLIGHTED_FOR_DROP:
		if (!details->is_highlighted_for_drop == !g_value_get_boolean (value)) {
415
			return;
416
		}
417
		details->is_highlighted_for_drop = g_value_get_boolean (value);
418
		break;
419
		
420
	default:
421
422
		g_warning ("nautilus_icons_view_item_item_set_arg on unknown argument");
		return;
423
	}
424
	
425
	eel_canvas_item_request_update (EEL_CANVAS_ITEM (object));
426
427
}

428
/* Get property handler for the icon item */
429
static void
430
431
432
433
nautilus_icon_canvas_item_get_property (GObject        *object,
					guint           property_id,
					GValue         *value,
					GParamSpec     *pspec)
434
{
435
	NautilusIconCanvasItemDetails *details;
436
	
437
	details = NAUTILUS_ICON_CANVAS_ITEM (object)->details;
438
	
439
	switch (property_id) {
440
		
441
442
	case PROP_EDITABLE_TEXT:
		g_value_set_string (value, details->editable_text);
443
444
		break;

445
446
	case PROP_ADDITIONAL_TEXT:
		g_value_set_string (value, details->additional_text);
447
		break;
448
		
449
450
        case PROP_HIGHLIGHTED_FOR_SELECTION:
		g_value_set_boolean (value, details->is_highlighted_for_selection);
451
                break;
452
		
453
454
        case PROP_HIGHLIGHTED_AS_KEYBOARD_FOCUS:
		g_value_set_boolean (value, details->is_highlighted_as_keyboard_focus);
455
                break;
456
		
457
458
        case PROP_HIGHLIGHTED_FOR_DROP:
		g_value_set_boolean (value, details->is_highlighted_for_drop);
459
                break;
460

461
        default:
462
		g_warning ("invalid property %d", property_id);
463
464
465
		break;
	}
}
466
      
467
468
469
GdkPixmap *
nautilus_icon_canvas_item_get_image (NautilusIconCanvasItem *item,
				     GdkBitmap **mask)
470
{
471
472
	GdkPixmap *pixmap;
	EelCanvas *canvas;
473
474
475
476
477
478
	GdkScreen *screen;
	GdkColormap *colormap;
	GdkGC *gc;
	int width, height;
	int item_offset_x, item_offset_y;
	ArtIRect icon_rect;
479
	ArtIRect emblem_rect;
480
	GdkPixbuf *pixbuf;
481
482
	GdkPixbuf *emblem_pixbuf;
	EmblemLayout emblem_layout;
483
	double item_x, item_y;
484
	
485
	g_return_val_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (item), NULL);
486

487
	canvas = EEL_CANVAS_ITEM (item)->canvas;
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
	colormap = gtk_widget_get_colormap (GTK_WIDGET (canvas));
	screen = gdk_colormap_get_screen (colormap);

	/* Assume we're updated so canvas item data is right */

	/* Calculate the offset from the top-left corner of the
	   new image to the item position (where the pixmap is placed) */
	eel_canvas_world_to_window (canvas,
				    item->details->x, item->details->y,
				    &item_x, &item_y);

	item_offset_x = item_x - EEL_CANVAS_ITEM (item)->x1;
	item_offset_y = item_y - EEL_CANVAS_ITEM (item)->y1;

	/* Calculate the width of the item */
	width = EEL_CANVAS_ITEM (item)->x2 - EEL_CANVAS_ITEM (item)->x1;
	height = EEL_CANVAS_ITEM (item)->y2 - EEL_CANVAS_ITEM (item)->y1;
505
	
506
507
508
509
510
	pixmap = gdk_pixmap_new (gdk_screen_get_root_window (screen),
				 width,	height,
				 gdk_colormap_get_visual (colormap)->depth);
	gdk_drawable_set_colormap (GDK_DRAWABLE (pixmap), colormap);

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
	pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
				 TRUE,
				 gdk_pixbuf_get_bits_per_sample (item->details->pixbuf),
				 width, height);
	gdk_pixbuf_fill (pixbuf, 0x00000000);

	gdk_pixbuf_composite (item->details->pixbuf, pixbuf,
			      item_offset_x, item_offset_y,
			      gdk_pixbuf_get_width (item->details->pixbuf),
			      gdk_pixbuf_get_height (item->details->pixbuf),
			      item_offset_x, item_offset_y, 1.0, 1.0,
			      GDK_INTERP_BILINEAR, 255);

	icon_rect.x0 = item_offset_x;
	icon_rect.y0 = item_offset_y;
	icon_rect.x1 = item_offset_x + gdk_pixbuf_get_width (item->details->pixbuf);
	icon_rect.y1 = item_offset_y + gdk_pixbuf_get_height (item->details->pixbuf);

	emblem_layout_reset (&emblem_layout, item, icon_rect);
	while (emblem_layout_next (&emblem_layout, &emblem_pixbuf, &emblem_rect)) {
		gdk_pixbuf_composite (emblem_pixbuf, pixbuf,
				      emblem_rect.x0, emblem_rect.y0,
				      gdk_pixbuf_get_width (emblem_pixbuf),
				      gdk_pixbuf_get_height (emblem_pixbuf),
				      emblem_rect.x0, emblem_rect.y0,
				      1.0, 1.0,
				      GDK_INTERP_BILINEAR, 255);
	}

540
541
542
543
544
545
	gc = gdk_gc_new (pixmap);
	gdk_draw_rectangle (pixmap, GTK_WIDGET (canvas)->style->white_gc,
			    TRUE,
			    0, 0,
			    width, height);
	gdk_draw_pixbuf (pixmap, gc, pixbuf, 
546
			 0, 0, 0, 0,
547
548
549
550
			 gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
			 GDK_RGB_DITHER_NORMAL,
			 0, 0);
	g_object_unref (gc);
551

552
553
554
555
556
557
558
559
560
561
562
	*mask = gdk_pixmap_new (gdk_screen_get_root_window (screen),
				width, height,
				1);
	gc = gdk_gc_new (*mask);
	gdk_draw_rectangle (*mask, gc,
			    TRUE,
			    0, 0,
			    width, height);
	g_object_unref (gc);
	  
	gdk_pixbuf_render_threshold_alpha (pixbuf, *mask,
563
					   0, 0, 0, 0,
564
565
566
567
568
569
570
571
					   gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
					   128);
	
	draw_embedded_text (item, GDK_DRAWABLE (pixmap),
			    item_offset_x, item_offset_y);

	draw_label_text (item, GDK_DRAWABLE (pixmap), FALSE, icon_rect);
	draw_label_text (item, GDK_DRAWABLE (*mask), TRUE, icon_rect);
572
573
574

	gdk_pixbuf_unref (pixbuf);

575
	return pixmap;
576
577
578
}

void
579
nautilus_icon_canvas_item_set_image (NautilusIconCanvasItem *item,
580
				     GdkPixbuf *image)
581
{
582
583
	NautilusIconCanvasItemDetails *details;	
	
584
	g_return_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (item));
585
586
	g_return_if_fail (image == NULL || pixbuf_is_acceptable (image));

587
	details = item->details;	
588
	if (details->pixbuf == image) {
589
590
591
592
		return;
	}

	if (image != NULL) {
593
		g_object_ref (image);
594
595
	}
	if (details->pixbuf != NULL) {
596
		g_object_unref (details->pixbuf);
597
	}
598
	if (details->rendered_pixbuf != NULL) {
599
		g_object_unref (details->rendered_pixbuf);
600
601
		details->rendered_pixbuf = NULL;
	}
602
603

	details->pixbuf = image;
604
			
605
	nautilus_icon_canvas_item_invalidate_bounds_cache (item);
606
	eel_canvas_item_request_update (EEL_CANVAS_ITEM (item));	
607
608
}

609
void
610
nautilus_icon_canvas_item_set_emblems (NautilusIconCanvasItem *item,
611
				       GList *emblem_pixbufs)
612
{
613
614
	GList *p;

615
	g_return_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (item));
616

617
	g_assert (item->details->emblem_pixbufs != emblem_pixbufs || emblem_pixbufs == NULL);
618
619
620
621

	/* The case where the emblems are identical is fairly common,
	 * so lets take the time to check for it.
	 */
Ramiro Estrugo's avatar
Ramiro Estrugo committed
622
	if (eel_g_list_equal (item->details->emblem_pixbufs, emblem_pixbufs)) {
623
624
625
		return;
	}

626
627
628
629
630
	/* Check if they are acceptable. */
	for (p = emblem_pixbufs; p != NULL; p = p->next) {
		g_return_if_fail (pixbuf_is_acceptable (p->data));
	}
	
631
	/* Take in the new list of emblems. */
Ramiro Estrugo's avatar
Ramiro Estrugo committed
632
633
	eel_gdk_pixbuf_list_ref (emblem_pixbufs);
	eel_gdk_pixbuf_list_free (item->details->emblem_pixbufs);
634
	item->details->emblem_pixbufs = g_list_copy (emblem_pixbufs);
635
636

	nautilus_icon_canvas_item_invalidate_bounds_cache (item);
637
	eel_canvas_item_request_update (EEL_CANVAS_ITEM (item));
638
639
}

640
641
void 
nautilus_icon_canvas_item_set_attach_points (NautilusIconCanvasItem *item,
642
					     NautilusEmblemAttachPoints *attach_points)
643
{
644
645
	g_free (item->details->attach_points);
	item->details->attach_points = NULL;
646

647
648
649
	if (attach_points != NULL && attach_points->num_points != 0) {
		item->details->attach_points = g_new (NautilusEmblemAttachPoints, 1);
		*item->details->attach_points = *attach_points;
650
	}
651
652
	
	nautilus_icon_canvas_item_invalidate_bounds_cache (item);
653
654
}

655
656
657
658
659
660
void
nautilus_icon_canvas_item_set_embedded_text_rect (NautilusIconCanvasItem       *item,
						  const GdkRectangle           *text_rect)
{
	item->details->embedded_text_rect = *text_rect;

661
	nautilus_icon_canvas_item_invalidate_bounds_cache (item);
662
663
664
665
666
667
668
	eel_canvas_item_request_update (EEL_CANVAS_ITEM (item));
}

void
nautilus_icon_canvas_item_set_embedded_text (NautilusIconCanvasItem       *item,
					     const char                   *text)
{
669
670
671
	g_free (item->details->embedded_text);
	item->details->embedded_text = g_strdup (text);

672
673
674
675
676
677
678
	if (item->details->embedded_text_layout != NULL) {
		if (text != NULL) {
			pango_layout_set_text (item->details->embedded_text_layout, text, -1);
		} else {
			pango_layout_set_text (item->details->embedded_text_layout, "", -1);
		}
	}
679
680

	eel_canvas_item_request_update (EEL_CANVAS_ITEM (item));
681
682
683
}


684
685
686
687
/* Recomputes the bounding box of a icon canvas item.
 * This is a generic implementation that could be used for any canvas item
 * class, it has no assumptions about how the item is used.
 */
688
static void
689
690
recompute_bounding_box (NautilusIconCanvasItem *icon_item,
			double i2w_dx, double i2w_dy)
691
{
692
	/* The bounds stored in the item is the same as what get_bounds
693
694
	 * returns, except it's in canvas coordinates instead of the item's
	 * parent's coordinates.
695
696
	 */

697
	EelCanvasItem *item;
698
699
	ArtPoint top_left, bottom_right;

700
	item = EEL_CANVAS_ITEM (icon_item);
701

702
703
704
	eel_canvas_item_get_bounds (item,
				    &top_left.x, &top_left.y,
				    &bottom_right.x, &bottom_right.y);
705

706
707
708
709
710
711
712
713
714
715
	top_left.x += i2w_dx;
	top_left.y += i2w_dy;
	bottom_right.x += i2w_dx;
	bottom_right.y += i2w_dy;
	eel_canvas_w2c_d (item->canvas,
			  top_left.x, top_left.y,
			  &item->x1, &item->y1);
	eel_canvas_w2c_d (item->canvas,
			  bottom_right.x, bottom_right.y,
			  &item->x2, &item->y2);
716
}
717

718
static ArtIRect
Dave Camp's avatar
Dave Camp committed
719
720
721
compute_text_rectangle (const NautilusIconCanvasItem *item,
			ArtIRect icon_rectangle,
			gboolean canvas_coords)
722
{
723
	ArtIRect text_rectangle;
724
	double pixels_per_unit;
725
	double text_width, text_height, text_dx;
726
727

	pixels_per_unit = EEL_CANVAS_ITEM (item)->canvas->pixels_per_unit;
Dave Camp's avatar
Dave Camp committed
728
729
730
	if (canvas_coords) {
		text_width = item->details->text_width;
		text_height = item->details->text_height;
731
		text_dx = item->details->text_dx;
Dave Camp's avatar
Dave Camp committed
732
733
734
	} else {
		text_width = item->details->text_width / pixels_per_unit;
		text_height = item->details->text_height / pixels_per_unit;
735
		text_dx = item->details->text_dx / pixels_per_unit;
Dave Camp's avatar
Dave Camp committed
736
737
738
739
	}
	
	if (NAUTILUS_ICON_CONTAINER (EEL_CANVAS_ITEM (item)->canvas)->details->label_position == NAUTILUS_ICON_LABEL_POSITION_BESIDE) {
                text_rectangle.x0 = icon_rectangle.x1;
740
                text_rectangle.x1 = text_rectangle.x0 + text_dx + text_width;
741
                text_rectangle.y0 = (icon_rectangle.y0 + icon_rectangle.y1) / 2- (int) text_height / 2;
Dave Camp's avatar
Dave Camp committed
742
743
                text_rectangle.y1 = text_rectangle.y0 + text_height + LABEL_OFFSET / pixels_per_unit;
	} else {
744
                text_rectangle.x0 = (icon_rectangle.x0 + icon_rectangle.x1) / 2 - (int) text_width / 2;
Dave Camp's avatar
Dave Camp committed
745
746
747
748
                text_rectangle.y0 = icon_rectangle.y1;
                text_rectangle.x1 = text_rectangle.x0 + text_width;
                text_rectangle.y1 = text_rectangle.y0 + text_height + LABEL_OFFSET / pixels_per_unit;
        }
749
750

	return text_rectangle;
751
752
}

753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
static ArtIRect
get_current_canvas_bounds (EelCanvasItem *item)
{
	ArtIRect bounds;

	g_return_val_if_fail (EEL_IS_CANVAS_ITEM (item), eel_art_irect_empty);

	bounds.x0 = item->x1;
	bounds.y0 = item->y1;
	bounds.x1 = item->x2;
	bounds.y1 = item->y2;

	return bounds;
}

768
void
769
770
nautilus_icon_canvas_item_update_bounds (NautilusIconCanvasItem *item,
					 double i2w_dx, double i2w_dy)
771
{
772
773
	ArtIRect before, after, emblem_rect;
	EmblemLayout emblem_layout;
774
	EelCanvasItem *canvas_item;
775
	GdkPixbuf *emblem_pixbuf;
Anders Carlsson's avatar
Anders Carlsson committed
776

777
778
	canvas_item = EEL_CANVAS_ITEM (item);
	
779
	/* Compute new bounds. */
780
781
782
	before = get_current_canvas_bounds (canvas_item);
	recompute_bounding_box (item, i2w_dx, i2w_dy);
	after = get_current_canvas_bounds (canvas_item);
783

784
	/* If the bounds didn't change, we are done. */
785
	if (eel_art_irect_equal (before, after)) {
786
787
		return;
	}
788
789
790
	
	/* Update canvas and text rect cache */
	get_icon_canvas_rectangle (item, &item->details->canvas_rect);
791
	item->details->text_rect = compute_text_rectangle (item, item->details->canvas_rect, TRUE);
792
793
794
795
796
797
	
	/* Update emblem rect cache */
	item->details->emblem_rect.x0 = 0;
	item->details->emblem_rect.x1 = 0;
	item->details->emblem_rect.y0 = 0;
	item->details->emblem_rect.y1 = 0;
798
	emblem_layout_reset (&emblem_layout, item, item->details->canvas_rect);
799
800
801
	while (emblem_layout_next (&emblem_layout, &emblem_pixbuf, &emblem_rect)) {
		art_irect_union (&item->details->emblem_rect, &item->details->emblem_rect, &emblem_rect);
	}
802

803
	/* queue a redraw. */
804
805
	eel_canvas_request_redraw (canvas_item->canvas,
				   before.x0, before.y0,
Gaute Lindkvist's avatar
Gaute Lindkvist committed
806
				   before.x1 + 1, before.y1 + 1);
807
808
}

809
810
/* Update handler for the icon canvas item. */
static void
811
812
813
nautilus_icon_canvas_item_update (EelCanvasItem *item,
				  double i2w_dx, double i2w_dy,
				  gint flags)
814
{
815
	nautilus_icon_canvas_item_update_bounds (NAUTILUS_ICON_CANVAS_ITEM (item), i2w_dx, i2w_dy);
816

817
	eel_canvas_item_request_redraw (EEL_CANVAS_ITEM (item));
818

819
820
	EEL_CALL_PARENT (EEL_CANVAS_ITEM_CLASS, update,
			 (item, i2w_dx, i2w_dy, flags));
821
}
822
823
824

/* Rendering */

825
static gboolean
826
in_single_click_mode (void)
827
{
828
	return click_policy_auto_value == NAUTILUS_CLICK_POLICY_SINGLE;
829
830
}

Federico Mena Quintero's avatar
Federico Mena Quintero committed
831
832
833
834
835
836
837
838
839
/* Multiplies each pixel in a pixbuf by the specified color */
static void
multiply_pixbuf_rgba (GdkPixbuf *pixbuf, guint rgba)
{
	guchar *pixels;
	int r, g, b, a;
	int width, height, rowstride;
	gboolean has_alpha;
	int x, y;
840
	guchar *p;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876

	g_return_if_fail (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
	g_return_if_fail (gdk_pixbuf_get_n_channels (pixbuf) == 3
			  || gdk_pixbuf_get_n_channels (pixbuf) == 4);

	r = EEL_RGBA_COLOR_GET_R (rgba);
	g = EEL_RGBA_COLOR_GET_G (rgba);
	b = EEL_RGBA_COLOR_GET_B (rgba);
	a = EEL_RGBA_COLOR_GET_A (rgba);

	width = gdk_pixbuf_get_width (pixbuf);
	height = gdk_pixbuf_get_height (pixbuf);
	rowstride = gdk_pixbuf_get_rowstride (pixbuf);
	has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);

	pixels = gdk_pixbuf_get_pixels (pixbuf);

	for (y = 0; y < height; y++) {
		p = pixels;

		for (x = 0; x < width; x++) {
			p[0] = p[0] * r / 255;
			p[1] = p[1] * g / 255;
			p[2] = p[2] * b / 255;

			if (has_alpha) {
				p[3] = p[3] * a / 255;
				p += 4;
			} else
				p += 3;
		}

		pixels += rowstride;
	}
}

877
878
879
880
static void
draw_frame (NautilusIconCanvasItem *item,
	    GdkDrawable *drawable,
	    guint color,
881
	    gboolean create_mask,
882
883
884
885
886
887
888
889
	    int x, 
	    int y,
	    int width,
	    int height)
{
	GdkPixbuf *selection_pixbuf;
	NautilusIconContainer *container;

890
	container = NAUTILUS_ICON_CONTAINER (EEL_CANVAS_ITEM (item)->canvas);
891
892
893
894
895
896
897
898
899
900

	if (ANTIALIAS_SELECTION_RECTANGLE) {
		selection_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
						   TRUE,
						   8,
						   width,
						   height);
		eel_gdk_pixbuf_fill_rectangle_with_color (selection_pixbuf,
							  eel_gdk_pixbuf_whole_pixbuf,
							  0xffffffff);
901
902
903
904
905
		if (container->details->highlight_frame != NULL) {
			clear_rounded_corners (selection_pixbuf,
					       container->details->highlight_frame,
					       5);
		}
906
907
		multiply_pixbuf_rgba (selection_pixbuf,
				      color);
908
909
910
911
912
		if (create_mask) {
			draw_mask (selection_pixbuf, drawable, x, y);
		} else {
			draw_pixbuf (selection_pixbuf, drawable, x, y);
		}
913
914
915
916
917
918
919
920
		g_object_unref (selection_pixbuf);
	} else {
		gdk_draw_rectangle
			(drawable, 
			 GTK_WIDGET (container)->style->black_gc, TRUE,
			 x, y, width, height);
	}
}
921

922
/* Keep these for a bit while we work on performance of draw_or_measure_label_text. */
923
/*
924
925
  #define PERFORMANCE_TEST_DRAW_DISABLE
  #define PERFORMANCE_TEST_MEASURE_DISABLE
926
927
*/

928
929
930
931
932
933
934
/* This gets the size of the layout from the position of the layout.
 * This means that if the layout is right aligned we get the full width
 * of the layout, not just the width of the text snippet on the right side
 */
static void
layout_get_full_size (PangoLayout *layout,
		      int         *width,
935
936
		      int         *height,
		      int         *dx)
937
938
{
	PangoRectangle logical_rect;
939
	int total_width;
940
941
	
	pango_layout_get_extents (layout, NULL, &logical_rect);
942
943
944
945
	*width = (logical_rect.width + PANGO_SCALE / 2) / PANGO_SCALE;
	total_width = (logical_rect.x + logical_rect.width + PANGO_SCALE / 2) / PANGO_SCALE;
	*dx = total_width - *width;
	*height = (logical_rect.height + PANGO_SCALE / 2) / PANGO_SCALE;
946
947
948
}


949
static void
950
draw_or_measure_label_text (NautilusIconCanvasItem *item,
951
			    GdkDrawable *drawable,
952
			    gboolean create_mask,
Dave Camp's avatar
Dave Camp committed
953
			    ArtIRect icon_rect)
954
{
955
	NautilusIconCanvasItemDetails *details;
956
	NautilusIconContainer *container;
957
958
	guint editable_height, editable_width, editable_dx;
	guint additional_height, additional_width, additional_dx;
959
	EelCanvasItem *canvas_item;
960
961
	PangoLayout *editable_layout;
	PangoLayout *additional_layout;
962
	GdkColor *label_color;
Anders Carlsson's avatar
Anders Carlsson committed
963
	int icon_width;
964
	gboolean have_editable, have_additional, needs_highlight, needs_frame, prelight_label;
Dave Camp's avatar
Dave Camp committed
965
966
	int max_text_width;
	int x;
Anders Carlsson's avatar
Anders Carlsson committed
967
	GdkGC *gc;
Dave Camp's avatar
Dave Camp committed
968
	ArtIRect text_rect;
969
	int text_back_padding_x, text_back_padding_y;
Anders Carlsson's avatar
Anders Carlsson committed
970
	
971
	icon_width = 0;
Anders Carlsson's avatar
Anders Carlsson committed
972
	gc = NULL;
Dave Camp's avatar
Dave Camp committed
973

974
	details = item->details;
975
	needs_highlight = details->is_highlighted_for_selection || details->is_highlighted_for_drop;