summaryrefslogtreecommitdiff
path: root/libnautilus-extensions/nautilus-icon-canvas-item.c
blob: 57b08550a1a4e0d2deb0121c372adc7c9ed85dfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
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
637
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
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
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
943
944
945
946
947
948
949
950
951
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
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
1063
1064
1065
1066
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
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
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
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
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
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* Nautilus - Icon canvas item class for icon container.
 *
 * Copyright (C) 2000 Eazel, Inc
 * Author: Andy Hertzfeld <andy@eazel.com>
 *
 * 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>
#include "nautilus-icon-canvas-item.h"

#include <math.h>
#include <string.h>
#include <stdio.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkmain.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-canvas-rect-ellipse.h>
#include <libgnomeui/gnome-canvas-util.h>
#include <libgnomeui/gnome-icon-text.h>
#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>
#include "nautilus-icon-private.h"
#include "nautilus-string.h"
#include "nautilus-art-extensions.h"
#include "nautilus-canvas-note-item.h"
#include "nautilus-glib-extensions.h"
#include "nautilus-gdk-extensions.h"
#include "nautilus-gdk-font-extensions.h"
#include "nautilus-gdk-pixbuf-extensions.h"
#include "nautilus-global-preferences.h"
#include "nautilus-gtk-macros.h"
#include "nautilus-gnome-extensions.h"
#include "nautilus-graphic-effects.h"
#include "nautilus-file-utilities.h"
#include "nautilus-icon-factory.h"
#include "nautilus-theme.h"
#include "nautilus-smooth-text-layout.h"
#include "nautilus-smooth-text-layout-cache.h"

#define EMBLEM_SPACING 2

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

#define MAX_TEXT_WIDTH_STANDARD 135
#define MAX_TEXT_WIDTH_TIGHTER 80

/* The list of characters that cause line breaks can be localized. */
static const char untranslated_line_break_characters[] = N_(" -_,;.?/&");
#define LINE_BREAK_CHARACTERS _(untranslated_line_break_characters)

/* Private part of the NautilusIconCanvasItem structure. */
struct NautilusIconCanvasItemDetails {
	/* The image, text, font. */
	GdkPixbuf *pixbuf;
	GList *emblem_pixbufs;
	char *editable_text;		/* Text that can be modified by a renaming function */
	char *additional_text;		/* Text that cannot be modifed, such as file size, etc. */
	GdkFont *font;
	NautilusEmblemAttachPoints *attach_points;
	
	/* stuff for controls; if this gets too big, we'll put it in a separate struct */
	GtkWidget *control;		/* optional Bonobo control*/
	guint control_destroy_id;
	
	/* stuff for annotations - since these are infrequently used, we probably should
	 * combine them into a structure so we only use a pointer for every item eventually
         */
	GnomeCanvasItem *annotation;
	int annotation_time_out;
	int note_state;
		
	/* Size of the text at current font. */
	int text_width;
	int text_height;
	
	/* preview state */
	guint is_active : 1;

    	/* Highlight state. */
   	guint is_highlighted_for_selection : 1;
	guint is_highlighted_as_keyboard_focus: 1;
   	guint is_highlighted_for_drop : 1;
	guint show_stretch_handles : 1;
	guint is_prelit : 1;
	guint in_control_destroy : 1;
	gboolean is_renaming;

	/* Font stuff whilst in smooth mode */
	int smooth_font_size;
	NautilusScalableFont *smooth_font;
	
	/* Cached rectangle in canvas coordinates */
	ArtIRect canvas_rect;
	ArtIRect text_rect;
	ArtIRect emblem_rect;
};

/* Object argument IDs. */
enum {
	ARG_0,
	ARG_EDITABLE_TEXT,
	ARG_ADDITIONAL_TEXT,
	ARG_FONT,
 	ARG_HIGHLIGHTED_FOR_SELECTION,
    	ARG_HIGHLIGHTED_AS_KEYBOARD_FOCUS,
    	ARG_HIGHLIGHTED_FOR_DROP,
    	ARG_MODIFIER,
	ARG_SMOOTH_FONT_SIZE,
	ARG_SMOOTH_FONT
};

typedef enum {
	RIGHT_SIDE,
	BOTTOM_SIDE,
	LEFT_SIDE,
	TOP_SIDE
} RectangleSide;

typedef enum {
	NO_HIT,
	ICON_HIT,
	LABEL_HIT,
	STRETCH_HANDLE_HIT,
	EMBLEM_HIT
} HitType;

typedef struct {
	NautilusIconCanvasItem *icon_item;
	ArtIRect icon_rect;
	RectangleSide side;
	int position;
	int index;
	GList *emblem;
} EmblemLayout;

enum {
	BOUNDS_CHANGED,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

static	guint32 highlight_background_color = NAUTILUS_RGBA_COLOR_PACK (0x00, 0x00, 0x00, 0xFF);
static	guint32 highlight_text_color	   = NAUTILUS_RGBA_COLOR_PACK (0xFF, 0xFF, 0xFF, 0xFF);
static  guint32 highlight_text_info_color  = NAUTILUS_RGBA_COLOR_PACK (0xCC, 0xCC, 0xCC, 0xFF);

static int click_policy_auto_value;

/* GtkObject */
static void     nautilus_icon_canvas_item_initialize_class (NautilusIconCanvasItemClass   *class);
static void     nautilus_icon_canvas_item_initialize       (NautilusIconCanvasItem        *item);
static void     nautilus_icon_canvas_item_destroy          (GtkObject                     *object);
static int      nautilus_icon_canvas_item_event            (GnomeCanvasItem               *item,
							    GdkEvent                      *event);
static void     nautilus_icon_canvas_item_set_arg          (GtkObject                     *object,
							    GtkArg                        *arg,
							    guint                          arg_id);
static void     nautilus_icon_canvas_item_get_arg          (GtkObject                     *object,
							    GtkArg                        *arg,
							    guint                          arg_id);



/* GnomeCanvasItem */
static void     nautilus_icon_canvas_item_update           (GnomeCanvasItem               *item,
							    double                        *affine,
							    ArtSVP                        *clip_path,
							    int                            flags);
static void     nautilus_icon_canvas_item_draw             (GnomeCanvasItem               *item,
							    GdkDrawable                   *drawable,
							    int                            x,
							    int                            y,
							    int                            width,
							    int                            height);
static void     nautilus_icon_canvas_item_render           (GnomeCanvasItem               *item,
							    GnomeCanvasBuf                *buffer);
static double   nautilus_icon_canvas_item_point            (GnomeCanvasItem               *item,
							    double                         x,
							    double                         y,
							    int                            cx,
							    int                            cy,
							    GnomeCanvasItem              **actual_item);
static void     nautilus_icon_canvas_item_bounds           (GnomeCanvasItem               *item,
							    double                        *x1,
							    double                        *y1,
							    double                        *x2,
							    double                        *y2);



/* private */
static void     draw_or_measure_label_text                 (NautilusIconCanvasItem        *item,
							    GdkDrawable                   *drawable,
							    int                            icon_left,
							    int                            icon_bottom);
static void     draw_or_measure_label_text_aa              (NautilusIconCanvasItem        *item,
							    GdkPixbuf                     *destination_pixbuf,
							    int                            icon_left,
							    int                            icon_bottom);
static void     draw_label_text                            (NautilusIconCanvasItem        *item,
							    GdkDrawable                   *drawable,
							    int                            icon_left,
							    int                            icon_bottom);
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,
							    const 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);
static gboolean hit_test_stretch_handle                    (NautilusIconCanvasItem        *item,
							    const ArtIRect                *canvas_rect);
static gboolean icon_canvas_item_is_smooth                 (const NautilusIconCanvasItem  *icon_item);

static gboolean hit_test				   (NautilusIconCanvasItem 	  *icon_item,
							    const ArtIRect 		  *canvas_rect,
							    HitType 			  *hit_type,
							    int 			  *hit_index);


NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusIconCanvasItem, nautilus_icon_canvas_item, GNOME_TYPE_CANVAS_ITEM)

static NautilusSmoothTextLayoutCache *layout_cache;

static void
free_layout_cache (void)
{
	gtk_object_unref (GTK_OBJECT (layout_cache));
}

/* Class initialization function for the icon canvas item. */
static void
nautilus_icon_canvas_item_initialize_class (NautilusIconCanvasItemClass *class)
{
	GtkObjectClass *object_class;
	GnomeCanvasItemClass *item_class;

	if (layout_cache == NULL) {
		layout_cache = nautilus_smooth_text_layout_cache_new ();
		g_atexit (free_layout_cache);
	}

	object_class = GTK_OBJECT_CLASS (class);
	item_class = GNOME_CANVAS_ITEM_CLASS (class);

	gtk_object_add_arg_type	("NautilusIconCanvasItem::editable_text",
				 GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_EDITABLE_TEXT);
	gtk_object_add_arg_type	("NautilusIconCanvasItem::additional_text",
				 GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_ADDITIONAL_TEXT);
	gtk_object_add_arg_type	("NautilusIconCanvasItem::font",
				 GTK_TYPE_BOXED, GTK_ARG_READWRITE, ARG_FONT);	
	gtk_object_add_arg_type	("NautilusIconCanvasItem::highlighted_for_selection",
				 GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_HIGHLIGHTED_FOR_SELECTION);
	gtk_object_add_arg_type	("NautilusIconCanvasItem::highlighted_as_keyboard_focus",
				 GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_HIGHLIGHTED_AS_KEYBOARD_FOCUS);
	gtk_object_add_arg_type	("NautilusIconCanvasItem::highlighted_for_drop",
				 GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_HIGHLIGHTED_FOR_DROP);
	gtk_object_add_arg_type	("NautilusIconCanvasItem::smooth_font_size",
				 GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_SMOOTH_FONT_SIZE);
	gtk_object_add_arg_type	("NautilusIconCanvasItem::smooth_font",
				 GTK_TYPE_OBJECT, GTK_ARG_READWRITE, ARG_SMOOTH_FONT);

	object_class->destroy = nautilus_icon_canvas_item_destroy;
	object_class->set_arg = nautilus_icon_canvas_item_set_arg;
	object_class->get_arg = nautilus_icon_canvas_item_get_arg;

	signals[BOUNDS_CHANGED]
		= gtk_signal_new ("bounds_changed",
				  GTK_RUN_LAST,
				  object_class->type,
				  GTK_SIGNAL_OFFSET (NautilusIconCanvasItemClass,
						     bounds_changed),
				  gtk_marshal_NONE__NONE,
				  GTK_TYPE_NONE, 0);

	gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL);

	item_class->update = nautilus_icon_canvas_item_update;
	item_class->draw = nautilus_icon_canvas_item_draw;
	item_class->render = nautilus_icon_canvas_item_render;
	item_class->point = nautilus_icon_canvas_item_point;
	item_class->bounds = nautilus_icon_canvas_item_bounds;
	item_class->event = nautilus_icon_canvas_item_event;

	nautilus_preferences_add_auto_integer (NAUTILUS_PREFERENCES_CLICK_POLICY,
					       &click_policy_auto_value);
}

