/
tkWinDialog.c
2646 lines (2362 loc) · 72.5 KB
/
tkWinDialog.c
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
/*
* tkWinDialog.c --
*
* Contains the Windows implementation of the common dialog boxes.
*
* Copyright (c) 1996-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tkWinDialog.c,v 1.32 2003/07/28 22:12:33 patthoyts Exp $
*
*/
#include "tkWinInt.h"
#include "tkFileFilter.h"
#include <commdlg.h> /* includes common dialog functionality */
#include <dlgs.h> /* includes common dialog template defines */
#include <cderr.h> /* includes the common dialog error codes */
/*
* This controls the use of the new style tk_chooseDirectory dialog.
*/
#define USE_NEW_CHOOSEDIR 1
#ifdef USE_NEW_CHOOSEDIR
#include <shlobj.h> /* includes SHBrowseForFolder */
/* These needed for compilation with VC++ 5.2 */
#ifndef BIF_EDITBOX
#define BIF_EDITBOX 0x10
#endif
#ifndef BIF_VALIDATE
#define BIF_VALIDATE 0x0020
#endif
#ifndef BFFM_VALIDATEFAILED
#ifdef UNICODE
#define BFFM_VALIDATEFAILED 4
#else
#define BFFM_VALIDATEFAILED 3
#endif
#endif
/*
* The following structure is used by the new Tk_ChooseDirectoryObjCmd
* to pass data between it and its callback. Unqiue to Winodws platform.
*/
typedef struct ChooseDirData {
TCHAR utfInitDir[MAX_PATH]; /* Initial folder to use */
TCHAR utfRetDir[MAX_PATH]; /* Returned folder to use */
Tcl_Interp *interp;
int mustExist; /* true if file must exist to return from
* callback */
} CHOOSEDIRDATA;
#endif
typedef struct ThreadSpecificData {
int debugFlag; /* Flags whether we should output debugging
* information while displaying a builtin
* dialog. */
Tcl_Interp *debugInterp; /* Interpreter to used for debugging. */
UINT WM_LBSELCHANGED; /* Holds a registered windows event used for
* communicating between the Directory
* Chooser dialog and its hook proc. */
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
/*
* The following structures are used by Tk_MessageBoxCmd() to parse
* arguments and return results.
*/
static const TkStateMap iconMap[] = {
{MB_ICONERROR, "error"},
{MB_ICONINFORMATION, "info"},
{MB_ICONQUESTION, "question"},
{MB_ICONWARNING, "warning"},
{-1, NULL}
};
static const TkStateMap typeMap[] = {
{MB_ABORTRETRYIGNORE, "abortretryignore"},
{MB_OK, "ok"},
{MB_OKCANCEL, "okcancel"},
{MB_RETRYCANCEL, "retrycancel"},
{MB_YESNO, "yesno"},
{MB_YESNOCANCEL, "yesnocancel"},
{-1, NULL}
};
static const TkStateMap buttonMap[] = {
{IDABORT, "abort"},
{IDRETRY, "retry"},
{IDIGNORE, "ignore"},
{IDOK, "ok"},
{IDCANCEL, "cancel"},
{IDNO, "no"},
{IDYES, "yes"},
{-1, NULL}
};
static const int buttonFlagMap[] = {
MB_DEFBUTTON1, MB_DEFBUTTON2, MB_DEFBUTTON3, MB_DEFBUTTON4
};
static const struct {int type; int btnIds[3];} allowedTypes[] = {
{MB_ABORTRETRYIGNORE, {IDABORT, IDRETRY, IDIGNORE}},
{MB_OK, {IDOK, -1, -1 }},
{MB_OKCANCEL, {IDOK, IDCANCEL, -1 }},
{MB_RETRYCANCEL, {IDRETRY, IDCANCEL, -1 }},
{MB_YESNO, {IDYES, IDNO, -1 }},
{MB_YESNOCANCEL, {IDYES, IDNO, IDCANCEL}}
};
#define NUM_TYPES (sizeof(allowedTypes) / sizeof(allowedTypes[0]))
/*
* The value of TK_MULTI_MAX_PATH dictactes how many files can
* be retrieved with tk_get*File -multiple 1. It must be allocated
* on the stack, so make it large enough but not too large. -- hobbs
* The data is stored as <dir>\0<file1>\0<file2>\0...<fileN>\0\0.
* MAX_PATH == 260 on Win2K/NT, so *40 is ~10K.
*/
#define TK_MULTI_MAX_PATH (MAX_PATH*40)
/*
* The following structure is used to pass information between the directory
* chooser procedure, Tk_ChooseDirectoryObjCmd(), and its dialog hook proc.
*/
typedef struct ChooseDir {
Tcl_Interp *interp; /* Interp, used only if debug is turned on,
* for setting the "tk_dialog" variable. */
int lastCtrl; /* Used by hook proc to keep track of last
* control that had input focus, so when OK
* is pressed we know whether to browse a
* new directory or return. */
int lastIdx; /* Last item that was selected in directory
* browser listbox. */
TCHAR path[MAX_PATH]; /* On return from choose directory dialog,
* holds the selected path. Cannot return
* selected path in ofnPtr->lpstrFile because
* the default dialog proc stores a '\0' in
* it, since, of course, no _file_ was
* selected. */
OPENFILENAME *ofnPtr; /* pointer to the OFN structure */
} ChooseDir;
/*
* Definitions of procedures used only in this file.
*/
#ifdef USE_NEW_CHOOSEDIR
static UINT APIENTRY ChooseDirectoryValidateProc(HWND hdlg, UINT uMsg,
LPARAM wParam, LPARAM lParam);
#else
static UINT APIENTRY ChooseDirectoryHookProc(HWND hdlg, UINT uMsg,
WPARAM wParam, LPARAM lParam);
#endif
static UINT CALLBACK ColorDlgHookProc(HWND hDlg, UINT uMsg, WPARAM wParam,
LPARAM lParam);
static int GetFileNameA(ClientData clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *CONST objv[], int isOpen);
static int GetFileNameW(ClientData clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *CONST objv[], int isOpen);
static int MakeFilter(Tcl_Interp *interp, char *string,
Tcl_DString *dsPtr);
static UINT APIENTRY OFNHookProc(HWND hdlg, UINT uMsg, WPARAM wParam,
LPARAM lParam);
static UINT APIENTRY OFNHookProcW(HWND hdlg, UINT uMsg, WPARAM wParam,
LPARAM lParam);
static void SetTkDialog(ClientData clientData);
/*
*-------------------------------------------------------------------------
*
* EatSpuriousMessageBugFix --
*
* In the file open/save dialog, double clicking on a list item
* causes the dialog box to close, but an unwanted WM_LBUTTONUP
* message is sent to the window underneath. If the window underneath
* happens to be a windows control (eg a button) then it will be
* activated by accident.
*
* This problem does not occur in dialog boxes, because windows
* must do some special processing to solve the problem. (separate
* message processing functions are used to cope with keyboard
* navigation of controls.)
*
* Here is one solution. After returning, we poll the message queue
* for 1/4s looking for WM_LBUTTON up messages. If we see one it's
* consumed. If we get a WM_LBUTTONDOWN message, then we exit early,
* since the user must be doing something new. This fix only works
* for the current application, so the problem will still occur if
* the open dialog happens to be over another applications button.
* However this is a fairly rare occurrance.
*
* Results:
* None.
*
* Side effects:
* Consumes an unwanted BUTTON messages.
*
*-------------------------------------------------------------------------
*/
static void EatSpuriousMessageBugFix( void )
{
MSG msg;
DWORD nTime = GetTickCount() + 250;
while( GetTickCount() < nTime )
{
if( PeekMessage(&msg,0,WM_LBUTTONDOWN,WM_LBUTTONDOWN,PM_NOREMOVE) )
break;
PeekMessage(&msg,0,WM_LBUTTONUP,WM_LBUTTONUP,PM_REMOVE);
}
}
/*
*-------------------------------------------------------------------------
*
* TkWinDialogDebug --
*
* Function to turn on/off debugging support for common dialogs under
* windows. The variable "tk_debug" is set to the identifier of the
* dialog window when the modal dialog window pops up and it is safe to
* send messages to the dialog.
*
* Results:
* None.
*
* Side effects:
* This variable only makes sense if just one dialog is up at a time.
*
*-------------------------------------------------------------------------
*/
void
TkWinDialogDebug(
int debug)
{
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
tsdPtr->debugFlag = debug;
}
/*
*-------------------------------------------------------------------------
*
* Tk_ChooseColorObjCmd --
*
* This procedure implements the color dialog box for the Windows
* platform. See the user documentation for details on what it
* does.
*
* Results:
* See user documentation.
*
* Side effects:
* A dialog window is created the first time this procedure is called.
* This window is not destroyed and will be reused the next time the
* application invokes the "tk_chooseColor" command.
*
*-------------------------------------------------------------------------
*/
int
Tk_ChooseColorObjCmd(clientData, interp, objc, objv)
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, parent;
HWND hWnd;
int i, oldMode, winCode, result;
CHOOSECOLOR chooseColor;
static int inited = 0;
static COLORREF dwCustColors[16];
static long oldColor; /* the color selected last time */
static CONST char *optionStrings[] = {
"-initialcolor", "-parent", "-title", NULL
};
enum options {
COLOR_INITIAL, COLOR_PARENT, COLOR_TITLE
};
result = TCL_OK;
if (inited == 0) {
/*
* dwCustColors stores the custom color which the user can
* modify. We store these colors in a static array so that the next
* time the color dialog pops up, the same set of custom colors
* remain in the dialog.
*/
for (i = 0; i < 16; i++) {
dwCustColors[i] = RGB(255-i * 10, i, i * 10);
}
oldColor = RGB(0xa0, 0xa0, 0xa0);
inited = 1;
}
tkwin = (Tk_Window) clientData;
parent = tkwin;
chooseColor.lStructSize = sizeof(CHOOSECOLOR);
chooseColor.hwndOwner = NULL;
chooseColor.hInstance = NULL;
chooseColor.rgbResult = oldColor;
chooseColor.lpCustColors = dwCustColors;
chooseColor.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ENABLEHOOK;
chooseColor.lCustData = (LPARAM) NULL;
chooseColor.lpfnHook = (LPOFNHOOKPROC) ColorDlgHookProc;
chooseColor.lpTemplateName = (LPTSTR) interp;
for (i = 1; i < objc; i += 2) {
int index;
char *string;
Tcl_Obj *optionPtr, *valuePtr;
optionPtr = objv[i];
valuePtr = objv[i + 1];
if (Tcl_GetIndexFromObj(interp, optionPtr, optionStrings, "option",
TCL_EXACT, &index) != TCL_OK) {
return TCL_ERROR;
}
if (i + 1 == objc) {
string = Tcl_GetStringFromObj(optionPtr, NULL);
Tcl_AppendResult(interp, "value for \"", string, "\" missing",
(char *) NULL);
return TCL_ERROR;
}
string = Tcl_GetStringFromObj(valuePtr, NULL);
switch ((enum options) index) {
case COLOR_INITIAL: {
XColor *colorPtr;
colorPtr = Tk_GetColor(interp, tkwin, string);
if (colorPtr == NULL) {
return TCL_ERROR;
}
chooseColor.rgbResult = RGB(colorPtr->red / 0x100,
colorPtr->green / 0x100, colorPtr->blue / 0x100);
break;
}
case COLOR_PARENT: {
parent = Tk_NameToWindow(interp, string, tkwin);
if (parent == NULL) {
return TCL_ERROR;
}
break;
}
case COLOR_TITLE: {
chooseColor.lCustData = (LPARAM) string;
break;
}
}
}
Tk_MakeWindowExist(parent);
chooseColor.hwndOwner = NULL;
hWnd = Tk_GetHWND(Tk_WindowId(parent));
chooseColor.hwndOwner = hWnd;
oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
winCode = ChooseColor(&chooseColor);
(void) Tcl_SetServiceMode(oldMode);
/*
* Ensure that hWnd is enabled, because it can happen that we
* have updated the wrapper of the parent, which causes us to
* leave this child disabled (Windows loses sync).
*/
EnableWindow(hWnd, 1);
/*
* Clear the interp result since anything may have happened during the
* modal loop.
*/
Tcl_ResetResult(interp);
/*
* 3. Process the result of the dialog
*/
if (winCode) {
/*
* User has selected a color
*/
char color[100];
sprintf(color, "#%02x%02x%02x",
GetRValue(chooseColor.rgbResult),
GetGValue(chooseColor.rgbResult),
GetBValue(chooseColor.rgbResult));
Tcl_AppendResult(interp, color, NULL);
oldColor = chooseColor.rgbResult;
result = TCL_OK;
}
return result;
}
/*
*-------------------------------------------------------------------------
*
* ColorDlgHookProc --
*
* Provides special handling of messages for the Color common dialog
* box. Used to set the title when the dialog first appears.
*
* Results:
* The return value is 0 if the default dialog box procedure should
* handle the message, non-zero otherwise.
*
* Side effects:
* Changes the title of the dialog window.
*
*----------------------------------------------------------------------
*/
static UINT CALLBACK
ColorDlgHookProc(hDlg, uMsg, wParam, lParam)
HWND hDlg; /* Handle to the color dialog. */
UINT uMsg; /* Type of message. */
WPARAM wParam; /* First message parameter. */
LPARAM lParam; /* Second message parameter. */
{
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
switch (uMsg) {
case WM_INITDIALOG: {
const char *title;
CHOOSECOLOR *ccPtr;
Tcl_DString ds;
/*
* Set the title string of the dialog.
*/
ccPtr = (CHOOSECOLOR *) lParam;
title = (const char *) ccPtr->lCustData;
if ((title != NULL) && (title[0] != '\0')) {
(*tkWinProcs->setWindowText)(hDlg,
Tcl_WinUtfToTChar(title, -1, &ds));
Tcl_DStringFree(&ds);
}
if (tsdPtr->debugFlag) {
tsdPtr->debugInterp = (Tcl_Interp *) ccPtr->lpTemplateName;
Tcl_DoWhenIdle(SetTkDialog, (ClientData) hDlg);
}
return TRUE;
}
}
return FALSE;
}
/*
*----------------------------------------------------------------------
*
* Tk_GetOpenFileCmd --
*
* This procedure implements the "open file" dialog box for the
* Windows platform. See the user documentation for details on what
* it does.
*
* Results:
* See user documentation.
*
* Side effects:
* A dialog window is created the first this procedure is called.
*
*----------------------------------------------------------------------
*/
int
Tk_GetOpenFileObjCmd(clientData, interp, objc, objv)
ClientData clientData; /* Main window associated with interpreter. */
Tcl_Interp *interp; /* Current interpreter. */
int objc; /* Number of arguments. */
Tcl_Obj *CONST objv[]; /* Argument objects. */
{
if (TkWinGetPlatformId() == VER_PLATFORM_WIN32_NT) {
return GetFileNameW(clientData, interp, objc, objv, 1);
} else {
return GetFileNameA(clientData, interp, objc, objv, 1);
}
}
/*
*----------------------------------------------------------------------
*
* Tk_GetSaveFileCmd --
*
* Same as Tk_GetOpenFileCmd but opens a "save file" dialog box
* instead
*
* Results:
* Same as Tk_GetOpenFileCmd.
*
* Side effects:
* Same as Tk_GetOpenFileCmd.
*
*----------------------------------------------------------------------
*/
int
Tk_GetSaveFileObjCmd(clientData, interp, objc, objv)
ClientData clientData; /* Main window associated with interpreter. */
Tcl_Interp *interp; /* Current interpreter. */
int objc; /* Number of arguments. */
Tcl_Obj *CONST objv[]; /* Argument objects. */
{
if (TkWinGetPlatformId() == VER_PLATFORM_WIN32_NT) {
return GetFileNameW(clientData, interp, objc, objv, 0);
} else {
return GetFileNameA(clientData, interp, objc, objv, 0);
}
}
/*
*----------------------------------------------------------------------
*
* GetFileNameW --
*
* Calls GetOpenFileName() or GetSaveFileName().
*
* Results:
* See user documentation.
*
* Side effects:
* See user documentation.
*
*----------------------------------------------------------------------
*/
static int
GetFileNameW(clientData, interp, objc, objv, open)
ClientData clientData; /* Main window associated with interpreter. */
Tcl_Interp *interp; /* Current interpreter. */
int objc; /* Number of arguments. */
Tcl_Obj *CONST objv[]; /* Argument objects. */
int open; /* 1 to call GetOpenFileName(), 0 to
* call GetSaveFileName(). */
{
OPENFILENAMEW ofn;
WCHAR file[TK_MULTI_MAX_PATH];
int result, winCode, oldMode, i, multi = 0;
char *extension, *filter, *title;
Tk_Window tkwin;
HWND hWnd;
Tcl_DString utfFilterString, utfDirString;
Tcl_DString extString, filterString, dirString, titleString;
Tcl_Encoding unicodeEncoding = TkWinGetUnicodeEncoding();
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
static CONST char *saveOptionStrings[] = {
"-defaultextension", "-filetypes", "-initialdir", "-initialfile",
"-parent", "-title", NULL
};
static CONST char *openOptionStrings[] = {
"-defaultextension", "-filetypes", "-initialdir", "-initialfile",
"-multiple", "-parent", "-title", NULL
};
CONST char **optionStrings;
enum options {
FILE_DEFAULT, FILE_TYPES, FILE_INITDIR, FILE_INITFILE,
FILE_MULTIPLE, FILE_PARENT, FILE_TITLE
};
result = TCL_ERROR;
file[0] = '\0';
/*
* Parse the arguments.
*/
extension = NULL;
filter = NULL;
Tcl_DStringInit(&utfFilterString);
Tcl_DStringInit(&utfDirString);
tkwin = (Tk_Window) clientData;
title = NULL;
if (open) {
optionStrings = openOptionStrings;
} else {
optionStrings = saveOptionStrings;
}
for (i = 1; i < objc; i += 2) {
int index;
char *string;
Tcl_Obj *optionPtr, *valuePtr;
optionPtr = objv[i];
valuePtr = objv[i + 1];
if (Tcl_GetIndexFromObj(interp, optionPtr, optionStrings,
"option", 0, &index) != TCL_OK) {
goto end;
}
/*
* We want to maximize code sharing between the open and save file
* dialog implementations; in particular, the switch statement below.
* We use different sets of option strings from the GetIndexFromObj
* call above, but a single enumeration for both. The save file
* dialog doesn't support -multiple, but it falls in the middle of
* the enumeration. Ultimately, this means that when the index found
* by GetIndexFromObj is >= FILE_MULTIPLE, when doing a save file
* dialog, we have to increment the index, so that it matches the
* open file dialog enumeration.
*/
if (!open && index >= FILE_MULTIPLE) {
index++;
}
if (i + 1 == objc) {
string = Tcl_GetStringFromObj(optionPtr, NULL);
Tcl_AppendResult(interp, "value for \"", string, "\" missing",
(char *) NULL);
goto end;
}
string = Tcl_GetStringFromObj(valuePtr, NULL);
switch ((enum options) index) {
case FILE_DEFAULT: {
if (string[0] == '.') {
string++;
}
extension = string;
break;
}
case FILE_TYPES: {
Tcl_DStringFree(&utfFilterString);
if (MakeFilter(interp, string, &utfFilterString) != TCL_OK) {
goto end;
}
filter = Tcl_DStringValue(&utfFilterString);
break;
}
case FILE_INITDIR: {
Tcl_DStringFree(&utfDirString);
if (Tcl_TranslateFileName(interp, string,
&utfDirString) == NULL) {
goto end;
}
break;
}
case FILE_INITFILE: {
Tcl_DString ds;
if (Tcl_TranslateFileName(interp, string, &ds) == NULL) {
goto end;
}
Tcl_UtfToExternal(NULL, unicodeEncoding, Tcl_DStringValue(&ds),
Tcl_DStringLength(&ds), 0, NULL, (char *) file,
sizeof(file), NULL, NULL, NULL);
break;
}
case FILE_MULTIPLE: {
if (Tcl_GetBooleanFromObj(interp, valuePtr,
&multi) != TCL_OK) {
return TCL_ERROR;
}
break;
}
case FILE_PARENT: {
tkwin = Tk_NameToWindow(interp, string, tkwin);
if (tkwin == NULL) {
goto end;
}
break;
}
case FILE_TITLE: {
title = string;
break;
}
}
}
if (filter == NULL) {
if (MakeFilter(interp, "", &utfFilterString) != TCL_OK) {
goto end;
}
}
Tk_MakeWindowExist(tkwin);
hWnd = Tk_GetHWND(Tk_WindowId(tkwin));
ZeroMemory(&ofn, sizeof(OPENFILENAMEW));
ofn.lStructSize = sizeof(OPENFILENAMEW);
ofn.hwndOwner = hWnd;
#ifdef _WIN64
ofn.hInstance = (HINSTANCE) GetWindowLongPtr(ofn.hwndOwner,
GWLP_HINSTANCE);
#else
ofn.hInstance = (HINSTANCE) GetWindowLong(ofn.hwndOwner,
GWL_HINSTANCE);
#endif
ofn.lpstrFile = (WCHAR *) file;
ofn.nMaxFile = TK_MULTI_MAX_PATH;
ofn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST
| OFN_NOCHANGEDIR | OFN_EXPLORER;
ofn.lpfnHook = (LPOFNHOOKPROC) OFNHookProcW;
ofn.lCustData = (LPARAM) interp;
if (open != 0) {
ofn.Flags |= OFN_FILEMUSTEXIST;
} else {
ofn.Flags |= OFN_OVERWRITEPROMPT;
}
if (tsdPtr->debugFlag != 0) {
ofn.Flags |= OFN_ENABLEHOOK;
}
if (multi != 0) {
ofn.Flags |= OFN_ALLOWMULTISELECT;
}
if (extension != NULL) {
Tcl_UtfToExternalDString(unicodeEncoding, extension, -1, &extString);
ofn.lpstrDefExt = (WCHAR *) Tcl_DStringValue(&extString);
}
Tcl_UtfToExternalDString(unicodeEncoding,
Tcl_DStringValue(&utfFilterString),
Tcl_DStringLength(&utfFilterString), &filterString);
ofn.lpstrFilter = (WCHAR *) Tcl_DStringValue(&filterString);
if (Tcl_DStringValue(&utfDirString)[0] != '\0') {
Tcl_UtfToExternalDString(unicodeEncoding,
Tcl_DStringValue(&utfDirString),
Tcl_DStringLength(&utfDirString), &dirString);
} else {
/*
* NT 5.0 changed the meaning of lpstrInitialDir, so we have
* to ensure that we set the [pwd] if the user didn't specify
* anything else.
*/
Tcl_DString cwd;
Tcl_DStringFree(&utfDirString);
if ((Tcl_GetCwd(interp, &utfDirString) == (char *) NULL) ||
(Tcl_TranslateFileName(interp,
Tcl_DStringValue(&utfDirString), &cwd) == NULL)) {
Tcl_ResetResult(interp);
} else {
Tcl_UtfToExternalDString(unicodeEncoding, Tcl_DStringValue(&cwd),
Tcl_DStringLength(&cwd), &dirString);
}
Tcl_DStringFree(&cwd);
}
ofn.lpstrInitialDir = (WCHAR *) Tcl_DStringValue(&dirString);
if (title != NULL) {
Tcl_UtfToExternalDString(unicodeEncoding, title, -1, &titleString);
ofn.lpstrTitle = (WCHAR *) Tcl_DStringValue(&titleString);
}
/*
* Popup the dialog.
*/
oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
if (open != 0) {
winCode = GetOpenFileNameW(&ofn);
} else {
winCode = GetSaveFileNameW(&ofn);
}
Tcl_SetServiceMode(oldMode);
EatSpuriousMessageBugFix();
/*
* Ensure that hWnd is enabled, because it can happen that we
* have updated the wrapper of the parent, which causes us to
* leave this child disabled (Windows loses sync).
*/
EnableWindow(hWnd, 1);
/*
* Clear the interp result since anything may have happened during the
* modal loop.
*/
Tcl_ResetResult(interp);
/*
* Process the results.
*/
if (winCode != 0) {
if (ofn.Flags & OFN_ALLOWMULTISELECT) {
/*
* The result in custData->szFile contains many items,
* separated with null characters. It is terminated with
* two nulls in a row. The first element is the directory
* path.
*/
char *dir;
char *p;
char *file;
WCHAR *files;
Tcl_DString ds;
Tcl_DString fullname, filename;
Tcl_Obj *returnList;
int count = 0;
returnList = Tcl_NewObj();
Tcl_IncrRefCount(returnList);
files = ofn.lpstrFile;
Tcl_ExternalToUtfDString(unicodeEncoding, (char *) files, -1, &ds);
/* Get directory */
dir = Tcl_DStringValue(&ds);
for (p = dir; p && *p; p++) {
/*
* Change the pathname to the Tcl "normalized" pathname, where
* back slashes are used instead of forward slashes
*/
if (*p == '\\') {
*p = '/';
}
}
while (*files != '\0') {
while (*files != '\0') {
files++;
}
files++;
if (*files != '\0') {
count++;
Tcl_ExternalToUtfDString(unicodeEncoding,
(char *)files, -1, &filename);
file = Tcl_DStringValue(&filename);
for (p = file; *p != '\0'; p++) {
if (*p == '\\') {
*p = '/';
}
}
Tcl_DStringInit(&fullname);
Tcl_DStringAppend(&fullname, dir, -1);
Tcl_DStringAppend(&fullname, "/", -1);
Tcl_DStringAppend(&fullname, file, -1);
Tcl_ListObjAppendElement(interp, returnList,
Tcl_NewStringObj(Tcl_DStringValue(&fullname), -1));
Tcl_DStringFree(&fullname);
Tcl_DStringFree(&filename);
}
}
if (count == 0) {
/*
* Only one file was returned.
*/
Tcl_ListObjAppendElement(interp, returnList,
Tcl_NewStringObj(dir, -1));
}
Tcl_SetObjResult(interp, returnList);
Tcl_DecrRefCount(returnList);
Tcl_DStringFree(&ds);
} else {
char *p;
Tcl_DString ds;
Tcl_ExternalToUtfDString(unicodeEncoding,
(char *) ofn.lpstrFile, -1, &ds);
for (p = Tcl_DStringValue(&ds); *p != '\0'; p++) {
/*
* Change the pathname to the Tcl "normalized" pathname, where
* back slashes are used instead of forward slashes
*/
if (*p == '\\') {
*p = '/';
}
}
Tcl_AppendResult(interp, Tcl_DStringValue(&ds), NULL);
Tcl_DStringFree(&ds);
}
result = TCL_OK;
} else {
/*
* Use the CommDlgExtendedError() function to retrieve the error code.
* This function can return one of about two dozen codes; most of
* these indicate some sort of gross system failure (insufficient
* memory, bad window handles, etc.). Most of the error codes will be
* ignored; as we find we want more specific error messages for
* particular errors, we can extend the code as needed.
*
* We could also check for FNERR_BUFFERTOOSMALL, but we can't
* really do anything about it when it happens.
*/
if (CommDlgExtendedError() == FNERR_INVALIDFILENAME) {
char *p;
Tcl_DString ds;
Tcl_ExternalToUtfDString(unicodeEncoding,
(char *) ofn.lpstrFile, -1, &ds);
for (p = Tcl_DStringValue(&ds); *p != '\0'; p++) {
/*
* Change the pathname to the Tcl "normalized" pathname,
* where back slashes are used instead of forward slashes
*/
if (*p == '\\') {
*p = '/';
}
}
Tcl_SetResult(interp, "invalid filename \"", TCL_STATIC);
Tcl_AppendResult(interp, Tcl_DStringValue(&ds), "\"", NULL);
Tcl_DStringFree(&ds);
} else {
result = TCL_OK;
}
}
if (ofn.lpstrTitle != NULL) {
Tcl_DStringFree(&titleString);
}
if (ofn.lpstrInitialDir != NULL) {
Tcl_DStringFree(&dirString);
}
Tcl_DStringFree(&filterString);
if (ofn.lpstrDefExt != NULL) {
Tcl_DStringFree(&extString);
}
end:
Tcl_DStringFree(&utfDirString);
Tcl_DStringFree(&utfFilterString);
return result;
}
/*
*-------------------------------------------------------------------------
*
* OFNHookProcW --
*
* Hook procedure called only if debugging is turned on. Sets
* the "tk_dialog" variable when the dialog is ready to receive
* messages.
*
* Results:
* Returns 0 to allow default processing of messages to occur.
*
* Side effects:
* None.
*
*-------------------------------------------------------------------------
*/
static UINT APIENTRY
OFNHookProcW(
HWND hdlg, // handle to child dialog window
UINT uMsg, // message identifier
WPARAM wParam, // message parameter
LPARAM lParam) // message parameter
{
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
OPENFILENAMEW *ofnPtr;
if (uMsg == WM_INITDIALOG) {
#ifdef _WIN64
SetWindowLongPtr(hdlg, GWLP_USERDATA, lParam);
#else
SetWindowLong(hdlg, GWL_USERDATA, lParam);
#endif
} else if (uMsg == WM_WINDOWPOSCHANGED) {
/*
* This message is delivered at the right time to enable Tk
* to set the debug information. Unhooks itself so it
* won't set the debug information every time it gets a
* WM_WINDOWPOSCHANGED message.
*/
#ifdef _WIN64
ofnPtr = (OPENFILENAMEW *) GetWindowLongPtr(hdlg, GWLP_USERDATA);
#else
ofnPtr = (OPENFILENAMEW *) GetWindowLong(hdlg, GWL_USERDATA);
#endif
if (ofnPtr != NULL) {
hdlg = GetParent(hdlg);
tsdPtr->debugInterp = (Tcl_Interp *) ofnPtr->lCustData;
Tcl_DoWhenIdle(SetTkDialog, (ClientData) hdlg);
#ifdef _WIN64