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
|
/*
* tkMacOSXDialog.c --
*
* Contains the Mac implementation of the common dialog boxes.
*
* Copyright (c) 1996-1997 Sun Microsystems, Inc.
* Copyright 2001-2009, Apple Inc.
* Copyright (c) 2006-2009 Daniel A. Steffen <das@users.sourceforge.net>
* Copyright (c) 2017 Christian Gollwitzer.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tkMacOSXPrivate.h"
#include "tkFileFilter.h"
#include "tkMacOSXConstants.h"
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
#define modalOK NSOKButton
#define modalCancel NSCancelButton
#else
#define modalOK NSModalResponseOK
#define modalCancel NSModalResponseCancel
#endif // MAC_OS_X_VERSION_MIN_REQUIRED < 1090
#define modalOther -1
#define modalError -2
/*
* Vars for filtering in "open file" and "save file" dialogs.
*/
typedef struct {
bool doFileTypes; /* Show the accessory view which
* displays the filter menu */
bool preselectFilter; /* A filter was selected by the
* typevariable. */
bool userHasSelectedFilter; /* The user has changed the filter in
* the accessory view. */
NSMutableArray *fileTypeNames; /* Array of names, e.g. "Text
* document". */
NSMutableArray *fileTypeExtensions; /* Array of allowed extensions per
* name, e.g. "txt", "doc". */
NSMutableArray *fileTypeLabels; /* Displayed string, e.g. "Text
* document (.txt, .doc)". */
NSMutableArray *fileTypeAllowsAll; /* Boolean if the all pattern (*.*) is
* included. */
NSMutableArray *allowedExtensions; /* Set of all allowed extensions. */
bool allowedExtensionsAllowAll; /* Set of all allowed extensions
* includes *.* */
NSUInteger fileTypeIndex; /* Index of currently selected
* filter. */
} filepanelFilterInfo;
static filepanelFilterInfo filterInfo;
static NSOpenPanel *openpanel;
static NSSavePanel *savepanel;
static const char *const colorOptionStrings[] = {
"-initialcolor", "-parent", "-title", NULL
};
enum colorOptions {
COLOR_INITIAL, COLOR_PARENT, COLOR_TITLE
};
static const char *const openOptionStrings[] = {
"-defaultextension", "-filetypes", "-initialdir", "-initialfile",
"-message", "-multiple", "-parent", "-title", "-typevariable",
"-command", NULL
};
enum openOptions {
OPEN_DEFAULT, OPEN_FILETYPES, OPEN_INITDIR, OPEN_INITFILE,
OPEN_MESSAGE, OPEN_MULTIPLE, OPEN_PARENT, OPEN_TITLE,
OPEN_TYPEVARIABLE, OPEN_COMMAND,
};
static const char *const saveOptionStrings[] = {
"-defaultextension", "-filetypes", "-initialdir", "-initialfile",
"-message", "-parent", "-title", "-typevariable", "-command",
"-confirmoverwrite", NULL
};
enum saveOptions {
SAVE_DEFAULT, SAVE_FILETYPES, SAVE_INITDIR, SAVE_INITFILE,
SAVE_MESSAGE, SAVE_PARENT, SAVE_TITLE, SAVE_TYPEVARIABLE, SAVE_COMMAND,
SAVE_CONFIRMOW
};
static const char *const chooseOptionStrings[] = {
"-initialdir", "-message", "-mustexist", "-parent", "-title", "-command",
NULL
};
enum chooseOptions {
CHOOSE_INITDIR, CHOOSE_MESSAGE, CHOOSE_MUSTEXIST, CHOOSE_PARENT,
CHOOSE_TITLE, CHOOSE_COMMAND,
};
typedef struct {
Tcl_Interp *interp;
Tcl_Obj *cmdObj;
int multiple;
} FilePanelCallbackInfo;
static const char *const alertOptionStrings[] = {
"-default", "-detail", "-icon", "-message", "-parent", "-title",
"-type", "-command", NULL
};
enum alertOptions {
ALERT_DEFAULT, ALERT_DETAIL, ALERT_ICON, ALERT_MESSAGE, ALERT_PARENT,
ALERT_TITLE, ALERT_TYPE, ALERT_COMMAND,
};
typedef struct {
Tcl_Interp *interp;
Tcl_Obj *cmdObj;
int typeIndex;
} AlertCallbackInfo;
static const char *const alertTypeStrings[] = {
"abortretryignore", "ok", "okcancel", "retrycancel", "yesno",
"yesnocancel", NULL
};
enum alertTypeOptions {
TYPE_ABORTRETRYIGNORE, TYPE_OK, TYPE_OKCANCEL, TYPE_RETRYCANCEL,
TYPE_YESNO, TYPE_YESNOCANCEL
};
static const char *const alertIconStrings[] = {
"error", "info", "question", "warning", NULL
};
enum alertIconOptions {
ICON_ERROR, ICON_INFO, ICON_QUESTION, ICON_WARNING
};
static const char *const alertButtonStrings[] = {
"abort", "retry", "ignore", "ok", "cancel", "no", "yes", NULL
};
static const NSString *const alertButtonNames[][3] = {
[TYPE_ABORTRETRYIGNORE] = {@"Abort", @"Retry", @"Ignore"},
[TYPE_OK] = {@"OK"},
[TYPE_OKCANCEL] = {@"OK", @"Cancel"},
[TYPE_RETRYCANCEL] = {@"Retry", @"Cancel"},
[TYPE_YESNO] = {@"Yes", @"No"},
[TYPE_YESNOCANCEL] = {@"Yes", @"No", @"Cancel"},
};
static const NSAlertStyle alertStyles[] = {
[ICON_ERROR] = NSWarningAlertStyle,
[ICON_INFO] = NSInformationalAlertStyle,
[ICON_QUESTION] = NSWarningAlertStyle,
[ICON_WARNING] = NSCriticalAlertStyle,
};
/*
* Need to map from 'alertButtonStrings' and its corresponding integer, index
* to the native button index, which is 1, 2, 3, from right to left. This is
* necessary to do for each separate '-type' of button sets.
*/
static const short alertButtonIndexAndTypeToNativeButtonIndex[][7] = {
/* abort retry ignore ok cancel yes no */
[TYPE_ABORTRETRYIGNORE] = {1, 2, 3, 0, 0, 0, 0},
[TYPE_OK] = {0, 0, 0, 1, 0, 0, 0},
[TYPE_OKCANCEL] = {0, 0, 0, 1, 2, 0, 0},
[TYPE_RETRYCANCEL] = {0, 1, 0, 0, 2, 0, 0},
[TYPE_YESNO] = {0, 0, 0, 0, 0, 2, 1},
[TYPE_YESNOCANCEL] = {0, 0, 0, 0, 3, 2, 1},
};
/*
* Need also the inverse mapping, from NSAlertFirstButtonReturn etc to the
* descriptive button text string index.
*/
static const short alertNativeButtonIndexAndTypeToButtonIndex[][3] = {
[TYPE_ABORTRETRYIGNORE] = {0, 1, 2},
[TYPE_OK] = {3, 0, 0},
[TYPE_OKCANCEL] = {3, 4, 0},
[TYPE_RETRYCANCEL] = {1, 4, 0},
[TYPE_YESNO] = {6, 5, 0},
[TYPE_YESNOCANCEL] = {6, 5, 4},
};
/*
* Construct a file URL from directory and filename. Either may be nil. If both
* are nil, returns nil.
*/
static NSURL *
getFileURL(
NSString *directory,
NSString *filename)
{
NSURL *url = nil;
if (directory) {
url = [NSURL fileURLWithPath:directory isDirectory:YES];
}
if (filename) {
url = [NSURL URLWithString:filename relativeToURL:url];
}
return url;
}
#pragma mark TKApplication(TKDialog)
@implementation TKApplication(TKDialog)
- (BOOL)panel:(id)sender shouldEnableURL:(NSURL *)url {
(void)sender;
(void)url;
return YES;
}
- (void)panel:(id)sender didChangeToDirectoryURL:(NSURL *)url {
(void)sender; (void)url;
}
- (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError **)outError {
(void)sender; (void)url;
*outError = nil;
return YES;
}
- (void) tkFilePanelDidEnd: (NSSavePanel *) panel
returnCode: (NSModalResponse) returnCode
contextInfo: (void *) contextInfo
{
FilePanelCallbackInfo *callbackInfo = contextInfo;
if (returnCode == modalOK) {
Tcl_Obj *resultObj;
if (callbackInfo->multiple) {
resultObj = Tcl_NewListObj(0, NULL);
for (NSURL *url in [(NSOpenPanel*)panel URLs]) {
Tcl_ListObjAppendElement(callbackInfo->interp, resultObj,
Tcl_NewStringObj([[url path] UTF8String], -1));
}
} else {
resultObj = Tcl_NewStringObj([[[panel URL]path] UTF8String], -1);
}
if (callbackInfo->cmdObj) {
Tcl_Obj **objv, **tmpv;
int objc, result = Tcl_ListObjGetElements(callbackInfo->interp,
callbackInfo->cmdObj, &objc, &objv);
if (result == TCL_OK && objc) {
tmpv = (Tcl_Obj **)ckalloc(sizeof(Tcl_Obj *) * (objc + 2));
memcpy(tmpv, objv, sizeof(Tcl_Obj *) * objc);
tmpv[objc] = resultObj;
TkBackgroundEvalObjv(callbackInfo->interp, objc + 1, tmpv,
TCL_EVAL_GLOBAL);
ckfree(tmpv);
}
} else {
Tcl_SetObjResult(callbackInfo->interp, resultObj);
}
} else if (returnCode == modalCancel) {
Tcl_ResetResult(callbackInfo->interp);
}
if (panel == [NSApp modalWindow]) {
[NSApp stopModalWithCode:returnCode];
}
if (callbackInfo->cmdObj) {
Tcl_DecrRefCount(callbackInfo->cmdObj);
ckfree(callbackInfo);
}
}
- (void) tkAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode
contextInfo: (void *) contextInfo
{
AlertCallbackInfo *callbackInfo = contextInfo;
if (returnCode >= NSAlertFirstButtonReturn) {
Tcl_Obj *resultObj = Tcl_NewStringObj(alertButtonStrings[
alertNativeButtonIndexAndTypeToButtonIndex[callbackInfo->
typeIndex][returnCode - NSAlertFirstButtonReturn]], -1);
if (callbackInfo->cmdObj) {
Tcl_Obj **objv, **tmpv;
int objc, result = Tcl_ListObjGetElements(callbackInfo->interp,
callbackInfo->cmdObj, &objc, &objv);
if (result == TCL_OK && objc) {
tmpv = (Tcl_Obj **)ckalloc(sizeof(Tcl_Obj *) * (objc + 2));
memcpy(tmpv, objv, sizeof(Tcl_Obj *) * objc);
tmpv[objc] = resultObj;
TkBackgroundEvalObjv(callbackInfo->interp, objc + 1, tmpv,
TCL_EVAL_GLOBAL);
ckfree(tmpv);
}
} else {
Tcl_SetObjResult(callbackInfo->interp, resultObj);
}
}
if ([alert window] == [NSApp modalWindow]) {
[NSApp stopModalWithCode:returnCode];
}
if (callbackInfo->cmdObj) {
Tcl_DecrRefCount(callbackInfo->cmdObj);
ckfree(callbackInfo);
}
}
- (void)selectFormat:(id)sender {
NSPopUpButton *button = (NSPopUpButton *)sender;
filterInfo.fileTypeIndex = [button indexOfSelectedItem];
if ([[filterInfo.fileTypeAllowsAll objectAtIndex:filterInfo.fileTypeIndex] boolValue]) {
[openpanel setAllowsOtherFileTypes:YES];
/*
* setAllowsOtherFileTypes might have no effect; it's inherited from
* the NSSavePanel, where it has the effect that it does not append an
* extension. Setting the allowed file types to nil allows selecting
* any file.
*/
[openpanel setAllowedFileTypes:nil];
} else {
NSMutableArray *allowedtypes =
[filterInfo.fileTypeExtensions objectAtIndex:filterInfo.fileTypeIndex];
[openpanel setAllowedFileTypes:allowedtypes];
[openpanel setAllowsOtherFileTypes:NO];
}
filterInfo.userHasSelectedFilter = true;
}
- (void)saveFormat:(id)sender {
NSPopUpButton *button = (NSPopUpButton *)sender;
filterInfo.fileTypeIndex = [button indexOfSelectedItem];
if ([[filterInfo.fileTypeAllowsAll objectAtIndex:filterInfo.fileTypeIndex] boolValue]) {
[savepanel setAllowsOtherFileTypes:YES];
[savepanel setAllowedFileTypes:nil];
} else {
NSMutableArray *allowedtypes =
[filterInfo.fileTypeExtensions objectAtIndex:filterInfo.fileTypeIndex];
[savepanel setAllowedFileTypes:allowedtypes];
[savepanel setAllowsOtherFileTypes:NO];
}
filterInfo.userHasSelectedFilter = true;
}
@end
#pragma mark -
static NSInteger showOpenSavePanel(
NSSavePanel *panel,
NSWindow *parent,
FilePanelCallbackInfo *callbackInfo)
{
NSInteger modalReturnCode;
if (parent && ![parent attachedSheet]) {
[panel beginSheetModalForWindow:parent
completionHandler:^(NSModalResponse returnCode) {
[NSApp tkFilePanelDidEnd:panel
returnCode:returnCode
contextInfo:callbackInfo ];
}];
modalReturnCode = callbackInfo->cmdObj ? modalOther :
[panel runModal];
} else {
modalReturnCode = [panel runModal];
[NSApp tkFilePanelDidEnd:panel returnCode:modalReturnCode
contextInfo:callbackInfo];
}
return modalReturnCode;
}
/*
*----------------------------------------------------------------------
*
* Tk_ChooseColorObjCmd --
*
* This procedure implements the color dialog box for the Mac platform.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*----------------------------------------------------------------------
*/
int
Tk_ChooseColorObjCmd(
ClientData clientData, /* Main window associated with interpreter. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
int result = TCL_ERROR;
Tk_Window parent, tkwin = clientData;
const char *title = NULL;
int i;
NSColor *color = nil, *initialColor = nil;
NSColorPanel *colorPanel;
NSInteger returnCode, numberOfComponents = 0;
for (i = 1; i < objc; i += 2) {
int index;
const char *value;
if (Tcl_GetIndexFromObjStruct(interp, objv[i], colorOptionStrings,
sizeof(char *), "option", TCL_EXACT, &index) != TCL_OK) {
goto end;
}
if (i + 1 == objc) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"value for \"%s\" missing", Tcl_GetString(objv[i])));
Tcl_SetErrorCode(interp, "TK", "COLORDIALOG", "VALUE", NULL);
goto end;
}
value = Tcl_GetString(objv[i + 1]);
switch (index) {
case COLOR_INITIAL: {
XColor *colorPtr;
colorPtr = Tk_GetColor(interp, tkwin, value);
if (colorPtr == NULL) {
goto end;
}
initialColor = TkMacOSXGetNSColor(NULL, colorPtr->pixel);
Tk_FreeColor(colorPtr);
break;
}
case COLOR_PARENT:
parent = Tk_NameToWindow(interp, value, tkwin);
if (parent == NULL) {
goto end;
}
break;
case COLOR_TITLE:
title = value;
break;
}
}
colorPanel = [NSColorPanel sharedColorPanel];
[colorPanel orderOut:NSApp];
[colorPanel setContinuous:NO];
[colorPanel setBecomesKeyOnlyIfNeeded:NO];
[colorPanel setShowsAlpha: NO];
[colorPanel _setUseModalAppearance:YES];
if (title) {
NSString *s = [[NSString alloc] initWithUTF8String:title];
[colorPanel setTitle:s];
[s release];
}
if (initialColor) {
[colorPanel setColor:initialColor];
}
returnCode = [NSApp runModalForWindow:colorPanel];
if (returnCode == modalOK) {
color = [[colorPanel color] colorUsingColorSpace:
[NSColorSpace deviceRGBColorSpace]];
numberOfComponents = [color numberOfComponents];
}
if (color && numberOfComponents >= 3 && numberOfComponents <= 4) {
CGFloat components[4];
char colorstr[8];
[color getComponents:components];
snprintf(colorstr, 8, "#%02x%02x%02x",
(short)(components[0] * 255),
(short)(components[1] * 255),
(short)(components[2] * 255));
Tcl_SetObjResult(interp, Tcl_NewStringObj(colorstr, 7));
} else {
Tcl_ResetResult(interp);
}
result = TCL_OK;
end:
return result;
}
/*
* Dissect the -filetype nested lists and store the information in the
* filterInfo structure.
*/
static int
parseFileFilters(
Tcl_Interp *interp,
Tcl_Obj *fileTypesPtr,
Tcl_Obj *typeVariablePtr)
{
if (!fileTypesPtr) {
filterInfo.doFileTypes = false;
return TCL_OK;
}
FileFilterList fl;
TkInitFileFilters(&fl);
if (TkGetFileFilters(interp, &fl, fileTypesPtr, 0) != TCL_OK) {
TkFreeFileFilters(&fl);
return TCL_ERROR;
}
filterInfo.doFileTypes = (fl.filters != NULL);
filterInfo.fileTypeIndex = 0;
filterInfo.fileTypeExtensions = [NSMutableArray array];
filterInfo.fileTypeNames = [NSMutableArray array];
filterInfo.fileTypeLabels = [NSMutableArray array];
filterInfo.fileTypeAllowsAll = [NSMutableArray array];
filterInfo.allowedExtensions = [NSMutableArray array];
filterInfo.allowedExtensionsAllowAll = NO;
if (filterInfo.doFileTypes) {
for (FileFilter *filterPtr = fl.filters; filterPtr;
filterPtr = filterPtr->next) {
NSString *name = [[NSString alloc] initWithUTF8String: filterPtr->name];
[filterInfo.fileTypeNames addObject:name];
[name release];
NSMutableArray *clauseextensions = [NSMutableArray array];
NSMutableArray *displayextensions = [NSMutableArray array];
bool allowsAll = NO;
for (FileFilterClause *clausePtr = filterPtr->clauses; clausePtr;
clausePtr = clausePtr->next) {
for (GlobPattern *globPtr = clausePtr->patterns; globPtr;
globPtr = globPtr->next) {
const char *str = globPtr->pattern;
while (*str && (*str == '*' || *str == '.')) {
str++;
}
if (*str) {
NSString *extension = [[NSString alloc] initWithUTF8String:str];
if (![filterInfo.allowedExtensions containsObject:extension]) {
[filterInfo.allowedExtensions addObject:extension];
}
[clauseextensions addObject:extension];
[displayextensions addObject:[@"." stringByAppendingString:extension]];
[extension release];
} else {
/*
* It is the all pattern (*, .* or *.*)
*/
allowsAll = YES;
filterInfo.allowedExtensionsAllowAll = YES;
[displayextensions addObject:@"*"];
}
}
}
[filterInfo.fileTypeExtensions addObject:clauseextensions];
[filterInfo.fileTypeAllowsAll addObject:[NSNumber numberWithBool:allowsAll]];
NSMutableString *label = [[NSMutableString alloc] initWithString:name];
[label appendString:@" ("];
[label appendString:[displayextensions componentsJoinedByString:@", "]];
[label appendString:@")"];
[filterInfo.fileTypeLabels addObject:label];
[label release];
}
/*
* Check if the typevariable exists and matches one of the names.
*/
filterInfo.preselectFilter = false;
filterInfo.userHasSelectedFilter = false;
if (typeVariablePtr) {
/*
* Extract the variable content as a NSString.
*/
Tcl_Obj *selectedFileTypeObj = Tcl_ObjGetVar2(interp,
typeVariablePtr, NULL, TCL_GLOBAL_ONLY);
/*
* Check that the typevariable exists.
*/
if (selectedFileTypeObj != NULL) {
const char *selectedFileType =
Tcl_GetString(selectedFileTypeObj);
NSString *selectedFileTypeStr =
[[NSString alloc] initWithUTF8String:selectedFileType];
NSUInteger index =
[filterInfo.fileTypeNames indexOfObject:selectedFileTypeStr];
if (index != NSNotFound) {
filterInfo.fileTypeIndex = index;
filterInfo.preselectFilter = true;
}
}
}
}
TkFreeFileFilters(&fl);
return TCL_OK;
}
static bool
filterCompatible(
NSString *extension,
int filterIndex)
{
NSMutableArray *allowedExtensions =
[filterInfo.fileTypeExtensions objectAtIndex: filterIndex];
/*
* If this contains the all pattern, accept any extension.
*/
if ([[filterInfo.fileTypeAllowsAll objectAtIndex:filterIndex] boolValue]) {
return true;
}
return [allowedExtensions containsObject: extension];
}
/*
*----------------------------------------------------------------------
*
* Tk_GetOpenFileObjCmd --
*
* This procedure implements the "open file" dialog box for the Mac
* platform. See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See user documentation.
*----------------------------------------------------------------------
*/
int
Tk_GetOpenFileObjCmd(
ClientData clientData, /* Main window associated with interpreter. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tk_Window tkwin = clientData;
char *str;
int i, result = TCL_ERROR, haveParentOption = 0;
int index, len, multiple = 0;
Tcl_Obj *cmdObj = NULL, *typeVariablePtr = NULL, *fileTypesPtr = NULL;
FilePanelCallbackInfo callbackInfoStruct;
FilePanelCallbackInfo *callbackInfo = &callbackInfoStruct;
NSString *directory = nil, *filename = nil;
NSString *message = nil, *title = nil;
NSWindow *parent;
openpanel = [NSOpenPanel openPanel];
NSInteger modalReturnCode = modalError;
BOOL parentIsKey = NO;
for (i = 1; i < objc; i += 2) {
if (Tcl_GetIndexFromObjStruct(interp, objv[i], openOptionStrings,
sizeof(char *), "option", TCL_EXACT, &index) != TCL_OK) {
goto end;
}
if (i + 1 == objc) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"value for \"%s\" missing", Tcl_GetString(objv[i])));
Tcl_SetErrorCode(interp, "TK", "FILEDIALOG", "VALUE", NULL);
goto end;
}
switch (index) {
case OPEN_DEFAULT:
break;
case OPEN_FILETYPES:
fileTypesPtr = objv[i + 1];
break;
case OPEN_INITDIR:
str = Tcl_GetStringFromObj(objv[i + 1], &len);
if (len) {
directory = [[[NSString alloc] initWithUTF8String:str]
autorelease];
}
break;
case OPEN_INITFILE:
str = Tcl_GetStringFromObj(objv[i + 1], &len);
if (len) {
filename = [[[NSString alloc] initWithUTF8String:str]
autorelease];
}
break;
case OPEN_MESSAGE:
message = [[NSString alloc] initWithUTF8String:
Tcl_GetString(objv[i + 1])];
break;
case OPEN_MULTIPLE:
if (Tcl_GetBooleanFromObj(interp, objv[i + 1],
&multiple) != TCL_OK) {
goto end;
}
break;
case OPEN_PARENT:
str = Tcl_GetStringFromObj(objv[i + 1], &len);
tkwin = Tk_NameToWindow(interp, str, tkwin);
if (!tkwin) {
goto end;
}
haveParentOption = 1;
break;
case OPEN_TITLE:
title = [[NSString alloc] initWithUTF8String:
Tcl_GetString(objv[i + 1])];
break;
case OPEN_TYPEVARIABLE:
typeVariablePtr = objv[i + 1];
break;
case OPEN_COMMAND:
cmdObj = objv[i+1];
break;
}
}
if (title) {
[openpanel setTitle:title];
/*
* From OSX 10.11, the title string is silently ignored in the open
* panel. Prepend the title to the message in this case.
*/
if ([NSApp macOSVersion] > 101000) {
if (message) {
NSString *fullmessage =
[[NSString alloc] initWithFormat:@"%@\n%@", title, message];
[message release];
[title release];
message = fullmessage;
} else {
message = title;
}
}
}
if (message) {
[openpanel setMessage:message];
[message release];
}
[openpanel setAllowsMultipleSelection:multiple];
if (parseFileFilters(interp, fileTypesPtr, typeVariablePtr) != TCL_OK) {
goto end;
}
if (filterInfo.doFileTypes) {
NSTextField *label = [[NSTextField alloc]
initWithFrame:NSMakeRect(0, 0, 60, 22)];
NSPopUpButton *popupButton = [[NSPopUpButton alloc]
initWithFrame:NSMakeRect(50.0, 2, 240, 22.0) pullsDown:NO];
NSView *accessoryView = [[NSView alloc]
initWithFrame:NSMakeRect(0.0, 0.0, 300, 32.0)];
[label setEditable:NO];
[label setStringValue:@"Filter:"];
[label setBordered:NO];
[label setBezeled:NO];
[label setDrawsBackground:NO];
[popupButton addItemsWithTitles:filterInfo.fileTypeLabels];
[popupButton setAction:@selector(selectFormat:)];
[accessoryView addSubview:label];
[accessoryView addSubview:popupButton];
if (filterInfo.preselectFilter) {
/*
* A specific filter was selected from the typevariable. Select it
* and open the accessory view.
*/
[popupButton selectItemAtIndex:filterInfo.fileTypeIndex];
/*
* On OSX > 10.11, the options are not visible by default. Ergo
* allow all file types
[openpanel setAllowedFileTypes:filterInfo.fileTypeExtensions[filterInfo.fileTypeIndex]];
*/
[openpanel setAllowedFileTypes:filterInfo.allowedExtensions];
} else {
[openpanel setAllowedFileTypes:filterInfo.allowedExtensions];
}
if (filterInfo.allowedExtensionsAllowAll) {
[openpanel setAllowsOtherFileTypes:YES];
} else {
[openpanel setAllowsOtherFileTypes:NO];
}
[openpanel setAccessoryView:accessoryView];
} else {
/*
* No filters are given. Allow picking all files.
*/
[openpanel setAllowsOtherFileTypes:YES];
}
if (cmdObj) {
if (Tcl_IsShared(cmdObj)) {
cmdObj = Tcl_DuplicateObj(cmdObj);
}
Tcl_IncrRefCount(cmdObj);
}
callbackInfo = (FilePanelCallbackInfo *)ckalloc(sizeof(FilePanelCallbackInfo));
callbackInfo->cmdObj = cmdObj;
callbackInfo->interp = interp;
callbackInfo->multiple = multiple;
if (directory || filename) {
NSURL *fileURL = getFileURL(directory, filename);
[openpanel setDirectoryURL:fileURL];
}
if (haveParentOption) {
parent = TkMacOSXDrawableWindow(((TkWindow *) tkwin)->window);
parentIsKey = parent && [parent isKeyWindow];
} else {
parent = nil;
parentIsKey = False;
}
modalReturnCode = showOpenSavePanel(openpanel, parent, callbackInfo);
result = (modalReturnCode != modalError) ? TCL_OK : TCL_ERROR;
if (parentIsKey) {
[parent makeKeyWindow];
}
if ((typeVariablePtr && (modalReturnCode == NSOKButton))
&& filterInfo.doFileTypes) {
/*
* The -typevariable must be set to the selected file type, if the
* dialog was not cancelled.
*/
NSUInteger selectedFilterIndex = filterInfo.fileTypeIndex;
NSString *selectedFilter = NULL;
if (filterInfo.userHasSelectedFilter) {
selectedFilterIndex = filterInfo.fileTypeIndex;
selectedFilter = [filterInfo.fileTypeNames objectAtIndex:selectedFilterIndex];
} else {
/*
* Difficult case: the user has not touched the filter settings,
* but we must return something in the typevariable. First check if
* the preselected type is compatible with the selected file,
* otherwise choose the first compatible type from the list,
* finally fall back to the empty string.
*/
NSURL *selectedFile;
NSString *extension;
if (multiple) {
/*
* Use the first file in the case of multiple selection.
* Anyway it is not overly useful here.
*/
selectedFile = [[openpanel URLs] objectAtIndex:0];
} else {
selectedFile = [openpanel URL];
}
extension = [selectedFile pathExtension];
if (filterInfo.preselectFilter &&
filterCompatible(extension, filterInfo.fileTypeIndex)) {
selectedFilterIndex = filterInfo.fileTypeIndex; // The preselection from the typevariable
selectedFilter = [filterInfo.fileTypeNames objectAtIndex:selectedFilterIndex];
} else {
NSUInteger i;
for (i = 0; i < [filterInfo.fileTypeNames count]; i++) {
if (filterCompatible(extension, i)) {
selectedFilterIndex = i;
break;
}
}
if (i == selectedFilterIndex) {
selectedFilter = [filterInfo.fileTypeNames objectAtIndex:selectedFilterIndex];
} else {
selectedFilter = @"";
}
}
}
Tcl_ObjSetVar2(interp, typeVariablePtr, NULL,
Tcl_NewStringObj([selectedFilter UTF8String], -1),
TCL_GLOBAL_ONLY);
}
end:
return result;
}
/*
*----------------------------------------------------------------------
*
* Tk_GetSaveFileObjCmd --
*
* This procedure implements the "save file" dialog box for the Mac
* platform. See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See user documentation.
*
*----------------------------------------------------------------------
*/
int
Tk_GetSaveFileObjCmd(
ClientData clientData, /* Main window associated with interpreter. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tk_Window tkwin = clientData;
char *str;
int i, result = TCL_ERROR, haveParentOption = 0;
int confirmOverwrite = 1;
int index, len;
Tcl_Obj *cmdObj = NULL, *typeVariablePtr = NULL, *fileTypesPtr = NULL;
FilePanelCallbackInfo callbackInfoStruct;
FilePanelCallbackInfo *callbackInfo = &callbackInfoStruct;
NSString *directory = nil, *filename = nil, *defaultType = nil;
NSString *message = nil, *title = nil;
NSWindow *parent;
savepanel = [NSSavePanel savePanel];
NSInteger modalReturnCode = modalError;
BOOL parentIsKey = NO;
for (i = 1; i < objc; i += 2) {
if (Tcl_GetIndexFromObjStruct(interp, objv[i], saveOptionStrings,
sizeof(char *), "option", TCL_EXACT, &index) != TCL_OK) {
goto end;
}
if (i + 1 == objc) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"value for \"%s\" missing", Tcl_GetString(objv[i])));
Tcl_SetErrorCode(interp, "TK", "FILEDIALOG", "VALUE", NULL);
goto end;
}
switch (index) {
case SAVE_DEFAULT:
str = Tcl_GetStringFromObj(objv[i + 1], &len);
while (*str && (*str == '*' || *str == '.')) {
str++;
}
if (*str) {
defaultType = [[[NSString alloc] initWithUTF8String:str]
autorelease];
}
break;
case SAVE_FILETYPES:
fileTypesPtr = objv[i + 1];
break;
case SAVE_INITDIR:
str = Tcl_GetStringFromObj(objv[i + 1], &len);
if (len) {
directory = [[[NSString alloc] initWithUTF8String:str]
autorelease];
}
break;
case SAVE_INITFILE:
str = Tcl_GetStringFromObj(objv[i + 1], &len);
if (len) {
filename = [[[NSString alloc] initWithUTF8String:str]
autorelease];
[savepanel setNameFieldStringValue:filename];
}
break;
case SAVE_MESSAGE:
message = [[NSString alloc] initWithUTF8String:
Tcl_GetString(objv[i + 1])];
break;
case SAVE_PARENT:
str = Tcl_GetStringFromObj(objv[i + 1], &len);
tkwin = Tk_NameToWindow(interp, str, tkwin);
if (!tkwin) {
goto end;
}
haveParentOption = 1;
break;
case SAVE_TITLE:
title = [[NSString alloc] initWithUTF8String:
Tcl_GetString(objv[i + 1])];
break;
case SAVE_TYPEVARIABLE:
typeVariablePtr = objv[i + 1];
break;
case SAVE_COMMAND:
cmdObj = objv[i+1];
break;
case SAVE_CONFIRMOW:
if (Tcl_GetBooleanFromObj(interp, objv[i + 1],
&confirmOverwrite) != TCL_OK) {
goto end;
}
break;
}
}
if (title) {
[savepanel setTitle:title];
/*
* From OSX 10.11, the title string is silently ignored, if the save
* panel is a sheet. Prepend the title to the message in this case.
* NOTE: should be conditional on OSX version, but -mmacosx-version-min
* does not revert this behaviour.
*/
if (haveParentOption) {
if (message) {
NSString *fullmessage =
[[NSString alloc] initWithFormat:@"%@\n%@",title,message];
[message release];
[title release];
message = fullmessage;
} else {
message = title;
}
}
}
if (message) {
[savepanel setMessage:message];
[message release];
}
if (parseFileFilters(interp, fileTypesPtr, typeVariablePtr) != TCL_OK) {
goto end;
}
if (filterInfo.doFileTypes) {
NSView *accessoryView = [[NSView alloc]
initWithFrame:NSMakeRect(0.0, 0.0, 300, 32.0)];
NSTextField *label = [[NSTextField alloc]
initWithFrame:NSMakeRect(0, 0, 60, 22)];
[label setEditable:NO];
[label setStringValue:NSLocalizedString(@"Format:", nil)];
[label setBordered:NO];
[label setBezeled:NO];
[label setDrawsBackground:NO];
NSPopUpButton *popupButton = [[NSPopUpButton alloc]
initWithFrame:NSMakeRect(50.0, 2, 340, 22.0) pullsDown:NO];
[popupButton addItemsWithTitles:filterInfo.fileTypeLabels];
[popupButton selectItemAtIndex:filterInfo.fileTypeIndex];
[popupButton setAction:@selector(saveFormat:)];
[accessoryView addSubview:label];
[accessoryView addSubview:popupButton];
[savepanel setAccessoryView:accessoryView];
[savepanel setAllowedFileTypes:[filterInfo.fileTypeExtensions objectAtIndex:filterInfo.fileTypeIndex]];
[savepanel setAllowsOtherFileTypes:filterInfo.allowedExtensionsAllowAll];
} else if (defaultType) {
/*
* If no filetypes are given, defaultextension is an alternative way to
* specify the attached extension. Just propose this extension, but
* don't display an accessory view.
*/
NSMutableArray *AllowedFileTypes = [NSMutableArray array];
[AllowedFileTypes addObject:defaultType];
[savepanel setAllowedFileTypes:AllowedFileTypes];
[savepanel setAllowsOtherFileTypes:YES];
}
[savepanel setCanSelectHiddenExtension:YES];
[savepanel setExtensionHidden:NO];
if (cmdObj) {
if (Tcl_IsShared(cmdObj)) {
cmdObj = Tcl_DuplicateObj(cmdObj);
}
Tcl_IncrRefCount(cmdObj);
}
callbackInfo = (FilePanelCallbackInfo *)ckalloc(sizeof(FilePanelCallbackInfo));
callbackInfo->cmdObj = cmdObj;
callbackInfo->interp = interp;
callbackInfo->multiple = 0;
if (directory) {
[savepanel setDirectoryURL:[NSURL fileURLWithPath:directory isDirectory:YES]];
}
/*
* Check for file name and set to the empty string if nil. This prevents a crash
* with an uncaught exception.
*/
if (filename) {
[savepanel setNameFieldStringValue:filename];
} else {
[savepanel setNameFieldStringValue:@""];
}
if (haveParentOption) {
parent = TkMacOSXDrawableWindow(((TkWindow *) tkwin)->window);
parentIsKey = parent && [parent isKeyWindow];
} else {
parent = nil;
parentIsKey = False;
}
modalReturnCode = showOpenSavePanel(savepanel, parent, callbackInfo);
result = (modalReturnCode != modalError) ? TCL_OK : TCL_ERROR;
if (parentIsKey) {
[parent makeKeyWindow];
}
if (typeVariablePtr && (modalReturnCode == NSOKButton)
&& filterInfo.doFileTypes) {
/*
* The -typevariable must be set to the selected file type, if the
* dialog was not cancelled.
*/
NSString *selectedFilter =
[filterInfo.fileTypeNames objectAtIndex:filterInfo.fileTypeIndex];
Tcl_ObjSetVar2(interp, typeVariablePtr, NULL,
Tcl_NewStringObj([selectedFilter UTF8String], -1),
TCL_GLOBAL_ONLY);
}
end:
return result;
}
/*
*----------------------------------------------------------------------
*
* Tk_ChooseDirectoryObjCmd --
*
* This procedure implements the "tk_chooseDirectory" dialog box for the
* MacOS X platform. See the user documentation for details on what it
* does.
*
* Results:
* See user documentation.
*
* Side effects:
* A modal dialog window is created.
*
*----------------------------------------------------------------------
*/
int
Tk_ChooseDirectoryObjCmd(
ClientData clientData, /* Main window associated with interpreter. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tk_Window tkwin = clientData;
char *str;
int i, result = TCL_ERROR, haveParentOption = 0;
int index, len, mustexist = 0;
Tcl_Obj *cmdObj = NULL;
FilePanelCallbackInfo callbackInfoStruct;
FilePanelCallbackInfo *callbackInfo = &callbackInfoStruct;
NSString *directory = nil;
NSString *message, *title;
NSWindow *parent;
NSOpenPanel *panel = [NSOpenPanel openPanel];
NSInteger modalReturnCode = modalError;
BOOL parentIsKey = NO;
for (i = 1; i < objc; i += 2) {
if (Tcl_GetIndexFromObjStruct(interp, objv[i], chooseOptionStrings,
sizeof(char *), "option", TCL_EXACT, &index) != TCL_OK) {
goto end;
}
if (i + 1 == objc) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"value for \"%s\" missing", Tcl_GetString(objv[i])));
Tcl_SetErrorCode(interp, "TK", "DIRDIALOG", "VALUE", NULL);
goto end;
}
switch (index) {
case CHOOSE_INITDIR:
str = Tcl_GetStringFromObj(objv[i + 1], &len);
if (len) {
directory = [[[NSString alloc] initWithUTF8String:str]
autorelease];
}
break;
case CHOOSE_MESSAGE:
message = [[NSString alloc] initWithUTF8String:
Tcl_GetString(objv[i + 1])];
[panel setMessage:message];
[message release];
break;
case CHOOSE_MUSTEXIST:
if (Tcl_GetBooleanFromObj(interp, objv[i + 1],
&mustexist) != TCL_OK) {
goto end;
}
break;
case CHOOSE_PARENT:
str = Tcl_GetStringFromObj(objv[i + 1], &len);
tkwin = Tk_NameToWindow(interp, str, tkwin);
if (!tkwin) {
goto end;
}
haveParentOption = 1;
break;
case CHOOSE_TITLE:
title = [[NSString alloc] initWithUTF8String:
Tcl_GetString(objv[i + 1])];
[panel setTitle:title];
[title release];
break;
case CHOOSE_COMMAND:
cmdObj = objv[i+1];
break;
}
}
[panel setPrompt:@"Choose"];
[panel setCanChooseFiles:NO];
[panel setCanChooseDirectories:YES];
[panel setCanCreateDirectories:!mustexist];
if (cmdObj) {
if (Tcl_IsShared(cmdObj)) {
cmdObj = Tcl_DuplicateObj(cmdObj);
}
Tcl_IncrRefCount(cmdObj);
}
callbackInfo = (FilePanelCallbackInfo *)ckalloc(sizeof(FilePanelCallbackInfo));
callbackInfo->cmdObj = cmdObj;
callbackInfo->interp = interp;
callbackInfo->multiple = 0;
/*
* Check for directory value, set to root if not specified; otherwise
* crashes with exception because of nil string parameter.
*/
if (!directory) {
directory = @"/";
}
parent = TkMacOSXDrawableWindow(((TkWindow *) tkwin)->window);
[panel setDirectoryURL:[NSURL fileURLWithPath:directory isDirectory:YES]];
if (haveParentOption) {
parent = TkMacOSXDrawableWindow(((TkWindow *) tkwin)->window);
parentIsKey = parent && [parent isKeyWindow];
} else {
parent = nil;
parentIsKey = False;
}
modalReturnCode = showOpenSavePanel(panel, parent, callbackInfo);
result = (modalReturnCode != modalError) ? TCL_OK : TCL_ERROR;
if (parentIsKey) {
[parent makeKeyWindow];
}
end:
return result;
}
/*
*----------------------------------------------------------------------
*
* TkAboutDlg --
*
* Displays the default Tk About box.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
TkAboutDlg(void)
{
NSImage *image;
NSString *path = [NSApp tkFrameworkImagePath: @"Tk.tiff"];
if (path) {
image = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
} else {
image = [NSApp applicationIconImage];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:@"Y"];
NSString *year = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
/*
* This replaces the old about dialog with a standard alert that displays
* correctly on 10.14.
*/
NSString *version = @"Tcl " TCL_PATCH_LEVEL " & Tk " TCL_PATCH_LEVEL;
NSString *url = @"www.tcl-lang.org";
NSTextView *credits = [[NSTextView alloc] initWithFrame:NSMakeRect(0,0,300,300)];
NSFont *font = [NSFont systemFontOfSize:[NSFont systemFontSize]];
NSDictionary *textAttributes = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
[credits insertText: [[NSAttributedString alloc]
initWithString:[NSString stringWithFormat: @"\n"
"Tcl and Tk are distributed under a modified BSD license: "
"www.tcl.tk/software/tcltk/license.html\n\n"
"%1$C 1987-%2$@ Tcl Core Team and Contributers.\n\n"
"%1$C 2011-%2$@ Kevin Walzer/WordTech Communications LLC.\n\n"
"%1$C 2014-%2$@ Marc Culler.\n\n"
"%1$C 2002-2012 Daniel A. Steffen.\n\n"
"%1$C 2001-2009 Apple Inc.\n\n"
"%1$C 2001-2002 Jim Ingham & Ian Reid\n\n"
"%1$C 1998-2000 Jim Ingham & Ray Johnson\n\n"
"%1$C 1998-2000 Scriptics Inc.\n\n"
"%1$C 1996-1997 Sun Microsystems Inc.", 0xA9, year]
attributes:textAttributes]
replacementRange:NSMakeRange(0,0)];
[credits setDrawsBackground:NO];
[credits setEditable:NO];
NSAlert *about = [[NSAlert alloc] init];
[[about window] setTitle:@"About Tcl & Tk"];
[about setMessageText: version];
[about setInformativeText:url];
about.accessoryView = credits;
[about runModal];
[about release];
}
/*
*----------------------------------------------------------------------
*
* TkMacOSXStandardAboutPanelObjCmd --
*
* Implements the ::tk::mac::standardAboutPanel command.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* none
*
*----------------------------------------------------------------------
*/
int
TkMacOSXStandardAboutPanelObjCmd(
ClientData dummy, /* Unused. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
(void)dummy;
if (objc > 1) {
Tcl_WrongNumArgs(interp, 1, objv, NULL);
return TCL_ERROR;
}
TkAboutDlg();
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Tk_MessageBoxObjCmd --
*
* Implements the tk_messageBox in native Mac OS X style.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* none
*
*----------------------------------------------------------------------
*/
int
Tk_MessageBoxObjCmd(
ClientData clientData, /* Main window associated with interpreter. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
Tk_Window tkwin = clientData;
char *str;
int i, result = TCL_ERROR, haveParentOption = 0;
int index, typeIndex, iconIndex, indexDefaultOption = 0;
int defaultNativeButtonIndex = 1; /* 1, 2, 3: right to left */
Tcl_Obj *cmdObj = NULL;
AlertCallbackInfo callbackInfoStruct, *callbackInfo = &callbackInfoStruct;
NSString *message, *title;
NSWindow *parent;
NSArray *buttons;
NSAlert *alert = [NSAlert new];
NSInteger modalReturnCode = 1;
BOOL parentIsKey = NO;
iconIndex = ICON_INFO;
typeIndex = TYPE_OK;
for (i = 1; i < objc; i += 2) {
if (Tcl_GetIndexFromObjStruct(interp, objv[i], alertOptionStrings,
sizeof(char *), "option", TCL_EXACT, &index) != TCL_OK) {
goto end;
}
if (i + 1 == objc) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"value for \"%s\" missing", Tcl_GetString(objv[i])));
Tcl_SetErrorCode(interp, "TK", "MSGBOX", "VALUE", NULL);
goto end;
}
switch (index) {
case ALERT_DEFAULT:
/*
* Need to postpone processing of this option until we are sure to
* know the '-type' as well.
*/
indexDefaultOption = i;
break;
case ALERT_DETAIL:
message = [[NSString alloc] initWithUTF8String:
Tcl_GetString(objv[i + 1])];
[alert setInformativeText:message];
[message release];
break;
case ALERT_ICON:
if (Tcl_GetIndexFromObjStruct(interp, objv[i + 1], alertIconStrings,
sizeof(char *), "-icon value", TCL_EXACT, &iconIndex) != TCL_OK) {
goto end;
}
break;
case ALERT_MESSAGE:
message = [[NSString alloc] initWithUTF8String:
Tcl_GetString(objv[i + 1])];
[alert setMessageText:message];
[message release];
break;
case ALERT_PARENT:
str = Tcl_GetString(objv[i + 1]);
tkwin = Tk_NameToWindow(interp, str, tkwin);
if (!tkwin) {
goto end;
}
haveParentOption = 1;
break;
case ALERT_TITLE:
title = [[NSString alloc] initWithUTF8String:
Tcl_GetString(objv[i + 1])];
[[alert window] setTitle:title];
[title release];
break;
case ALERT_TYPE:
if (Tcl_GetIndexFromObjStruct(interp, objv[i + 1], alertTypeStrings,
sizeof(char *), "-type value", TCL_EXACT, &typeIndex) != TCL_OK) {
goto end;
}
break;
case ALERT_COMMAND:
cmdObj = objv[i+1];
break;
}
}
if (indexDefaultOption) {
/*
* Any '-default' option needs to know the '-type' option, which is
* why we do this here.
*/
if (Tcl_GetIndexFromObjStruct(interp, objv[indexDefaultOption + 1],
alertButtonStrings, sizeof(char *), "-default value",
TCL_EXACT, &index) != TCL_OK) {
goto end;
}
/*
* Need to map from "ok" etc. to 1, 2, 3, right to left.
*/
defaultNativeButtonIndex =
alertButtonIndexAndTypeToNativeButtonIndex[typeIndex][index];
if (!defaultNativeButtonIndex) {
Tcl_SetObjResult(interp,
Tcl_NewStringObj("Illegal default option", -1));
Tcl_SetErrorCode(interp, "TK", "MSGBOX", "DEFAULT", NULL);
goto end;
}
}
[alert setIcon:[NSApp applicationIconImage]];
[alert setAlertStyle:alertStyles[iconIndex]];
i = 0;
while (i < 3 && alertButtonNames[typeIndex][i]) {
[alert addButtonWithTitle:(NSString*) alertButtonNames[typeIndex][i++]];
}
buttons = [alert buttons];
for (NSButton *b in buttons) {
NSString *ke = [b keyEquivalent];
if (([ke isEqualToString:@"\r"] || [ke isEqualToString:@"\033"]) &&
![b keyEquivalentModifierMask]) {
[b setKeyEquivalent:@""];
}
}
[[buttons objectAtIndex: [buttons count]-1] setKeyEquivalent: @"\033"];
[[buttons objectAtIndex: defaultNativeButtonIndex-1]
setKeyEquivalent: @"\r"];
if (cmdObj) {
if (Tcl_IsShared(cmdObj)) {
cmdObj = Tcl_DuplicateObj(cmdObj);
}
Tcl_IncrRefCount(cmdObj);
}
callbackInfo = (AlertCallbackInfo *)ckalloc(sizeof(AlertCallbackInfo));
callbackInfo->cmdObj = cmdObj;
callbackInfo->interp = interp;
callbackInfo->typeIndex = typeIndex;
parent = TkMacOSXDrawableWindow(((TkWindow *) tkwin)->window);
if (haveParentOption && parent && ![parent attachedSheet]) {
parentIsKey = [parent isKeyWindow];
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
[alert beginSheetModalForWindow:parent
completionHandler:^(NSModalResponse returnCode) {
[NSApp tkAlertDidEnd:alert
returnCode:returnCode
contextInfo:callbackInfo];
}];
#else
[alert beginSheetModalForWindow:parent
modalDelegate:NSApp
didEndSelector:@selector(tkAlertDidEnd:returnCode:contextInfo:)
contextInfo:callbackInfo];
#endif
modalReturnCode = cmdObj ? 0 :
[alert runModal];
} else {
modalReturnCode = [alert runModal];
[NSApp tkAlertDidEnd:alert returnCode:modalReturnCode
contextInfo:callbackInfo];
}
result = (modalReturnCode >= NSAlertFirstButtonReturn) ? TCL_OK : TCL_ERROR;
end:
[alert release];
if (parentIsKey) {
[parent makeKeyWindow];
}
return result;
}
/*
*----------------------------------------------------------------------
*/
#pragma mark [tk fontchooser] implementation (TIP 324)
/*
*----------------------------------------------------------------------
*/
#include "tkMacOSXInt.h"
#include "tkMacOSXFont.h"
typedef struct FontchooserData {
Tcl_Obj *titleObj;
Tcl_Obj *cmdObj;
Tk_Window parent;
} FontchooserData;
enum FontchooserEvent {
FontchooserClosed,
FontchooserSelection
};
static void FontchooserEvent(int kind);
static Tcl_Obj * FontchooserCget(FontchooserData *fcdPtr,
int optionIndex);
static int FontchooserConfigureCmd(ClientData clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *const objv[]);
static int FontchooserShowCmd(ClientData clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *const objv[]);
static int FontchooserHideCmd(ClientData clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *const objv[]);
static void FontchooserParentEventHandler(ClientData clientData,
XEvent *eventPtr);
static void DeleteFontchooserData(ClientData clientData,
Tcl_Interp *interp);
MODULE_SCOPE const TkEnsemble tkFontchooserEnsemble[];
const TkEnsemble tkFontchooserEnsemble[] = {
{ "configure", FontchooserConfigureCmd, NULL },
{ "show", FontchooserShowCmd, NULL },
{ "hide", FontchooserHideCmd, NULL },
{ NULL, NULL, NULL }
};
static Tcl_Interp *fontchooserInterp = NULL;
static NSFont *fontPanelFont = nil;
static NSMutableDictionary *fontPanelFontAttributes = nil;
static const char *const fontchooserOptionStrings[] = {
"-parent", "-title", "-font", "-command",
"-visible", NULL
};
enum FontchooserOption {
FontchooserParent, FontchooserTitle, FontchooserFont, FontchooserCmd,
FontchooserVisible
};
@implementation TKApplication(TKFontPanel)
- (void) changeFont: (id) sender
{
NSFontManager *fm = [NSFontManager sharedFontManager];
(void)sender;
if ([fm currentFontAction] == NSViaPanelFontAction) {
NSFont *font = [fm convertFont:fontPanelFont];
if (![fontPanelFont isEqual:font]) {
[fontPanelFont release];
fontPanelFont = [font retain];
FontchooserEvent(FontchooserSelection);
}
}
}
- (void) changeAttributes: (id) sender
{
NSDictionary *attributes = [sender convertAttributes:
fontPanelFontAttributes];
if (![fontPanelFontAttributes isEqual:attributes]) {
[fontPanelFontAttributes setDictionary:attributes];
FontchooserEvent(FontchooserSelection);
}
}
- (NSUInteger) validModesForFontPanel: (NSFontPanel *) fontPanel
{
(void)fontPanel;
return (NSFontPanelStandardModesMask & ~NSFontPanelAllEffectsModeMask) |
NSFontPanelUnderlineEffectModeMask |
NSFontPanelStrikethroughEffectModeMask;
}
- (void) windowDidOrderOffScreen: (NSNotification *) notification
{
#ifdef TK_MAC_DEBUG_NOTIFICATIONS
TKLog(@"-[%@(%p) %s] %@", [self class], self, _cmd, notification);
#endif
if ([[notification object] isEqual:[[NSFontManager sharedFontManager]
fontPanel:NO]]) {
FontchooserEvent(FontchooserClosed);
}
}
@end
/*
*----------------------------------------------------------------------
*
* FontchooserEvent --
*
* This processes events generated by user interaction with the font
* panel.
*
* Results:
* None.
*
* Side effects:
* Additional events may be placed on the Tk event queue.
*
*----------------------------------------------------------------------
*/
static void
FontchooserEvent(
int kind)
{
FontchooserData *fcdPtr;
Tcl_Obj *fontObj;
if (!fontchooserInterp) {
return;
}
fcdPtr = Tcl_GetAssocData(fontchooserInterp, "::tk::fontchooser", NULL);
switch (kind) {
case FontchooserClosed:
if (fcdPtr->parent != None) {
TkSendVirtualEvent(fcdPtr->parent, "TkFontchooserVisibility", NULL);
fontchooserInterp = NULL;
}
break;
case FontchooserSelection:
fontObj = TkMacOSXFontDescriptionForNSFontAndNSFontAttributes(
fontPanelFont, fontPanelFontAttributes);
if (fontObj) {
if (fcdPtr->cmdObj) {
int objc, result;
Tcl_Obj **objv, **tmpv;
result = Tcl_ListObjGetElements(fontchooserInterp,
fcdPtr->cmdObj, &objc, &objv);
if (result == TCL_OK) {
tmpv = (Tcl_Obj **)ckalloc(sizeof(Tcl_Obj *) * (objc + 2));
memcpy(tmpv, objv, sizeof(Tcl_Obj *) * objc);
tmpv[objc] = fontObj;
TkBackgroundEvalObjv(fontchooserInterp, objc + 1, tmpv,
TCL_EVAL_GLOBAL);
ckfree(tmpv);
}
}
TkSendVirtualEvent(fcdPtr->parent, "TkFontchooserFontChanged", NULL);
}
break;
}
}
/*
*----------------------------------------------------------------------
*
* FontchooserCget --
*
* Helper for the FontchooserConfigure command to return the current value
* of any of the options (which may be NULL in the structure).
*
* Results:
* Tcl object of option value.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static Tcl_Obj *
FontchooserCget(
FontchooserData *fcdPtr,
int optionIndex)
{
Tcl_Obj *resObj = NULL;
switch(optionIndex) {
case FontchooserParent:
if (fcdPtr->parent != None) {
resObj = Tcl_NewStringObj(
((TkWindow *) fcdPtr->parent)->pathName, -1);
} else {
resObj = Tcl_NewStringObj(".", 1);
}
break;
case FontchooserTitle:
if (fcdPtr->titleObj) {
resObj = fcdPtr->titleObj;
} else {
resObj = Tcl_NewObj();
}
break;
case FontchooserFont:
resObj = TkMacOSXFontDescriptionForNSFontAndNSFontAttributes(
fontPanelFont, fontPanelFontAttributes);
if (!resObj) {
resObj = Tcl_NewObj();
}
break;
case FontchooserCmd:
if (fcdPtr->cmdObj) {
resObj = fcdPtr->cmdObj;
} else {
resObj = Tcl_NewObj();
}
break;
case FontchooserVisible:
resObj = Tcl_NewWideIntObj([[[NSFontManager sharedFontManager]
fontPanel:NO] isVisible] != 0);
break;
default:
resObj = Tcl_NewObj();
}
return resObj;
}
/*
* ----------------------------------------------------------------------
*
* FontchooserConfigureCmd --
*
* Implementation of the 'tk fontchooser configure' ensemble command. See
* the user documentation for what it does.
*
* Results:
* See the user documentation.
*
* Side effects:
* Per-interp data structure may be modified
*
* ----------------------------------------------------------------------
*/
static int
FontchooserConfigureCmd(
ClientData clientData, /* Main window */
Tcl_Interp *interp,
int objc,
Tcl_Obj *const objv[])
{
Tk_Window tkwin = (Tk_Window)clientData;
FontchooserData *fcdPtr = Tcl_GetAssocData(interp, "::tk::fontchooser",
NULL);
int i, r = TCL_OK;
/*
* With no arguments we return all the options in a dict
*/
if (objc == 1) {
Tcl_Obj *keyObj, *valueObj;
Tcl_Obj *dictObj = Tcl_NewDictObj();
for (i = 0; r == TCL_OK && fontchooserOptionStrings[i] != NULL; ++i) {
keyObj = Tcl_NewStringObj(fontchooserOptionStrings[i], -1);
valueObj = FontchooserCget(fcdPtr, i);
r = Tcl_DictObjPut(interp, dictObj, keyObj, valueObj);
}
if (r == TCL_OK) {
Tcl_SetObjResult(interp, dictObj);
}
return r;
}
for (i = 1; i < objc; i += 2) {
int optionIndex, len;
if (Tcl_GetIndexFromObjStruct(interp, objv[i], fontchooserOptionStrings,
sizeof(char *), "option", 0, &optionIndex) != TCL_OK) {
return TCL_ERROR;
}
if (objc == 2) {
/*
* With one option and no arg, return the current value.
*/
Tcl_SetObjResult(interp, FontchooserCget(fcdPtr, optionIndex));
return TCL_OK;
}
if (i + 1 == objc) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"value for \"%s\" missing", Tcl_GetString(objv[i])));
Tcl_SetErrorCode(interp, "TK", "FONTDIALOG", "VALUE", NULL);
return TCL_ERROR;
}
switch (optionIndex) {
case FontchooserVisible: {
const char *msg = "cannot change read-only option "
"\"-visible\": use the show or hide command";
Tcl_SetObjResult(interp, Tcl_NewStringObj(msg, -1));
Tcl_SetErrorCode(interp, "TK", "FONTDIALOG", "READONLY", NULL);
return TCL_ERROR;
}
case FontchooserParent: {
Tk_Window parent = Tk_NameToWindow(interp,
Tcl_GetString(objv[i+1]), tkwin);
if (parent == None) {
return TCL_ERROR;
}
if (fcdPtr->parent) {
Tk_DeleteEventHandler(fcdPtr->parent, StructureNotifyMask,
FontchooserParentEventHandler, fcdPtr);
}
fcdPtr->parent = parent;
Tk_CreateEventHandler(fcdPtr->parent, StructureNotifyMask,
FontchooserParentEventHandler, fcdPtr);
break;
}
case FontchooserTitle:
if (fcdPtr->titleObj) {
Tcl_DecrRefCount(fcdPtr->titleObj);
}
Tcl_GetStringFromObj(objv[i+1], &len);
if (len) {
fcdPtr->titleObj = objv[i+1];
if (Tcl_IsShared(fcdPtr->titleObj)) {
fcdPtr->titleObj = Tcl_DuplicateObj(fcdPtr->titleObj);
}
Tcl_IncrRefCount(fcdPtr->titleObj);
} else {
fcdPtr->titleObj = NULL;
}
break;
case FontchooserFont:
Tcl_GetStringFromObj(objv[i+1], &len);
if (len) {
Tk_Font f = Tk_AllocFontFromObj(interp, tkwin, objv[i+1]);
if (!f) {
return TCL_ERROR;
}
[fontPanelFont autorelease];
fontPanelFont = [TkMacOSXNSFontForFont(f) retain];
[fontPanelFontAttributes setDictionary:
TkMacOSXNSFontAttributesForFont(f)];
[fontPanelFontAttributes removeObjectsForKeys:[NSArray
arrayWithObjects:NSFontAttributeName,
NSLigatureAttributeName, NSKernAttributeName, nil]];
Tk_FreeFont(f);
} else {
[fontPanelFont release];
fontPanelFont = nil;
[fontPanelFontAttributes removeAllObjects];
}
NSFontManager *fm = [NSFontManager sharedFontManager];
NSFontPanel *fp = [fm fontPanel:NO];
[fp setPanelFont:fontPanelFont isMultiple:NO];
[fm setSelectedFont:fontPanelFont isMultiple:NO];
[fm setSelectedAttributes:fontPanelFontAttributes
isMultiple:NO];
if ([fp isVisible]) {
TkSendVirtualEvent(fcdPtr->parent,
"TkFontchooserFontChanged", NULL);
}
break;
case FontchooserCmd:
if (fcdPtr->cmdObj) {
Tcl_DecrRefCount(fcdPtr->cmdObj);
}
Tcl_GetStringFromObj(objv[i+1], &len);
if (len) {
fcdPtr->cmdObj = objv[i+1];
if (Tcl_IsShared(fcdPtr->cmdObj)) {
fcdPtr->cmdObj = Tcl_DuplicateObj(fcdPtr->cmdObj);
}
Tcl_IncrRefCount(fcdPtr->cmdObj);
} else {
fcdPtr->cmdObj = NULL;
}
break;
}
}
return TCL_OK;
}
/*
* ----------------------------------------------------------------------
*
* FontchooserShowCmd --
*
* Implements the 'tk fontchooser show' ensemble command. The per-interp
* configuration data for the dialog is held in an interp associated
* structure.
*
* Results:
* See the user documentation.
*
* Side effects:
* Font Panel may be shown.
*
* ----------------------------------------------------------------------
*/
static int
FontchooserShowCmd(
ClientData clientData, /* Main window */
Tcl_Interp *interp,
int objc,
Tcl_Obj *const objv[])
{
FontchooserData *fcdPtr = Tcl_GetAssocData(interp, "::tk::fontchooser",
NULL);
(void)objc;
(void)objv;
if (fcdPtr->parent == None) {
fcdPtr->parent = (Tk_Window) clientData;
Tk_CreateEventHandler(fcdPtr->parent, StructureNotifyMask,
FontchooserParentEventHandler, fcdPtr);
}
NSFontManager *fm = [NSFontManager sharedFontManager];
NSFontPanel *fp = [fm fontPanel:YES];
if ([fp delegate] != NSApp) {
[fp setDelegate:NSApp];
}
if (![fp isVisible]) {
[fm orderFrontFontPanel:NSApp];
TkSendVirtualEvent(fcdPtr->parent, "TkFontchooserVisibility", NULL);
}
fontchooserInterp = interp;
return TCL_OK;
}
/*
* ----------------------------------------------------------------------
*
* FontchooserHideCmd --
*
* Implementation of the 'tk fontchooser hide' ensemble. See the user
* documentation for details.
*
* Results:
* See the user documentation.
*
* Side effects:
* Font Panel may be hidden.
*
* ----------------------------------------------------------------------
*/
static int
FontchooserHideCmd(
ClientData dummy, /* Main window */
Tcl_Interp *interp,
int objc,
Tcl_Obj *const objv[])
{
NSFontPanel *fp = [[NSFontManager sharedFontManager] fontPanel:NO];
(void)dummy;
(void)interp;
(void)objc;
(void)objv;
if ([fp isVisible]) {
[fp orderOut:NSApp];
}
return TCL_OK;
}
/*
* ----------------------------------------------------------------------
*
* FontchooserParentEventHandler --
*
* Event handler for StructureNotify events on the font chooser's parent
* window.
*
* Results:
* None.
*
* Side effects:
* Font chooser parent info is cleared and font panel is hidden.
*
* ----------------------------------------------------------------------
*/
static void
FontchooserParentEventHandler(
ClientData clientData,
XEvent *eventPtr)
{
FontchooserData *fcdPtr = clientData;
if (eventPtr->type == DestroyNotify) {
Tk_DeleteEventHandler(fcdPtr->parent, StructureNotifyMask,
FontchooserParentEventHandler, fcdPtr);
fcdPtr->parent = NULL;
FontchooserHideCmd(NULL, NULL, 0, NULL);
}
}
/*
* ----------------------------------------------------------------------
*
* DeleteFontchooserData --
*
* Clean up the font chooser configuration data when the interp is
* destroyed.
*
* Results:
* None.
*
* Side effects:
* per-interp configuration data is destroyed.
*
* ----------------------------------------------------------------------
*/
static void
DeleteFontchooserData(
ClientData clientData,
Tcl_Interp *interp)
{
FontchooserData *fcdPtr = clientData;
if (fcdPtr->titleObj) {
Tcl_DecrRefCount(fcdPtr->titleObj);
}
if (fcdPtr->cmdObj) {
Tcl_DecrRefCount(fcdPtr->cmdObj);
}
ckfree(fcdPtr);
if (fontchooserInterp == interp) {
fontchooserInterp = NULL;
}
}
/*
* ----------------------------------------------------------------------
*
* TkInitFontchooser --
*
* Associate the font chooser configuration data with the Tcl interpreter.
* There is one font chooser per interp.
*
* Results:
* None.
*
* Side effects:
* per-interp configuration data is destroyed.
*
* ----------------------------------------------------------------------
*/
MODULE_SCOPE int
TkInitFontchooser(
Tcl_Interp *interp,
ClientData dummy)
{
FontchooserData *fcdPtr = (FontchooserData *)ckalloc(sizeof(FontchooserData));
(void)dummy;
bzero(fcdPtr, sizeof(FontchooserData));
Tcl_SetAssocData(interp, "::tk::fontchooser", DeleteFontchooserData,
fcdPtr);
if (!fontPanelFontAttributes) {
fontPanelFontAttributes = [NSMutableDictionary new];
}
return TCL_OK;
}
/*
* Local Variables:
* mode: objc
* c-basic-offset: 4
* fill-column: 79
* coding: utf-8
* End:
*/
|