/* Object initialization function for the icon item. */
static void
nautilus_icon_canvas_item_initialize (NautilusIconCanvasItem *icon_item)
{
	NautilusIconCanvasItemDetails *details;

	details = g_new0 (NautilusIconCanvasItemDetails, 1);

	icon_item->details = details;

	icon_item->details->is_renaming = FALSE;

	/* invalidate cached text dimensions initially */
	nautilus_icon_canvas_item_invalidate_label_size (icon_item);
	
	/* set up the default font and size */
	icon_item->details->smooth_font_size = 12;
	icon_item->details->smooth_font = nautilus_scalable_font_get_default_font ();
	
	icon_item->details->annotation_time_out = -1;
}

/* Destroy handler for the icon canvas item. */
static void
nautilus_icon_canvas_item_destroy (GtkObject *object)
{
	GnomeCanvasItem *item;
	NautilusIconCanvasItem *icon_item;
	NautilusIconCanvasItemDetails *details;

	g_return_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (object));

	item = GNOME_CANVAS_ITEM (object);
	icon_item = (NAUTILUS_ICON_CANVAS_ITEM (object));
	details = icon_item->details;

	gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);

	if (details->pixbuf != NULL) {
		gdk_pixbuf_unref (details->pixbuf);
	}
	nautilus_gdk_pixbuf_list_free (details->emblem_pixbufs);
	g_free (details->editable_text);
	g_free (details->additional_text);
	g_free (details->attach_points);
	
	if (details->font != NULL) {
		gdk_font_unref (details->font);
	}

	if (details->control && !details->in_control_destroy) {
		gtk_signal_disconnect (GTK_OBJECT (details->control), details->control_destroy_id);
		gtk_widget_destroy (details->control);
	}

	gtk_object_unref (GTK_OBJECT (icon_item->details->smooth_font));
	icon_item->details->smooth_font = NULL;
	
	g_free (details);

	NAUTILUS_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
}
 
/* 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)
{
	return gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB
		&& ((!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))
		&& gdk_pixbuf_get_bits_per_sample (pixbuf) == 8;
}

/* invalidate the text width and height cached in the item details. */
void
nautilus_icon_canvas_item_invalidate_label_size (NautilusIconCanvasItem *item)
{
	item->details->text_width = -1;
	item->details->text_height = -1;
}

/* abstraction layer for icon width and height, to separate it from pixbuf with and height  */
static int
nautilus_icon_canvas_item_get_icon_width (NautilusIconCanvasItem *item)
{
	GtkRequisition size_requisition;
	double scale_factor = GNOME_CANVAS_ITEM (item)->canvas->pixels_per_unit;

	if (item->details->control != NULL) {
		gtk_widget_size_request (item->details->control, &size_requisition);		
		return size_requisition.width * scale_factor;
	}
	
	if (item->details->pixbuf == NULL) {
		return	NAUTILUS_ICON_SIZE_STANDARD;
	}
		
	return gdk_pixbuf_get_width (item->details->pixbuf);
}

static int
nautilus_icon_canvas_item_get_icon_height (NautilusIconCanvasItem *item)
{
	GtkRequisition size_requisition;
	double scale_factor = GNOME_CANVAS_ITEM (item)->canvas->pixels_per_unit;

	if (item->details->control != NULL) {
		gtk_widget_size_request (item->details->control, &size_requisition);		
		return size_requisition.height * scale_factor;
	}
	if (item->details->pixbuf == NULL) {
		return	NAUTILUS_ICON_SIZE_STANDARD;
	}
	
	return gdk_pixbuf_get_height (item->details->pixbuf);
}


/* Set_arg handler for the icon item. */
static void
nautilus_icon_canvas_item_set_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
	NautilusIconCanvasItem *item;
	NautilusIconCanvasItemDetails *details;
	GdkFont *font;

	item =  NAUTILUS_ICON_CANVAS_ITEM (object);
	details = item->details;

	switch (arg_id) {

	case ARG_EDITABLE_TEXT:
		if (nautilus_strcmp (details->editable_text, GTK_VALUE_STRING (*arg)) == 0) {
			return;
		}

		g_free (details->editable_text);
		details->editable_text = g_strdup (GTK_VALUE_STRING (*arg));
		
		nautilus_icon_canvas_item_invalidate_label_size (item);		
		break;

	case ARG_ADDITIONAL_TEXT:
		if (nautilus_strcmp (details->additional_text, GTK_VALUE_STRING (*arg)) == 0) {
			return;
		}

		g_free (details->additional_text);
		details->additional_text = g_strdup (GTK_VALUE_STRING (*arg));
		
		nautilus_icon_canvas_item_invalidate_label_size (item);		
		break;

	case ARG_FONT:
		font = GTK_VALUE_BOXED (*arg);
		if (nautilus_gdk_font_equal (font, details->font)) {
			return;
		}

		if (font != NULL) {
			gdk_font_ref (font);
		}
		if (details->font != NULL) {
			gdk_font_unref (details->font);
		}
		details->font = font;
		
		nautilus_icon_canvas_item_invalidate_label_size (item);		
		break;

	case ARG_HIGHLIGHTED_FOR_SELECTION:
		if (!details->is_highlighted_for_selection == !GTK_VALUE_BOOL (*arg)) {
			return;
		}
		details->is_highlighted_for_selection = GTK_VALUE_BOOL (*arg);
		break;
         
        case ARG_HIGHLIGHTED_AS_KEYBOARD_FOCUS:
		if (!details->is_highlighted_as_keyboard_focus == !GTK_VALUE_BOOL (*arg)) {
			return;
		}
		details->is_highlighted_as_keyboard_focus = GTK_VALUE_BOOL (*arg);
		break;
		
        case ARG_HIGHLIGHTED_FOR_DROP:
		if (!details->is_highlighted_for_drop == !GTK_VALUE_BOOL (*arg)) {
			return;
		}
		details->is_highlighted_for_drop = GTK_VALUE_BOOL (*arg);
		break;

        case ARG_SMOOTH_FONT:
		nautilus_icon_canvas_item_set_smooth_font (NAUTILUS_ICON_CANVAS_ITEM (object),
							   NAUTILUS_SCALABLE_FONT (GTK_VALUE_OBJECT (*arg)));
		nautilus_icon_canvas_item_invalidate_label_size (item);				
		break;

        case ARG_SMOOTH_FONT_SIZE:
		nautilus_icon_canvas_item_set_smooth_font_size (NAUTILUS_ICON_CANVAS_ITEM (object),
								GTK_VALUE_INT (*arg));
		break;       
	
	default:
		g_warning ("nautilus_icons_view_item_item_set_arg on unknown argument");
		return;
	}
	
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (object));
}

/* handler for the control's destroy signal */
static void
do_control_destroy (GtkObject *object, gpointer data)
{
	NautilusIconCanvasItemDetails *details;
	
	details = NAUTILUS_ICON_CANVAS_ITEM (data)->details;

	details->in_control_destroy = TRUE;

	gtk_object_destroy (GTK_OBJECT (data));
}

/* Get_arg handler for the icon item */
static void
nautilus_icon_canvas_item_get_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
	NautilusIconCanvasItemDetails *details;
	GnomeCanvasItem *item;
	
	item = GNOME_CANVAS_ITEM (object);
	details = NAUTILUS_ICON_CANVAS_ITEM (object)->details;
	
	switch (arg_id) {
		
	case ARG_EDITABLE_TEXT:
		GTK_VALUE_STRING (*arg) = g_strdup (details->editable_text);
		break;

	case ARG_ADDITIONAL_TEXT:
		GTK_VALUE_STRING (*arg) = g_strdup (details->additional_text);
		break;
		
	case ARG_FONT:
		GTK_VALUE_BOXED (*arg) = details->font;
		break;
				
        case ARG_HIGHLIGHTED_FOR_SELECTION:
                GTK_VALUE_BOOL (*arg) = details->is_highlighted_for_selection;
                break;
		
        case ARG_HIGHLIGHTED_AS_KEYBOARD_FOCUS:
                GTK_VALUE_BOOL (*arg) = details->is_highlighted_as_keyboard_focus;
                break;
		
        case ARG_HIGHLIGHTED_FOR_DROP:
                GTK_VALUE_BOOL (*arg) = details->is_highlighted_for_drop;
                break;

        case ARG_SMOOTH_FONT:
		gtk_object_ref (GTK_OBJECT (details->smooth_font));
                GTK_VALUE_OBJECT (*arg) = GTK_OBJECT (details->smooth_font);
		break;

        case ARG_SMOOTH_FONT_SIZE:
                GTK_VALUE_INT (*arg) = details->smooth_font_size;
		break;
        
        default:
		arg->type = GTK_TYPE_INVALID;
		break;
	}
}

GdkPixbuf *
nautilus_icon_canvas_item_get_image (NautilusIconCanvasItem *item)
{
	NautilusIconCanvasItemDetails *details;
	int width, height;
	GdkPixbuf *pixbuf;
	
	g_return_val_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (item), NULL);

	details = item->details;

	if (details->control) {
		width = details->control->allocation.width;
		height = details->control->allocation.height;
		pixbuf = nautilus_gdk_pixbuf_get_from_window_safe (details->control->window,
								   details->control->allocation.x,
								   details->control->allocation.y,
								   details->control->allocation.width,
								   details->control->allocation.height);
	} else {
		pixbuf = details->pixbuf;
		gdk_pixbuf_ref (pixbuf);
	}
	
	return pixbuf;	
}

void
nautilus_icon_canvas_item_set_image (NautilusIconCanvasItem *item,
				     GdkPixbuf *image)
{
	NautilusIconCanvasItemDetails *details;	
	
	g_return_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (item));
	g_return_if_fail (image == NULL || pixbuf_is_acceptable (image));

	details = item->details;	
	if (details->pixbuf == image) {
		return;
	}

	if (image != NULL) {
		gdk_pixbuf_ref (image);
	}
	if (details->pixbuf != NULL) {
		gdk_pixbuf_unref (details->pixbuf);
	}

	details->pixbuf = image;
			
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (item));	
}

void
nautilus_icon_canvas_item_set_emblems (NautilusIconCanvasItem *item,
					   GList *emblem_pixbufs)
{
	GList *p;

	g_return_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (item));

	g_assert (item->details->emblem_pixbufs != emblem_pixbufs || emblem_pixbufs == NULL);

	/* The case where the emblems are identical is fairly common,
	 * so lets take the time to check for it.
	 */
	if (nautilus_g_list_equal (item->details->emblem_pixbufs, emblem_pixbufs)) {
		return;
	}

	/* Check if they are acceptable. */
	for (p = emblem_pixbufs; p != NULL; p = p->next) {
		g_return_if_fail (pixbuf_is_acceptable (p->data));
	}
	
	/* Take in the new list of emblems. */
	nautilus_gdk_pixbuf_list_ref (emblem_pixbufs);
	nautilus_gdk_pixbuf_list_free (item->details->emblem_pixbufs);
	item->details->emblem_pixbufs = g_list_copy (emblem_pixbufs);
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (item));
}

