summaryrefslogtreecommitdiff
path: root/modules/graphics/macros/ged.sci
blob: 1cc533fb0081a859c60575a3fd5f1ac6e40fde76 (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
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) INRIA
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

function ged(k,win)

    if ~%tk then
        warning(msprintf(gettext("Tcl/Tk interface not installed.")));
        return
    end

    // Check number of arguments
    if argn(2) ==1 then
        win=get(gcf(),"figure_id")
    elseif argn(2)<>2 then
        error(msprintf(gettext("%s: Wrong number of input arguments: %d expected.\n"), "ged", 2));
    end

    // Check argument #1
    if typeof(k) <> "constant" then
        error(msprintf(gettext("%s: Wrong type for input argument #%d: A Real expected.\n"), "ged", 1));
    end
    if size(k, "*") <> 1 then
        error(msprintf(gettext("%s: Wrong size for input argument #%d: A Real expected.\n"), "ged", 1));
    end

    // Check argument #2
    if typeof(win) <> "constant" then
        error(msprintf(gettext("%s: Wrong type for input argument #%d: A Real expected.\n"), "ged", 2));
    end
    if size(win, "*") <> 1 then
        error(msprintf(gettext("%s: Wrong size for input argument #%d: A Real expected.\n"), "ged", 2));
    end

    global ged_current_figure
    global ged_cur_fig_handle

    ged_current_figure=gcf(); //Preserve current figure

    ged_cur_fig_handle=scf(win);
    show_window(ged_cur_fig_handle);

    // for TCL input args built with string(). See bug http://bugzilla.scilab.org/2479
    initFormat = format()
    format("v",18)  // To be restored with initFormat before leaving

    if k>3 then
        TCL_EvalStr("set isgedinterp [interp exists ged]")
        if ~TCL_ExistInterp( "ged" ) then
            TCL_CreateSlave( "ged" ) ;
        end

    end

    select k
    case 1 then //Select (make it current)
        format(initFormat(2),initFormat(1))
        return
    case 2 then //redraw
        // nothing to do in new graphic mode
    case 3 then //erase
        clf()
    case 4 then //copy
        ged_copy_entity()
    case 5 then //past
        ged_paste_entity()
    case 6 then //move
        ged_move_entity()
    case 7 then //Delete Entity
        ged_delete_entity()

    case 8 then //edit current figure properties

        // hierarchical viewer
        TK_send_handles_list(ged_cur_fig_handle)
        TCL_SetVar("curgedindex",string(Get_handle_pos_in_list(ged_cur_fig_handle)))

        //color_map array for color sample display
        f=ged_cur_fig_handle;
        for i=1:size(f.color_map,1)
            redname= "RED("+string(i)+")";
            TCL_EvalStr("set "+redname+" "+string(f.color_map(i,1)));
            grename= "GREEN("+string(i)+")";
            TCL_EvalStr("set "+grename+" "+string(f.color_map(i,2)));
            bluname= "BLUE("+string(i)+")";
            TCL_EvalStr("set "+bluname+" "+string(f.color_map(i,3)));
        end

        TCL_SetVar("msdos",string(getos() == "Windows")) // to know the OS

        // get the number of the window associated with ged
        TCL_SetVar("sciGedIsAlive",string(ged_cur_fig_handle.figure_id)) ;
        TCL_SetVar("SCIHOME", SCIHOME) // to know the home directory to put temporary files

        ged_figure(ged_cur_fig_handle)
    case 9 then //edit current axes

        // hierarchical viewer
        TK_send_handles_list(ged_cur_fig_handle)
        TCL_SetVar("curgedindex",string(Get_handle_pos_in_list(gca())))

        //color_map array for color sample display
        f=ged_cur_fig_handle;
        for i=1:size(f.color_map,1)
            redname= "RED("+string(i)+")";
            TCL_EvalStr("set "+redname+" "+string(f.color_map(i,1)));
            grename= "GREEN("+string(i)+")";
            TCL_EvalStr("set "+grename+" "+string(f.color_map(i,2)));
            bluname= "BLUE("+string(i)+")";
            TCL_EvalStr("set "+bluname+" "+string(f.color_map(i,3)));
        end

        TCL_SetVar("msdos",string(getos() == "Windows")) // to know the OS

        // get the number of the window associated with ged
        TCL_SetVar("sciGedIsAlive",string(ged_cur_fig_handle.figure_id)) ;
        TCL_SetVar("SCIHOME", SCIHOME) // to know the home directory to put temporary files

        ged_axes(gca())
    case 10 then //start Entity picker
        fig=ged_cur_fig_handle
        fig_ud=get(fig,"user_data")
        if fig.event_handler<>""& fig.event_handler<>"ged_eventhandler" then
            //push current event handler in fig user data if possible
            if fig_ud==[] then fig_ud=struct();end
            if typeof(fig_ud)=="st" then
                if ~isfield(fig_ud,"handlers") then  fig_ud.handlers=[],end
                fig_ud.handlers=[fig_ud.handlers;
                fig.event_handler fig.event_handler_enable]
                set(fig,"user_data",fig_ud)
            else
                warning(_("Entity picker cannot be enabled, user data figure field is already used" ))
                return
            end
            fig.event_handler_enable = "off" //to prevent against bug 7855
            fig.event_handler="ged_eventhandler"
            fig.event_handler_enable="on"
        end
        ged_cur_fig_handle.info_message=_("Left click on a graphic entity to open its property editor");
    case 11 then //stop Entity picker
        fig=ged_cur_fig_handle
        fig_ud=get(fig,"user_data")
        seteventhandler("")
        fig.event_handler_enable = "off"
        if typeof(fig_ud)=="st"&isfield(fig_ud,"handlers")&fig_ud.handlers<>[] then
            fig.event_handler=fig_ud.handlers($,1)
            fig.event_handler_enable=fig_ud.handlers($,2)
            fig_ud.handlers= fig_ud.handlers(1:$-1,:)
            set(fig,"user_data",fig_ud)
        else
            fig.event_handler_enable = "off"
        end
    end
    scf(ged_current_figure)
    format(initFormat(2), initFormat(1))
endfunction


function curgedindex_ = Get_handle_pos_in_list(h)
    global ged_cur_fig_handle

    curgedindex_ = [];

    handles = Get_handles_list(ged_cur_fig_handle)
    for i=1:size(handles,1)
        if (h==handles(i))
            curgedindex_ = i;
        end
    end

    // Other case :
    // a label has been selected (and for now they are included inside the Axes)
    if (curgedindex_==[])
        if h.type == "Label"
            h = h.parent;
            for i=1:size(handles,1)
                if (h==handles(i))
                    curgedindex_ = i;
                end
            end
        end
    end

endfunction


// Search the depth level for each handle
// Usefull for new hierarchical graphic tree.
function ged_levels = Get_levels(handles);

    ged_levels = 1; // for Figure, always 1

    f = handles(1);

    for i=2:size(handles,1)
        ged_levels(i) = Get_Depth(f,handles(i));
    end

    //disp("les levels sont:")xb

    //disp(ged_levels);

endfunction

function depth = Get_Depth(f,h)

    depth = 2;

    while  h.parent <> f
        h = h.parent;
        depth = depth + 1;
    end

endfunction



function TK_send_handles_list(h)
    iFig = 0;
    iUim = 0; // uimenu
    iUic = 0; // uicontrol
    iAxe = 0; // <=> subwindow in C code
    iAgr = 0;
    iPol = 0;
    iPl3 = 0;
    iFac = 0;
    iRec = 0;
    iTex = 0;
    iLeg = 0;
    iArc = 0;
    iSeg = 0;
    iCha = 0; // Champ
    iFec = 0;
    iGra = 0;
    iMat = 0; // forgotten object F.Leray 22.10.04
    iAxi = 0; // axis : entity created when using drawaxis method for example
    iLab = 0;

    f=getparfig(h);
    handles = Get_handles_list(f)

    ged_levels = Get_levels(handles);

    TCL_SetVar("ged_handle_list_size",string(size(handles,1)));

    for i=1:size(handles,1)
        SelObject="LEVELS("+string(i)+")";
        TCL_EvalStr("set "+SelObject+" "+string(ged_levels(i)));
    end


    for i=1:size(handles,1)
        SelObject="SELOBJECT("+string(i)+")";
        hand = handles(i);
        select  hand.type
        case "Figure"
            iFig = iFig+1;
            figname= "Figure("+string(iFig)+")";
            TCL_EvalStr("set "+SelObject+" "+figname);
        case "uimenu"
            iUim = iUim+1;
            uimname= "Uimenu("+string(iUim)+")";
            TCL_EvalStr("set "+SelObject+" "+uimname);
        case "uicontrol"
            iUic = iUic+1;
            uicname= "Uicontrol("+string(iUic)+")";
            TCL_EvalStr("set "+SelObject+" "+uicname);
        case "Axes"
            iAxe = iAxe+1;
            axename= "Axes("+string(iAxe)+")";
            TCL_EvalStr("set "+SelObject+" "+axename);
            //   case "Label"  // to see later: have labels at the same level than Axes (to have a better visibility)
            //    iLab = iLab+1;
            //    labname= "Label("+string(iLab)+")";
            //    TCL_EvalStr('set '+SelObject+" "+labname);
        case "Compound"
            iAgr = iAgr+1;
            agrname= "Compound("+string(iAgr)+")";
            TCL_EvalStr("set "+SelObject+" "+agrname);
        case "Polyline"
            iPol = iPol+1;
            polname= "Polyline("+string(iPol)+")";
            TCL_EvalStr("set "+SelObject+" "+polname);
        case "Plot3d"
            iPl3 = iPl3+1;
            pl3name= "Plot3d("+string(iPl3)+")";
            TCL_EvalStr("set "+SelObject+" "+pl3name);
        case "Fac3d"
            iFac = iFac+1;
            Facname= "Fac3d("+string(iFac)+")";
            TCL_EvalStr("set "+SelObject+" "+Facname);
        case "Rectangle"
            iRec = iRec+1;
            Recname= "Rectangle("+string(iRec)+")";
            TCL_EvalStr("set "+SelObject+" "+Recname);
        case "Text"
            iTex = iTex+1;
            Texname= "Text("+string(iTex)+")";
            TCL_EvalStr("set "+SelObject+" "+Texname);
        case "Legend"
            iLeg = iLeg+1;
            Legname= "Legend("+string(iLeg)+")";
            TCL_EvalStr("set "+SelObject+" "+Legname);
        case "Arc"
            iArc = iArc+1;
            Arcname= "Arc("+string(iArc)+")";
            TCL_EvalStr("set "+SelObject+" "+Arcname);
        case "Segs"
            iSeg = iSeg+1;
            Segname= "Segs("+string(iSeg)+")";
            TCL_EvalStr("set "+SelObject+" "+Segname);
        case "Champ"
            iCha = iCha+1;
            Chaname= "Champ("+string(iCha)+")";
            TCL_EvalStr("set "+SelObject+" "+Chaname);
        case "Fec"
            iFec = iFec+1;
            Fecname= "Fec("+string(iFec)+")";
            TCL_EvalStr("set "+SelObject+" "+Fecname);
        case "Grayplot"
            iGra = iGra+1;
            Graname= "Grayplot("+string(iGra)+")";
            TCL_EvalStr("set "+SelObject+" "+Graname);
        case "Matplot"
            iMat = iMat+1;
            Matname= "Matplot("+string(iMat)+")";
            TCL_EvalStr("set "+SelObject+" "+Matname);
        case "Axis"
            iAxi = iAxi+1;
            Axiname= "Axis("+string(iAxi)+")";
            TCL_EvalStr("set "+SelObject+" "+Axiname);
        else
            error( hand.type + " not handled");
        end
    end
endfunction


