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
|
/*
* 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>
*
* 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"
static const char *colorOptionStrings[] = {
"-initialcolor", "-parent", "-title", NULL
};
enum colorOptions {
COLOR_INITIAL, COLOR_PARENT, COLOR_TITLE
};
static const char *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 *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 *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 *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 *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 *alertIconStrings[] = {
"error", "info", "question", "warning", NULL
};
enum alertIconOptions {
ICON_ERROR, ICON_INFO, ICON_QUESTION, ICON_WARNING
};
static const char *alertButtonStrings[] = {
"abort", "retry", "ignore", "ok", "cancel", "yes", "no", 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, 1, 2},
[TYPE_YESNOCANCEL] = {0, 0, 0, 0, 3, 1, 2},
};
/*
* 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] = {5, 6, 0},
[TYPE_YESNOCANCEL] = {5, 6, 4},
};
#pragma mark TKApplication(TKDialog)
@interface NSColorPanel(TKDialog)
- (void)_setUseModalAppearance:(BOOL)flag;
@end
@implementation TKApplication(TKDialog)
- (void)tkFilePanelDidEnd:(NSSavePanel *)panel returnCode:(NSInteger)returnCode
contextInfo:(void *)contextInfo {
FilePanelCallbackInfo *callbackInfo = contextInfo;
if (returnCode == NSFileHandlingPanelOKButton) {
Tcl_Obj *resultObj;
if (callbackInfo->multiple) {
resultObj = Tcl_NewListObj(0, NULL);
for (NSString *name in [(NSOpenPanel*)panel filenames]) {
Tcl_ListObjAppendElement(callbackInfo->interp, resultObj,
Tcl_NewStringObj([name UTF8String], -1));
}
} else {
resultObj = Tcl_NewStringObj([[panel filename] 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((char *)tmpv);
}
} else {
Tcl_SetObjResult(callbackInfo->interp, resultObj);
}
} else if (returnCode == NSFileHandlingPanelCancelButton) {
Tcl_ResetResult(callbackInfo->interp);
}
if (panel == [NSApp modalWindow]) {
[NSApp stopModalWithCode:returnCode];
}
if (callbackInfo->cmdObj) {
Tcl_DecrRefCount(callbackInfo->cmdObj);
ckfree((char*) callbackInfo);
}
}
- (void)tkAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode
contextInfo:(void *)contextInfo {
AlertCallbackInfo *callbackInfo = contextInfo;
if (returnCode != NSAlertErrorReturn) {
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((char *)tmpv);
}
} else {
Tcl_SetObjResult(callbackInfo->interp, resultObj);
}
}
if ([alert window] == [NSApp modalWindow]) {
[NSApp stopModalWithCode:returnCode];
}
if (callbackInfo->cmdObj) {
Tcl_DecrRefCount(callbackInfo->cmdObj);
ckfree((char*) callbackInfo);
}
}
@end
#pragma mark -
/*
*----------------------------------------------------------------------
*
* 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 *option, *value;
if (Tcl_GetIndexFromObj(interp, objv[i], colorOptionStrings, "option",
TCL_EXACT, &index) != TCL_OK) {
goto end;
}
if (i + 1 == objc) {
option = Tcl_GetString(objv[i]);
Tcl_AppendResult(interp, "value for \"", option, "\" missing",
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 == NSOKButton) {
color = [[colorPanel color] colorUsingColorSpace:
[NSColorSpace genericRGBColorSpace]];
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;
}
/*
*----------------------------------------------------------------------
*
* 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;
FileFilterList fl;
Tcl_Obj *cmdObj = NULL, *typeVariablePtr = NULL;
FilePanelCallbackInfo callbackInfoStruct;
FilePanelCallbackInfo *callbackInfo = &callbackInfoStruct;
NSString *directory = nil, *filename = nil;
NSString *message, *title, *type;
NSWindow *parent;
NSMutableArray *fileTypes = nil;
NSOpenPanel *panel = [NSOpenPanel openPanel];
NSInteger returnCode = NSAlertErrorReturn;
TkInitFileFilters(&fl);
for (i = 1; i < objc; i += 2) {
if (Tcl_GetIndexFromObj(interp, objv[i], openOptionStrings, "option",
TCL_EXACT, &index) != TCL_OK) {
goto end;
}
if (i + 1 == objc) {
str = Tcl_GetString(objv[i]);
Tcl_AppendResult(interp, "value for \"", str, "\" missing", NULL);
goto end;
}
switch (index) {
case OPEN_DEFAULT:
break;
case OPEN_FILETYPES:
if (TkGetFileFilters(interp, &fl, objv[i + 1], 0) != TCL_OK) {
goto end;
}
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])];
[panel setMessage:message];
[message release];
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])];
[panel setTitle:title];
[title release];
break;
case OPEN_TYPEVARIABLE:
typeVariablePtr = objv[i + 1];
break;
case OPEN_COMMAND:
cmdObj = objv[i+1];
break;
}
}
if (fl.filters) {
fileTypes = [NSMutableArray array];
for (FileFilter *filterPtr = fl.filters; filterPtr;
filterPtr = filterPtr->next) {
for (FileFilterClause *clausePtr = filterPtr->clauses; clausePtr;
clausePtr = clausePtr->next) {
for (GlobPattern *globPtr = clausePtr->patterns; globPtr;
globPtr = globPtr->next) {
str = globPtr->pattern;
while (*str && (*str == '*' || *str == '.')) {
str++;
}
if (*str) {
type = [[NSString alloc] initWithUTF8String:str];
if (![fileTypes containsObject:type]) {
[fileTypes addObject:type];
}
[type release];
}
}
for (MacFileType *mfPtr = clausePtr->macTypes; mfPtr;
mfPtr = mfPtr->next) {
if (mfPtr->type) {
type = NSFileTypeForHFSTypeCode(mfPtr->type);
if (![fileTypes containsObject:type]) {
[fileTypes addObject:type];
}
}
}
}
}
}
[panel setAllowsMultipleSelection:multiple];
if (cmdObj) {
callbackInfo = (FilePanelCallbackInfo *)
ckalloc(sizeof(FilePanelCallbackInfo));
if (Tcl_IsShared(cmdObj)) {
cmdObj = Tcl_DuplicateObj(cmdObj);
}
Tcl_IncrRefCount(cmdObj);
}
callbackInfo->cmdObj = cmdObj;
callbackInfo->interp = interp;
callbackInfo->multiple = multiple;
parent = TkMacOSXDrawableWindow(((TkWindow *) tkwin)->window);
if (haveParentOption && parent && ![parent attachedSheet]) {
[panel beginSheetForDirectory:directory file:filename types:fileTypes
modalForWindow:parent modalDelegate:NSApp didEndSelector:
@selector(tkFilePanelDidEnd:returnCode:contextInfo:)
contextInfo:callbackInfo];
returnCode = cmdObj ? NSAlertOtherReturn :
[NSApp runModalForWindow:panel];
} else {
returnCode = [panel runModalForDirectory:directory file:filename
types:fileTypes];
[NSApp tkFilePanelDidEnd:panel returnCode:returnCode
contextInfo:callbackInfo];
}
result = (returnCode != NSAlertErrorReturn) ? TCL_OK : TCL_ERROR;
if (typeVariablePtr && result == TCL_OK) {
/*
* The -typevariable option is not really supported.
*/
Tcl_SetVar(interp, Tcl_GetString(typeVariablePtr), "",
TCL_GLOBAL_ONLY);
}
end:
TkFreeFileFilters(&fl);
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;
FileFilterList fl;
Tcl_Obj *cmdObj = NULL;
FilePanelCallbackInfo callbackInfoStruct;
FilePanelCallbackInfo *callbackInfo = &callbackInfoStruct;
NSString *directory = nil, *filename = nil, *defaultType = nil;
NSString *message, *title, *type;
NSWindow *parent;
NSMutableArray *fileTypes = nil;
NSSavePanel *panel = [NSSavePanel savePanel];
NSInteger returnCode = NSAlertErrorReturn;
TkInitFileFilters(&fl);
for (i = 1; i < objc; i += 2) {
if (Tcl_GetIndexFromObj(interp, objv[i], saveOptionStrings, "option",
TCL_EXACT, &index) != TCL_OK) {
goto end;
}
if (i + 1 == objc) {
str = Tcl_GetString(objv[i]);
Tcl_AppendResult(interp, "value for \"", str, "\" missing",
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:
if (TkGetFileFilters(interp, &fl, objv[i + 1], 0) != TCL_OK) {
goto end;
}
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];
}
break;
case SAVE_MESSAGE:
message = [[NSString alloc] initWithUTF8String:
Tcl_GetString(objv[i + 1])];
[panel setMessage:message];
[message release];
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])];
[panel setTitle:title];
[title release];
break;
case SAVE_TYPEVARIABLE:
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 (fl.filters || defaultType) {
fileTypes = [NSMutableArray array];
[fileTypes addObject:defaultType ? defaultType : (id)kUTTypeContent];
for (FileFilter *filterPtr = fl.filters; filterPtr;
filterPtr = filterPtr->next) {
for (FileFilterClause *clausePtr = filterPtr->clauses; clausePtr;
clausePtr = clausePtr->next) {
for (GlobPattern *globPtr = clausePtr->patterns; globPtr;
globPtr = globPtr->next) {
str = globPtr->pattern;
while (*str && (*str == '*' || *str == '.')) {
str++;
}
if (*str) {
type = [[NSString alloc] initWithUTF8String:str];
if (![fileTypes containsObject:type]) {
[fileTypes addObject:type];
}
[type release];
}
}
}
}
[panel setAllowedFileTypes:fileTypes];
[panel setAllowsOtherFileTypes:YES];
}
[panel setCanSelectHiddenExtension:YES];
[panel setExtensionHidden:NO];
if (cmdObj) {
callbackInfo = (FilePanelCallbackInfo *)
ckalloc(sizeof(FilePanelCallbackInfo));
if (Tcl_IsShared(cmdObj)) {
cmdObj = Tcl_DuplicateObj(cmdObj);
}
Tcl_IncrRefCount(cmdObj);
}
callbackInfo->cmdObj = cmdObj;
callbackInfo->interp = interp;
callbackInfo->multiple = 0;
parent = TkMacOSXDrawableWindow(((TkWindow *) tkwin)->window);
if (haveParentOption && parent && ![parent attachedSheet]) {
[panel beginSheetForDirectory:directory file:filename
modalForWindow:parent modalDelegate:NSApp didEndSelector:
@selector(tkFilePanelDidEnd:returnCode:contextInfo:)
contextInfo:callbackInfo];
returnCode = cmdObj ? NSAlertOtherReturn :
[NSApp runModalForWindow:panel];
} else {
returnCode = [panel runModalForDirectory:directory file:filename];
[NSApp tkFilePanelDidEnd:panel returnCode:returnCode
contextInfo:callbackInfo];
}
result = (returnCode != NSAlertErrorReturn) ? TCL_OK : TCL_ERROR;
end:
TkFreeFileFilters(&fl);
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, *filename = nil;
NSString *message, *title;
NSWindow *parent;
NSOpenPanel *panel = [NSOpenPanel openPanel];
NSInteger returnCode = NSAlertErrorReturn;
for (i = 1; i < objc; i += 2) {
if (Tcl_GetIndexFromObj(interp, objv[i], chooseOptionStrings, "option",
TCL_EXACT, &index) != TCL_OK) {
goto end;
}
if (i + 1 == objc) {
str = Tcl_GetString(objv[i]);
Tcl_AppendResult(interp, "value for \"", str, "\" missing", 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) {
callbackInfo = (FilePanelCallbackInfo *)
ckalloc(sizeof(FilePanelCallbackInfo));
if (Tcl_IsShared(cmdObj)) {
cmdObj = Tcl_DuplicateObj(cmdObj);
}
Tcl_IncrRefCount(cmdObj);
}
callbackInfo->cmdObj = cmdObj;
callbackInfo->interp = interp;
callbackInfo->multiple = 0;
parent = TkMacOSXDrawableWindow(((TkWindow *) tkwin)->window);
if (haveParentOption && parent && ![parent attachedSheet]) {
[panel beginSheetForDirectory:directory file:filename
modalForWindow:parent modalDelegate:NSApp didEndSelector:
@selector(tkFilePanelDidEnd:returnCode:contextInfo:)
contextInfo:callbackInfo];
returnCode = cmdObj ? NSAlertOtherReturn :
[NSApp runModalForWindow:panel];
} else {
returnCode = [panel runModalForDirectory:directory file:filename];
[NSApp tkFilePanelDidEnd:panel returnCode:returnCode
contextInfo:callbackInfo];
}
result = (returnCode != NSAlertErrorReturn) ? TCL_OK : TCL_ERROR;
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];
NSMutableParagraphStyle *style = [[[NSParagraphStyle defaultParagraphStyle]
mutableCopy] autorelease];
[style setAlignment:NSCenterTextAlignment];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
@"Tcl & Tk", @"ApplicationName",
@"Tcl " TCL_VERSION " & Tk " TK_VERSION, @"ApplicationVersion",
@TK_PATCH_LEVEL, @"Version",
image, @"ApplicationIcon",
[NSString stringWithFormat:@"Copyright %1$C 1987-%2$@.", 0xA9,
year], @"Copyright",
[[[NSAttributedString alloc] initWithString:
[NSString stringWithFormat:
@"%1$C 1987-%2$@ Tcl Core Team." "\n\n"
"%1$C 2002-%2$@ 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:
[NSDictionary dictionaryWithObject:style
forKey:NSParagraphStyleAttributeName]] autorelease], @"Credits",
nil];
[NSApp orderFrontStandardAboutPanelWithOptions:options];
}
/*
*----------------------------------------------------------------------
*
* TkMacOSXStandardAboutPanelObjCmd --
*
* Implements the ::tk::mac::standardAboutPanel command.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* none
*
*----------------------------------------------------------------------
*/
int
TkMacOSXStandardAboutPanelObjCmd(
ClientData clientData, /* Unused. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument objects. */
{
if (objc > 1) {
Tcl_WrongNumArgs(interp, 1, objv, NULL);
return TCL_ERROR;
}
[NSApp orderFrontStandardAboutPanelWithOptions:nil];
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 returnCode = NSAlertErrorReturn;
iconIndex = ICON_INFO;
typeIndex = TYPE_OK;
for (i = 1; i < objc; i += 2) {
if (Tcl_GetIndexFromObj(interp, objv[i], alertOptionStrings, "option",
TCL_EXACT, &index) != TCL_OK) {
goto end;
}
if (i + 1 == objc) {
str = Tcl_GetString(objv[i]);
Tcl_AppendResult(interp, "value for \"", str, "\" missing", 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_GetIndexFromObj(interp, objv[i + 1], alertIconStrings,
"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_GetIndexFromObj(interp, objv[i + 1], alertTypeStrings,
"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_GetIndexFromObj(interp, objv[indexDefaultOption + 1],
alertButtonStrings, "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));
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) {
callbackInfo = (AlertCallbackInfo *) ckalloc(sizeof(AlertCallbackInfo));
if (Tcl_IsShared(cmdObj)) {
cmdObj = Tcl_DuplicateObj(cmdObj);
}
Tcl_IncrRefCount(cmdObj);
}
callbackInfo->cmdObj = cmdObj;
callbackInfo->interp = interp;
callbackInfo->typeIndex = typeIndex;
parent = TkMacOSXDrawableWindow(((TkWindow *) tkwin)->window);
if (haveParentOption && parent && ![parent attachedSheet]) {
[alert beginSheetModalForWindow:parent modalDelegate:NSApp
didEndSelector:@selector(tkAlertDidEnd:returnCode:contextInfo:)
contextInfo:callbackInfo];
returnCode = cmdObj ? NSAlertOtherReturn :
[NSApp runModalForWindow:[alert window]];
} else {
returnCode = [alert runModal];
[NSApp tkAlertDidEnd:alert returnCode:returnCode
contextInfo:callbackInfo];
}
result = (returnCode != NSAlertErrorReturn) ? TCL_OK : TCL_ERROR;
end:
[alert release];
return result;
}
/*
* Local Variables:
* mode: objc
* c-basic-offset: 4
* fill-column: 79
* coding: utf-8
* End:
*/
|