void 
nautilus_icon_canvas_item_set_attach_points (NautilusIconCanvasItem *item,
					     NautilusEmblemAttachPoints *attach_points)
{
	g_free (item->details->attach_points);
	item->details->attach_points = NULL;

	if (attach_points != NULL && attach_points->num_points != 0) {
		item->details->attach_points = g_new (NautilusEmblemAttachPoints, 1);
		*item->details->attach_points = *attach_points;
	}
}

/* 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.
 */
static void
recompute_bounding_box (NautilusIconCanvasItem *icon_item)
{
	/* The bounds stored in the item is the same as what get_bounds
	 * returns, except it's in canvas coordinates instead of the item's
	 * parent's coordinates.
	 */

	GnomeCanvasItem *item;
	ArtPoint top_left, bottom_right;
	double i2c[6];

	item = GNOME_CANVAS_ITEM (icon_item);

	gnome_canvas_item_get_bounds (item,
				      &top_left.x, &top_left.y,
				      &bottom_right.x, &bottom_right.y);

	gnome_canvas_item_i2c_affine (item->parent, i2c);

	art_affine_point (&top_left, &top_left, i2c);
	art_affine_point (&bottom_right, &bottom_right, i2c);

	item->x1 = top_left.x;
	item->y1 = top_left.y;
	item->x2 = bottom_right.x;
	item->y2 = bottom_right.y;

	if (icon_item->details->control)
		gtk_layout_move (GTK_LAYOUT (item->canvas), icon_item->details->control,
				 item->x1 + item->canvas->zoom_xofs,
				 item->y1 + item->canvas->zoom_yofs);

}

static void
compute_text_rectangle (NautilusIconCanvasItem *item,
			const ArtIRect *icon_rect,
			ArtIRect *text_rect)
{
	/* Compute text rectangle. */
	text_rect->x0 = (icon_rect->x0 + icon_rect->x1) / 2 - item->details->text_width / 2;
	text_rect->y0 = icon_rect->y1 + LABEL_OFFSET;
	text_rect->x1 = text_rect->x0 + item->details->text_width;
	text_rect->y1 = text_rect->y0 + item->details->text_height;
}


void
nautilus_icon_canvas_item_update_bounds (NautilusIconCanvasItem *item)
{
	ArtIRect before, after, emblem_rect;
	EmblemLayout emblem_layout;
	GdkPixbuf *emblem_pixbuf;
	GtkRequisition size_requisition;
	int item_width, item_height;
	
	/* Compute new bounds. */
	nautilus_gnome_canvas_item_get_current_canvas_bounds
		(GNOME_CANVAS_ITEM (item), &before);
	recompute_bounding_box (item);
	nautilus_gnome_canvas_item_get_current_canvas_bounds
		(GNOME_CANVAS_ITEM (item), &after);

	/* If the bounds didn't change, we are done. */
	if (nautilus_art_irect_equal (&before, &after)) {
		return;
	}
	
	/* Update canvas and text rect cache */
	get_icon_canvas_rectangle (item, &item->details->canvas_rect);
	compute_text_rectangle (item, &item->details->canvas_rect, &item->details->text_rect);
	
	/* 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;
	emblem_layout_reset (&emblem_layout, item, &item->details->canvas_rect);
	while (emblem_layout_next (&emblem_layout, &emblem_pixbuf, &emblem_rect)) {
		art_irect_union (&item->details->emblem_rect, &item->details->emblem_rect, &emblem_rect);
	}

	/* if there is an embedded control, make a size request and size accordingly */
	if (item->details->control) {	
		/* size the control appropriately */
		gtk_widget_size_request (item->details->control, &size_requisition);		
		item_width = size_requisition.width * GNOME_CANVAS_ITEM (item)->canvas->pixels_per_unit;
		item_height = size_requisition.height * GNOME_CANVAS_ITEM (item)->canvas->pixels_per_unit;

		gtk_widget_set_usize (item->details->control, item_width, item_height);
		}
	
	/* Send out the bounds_changed signal and queue a redraw. */
	nautilus_gnome_canvas_request_redraw_rectangle
		(GNOME_CANVAS_ITEM (item)->canvas, &before);
	gtk_signal_emit (GTK_OBJECT (item),
			 signals[BOUNDS_CHANGED]);
	nautilus_gnome_canvas_item_request_redraw
		(GNOME_CANVAS_ITEM (item));
}

/* Update handler for the icon canvas item. */
static void
nautilus_icon_canvas_item_update (GnomeCanvasItem *item,
				      double *affine,
				      ArtSVP *clip_path,
				      int flags)
{
	nautilus_icon_canvas_item_update_bounds (NAUTILUS_ICON_CANVAS_ITEM (item));
	nautilus_gnome_canvas_item_request_redraw (item);
	NAUTILUS_CALL_PARENT (GNOME_CANVAS_ITEM_CLASS, update,
			      (item, affine, clip_path, flags));
}

/* Rendering */

/* routine to underline the text in a gnome_icon_text structure */

static void
gnome_icon_underline_text (GnomeIconTextInfo *text_info,
			   GdkDrawable *drawable,
			   GdkGC *gc,
			   int x, int y)
{
	GList *item;
	int text_width;
	GnomeIconTextInfoRow *row;
	int xpos;

	y += text_info->font->ascent;

	for (item = text_info->rows; item; item = item->next) {
		if (item->data) {
			row = item->data;
			xpos = (text_info->width - row->width) / 2;
			text_width = gdk_text_width_wc(text_info->font, row->text_wc, row->text_length);
			gdk_draw_line(drawable, gc, x + xpos, y + 1, x + xpos + text_width, y + 1);
	
			y += text_info->baseline_skip;
		} else
			y += text_info->baseline_skip / 2;
	}
}

static gboolean
in_single_click_mode ()
{
	return click_policy_auto_value == NAUTILUS_CLICK_POLICY_SINGLE;
}

/* Keep these for a bit while we work on performance of draw_or_measure_label_text. */

/*
#define PERFORMANCE_TEST_DRAW_DISABLE
#define PERFORMANCE_TEST_MEASURE_DISABLE
*/

/* Draw the text in a box, using gnomelib routines. */
static void
draw_or_measure_label_text (NautilusIconCanvasItem *item,
			    GdkDrawable *drawable,
			    int icon_left,
			    int icon_bottom)
{
	NautilusIconCanvasItemDetails *details;
	guint width_so_far, height_so_far;
	GdkGC* gc;
	GdkGCValues save_gc;
	guint32 label_color;
	GnomeCanvasItem *canvas_item;
	int max_text_width;
	int icon_width, text_left, box_left;
	GnomeIconTextInfo *icon_text_info;
	char **pieces;
	const char *text_piece;
	int i;
	char *combined_text;
	gboolean have_editable, have_additional, needs_highlight;

	gc = NULL;
	icon_width = 0;
	
	details = item->details;
	needs_highlight = details->is_highlighted_for_selection || details->is_highlighted_for_drop;

	have_editable = details->editable_text != NULL
		&& details->editable_text[0] != '\0';
	have_additional = details->additional_text != NULL
		&& details->additional_text[0] != '\0';


	/* No font or no text, then do no work. */
	if (details->font == NULL || (!have_editable && !have_additional)) {
		details->text_height = 0;
		details->text_width = 0;			
		return;
	}

#if (defined PERFORMANCE_TEST_MEASURE_DISABLE && defined PERFORMANCE_TEST_DRAW_DISABLE)
	/* don't do any drawing and fake out the width */
	details->text_width = 80;
	details->text_height = 20;
	return;
#endif

#ifdef PERFORMANCE_TEST_MEASURE_DISABLE
	if (drawable == NULL) {
		/* fake out the width */
		details->text_width = 80;
		details->text_height = 20;
		return;
	}
#endif

#ifdef PERFORMANCE_TEST_DRAW_DISABLE
	if (drawable != NULL) {
		return;
	}
#endif

	/* Combine editable and additional text for processing */
	combined_text = g_strconcat
		(have_editable ? details->editable_text : "",
		 (have_editable && have_additional) ? "\n" : "",
		 have_additional ? details->additional_text : "",
		 NULL);

	width_so_far = 0;
	height_so_far = 0;

	canvas_item = GNOME_CANVAS_ITEM (item);
	if (drawable != NULL) {
		icon_width = details->pixbuf == NULL ? 0 : nautilus_icon_canvas_item_get_icon_width (item);
		gc = gdk_gc_new (canvas_item->canvas->layout.bin_window);
		gdk_gc_get_values (gc, &save_gc);
	}
	
	max_text_width = floor (nautilus_icon_canvas_item_get_max_text_width (item));
				
	/* if the icon is highlighted, do some set-up */
	if (needs_highlight && drawable != NULL && !details->is_renaming) {
		gdk_rgb_gc_set_foreground (gc, highlight_background_color);
		
		gdk_draw_rectangle
			(drawable, gc, TRUE,
			icon_left + (icon_width - details->text_width) / 2,
			 icon_bottom,
			 details->text_width, details->text_height);

		gdk_rgb_gc_set_foreground (gc, highlight_text_color);
	}
	
	if (!needs_highlight && drawable != NULL) {
		label_color = nautilus_icon_container_get_label_color (NAUTILUS_ICON_CONTAINER (canvas_item->canvas), TRUE);
		gdk_rgb_gc_set_foreground (gc, label_color);
	}
	
	pieces = g_strsplit (combined_text, "\n", 0);
	
	for (i = 0; (text_piece = pieces[i]) != NULL; i++) {
		/* Replace empty string with space for measurement and drawing.
		 * This makes empty lines appear, instead of being collapsed out.
		 */
		if (text_piece[0] == '\0') {
			text_piece = " ";
		}
		
		icon_text_info = gnome_icon_layout_text
			(details->font, text_piece,
			 LINE_BREAK_CHARACTERS,
			 max_text_width, TRUE);
		
		/* Draw text if we are not in user rename mode */
		if (drawable != NULL && !details->is_renaming) {
			text_left = icon_left + (icon_width - icon_text_info->width) / 2;
						
			gnome_icon_paint_text
				(icon_text_info, drawable, gc,
				 text_left, icon_bottom + height_so_far,
				 GTK_JUSTIFY_CENTER);
			
			/* if it's highlighted, embolden by drawing twice */
			if (needs_highlight) {
				gnome_icon_paint_text
					(icon_text_info, drawable, gc,
					 text_left + 1, icon_bottom + height_so_far,
					 GTK_JUSTIFY_CENTER);
			}
			
			/* if it's prelit, and we're in click-to-activate mode, underline the text */
			if (details->is_prelit && in_single_click_mode ()) {
				gnome_icon_underline_text
					(icon_text_info, drawable, gc,
					 text_left + 1, icon_bottom + height_so_far);
			}
		}

		if (drawable != NULL && i == 0) {
			if (needs_highlight) {
				gdk_rgb_gc_set_foreground (gc, highlight_text_info_color);
			} else {
				label_color = nautilus_icon_container_get_label_color (NAUTILUS_ICON_CONTAINER (canvas_item->canvas), FALSE);
				gdk_rgb_gc_set_foreground (gc, label_color);
			}
		}
		
		width_so_far = MAX (width_so_far, (guint) icon_text_info->width);
		height_so_far += icon_text_info->height;
		
		gnome_icon_text_info_free (icon_text_info);
	}
	g_strfreev (pieces);
	
	/* add slop used for highlighting, even if we're not highlighting now */
	width_so_far += 4;
	
	if (drawable != NULL) {
		/* Current calculations should match what we measured before drawing.
		 * This assumes that we will always make a separate call to measure
		 * before the call to draw. We might later decide to use this function
		 * differently and change these asserts.
		 */
#if (defined PERFORMANCE_TEST_MEASURE_DISABLE || defined PERFORMANCE_TEST_DRAW_DISABLE)
		g_assert ((int) height_so_far == details->text_height);
		g_assert ((int) width_so_far == details->text_width);
#endif

		gdk_gc_set_foreground (gc, &save_gc.foreground);
	
		box_left = icon_left + (icon_width - width_so_far) / 2;
		
		/* indicate keyboard selection by framing the text with a gray-stippled rectangle */
		if (details->is_highlighted_as_keyboard_focus) {
			gdk_gc_set_stipple (gc, nautilus_stipple_bitmap ());
			gdk_gc_set_fill (gc, GDK_STIPPLED);
			gdk_draw_rectangle
				(drawable, gc, FALSE,
				 box_left, icon_bottom - 2,
				 width_so_far, 2 + height_so_far);
		}
		
		gdk_gc_unref (gc);
	} else {
		/* If measuring, remember the width & height. */
		details->text_width = width_so_far;
		details->text_height = height_so_far;
	}

	g_free (combined_text);
}