//function h=Get_handle_from_index(index)
function Get_handle_from_index(index)
    global ged_handle;
    global ged_cur_fig_handle

    hl = Get_handles_list(ged_cur_fig_handle);

    ged_handle = hl(index);
    //   h=ged_handle;
    tkged();

endfunction


////////////////////////////////////////////
function  hfig= getparfig( h )

    htmp = h;
    hfig= []

    while htmp.type<>"Figure" do
        htmp=htmp.parent
    end

    hfig = htmp;
endfunction


function h_out_list=Get_handles_list(h);

    global ged_handle_out;

    f=getparfig(h);
    ged_handle_out=[f];

    List_handles(f);

    //disp(ged_handle_out);

    h_out_list=ged_handle_out;

endfunction


function List_handles(h)
    global ged_handle_out;

    i = 1;
    //if h.type=="Axes" then
    //   ged_handle_out = [ged_handle_out;h.x_label;h.y_label;h.z_label;h.title];
    //end
    psonstmp = h.children;
    if psonstmp <> [] then
        psonstmp = h.children(1);
    end

    while ((psonstmp <>[]) & ((i) <=size(psonstmp.parent.children,1)))
        i = i+1;
        ged_handle_out = [ged_handle_out;  psonstmp];
        List_handles(psonstmp);

        if ((i) <=size(psonstmp.parent.children,1)) then
            psonstmp = psonstmp.parent.children(i);
        else
            psonstmp=[];
        end

    end

endfunction



function ged_Compound(h)
    global ged_handle;ged_handle=h;
    TCL_SetVar("curvis",h.visible)
    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Compound.tcl")
endfunction


function ged_figure(h)
    global ged_handle;ged_handle=h;
    TCL_SetVar("background",string(h.background))
    TCL_SetVar("rotation_style",h.rotation_style)
    TCL_SetVar("figure_name",h.figure_name)
    TCL_SetVar("figure_id",string(h.figure_id))
    TCL_SetVar("figure_xposition",string(h.figure_position(1)))
    TCL_SetVar("figure_yposition",string(h.figure_position(2)))
    TCL_SetVar("figure_xsiz",string(h.figure_size(1)))
    TCL_SetVar("figure_ysiz",string(h.figure_size(2)))
    TCL_SetVar("figure_xaxesiz",string(h.axes_size(1)))
    TCL_SetVar("figure_yaxesiz",string(h.axes_size(2)))
    TCL_SetVar("bcolor",string(h.background))
    TCL_SetVar("ncolors",string(size(h.color_map,1)))
    TCL_SetVar("curvis",h.visible)
    TCL_SetVar("curpdm",h.pixel_drawing_mode)
    TCL_SetVar("curautoresize",h.auto_resize)
    TCL_SetVar("currotation_style",h.rotation_style)
    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Figure.tcl")
endfunction


