aboutsummaryrefslogtreecommitdiff
path: root/Perl/Zinc/Debug.pm
blob: b50acf0698ef783958949047ca2f0550424ca354 (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
#        ZincDebug Perl Module :
#
#        For debugging/analysing a Zinc application. Only one instance
#        of zinc can be managed.
#
#        Author : Daniel Etienne <etienne@cena.fr>
#
# $Id$
#---------------------------------------------------------------------------
package ZincDebug;

use strict 'vars';
use vars qw(@ISA @EXPORT @EXPORT_OK $WARNING);
use Carp;
use English;
require Exporter;
use File::Basename;
use Tk;
use Tk::Zinc;
use Tk::LabFrame;
use Tk::Pane;
use Tk::Dialog;
use Tk::Tree;
use Tk::ItemStyle;

@ISA = qw(Exporter);
@EXPORT = qw(finditems snapshot tree);
@EXPORT_OK = qw(finditems snapshot tree);

my ($itemstyle, $groupstyle);
my (%help_tl, $result_tl, $result_fm, $search_tl, $helptree_tl, $coords_tl,
    $searchtree_tl, $tree_tl, $tree);
my $showitemflag;
my ($x0, $y0);
my ($help_print, $imagecounter, $saving) = (0, 0, 0);
my %searchEntryValue;
my $searchTreeEntryValue;
my %enclosedModBtn;
my %overlapModBtn;
my %treeModBtn;
my %searchKey;
my %snapKey;
my %treeKey;
my %keys;
my %seq;
my %wwidth;
my %wheight;
my $preload;
my %defaultoptions;
my %focus;
my %instances;
my @instances;
my %cmdoptions;
my $initobjectfunction;

sub BEGIN {
    # test if ZincDebug is loaded using the -M perl option
    $preload = 1 if (caller(2))[2] == 0;
    return unless $preload;
    # parse ZincDebug options
    require Getopt::Long;
    Getopt::Long::Configure('pass_through');
    Getopt::Long::GetOptions(\%cmdoptions,
	       'itemModBtn=s', 'tkey=s', 'optionsToDisplay=s', 'optionsFormat=s',
	       'color=s', 'enclosedModBtn=s', 'overlapModBtn=s', 'searchKey=s',
	       'skey=s', 'verbosity=s', 'basename=s',
	       );
    # save current Tk::Zinc::InitObject function; it will be invoked in
    # overloaded one (see below)
    $initobjectfunction = Tk::Zinc->can('InitObject');
	
}

# Hack to capture the instance of zinc. ZincDebug functions are invoked here.
# Note that created bindings might be overloaded by the application.
sub Tk::Zinc::InitObject {
    Tk::Widget::InitObject(@_);
    print "ZincDebug is ON\n";
    # invoke function possibly overloaded in other modules
    &$initobjectfunction(@_) if $initobjectfunction;
    return unless $preload;
    my $zinc = $_[0];
    my @options = ();
    push (@options, -itemModBtn => [split(/,/, $cmdoptions{itemModBtn})])
	if $cmdoptions{itemModBtn};
    push (@options, -tkey => $cmdoptions{tkey}) if $cmdoptions{tkey};
    push (@options, -optionsToDisplay => $cmdoptions{optionsToDisplay})
	if $cmdoptions{optionsToDisplay};
    push (@options, -optionsFormat => $cmdoptions{optionsFormat})
	if $cmdoptions{optionsFormat};
      #print "options=@options\n";
    &tree($zinc, @options);
    @options = ();
    push (@options, -color => $cmdoptions{color}) if $cmdoptions{color};
    push (@options, -enclosedModBtn => [split(/,/, $cmdoptions{enclosedModBtn})])
	if $cmdoptions{enclosedModBtn};
    push (@options, -overlapModBtn => [split(/,/, $cmdoptions{overlapModBtn})])
	if $cmdoptions{overlapModBtn};
    &finditems($zinc, @options);
    @options = ();
    push (@options, -searchKey => $cmdoptions{searchKey}) if $cmdoptions{searchKey};
    push (@options, -skey => $cmdoptions{skey}) if $cmdoptions{skey};
    push (@options, -verbosity => $cmdoptions{verbosity}) if $cmdoptions{verbosity};
    push (@options, -basename => $cmdoptions{basename}) if $cmdoptions{basename};
    &snapshot($zinc, @options);
}


#---------------------------------------------------------------------------
# tree : display items hierarchy
#---------------------------------------------------------------------------
sub tree {
    
    my $zinc = shift;
    unless ($zinc) {
	carp "In ZincDebug module, tree() function, widget must be specified\n";
	return;
    }
    &newinstance($zinc);
    # styles definition
    $itemstyle =
	$zinc->ItemStyle('text', -stylename => "item", -foreground => 'black')
	    unless $itemstyle;
    $groupstyle =
	$zinc->ItemStyle('text', -stylename => "group", -foreground => 'black')
	    unless $groupstyle;
    
    # options 
    my %options = @_;
    for my $opt (keys(%options)) {
	carp "in ZincDebug module, tree() function, unknown option $opt\n"
	    unless ($opt eq '-itemModBtn' or $opt eq '-key' or $opt eq '-tkey' or
		    $opt eq '-optionsToDisplay' or $opt eq '-optionsFormat');
    }
    
    # unset previous bindings;
    $zinc->Tk::bind('<'.$treeKey{$zinc}.'>', '') if $treeKey{$zinc};
    if ($treeModBtn{$zinc}) {
	my $seq;
	if ($treeModBtn{$zinc}->[0]) {
	    $seq = $treeModBtn{$zinc}->[0]."-".$treeModBtn{$zinc}->[1];
	} else {
	    $seq = $treeModBtn{$zinc}->[1];

	}
	$zinc->Tk::bind('<'.$seq.'>', '');
    }
    if ($options{-tkey}) {
	$treeKey{$zinc} = $options{-tkey};
    } elsif ($options{-key}) {
	$treeKey{$zinc} = $options{-key};
    } else {
	$treeKey{$zinc} = 'Control-t';
    }
    $treeModBtn{$zinc} = ($options{-itemModBtn}) ? $options{-itemModBtn} :
	['Control', 2];
    $options{-optionsFormat} = 'column' unless $options{-optionsFormat};
    if ($options{-optionsFormat} ne 'row' and $options{-optionsFormat} ne 'column') {
	carp "in ZincDebug module, tree() function, expected values for ".
	    "-optionsFormat are 'row' or 'column'. Option is set to 'column'\n";
	$options{-optionsFormat} = 'column';
    }
    # binding for building tree
    $zinc->Tk::bind('<'.$treeKey{$zinc}.'>',
			      [\&showtree, $options{-optionsToDisplay},
			       $options{-optionsFormat}]);
    # binding for displaying item in tree
    my $seq;
    if ($treeModBtn{$zinc}->[0]) {
	$seq = $treeModBtn{$zinc}->[0]."-".$treeModBtn{$zinc}->[1];
    } else {
	$seq = $treeModBtn{$zinc}->[1];
    }
    $zinc->Tk::bind("<".$seq.">", [\&findintree, $options{-optionsToDisplay},
			   $options{-optionsFormat}]);
    # binding for general help
    $zinc->toplevel->Tk::bind('<Key-Escape>', \&showgeneralhelp);
    
} # end tree


#---------------------------------------------------------------------------
# finditems : scan items which are enclosed in a rectangular area first
# drawn by drag & drop or all items which overlap it
#---------------------------------------------------------------------------
sub finditems {
    
    my $zinc = shift;
    unless ($zinc) {
	carp "In ZincDebug module, finditems() function, widget must be specified\n";
	return;
    }
    &newinstance($zinc);
    # options 
    my %options = @_;
    for my $opt (keys(%options)) {
	carp "in ZincDebug module, finditems() function, unknown option $opt\n"
	    unless ($opt eq '-color' or $opt eq '-enclosedModBtn' or
		    $opt eq '-overlapModBtn' or $opt eq '-searchKey');
    }
    # unset previous bindings;
    my $ekb = $enclosedModBtn{$zinc};
    if ($ekb) {
	if ($ekb->[0]) {
	    $zinc->Tk::bind("<".$ekb->[0]."-".$ekb->[1].">", '');
	    $zinc->Tk::bind("<".$ekb->[0]."-B".$ekb->[1]."-Motion>", '');
	    $zinc->Tk::bind("<".$ekb->[0]."-B".$ekb->[1]."-ButtonRelease>", '');
	} else {
	    $zinc->Tk::bind("<".$ekb->[1].">", '');
	    $zinc->Tk::bind("<B".$ekb->[1]."-Motion>", '');
	    $zinc->Tk::bind("<B".$ekb->[1]."-ButtonRelease>", '');
	}
    }
    my $okb = $overlapModBtn{$zinc};
    if ($okb) {
	if ($okb->[0]) {
	    $zinc->Tk::bind("<".$okb->[0]."-".$okb->[1].">", '');
	    $zinc->Tk::bind("<".$okb->[0]."-B".$okb->[1]."-Motion>", '');
	    $zinc->Tk::bind("<".$okb->[0]."-B".$okb->[1]."-ButtonRelease>", '');
	} else {
	    $zinc->Tk::bind("<".$okb->[1].">", '');
	    $zinc->Tk::bind("<B".$okb->[1]."-Motion>", '');
	    $zinc->Tk::bind("<B".$okb->[1]."-ButtonRelease>", '');
	}
    }
    
    my $color = ($options{-color}) ? $options{-color} : 'sienna';
    $enclosedModBtn{$zinc} = ($options{-enclosedModBtn}) ? $options{-enclosedModBtn} :
	['Control', 3];
    $overlapModBtn{$zinc} = ($options{-overlapModBtn}) ? $options{-overlapModBtn} :
	['Shift', 3];
    $searchKey{$zinc} = ($options{-searchKey}) ? $options{-searchKey} : 'Control-f';
    # bindings for Enclosed search
    $ekb = $enclosedModBtn{$zinc};
    if ($ekb) {
	if ($ekb->[0]) {
	    $zinc->Tk::bind("<".$ekb->[0]."-".$ekb->[1].">",
			    [\&startrectangle, 'simple', 'Enclosed', $color]);
	    $zinc->Tk::bind("<".$ekb->[0]."-B".$ekb->[1]."-Motion>", \&resizerectangle);
	    $zinc->Tk::bind("<".$ekb->[0]."-B".$ekb->[1]."-ButtonRelease>",
			    [\&stoprectangle, 'enclosed', 'Enclosed search']);
	} else {
	    $zinc->Tk::bind("<".$ekb->[1].">",
			    [\&startrectangle, 'simple', 'Enclosed', $color]);
	    $zinc->Tk::bind("<B".$ekb->[1]."-Motion>", \&resizerectangle);
	    $zinc->Tk::bind("<B".$ekb->[1]."-ButtonRelease>",
			    [\&stoprectangle, 'enclosed', 'Enclosed search']);
	}
    }
    # bindings for Overlap search
    $okb = $overlapModBtn{$zinc};
    if ($okb) {
	if ($okb->[0]) {
	    $zinc->Tk::bind("<".$okb->[0]."-".$okb->[1].">",
			    [\&startrectangle, 'mixed', 'Overlap', $color]);
	    $zinc->Tk::bind("<".$okb->[0]."-B".$okb->[1]."-Motion>", \&resizerectangle);
	    $zinc->Tk::bind("<".$okb->[0]."-B".$okb->[1]."-ButtonRelease>",
			    [\&stoprectangle, 'overlapping', 'Overlap search']);
	} else {
	    $zinc->Tk::bind("<".$okb->[1].">",
			    [\&startrectangle, 'mixed', 'Overlap', $color]);
	    $zinc->Tk::bind("<B".$okb->[1]."-Motion>", \&resizerectangle);
	    $zinc->Tk::bind("<B".$okb->[1]."-ButtonRelease>",
			    [\&stoprectangle, 'overlapping', 'Overlap search']);
	}
    }
    # binding for search entry
    $zinc->Tk::bind('<'.$searchKey{$zinc}.'>', \&searchentry);
    # binding for general help
    $zinc->toplevel->Tk::bind('<Key-Escape>', \&showgeneralhelp);
    
} # end finditems



#---------------------------------------------------------------------------
# snapshot : snapshot the application window, in order to illustrate a
# graphical bug for example
#---------------------------------------------------------------------------
sub snapshot {
    
    my $zinc = shift;
    unless ($zinc) {
	carp "In ZincDebug module, snapshot() function, widget must be specified\n";
	return;
    }
    &newinstance($zinc);
    # options 
    my %options = @_;
    for my $opt (keys(%options)) {
	carp "in ZincDebug module, snapshot() function, unknown option $opt\n"
	    unless ($opt eq '-key' or $opt eq '-skey' or
		    $opt eq '-verbosity' or $opt eq '-basename');
    }
    # unset previous bindings;
    $zinc->Tk::bind("<".$snapKey{$zinc}.">", '') if $snapKey{$zinc};
    
    if ($options{-skey}) {
	$snapKey{$zinc} = $options{-skey};
    } elsif ($options{-key}) {
	$snapKey{$zinc} = $options{-key};
    } else {
	$snapKey{$zinc} = 'Control-s';
    }
    my $snapshotVerbosity = (defined $options{-verbosity}) ? $options{-verbosity} : 1;
    my $snapshotBasename = ($options{-basename}) ? $options{-basename} : "zincsnapshot";
    # binding for printing a full zinc window
    $zinc->Tk::bind("<".$snapKey{$zinc}.">",
  		    [\&printWindow , $snapshotBasename, $snapshotVerbosity]);
    # binding for general help
    $zinc->toplevel->Tk::bind('<Key-Escape>', \&showgeneralhelp);

} # end snapshot


#---------------------------------------------------------------------------
#
# TREE PRIVATE FUNCTIONS
#
#---------------------------------------------------------------------------
sub findintree {
    my ($zinc, $optionstodisplay, $optionsFormat) = @_;
    if (not Tk::Exists($tree_tl)) {
	&showtree($zinc, $optionstodisplay, $optionsFormat);
    }
    my $ev = $zinc->XEvent;
    ($x0, $y0) = ($ev->x, $ev->y);
    my @atomicgroups = &unsetAtomicity($zinc);
    my $item = $zinc->find('closest', $x0, $y0);
    &restoreAtomicity($zinc, @atomicgroups);
    return unless $item > 1;
    my @ancestors = reverse($zinc->find('ancestors', $item));
    my $path = join('.', @ancestors).".".$item;
    $tree->see($path);
    $tree->selectionClear;
    $tree->anchorSet($path);
    $tree->selectionSet($path);
    &surrounditem($zinc, $item);
    $tree->focus;

} # end findintree



sub showtree {
    my ($zinc, $optionstodisplay, $optionsFormat) = @_;
    $WARNING = 0;
    my @optionstodisplay = split(/,/, $optionstodisplay);
    $WARNING = 1;
    $tree_tl->destroy if $tree_tl and Tk::Exists($search_tl);
    $tree_tl = $zinc->Toplevel;
    $tree_tl->minsize(280, 200);
    $tree_tl->title("Zinc Items Tree");
    $tree = $tree_tl->Scrolled('Tree',
			       -scrollbars => 'se',
			       -height => 40,
			       -width => 50,
			       -itemtype => 'text',
			       -selectmode => 'single',
			       -separator => '.',
			       -drawbranch => 1,
			       -indent => 30,
			       -command => sub {
				   my $path = shift;
				   my $item = (split(/\./, $path))[-1];
				   &showresult("", $zinc, $item);
				   $zinc->after(100, sub {$zinc->remove("zincdebug")});
			       },
			       );
    $tree->bind('<1>', [sub {
	my $path = $tree->nearest($_[1]);
	my $item = (split(/\./, $path))[-1];
	&highlightitem($tree, $zinc, $item, 0);
		       
    }, Ev('y')]);
    
    $tree->bind('<2>', [sub {
	my $path = $tree->nearest($_[1]);
	return if $path eq 1;
	$tree->selectionClear;
	$tree->selectionSet($path);
	$tree->anchorSet($path);
	my $item = (split(/\./, $path))[-1];
	&highlightitem($tree, $zinc, $item, 1);
		       
    }, Ev('y')]);

    $tree->bind('<3>', [sub {
	my $path = $tree->nearest($_[1]);
	return if $path eq 1;
	$tree->selectionClear;
	$tree->selectionSet($path);
	$tree->anchorSet($path);
	my $item = (split(/\./, $path))[-1];
	&highlightitem($tree, $zinc, $item, 2);
		       
    }, Ev('y')]);
    
    $tree->add("1", -text => "Group(1)", -state => 'disabled');
    &scangroup($zinc, $tree, 1, "1", $optionsFormat, @optionstodisplay);
    $tree->autosetmode;
    # control buttons frame
    my $tree_butt_fm = $tree_tl->Frame(-height => 40)->pack(-side => 'bottom',
							    -fill => 'y');
    $tree_butt_fm->Button(-text => 'Help',
			  -command => [\&showHelpAboutTree, $zinc],
			  )->pack(-side => 'left', -pady => 10,
				  -padx => 30, -fill => 'both');
    
    $tree_butt_fm->Button(-text => 'Search',
			  -command => [\&searchInTree, $zinc],
			  )->pack(-side => 'left', -pady => 10,
				  -padx => 30, -fill => 'both');

    $tree_butt_fm->Button(-text => 'Close',
			  -command => sub {$zinc->remove("zincdebug");
					   $tree_tl->destroy},
			  )->pack(-side => 'left', -pady => 10,
				  -padx => 30, -fill => 'both');
    # pack tree
    $tree->pack(-padx => 10, -pady => 10,
		-ipadx => 10,
		-side => 'top',
		-fill => 'both',
		-expand => 1,
		);


} # end showtree



sub searchInTree {
    my $zinc = shift;
    $searchtree_tl->destroy if $searchtree_tl and Tk::Exists($searchtree_tl);
    $searchtree_tl = $zinc->Toplevel;
    $searchtree_tl->title("Find string in tree");
    my $fm = $searchtree_tl->Frame->pack(-side => 'top');
    $fm->Label(-text => "Find : ",
	       )->pack(-side => 'left', -padx => 10, -pady => 10);
    my $entry = $fm->Entry(-width => 20)->pack(-side => 'left',
					       -padx => 10, -pady => 10);
    my $status = $searchtree_tl->Label(-foreground => 'sienna',
				   )->pack(-side => 'top');
    my $ep = 1;
    my $searchfunc =  sub {
	my $side = shift;
	my $found = 0;
        #print "ep=$ep side=$side\n";
	$status->configure(-text => "");
	$status->update;
	$searchTreeEntryValue = $entry->get();
	$searchTreeEntryValue = quotemeta($searchTreeEntryValue);
	my $text;
	while ($ep) {
 	    $ep = $tree->info($side, $ep);
	    unless ($ep) {
		$ep = 1;
		$found = 0;
		last;
	    }
	    $text = $tree->entrycget($ep, -text);
	    if ($text =~ /$searchTreeEntryValue/) {
		$tree->see($ep);
		$tree->selectionClear;
		$tree->anchorSet($ep);
		$tree->selectionSet($ep);
		$found = 1;
		last;
	    }
	}
	#print "searchTreeEntryValue=$searchTreeEntryValue found=$found\n";
	$status->configure(-text => "Search string not found") unless $found > 0;
    };

    my $fm2 = $searchtree_tl->Frame->pack(-side => 'top');
    $fm2->Button(-text => 'Prev',
		 -command => sub {&$searchfunc('prev');},
		 )->pack(-side => 'left', -pady => 10);
    $fm2->Button(-text => 'Next',
		 -command => sub {&$searchfunc('next');},
		 )->pack(-side => 'left', -pady => 10);
    $fm2->Button(-text => 'Close',
		 -command => sub {$searchtree_tl->destroy},
		 )->pack(-side => 'right', -pady => 10);
    $entry->focus;
    $entry->delete(0, 'end');
    $entry->insert(0, $searchTreeEntryValue) if $searchTreeEntryValue;
    $entry->bind('<Key-Return>', sub {&$searchfunc('next');});
    
} # end searchInTree


sub extractinfo {
    my $zinc = shift;
    my $item = shift;
    my $format = shift;
    my $option = shift;
    my $titleflag = shift;
    $option =~ s/^\s+//;
    $option =~ s/\s+$//;
    #print "option=[$option]\n";
    my @info;
    $WARNING = 0;
    eval {@info = $zinc->itemcget($item, $option)};
    #print "eval $option = (@info) $@\n";
    return if $@;
    return if @info == 0;
    my $info;
    my $sep = ($format eq 'column') ? "\n  " : ", ";
    if ($titleflag) {
	$info = $sep."[$option] ".$info[0];
    } else {
	$info = $sep.$info[0];
    }
    if (@info > 1) {
	shift(@info);
	for (@info) {
	    if ($format eq 'column') {
		if (length($info." ".$_) > 40) {
		    if ($titleflag) {
			$info .= $sep."[$option] ".$_;
		    } else {
			$info .= $sep.$_;
		    }
		} else {
		    $info .= ", $_";
		}
	    } else {
		$info .= $sep.$_;
	    }
	}
    }
    $WARNING = 1;
    return $info;
    
} # end extractinfo


sub scangroup {
    my ($zinc, $tree, $group, $path, $format, @optionstodisplay) = @_;
    my @items = $zinc->find('withtag', "$group.");
    for my $item (@items) {
	my $type = ucfirst($zinc->type($item));
	my $info = " ";
	if (@optionstodisplay == 1) {
	    $info .= &extractinfo($zinc, $item, $format, $optionstodisplay[0]);
	} elsif (@optionstodisplay > 1) {
	    for my $opt (@optionstodisplay) {
		$info .= &extractinfo($zinc, $item, $format, $opt, 1);
	    }
	}
	if ($type eq "Group") {
	    $tree->add($path.".".$item,
		       -text => "$type($item)$info",
		       -style => 'group');
	    &scangroup($zinc, $tree, $item, $path.".".$item, $format, @optionstodisplay);
	} else {
	    $tree->add($path.".".$item,
		       -text => "$type($item)$info",
		       -style => 'item');
	}
    }

} # end scangroup

#---------------------------------------------------------------------------
#
# AREA SEARCH PRIVATE FUNCTIONS
#
#---------------------------------------------------------------------------
# begin to draw rectangular area for search
sub startrectangle {
    my ($zinc, $style, $text, $color) = @_;
    $zinc->remove("zincdebugrectangle", "zincdebuglabel");
    my $ev = $zinc->XEvent;
    ($x0, $y0) = ($ev->x, $ev->y);
    $zinc->add('rectangle', 1, [$x0, $y0, $x0, $y0],
	       -linecolor => $color,
	       -linewidth => 2,
	       -linestyle => $style,
	       -tags => ["zincdebugrectangle"],
			       );
    $zinc->add('text', 1,
	       -color => $color,
	       -font => '7x13',
	       -position => [$x0+5, $y0-15],
	       -text => $text,
	       -tags => ["zincdebuglabel"],
	       );
  
} # end startrectangle


# resize the rectangular area for search
sub resizerectangle {
    my $zinc = shift;
    my $ev = $zinc->XEvent;
    my ($x, $y) = ($ev->x, $ev->y);
    return unless ($zinc->find('withtag', "zincdebugrectangle"));

    $zinc->coords("zincdebugrectangle", 1, 1, [$x, $y]);
    if ($x < $x0) {
	if ($y < $y0) {
	    $zinc->coords("zincdebuglabel", [$x+5, $y-15]);
	} else {
	    $zinc->coords("zincdebuglabel", [$x+5, $y0-15]);
	}
    } else {
	if ($y < $y0) {
	    $zinc->coords("zincdebuglabel", [$x0+5, $y-15]);
	} else {
	    $zinc->coords("zincdebuglabel", [$x0+5, $y0-15]);
	}
    }
    $zinc->raise("zincdebugrectangle");
    $zinc->raise("zincdebuglabel");

} # end resizerectangle



# stop drawing rectangular area for search
sub stoprectangle {
    my ($zinc, $searchtype, $text) = @_;
    return unless ($zinc->find('withtag', "zincdebugrectangle"));

    my @atomicgroups = &unsetAtomicity($zinc);
    my @coords = $zinc->coords0("zincdebugrectangle");
    my @items;
    for my $item ($zinc->find($searchtype, @coords, 1, 1)) {
	push (@items, $item) unless $zinc->hastag($item, "zincdebugrectangle") or
	    $zinc->hastag($item, "zincdebuglabel");
    }
    &restoreAtomicity($zinc, @atomicgroups);
    if (@items) {
	&showresult($text, $zinc, @items);
    } else {
	$zinc->remove("zincdebugrectangle", "zincdebuglabel");
    }

} # end stoprectangle



# in order to avoid find problems with group atomicity, we set all -atomic
# attributes to 0
sub unsetAtomicity {
    my $zinc = shift;
    my @groups = $zinc->find('withtype', 'group');
    my @atomicgroups;
    for my $group (@groups) {
	if ($zinc->itemcget($group, -atomic)) {
	    push(@atomicgroups, $group);
	    $zinc->itemconfigure($group, -atomic => 0);
	}
    }
    return @atomicgroups;
    
} # end unsetAtomicity



sub restoreAtomicity {
    my $zinc = shift;
    my @atomicgroups = @_;
    for my $group (@atomicgroups) {
	$zinc->itemconfigure($group, -atomic => 1);
    }

} # end restoreAtomicity



# display search entry field
sub searchentry {
    my $zinc = shift;
    $search_tl->destroy if $search_tl and Tk::Exists($search_tl);
    $search_tl = $zinc->Toplevel;
    $search_tl->title("Specific search");
    my $fm = $search_tl->Frame->pack(-side => 'top');
    $fm->Label(-text => "Item TagOrId : ",
	       )->pack(-side => 'left', -padx => 10, -pady => 10);
    my $entry = $fm->Entry(-width => 20)->pack(-side => 'left',
					       -padx => 10, -pady => 10);
    my $status = $search_tl->Label(-foreground => 'sienna',
				   )->pack(-side => 'top');
    $search_tl->Button(-text => 'Close',
		       -command => sub {$search_tl->destroy},
		       )->pack(-side => 'top', -pady => 10);
    $entry->focus;
    $entry->delete(0, 'end');
    $entry->insert(0, $searchEntryValue{$zinc}) if $searchEntryValue{$zinc};
    $entry->bind('<Key-Return>', [sub {
	$status->configure(-text => "");
	$status->update;
	$searchEntryValue{$zinc} = $entry->get();
	my @items = $zinc->find('withtag', $searchEntryValue{$zinc});
	if (@items) {
	    &showresult("Search with TagOrId $searchEntryValue{$zinc}", $zinc, @items);
	} else {
	    $status->configure(-text => "No such TagOrId ($searchEntryValue{$zinc})");
	}
    }]);
    
} # end searchentry


#---------------------------------------------------------------------------
#
# RESULTS DISPLAY PRIVATE FUNCTIONS
#
#---------------------------------------------------------------------------

# display in a toplevel the result of search ; a new toplevel destroyes the
# previous one
sub showresult {
    my ($label, $zinc, @items) = @_;
    # toplevel (re-)creation
    $result_tl->destroy if Tk::Exists($result_tl);
    $result_tl = $zinc->Toplevel();
    my $title = "Zinc Debug";
    $title .= " - $label" if $label;
    $result_tl->title($title);
    $result_tl->geometry('+10+20');
    my $fm = $result_tl->Frame()->pack(-side => 'bottom',
				       );
    $fm->Button(-text => 'Help',
		-command => [\&showHelpAboutAttributes, $zinc]
		)->pack(-side => 'left', -padx => 40, -pady => 10);
    $fm->Button(-text => 'Close',
		-command => sub {
		    $result_tl->destroy;
		    $zinc->remove("zincdebugrectangle", "zincdebuglabel");
		})->pack(-side => 'left', -padx => 40, -pady => 10);
    
    # scrolled pane creation
    my $heightmax = 500;
    my $height = 100 + 50*@items;
    my $width = $result_tl->screenwidth;
    $width = 1200 if $width > 1200;
    $height = $heightmax if $height > $heightmax;
    $result_fm = $result_tl->Scrolled('Pane',
				      -scrollbars => 'soeo',
				      -gridded => 'xy',
				      -width => $width,
				      -height => $height,
				      );
    $result_fm->pack(-padx => 10, -pady => 10,
		     -ipadx => 10,
		     -fill => 'both',
		     -expand => 1,
		     );
    # attributes display
    &showattributes($zinc, $result_fm, \@items);

} # end showresult


# display in a toplevel the values of other options
sub showotheroptions {
    my ($zinc, $item, $fmp) = @_;
    my $tl = $fmp->Toplevel;
    #$tl->transient($fmp);
    my $title = "Other options of item $item";
    $tl->title($title);
    my $background = $tl->cget(-background);
    my $fm = $tl->LabFrame(-labelside => 'acrosstop',
			   -label => $title,
			   )->pack(-padx => 10, -pady => 10,
				   -ipadx => 10,
				   -fill => 'both');
    my $btn1 = $fm->Button(-text => 'Show Item',)
	->grid(-row => 1, -col => 1, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $btn1->bind('<1>', [\&highlightitem, $zinc, $item, 0]);
    $btn1->bind('<2>', [\&highlightitem, $zinc, $item, 1]);
    $btn1->bind('<3>', [\&highlightitem, $zinc, $item, 2]);
    my $btn2 = $fm->Button(-text => 'Bounding Box')
	->grid(-row => 1, -col => 2, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $btn2->bind("<1>", [\&showbbox, $zinc, $item]);
    $btn2->bind("<ButtonRelease-1>", [\&hidebbox, $zinc]);

    my $bgcolor = 'ivory';
    $fm->Label(-text => 'Option', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => 2, -col => 1, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $fm->Label(-text => 'Value', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => 2, -col => 2, -ipady => 10, -ipadx => 5, -sticky => 'nswe');

    my @options = $zinc->itemconfigure($item);
    my $i = 3;
    for my $elem (@options) {
	my ($option, $type, $value) = (@$elem)[0,1,4];
	#print "option=$option type=$type\n";
	next if ($option eq '-visible' or $option eq '-sensitive' or
		 $option eq '-tags' or $option eq '-position' or
		 $option eq '-priority');
	if ($type eq 'gradient') {
	    my ($gradient) = $zinc->gname($value);
	    #print "value=$value gradient=$gradient\n";
	}
	$fm->Label(-text => $option, -relief => 'ridge')
	    ->grid(-row => $i, -col => 1, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
	&entryoption($fm, $item, $zinc, $option)
	    ->grid(-row => $i, -col => 2, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
	$i++;
    }
    $tl->Button(-text => 'Close',
		-command => sub {$tl->destroy})->pack;

} # end showotheroptions


sub showdevicecoords {
    my ($zinc, $item) = @_;
    &showcoords($zinc, $item, 1);

} # end showdevicecoords


sub showcoords {
    my ($zinc, $item, $deviceflag) = @_;
    my $bgcolor = 'ivory';
    my $bgcolor2 = 'gray75';
    $coords_tl->destroy if Tk::Exists($coords_tl) and not $deviceflag;
    
    $coords_tl = $zinc->Toplevel();
    my $title = "Zinc Debug";
    if ($deviceflag) {
	$title .= " - Coords of item $item";
    } else {
	$title .= " - Device coords of item $item";
    }
    $coords_tl->title($title);
    $coords_tl->geometry('+10+20');
    $coords_tl->Button(-text => 'Close',
		       -command => sub {
			   $coords_tl->destroy;
		       })->pack(-side => 'bottom');
    # scrolled pane creation
    my $heightmax = 500;
    my $height = 100 + 50;
    $height = $heightmax if $height > $heightmax;
    my $width = $coords_tl->screenwidth;
    $width = 1200 if $width > 1200;
    my $coords_fm = $coords_tl->Scrolled('Pane',
				      -scrollbars => 'se',
				      -width => $width,
				      -height => $height,
				      );
    $coords_fm->pack(-padx => 10, -pady => 10,
		     -ipadx => 10,
		     -fill => 'both',
		     -expand => 1,
		     );
    my @contour;
    my $contournum = $zinc->contour($item);
    for (my $i=0; $i < $contournum; $i++) {
	my @coords = $zinc->coords($item, $i);
	if (!ref $coords[0]) {
	    ## The first item of the list is not a reference, so the
	    ## list is guarranted to be a flat list (x, y, ...)
	    ## normaly of only one pair of (x y)
	    @coords = $zinc->transform(scalar $zinc->group($item), 1, [@coords])
		if $deviceflag;
	    for (my $j=0; $j < @coords; $j += 2) {
		push(@{$contour[$i]}, [$coords[$j], $coords[$j+1]]);
	    }
	}
	else {
	    ## the first element is an array reference, as every
	    ## other elements of the list
	    for (my $j=0; $j < @coords; $j ++) {
		my @c = @{$coords[$j]};
		@c = $zinc->transform(scalar $zinc->group($item), 1, [@c])
		    if $deviceflag;
		push(@{$contour[$i]}, [@c]);
	    }
	}
    }
    my $row = 1;
    my $col = 1;
    for (my $i=0; $i < @contour; $i++) {
	$col = 1;
	$coords_fm->Label(-text => "Contour $i",
			  -background => $bgcolor,
			  -relief => 'ridge')->grid(-row => $row,
						    -col => $col++,
						    -ipadx => 5,
						    -ipady => 5,
						    -sticky => 'nswe');
	for my $coords (@{$contour[$i]}) {
	    if ($col > 10) {
		$col = 2;
		$row++;
	    }
	    $coords->[0] =~ s/\.(\d\d).*/\.$1/;
	    $coords->[1] =~ s/\.(\d\d).*/\.$1/;
	    my @opt;
	    if (defined $coords->[2]) {
		@opt = (-text => sprintf('%s, %s, %s', @$coords),
			-underline => length(join(',', @$coords)) + 1,
			);
	    } else {
		@opt = (-text => sprintf('%s, %s', @{$coords}[0,1]));
	    }
	    $coords_fm->Label(@opt,
			      -width => 15,
			      -relief => 'ridge')->grid(-row => $row,
							-ipadx => 5,
							-ipady => 5,
							-col => $col++,
							-sticky => 'nswe');
	}
	$row++;
    }

} # end showcoords


# display in a toplevel group's attributes
sub showgroupattributes {
    my ($zinc, $item) = @_;
    my $tl = $zinc->Toplevel;
    my $title = "About group $item";
    $tl->title($title);
    my $fm = $tl->LabFrame(-labelside => 'acrosstop',
			   -label => $title,
			   )->pack(-padx => 10, -pady => 10,
				   -ipadx => 10,
				   -fill => 'both');
    my $r = 1;
    # content
    $fm->Button(-command => [\&showgroupcontent, $zinc, $item],
		-height => 2,
		-text => 'Content',
		)->grid(-row => $r, -col => 1, -sticky => 'nswe');
    my $btn = $fm->Button(-height => 2,
			  -text => 'Bounding Box',
			  )->grid(-row => $r++, -col => 2, -sticky => 'nswe');
    $btn->bind("<1>", [\&showbbox, $zinc, $item]);
    $btn->bind("<ButtonRelease-1>", [\&hidebbox, $zinc]);

    # parent group
    $fm->Label(-text => 'Parent group', -relief => 'ridge')
	->grid(-row => $r, -col => 1, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    my $gr = $zinc->group($item);
    my $bpg = $fm->Button(-text => $gr,
			  -command => [\&showgroupattributes, $zinc, $gr])
	->grid(-row => $r++, -col => 2, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $bpg->configure(-disabledforeground => scalar $bpg->cget(-foreground),
		    -state => 'disabled') if $gr == $item;
    my $bgcolor = 'ivory';
    # coords
    $fm->Label(-text => 'Coordinates', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => $r++, -col => 1, -ipady => 10, -ipadx => 5, -sticky => 'nswe',
	       -columnspan => 2);	# coords
    $fm->Label(-text => 'Coords', -relief => 'ridge')
	->grid(-row => $r, -col => 1, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    my @coords = $zinc->coords($item);
    my $coords;
    if (@coords == 2) {
	my $x0 = int($coords[0]);
	my $y0 = int($coords[1]);
	$coords = "($x0, $y0)";
    } elsif (@coords == 4) {
	my $x0 = int($coords[0]);
	my $y0 = int($coords[1]);
	my $x1 = int($coords[2]);
	my $y1 = int($coords[3]);
	$coords = "($x0, $y0, $x1, $y1)";
	print "we should not go through this case (1)!\n";
    } else {
	my $x0 = int($coords[0]);
	my $y0 = int($coords[1]);
	my $xn = int($coords[$#coords-1]);
	my $yn = int($coords[$#coords]);
	my $n = @coords/2 - 1;
	$coords = "C0=($x0, $y0), ..., C".$n."=($xn, $yn)";
	print "we should not go through this case (2d)!\n";
    }
    $fm->Label(-text => $coords, -relief => 'ridge')
	->grid(-row => $r++, -col => 2, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    # device coords
    $fm->Label(-text => 'Device coords', -relief => 'ridge')
	->grid(-row => $r, -col => 1, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    @coords = $zinc->transform(scalar $zinc->group($item), 1, [@coords]);
    if (@coords == 2) {
	my $x0 = int($coords[0]);
	my $y0 = int($coords[1]);
	$coords = "($x0, $y0)";
    } elsif (@coords == 4) {
	my $x0 = int($coords[0]);
	my $y0 = int($coords[1]);
	my $x1 = int($coords[2]);
	my $y1 = int($coords[3]);
	$coords = "($x0, $y0, $x1, $y1)";
	print "we should not go through this case (3)!\n";
    } else {
	my $x0 = int($coords[0]);
	my $y0 = int($coords[1]);
	my $xn = int($coords[$#coords-1]);
	my $yn = int($coords[$#coords]);
	my $n = @coords/2 - 1;
	$coords = "C0=($x0, $y0), ..., C".$n."=($xn, $yn)";
	print "we should not go through this case (4)!\n";
    }
    $fm->Label(-text => $coords, -relief => 'ridge')
	->grid(-row => $r++, -col => 2, -ipady => 10, -ipadx => 5, -sticky => 'nswe');

    # options
    $fm->Label(-text => 'Option', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => $r, -col => 1, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $fm->Label(-text => 'Value', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => $r++, -col => 2, -ipady => 10, -ipadx => 5, -sticky => 'nswe');

    my @options = $zinc->itemconfigure($item);
    for my $elem (@options) {
	my ($option, $value) = (@$elem)[0,4];
	$fm->Label(-text => $option, -relief => 'ridge')
	    ->grid(-row => $r, -col => 1, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
	my $w;
	if ($option and $option eq '-tags') {
	    $value = join("\n", @$value);
	    $w = $fm->Label(-text => $value, -relief => 'ridge');
	} elsif ($option and $option eq '-clip' and $value and $value > 0) {
	    $value .= " (". $zinc->type($value) .")";
	    $w = $fm->Label(-text => $value, -relief => 'ridge');
	} else {
	    $w = &entryoption($fm, $item, $zinc, $option);
	}
	$w->grid(-row => $r, -col => 2, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
	$r++;
    }
    $tl->Button(-text => 'Close',
		-command => sub {$tl->destroy})->pack;    

} # end showgroupattributes


# display in a toplevel the content of a group item
sub showgroupcontent {
    my ($zinc, $group) = @_;
    my $tl = $zinc->Toplevel;
    my @items = $zinc->find('withtag', $group.".");
    my $heightmax = 500;
    my $height = 100 + 50*@items;
    $height = $heightmax if $height > $heightmax;    
    my $title = "Content of group $group";
    $tl->title($title);
    my $fm2 = $tl->Frame()->pack(-side => 'bottom',
				 );
    $fm2->Button(-text => 'Help',
		-command => [\&showHelpAboutAttributes, $zinc]
		 )->pack(-side => 'left', -padx => 40, -pady => 10);
    $fm2->Button(-text => 'Close',
		-command => sub {
		    $tl->destroy;
		})->pack(-side => 'left', -padx => 40, -pady => 10);

    my $width = $result_tl->screenwidth;
    $width = 1200 if $width > 1200;
    my $fm = $tl->Scrolled('Pane',
  			   -scrollbars => 'se',
  			   -width => $width,
  			   -height => $height,
  			   -label => $title,
  			   )->pack(-padx => 10, -pady => 10,
  				   -ipadx => 10,
				   -expand => 1,
  				   -fill => 'both');
    &showattributes($zinc, $fm, \@items);

} # end showgroupcontent

# display the bbox of a group item
sub showbbox {
    my ($btn, $zinc, $item) = @_;
    my @bbox  = $zinc->bbox($item);
    if (scalar @bbox == 4) {
	# If item is visible, rectangle is drawm surround it.
	# Else, a warning is displayed.
	unless (&itemisoutside($zinc, @bbox)) {
	    my $i = -2;
	    for ('white', 'blue', 'white') {
		$zinc->add('rectangle', 1,
			   #[$bbox[0] - 5 - 2*$i, $bbox[1] - 5 - 2*$i,
			    #$bbox[2] + 5 + 2*$i, $bbox[3] + 5 + 2*$i],
			   [$bbox[0] + $i, $bbox[1] + $i,
			    $bbox[2] - $i, $bbox[3] + $i],
			   -linecolor => $_,
			   -linewidth => 1,
			   -tags => ['zincdebugbbox']);
		$i += 2;
	    }
	}
    }
    $zinc->raise('zincdebugbbox');

} # end showgroupbbox


sub hidebbox {
    my ($btn, $zinc) = @_;
    $zinc->remove("zincdebugbbox");

} # end hidegroupbbox


# highlight an item (by cloning it and hiding other found items)
# why cloning? because we can't simply make visible an item which
# belongs to an invisible group.
sub highlightitem {
    my ($btn, $zinc, $item, $level) = @_;
    return if $showitemflag or $item == 1;
    $showitemflag = 1;
    
    &surrounditem($zinc, $item, $level);
    
    $btn->bind('<ButtonRelease>', [\&undohighlightitem, $zinc]) if $btn;

} # end highlightitem

sub itemisoutside {
    my $zinc = shift;
    my @bbox = @_;
    return unless @bbox == 4;
    my $outflag;
    $WARNING = 0;
    if ($bbox[2] < 0) {
	if ($bbox[1] >  $wheight{$zinc}) {
	    $outflag = 'left+bottom';
	} elsif ($bbox[3] < 0) {
	    $outflag = 'left+top';
	} else {
	    $outflag = 'left';
	} 
    } elsif ($bbox[0] > $wwidth{$zinc}) {
	if ($bbox[1] >  $wheight{$zinc}) {
	    $outflag = 'right+bottom';
	} elsif ($bbox[3] < 0) {
	    $outflag = 'right+top';
	} else {
	    $outflag = 'right';
	}
    } elsif ($bbox[3] < 0) {
	$outflag = 'top';
    } elsif ($bbox[1] > $wheight{$zinc}) {
	$outflag = 'bottom';
    }
    #print "outflag=$outflag bbox=@bbox\n";
    return 0 unless $outflag;
    my $g = $zinc->add('group', 1, -tags => ['zincdebug']);
    my $hw = 110;
    my $hh = 80;
    my $r = 5;
    $zinc->add('rectangle', $g, [-$hw, -$hh, $hw, $hh],
	       -filled => 1,
	       -linecolor => 'sienna',
	       -linewidth => 3,
	       -fillcolor => 'bisque',
	       -priority => 1,
	       );
    $zinc->add('text', $g,
	       -position => [0, 0],
	       -color => 'sienna',
	       -font => '-b&h-lucida-bold-i-normal-sans-34-240-*-*-p-*-iso8859-1',
	       -anchor => 'center',
	       -priority => 2,
	       -text => "Item is\noutside\nwindow\n");
    my ($x, $y);
    if ($outflag eq 'bottom') {
	$x = $bbox[0] + ($bbox[2]-$bbox[0])/2;
	$x = $hw + 10 if $x < $hw + 10;
	$x = $wwidth{$zinc} - $hw - 10 if $x > $wwidth{$zinc} - $hw - 10;
	$y = $wheight{$zinc} - $hh - 10;
    } elsif ($outflag eq 'top') {
	$x = $bbox[0] + ($bbox[2]-$bbox[0])/2;
	$x = $hw + 10 if $x < $hw + 10;
	$x = $wwidth{$zinc} - $hw - 10if $x > $wwidth{$zinc} - $hw - 10;
	$y = $hh + 10;
    } elsif ($outflag eq 'left') {
	$x = $hw + 10;
	$y = $bbox[1] + ($bbox[3]-$bbox[1])/2;
	$y = $hh + 10 if $y < $hh + 10;
	$y = $wheight{$zinc} - $hh - 10 if $y > $wheight{$zinc} - $hh - 10;
    } elsif ($outflag eq 'right') {
	$x = $wwidth{$zinc} - $hw - 10;
	$y = $bbox[1] + ($bbox[3]-$bbox[1])/2;
	$y = $hh + 10 if $y < $hh + 10;
	$y = $wheight{$zinc} - $hh - 10 if $y > $wheight{$zinc} - $hh - 10;
    } elsif ($outflag eq 'left+top') {
	$x = $hw + 10;
	$y = $hh + 10;
    } elsif ($outflag eq 'left+bottom') {
	$x = $hw + 10;
	$y = $wheight{$zinc} - $hh - 10;
    } elsif ($outflag eq 'right+top') {
	$x = $wwidth{$zinc} - $hw - 10;
	$y = $hh + 10;
    } elsif ($outflag eq 'right+bottom') {
	$x = $wwidth{$zinc} - $hw - 10;
	$y = $wheight{$zinc} - $hh - 10;
    }
    $zinc->coords($g, [$x, $y]);
    $zinc->raise('zincdebug');
    
} # end itemisoutside


# draw a rectangle arround the selected item
sub surrounditem {
    my ($zinc, $item, $level) = @_;
    $zinc->remove("zincdebug");
    # get item ancestors
    my @itemancestors = reverse($zinc->find('ancestors', $item));
    # skip group 1
    shift(@itemancestors);
    # create item's tree with good transformations
    my $topgroup = 1;
    for my $g (@itemancestors) {
	my $gc = $zinc->add('group', $topgroup, -tags => ['zincdebug']);
	$zinc->tsave($g, "mytrans");
	my @c = $zinc->coords($g);
	$zinc->trestore($gc, "mytrans");
	$zinc->coords($gc, [@c]);
	$zinc->tdelete("mytrans");
	$topgroup = $gc;
    }
    # cloning
    my $clone = $zinc->clone($item, -visible => 1, -tags => ['zincdebug']);
    # move in topgroup  
    $zinc->chggroup($clone, $topgroup);
    # create a rectangle around 
    my @bbox0  = $zinc->bbox($clone);
    if (scalar @bbox0 == 4) {
	my @bbox = $zinc->transform(1, $topgroup, [@bbox0]);
	# If item is visible, rectangle is drawm surround it.
	# Else, a warning is displayed.
	unless (&itemisoutside($zinc, @bbox0)) {
	    if (defined($level) and $level > 0) {
		my $r = $zinc->add('Rectangle', $topgroup,
				   [$bbox[0] - 10, $bbox[1] - 10,
				    $bbox[2] + 10, $bbox[3] + 10],
				   -filled => 1,
				   -tags => ['zincdebug'],
				   -fillcolor => "gray20");
		$zinc->itemconfigure($r, -fillcolor => "gray80") if $level == 1;
	    } 
	    my $i = 0;
	    for ('white', 'red', 'white') {
		$zinc->add('rectangle', $topgroup,
			   [$bbox[0] - 5 - 2*$i, $bbox[1] - 5 - 2*$i,
			    $bbox[2] + 5 + 2*$i, $bbox[3] + 5 + 2*$i],
			   -linecolor => $_,
			   -linewidth => 1,
			   -tags => ['zincdebug']);
		$i++;
	    }
	}
    }
    # raise
    $zinc->raise('zincdebug');
    $zinc->raise($clone);
   
} # end surrounditem


sub undohighlightitem {
    my ($btn, $zinc) = @_;
    $btn->bind('ReleaseButton', '');
    $zinc->remove('zincdebug');
    $showitemflag = 0;

} # end undohighlightitem



sub showbanner {
    my $fm = shift;
    my $i = shift;
    my $bgcolor = 'ivory';
    $fm->Label(-text => 'Id', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => $i, -col => 1, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $fm->Label(-text => 'Type', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => $i, -col => 2, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $fm->Label(-text => 'Group', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => $i, -col => 3, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $fm->Label(-text => 'Priority', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => $i, -col => 4, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $fm->Label(-text => 'Sensitive', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => $i, -col => 5, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $fm->Label(-text => 'Visible', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => $i, -col => 6, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $fm->Label(-text => 'Coords', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => $i, -col => 7, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $fm->Label(-text => 'Device coords', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => $i, -col => 8, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $fm->Label(-text => 'Bounding box', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => $i, -col => 9, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $fm->Label(-text => 'Tags', -background => $bgcolor, -relief => 'ridge')
	->grid(-row => $i, -col => 10, -ipady => 10, -ipadx => 5, -sticky => 'nswe');
    $fm->Label()->grid(-row => 1, -col => 11, -pady => 10);
     
} # end showbanner


# display in a grid the values of most important attributes 
sub showattributes {
    my ($zinc, $fm, $items) = @_;
    unless ($wwidth{$zinc} > 1) {
        $zinc->update;
	my $geom = $zinc->geometry =~ /(\d+)x(\d+)+/=~ /(\d+)x(\d+)+/;
	($wwidth{$zinc}, $wheight{$zinc}) = ($1, $2);
    }
    my $bgcolor = 'ivory';
    my $i = 1;
    &showbanner($fm, $i++);
    for my $item (@$items) {
	my $type = $zinc->type($item);
	# id
	my $idbtn =
	    $fm->Button(-text => $item,
			-foreground => 'red'
			)->grid(-row => $i, -col => 1, -sticky => 'nswe',
				-ipadx => 5);
	$idbtn->bind('<1>', [\&highlightitem, $zinc, $item, 0]);
	$idbtn->bind('<2>', [\&highlightitem, $zinc, $item, 1]);
	$idbtn->bind('<3>', [\&highlightitem, $zinc, $item, 2]);
	# type
	if ($type eq 'group') {
	    $fm->Button(-text => $type, 
			-command => [\&showgroupcontent, $zinc, $item])
		->grid(-row => $i, -col => 2, -sticky => 'nswe', -ipadx => 5);
	} else {
	    $fm->Label(-text => $type, -relief => 'ridge')
		->grid(-row => $i, -col => 2, -sticky => 'nswe', -ipadx => 5);
	}
	# group
	my $group = $zinc->group($item);
	$fm->Button(-text => $group,
		    -command => [\&showgroupattributes, $zinc, $group])
	    ->grid(-row => $i, -col => 3, -sticky => 'nswe', -ipadx => 5);
	# priority
	&entryoption($fm, $item, $zinc, -priority)
	    ->grid(-row => $i, -col => 4, -sticky => 'nswe', -ipadx => 5);
	# sensitiveness
	&entryoption($fm, $item, $zinc, -sensitive)
	    ->grid(-row => $i, -col => 5, -sticky => 'nswe', -ipadx => 5);
	# visibility
	&entryoption($fm, $item, $zinc, -visible)
	    ->grid(-row => $i, -col => 6, -sticky => 'nswe', -ipadx => 5);
	# coords
	my @coords = $zinc->coords($item);
	my $coords;
	if (!ref $coords[0]) {
	    my $x0 = int($coords[0]);
	    my $y0 = int($coords[1]);
	    $coords = "($x0, $y0)";
	} else {
	    my @points0 = @{$coords[0]};
	    my $n = $#coords;
	    my @pointsN = @{$coords[$n]};
	    my $x0 = int($points0[0]);
	    my $y0 = int($points0[1]);
	    my $xn = int($pointsN[0]);
	    my $yn = int($pointsN[1]);
	    if ($n == 1) { ## a couple of points
		$coords = "($x0, $y0, $xn, $yn)";
	    } else {
		$coords = "C0=($x0, $y0), ..., C".$n."=($xn, $yn)";
	    }
	}
	if ($type eq 'curve' and @coords > 2) {
	    $fm->Button(-text => $coords,
			-command => [\&showcoords, $zinc, $item])
		->grid(-row => $i, -col => 7, -sticky => 'nswe', -ipadx => 5);
	} else {
	    $fm->Label(-text => $coords, -relief => 'ridge')
		->grid(-row => $i, -col => 7, -sticky => 'nswe', -ipadx => 5);
	}
	# device coords
	@coords = $zinc->transform(scalar $zinc->group($item), 1, [@coords]);
	if (!ref $coords[0]) {
	    my $x0 = int($coords[0]);
	    my $y0 = int($coords[1]);
	    $coords = "($x0, $y0)";
	} else {
	    my @points0 = @{$coords[0]};
	    my $n = $#coords;
	    my @pointsN = @{$coords[$n]};
	    my $x0 = int($points0[0]);
	    my $y0 = int($points0[1]);
	    my $xn = int($pointsN[0]);
	    my $yn = int($pointsN[1]);
	    if ($n == 1) { ## a couple of points
		$coords = "($x0, $y0, $xn, $yn)";
	    } else {
		$coords = "C0=($x0, $y0), ..., C".$n."=($xn, $yn)";
	    }
	}
	if ($type eq 'curve' and @coords > 2) {
	    $fm->Button(-text => $coords,
			-command => [\&showdevicecoords, $zinc, $item])
		->grid(-row => $i, -col => 8, -sticky => 'nswe', -ipadx => 5);
	} else {
	    $fm->Label(-text => $coords, -relief => 'ridge')
		->grid(-row => $i, -col => 8, -sticky => 'nswe', -ipadx => 5);
	}
	# bounding box
	my @bbox = $zinc->bbox($item);
	if (@bbox == 4) {
	    my $btn = $fm->Button(-text => "($bbox[0], $bbox[1]), ($bbox[2], $bbox[3])")
		->grid(-row => $i, -col => 9, -sticky => 'nswe', -ipadx => 5);
	    $btn->bind('<1>', [\&showbbox, $zinc, $item]);
	    $btn->bind('<ButtonRelease-1>', [\&hidebbox, $zinc]) ;
	} else {
	    $fm->Label(-text => "--", , -relief => 'ridge')
		->grid(-row => $i, -col => 9, -sticky => 'nswe', -ipadx => 5);
	}
	# tags
  	my @tags = $zinc->gettags($item);
	$fm->Label(-text => join("\n", @tags),
		   -relief => 'ridge')
	    ->grid(-row => $i, -col => 10, -sticky => 'nswe', -ipadx => 5);
	$fm->Button(-text => 'Other options',
		    -command => [\&showotheroptions, $zinc, $item, $fm])
	    ->grid(-row => $i, -col => 11, -sticky => 'nswe', -ipadx => 5);
	$i++;
	&showbanner($fm, $i++) if ($i % 15 == 0);
    }
    $fm->update;
    return ($fm->width, $fm->height);
    
} # end showattributes

#---------------------------------------------------------------------------
#
# SNAPSHOT FUNCTIONS
#
#---------------------------------------------------------------------------

# print a zinc window in png format
sub printWindow {
    exit if $saving;
    $saving = 1;
    my ($zinc,$basename,$verbosity) = @_;
    my $id = $zinc->id;
    my $filename = $basename . $imagecounter . ".png";
    $imagecounter++;
    my $original_cursor = ($zinc->configure(-cursor))[3];
    $zinc->configure(-cursor => 'watch');
    $zinc->update;
    my $res = system("import", -window, $id, $filename);
    $zinc->configure(-cursor => $original_cursor);
    
    $saving = 0;
    if ($res) {
	&showErrorWhilePrinting($zinc, $res)
	}
    else {
	my $dir = `pwd`; chomp ($dir);
	print "ZincDebug: Zinc window snapshot saved in $dir". "/$filename\n"
	    if $verbosity;
    }

} # end printWindow


# display complete help screen
sub showErrorWhilePrinting {
    my ($zinc, $res) = @_;
    my $dir = `pwd`; chomp ($dir);
    $help_print->destroy if $help_print and Tk::Exists($help_print);
    $help_print = $zinc->Dialog(-title => 'Zinc Print info',
				-text =>
				"To acquire a TkZinc window snapshot, you must " .
				"have access to the import command, which is ".
				"part of imageMagic package\n\n".
				"You must also have the rights to write ".
				"in the current dir : $dir",
				-bitmap => 'warning',
				);
    $help_print->after(300, sub {$help_print->grabRelease});
    $help_print->Show();

} # end showErrorWhilePrinting

#---------------------------------------------------------------------------
#
# HELP FUNCTIONS
#
#---------------------------------------------------------------------------
# display complete help screen
sub showgeneralhelp {
    if (@instances == 1) {
	&showinstancehelp($instances[0], "ZincDebug help", 1);
    } elsif (@instances > 1) {
	$help_tl{gene}->destroy if $help_tl{gene} and Tk::Exists($help_tl{gene});
	$help_tl{gene} = $instances[0]->Toplevel;
	$help_tl{gene}->title("ZincDebug general help");

	my $text = $help_tl{gene}->Scrolled('Text', -font =>
					     scalar $instances[0]->cget(-font),
					     -wrap => 'word',
					     -foreground => 'gray10',
					     -width => 50,
					     -height => 15,
					     -scrollbars => 'osoe',
					     );
	$text->tagConfigure('keyword', -foreground => 'darkblue');
	$text->tagConfigure('title', -foreground => 'ivory',
			    -background => 'gray60',
			    -spacing1 => 3,
			    -spacing3 => 3);

	my $fm = $text->Frame;
	for (my $i=0; $i < @instances; $i++) {
	    my $j = $i + 1;
	    $fm->Label(-text => 'Instance #'.$j)->grid(-row => $j, -column => 1);
	    $fm->Button(-text => 'Show',
			-cursor  => 'top_left_arrow',
			-command => [\&showinstance, $instances[$i]],
			)->grid(-row => $j, -column => 2);
	    
	    $fm->Button(-text => 'Take focus',
			-cursor  => 'top_left_arrow',
			-command => [\&takefocus, $instances[$i]],
			)->grid(-row => $j, -column => 3);
	    
	    $fm->Button(-text => 'Help',
			-cursor  => 'top_left_arrow',
			-command => [\&showinstancehelp, $instances[$i],
				     'ZincDebug help about instance #'.$j],
			)->grid(-row => $j, -column => 4);
	    #&showinstancehelp($_);
	}
	$text->insert('end', "Several instances of Zinc widget are managed. ");
	$text->insert('end', "They are listed in the following table. \n\n\n");
	$text->window('create', 'end', -window => $fm);
	$text->insert('end', "\n\n\nStrike <");
	$text->insert('end', 'Escape', 'keyword');
	$text->insert('end', "> key to display this help message again.");

	$help_tl{gene}->Button(-command => sub {$help_tl{gene}->destroy},
			    -text => 'Close')->pack(-side => 'bottom',
						 -pady => 10);
	$text->pack(-side => 'top', -pady => 10, -padx => 10);
    }
} # end showgeneralhelp

sub showinstancehelp {
    my $zinc = shift;
    my $title = shift;
    my $singleflag = shift;
    &takefocus($zinc);
    $help_tl{$zinc}->destroy if $help_tl{$zinc} and Tk::Exists($help_tl{$zinc});
    if ($singleflag) {
	$help_tl{$zinc} = $zinc->Toplevel;
    } else {
	$help_tl{$zinc} = $help_tl{gene}->Toplevel;
	$help_tl{$zinc}->transient($help_tl{gene}) unless $singleflag;
    }
    $help_tl{$zinc}->title($title);

    my $text = $help_tl{$zinc}->Scrolled('Text', -font => scalar $zinc->cget(-font),
					 -wrap => 'word',
					 -foreground => 'gray10',
					 -width => 50,
					 -height => 32,
					 -scrollbars => 'osoe',
					 );
    $text->tagConfigure('keyword', -foreground => 'darkblue');
    $text->tagConfigure('title', -foreground => 'ivory',
			-background => 'gray60',
			-spacing1 => 3,
			-spacing3 => 3);
    my $zincnb = scalar keys(%instances);
   if ($treeKey{$zinc}) {
	$text->insert('end', " To display the items tree\n", 'title');
	$text->insert('end', "\nUse the <");
	$text->insert('end', $treeKey{$zinc}, 'keyword');
	$text->insert('end', "> sequence.\n\n");
    }
    if ($enclosedModBtn{$zinc}) {
	my $eseq = $enclosedModBtn{$zinc}->[0]."-Button".$enclosedModBtn{$zinc}->[1];
	my $oseq = $overlapModBtn{$zinc}->[0]."-Button".$overlapModBtn{$zinc}->[1];
	$eseq =~ s/^-//;
	$oseq =~ s/^-//;
	$text->insert('end', " To analyse a particular area\n", 'title');
	$text->insert('end', "\nWith <");
	$text->insert('end', $oseq, 'keyword');
	$text->insert('end', "> sequence, create a rectangular area to parse items ");
	$text->insert('end', "which overlap it.\n");
	$text->insert('end', "\nWith <");
	$text->insert('end',  $eseq, 'keyword');
	$text->insert('end', "> sequence, create a rectangular area to parse items ");
	$text->insert('end', "which are enclosed in it.\n\n");
    }
    if ($treeKey{$zinc} or $enclosedModBtn{$zinc}) {
	$text->insert('end', "To analyse a specific item.\n", 'title');
	if ($enclosedModBtn{$zinc}) {
	    $text->insert('end', "\nWith <");
	    $text->insert('end', $searchKey{$zinc}, 'keyword');
	    $text->insert('end', "> sequence, locate a specific item entering ".
			  "its tagOrId.\n");
	}
	if ($treeKey{$zinc}) {
	    my $tseq = $treeModBtn{$zinc}->[0]."-Button".$treeModBtn{$zinc}->[1];
	    $tseq =~ s/^-//;
	    $text->insert('end', "\nWith <");
	    $text->insert('end', $tseq, 'keyword');
	    $text->insert('end', "> sequence, select a particular item in the ".
			  "application window and locate it in the tree.\n");
	}
	$text->insert('end', "\n");
    }

    if ($snapKey{$zinc}) {
	$text->insert('end', "To snapshot the application window.\n", 'title');
	$text->insert('end', "\nWith <");
	$text->insert('end', $snapKey{$zinc}, 'keyword');
	$text->insert('end', "> sequence you can acquire " .
		      "a snapshot of the full zinc window. ".
		      "It will be saved in the current directory ".
		      "with the name zincsnapshot<n>.png ".
		      "The ImageMagic package must be installed.\n");
    }
    my $fm = $help_tl{$zinc}->Frame->pack(-side => 'bottom',
					  -pady => 5,
					  -expand => 1,
					  -fill => 'none');
    $fm->Button(-text => 'Show',
		-cursor  => 'top_left_arrow',
		-command => [\&showinstance, $zinc],
		)->pack(-side => 'left', -padx => 10) unless $singleflag;
	    
    $fm->Button(-text => 'Take focus',
		-cursor  => 'top_left_arrow',
		-command => [\&takefocus, $zinc],
		)->pack(-side => 'left', -padx => 10);
	    
    $fm->Button(-command => sub {$help_tl{$zinc}->destroy},
			    -text => 'Close')->pack(-side => 'left', -padx => 10);
    $text->pack(-side => 'top', -pady => 10, -padx => 10);
    
} # end showsinstancehelp


# display help about tree
sub showHelpAboutTree {
    my $zinc = shift;
    $helptree_tl->destroy if $helptree_tl and Tk::Exists($helptree_tl);
    $helptree_tl = $tree_tl->Toplevel;
    $helptree_tl->title("Help about Tree");

    my $text = $helptree_tl->Text(-font => scalar $zinc->cget(-font),
				  -wrap => 'word',
				  -foreground => 'gray10',
				  );
    $text->tagConfigure('keyword', -foreground => 'darkblue');
    $text->insert('end', "\nNAVIGATION IN TREE\n\n");
    $text->insert('end', "<Up>", "keyword");
    $text->insert('end', " arrow key moves the anchor point to the item right on ".
		  "top of the current anchor item. ");
    $text->insert('end', "<Down>", "keyword");
    $text->insert('end', " arrow key moves the anchor point to the item right below ".
		  "the current anchor item. ");
    $text->insert('end', "<Left>", "keyword");
    $text->insert('end', " arrow key moves the anchor to the parent item of the ".
		  "current anchor item. ");
    $text->insert('end', "<Right>", "keyword");
    $text->insert('end', " moves the anchor to the first child of the current anchor ".
		  "item. If the current anchor item does not have any children, moves ".
		  "the anchor to the item right below the current anchor item.\n\n");
    $text->insert('end', "\nHIGHLIGHTING ITEMS\n\n");
    $text->insert('end', "To display item's features, ");
    $text->insert('end', "double-click", "keyword");
    $text->insert('end', " on it or press ");
    $text->insert('end', "<Return>", "keyword");
    $text->insert('end', " key.\n\n");
    $text->insert('end', "To highlight item in the application, simply ");
    $text->insert('end', "click", "keyword");
    $text->insert('end', " on it. ");
    &infoAboutHighlighting($text);
    $text->configure(-state => 'disabled');
    
    $helptree_tl->Button(-command => sub {$helptree_tl->destroy},
			 -text => 'Close')->pack(-side => 'bottom',
						 -pady => 10);
    $text->pack->pack(-side => 'top', -pady => 10, -padx => 10);
} # end showHelpAboutTree



sub showHelpAboutAttributes {
    my $zinc = shift;
    $helptree_tl->destroy if $helptree_tl and Tk::Exists($helptree_tl);
    $helptree_tl = $zinc->Toplevel;
    $helptree_tl->title("Help about attributes");

    my $text = $helptree_tl->Text(-font => scalar $zinc->cget(-font),
				  -wrap => 'word',
				  -foreground => 'gray10',
				  );
    $text->tagConfigure('keyword', -foreground => 'darkblue');
    $text->tagConfigure('title', -foreground => 'ivory',
			-background => 'gray60',
			-spacing1 => 3,
			-spacing3 => 3);

    
    $text->insert('end', " To highlight a specific item\n", 'title');
    $text->insert('end',
		  "\nFirst column contains items identifiers buttons you can press to ".
		  "highlight corresponding items in the application.\n");
    &infoAboutHighlighting($text);
    $text->insert('end', "\n\nThird column contains groups identifiers buttons you can ".
		  "press to display groups content and attributes.\n\n");
    $text->insert('end', " To display the bounding box of an item\n", 'title');
    $text->insert('end', "\nUse the buttons of the column labeled ".
		  "'Bounding Box'.\n\n");
    $text->insert('end', " To change the value of attributes\n", 'title');
    $text->insert('end', "\nMost of information fields are editable. A simple ".
		  "colored feedback shows which attributes have changed. Use <");
    $text->insert('end', "Control-z", "keyword");
    $text->insert('end', "> sequence to restore the initial value\n");

    $text->configure(-state => 'disabled');
    
    $helptree_tl->Button(-command => sub {$helptree_tl->destroy},
			 -text => 'Close')->pack(-side => 'bottom',
						 -pady => 10);
    $text->pack->pack(-side => 'top', -pady => 10, -padx => 10);

} # end showHelpAboutAttributes



sub infoAboutHighlighting {
    my $text = shift;
    $text->insert('end', "By default, using ");
    $text->insert('end', "left mouse button", "keyword");
    $text->insert('end', ", highlighting is done by raising selected item and drawing ".
		  "a rectangle arround. ");
    $text->insert('end', "In order to improve visibility, ");
    $text->insert('end', "item will be light backgrounded if you use ");
    $text->insert('end', "center mouse button", "keyword");
    $text->insert('end', " and dark backgrounded if you use ");
    $text->insert('end', "right mouse button", "keyword");
    $text->insert('end', ". ");
    
} # end infoAboutHighlighting


#---------------------------------------------------------------------------
#
# EDITION FUNCTION
#
#---------------------------------------------------------------------------
sub entryoption {
    my ($fm, $item, $zinc, $option) = @_;
    my $def = $zinc->itemcget($item, $option);
    my $i0;
    my $e;
    if ($def =~ /\n/) {
	$e = $fm->Text(-height => 1, -width => 1, -wrap => 'word');
	$i0 = '0.0';
    } else {
	$e = $fm->Entry();
	$i0 = 0;
    }
    my $len = length($def);
    $len = 50 if $len > 50;
    $e->configure(-width => $len);
    if ($defaultoptions{$item}->{$option} and
	$def ne $defaultoptions{$item}->{$option}) {
	$e->configure(-foreground => 'blue');
    }
    
    $e->insert($i0, $def);
    $e->bind('<Control-z>', sub {
	return unless $defaultoptions{$item}->{$option};
	my $bg = $e->cget(-background);
	$zinc->itemconfigure($item, $option => $defaultoptions{$item}->{$option});
	$e->delete($i0, 'end');
	$e->insert($i0, $defaultoptions{$item}->{$option});
	$e->configure(-background => 'ivory');
	$e->after(80, sub {$e->configure(-background => $bg, -foreground => 'black')});
    });
    $e->bind('<Key-Return>',
	     sub {my $val = $e->get;
		  my $bg = $e->cget(-background);
		  $e->configure(-background => 'ivory');
		  if ($def ne $val) {
		      $defaultoptions{$item}->{$option} = $def
			  unless $defaultoptions{$item}->{$option};
		  }
		  my $fg = ($val ne $defaultoptions{$item}->{$option}) ?
		      'blue' : 'black';
		  $e->after(80, sub {
		      $e->configure(-background => $bg, -foreground => $fg);
		  });
		  $zinc->itemconfigure($item, $option => $val);
	      });

    return $e;

} # end entryoption


sub showinstance {
    my $zinc = shift;
    my $a = $zinc->itemcget(1, -alpha);
    my $b = ($a > 40) ? 10 : 100;
    $zinc->itemconfigure(1, -alpha => $b);
    $zinc->update;
    $zinc->after(100);
    $zinc->itemconfigure(1, -alpha => $a);
    $zinc->update;

} # end showinstance


sub takefocus {
    my $zinc = shift;
    $zinc->Tk::focus;

} # end takefocus


sub newinstance {
    my $zinc = shift;
    return if $instances{$zinc};
    &takefocus($zinc);
    $instances{$zinc} = 1;    
    push(@instances, $zinc);
    
} # end newinstance

1;

__END__


=head1 NAME

ZincDebug - a perl module for analysing a Zinc application. 


=head1 SYNOPSIS

 perl -MZincDebug zincscript [zincscript-opts] [zincdebug-opts]
    
     or
    
 use ZincDebug;
 my $zinc = MainWindow->new()->Zinc()->pack;
 finditems($zinc, [options]);
 tree($zinc, [options]);
 snapshot($zinc, [options]);


=head1 DESCRIPTION

ZincDebug provides an interface to help developers to debug or analyse Zinc applications.

With B<finditems()> function, you are able to scan all items which are enclosed in a rectangular area you have first drawn by drag & drop, or all items which overlap it. Result is a Tk table which presents details (options, coordinates, ...) about found items; you can also highlight a particular item, even if it's not visible, by clicking on its corresponding button in the table. You can also display particular item's features by entering this id in dedicated entry field

B<tree()> function displays items hierarchy. You can find a particular item's position in the tree and you can highlight items and see their features as described above.

With B<snapshot()> function, you are able to snapshot the application window, in order to illustrate a graphical bug for example.
    
Press B<Escape> key in the toplevel of the application to have some help about available input sequences.

B<If you load ZincDebug using the -M perl option, nothing needs to be added to your code>. By default, all the previous specific functions are invoked with their default attributes for each instance of Zinc widget. You can overload these by passing the same options to the command. Note that perl arrays must be transformed to comma separated string. For example:
   perl -M ZincDebug zincscript -optionsToDisplay '-tags'
   -optionsFormat row -itemModBtn Control,1


=head1 FUNCTIONS 


=over

=item B<finditems>($zinc, ?option => value, ...?)

This function creates required Tk bindings to permit items search. You can specify the following options :

=over

=item E<32>E<32>E<32>B<-color> => color

Defines color of search area contour. Default to 'sienna'.

=item E<32>E<32>E<32>B<-enclosedModBtn> => [Mod, Btn]

Defines input sequence used to process "enclosed" search. Default to ['Control', 3]. B<Mod> can be set to undef.

=item E<32>E<32>E<32>B<-overlapModBtn> => [Mod, Btn]

Defines input sequence used to process "overlap" search. Default to ['Shift', 3]. B<Mod> can be set to undef.

=item E<32>E<32>E<32>B<-searchKey> => key

Defines input key used to process particular search. Default to 'Control-f'.

=back

=item B<tree>($zinc, ?option => value, ...?)

This function creates required Tk bindings to build items tree. You can specify the following options :

=over

=item E<32>E<32>E<32>B<-tkey|-key> => key

Defines input sequence used to build and display items tree. Default to 'Control-t'.

=item E<32>E<32>E<32>B<-itemModBtn> => [Mod, Btn]

Defines input sequence used to select an item in the application window in order to display its position in the item's tree. Default to ['Control', 2]. B<Mod> can be set to undef.

=item E<32>E<32>E<32>B<-optionsToDisplay> => opt1[,..,optN]

Used to display some option's values associated to items of tree. Expected argument is a string of commas separated options.


=item E<32>E<32>E<32>B<-optionsFormat> => row | column

Defines the display format of option's values. Default is 'column'.

=back

    
=item B<snapshot>($zinc, ?option => value, ...?)

This function creates required Tk binding to snapshot the application window. You can specify the following options :

=over

=item E<32>E<32>E<32>B<-skey|-key> => key

Defines input key used to process a snapshot of the zinc window. Default to ['Control-s'].

=item E<32>E<32>E<32>B<-verbosity> => boolean

Defines if snapshot should print a message on the terminal. Default to true.

=item E<32>E<32>E<32>B<-basename> => "a_string"

Defines the basename used for the file containing the snaphshot. The filename will be <currentdir>/basename<n>.png  Defaulted to zincsnapshot.


=back

=back
    

=head1 AUTEUR

Daniel Etienne <etienne@cena.fr>

    
=head1 HISTORY

Mar 11 2003 : ZincDebug can manage several instances of Zinc widget. Options of ZincDebug functions can be set on the command line.

Jan 20 2003 : item's attributes can be edited.

Jan 14 2003 : ZincDebug can be loaded at runtime using the -M perl option without any change in the application's code.

Nov 6 2002 : some additional informations (like tags or other attributes values) can be displayed in the items tree. Add feedback when selected item is not visible because outside window.

Sep 2 2002 : add the tree() function

May 27 2002 : add the snapshot() function contributed by Ch. Mertz.
    
Jan 28 2002 : Zincdebug provides the finditems() function and can manage only one instance of Zinc widget.