static void
measure_label_text (NautilusIconCanvasItem *item)
{
	/* check to see if the cached values are still valid; if so, there's
	 * no work necessary
	 */
	
	if (item->details->text_width >= 0 && item->details->text_height >= 0) {
		return;
	}
	
	if (icon_canvas_item_is_smooth (item)) {
		draw_or_measure_label_text_aa (item, NULL, 0, 0);
	}
	else {
		draw_or_measure_label_text (item, NULL, 0, 0);
	}
}

static void
draw_label_text (NautilusIconCanvasItem *item, GdkDrawable *drawable,
		 int icon_left, int icon_bottom)
{
	draw_or_measure_label_text (item, drawable, icon_left, icon_bottom + LABEL_OFFSET);
}

static void
draw_stretch_handles (NautilusIconCanvasItem *item, GdkDrawable *drawable,
		      const ArtIRect *rect)
{
	GdkGC *gc;
	char *knob_filename;
	GdkPixbuf *knob_pixbuf;
	int knob_width, knob_height;
	
	if (!item->details->show_stretch_handles) {
		return;
	}

	gc = gdk_gc_new (drawable);

	knob_filename = nautilus_theme_get_image_path ("knob.png");
	knob_pixbuf = gdk_pixbuf_new_from_file (knob_filename);
	knob_width = gdk_pixbuf_get_width (knob_pixbuf);
	knob_height = gdk_pixbuf_get_height (knob_pixbuf);
	
	/* first draw the box */		
	gdk_gc_set_stipple (gc, nautilus_stipple_bitmap ());
	gdk_gc_set_fill (gc, GDK_STIPPLED);
	gdk_draw_rectangle
		(drawable, gc, FALSE,
		 rect->x0,
		 rect->y0,
		 rect->x1 - rect->x0 - 1,
		 rect->y1 - rect->y0 - 1);
	
	/* draw the stretch handles themselves */
	
	draw_pixbuf (knob_pixbuf, drawable, rect->x0, rect->y0);
	draw_pixbuf (knob_pixbuf, drawable, rect->x0,  rect->y1 - knob_height);
	draw_pixbuf (knob_pixbuf, drawable, rect->x1 - knob_width, rect->y0);
	draw_pixbuf (knob_pixbuf, drawable, rect->x1 - knob_width, rect->y1 - knob_height);
	
	g_free(knob_filename);
	gdk_pixbuf_unref(knob_pixbuf);	

	gdk_gc_unref (gc);
}

/* utility routine that uses libart to draw an outlined rectangle */
static void
draw_outline_rectangle_aa (GnomeCanvasBuf *buf, int x0, int y0, int x1, int y1, guint outline_color)
{
	ArtVpath vpath[12];
	ArtSVP *path;
	int halfwidth;
	
	halfwidth = 1.0;

	vpath[0].code = ART_MOVETO;
	vpath[0].x = x0 - halfwidth;
	vpath[0].y = y0 - halfwidth;

	vpath[1].code = ART_LINETO;
	vpath[1].x = x0 - halfwidth;
	vpath[1].y = y1 + halfwidth;

	vpath[2].code = ART_LINETO;
	vpath[2].x = x1 + halfwidth;
	vpath[2].y = y1 + halfwidth;

	vpath[3].code = ART_LINETO;
	vpath[3].x = x1 + halfwidth;
	vpath[3].y = y0 - halfwidth;

	vpath[4].code = ART_LINETO;
	vpath[4].x = x0 - halfwidth;
	vpath[4].y = y0 - halfwidth;

	vpath[5].code = ART_MOVETO;
	vpath[5].x = x0 + halfwidth;
	vpath[5].y = y0 + halfwidth;

	vpath[6].code = ART_LINETO;
	vpath[6].x = x1 - halfwidth;
	vpath[6].y = y0 + halfwidth;

	vpath[7].code = ART_LINETO;
	vpath[7].x = x1 - halfwidth;
	vpath[7].y = y1 - halfwidth;

	vpath[8].code = ART_LINETO;
	vpath[8].x = x0 + halfwidth;
	vpath[8].y = y1 - halfwidth;

	vpath[9].code = ART_LINETO;
	vpath[9].x = x0 + halfwidth;
	vpath[9].y = y0 + halfwidth;

	vpath[10].code = ART_END;
	vpath[10].x = 0;
	vpath[10].y = 0;
	
	path = art_svp_from_vpath(vpath);
	gnome_canvas_render_svp(buf, path, outline_color);
	art_svp_free(path);	
}

/* draw the stretch handles in the anti-aliased canvas */
static void
draw_stretch_handles_aa (NautilusIconCanvasItem *item, GnomeCanvasBuf *buf,
		      const ArtIRect *rect)
{
	int knob_width, knob_height;
	GnomeCanvasItem *canvas_item;
	char *knob_filename;
	GdkPixbuf *knob_pixbuf;
	
	if (!item->details->show_stretch_handles) {
		return;
	}

	canvas_item = GNOME_CANVAS_ITEM (item);
	
	knob_filename = nautilus_theme_get_image_path ("knob.png");
	knob_pixbuf = gdk_pixbuf_new_from_file (knob_filename);
	knob_width = gdk_pixbuf_get_width (knob_pixbuf);
	knob_height = gdk_pixbuf_get_height (knob_pixbuf);
		
	/* draw a box to connect the dots */
	draw_outline_rectangle_aa (buf, rect->x0 + 1, rect->y0 + 1,
				   rect->x1 - 1, rect->y1 - 1,
				   NAUTILUS_RGBA_COLOR_PACK (153, 153, 153, 127));	

	/* now draw the stretch handles themselves  */
	nautilus_gnome_canvas_draw_pixbuf (buf, knob_pixbuf, rect->x0, rect->y0);
	nautilus_gnome_canvas_draw_pixbuf (buf, knob_pixbuf, rect->x0, rect->y1 - knob_height);
	nautilus_gnome_canvas_draw_pixbuf (buf, knob_pixbuf, rect->x1 - knob_width, rect->y0);
	nautilus_gnome_canvas_draw_pixbuf (buf, knob_pixbuf, rect->x1 - knob_width, rect->y1 - knob_height);
			
	g_free(knob_filename);
	gdk_pixbuf_unref(knob_pixbuf);	
}

static void
emblem_layout_reset (EmblemLayout *layout, NautilusIconCanvasItem *icon_item, const ArtIRect *icon_rect)
{
	layout->icon_item = icon_item;
	layout->icon_rect = *icon_rect;
	layout->side = RIGHT_SIDE;
	layout->position = 0;
	layout->index = 0;
	layout->emblem = icon_item->details->emblem_pixbufs;
}

static gboolean
emblem_layout_next (EmblemLayout *layout,
		    GdkPixbuf **emblem_pixbuf,
		    ArtIRect *emblem_rect)
{
	GdkPixbuf *pixbuf;
	int width, height, x, y;
	NautilusEmblemAttachPoints *attach_points;
	
	/* Check if we have layed out all of the pixbufs. */
	if (layout->emblem == NULL) {
		return FALSE;
	}

	/* Get the pixbuf. */
	pixbuf = layout->emblem->data;
	width = gdk_pixbuf_get_width (pixbuf);
	height = gdk_pixbuf_get_height (pixbuf);


	/* Advance to the next emblem. */
	layout->emblem = layout->emblem->next;
	layout->index += 1;	

	attach_points = layout->icon_item->details->attach_points;
	if (attach_points != NULL) {
		if (layout->index >= attach_points->num_points) {
			return FALSE;
		}
		
		x = layout->icon_rect.x0 + attach_points->points[layout->index].x;
		y = layout->icon_rect.y0 + attach_points->points[layout->index].y;

		/* Return the rectangle and pixbuf. */
		*emblem_pixbuf = pixbuf;
		emblem_rect->x0 = x - width / 2;
		emblem_rect->y0 = y - height / 2;
		emblem_rect->x1 = emblem_rect->x0 + width;
		emblem_rect->y1 = emblem_rect->y0 + height;

		return TRUE;

	}
	
	for (;;) {

		/* Find the side to lay out along. */
		switch (layout->side) {
		case RIGHT_SIDE:
			x = layout->icon_rect.x1;
			y = layout->icon_rect.y0;
			break;
		case BOTTOM_SIDE:
			x = layout->icon_rect.x1;
			y = layout->icon_rect.y1;
			break;
		case LEFT_SIDE:
			x = layout->icon_rect.x0;
			y = layout->icon_rect.y1;
			break;
		case TOP_SIDE:
			x = layout->icon_rect.x0;
			y = layout->icon_rect.y0;
			break;
		default:
			g_assert_not_reached ();
			x = 0;
			y = 0;
			break;
		}
		if (layout->position != 0) {
			switch (layout->side) {
			case RIGHT_SIDE:
				y += layout->position + height / 2;
				break;
			case BOTTOM_SIDE:
				x -= layout->position + width / 2;
				break;
			case LEFT_SIDE:
				y -= layout->position + height / 2;
				break;
			case TOP_SIDE:
				x += layout->position + width / 2;
				break;
			}
		}
		
		/* Check to see if emblem fits in current side. */
		if (x >= layout->icon_rect.x0 && x <= layout->icon_rect.x1
		    && y >= layout->icon_rect.y0 && y <= layout->icon_rect.y1) {

			/* It fits. */

			/* Advance along the side. */
			switch (layout->side) {
			case RIGHT_SIDE:
			case LEFT_SIDE:
				layout->position += height + EMBLEM_SPACING;
				break;
			case BOTTOM_SIDE:
			case TOP_SIDE:
				layout->position += width + EMBLEM_SPACING;
				break;
			}

			/* Return the rectangle and pixbuf. */
			*emblem_pixbuf = pixbuf;
			if (layout->icon_item->details->control) {
				emblem_rect->x0 = x;
				emblem_rect->y0 = y;	
			} else {
				emblem_rect->x0 = x - width / 2;
				emblem_rect->y0 = y - height / 2;
			}

			emblem_rect->x1 = emblem_rect->x0 + width;
			emblem_rect->y1 = emblem_rect->y0 + height;
			
			return TRUE;
		}
	
		/* It doesn't fit, so move to the next side. */
		switch (layout->side) {
		case RIGHT_SIDE:
			layout->side = BOTTOM_SIDE;
			break;
		case BOTTOM_SIDE:
			layout->side = LEFT_SIDE;
			break;
		case LEFT_SIDE:
			layout->side = TOP_SIDE;
			break;
		case TOP_SIDE:
		default:
			return FALSE;
		}
		layout->position = 0;
	}
}