function ged_axes(h)
    global ged_handle;ged_handle=h;

    LoadTicks2TCL(h);

    TCL_SetVar("Xaxes_reverseToggle",h.axes_reverse(1))
    TCL_SetVar("Yaxes_reverseToggle",h.axes_reverse(2))
    TCL_SetVar("Zaxes_reverseToggle",h.axes_reverse(3))

    // forgotten axes bounds info.
    TCL_SetVar("axes_boundsL",string(h.axes_bounds(1,1)))
    TCL_SetVar("axes_boundsU",string(h.axes_bounds(1,2)))
    TCL_SetVar("axes_boundsW",string(h.axes_bounds(1,3)))
    TCL_SetVar("axes_boundsH",string(h.axes_bounds(1,4)))

    // forgotten visibility info.
    TCL_SetVar("xlabel_visibility",string(h.x_label.visible))
    TCL_SetVar("ylabel_visibility",string(h.y_label.visible))
    TCL_SetVar("zlabel_visibility",string(h.z_label.visible))
    TCL_SetVar("titlelabel_visibility",string(h.title.visible))

    TCL_SetVar("Lmargins",string(h.margins(1)));
    TCL_SetVar("Rmargins",string(h.margins(2)));
    TCL_SetVar("Tmargins",string(h.margins(3)));
    TCL_SetVar("Bmargins",string(h.margins(4)));
    ged_linestylearray=["solid" "dash" "dash dot" "longdash dot" "bigdash dot" "bigdash longdash" "dot" "double dot"];
    TCL_SetVar("curlinestyle",ged_linestylearray(max(h.line_style,1)))
    if (h.clip_box==[])
        TCL_SetVar("old_Xclipbox","")
        TCL_SetVar("old_Yclipbox","")
        TCL_SetVar("old_Wclipbox","")
        TCL_SetVar("old_Hclipbox","")
        TCL_SetVar("Xclipbox","")
        TCL_SetVar("Yclipbox","")
        TCL_SetVar("Wclipbox","")
        TCL_SetVar("Hclipbox","")
    else
        TCL_SetVar("old_Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("old_Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("old_Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("old_Hclipbox",string(h.clip_box(4)))
        TCL_SetVar("Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("Hclipbox",string(h.clip_box(4)))
    end
    TCL_SetVar("curclipstate",h.clip_state);
    TCL_SetVar("curautoclear",h.auto_clear);
    TCL_SetVar("curautoscale",h.auto_scale);
    //TCL_SetVar("curfillmode",h.fill_mode);
    TCL_SetVar("curalpharotation",string(h.rotation_angles(1)))
    TCL_SetVar("curthetarotation",string(h.rotation_angles(2)))
    ged_fontarray = ["Monospaced" "Symbol" "Serif",..
    "Serif Italic" "Serif Bold" "Serif Bold Italic" ,..
    "SansSerif"  "SansSerif Italic" "SansSerif Bold",..
    "SansSerif Bold Italic"];
    TCL_SetVar("Xlabelpos",h.x_location)
    TCL_SetVar("Ylabelpos",h.y_location)
    TCL_SetVar("Xlabelfontstyle",ged_fontarray(h.x_label.font_style+1))
    TCL_SetVar("Ylabelfontstyle",ged_fontarray(h.y_label.font_style+1))
    TCL_SetVar("Zlabelfontstyle",ged_fontarray(h.z_label.font_style+1))
    TCL_SetVar("TITLEfontstyle",ged_fontarray(h.title.font_style+1))
    TCL_SetVar("fontstyle",ged_fontarray(h.font_style+1))

    // label texts
    if size(h.x_label.text,"*") == 1 then
        txt = """" + h.x_label.text + """" ;
    else
        // an array of strings
        txt = "["+strcat(string(size(h.x_label.text)),"x")+" string array]" ;
    end
    TCL_SetVar("xlabel", txt );

    if size(h.y_label.text,"*") == 1 then
        txt = """" + h.y_label.text + """" ;
    else
        // an array of strings
        txt = "["+strcat(string(size(h.y_label.text)),"x")+" string array]" ;
    end
    TCL_SetVar("ylabel", txt );

    if size(h.z_label.text,"*") == 1 then
        txt = """" + h.z_label.text + """" ;
    else
        // an array of strings
        txt = "["+strcat(string(size(h.z_label.text)),"x")+" string array]" ;
    end
    TCL_SetVar("zlabel", txt );

    if size(h.title.text,"*") == 1 then
        txt = """" + h.title.text + """" ;
    else
        // an array of strings
        txt = "["+strcat(string(size(h.title.text)),"x")+" string array]" ;
    end
    TCL_SetVar("tlabel", txt );

    TCL_SetVar("xlabel_fontforeground",string(h.x_label.font_foreground))
    TCL_SetVar("ylabel_fontforeground",string(h.y_label.font_foreground))
    TCL_SetVar("zlabel_fontforeground",string(h.z_label.font_foreground))
    TCL_SetVar("titlelabel_fontforg",string(h.title.font_foreground))
    TCL_SetVar("xlabel_foreground",string(h.x_label.foreground))
    TCL_SetVar("ylabel_foreground",string(h.y_label.foreground))
    TCL_SetVar("zlabel_foreground",string(h.z_label.foreground))
    TCL_SetVar("titlelabel_foreground",string(h.title.foreground))
    TCL_SetVar("xlabel_background",string(h.x_label.background))
    TCL_SetVar("ylabel_background",string(h.y_label.background))
    TCL_SetVar("zlabel_background",string(h.z_label.background))
    TCL_SetVar("titlelabel_background",string(h.title.background))
    TCL_SetVar("x_position",sci2exp(h.x_label.position,0))
    TCL_SetVar("y_position",sci2exp(h.y_label.position,0))
    TCL_SetVar("z_position",sci2exp(h.z_label.position,0))
    TCL_SetVar("title_position",sci2exp(h.title.position,0))
    TCL_SetVar("xauto_position",h.x_label.auto_position)
    TCL_SetVar("yauto_position",h.y_label.auto_position)
    TCL_SetVar("zauto_position",h.z_label.auto_position)
    TCL_SetVar("titleauto_position",h.title.auto_position)
    TCL_SetVar("xauto_rotation",h.x_label.auto_rotation)
    TCL_SetVar("yauto_rotation",h.y_label.auto_rotation)
    TCL_SetVar("zauto_rotation",h.z_label.auto_rotation)
    TCL_SetVar("titleauto_rotation",h.title.auto_rotation)
    TCL_SetVar("Xfillmode",h.x_label.fill_mode)
    TCL_SetVar("Yfillmode",h.y_label.fill_mode)
    TCL_SetVar("Zfillmode",h.z_label.fill_mode)
    TCL_SetVar("Titlefillmode",h.title.fill_mode)
    TCL_SetVar("xlabel_fontsize",string(h.x_label.font_size))
    TCL_SetVar("ylabel_fontsize",string(h.y_label.font_size))
    TCL_SetVar("zlabel_fontsize",string(h.z_label.font_size))
    TCL_SetVar("titlelabel_fontsize",string(h.title.font_size))
    TCL_SetVar("ncolors",string(size(f.color_map,1)))
    TCL_SetVar("fcolor",string(h.foreground))
    TCL_SetVar("bcolor",string(h.background))
    TCL_SetVar("curthick",string(h.thickness))
    TCL_SetVar("curvis",h.visible)
    TCL_SetVar("curfontsize",string(h.font_size))
    TCL_SetVar("curfontcolor",string(h.font_color))
    TCL_SetVar("limToggle",h.tight_limits(1))
    TCL_SetVar("isoToggle",h.isoview)
    TCL_SetVar("cubToggle",h.cube_scaling)
    TCL_SetVar("viewToggle",h.view)
    TCL_SetVar("curBoxState",h.box)
    TCL_SetVar("xToggle",part(h.log_flags,1))
    TCL_SetVar("yToggle",part(h.log_flags,2))
    TCL_SetVar("zToggle",part(h.log_flags,3))
    TCL_SetVar("xGrid",string(h.grid(1)))
    TCL_SetVar("yGrid",string(h.grid(2)))

    TCL_SetVar("hiddenAxisColor",h.hidden_axis_color)
    TCL_SetVar("curfontangle_x",string(h.x_label.font_angle))
    TCL_SetVar("curfontangle_y",string(h.y_label.font_angle))
    TCL_SetVar("curfontangle_z",string(h.z_label.font_angle))
    TCL_SetVar("curfontangle_title",string(h.title.font_angle))


    select h.view
    case "2d"
        drawlater();
        h.view="3d"
        TCL_SetVar("old_curalpharotation",string(h.rotation_angles(1)))
        TCL_SetVar("old_curthetarotation",string(h.rotation_angles(2)))
        TCL_SetVar("zGrid",string(h.grid(3)))
        TCL_SetVar("zGrid_initial",string(h.grid(3))) //to avoid useless redraw (see Axes.tcl)
        TCL_SetVar("dbxmin",string(h.data_bounds(1,1)))
        TCL_SetVar("dbymin",string(h.data_bounds(1,2)))
        TCL_SetVar("dbzmin",string(h.data_bounds(1,3)))
        TCL_SetVar("dbxmax",string(h.data_bounds(2,1)))
        TCL_SetVar("dbymax",string(h.data_bounds(2,2)))
        TCL_SetVar("dbzmax",string(h.data_bounds(2,3)))
        h.view="2d"
        drawnow();
    case "3d"
        TCL_SetVar("zGrid",string(h.grid(3)))
        TCL_SetVar("zGrid_initial",string(h.grid(3))) //to avoid useless redraw (see Axes.tcl)
        TCL_SetVar("dbxmin",string(h.data_bounds(1,1)))
        TCL_SetVar("dbymin",string(h.data_bounds(1,2)))
        TCL_SetVar("dbzmin",string(h.data_bounds(1,3)))
        TCL_SetVar("dbxmax",string(h.data_bounds(2,1)))
        TCL_SetVar("dbymax",string(h.data_bounds(2,2)))
        TCL_SetVar("dbzmax",string(h.data_bounds(2,3)))
    end

    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Axes.tcl")
endfunction

function ged_rectangle(h)
    global ged_handle; ged_handle=h
    if (h.clip_box==[])
        TCL_SetVar("old_Xclipbox","")
        TCL_SetVar("old_Yclipbox","")
        TCL_SetVar("old_Wclipbox","")
        TCL_SetVar("old_Hclipbox","")
        TCL_SetVar("Xclipbox","")
        TCL_SetVar("Yclipbox","")
        TCL_SetVar("Wclipbox","")
        TCL_SetVar("Hclipbox","")
    else
        TCL_SetVar("old_Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("old_Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("old_Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("old_Hclipbox",string(h.clip_box(4)))
        TCL_SetVar("Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("Hclipbox",string(h.clip_box(4)))
    end
    TCL_SetVar("curclipstate",h.clip_state);

    f=h;while stripblanks(f.type)<>"Figure" then f=f.parent,end
    ax=h;while stripblanks(ax.type)<>"Axes" then ax=ax.parent,end
    TCL_SetVar("ncolors",string(size(f.color_map,1)))
    TCL_SetVar("curcolor",string(h.foreground))
    TCL_SetVar("curback",string(h.background))
    TCL_SetVar("curthick",string(h.thickness))
    TCL_SetVar("curvis",h.visible)
    ged_linestylearray=["solid" "dash" "dash dot" "longdash dot" "bigdash dot" "bigdash longdash" "dot" "double dot"];
    TCL_SetVar("curlinestyle",ged_linestylearray(max(h.line_style,1)))
    ged_markstylearray=["dot" "plus" "cross" "star" "filled diamond" ..
    "diamond" "triangle up" "triangle down" "diamond plus" "circle" ..
    "asterisk" "square" "triangle right" "triangle left" "pentagram"];
    TCL_SetVar("curmarkstyle",ged_markstylearray(abs(h.mark_style)+1))
    TCL_SetVar("curmarkmode",h.mark_mode)
    TCL_SetVar("curmarksize",string(h.mark_size))
    TCL_SetVar("curmarksizeunit",h.mark_size_unit);
    TCL_SetVar("curmarkforeground",string(h.mark_foreground))
    TCL_SetVar("curmarkbackground",string(h.mark_background))


    TCL_SetVar("curlinemode",h.line_mode)
    TCL_SetVar("curfillmode",h.fill_mode)
    // Rectangle data
    select ax.view
    case "2d"
        drawlater();
        ax.view="3d"
        TCL_SetVar("Xval",string(h.data(1)))
        TCL_SetVar("Yval",string(h.data(2)))
        TCL_SetVar("Zval",string(h.data(3)))
        TCL_SetVar("Wval",string(h.data(4)))
        TCL_SetVar("Hval",string(h.data(5)))
        ax.view="2d"
        drawnow();
    case "3d"
        TCL_SetVar("Xval",string(h.data(1)))
        TCL_SetVar("Yval",string(h.data(2)))
        TCL_SetVar("Zval",string(h.data(3)))
        TCL_SetVar("Wval",string(h.data(4)))
        TCL_SetVar("Hval",string(h.data(5)))
    end
    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Rectangle.tcl")
endfunction

function ged_polyline(h)
    global ged_handle; ged_handle=h

    if (h.clip_box==[])
        TCL_SetVar("old_Xclipbox","")
        TCL_SetVar("old_Yclipbox","")
        TCL_SetVar("old_Wclipbox","")
        TCL_SetVar("old_Hclipbox","")
        TCL_SetVar("Xclipbox","")
        TCL_SetVar("Yclipbox","")
        TCL_SetVar("Wclipbox","")
        TCL_SetVar("Hclipbox","")
    else
        TCL_SetVar("old_Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("old_Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("old_Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("old_Hclipbox",string(h.clip_box(4)))
        TCL_SetVar("Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("Hclipbox",string(h.clip_box(4)))
    end
    TCL_SetVar("curclipstate",h.clip_state);

    f=h;while stripblanks(f.type)<>"Figure" then f=f.parent,end
    TCL_SetVar("ncolors",string(size(f.color_map,1)))
    TCL_SetVar("curcolor",string(h.foreground))
    TCL_SetVar("curback",string(h.background))
    TCL_SetVar("curthick",string(h.thickness))
    TCL_SetVar("curarrowsizefactor",string(h.arrow_size_factor))
    TCL_SetVar("curvis",h.visible)

    ged_polylinestylearray=["interpolated" "staircase" "barplot" "arrowed" "filled" "bar"];
    TCL_SetVar("curpolylinestyle",ged_polylinestylearray(max(h.polyline_style,1)))
    ged_linestylearray=["solid" "dash" "dash dot" "longdash dot" "bigdash dot" "bigdash longdash" "dot" "double dot"];
    TCL_SetVar("curlinestyle",ged_linestylearray(max(h.line_style,1)))
    ged_markstylearray=["dot" "plus" "cross" "star" "filled diamond" ..
    "diamond" "triangle up" "triangle down" "diamond plus" "circle" ..
    "asterisk" "square" "triangle right" "triangle left" "pentagram"];

    TCL_SetVar("curmarkstyle",ged_markstylearray(abs(h.mark_style)+1))
    TCL_SetVar("curmarkmode",h.mark_mode)
    TCL_SetVar("curmarksize",string(h.mark_size))
    TCL_SetVar("curmarksizeunit",h.mark_size_unit);
    TCL_SetVar("curmarkforeground",string(h.mark_foreground))
    TCL_SetVar("curmarkbackground",string(h.mark_background))

    TCL_SetVar("curlinemode",h.line_mode)
    TCL_SetVar("curclosedmode",h.closed)
    TCL_SetVar("curfillmode",h.fill_mode)
    TCL_SetVar("curinterpcolormode",h.interp_color_mode)
    TCL_SetVar("curinterpcolorvector",sci2exp(h.interp_color_vector,0))

    d="["+strcat(string(size(h.data)),"x")+" double array]"
    TCL_SetVar("curdata",d);

    select get(getparaxe(h),"view")
    case "2d"
        TCL_SetVar("nbcol",string(2));
    case "3d"
        TCL_SetVar("nbcol",string(3));
    end
    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Polyline.tcl")
endfunction


function ged_plot3d(h)
    global ged_handle; ged_handle=h

    //  if (h.clip_box==[])
    //    TCL_SetVar("old_Xclipbox","")
    //    TCL_SetVar("old_Yclipbox","")
    //    TCL_SetVar("old_Wclipbox","")
    //    TCL_SetVar("old_Hclipbox","")
    //    TCL_SetVar("Xclipbox","")
    //    TCL_SetVar("Yclipbox","")
    //    TCL_SetVar("Wclipbox","")
    //    TCL_SetVar("Hclipbox","")
    //   else
    //    TCL_SetVar("old_Xclipbox",string(h.clip_box(1)))
    //    TCL_SetVar("old_Yclipbox",string(h.clip_box(2)))
    //    TCL_SetVar("old_Wclipbox",string(h.clip_box(3)))
    //    TCL_SetVar("old_Hclipbox",string(h.clip_box(4)))
    //    TCL_SetVar("Xclipbox",string(h.clip_box(1)))
    //    TCL_SetVar("Yclipbox",string(h.clip_box(2)))
    //    TCL_SetVar("Wclipbox",string(h.clip_box(3)))
    //    TCL_SetVar("Hclipbox",string(h.clip_box(4)))
    //  end
    //  TCL_SetVar("curclipstate",h.clip_state);

    TCL_SetVar("curvis",h.visible)
    TCL_SetVar("curcolormode",string(h.color_mode))
    TCL_SetVar("colorflagToggle",string(h.color_flag))
    TCL_SetVar("curforeground",string(h.foreground))
    TCL_SetVar("curhiddencolor",string(h.hiddencolor))
    TCL_SetVar("curthick",string(h.thickness))

    ged_markstylearray=["dot" "plus" "cross" "star" "filled diamond" ..
    "diamond" "triangle up" "triangle down" "diamond plus" "circle" ..
    "asterisk" "square" "triangle right" "triangle left" "pentagram"];

    TCL_SetVar("curmarkstyle",ged_markstylearray(abs(h.mark_style)+1))
    TCL_SetVar("curmarkmode",h.mark_mode)
    TCL_SetVar("curmarksize",string(h.mark_size))
    TCL_SetVar("curmarksizeunit",h.mark_size_unit);
    TCL_SetVar("curmarkforeground",string(h.mark_foreground))
    TCL_SetVar("curmarkbackground",string(h.mark_background))
    TCL_SetVar("curlinemode",h.surface_mode)


    d="["+strcat(string(size(h.data.x)),"x")+" double array]"
    TCL_SetVar("curdata_x",d);
    d="["+strcat(string(size(h.data.y)),"x")+" double array]"
    TCL_SetVar("curdata_y",d);
    d="["+strcat(string(size(h.data.z)),"x")+" double array]"
    TCL_SetVar("curdata_z",d);

    TCL_EvalStr("set flagCOLOR 0")
    if(h.data(1)==["3d" "x" "y" "z" "color"])
        TCL_EvalStr("set flagCOLOR 1")
        d="["+strcat(string(size(h.data.color)),"x")+" integer array]"
        TCL_SetVar("curdata_color",d);
    end
    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Plot3d.tcl")
endfunction


function ged_fac3d(h)
    global ged_handle; ged_handle=h

    //  if (h.clip_box==[])
    //    TCL_SetVar("old_Xclipbox","")
    //    TCL_SetVar("old_Yclipbox","")
    //    TCL_SetVar("old_Wclipbox","")
    //    TCL_SetVar("old_Hclipbox","")
    //    TCL_SetVar("Xclipbox","")
    //    TCL_SetVar("Yclipbox","")
    //    TCL_SetVar("Wclipbox","")
    //    TCL_SetVar("Hclipbox","")
    //   else
    //    TCL_SetVar("old_Xclipbox",string(h.clip_box(1)))
    //    TCL_SetVar("old_Yclipbox",string(h.clip_box(2)))
    //    TCL_SetVar("old_Wclipbox",string(h.clip_box(3)))
    //    TCL_SetVar("old_Hclipbox",string(h.clip_box(4)))
    //    TCL_SetVar("Xclipbox",string(h.clip_box(1)))
    //    TCL_SetVar("Yclipbox",string(h.clip_box(2)))
    //    TCL_SetVar("Wclipbox",string(h.clip_box(3)))
    //    TCL_SetVar("Hclipbox",string(h.clip_box(4)))
    //  end
    //  TCL_SetVar("curclipstate",h.clip_state);

    TCL_SetVar("curvis",h.visible)
    TCL_SetVar("curcolormode",string(h.color_mode))
    TCL_SetVar("colorflagToggle",string(h.color_flag))
    TCL_SetVar("curforeground",string(h.foreground))
    TCL_SetVar("curhiddencolor",string(h.hiddencolor))
    TCL_SetVar("curthick",string(h.thickness))

    ged_markstylearray=["dot" "plus" "cross" "star" "filled diamond" ..
    "diamond" "triangle up" "triangle down" "diamond plus" "circle" ..
    "asterisk" "square" "triangle right" "triangle left" "pentagram"];

    TCL_SetVar("curmarkstyle",ged_markstylearray(abs(h.mark_style)+1))
    TCL_SetVar("curmarkmode",h.mark_mode)
    TCL_SetVar("curmarksize",string(h.mark_size))
    TCL_SetVar("curmarksizeunit",h.mark_size_unit);
    TCL_SetVar("curmarkforeground",string(h.mark_foreground))
    TCL_SetVar("curmarkbackground",string(h.mark_background))
    TCL_SetVar("curlinemode",h.surface_mode)


    d="["+strcat(string(size(h.data.x)),"x")+" double array]"
    TCL_SetVar("curdata_x",d);
    d="["+strcat(string(size(h.data.y)),"x")+" double array]"
    TCL_SetVar("curdata_y",d);
    d="["+strcat(string(size(h.data.z)),"x")+" double array]"
    TCL_SetVar("curdata_z",d);

    TCL_EvalStr("set flagCOLOR 0")
    if(h.data(1)==["3d" "x" "y" "z" "color"])
        TCL_EvalStr("set flagCOLOR 1")
        d="["+strcat(string(size(h.data.color)),"x")+" integer array]"
        TCL_SetVar("curdata_color",d);
    end


    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Fac3d.tcl")
endfunction


function ged_text(h)
    global ged_handle; ged_handle=h
    f=h;while stripblanks(f.type)<>"Figure" then f=f.parent,end
    TCL_SetVar("curvis",h.visible)
    TCL_SetVar("ncolors",string(size(f.color_map,1)))
    TCL_SetVar("curfontforeground",string(h.font_foreground))
    ged_fontarray = ["Monospaced" "Symbol" "Serif",..
    "Serif Italic" "Serif Bold" "Serif Bold Italic" ,..
    "SansSerif"  "SansSerif Italic" "SansSerif Bold",..
    "SansSerif Bold Italic"];
    TCL_SetVar("curfontstyle",ged_fontarray(h.font_style+1))
    TCL_SetVar("curfontsize",string(h.font_size))
    TCL_SetVar("curfontangle",string(h.font_angle))
    TCL_SetVar("curtextboxmode",h.text_box_mode)
    TCL_SetVar("curtext",h.text)
    TCL_SetVar("curforeground",string(h.foreground))
    TCL_SetVar("curbackground",string(h.background))
    TCL_SetVar("curboxmode",h.box)
    TCL_SetVar("curlinemode",h.line_mode);
    TCL_SetVar("curfillmode",h.fill_mode);
    //TCL_SetVar("curPosition",h.data) ;

    if (h.clip_box==[])
        TCL_SetVar("old_Xclipbox","")
        TCL_SetVar("old_Yclipbox","")
        TCL_SetVar("old_Wclipbox","")
        TCL_SetVar("old_Hclipbox","")
        TCL_SetVar("Xclipbox","")
        TCL_SetVar("Yclipbox","")
        TCL_SetVar("Wclipbox","")
        TCL_SetVar("Hclipbox","")
    else
        TCL_SetVar("old_Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("old_Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("old_Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("old_Hclipbox",string(h.clip_box(4)))
        TCL_SetVar("Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("Hclipbox",string(h.clip_box(4)))
    end
    TCL_SetVar("curclipstate",h.clip_state);

    if size(h.text,"*") == 1 then
        txt =""""+ h.text +"""" ; // one double quote at each side
    else
        txt = "["+strcat(string(size(h.text)),"x")+" string array]"
    end
    TCL_SetVar("curtext", txt );
    TCL_SetVar( "textBoxWidth", string( h.text_box(1) ) ) ;
    TCL_SetVar("textBoxHeight", string( h.text_box(2) ) ) ;
    TCL_SetVar("curAlignment",string(h.alignment) ) ;

    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Text.tcl")
endfunction


function ged_legend(h)
    global ged_handle; ged_handle=h
    TCL_SetVar("curvis",h.visible)
    TCL_SetVar("ncolors",string(size(f.color_map,1)))
    TCL_SetVar("curforeground",string(h.foreground))
    ged_fontarray = ["Monospaced" "Symbol" "Serif",..
    "Serif Italic" "Serif Bold" "Serif Bold Italic" ,..
    "SansSerif"  "SansSerif Italic" "SansSerif Bold",..
    "SansSerif Bold Italic"];
    TCL_SetVar("curfontstyle",ged_fontarray(h.font_style+1))
    TCL_SetVar("curfontsize",string(h.font_size))
    TCL_SetVar("curtext",h.text)

    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Legend.tcl")
endfunction


function ged_arc(h)
    global ged_handle; ged_handle=h

    if (h.clip_box==[])
        TCL_SetVar("old_Xclipbox","")
        TCL_SetVar("old_Yclipbox","")
        TCL_SetVar("old_Wclipbox","")
        TCL_SetVar("old_Hclipbox","")
        TCL_SetVar("Xclipbox","")
        TCL_SetVar("Yclipbox","")
        TCL_SetVar("Wclipbox","")
        TCL_SetVar("Hclipbox","")
    else
        TCL_SetVar("old_Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("old_Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("old_Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("old_Hclipbox",string(h.clip_box(4)))
        TCL_SetVar("Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("Hclipbox",string(h.clip_box(4)))
    end
    TCL_SetVar("curclipstate",h.clip_state);
    ged_linestylearray=["solid" "dash" "dash dot" "longdash dot" "bigdash dot" "bigdash longdash" "dot" "double dot"];
    TCL_SetVar("curlinestyle",ged_linestylearray(max(h.line_style,1)))
    TCL_SetVar("nbcol",string(size(h.data,2)))
    for i=1:size(h.data,2)
        val= "arcVAL("+string(i)+")";
        TCL_EvalStr("set "+val+" "+string(h.data(2)));
    end
    f=h;while stripblanks(f.type)<>"Figure" then f=f.parent,end
    TCL_SetVar("ncolors",string(size(f.color_map,1)))
    TCL_SetVar("curcolor",string(h.foreground))
    TCL_SetVar("curback",string(h.background))
    TCL_SetVar("curvis",h.visible)
    TCL_SetVar("curlinemode",h.line_mode)
    TCL_SetVar("curfillmode",h.fill_mode)
    TCL_SetVar("curthick",string(h.thickness))

    ax=getparaxe(h);
    // Arc data
    select ax.view
    case "2d"
        drawlater();
        ax.view="3d"  //strange behavior in 3D... seems to be bugged!!
        TCL_SetVar("Xval",string(h.data(1)))
        TCL_SetVar("Yval",string(h.data(2)))
        TCL_SetVar("Zval",string(h.data(3)))
        TCL_SetVar("Wval",string(h.data(4)))
        TCL_SetVar("Hval",string(h.data(5)))
        TCL_SetVar("A1val",string(h.data(6)))
        TCL_SetVar("A2val",string(h.data(7)))
        ax.view="2d"
        drawnow();
    case "3d"
        TCL_SetVar("Xval",string(h.data(1)))
        TCL_SetVar("Yval",string(h.data(2)))
        TCL_SetVar("Zval",string(h.data(3)))
        TCL_SetVar("Wval",string(h.data(4)))
        TCL_SetVar("Hval",string(h.data(5)))
        TCL_SetVar("A1val",string(h.data(6)))
        TCL_SetVar("A2val",string(h.data(7)))
    end
    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Arc.tcl")
endfunction


function ged_segs(h)
    global ged_handle; ged_handle=h
    TCL_SetVar("curvis",h.visible)
    TCL_SetVar("ncolors",string(size(f.color_map,1)))
    TCL_SetVar("curarrowsize",string(h.arrow_size))
    TCL_SetVar("curthick",string(h.thickness))
    ged_linestylearray=["solid" "dash" "dash dot" "longdash dot" "bigdash dot" "bigdash longdash" "dot" "double dot"];
    TCL_SetVar("curlinestyle",ged_linestylearray(max(h.line_style,1)))

    ged_markstylearray=["dot" "plus" "cross" "star" "filled diamond" ..
    "diamond" "triangle up" "triangle down" "diamond plus" "circle" ..
    "asterisk" "square" "triangle right" "triangle left" "pentagram"];

    TCL_SetVar("curmarkstyle",ged_markstylearray(abs(h.mark_style)+1))
    TCL_SetVar("curmarkmode",h.mark_mode)
    TCL_SetVar("curmarksize",string(h.mark_size))
    TCL_SetVar("curmarksizeunit",h.mark_size_unit);
    TCL_SetVar("curmarkforeground",string(h.mark_foreground))
    TCL_SetVar("curmarkbackground",string(h.mark_background))
    TCL_SetVar("curlinemode",h.line_mode)

    TCL_SetVar("nbrow",string(size(h.data,1)))

    d="["+strcat(string(size(h.data)),"x")+" double array]"
    TCL_SetVar("curdata",d);

    TCL_SetVar("nbcolsegscolor",string(size(h.segs_color,2)))
    for i=1:size(h.segs_color,2)
        val= "segscolorVAL("+string(i)+")";
        TCL_EvalStr("set "+val+" "+string(h.segs_color(i)));
    end

    if (h.clip_box==[])
        TCL_SetVar("old_Xclipbox","")
        TCL_SetVar("old_Yclipbox","")
        TCL_SetVar("old_Wclipbox","")
        TCL_SetVar("old_Hclipbox","")
        TCL_SetVar("Xclipbox","")
        TCL_SetVar("Yclipbox","")
        TCL_SetVar("Wclipbox","")
        TCL_SetVar("Hclipbox","")
    else
        TCL_SetVar("old_Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("old_Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("old_Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("old_Hclipbox",string(h.clip_box(4)))
        TCL_SetVar("Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("Hclipbox",string(h.clip_box(4)))
    end
    TCL_SetVar("curclipstate",h.clip_state);

    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Segs.tcl")
endfunction


function ged_champ(h)
    global ged_handle; ged_handle=h
    TCL_SetVar("curvis",h.visible)
    TCL_SetVar("curcolored",h.colored)
    TCL_SetVar("ncolors",string(size(f.color_map,1)))
    TCL_SetVar("curarrowsize",string(h.arrow_size))
    TCL_SetVar("curthick",string(h.thickness))
    ged_linestylearray=["solid" "dash" "dash dot" "longdash dot" "bigdash dot" "bigdash longdash" "dot" "double dot"];
    TCL_SetVar("curlinestyle",ged_linestylearray(max(h.line_style,1)))

    d="["+strcat(string(size(h.data.x)),"x")+" double array]"
    TCL_SetVar("curdata_x",d);
    d="["+strcat(string(size(h.data.y)),"x")+" double array]"
    TCL_SetVar("curdata_y",d);
    d="["+strcat(string(size(h.data.fx)),"x")+" double array]"
    TCL_SetVar("curdata_fx",d);
    d="["+strcat(string(size(h.data.fy)),"x")+" double array]"
    TCL_SetVar("curdata_fy",d);

    if (h.clip_box==[])
        TCL_SetVar("old_Xclipbox","")
        TCL_SetVar("old_Yclipbox","")
        TCL_SetVar("old_Wclipbox","")
        TCL_SetVar("old_Hclipbox","")
        TCL_SetVar("Xclipbox","")
        TCL_SetVar("Yclipbox","")
        TCL_SetVar("Wclipbox","")
        TCL_SetVar("Hclipbox","")
    else
        TCL_SetVar("old_Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("old_Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("old_Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("old_Hclipbox",string(h.clip_box(4)))
        TCL_SetVar("Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("Hclipbox",string(h.clip_box(4)))
    end
    TCL_SetVar("curclipstate",h.clip_state);

    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Champ.tcl")
endfunction

function ged_fec(h)
    global ged_handle; ged_handle=h
    TCL_SetVar("curvis",h.visible)
    TCL_SetVar("zbmin",string(h.z_bounds(1)))
    TCL_SetVar("zbmax",string(h.z_bounds(2)))
    //  TCL_SetVar("nbrow",string(size(h.data,1)))
    //  TCL_SetVar("nbcol",string(size(h.data,2)))
    //  TCL_SetVar("nbrowTri",string(size(h.triangles,1)))
    //  TCL_SetVar("nbcolTri",string(size(h.triangles,2)))

    d="["+strcat(string(size(h.data)),"x")+" double array]"
    TCL_SetVar("curdata_data",d);
    d="["+strcat(string(size(h.triangles)),"x")+" double array]"
    TCL_SetVar("curdata_triangles",d);

    //  if (h.clip_box==[])
    //    TCL_SetVar("old_Xclipbox","")
    //    TCL_SetVar("old_Yclipbox","")
    //    TCL_SetVar("old_Wclipbox","")
    //    TCL_SetVar("old_Hclipbox","")
    //    TCL_SetVar("Xclipbox","")
    //    TCL_SetVar("Yclipbox","")
    //    TCL_SetVar("Wclipbox","")
    //    TCL_SetVar("Hclipbox","")
    //   else
    //    TCL_SetVar("old_Xclipbox",string(h.clip_box(1)))
    //    TCL_SetVar("old_Yclipbox",string(h.clip_box(2)))
    //    TCL_SetVar("old_Wclipbox",string(h.clip_box(3)))
    //    TCL_SetVar("old_Hclipbox",string(h.clip_box(4)))
    //    TCL_SetVar("Xclipbox",string(h.clip_box(1)))
    //    TCL_SetVar("Yclipbox",string(h.clip_box(2)))
    //    TCL_SetVar("Wclipbox",string(h.clip_box(3)))
    //    TCL_SetVar("Hclipbox",string(h.clip_box(4)))
    //  end
    //  TCL_SetVar("curclipstate",h.clip_state);

    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Fec.tcl")
endfunction


function ged_grayplot(h)
    global ged_handle; ged_handle=h
    TCL_SetVar("curvis",h.visible)
    TCL_SetVar("curdatamapping",h.data_mapping)


    d="["+strcat(string(size(h.data.x)),"x")+" double array]"
    TCL_SetVar("curdata_x",d);
    d="["+strcat(string(size(h.data.y)),"x")+" double array]"
    TCL_SetVar("curdata_y",d);
    d="["+strcat(string(size(h.data.z)),"x")+" double array]"
    TCL_SetVar("curdata_z",d);

    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Grayplot.tcl")

endfunction

function ged_matplot(h)
    global ged_handle; ged_handle=h
    TCL_SetVar("curvis",h.visible)

    d="["+strcat(string(size(h.data)),"x")+" double array]"
    TCL_SetVar("curdata",d);

    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Matplot.tcl")

endfunction


function ged_axis(h)
    global ged_handle; ged_handle=h
    TCL_SetVar("curvis",h.visible)
    TCL_SetVar("curseg",h.tics_segment)
    TCL_SetVar("curcolor",string(h.tics_color))
    TCL_SetVar("curticsstyle",h.tics_style)
    TCL_SetVar("curfontcolor",string(h.labels_font_color))
    TCL_SetVar("curfontsize",string(h.labels_font_size))
    TCL_SetVar("nbcolX",string(size(h.xtics_coord,2)))
    TCL_SetVar("nbcolY",string(size(h.ytics_coord,2)))
    TCL_SetVar("xticscoord",sci2exp(h.xtics_coord,0))
    TCL_SetVar("yticscoord",sci2exp(h.ytics_coord,0))
    TCL_SetVar("cursubtics",string(h.sub_tics))
    TCL_SetVar("curticslabel",sci2exp(h.tics_labels,0))
    TCL_SetVar("curticsdir",string(h.tics_direction))

    if (h.clip_box==[])
        TCL_SetVar("old_Xclipbox","")
        TCL_SetVar("old_Yclipbox","")
        TCL_SetVar("old_Wclipbox","")
        TCL_SetVar("old_Hclipbox","")
        TCL_SetVar("Xclipbox","")
        TCL_SetVar("Yclipbox","")
        TCL_SetVar("Wclipbox","")
        TCL_SetVar("Hclipbox","")
    else
        TCL_SetVar("old_Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("old_Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("old_Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("old_Hclipbox",string(h.clip_box(4)))
        TCL_SetVar("Xclipbox",string(h.clip_box(1)))
        TCL_SetVar("Yclipbox",string(h.clip_box(2)))
        TCL_SetVar("Wclipbox",string(h.clip_box(3)))
        TCL_SetVar("Hclipbox",string(h.clip_box(4)))
    end
    TCL_SetVar("curclipstate",h.clip_state);

    TCL_EvalFile(SCI+"/modules/graphics/tcl/ged/Axis.tcl")
endfunction


function [h,Axes]=ged_getobject(pt)
    h=[];Axes=[];
    f=get("current_figure");
    aold=get("current_axes")
    axes_array=f.children
    //retains only the entities of type Axes (remove uimenus)
    axes_array(axes_array.type<>"Axes")=[];
    // assume that the lastly created objects
    // are at the beginning of the arrays of children
    // We can then select the last object
    // first in the loop.
    for k=1:size(axes_array,"*")
        Axes=axes_array(k)
        set("current_axes",Axes)
        h=ged_loop(Axes,pt)
        if h<>[] then break,end
    end
    set("current_axes",aold)
endfunction

function h=ged_loop(a,pt)

    h=[]
    minDist    = 0.01 ;
    minPixDist = 3    ;

    for ka=1:size(a,"*")
        ck=a(ka) ;
        select ck.type

        case "Polyline"
            xy=ck.data;
            d=Dist2polyline((xy(:,1)-Xmin)/Dx,(xy(:,2)-Ymin)/Dy,pts)
            if d < minDist then h=ck,return,end

        case "Rectangle"
            xy=ck.data;
            x0=xy(1);y0=xy(2);W=xy(3);H=xy(4);
            d=Dist2polyline((x0+[0,W,W,0]-Xmin)/Dx,(y0+[0,0,-H,-H]-Ymin)/Dy,pts)
            if d < minDist then h=ck,return,end

        case "Arc"
            xy=ck.data;
            [xp,yp]=xchange(pt(1),pt(2),"i2f")
            //[dist, toto] = dist2Arc( [xp,yp] ./ [Dx,Dy], xy(1:2)./[Dx,Dy], xy(3)/Dx, xy(4)/Dy, xy(5) / 64., xy(6) / 64. ) ;
            dist = pixDist2Arc( [xp,yp], xy(1:2), xy(3), xy(4), xy(5) / 64., xy(6) / 64. ) ;
            if dist <= minPixDist then
                h=ck;
                return;
            end

        case "Segs"
            xy=ck.data;
            xv=(matrix(xy(:,1),2,-1)-Xmin)/Dx
            yv=(matrix(xy(:,2),2,-1)-Ymin)/Dy
            for ks=1:size(xv,2)
                d=Dist2polyline(xv(:,ks),yv(:,ks),pts)
                if d < minDist then h=ck,return,end
            end
        case "Compound"
            h=ged_loop(ck.children,pt)
            if h<>[] then return,end

        case "Axes"
            xy=ck.data_bounds;
            [xp,yp]=xchange(pt(1),pt(2),"i2f")
            Xmin=xy(1,1);Ymin=xy(1,2),Dx=xy(2,1)-xy(1,1);Dy=xy(2,2)-xy(1,2);
            pts=[(xp-Xmin)/Dx (yp-Ymin)/Dy]
            d=Dist2polyline([0,1,1,0],[0,0,1,1],pts)
            if d < minDist then h=ck,return,end
            h=ged_loop([a.children(:);ck.x_label;ck.y_label;ck.z_label;ck.title],pt)
            if h<>[] then return,end

        case "Text"
            if is_in_text(ck,[xp;yp]) then
                h=ck,
                return,
            end

        case "Label"
            if is_in_text(ck,[xp;yp]) then
                h=ck
                return,
            end

        end
    end
endfunction

function r=is_in_text(h,xy)
    if h.Type == "Text" & h.text_box_mode=="filled" then
        r=(xy(1)>h.data(1)&xy(1)<h.data(1)+h.text_box(1))&(xy(2)>h.data(2)&xy(2)<h.data(2)+h.text_box(2))
    else
        r = stringbox(h);
        r=[r r(:,1)];
        r=and([xy(2) -xy(1)]*diff(r,1,2)+(r(1,1:$-1).*r(2,2:$)-r(1,2:$).*r(2,1:$-1))<0)
    end
endfunction

// compute the square of distance between a point and the ellipse
// in 2D included in an axis aligned rectangle whose upper left
// corner is upperLeft and its wifth and heigth is defined.
function [dist] = dist2Ellipse( point, upperLeft, width, heigth )
    width2  = width  / 2. ;
    heigth2 = heigth / 2. ;
    centerC = [ upperLeft(1) + width2, upperLeft(2) - heigth2 ] ; // center of the ellipse

    // clicked point in the circle frame
    pointC  = [ (point(1) - centerC(1)) / width2, (point(2) - centerC(2)) / heigth2 ] ;

    // get the vector between the point and the closest on the circle
    diffclose = ( 1 - 1 / norm( pointC ) ) * pointC ;
    //closest = pointC / sqrt( pointC(1) * pointC(1) + pointC(2) * pointC(2) ) ;

    // get the difference between the two
    //ffclose = pointC - closest ;

    // bring it back to the current frame value
    diffclose(1) = diffclose(1) * width2  ;
    diffclose(2) = diffclose(2) * heigth2 ;

    // get the distance with the closest point
    dist = norm( diffclose ) ;

endfunction

// compute the distance between a point and the arc
// in 2D included in an axis aligned rectangle whose upper left
// corner is upperLeft and its wifth and heigth is defined.
function [dist,diffClose] = dist2Arc( point, upperLeft, width, heigth, sector1, sector2 )


    if ( width == 0 | heigth == 0 ) then
        dist = %inf ;
        diffClose = [%inf,%inf];
        return ;
    end

    // convert the sector into radiant angle
    angle1 =  sector1            * %pi / 180. ;
    angle2 = (sector1 + sector2) * %pi / 180. ;

    width2  = width  / 2. ;
    heigth2 = heigth / 2. ;
    centerC = [ upperLeft(1) + width2, upperLeft(2) - heigth2 ] ; // center of the ellipse

    // clicked point in the circle frame
    pointC  = [ (point(1) - centerC(1)) / width2, (point(2) - centerC(2)) / heigth2 ] ;

    // get the projection of the clicked point on the circle
    closest = pointC / norm( pointC ) ;

    // now a quite tricky part. The aim is to find
    // if the closest point is in the drawing sector
    // ie if it is between bound1 and bound2 on the circle
    // maybe a eayer solution exists.

    // get the boundaries of the displayed angle
    // the closest point is not on the arc it is one of the two
    // boundaries
    bound1 = [cos(angle1),sin(angle1)] ;
    bound2 = [cos(angle2),sin(angle2)] ;

    // now get the vector of bissecting line between the two bounds
    // with the orientation toward the arc
    b2b1       = bound1 -  bound2 ;
    bissect(1) = -b2b1(2)         ;
    bissect(2) =  b2b1(1)         ;

    // get the position of the point along this axis
    side = closest(1) * bissect(1) + closest(2) * bissect(2) ;

    // get the position of one of the bound (same value for both)
    boundPos = bound1(1) * bissect(1) + bound1(2) * bissect(2) ;

    if side > boundPos  then
        // the closest point is on the arc
        diffClose = ( pointC - closest ) .* [width2,heigth2] ;
        // bring it back to the current frame value
        //diffclose = diffclose .* [width2,heigth2] ;

        // get the distance with the closest point
        dist = norm( diffClose ) ;

    else
        // the closest point is one of the bounds
        // return back to the real coordinates
        bound1 = centerC + bound1 .* [width2,heigth2];
        bound2 = centerC + bound2 .* [width2,heigth2];

        // get the minimum distance
        dist  = norm( bound1 - point ) ;
        dist2 = norm( bound2 - point ) ;
        if dist > dist2 then
            diffClose = bound1 - point ;
        else
            dist = dist2 ;
            diffClose = bound2 - point ;
        end
        //dist = min( norm( bound1 - point ), norm( bound2 - point ) ) ;
    end

endfunction

// same as before but return the value in pixels
function dist = pixDist2Arc( point, upperLeft, width, heigth, sector1, sector2 )

    [dist, difference] = dist2Arc( point, upperLeft, width, heigth, sector1, sector2 ) ;
    // convert to pixels
    // get the length of the difference vector
    // we construct it by getting two points
    [origin(1),origin(2)] = xchange(0,0,"f2i");
    [extremity(1),extremity(2)] = xchange(difference(1),difference(2),"f2i");
    dist = norm( extremity - origin ) ;

endfunction

function [d,pt,ind]=Dist2polyline(xp,yp,pt)
    // computes minimum distance from a point to a polyline
    //d    minimum distance to polyline
    //pt   coordinate of the polyline closest point
    //ind
    //     if negative polyline closest point is a polyline corner:pt=[xp(-ind) yp(-ind)]
    //     if positive pt lies on segment [ind ind+1]

    // Copyright INRIA
    x=pt(1)
    y=pt(2)
    xp=xp(:);yp=yp(:)
    cr=4*sign((xp(1:$-1)-x).*(xp(1:$-1)-xp(2:$))+..
    (yp(1:$-1)-y).*(yp(1:$-1)-yp(2:$)))+..
    sign((xp(2:$)-x).*(xp(2:$)-xp(1:$-1))+..
    (yp(2:$)-y).*(yp(2:$)-yp(1:$-1)))

    ki=find(cr==5) // index of segments for which projection fall inside
    np=size(xp,"*")
    if ki<>[] then
        //projection on segments
        x=[xp(ki) xp(ki+1)]
        y=[yp(ki) yp(ki+1)]
        dx=x(:,2)-x(:,1)
        dy=y(:,2)-y(:,1)
        d_d=dx.^2+dy.^2
        d_x=( dy.*(-x(:,2).*y(:,1)+x(:,1).*y(:,2))+dx.*(dx*pt(1)+dy*pt(2)))./d_d
        d_y=(-dx.*(-x(:,2).*y(:,1)+x(:,1).*y(:,2))+dy.*(dx*pt(1)+dy*pt(2)))./d_d
        xp=[xp;d_x]
        yp=[yp;d_y]
    end
    [d,k]=min(((xp-pt(1))).^2+((yp-pt(2))).^2) //distance with all points
    d=sqrt(d)
    pt(1)=xp(k)
    pt(2)=yp(k)
    if k>np then ind=ki(k-np),else ind=-k,end
endfunction

function ged_eventhandler(win,x,y,ibut)
    //Copyright INRIA
    //Author : Serge Steer 2002
    if ibut==-1 then return,end //ignore move
    if and(win<>winsid())| ibut==-1000 then
        //window has been deleted by the user
        return
    end
    cur=gcf(); //preserve current figure
    fig=scf(win) //make the window associated to the event active

    //disable the event handler not to execute new event before finishing this one
    fig.event_handler_enable = "off";


    global ged_handle;ged_handle=[]
    ged_handle=ged_getobject([x,y])

    if ged_handle~=[] then
        if  or(ibut==[0 3 10]) then //left button --> edit properties
            tkged()
        elseif or(ibut==[2 5 12]) then //right button -->move
            [x,y]=xchange(x,y,"i2f")
            pos=[x,y]
            while %t then
                rep=xgetmouse([%t %t])
                if rep(3)>0 then break,end
                move(ged_handle,rep(1:2)-pos)
                pos=rep(1:2)
            end
        end
    end
    fig.event_handler_enable = "on";
    scf(cur) //reset current window
endfunction


function [ini,typs]=build_args(labels)
    n=size(labels,"*")
    ini=[]
    typs=list()
    for k=1:n
        typ=type(h(labels(k)))
        execstr(["if typ==10 then"
        "   w=h."+labels(k)
        "else"
        "   w=sci2exp(h."+labels(k)+",0)"
        "end"])
        ini=[ini;w]
        if typ==10 then
            typs($+1)="str";typs($+1)=-1
        else
            typs($+1)="vec";typs($+1)=1
        end
    end
endfunction

function GetSetValue(h)
    n=size(labels,"*")
    lhs="[ok,"+strcat("x"+string(1:n),",")+"]"
    execstr(lhs+"=getvalue(t,labels,typs,ini)")
    if ok then
        for k=1:n
            execstr("if x"+string(k)+"<>h."+labels(k)+" then h."+..
            labels(k)+"=x"+string(k)+",end")
        end
    end
endfunction



function tkged()
    global ged_handle
    global ged_cur_fig_handle

    h=ged_handle

    // hierarchical viewer
    TK_send_handles_list(ged_cur_fig_handle)
    TCL_SetVar("curgedindex",string(Get_handle_pos_in_list(h)))

    //color_map array for color sample display
    f=getparfig(h);
    for i=1:size(f.color_map,1)
        redname= "RED("+string(i)+")";
        TCL_EvalStr("set "+redname+" "+string(f.color_map(i,1)));
        grename= "GREEN("+string(i)+")";
        TCL_EvalStr("set "+grename+" "+string(f.color_map(i,2)));
        bluname= "BLUE("+string(i)+")";
        TCL_EvalStr("set "+bluname+" "+string(f.color_map(i,3)));
    end

    TCL_SetVar("msdos",string(getos() == "Windows")) // to know the OS

    // get the number of the window associated with ged
    TCL_SetVar("sciGedIsAlive",string(ged_cur_fig_handle.figure_id)) ;
    TCL_SetVar("SCIHOME", SCIHOME) // to know the home directory to put temporary files

    select h.type
    case "Polyline"
        ged_polyline(h)
    case "Rectangle"
        ged_rectangle(h)
    case "Axes"
        ged_axes(h)
    case "Label" // for now the labels are inside the axes (F.Leray 06.12.05)
        ged_axes(h.parent)
        if (h == h.parent.x_label)
            TCL_EvalStr("Notebook:raise $uf.n X");
        elseif (h == h.parent.y_label)
            TCL_EvalStr("Notebook:raise $uf.n Y");
        elseif (h == h.parent.z_label)
            TCL_EvalStr("Notebook:raise $uf.n Z");
        elseif (h == h.parent.title)
            TCL_EvalStr("Notebook:raise $uf.n Title");
        end
    case "Figure"
        ged_figure(h)
    case "Compound"
        ged_Compound(h)

    case "Plot3d"
        ged_plot3d(h)
    case "Fac3d"
        ged_fac3d(h)
    case "Text"
        ged_text(h)
    case "Legend"
        ged_legend(h)
    case "Arc"
        ged_arc(h)
    case "Segs"
        ged_segs(h)
    case "Champ"
        ged_champ(h)
    case "Fec"
        ged_fec(h)
    case "Grayplot"
        ged_grayplot(h)
    case "Matplot"
        ged_matplot(h)
    case "Axis"
        ged_axis(h)
    end
endfunction

function setStyle(sty)
    global ged_handle; h=ged_handle
    h.polyline_style=find(sty==["interpolated","staircase", ...
    "barplot","arrowed","filled" "bar"])
endfunction
function setLineStyle(sty)
    global ged_handle; h=ged_handle
    h.line_style=find(sty==[ "solid" "dash" "dash dot" "longdash dot" ..
    "bigdash dot" "bigdash longdash" "dot" "double dot"])

endfunction
function setMarkStyle(sty)
    global ged_handle; h=ged_handle
    h.mark_style=find(sty==["dot" "plus" "cross" "star" "filled diamond" ..
    "diamond" "triangle up" "triangle down" "diamond plus" "circle" ..
    "asterisk" "square" "triangle right" "triangle left" "pentagram"])-1
endfunction
function setFontStyle(ftn)
    global ged_handle; h=ged_handle
    h.font_style=find(ftn==["Monospaced" "Symbol" "Serif",..
    "Serif Italic" "Serif Bold" "Serif Bold Italic" ,..
    "SansSerif"  "SansSerif Italic" "SansSerif Bold",..
    "SansSerif Bold Italic"])-1;
endfunction

function setLabelsFontStyle(label,ftn)
    global ged_handle; h=ged_handle
    select label
    case "t"
        TCL_EvalStr("Notebook:raise $uf.n Z");
        h.title.font_style=find(ftn==["Monospaced" "Symbol" "Serif",..
        "Serif Italic" "Serif Bold" "Serif Bold Italic" ,..
        "SansSerif"  "SansSerif Italic" "SansSerif Bold",..
        "SansSerif Bold Italic"])-1;
    case "x"
        h.x_label.font_style=find(ftn==["Monospaced" "Symbol" "Serif",..
        "Serif Italic" "Serif Bold" "Serif Bold Italic" ,..
        "SansSerif"  "SansSerif Italic" "SansSerif Bold",..
        "SansSerif Bold Italic"])-1;
    case "y"
        h.y_label.font_style=find(ftn==["Monospaced" "Symbol" "Serif",..
        "Serif Italic" "Serif Bold" "Serif Bold Italic" ,..
        "SansSerif"  "SansSerif Italic" "SansSerif Bold",..
        "SansSerif Bold Italic"])-1;
    case "z"
        h.z_label.font_style=find(ftn==["Monospaced" "Symbol" "Serif",..
        "Serif Italic" "Serif Bold" "Serif Bold Italic" ,..
        "SansSerif"  "SansSerif Italic" "SansSerif Bold",..
        "SansSerif Bold Italic"])-1;
    end;
endfunction


// Axes data (data_bounds)
function setXdb(xmin, xmax)
    global ged_handle; h=ged_handle
    tmp=h.data_bounds;
    tmp(1,1)=xmin;
    tmp(2,1)=xmax;
    h.data_bounds=tmp;
    //  tst=execstr('h.data_bounds=tmp','errcatch','n');
    //  if tst<>0 then
    //   disp 'Warning: X data_bounds must contain double'
    //  end
endfunction


function setYdb(ymin, ymax)
    global ged_handle; h=ged_handle
    tmp=h.data_bounds;
    tmp(1,2)=ymin;
    tmp(2,2)=ymax;
    h.data_bounds=tmp;
    //  tst=execstr('h.data_bounds=tmp','errcatch','n');
    //  if tst<>0 then
    //   disp 'Warning: Y data_bounds must contain double'
    //  end
endfunction

function setZdb(zmin, zmax)
    global ged_handle; h=ged_handle
    select h.view
    case "2d"
        drawlater();
        h.view="3d";
        tmp=h.data_bounds;
        tmp(1,3)=zmin;
        tmp(2,3)=zmax;
        h.data_bounds=tmp;
        h.view="2d";
        drawnow();
        //      tst=execstr('h.data_bounds=tmp','errcatch','n');
        //      h.view='2d';
        //      if tst<>0 then
        //       disp 'Warning: Z data_bounds must contain double'
        //      end
    case "3d"
        tmp=h.data_bounds;
        tmp(1,3)=zmin;
        tmp(2,3)=zmax;
        h.data_bounds=tmp;
        //      tst=execstr('h.data_bounds=tmp','errcatch','n');
        //      if tst<>0 then
        //        disp 'Warning: Z data_bounds must contain double'
        //      end
    end
endfunction


function LogtoggleX( tog)
    global ged_handle; h=ged_handle

    h.log_flags=tog+part(h.log_flags,2);


    //tst=execstr("global h;h.log_flags=tog+part(h.log_flags,2)",'errcatch','n');

    //if tst<>0 then
    //   disp 'Warning: X bounds must be strictly positive'
    //end
endfunction

function LogtoggleY( tog)
    global ged_handle; h=ged_handle

    h.log_flags=part(h.log_flags,1)+tog;


    //tst=execstr("global h;h.log_flags=part(h.log_flags,1)+tog",'errcatch','n');

    //if tst<>0 then
    //   disp 'Warning: Y bounds must be strictly positive'
    //end

endfunction


function LogtoggleZ( tog)
    global ged_handle; h=ged_handle

    h.log_flags=part(h.log_flags,1)+part(h.log_flags,2)+tog;


    //tst=execstr("global h;h.log_flags=part(h.log_flags,1)+tog",'errcatch','n');

    //if tst<>0 then
    //   disp 'Warning: Z bounds must be strictly positive'
    //end

endfunction



function [haxe] = getparaxe( h )

    htmp = h;
    haxe = [];
    while htmp.type<>"Axes" do
        htmp = htmp.parent
    end

    haxe = htmp;
endfunction




// Rectangle data
function setXval(val)
    global ged_handle; h=ged_handle
    tmp=h.data;
    tmp(1)=val;
    tst=execstr("h.data=tmp","errcatch","n");
    if tst<>0 then
        disp "Warning: Y data must contain double"
    end
endfunction

function setYval(val)
    global ged_handle; h=ged_handle
    tmp=h.data;
    tmp(2)=val;
    tst=execstr("h.data=tmp","errcatch","n");
    if tst<>0 then
        disp "Warning: Y data must contain double"
    end
endfunction

function setZval(val)
    global ged_handle; h=ged_handle
    ax=getparaxe(h);
    select ax.view
    case "2d"
        drawlater();
        ax.view="3d";
        tmp=h.data;
        tmp(3)=val;
        tst=execstr("h.data=tmp","errcatch","n");
        ax.view="2d";
        drawnow();
        if tst<>0 then
            disp "Warning: Z data must contain double"
        end
    case "3d"
        tmp=h.data;
        tmp(3)=val;
        tst=execstr("h.data=tmp","errcatch","n");
        if tst<>0 then
            disp "Warning: Z data must contain double"
        end
    end
endfunction


function setWval(val)
    global ged_handle; h=ged_handle
    ax=getparaxe(h);
    select ax.view
    case "2d"
        drawlater();
        ax.view="3d";
        tmp=h.data;
        tmp(4)=val;
        tst=execstr("h.data=tmp","errcatch","n");
        ax.view="2d";
        drawnow();
        if tst<>0 then
            disp "Warning: Width data must contain double"
        end
    case "3d"
        tmp=h.data;
        tmp(4)=val;
        tst=execstr("h.data=tmp","errcatch","n");
        if tst<>0 then
            disp "Warning: Width data must contain double"
        end
    end
endfunction


function setHval(val)
    global ged_handle; h=ged_handle
    ax=getparaxe(h);
    select ax.view
    case "2d"
        drawlater();
        ax.view="3d";
        tmp=h.data;
        tmp(5)=val;
        tst=execstr("h.data=tmp","errcatch","n");
        ax.view="2d";
        drawnow();
        if tst<>0 then
            disp "Warning: Height data must contain double"
        end
    case "3d"
        tmp=h.data;
        tmp(5)=val;
        tst=execstr("h.data=tmp","errcatch","n");
        if tst<>0 then
            disp "Warning: Height data must contain double"
        end
    end
endfunction


// complement for Arc entity
function setA1val(val)
    global ged_handle; h=ged_handle
    ax=getparaxe(h);
    select ax.view
    case "2d"
        drawlater();
        ax.view="3d";
        tmp=h.data;
        tmp(6)=val;
        tst=execstr("h.data=tmp","errcatch","n");
        ax.view="2d";
        drawnow();
        if tst<>0 then
            disp "Warning: Width data must contain double"
        end
    case "3d"
        tmp=h.data;
        tmp(6)=val;
        tst=execstr("h.data=tmp","errcatch","n");
        if tst<>0 then
            disp "Warning: a1 data must contain double"
        end
    end
endfunction

function setA2val(val)
    global ged_handle; h=ged_handle
    ax=getparaxe(h);
    select ax.view
    case "2d"
        drawlater();
        ax.view="3d";
        tmp=h.data;
        tmp(7)=val;
        tst=execstr("h.data=tmp","errcatch","n");
        ax.view="2d";
        drawnow();
        if tst<>0 then
            disp "Warning: Width data must contain double"
        end
    case "3d"
        tmp=h.data;
        tmp(7)=val;
        tst=execstr("h.data=tmp","errcatch","n");
        if tst<>0 then
            disp "Warning: a2 data must contain double"
        end
    end
endfunction


//Fec
function setZb(min, max)
    global ged_handle; h=ged_handle
    tmp=h.z_bounds;
    tmp(1,1)=min;
    tmp(1,2)=max;
    tst=execstr("h.z_bounds=tmp","errcatch","n");
    if tst<>0 then
        disp "Warning: X data_bounds must contain double"
    end
endfunction



////////////////////////////////
// Scilab Command Interface ////
////////////////////////////////
//Plot3d
function set3dtlistXYZ (X,Y,Z)
    global ged_handle; h=ged_handle
    ged_tmp_tlist = tlist(["3d","x","y","z"],X,Y,Z)
    h.data=ged_tmp_tlist;
    clear ged_tmp_tlist;
endfunction

function set3dtlistXYZC (X,Y,Z,COLOR)
    global ged_handle; h=ged_handle
    ged_tmp_tlist = tlist(["3d","x","y","z","color"],X,Y,Z,COLOR)
    h.data=ged_tmp_tlist;
    clear ged_tmp_tlist;
endfunction

function setGrayplottlist(X,Y,Z)
    global ged_handle; h=ged_handle
    ged_tmp_tlist = tlist(["grayplotdata","x","y","z"],X,Y,Z)
    h.data=ged_tmp_tlist;
    clear ged_tmp_tlist;
endfunction

//Champ
function setchamptlistXYFXFY (X,Y,FX,FY)
    global ged_handle; h=ged_handle
    ged_tmp_tlist = tlist(["champdata","x","y","fx","fy"],X,Y,FX,FY)
    h.data=ged_tmp_tlist;
    clear ged_tmp_tlist;
endfunction


///////////////////////////////
// Edit Data using sciGUI /////
///////////////////////////////

function EditData(TheData,datastring)
    // TheData must be a real scalar or matrix
    global ged_handle; h=ged_handle

    // I declare ged_tmp ged_tmp_string WINDOW as global
    global ged_tmp;
    global ged_tmp_string;
    global WINDOW;

    ged_tmp_string = datastring;

    ged_tmp=TheData;

    WINDOW = GEDeditvar("ged_tmp")

endfunction


function CloseEditorSaveData()
    // Called when closing data editor

    global ged_handle; // To leave here because used when doing execstr(ged_tmp_string...
    global ged_tmp;
    global ged_tmp_string;
    global WINDOW;

    Nan = %nan // to avoid error message because of special Nan display

    ged_tmp=GEDeditvar_get(WINDOW);

    execstr(ged_tmp_string+"= ged_tmp");

    clearglobal ged_tmp ged_tmp_string WINDOW
    clear ged_tmp ged_tmp_string WINDOW

endfunction


function outvar=GEDeditvar_get(winId)
    // Simple variable editor - import data
    // This file is part of sciGUI toolbox
    // Copyright (C) 2004 Jaime Urzua Grez
    // mailto:jaime_urzua@yahoo.com
    // rev. 0.1
    //
    // This program is free software; you can redistribute it and/or modify
    //it under the terms of the GNU General Public License as published by
    //the Free Software Foundation; either version 2 of the License, or
    //(at your option) any later version.

    //disp("Please wait...");
    outvar=[];
    base="sciGUITable(win,"+string(winId)+",data";
    varType=evstr(TCL_GetVar(base+",type)"));
    varni=evstr(TCL_GetVar(base+",ni)"));
    varnj=evstr(TCL_GetVar(base+",nj)"));
    for j=1:varnj,
        ww=[];
        for i=1:varni,
            q=TCL_GetVar(base+","+string(i)+","+string(j)+")");
            if (varType~=10) then
                if (varType==4) then
                    if ((q=="T")|(q=="t")) then
                        ww=[ww;%t];
                    else
                        ww=[ww;%f];
                    end
                else
                    ww=[ww;evstr(q)];
                end
            else
                ww=[ww;q];
            end
        end
        outvar=[outvar ww];
    end
endfunction

function [WINID] = GEDeditvar(varargin)
    // Simple Variable Editor
    // This file is part of sciGUI toolbox
    // Copyright (C) 2004 Jaime Urzua Grez
    // mailto:jaime_urzua@yahoo.com
    // rev. 0.2 2004/06/24
    //
    // This program is free software; you can redistribute it and/or modify
    //it under the terms of the GNU General Public License as published by
    //the Free Software Foundation; either version 2 of the License, or
    //(at your option) any later version.

    sciGUI_init()

    [%_nams]=who("get");
    %_loc_type=type(varargin(1))
    if (%_loc_type~=10) then error(42), end
    %_in_list=find(%_nams==varargin(1));
    if (%_in_list==[]) then error(42), end
    execstr("%_loc_var="+varargin(1));
    %_loc_type=type(%_loc_var);
    %_allo=find([1;4;10]==%_loc_type);
    if (%_allo==[]) then error(42), end

    //disp("Please wait...");
    %_loc_nam=varargin(1);
    %_loc_ni=size(%_loc_var,1);
    %_loc_nj=size(%_loc_var,2);
    %_loc_type=string(%_loc_type);

    TCL_EvalStr("set EdVarLoc [GEDsciGUIEditVar -1]");

    %_winId=TCL_GetVar("EdVarLoc");

    TCL_SetVar("sciGUITable(win,"+%_winId+",data,name)",%_loc_nam);
    TCL_SetVar("sciGUITable(win,"+%_winId+",data,type)",string(%_loc_type));
    TCL_SetVar("sciGUITable(win,"+%_winId+",data,ni)",string(%_loc_ni));
    TCL_SetVar("sciGUITable(win,"+%_winId+",data,nj)",string(%_loc_nj));

    //
    // This is a slow manner to move the data ...
    // Anyone have a faster method than TCL_SetVar
    // I would like set some tcl variable like varname(pos_i,pos_j)
    //
    Nb_data=(%_loc_nj)*(%_loc_ni)
    // disp("Nb_data=")
    // disp(Nb_data);
    // winWB=waitbar('Loading data...');
    // tmp = 0;
    // waitbar(tmp,winWB);


    for %_j=1:%_loc_nj,
        for %_i=1:%_loc_ni,
            %_value=string(%_loc_var(%_i,%_j));
            %_varname="sciGUITable(win,"+%_winId+",data,"+string(%_i)+","+string(%_j)+")";
            TCL_SetVar(%_varname,%_value);
        end

        //tmp = ((%_j)*(%_loc_ni)) / Nb_data;
        //tmp = tmp *100; tmp = int(tmp); tmp = tmp /100;
        //disp("tmp dans for=")
        //disp(tmp)
        //waitbar(tmp,winWB);
    end


    // disp("%_winId=");
    // disp(%_winId);
    // disp("type(%_winId)=");
    // disp(type(%_winId));
    // disp("winWB=");
    // disp(winWB);
    // disp("type(winWB)=");
    // disp(type(winWB));
    // disp("AVANT DrawGrid");


    TCL_EvalStr("GEDsciGUIEditVarDrawGrid "+%_winId)

    WINID = %_winId;

endfunction


// for the ticks arrays Locations and Labels (inside TLIST)

function ged_tablo=GetTab(val,index)
    //disp("ICI")
    ged_tablo(index) =val
endfunction


function ged_tablo=GetTab2(val,index,ged_tablo)
    //disp("ICI")
    ged_tablo(index) =val
endfunction

function setTicksTList(XYZ,locations,labels)
    global ged_handle;h= ged_handle;

    TL=tlist(["ticks","locations","labels"],locations,labels);

    if XYZ=="X"
        h.x_ticks=TL;
    elseif XYZ=="Y"
        h.y_ticks=TL;
    elseif XYZ=="Z"
        h.z_ticks=TL;
    end

endfunction


// Is called by ged_axes

function LoadTicks2TCL(h)
    global ged_handle;ged_handle=h;

    TCL_SetVar("Xaxes_visibleToggle",h.axes_visible(1))
    TCL_SetVar("Yaxes_visibleToggle",h.axes_visible(2))
    TCL_SetVar("Zaxes_visibleToggle",h.axes_visible(3))

    TCL_SetVar("SubticksEntryX",string(h.sub_ticks(1)))
    TCL_GetVar("SubticksEntryX")

    // disp("h.sub_ticks(1) =")
    // disp(h.sub_ticks(1));

    TCL_SetVar("SubticksEntryY",string(h.sub_ticks(2)))

    select h.view
    case "2d"
        drawlater(); // postpon the drawings due to switching from 2d to 3d mode (for example)
        // in order to know the complete data set (z data for axes...)
        h.view="3d"

        TCL_SetVar("SubticksEntryZ",string(h.sub_ticks(3)))
        h.view="2d"
        drawnow();
    case "3d"
        TCL_SetVar("SubticksEntryZ",string(h.sub_ticks(3)))
    end

    TCL_SetVar("XautoticksToggle",h.auto_ticks(1))
    TCL_SetVar("YautoticksToggle",h.auto_ticks(2))
    TCL_SetVar("ZautoticksToggle",h.auto_ticks(3))

    //ticks value: X axis
    ticks = h.x_ticks;
    sizeticks = size(ticks.locations,1);
    TCL_SetVar("nbticks_x",string(sizeticks));
    for i=1:sizeticks
        val= "LOCATIONS_X("+string(i)+")";
        TCL_EvalStr("set "+val+" "+string(ticks.locations(i)));
        val= "LABELS_X("+string(i)+")";
        TCL_EvalStr("set "+val+" {"+ticks.labels(i)+"}");
    end

    //ticks value: Y axis
    ticks = h.y_ticks;
    sizeticks = size(ticks.locations,1);
    TCL_SetVar("nbticks_y",string(sizeticks));
    for i=1:sizeticks
        val= "LOCATIONS_Y("+string(i)+")";
        TCL_EvalStr("set "+val+" "+string(ticks.locations(i)));
        val= "LABELS_Y("+string(i)+")";
        TCL_EvalStr("set "+val+" {"+ticks.labels(i)+"}");
    end

    //ticks value: Z axis
    ticks = h.z_ticks;
    sizeticks = size(ticks.locations,1);
    TCL_SetVar("nbticks_z",string(sizeticks));
    for i=1:sizeticks
        val= "LOCATIONS_Z("+string(i)+")";
        TCL_EvalStr("set "+val+" "+string(ticks.locations(i)));
        val= "LABELS_Z("+string(i)+")";
        TCL_EvalStr("set "+val+" {"+ticks.labels(i)+"}");
    end

endfunction



function Subtickstoggle( tog, index)
    global ged_handle; h=ged_handle

    subticks=h.sub_ticks;
    subticks(index)=tog;
    //disp("subticks vaut:")
    //disp(subticks)

    h.sub_ticks = subticks;
endfunction


// when clicking on Ticks button : REload ticks is called

function ReLoadTicks2TCL(h)
    global ged_handle;ged_handle=h;

    TCL_SetVar("Xaxes_visibleToggle",h.axes_visible(1))
    TCL_SetVar("Yaxes_visibleToggle",h.axes_visible(2))
    TCL_SetVar("Zaxes_visibleToggle",h.axes_visible(3))

    ticks = h.x_ticks;
    sizeticks = size(ticks.locations,1);
    if (sizeticks <> 0)
        TCL_EvalStr("unset LOCATIONS_X");
        TCL_EvalStr("unset LABELS_X");
    end

    ticks = h.y_ticks;
    sizeticks = size(ticks.locations,1);
    if (sizeticks <> 0)
        TCL_EvalStr("unset LOCATIONS_Y");
        TCL_EvalStr("unset LABELS_Y");
    end

    ticks = h.z_ticks;
    sizeticks = size(ticks.locations,1);
    if (sizeticks <> 0)
        TCL_EvalStr("unset LOCATIONS_Z");
        TCL_EvalStr("unset LABELS_Z");
    end

    TCL_SetVar("SubticksEntryX",string(h.sub_ticks(1)))
    //  TCL_GetVar("SubticksEntryX")

    // disp("h.sub_ticks(1) =")
    // disp(h.sub_ticks(1));

    TCL_SetVar("SubticksEntryY",string(h.sub_ticks(2)))

    select h.view
    case "2d"
        drawlater();
        h.view="3d"
        TCL_SetVar("SubticksEntryZ",string(h.sub_ticks(3)))
        h.view="2d"
        drawnow();
    case "3d"
        TCL_SetVar("SubticksEntryZ",string(h.sub_ticks(3)))
    end

    TCL_SetVar("XautoticksToggle",h.auto_ticks(1))
    TCL_SetVar("YautoticksToggle",h.auto_ticks(2))
    TCL_SetVar("ZautoticksToggle",h.auto_ticks(3))

    //ticks value: X axis
    ticks = h.x_ticks;

    sizeticks = size(ticks.locations,1);
    TCL_SetVar("nbticks_x",string(sizeticks));
    for i=1:sizeticks
        val= "LOCATIONS_X("+string(i)+")";
        TCL_EvalStr("set "+val+" "+string(ticks.locations(i)));
        //      disp("i vaut:");
        //      disp(i);
        val= "LABELS_X("+string(i)+")";
        TCL_EvalStr("set "+val+" {"+ticks.labels(i)+"}");
    end

    //ticks value: Y axis
    ticks = h.y_ticks;
    sizeticks = size(ticks.locations,1);
    TCL_SetVar("nbticks_y",string(sizeticks));
    for i=1:sizeticks
        val= "LOCATIONS_Y("+string(i)+")";
        TCL_EvalStr("set "+val+" "+string(ticks.locations(i)));
        val= "LABELS_Y("+string(i)+")";
        TCL_EvalStr("set "+val+" {"+ticks.labels(i)+"}");
    end

    //ticks value: Z axis
    ticks = h.z_ticks;
    sizeticks = size(ticks.locations,1);
    TCL_SetVar("nbticks_z",string(sizeticks));
    for i=1:sizeticks
        val= "LOCATIONS_Z("+string(i)+")";
        TCL_EvalStr("set "+val+" "+string(ticks.locations(i)));
        val= "LABELS_Z("+string(i)+")";
        TCL_EvalStr("set "+val+" {"+ticks.labels(i)+"}");
    end

endfunction



function DestroyGlobals()
    global ged_current_figure

    if ~isempty(winsid()) & ~isempty(find(ged_current_figure==winsid())) then
        scf(ged_current_figure)
    end

    // ged is closed
    TCL_UnsetVar("sciGedIsAlive");

    clearglobal ged_current_figure
    clear ged_current_figure

    // disp("PASSE PAR DestroyGlobals Scilab");
endfunction


function ged_delete_entity()
    [btn,xc,yc]=xclick()
    [xc,yc]=xchange(xc,yc,"f2i")
    h=ged_getobject([xc,yc])
    if h<>[] then delete(h),end
endfunction

function ged_move_entity()
    [btn,xc,yc]=xclick()
    pos=[xc,yc]
    [xc,yc]=xchange(xc,yc,"f2i")
    [r,ax]=ged_getobject([xc,yc])
    cur_ax=gca(),sca(ax)
    [xc,yc]=xchange(xc,yc,"i2f");pos=[xc,yc]
    if r==[] return,end
    drawlater();
    rep(3)=-1
    select r.type
    case "Rectangle" then
        while rep(3)==-1 do
            rep=xgetmouse([%t %t])
            r.data(1:2)= r.data(1:2)+(rep(1:2)-pos)
            pos=rep(1:2)
            drawnow();
        end
    case "Segs" then //Segment
        while rep(3)==-1 do
            rep=xgetmouse([%t %t])
            r.data=r.data+ones(2,1)*(rep(1:2)-pos)
            pos=rep(1:2)
            drawnow();
        end
    case "Polyline" then //Polyline
        while rep(3)==-1 do
            rep=xgetmouse([%t %t])
            r.data(:,1:2)=r.data(:,1:2)+ones(r.data(:,1))*(rep(1:2)-pos)
            pos=rep(1:2)
            drawnow();
        end
    case "Arc" then //Circle
        while rep(3)==-1 do
            rep=xgetmouse([%t %t])
            r.data(1:2)= r.data(1:2)+(rep(1:2)-pos)
            pos=rep(1:2)
            drawnow();
        end
    case "Text" then
        while rep(3)==-1 do
            rep=xgetmouse([%t %t])
            r.data(1:2)= r.data(1:2)+(rep(1:2)-pos)
            pos=rep(1:2)
            drawnow();
        end
    case "Label" then
        while rep(3)==-1 do
            rep=xgetmouse([%t %t])
            r.position= r.position+(rep(1:2)-pos)
            r.auto_position = "off"
            pos=rep(1:2)
            drawnow();
        end

    end
    sca(cur_ax)
endfunction

function ged_copy_entity()
    [btn,xc,yc]=xclick()
    [xc,yc]=xchange(xc,yc,"f2i")
    r=ged_getobject([xc,yc])
    if r==[] return,end
    twinkle(r,1);
    save(TMPDIR+"/G_Clipboard",r)
    //make the axes containning the clicked point the current one
    sca(ged_select_axes(xc,yc))
endfunction


function ged_paste_entity()

    // check the file
    [info,err] = fileinfo(TMPDIR + "/G_Clipboard" ) ;

    if err <> 0 then
        return ;
    end

    // create the saved object
    load(TMPDIR+"/G_Clipboard") ;

    //  a=gca();b=a.data_bounds;
    //  move(r,[-1 1]*a.data_bounds/20)
endfunction

function axes =  ged_select_axes(x,y)
    // x and y are pixel coord.
    f=gcf()
    nb_axes = size(f.children,"*") // for now Iconsider that children of a figure are of type Axes
    axes_size = f.axes_size // given in pixels
    axes_size = [axes_size axes_size];

    for i=1:nb_axes
        axes = f.children(i);
        cur_axes_bounds = axes.axes_bounds;
        rect = cur_axes_bounds.*axes_size; // rectangle in pixels (margins inside)

        rect(3) = rect(3) + rect(1);
        rect(4) = rect(4) + rect(2);
        if (x>rect(1) & x<rect(3) & y>rect(2) & y<rect(4)) then
            return
        end
    end
    axes=[]
endfunction