static void
draw_pixbuf (GdkPixbuf *pixbuf, GdkDrawable *drawable, int x, int y)
{
	/* FIXME bugzilla.eazel.com 703: 
	 * Dither would be better if we passed dither values. 
	 */
	gdk_pixbuf_render_to_drawable_alpha (pixbuf, drawable, 0, 0, x, y,
					     gdk_pixbuf_get_width (pixbuf),
					     gdk_pixbuf_get_height (pixbuf),
					     GDK_PIXBUF_ALPHA_BILEVEL, 128, GDK_RGB_DITHER_MAX,
					     0, 0);
}

/* shared code to highlight or dim the passed-in pixbuf */
static GdkPixbuf *
map_pixbuf (NautilusIconCanvasItem *icon_item)
{
	GnomeCanvas *canvas;
	char *audio_filename;
	GdkPixbuf *temp_pixbuf, *old_pixbuf, *audio_pixbuf;
	
	temp_pixbuf = icon_item->details->pixbuf;
	canvas = GNOME_CANVAS_ITEM(icon_item)->canvas;
	
	if (icon_item->details->is_prelit) {
		temp_pixbuf = nautilus_create_spotlight_pixbuf (icon_item->details->pixbuf);
		
		/* FIXME bugzilla.eazel.com 2471: This hard-wired image is inappropriate to
		 * this level of code, which shouldn't know that the
		 * preview is audio, nor should it have an icon
		 * hard-wired in.
		 */

		/* if the icon is currently being previewed, superimpose an image to indicate that */
		/* audio is the only kind of previewing right now, so this code isn't as general as it could be */
		if (icon_item->details->is_active) {
			/* Load the audio symbol. */
			audio_filename = nautilus_pixmap_file ("audio.png");
			if (audio_filename != NULL) {
				audio_pixbuf = gdk_pixbuf_new_from_file (audio_filename);
			} else {
				audio_pixbuf = NULL;
			}
			
			/* Composite it onto the icon. */
			if (audio_pixbuf != NULL) {
				gdk_pixbuf_composite
					(audio_pixbuf,
					 temp_pixbuf,
					 0, 0,
					 gdk_pixbuf_get_width (temp_pixbuf),
					 gdk_pixbuf_get_height(temp_pixbuf),
					 0, 0,
					 canvas->pixels_per_unit,
					 canvas->pixels_per_unit,
					 GDK_INTERP_BILINEAR, 0xFF);
				
				gdk_pixbuf_unref (audio_pixbuf);
			}
			
			g_free (audio_filename);
		}
	}
	
	if (icon_item->details->is_highlighted_for_selection
	    || icon_item->details->is_highlighted_for_drop) {
		old_pixbuf = temp_pixbuf;
		temp_pixbuf = nautilus_create_darkened_pixbuf (temp_pixbuf,
							       0.8 * 255,
							       0.8 * 255);
		if (old_pixbuf != icon_item->details->pixbuf) {
			gdk_pixbuf_unref (old_pixbuf);
		}
	} 

	return temp_pixbuf;
}

/* Draw the icon item for non-anti-aliased mode. */
static void
nautilus_icon_canvas_item_draw (GnomeCanvasItem *item, GdkDrawable *drawable,
				int x, int y, int width, int height)
{
	NautilusIconCanvasItem *icon_item;
	NautilusIconCanvasItemDetails *details;
	ArtIRect icon_rect, emblem_rect;
	EmblemLayout emblem_layout;
	GdkPixbuf *emblem_pixbuf, *temp_pixbuf;
			
	icon_item = NAUTILUS_ICON_CANVAS_ITEM (item);
	details = icon_item->details;


	/* Compute icon rectangle in drawable coordinates. */
	icon_rect = icon_item->details->canvas_rect;
	icon_rect.x0 -= x;
	icon_rect.y0 -= y;
	icon_rect.x1 -= x;
	icon_rect.y1 -= y;
	/* draw the icon or widget */
	if (icon_item->details->control) {
		gtk_widget_queue_draw (icon_item->details->control);
	} else {
     		if (details->pixbuf != NULL) {

			/* Compute icon rectangle in drawable coordinates. */
			get_icon_canvas_rectangle (icon_item, &icon_rect);
			icon_rect.x0 -= x;
			icon_rect.y0 -= y;
			icon_rect.x1 -= x;
			icon_rect.y1 -= y;

			/* if the pre-lit or selection flag is set, make a pre-lit or darkened pixbuf and draw that instead */
			temp_pixbuf = map_pixbuf (icon_item);
			draw_pixbuf (temp_pixbuf, drawable, icon_rect.x0, icon_rect.y0);
			
			if (temp_pixbuf != details->pixbuf) {
				gdk_pixbuf_unref (temp_pixbuf);
			}

		}

	}

	/* Draw the emblem pixbufs. */
	emblem_layout_reset (&emblem_layout, icon_item, &icon_rect);
	while (emblem_layout_next (&emblem_layout, &emblem_pixbuf, &emblem_rect)) {
		draw_pixbuf (emblem_pixbuf, drawable, emblem_rect.x0, emblem_rect.y0);
	}
	
	/* Draw stretching handles (if necessary). */
	draw_stretch_handles (icon_item, drawable, &icon_rect);
	
	/* Draw the label text. */
	draw_label_text (icon_item, drawable, icon_rect.x0, icon_rect.y1);
}

static void
draw_or_measure_label_text_aa (NautilusIconCanvasItem *item,
			       GdkPixbuf *destination_pixbuf,
			       int icon_left,
			       int icon_bottom)
{
	NautilusIconCanvasItemDetails *details;
	guint width_so_far, height_so_far;
	guint32 label_name_color;
	guint32 label_info_color;
	GnomeCanvasItem *canvas_item;
	int max_text_width;
	int icon_width, text_left, box_left;
	const NautilusSmoothTextLayout *smooth_text_layout;
	char **pieces;
	const char *text_piece;
	int i;
	char *combined_text;
	gboolean have_editable, have_additional, needs_highlight;

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

	have_editable = details->editable_text != NULL && details->editable_text[0] != '\0';
	have_additional = details->additional_text != NULL && details->additional_text[0] != '\0';

	/* No font or no text, then do no work. */
	if (!have_editable && !have_additional) {
		details->text_height = 0;
		details->text_width = 0;			
		return;
	}
	
#if (defined PERFORMANCE_TEST_MEASURE_DISABLE && defined PERFORMANCE_TEST_DRAW_DISABLE)
	/* don't do any drawing and fake out the width */
	details->text_width = 80;
	details->text_height = 20;
	return;
#endif

#ifdef PERFORMANCE_TEST_MEASURE_DISABLE
	if (destination_pixbuf == NULL) {
		/* fake out the width */
		details->text_width = 80;
		details->text_height = 20;
		return;
	}
#endif

#ifdef PERFORMANCE_TEST_DRAW_DISABLE
	if (destination_pixbuf != NULL) {
		return;
	}
#endif

	/* Combine editable and additional text for processing */
	combined_text = g_strconcat
		(have_editable ? details->editable_text : "",
		 (have_editable && have_additional) ? "\n" : "",
		 have_additional ? details->additional_text : "",
		 NULL);

	width_so_far = 0;
	height_so_far = 0;

	canvas_item = GNOME_CANVAS_ITEM (item);
	if (destination_pixbuf == NULL ) {
		icon_width = 0;
	} else {
		icon_width = details->pixbuf == NULL ? 0 : nautilus_icon_canvas_item_get_icon_width (item);
	}
	
	max_text_width = floor (nautilus_icon_canvas_item_get_max_text_width (item));

	label_name_color = nautilus_icon_container_get_label_color (NAUTILUS_ICON_CONTAINER (canvas_item->canvas), TRUE);
	label_info_color = nautilus_icon_container_get_label_color (NAUTILUS_ICON_CONTAINER (canvas_item->canvas), FALSE);
	
	pieces = g_strsplit (combined_text, "\n", 0);
	
	for (i = 0; (text_piece = pieces[i]) != NULL; i++) {
		guint32 label_color;

		if (needs_highlight) {
			if (i == 0) {
				label_color = highlight_text_color;
			}
			else {
				label_color = highlight_text_info_color;
			}
		} else {
			if (i == 0) {
				label_color = label_name_color;
			}
			else {
				label_color = label_info_color;
			}
		}
		
		/* Replace empty string with space for measurement and drawing.
		 * This makes empty lines appear, instead of being collapsed out.
		 */
		if (text_piece[0] == '\0') {
			text_piece = " ";
		}

		smooth_text_layout = nautilus_smooth_text_layout_cache_render (layout_cache,
									       text_piece,
									       strlen (text_piece),
									       details->smooth_font,
									       details->smooth_font_size,
									       TRUE, LABEL_LINE_SPACING,
									       max_text_width);
		
		/* Draw text if we are not in user rename mode */
		if (destination_pixbuf != NULL && !details->is_renaming) {
			gboolean underlined;
			ArtIRect destination_area;

			text_left = icon_left + (icon_width - nautilus_smooth_text_layout_get_width (smooth_text_layout)) / 2;
			
			/* if it's prelit, and we're in click-to-activate mode, underline the text */
			underlined = (details->is_prelit && in_single_click_mode ());
			
			/* draw the shadow in black */
			if (needs_highlight) {
				icon_bottom += 1; /* leave some space for selection frame */
				text_left -= 1;
				
				destination_area.x0 = text_left + 2;
				destination_area.y0 = icon_bottom + height_so_far + 1;
				destination_area.x1 = destination_area.x0 + nautilus_smooth_text_layout_get_width (smooth_text_layout);
				destination_area.y1 = destination_area.y0 + nautilus_smooth_text_layout_get_height (smooth_text_layout);
				nautilus_smooth_text_layout_draw_to_pixbuf (smooth_text_layout,
									    destination_pixbuf,
									    0,
									    0,
									    &destination_area,
									    GTK_JUSTIFY_CENTER,
									    underlined,
									    NAUTILUS_RGB_COLOR_BLACK,
									    0xff);
			}
			
			destination_area.x0 = text_left;
			destination_area.y0 = icon_bottom + height_so_far;
			destination_area.x1 = destination_area.x0 + nautilus_smooth_text_layout_get_width (smooth_text_layout);
			destination_area.y1 = destination_area.y0 + nautilus_smooth_text_layout_get_height (smooth_text_layout);
			nautilus_smooth_text_layout_draw_to_pixbuf (smooth_text_layout,
								    destination_pixbuf,
								    0,
								    0,
								    &destination_area,
								    GTK_JUSTIFY_CENTER,
								    underlined,
								    label_color,
								    0xff);
			
			/* if it's highlighted, embolden by drawing twice */
			if (needs_highlight) {
				destination_area.x0 = text_left + 1;
				destination_area.y0 = icon_bottom + height_so_far;
				destination_area.x1 = destination_area.x0 + nautilus_smooth_text_layout_get_width (smooth_text_layout);
				destination_area.y1 = destination_area.y0 + nautilus_smooth_text_layout_get_height (smooth_text_layout);
				nautilus_smooth_text_layout_draw_to_pixbuf (smooth_text_layout,
									    destination_pixbuf,
									    0,
									    0,
									    &destination_area,
									    GTK_JUSTIFY_CENTER,
									    underlined,
									    label_color,
									    0xff);
			}
			
		}
		
		width_so_far = MAX (width_so_far, (guint) nautilus_smooth_text_layout_get_width (smooth_text_layout));
		height_so_far += nautilus_smooth_text_layout_get_height (smooth_text_layout) + LABEL_LINE_SPACING;
		
		gtk_object_unref (GTK_OBJECT (smooth_text_layout));
	}
	g_strfreev (pieces);
	
	/* add some extra space for highlighting, even when we don't highlight so things wont move */
	height_so_far += 2; /* extra slop for nicer highlighting */	
	width_so_far += 8;  /* account for emboldening, plus extra to make it look nicer */
	
	if (destination_pixbuf != NULL) {
		/* Current calculations should match what we measured before drawing.
		 * This assumes that we will always make a separate call to measure
		 * before the call to draw. We might later decide to use this function
		 * differently and change these asserts.
		 */
#if (defined PERFORMANCE_TEST_MEASURE_DISABLE || defined PERFORMANCE_TEST_DRAW_DISABLE)
		g_assert ((int) height_so_far == details->text_height);
		g_assert ((int) width_so_far == details->text_width);
#endif
	
		box_left = icon_left + (icon_width - width_so_far) / 2;

	} else {
		/* If measuring, remember the width & height. */
		details->text_width = width_so_far;
		details->text_height = height_so_far;
	}

	g_free (combined_text);
}

/* clear the corners of the selection pixbuf by copying the corners of the passed-in pixbuf */
static void
clear_rounded_corners (GdkPixbuf *destination_pixbuf, GdkPixbuf *corner_pixbuf, int corner_size)
{
	int dest_width, dest_height, src_width, src_height;
		
	dest_width = gdk_pixbuf_get_width (destination_pixbuf);
	dest_height = gdk_pixbuf_get_height (destination_pixbuf);
	
	src_width = gdk_pixbuf_get_width (corner_pixbuf);
	src_height = gdk_pixbuf_get_height (corner_pixbuf);
	
	/* draw top left corner */
	gdk_pixbuf_copy_area (corner_pixbuf,
				0, 0,
				corner_size, corner_size,
				destination_pixbuf,
				0, 0);
	
	/* draw top right corner */
	gdk_pixbuf_copy_area (corner_pixbuf,
				src_width - corner_size, 0,
				corner_size, corner_size,
				destination_pixbuf,
				dest_width - corner_size, 0);

	/* draw bottom left corner */
	gdk_pixbuf_copy_area (corner_pixbuf,
				0, src_height - corner_size,
				corner_size, corner_size,
				destination_pixbuf,
				0, dest_height - corner_size);
	
	/* draw bottom right corner */
	gdk_pixbuf_copy_area (corner_pixbuf,
				src_width - corner_size, src_height - corner_size,
				corner_size, corner_size,
				destination_pixbuf,
				dest_width - corner_size, dest_height - corner_size);
}

static void
draw_label_text_aa (NautilusIconCanvasItem *icon_item, GnomeCanvasBuf *buf, int x, int y, int x_delta)
{
	GdkPixbuf *text_pixbuf;
	NautilusIconContainer *container;
	
	gboolean needs_highlight;
	gboolean have_editable;
	gboolean have_additional;
	
	/* make sure this is really necessary */	
	have_editable = icon_item->details->editable_text != NULL
		&& icon_item->details->editable_text[0] != '\0';
	have_additional = icon_item->details->additional_text != NULL
		&& icon_item->details->additional_text[0] != '\0';

	/* No font or no text, then do no work. */
	if (icon_item->details->smooth_font == NULL
	    || (!have_editable && !have_additional)) {
		icon_item->details->text_height = 0;
		icon_item->details->text_width = 0;			
		return;
	}
	
	if (icon_item->details->is_renaming) {
		/* Exit if we are renaming. We don't need to set the text
		* width and height to 0 because there is text, it just is not
		* drawn to the canvas while the renaming widget is dispalyed.
		*/
		return;
	}
		
	/* Set up the background. */
	needs_highlight = icon_item->details->is_highlighted_for_selection
		|| icon_item->details->is_highlighted_for_drop;

	/* Optimizing out the allocation of this pixbuf on every call is a
	 * measureable speed improvement, but only by around 5%.
	 * draw_or_measure_label_text_aa accounts for about 90% of the time.
	 */
 	text_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
 				      TRUE,
 				      8,
 				      icon_item->details->text_width,
 				      icon_item->details->text_height);
	if (needs_highlight) {
		container = NAUTILUS_ICON_CONTAINER (GNOME_CANVAS_ITEM (icon_item)->canvas);	
		nautilus_gdk_pixbuf_fill_rectangle_with_color (text_pixbuf, NULL,
							       container->details->highlight_color);
		clear_rounded_corners (text_pixbuf, container->details->highlight_frame, 5);
		
	} else {
		nautilus_gdk_pixbuf_fill_rectangle_with_color (text_pixbuf, NULL,
							       NAUTILUS_RGBA_COLOR_PACK (0, 0, 0, 0));
	}

	draw_or_measure_label_text_aa (icon_item, text_pixbuf, x_delta, 0);
	
	/* Draw the pixbuf containing the label. */

	nautilus_gnome_canvas_draw_pixbuf (buf, text_pixbuf, x - x_delta, y + LABEL_OFFSET);

	gdk_pixbuf_unref (text_pixbuf);	

	/* draw the keyboard selection focus indicator if necessary */
	if (icon_item->details->is_highlighted_as_keyboard_focus) {
		draw_outline_rectangle_aa (buf, x - x_delta + 1, y + 1,
					   x - x_delta + icon_item->details->text_width,
					   y + icon_item->details->text_height,
					   NAUTILUS_RGBA_COLOR_PACK (153, 153, 153, 127));
	}
}

/* draw the item for anti-aliased mode */
static void
nautilus_icon_canvas_item_render (GnomeCanvasItem *item, GnomeCanvasBuf *buf)
{
	ArtIRect icon_rect, emblem_rect;
	EmblemLayout emblem_layout;
	GdkPixbuf *emblem_pixbuf, *temp_pixbuf;
	NautilusIconCanvasItem *icon_item;
	int x_delta;

	icon_item = NAUTILUS_ICON_CANVAS_ITEM (item);
	
	/* map the pixbuf for selection or other effects */
	temp_pixbuf = map_pixbuf (icon_item);

	icon_rect = icon_item->details->canvas_rect;

	if (buf->is_bg) {
		gnome_canvas_buf_ensure_buf (buf);
		buf->is_bg = FALSE;
	}
	
	/* draw the icon or widget */
	if (icon_item->details->control) {
		gtk_widget_queue_draw (icon_item->details->control);
	} else {
		nautilus_gnome_canvas_draw_pixbuf (buf, temp_pixbuf, icon_rect.x0, icon_rect.y0);
	
		if (temp_pixbuf != icon_item->details->pixbuf) {
			gdk_pixbuf_unref (temp_pixbuf);
		}
	}
	
	/* draw the emblems */
	get_icon_canvas_rectangle (icon_item, &icon_rect);
	
	emblem_layout_reset (&emblem_layout, icon_item, &icon_rect);
	while (emblem_layout_next (&emblem_layout, &emblem_pixbuf, &emblem_rect)) {
		nautilus_gnome_canvas_draw_pixbuf (buf, emblem_pixbuf, emblem_rect.x0, emblem_rect.y0);
	}

	/* draw the stretch handles */
	draw_stretch_handles_aa (icon_item, buf, &icon_rect);
		
	/* draw the text */
	x_delta = (icon_item->details->text_width - (icon_rect.x1 - icon_rect.x0)) / 2;
	draw_label_text_aa (icon_item, buf, icon_rect.x0, icon_rect.y1, x_delta);
}

/* create an annotation for the emblem designated by the passed-in index */
static void
create_annotation (NautilusIconCanvasItem *icon_item, int emblem_index)
{
	uint fill_color, outline_color;
	double top, left;
	ArtDRect icon_rect;
	char *note_text;
	GnomeCanvas *canvas;
	GnomeCanvasItem *item;
	
	/* compute the position for the top left of the annotation */
	nautilus_icon_canvas_item_get_icon_rectangle (icon_item, &icon_rect);
	left = icon_rect.x0 + 8.0;
	top = icon_rect.y0 + 8.0;
		
	fill_color = 0xFFFF75E0;
	outline_color = 0x000000FF;
	
	canvas = GNOME_CANVAS_ITEM (icon_item)->canvas;
	item = GNOME_CANVAS_ITEM (icon_item);
	
	note_text = nautilus_icon_container_get_note_text (NAUTILUS_ICON_CONTAINER (canvas), icon_item->user_data, emblem_index);		

	icon_item->details->annotation = gnome_canvas_item_new
			(gnome_canvas_root (canvas),
		 	nautilus_canvas_note_item_get_type (),
		 	"x1", left,
		 	"y1", top,
		 	"fill_color_rgba", fill_color,
		 	"outline_color_rgba", outline_color,
		 	"note_text", note_text,
		 	"width_pixels", 1,
		 	NULL);

	g_free (note_text);	
	gnome_canvas_item_raise_to_top (icon_item->details->annotation);		
}

/* remove any annotation that's showing */
static void
remove_annotation (NautilusIconCanvasItem *icon_item)
{
	if (icon_item->details->annotation != NULL) {
		gtk_object_destroy (GTK_OBJECT (icon_item->details->annotation));
		icon_item->details->annotation = NULL;	
		icon_item->details->note_state = 0;	
	}
}

/* handle the timeout firing by creating the annotation */
static int
create_annotation_timeout_callback (gpointer callback_data)
{
	NautilusIconCanvasItem *icon_item;

	icon_item = NAUTILUS_ICON_CANVAS_ITEM (callback_data);
	create_annotation (icon_item, icon_item->details->note_state);

	icon_item->details->annotation_time_out = -1;	
	return 0;
}

/* manage showing and hiding annotations, based on mouse-over the passed-in emblem */
static void
nautilus_icon_canvas_item_set_note_state (NautilusIconCanvasItem *icon_item, int new_state)
{
	/* nothing to do if nothing changed */
	if (new_state == icon_item->details->note_state) {
		return;
	}
	
	/* if there already is a timeout in progress and we're showing one, just wait for it to fire */
	if (new_state > 0 && icon_item->details->annotation_time_out >= 0) {
		return;
	}
	
	/* get rid of the old annotation, if there was one */
	if (icon_item->details->annotation) {
		remove_annotation (icon_item);
	}

	if (icon_item->details->annotation_time_out >= 0) {
		gtk_timeout_remove (icon_item->details->annotation_time_out);
		icon_item->details->annotation_time_out = -1;
	}

	icon_item->details->note_state = new_state;	
	
	/* add a timeout to create the new annotation */
	if (new_state > 0) {
		icon_item->details->annotation_time_out = gtk_timeout_add (750, create_annotation_timeout_callback, icon_item);
	}	
}


/* handle events */

static int
nautilus_icon_canvas_item_event (GnomeCanvasItem *item, GdkEvent *event)
{
	NautilusIconCanvasItem *icon_item;
	GdkEventMotion *motion_event;
	ArtIRect hit_rect;
	ArtDRect world_rect;
	HitType hit_type;
	int hit_index, emblem_state;
	
	icon_item = NAUTILUS_ICON_CANVAS_ITEM (item);

	switch (event->type) {
	case GDK_ENTER_NOTIFY:
		if (!icon_item->details->is_prelit) {
			icon_item->details->is_prelit = TRUE;
			gnome_canvas_item_request_update (item);
			/* FIXME bugzilla.eazel.com 2473: 
			 * We should emit our own signal here,
			 * not one from the container; it could hook
			 * up to that signal and emit one of its
			 * own. Doing it this way hard-codes what
			 * "user_data" is. Also, the two signals
			 * should be separate. The "unpreview" signal
			 * does not have a return value.
			 */
			icon_item->details->is_active = nautilus_icon_container_emit_preview_signal
				(NAUTILUS_ICON_CONTAINER (item->canvas),
				 NAUTILUS_ICON_CANVAS_ITEM (item)->user_data,
				 TRUE);
		}
		return TRUE;
		
	case GDK_LEAVE_NOTIFY:
		if (icon_item->details->is_prelit 
		    || icon_item->details->is_highlighted_for_drop) {
			/* When leaving, turn of the prelight state and the
			 * higlighted for drop. The latter gets turned on
			 * by the drag&drop motion callback.
			 */
			/* FIXME bugzilla.eazel.com 2473: 
			 * We should emit our own signal here,
			 * not one from the containe; it could hook up
			 * to that signal and emit one of its
			 * ownr. Doing it this way hard-codes what
			 * "user_data" is. Also, the two signals
			 * should be separate. The "unpreview" signal
			 * does not have a return value.
			 */
			nautilus_icon_container_emit_preview_signal
				(NAUTILUS_ICON_CONTAINER (item->canvas),
				 NAUTILUS_ICON_CANVAS_ITEM (item)->user_data,
				 FALSE);			
			icon_item->details->is_prelit = FALSE;
			icon_item->details->is_active = 0;			
			icon_item->details->is_highlighted_for_drop = FALSE;
			
			nautilus_icon_canvas_item_set_note_state (icon_item, 0);		
			gnome_canvas_item_request_update (item);
		}
		return TRUE;
	
	case GDK_MOTION_NOTIFY:
		motion_event = (GdkEventMotion*) event;

		world_rect.x0 =  motion_event->x;
		world_rect.y0 =  motion_event->y;
		world_rect.x1 = world_rect.x0 + 1.0;
		world_rect.y1 = world_rect.y0 + 1.0;
	
		nautilus_gnome_canvas_world_to_canvas_rectangle
			(GNOME_CANVAS_ITEM (item)->canvas, &world_rect, &hit_rect);
		
		/* hit-test so we can handle tooltips for emblems */
		hit_test (icon_item, &hit_rect, &hit_type, &hit_index);
		emblem_state = hit_type == EMBLEM_HIT ? hit_index : 0;
		nautilus_icon_canvas_item_set_note_state (icon_item, emblem_state);		
		return TRUE;
			
	default:
		/* Don't eat up other events; icon container might use them. */
		return FALSE;
	}
}

static gboolean
hit_test_pixbuf (GdkPixbuf *pixbuf, const ArtIRect *pixbuf_location, const ArtIRect *probe_rect)
{
	ArtIRect relative_rect, pixbuf_rect;
	int x, y;
	guint8 *pixel;
	
	/* You can get here without a pixbuf in some strange cases. */
	if (pixbuf == NULL) {
		return FALSE;
	}
	
	/* Check to see if it's within the rectangle at all. */
	relative_rect.x0 = probe_rect->x0 - pixbuf_location->x0;
	relative_rect.y0 = probe_rect->y0 - pixbuf_location->y0;
	relative_rect.x1 = probe_rect->x1 - pixbuf_location->x0;
	relative_rect.y1 = probe_rect->y1 - pixbuf_location->y0;
	pixbuf_rect.x0 = 0;
	pixbuf_rect.y0 = 0;
	pixbuf_rect.x1 = gdk_pixbuf_get_width (pixbuf);
	pixbuf_rect.y1 = gdk_pixbuf_get_height (pixbuf);
	art_irect_intersect (&relative_rect, &relative_rect, &pixbuf_rect);
	if (art_irect_empty (&relative_rect)) {
		return FALSE;
	}

	/* If there's no alpha channel, it's opaque and we have a hit. */
	if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
		return TRUE;
	}
	g_assert (gdk_pixbuf_get_n_channels (pixbuf) == 4);
	
	/* Check the alpha channel of the pixel to see if we have a hit. */
	for (x = relative_rect.x0; x < relative_rect.x1; x++) {
		for (y = relative_rect.y0; y < relative_rect.y1; y++) {
			pixel = gdk_pixbuf_get_pixels (pixbuf)
				+ y * gdk_pixbuf_get_rowstride (pixbuf)
				+ x * 4;
			if (pixel[3] > 1) {
				return TRUE;
			}
		}
	}
	return FALSE;
}

static gboolean
hit_test (NautilusIconCanvasItem *icon_item, const ArtIRect *canvas_rect, HitType *hit_type, int *hit_index)
{
	NautilusIconCanvasItemDetails *details;
	ArtIRect icon_rect;
	ArtIRect emblem_rect;
	EmblemLayout emblem_layout;
	GdkPixbuf *emblem_pixbuf;
	
	details = icon_item->details;
	
	/* Quick check to see if the rect hits the icon, text or emblems at all. */
	if (!nautilus_art_irect_hits_irect (&icon_item->details->canvas_rect, canvas_rect)
	    && (!nautilus_art_irect_hits_irect (&details->text_rect, canvas_rect))
	    && (!nautilus_art_irect_hits_irect (&details->emblem_rect, canvas_rect))) {
		return FALSE;
	}

	/* default to -1, which means nothing was hit */
	if (hit_index != NULL) {
		*hit_index = -1;
	}
	
	/* Check for hits in the stretch handles. */
	if (hit_test_stretch_handle (icon_item, canvas_rect)) {
		if (hit_type != NULL) {
			*hit_type = STRETCH_HANDLE_HIT;
		}
		return TRUE;
	}
	
	/* Check for hit in the icon. If we're highlighted for dropping, anywhere in the rect is OK */
	get_icon_canvas_rectangle (icon_item, &icon_rect);

	/* Check for hit in the emblem pixbufs first, since they appear on top of the icon. */
	emblem_layout_reset (&emblem_layout, icon_item, &icon_item->details->canvas_rect);
	while (emblem_layout_next (&emblem_layout, &emblem_pixbuf, &emblem_rect)) {
		if (hit_test_pixbuf (emblem_pixbuf, &emblem_rect, canvas_rect)) {
			if (hit_type != NULL) {
				*hit_type = EMBLEM_HIT;
			}
			if (hit_index != NULL) {
				*hit_index = emblem_layout.index;
			}
			return TRUE;
		}	
	}
	
	if (hit_type != NULL) {
		*hit_type = ICON_HIT;
	}

	if (icon_item->details->is_highlighted_for_drop) {
		if (nautilus_art_irect_hits_irect (&icon_item->details->canvas_rect, canvas_rect)) {
			return TRUE;
		}
	} else {
		if (hit_test_pixbuf (details->pixbuf, &icon_item->details->canvas_rect, canvas_rect)) {
			return TRUE;
		}
	}

	/* Check for hit in the text. */
	if (nautilus_art_irect_hits_irect (&details->text_rect, canvas_rect)
	    && !icon_item->details->is_renaming) {
		if (hit_type != NULL) {
			*hit_type = LABEL_HIT;
		}
		return TRUE;
	}

	/* there wasn't a hit, so indicate that */
	if (hit_type != NULL) {
		*hit_type = NO_HIT;
	}

	return FALSE;
}

/* Point handler for the icon canvas item. */
static double
nautilus_icon_canvas_item_point (GnomeCanvasItem *item, double x, double y, int cx, int cy,
				 GnomeCanvasItem **actual_item)
{
	ArtIRect canvas_rect;

	*actual_item = item;
	canvas_rect.x0 = cx;
	canvas_rect.y0 = cy;
	canvas_rect.x1 = cx + 1;
	canvas_rect.y1 = cy + 1;
	if (hit_test (NAUTILUS_ICON_CANVAS_ITEM (item), &canvas_rect, NULL, NULL)) {
		return 0.0;
	} else {
		/* This value means not hit.
		 * It's kind of arbitrary. Can we do better?
		 */
		return item->canvas->pixels_per_unit * 2 + 10;
	}
}

/* Bounds handler for the icon canvas item. */
static void
nautilus_icon_canvas_item_bounds (GnomeCanvasItem *item,
				  double *x1, double *y1, double *x2, double *y2)
{
	NautilusIconCanvasItem *icon_item;
	NautilusIconCanvasItemDetails *details;
	ArtIRect icon_rect, text_rect, total_rect, emblem_rect;
	double pixels_per_unit;
	EmblemLayout emblem_layout;
	GdkPixbuf *emblem_pixbuf;
	
	g_assert (x1 != NULL);
	g_assert (y1 != NULL);
	g_assert (x2 != NULL);
	g_assert (y2 != NULL);
	
	icon_item = NAUTILUS_ICON_CANVAS_ITEM (item);
	details = icon_item->details;

	measure_label_text (icon_item);

	/* Compute icon rectangle. */
	icon_rect.x0 = 0;
	icon_rect.y0 = 0;
	if (details->pixbuf == NULL) {
		icon_rect.x1 = 0;
		icon_rect.y1 = 0;
	} else {
		icon_rect.x1 = nautilus_icon_canvas_item_get_icon_width (icon_item);
		icon_rect.y1 = nautilus_icon_canvas_item_get_icon_height (icon_item);
	}
	
	/* Compute text rectangle. */
	compute_text_rectangle (icon_item, &icon_rect, &text_rect);

	/* Compute total rectangle, adding in emblem rectangles. */
	art_irect_union (&total_rect, &icon_rect, &text_rect);
	emblem_layout_reset (&emblem_layout, icon_item, &icon_rect);
	while (emblem_layout_next (&emblem_layout, &emblem_pixbuf, &emblem_rect)) {
		art_irect_union (&total_rect, &total_rect, &emblem_rect);
	}
        
	/* Return the result. */
	pixels_per_unit = item->canvas->pixels_per_unit;
	*x1 = total_rect.x0 / pixels_per_unit;
	*y1 = total_rect.y0 / pixels_per_unit;
	*x2 = total_rect.x1 / pixels_per_unit;
	*y2 = total_rect.y1 / pixels_per_unit;
}

/* Get the rectangle of the icon only, in world coordinates. */
void
nautilus_icon_canvas_item_get_icon_rectangle (NautilusIconCanvasItem *item,
					      ArtDRect *rect)
{
	double i2w[6];
	ArtPoint art_point;
	double pixels_per_unit;
	GdkPixbuf *pixbuf;
	
	g_return_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (item));
	g_return_if_fail (rect != NULL);

	gnome_canvas_item_i2w_affine (GNOME_CANVAS_ITEM (item), i2w);

	art_point.x = 0;
	art_point.y = 0;
	art_affine_point (&art_point, &art_point, i2w);
	
	rect->x0 = art_point.x;
	rect->y0 = art_point.y;

	pixbuf = item->details->pixbuf;
	pixels_per_unit = GNOME_CANVAS_ITEM (item)->canvas->pixels_per_unit;
	
	rect->x1 = rect->x0 + (pixbuf == NULL ? 0 : nautilus_icon_canvas_item_get_icon_width (item)) / pixels_per_unit;
	rect->y1 = rect->y0 + (pixbuf == NULL ? 0 : nautilus_icon_canvas_item_get_icon_height (item)) / pixels_per_unit;
}

/* Get the rectangle of the icon only, in canvas coordinates. */
static void
get_icon_canvas_rectangle (NautilusIconCanvasItem *item,
			   ArtIRect *rect)
{
	double i2c[6];
	ArtPoint art_point;
	GdkPixbuf *pixbuf;

	g_return_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (item));
	g_return_if_fail (rect != NULL);
	
	gnome_canvas_item_i2c_affine (GNOME_CANVAS_ITEM (item), i2c);

	art_point.x = 0;
	art_point.y = 0;
	art_affine_point (&art_point, &art_point, i2c);
	
	rect->x0 = floor (art_point.x);
	rect->y0 = floor (art_point.y);

	pixbuf = item->details->pixbuf;
	
	rect->x1 = rect->x0 + (pixbuf == NULL ? 0 : nautilus_icon_canvas_item_get_icon_width (item));
	rect->y1 = rect->y0 + (pixbuf == NULL ? 0 : nautilus_icon_canvas_item_get_icon_height (item));
}

void
nautilus_icon_canvas_item_set_show_stretch_handles (NautilusIconCanvasItem *item,
						    gboolean show_stretch_handles)
{
	g_return_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (item));
	g_return_if_fail (show_stretch_handles == FALSE || show_stretch_handles == TRUE);
	
	if (!item->details->show_stretch_handles == !show_stretch_handles) {
		return;
	}

	item->details->show_stretch_handles = show_stretch_handles;
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (item));
}

/* Check whether the item is in smooth mode */
static gboolean
icon_canvas_item_is_smooth (const NautilusIconCanvasItem *icon_item)
{
	GnomeCanvas *parent_canvas;

	g_return_val_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (icon_item), FALSE);

	parent_canvas = GNOME_CANVAS (GNOME_CANVAS_ITEM (icon_item)->canvas);

	return parent_canvas->aa;
}

/* Check if one of the stretch handles was hit. */
static gboolean
hit_test_stretch_handle (NautilusIconCanvasItem *item,
			 const ArtIRect *probe_canvas_rect)
{
	ArtIRect icon_rect;
	char *knob_filename;
	GdkPixbuf *knob_pixbuf;
	int knob_width, knob_height;
	
	g_return_val_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (item), FALSE);

	/* Make sure there are handles to hit. */
	if (!item->details->show_stretch_handles) {
		return FALSE;
	}

	/* Quick check to see if the rect hits the icon at all. */
	icon_rect = item->details->canvas_rect;
	if (!nautilus_art_irect_hits_irect (probe_canvas_rect, &icon_rect)) {
		return FALSE;
	}

	knob_filename = nautilus_theme_get_image_path ("knob.png");
	knob_pixbuf = gdk_pixbuf_new_from_file (knob_filename);
	knob_width = gdk_pixbuf_get_width (knob_pixbuf);
	knob_height = gdk_pixbuf_get_height (knob_pixbuf);

	g_free(knob_filename);
	gdk_pixbuf_unref(knob_pixbuf);	
	
	/* Check for hits in the stretch handles. */
	return (probe_canvas_rect->x0 < icon_rect.x0 + knob_width
     		|| probe_canvas_rect->x1 >= icon_rect.x1 - knob_width)
		&& (probe_canvas_rect->y0 < icon_rect.y0 + knob_height
		    || probe_canvas_rect->y1 >= icon_rect.y1 - knob_height);
}

gboolean
nautilus_icon_canvas_item_hit_test_stretch_handles (NautilusIconCanvasItem *item,
						    const ArtPoint *world_point)
{
	ArtIRect canvas_rect;

	g_return_val_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (item), FALSE);
	g_return_val_if_fail (world_point != NULL, FALSE);

	gnome_canvas_w2c (GNOME_CANVAS_ITEM (item)->canvas,
			  world_point->x,
			  world_point->y,
			  &canvas_rect.x0,
			  &canvas_rect.y0);
	canvas_rect.x1 = canvas_rect.x0 + 1;
	canvas_rect.y1 = canvas_rect.y0 + 1;
	return hit_test_stretch_handle (item, &canvas_rect);
}

/* nautilus_icon_canvas_item_hit_test_rectangle
 *
 * Check and see if there is an intersection between the item and the
 * canvas rect.
 */
gboolean
nautilus_icon_canvas_item_hit_test_rectangle (NautilusIconCanvasItem *item, const ArtIRect *canvas_rect)
{

	g_return_val_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (item), FALSE);
	g_return_val_if_fail (canvas_rect != NULL, FALSE);

/*	
	nautilus_gnome_canvas_world_to_canvas_rectangle
		(GNOME_CANVAS_ITEM (item)->canvas, world_rect, &canvas_rect);
*/
	return hit_test (item, canvas_rect, NULL, NULL);
}

const char *
nautilus_icon_canvas_item_get_editable_text (NautilusIconCanvasItem *icon_item)
{
	g_return_val_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (icon_item), NULL);

	return icon_item->details->editable_text;
}

void
nautilus_icon_canvas_item_set_renaming (NautilusIconCanvasItem *item, gboolean state)
{
	g_return_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (item));
	g_return_if_fail (state == FALSE || state == TRUE);

	if (item->details->is_renaming == state) {
		return;
	}

	item->details->is_renaming = state;
	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (item));
}

double
nautilus_icon_canvas_item_get_max_text_width (NautilusIconCanvasItem *item)
{
	GnomeCanvasItem *canvas_item;
	
	canvas_item = GNOME_CANVAS_ITEM (item);
	if (nautilus_icon_container_is_tighter_layout (NAUTILUS_ICON_CONTAINER (canvas_item->canvas))) {
		return MAX_TEXT_WIDTH_TIGHTER * canvas_item->canvas->pixels_per_unit;
	} else {
		return MAX_TEXT_WIDTH_STANDARD * canvas_item->canvas->pixels_per_unit;
	}

}

void
nautilus_icon_canvas_item_set_smooth_font (NautilusIconCanvasItem	*icon_item,
					   NautilusScalableFont		*font)
{
	g_return_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (icon_item));
	g_return_if_fail (NAUTILUS_IS_SCALABLE_FONT (font));

	gtk_object_unref (GTK_OBJECT (icon_item->details->smooth_font));

	gtk_object_ref (GTK_OBJECT (font));

	icon_item->details->smooth_font = font;

	/* Only need to update if in smooth mode */
	if (icon_canvas_item_is_smooth (icon_item)) {
		gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (icon_item));
	}
}

GtkWidget *
nautilus_icon_canvas_item_get_control (NautilusIconCanvasItem *icon_item)
{
	return icon_item->details->control;
}

void
nautilus_icon_canvas_item_set_control (NautilusIconCanvasItem *icon_item, GtkWidget *control)
{
	GnomeCanvasItem *item;
	
	if (icon_item->details->control == control) {
		return;
	}

	item = GNOME_CANVAS_ITEM (icon_item);		
	if (icon_item->details->control) {
		gtk_signal_disconnect (GTK_OBJECT (icon_item->details->control), icon_item->details->control_destroy_id);
		gtk_container_remove (GTK_CONTAINER (item->canvas), icon_item->details->control);
		icon_item->details->control = NULL;
	}

	if (control) {
		icon_item->details->control = control;
		icon_item->details->control_destroy_id = gtk_signal_connect (GTK_OBJECT (control), 
							"destroy",
							(GtkSignalFunc) do_control_destroy,
							item);
		gtk_widget_show (control);
		gtk_layout_put (GTK_LAYOUT (item->canvas), control,
				item->x1 + item->canvas->zoom_xofs,
				item->y1 + item->canvas->zoom_yofs);
	}
}

void
nautilus_icon_canvas_item_set_smooth_font_size (NautilusIconCanvasItem *icon_item,
						int font_size)
{
	g_return_if_fail (NAUTILUS_IS_ICON_CANVAS_ITEM (icon_item));
	g_return_if_fail (font_size > 0);

	if (icon_item->details->smooth_font_size == font_size) {
		return;
	}

	icon_item->details->smooth_font_size = font_size;

	/* Only need to update if in smooth mode */
	if (icon_canvas_item_is_smooth (icon_item)) {
		gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (icon_item));
	}
}