Debian Codegraph @

pTk/mTk/win/tkWinDialog.c

raw
1 /*
2 * tkWinDialog.c --
3 *
4 * Contains the Windows implementation of the common dialog boxes.
5 *
6 * Copyright (c) 1996-1997 Sun Microsystems, Inc.
7 *
8 * See the file "license.terms" for information on usage and redistribution
9 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 *
11 * RCS: @(#) $Id: tkWinDialog.c,v 1.30.2.1 2003/07/18 19:51:35 hobbs Exp $
12 *
13 */
14
15 #include "tkWinInt.h"
16 #include "tkFileFilter.h"
17
18 #ifndef __OPEN32__
19 #include <commdlg.h> /* includes common dialog functionality */
20 #include <dlgs.h> /* includes common dialog template defines */
21 #include <cderr.h> /* includes the common dialog error codes */
22 #endif
23
24 /*
25 * This controls the use of the new style tk_chooseDirectory dialog.
26 */
27 #define USE_NEW_CHOOSEDIR 1
28 #ifdef USE_NEW_CHOOSEDIR
29 #include <shlobj.h> /* includes SHBrowseForFolder */
30
31 /* These needed for compilation with VC++ 5.2 */
32 #ifndef BIF_EDITBOX
33 #define BIF_EDITBOX 0x10
34 #endif
35 #ifndef BIF_VALIDATE
36 #define BIF_VALIDATE 0x0020
37 #endif
38 #ifndef BFFM_VALIDATEFAILED
39 #ifdef UNICODE
40 #define BFFM_VALIDATEFAILED 4
41 #else
42 #define BFFM_VALIDATEFAILED 3
43 #endif
44 #endif
45
46 /*
47 * The following structure is used by the new Tk_ChooseDirectoryObjCmd
48 * to pass data between it and its callback. Unqiue to Winodws platform.
49 */
50 typedef struct ChooseDirData {
51 TCHAR utfInitDir[MAX_PATH]; /* Initial folder to use */
52 TCHAR utfRetDir[MAX_PATH]; /* Returned folder to use */
53 Tcl_Interp *interp;
54 int mustExist; /* true if file must exist to return from
55 * callback */
56 } CHOOSEDIRDATA;
57 #endif
58
59 typedef struct ThreadSpecificData {
60 int debugFlag; /* Flags whether we should output debugging
61 * information while displaying a builtin
62 * dialog. */
63 Tcl_Interp *debugInterp; /* Interpreter to used for debugging. */
64 UINT WM_LBSELCHANGED; /* Holds a registered windows event used for
65 * communicating between the Directory
66 * Chooser dialog and its hook proc. */
67 } ThreadSpecificData;
68 static Tcl_ThreadDataKey dataKey;
69
70 /*
71 * The following structures are used by Tk_MessageBoxCmd() to parse
72 * arguments and return results.
73 */
74
75 static const TkStateMap iconMap[] = {
76 {MB_ICONERROR, "error"},
77 {MB_ICONINFORMATION, "info"},
78 {MB_ICONQUESTION, "question"},
79 {MB_ICONWARNING, "warning"},
80 {-1, NULL}
81 };
82
83 static const TkStateMap typeMap[] = {
84 {MB_ABORTRETRYIGNORE, "abortretryignore"},
85 {MB_OK, "ok"},
86 {MB_OKCANCEL, "okcancel"},
87 {MB_RETRYCANCEL, "retrycancel"},
88 {MB_YESNO, "yesno"},
89 {MB_YESNOCANCEL, "yesnocancel"},
90 {-1, NULL}
91 };
92
93 static const TkStateMap buttonMap[] = {
94 {IDABORT, "abort"},
95 {IDRETRY, "retry"},
96 {IDIGNORE, "ignore"},
97 {IDOK, "ok"},
98 {IDCANCEL, "cancel"},
99 {IDNO, "no"},
100 {IDYES, "yes"},
101 {-1, NULL}
102 };
103
104 static const int buttonFlagMap[] = {
105 MB_DEFBUTTON1, MB_DEFBUTTON2, MB_DEFBUTTON3, MB_DEFBUTTON4
106 };
107
108 static const struct {int type; int btnIds[3];} allowedTypes[] = {
109 {MB_ABORTRETRYIGNORE, {IDABORT, IDRETRY, IDIGNORE}},
110 {MB_OK, {IDOK, -1, -1 }},
111 {MB_OKCANCEL, {IDOK, IDCANCEL, -1 }},
112 {MB_RETRYCANCEL, {IDRETRY, IDCANCEL, -1 }},
113 {MB_YESNO, {IDYES, IDNO, -1 }},
114 {MB_YESNOCANCEL, {IDYES, IDNO, IDCANCEL}}
115 };
116
117 #define NUM_TYPES (sizeof(allowedTypes) / sizeof(allowedTypes[0]))
118
119 /*
120 * The value of TK_MULTI_MAX_PATH dictactes how many files can
121 * be retrieved with tk_get*File -multiple 1. It must be allocated
122 * on the stack, so make it large enough but not too large. -- hobbs
123 * The data is stored as <dir>\0<file1>\0<file2>\0...<fileN>\0\0.
124 * MAX_PATH == 260 on Win2K/NT, so *40 is ~10K.
125 */
126
127 #define TK_MULTI_MAX_PATH (MAX_PATH*40)
128
129 /*
130 * The following structure is used to pass information between the directory
131 * chooser procedure, Tk_ChooseDirectoryObjCmd(), and its dialog hook proc.
132 */
133
134 typedef struct ChooseDir {
135 Tcl_Interp *interp; /* Interp, used only if debug is turned on,
136 * for setting the "tk_dialog" variable. */
137 int lastCtrl; /* Used by hook proc to keep track of last
138 * control that had input focus, so when OK
139 * is pressed we know whether to browse a
140 * new directory or return. */
141 int lastIdx; /* Last item that was selected in directory
142 * browser listbox. */
143 TCHAR path[MAX_PATH]; /* On return from choose directory dialog,
144 * holds the selected path. Cannot return
145 * selected path in ofnPtr->lpstrFile because
146 * the default dialog proc stores a '\0' in
147 * it, since, of course, no _file_ was
148 * selected. */
149 OPENFILENAME *ofnPtr; /* pointer to the OFN structure */
150 } ChooseDir;
151
152 /*
153 * Definitions of procedures used only in this file.
154 */
155
156 #ifdef USE_NEW_CHOOSEDIR
157 static UINT APIENTRY ChooseDirectoryValidateProc(HWND hdlg, UINT uMsg,
158 LPARAM wParam, LPARAM lParam);
159 #else
160 static UINT APIENTRY ChooseDirectoryHookProc(HWND hdlg, UINT uMsg,
161 WPARAM wParam, LPARAM lParam);
162 #endif
163 static UINT CALLBACK ColorDlgHookProc(HWND hDlg, UINT uMsg, WPARAM wParam,
164 LPARAM lParam);
165 static int GetFileNameA(ClientData clientData,
166 Tcl_Interp *interp, int objc,
167 Tcl_Obj *CONST objv[], int isOpen);
168 static int GetFileNameW(ClientData clientData,
169 Tcl_Interp *interp, int objc,
170 Tcl_Obj *CONST objv[], int isOpen);
171 static int MakeFilter(Tcl_Interp *interp, char *string,
172 Tcl_DString *dsPtr);
173 static UINT APIENTRY OFNHookProc(HWND hdlg, UINT uMsg, WPARAM wParam,
174 LPARAM lParam);
175 static UINT APIENTRY OFNHookProcW(HWND hdlg, UINT uMsg, WPARAM wParam,
176 LPARAM lParam);
177 static void SetTkDialog(ClientData clientData);
178
179 /*
180 *-------------------------------------------------------------------------
181 *
182 * TkWinDialogDebug --
183 *
184 * Function to turn on/off debugging support for common dialogs under
185 * windows. The variable "tk_debug" is set to the identifier of the
186 * dialog window when the modal dialog window pops up and it is safe to
187 * send messages to the dialog.
188 *
189 * Results:
190 * None.
191 *
192 * Side effects:
193 * This variable only makes sense if just one dialog is up at a time.
194 *
195 *-------------------------------------------------------------------------
196 */
197
198 void
199 TkWinDialogDebug(
200 int debug)
201 {
202 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
203 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
204
205 tsdPtr->debugFlag = debug;
206 }
207
208 /*
209 *-------------------------------------------------------------------------
210 *
211 * Tk_ChooseColorObjCmd --
212 *
213 * This procedure implements the color dialog box for the Windows
214 * platform. See the user documentation for details on what it
215 * does.
216 *
217 * Results:
218 * See user documentation.
219 *
220 * Side effects:
221 * A dialog window is created the first time this procedure is called.
222 * This window is not destroyed and will be reused the next time the
223 * application invokes the "tk_chooseColor" command.
224 *
225 *-------------------------------------------------------------------------
226 */
227
228 int
229 Tk_ChooseColorObjCmd(clientData, interp, objc, objv)
230 ClientData clientData; /* Main window associated with interpreter. */
231 Tcl_Interp *interp; /* Current interpreter. */
232 int objc; /* Number of arguments. */
233 Tcl_Obj *CONST objv[]; /* Argument objects. */
234 {
235 Tk_Window tkwin, parent;
236 HWND hWnd;
237 int i, oldMode, winCode, result;
238 CHOOSECOLOR chooseColor;
239 static int inited = 0;
240 static COLORREF dwCustColors[16];
241 static long oldColor; /* the color selected last time */
242 static CONST char *optionStrings[] = {
243 "-initialcolor", "-parent", "-title", NULL
244 };
245 enum options {
246 COLOR_INITIAL, COLOR_PARENT, COLOR_TITLE
247 };
248
249 result = TCL_OK;
250 if (inited == 0) {
251 /*
252 * dwCustColors stores the custom color which the user can
253 * modify. We store these colors in a static array so that the next
254 * time the color dialog pops up, the same set of custom colors
255 * remain in the dialog.
256 */
257 for (i = 0; i < 16; i++) {
258 dwCustColors[i] = RGB(255-i * 10, i, i * 10);
259 }
260 oldColor = RGB(0xa0, 0xa0, 0xa0);
261 inited = 1;
262 }
263
264 tkwin = (Tk_Window) clientData;
265
266 parent = tkwin;
267 chooseColor.lStructSize = sizeof(CHOOSECOLOR);
268 chooseColor.hwndOwner = NULL;
269 chooseColor.hInstance = NULL;
270 chooseColor.rgbResult = oldColor;
271 chooseColor.lpCustColors = dwCustColors;
272 chooseColor.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ENABLEHOOK;
273 chooseColor.lCustData = (LPARAM) NULL;
274 chooseColor.lpfnHook = (LPOFNHOOKPROC) ColorDlgHookProc;
275 chooseColor.lpTemplateName = (LPTSTR) interp;
276
277 for (i = 1; i < objc; i += 2) {
278 int index;
279 char *string;
280 Tcl_Obj *optionPtr, *valuePtr;
281
282 optionPtr = objv[i];
283 valuePtr = objv[i + 1];
284
285 if (Tcl_GetIndexFromObj(interp, optionPtr, optionStrings, "option",
286 TCL_EXACT, &index) != TCL_OK) {
287 return TCL_ERROR;
288 }
289 if (i + 1 == objc) {
290 string = Tcl_GetStringFromObj(optionPtr, NULL);
291 Tcl_AppendResult(interp, "value for \"", string, "\" missing",
292 (char *) NULL);
293 return TCL_ERROR;
294 }
295
296 string = Tcl_GetStringFromObj(valuePtr, NULL);
297 switch ((enum options) index) {
298 case COLOR_INITIAL: {
299 XColor *colorPtr;
300
301 colorPtr = Tk_GetColor(interp, tkwin, string);
302 if (colorPtr == NULL) {
303 return TCL_ERROR;
304 }
305 chooseColor.rgbResult = RGB(colorPtr->red / 0x100,
306 colorPtr->green / 0x100, colorPtr->blue / 0x100);
307 break;
308 }
309 case COLOR_PARENT: {
310 parent = Tk_NameToWindow(interp, string, tkwin);
311 if (parent == NULL) {
312 return TCL_ERROR;
313 }
314 break;
315 }
316 case COLOR_TITLE: {
317 chooseColor.lCustData = (LPARAM) string;
318 break;
319 }
320 }
321 }
322
323 Tk_MakeWindowExist(parent);
324 chooseColor.hwndOwner = NULL;
325 hWnd = Tk_GetHWND(Tk_WindowId(parent));
326 chooseColor.hwndOwner = hWnd;
327
328 oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
329 winCode = ChooseColor(&chooseColor);
330 (void) Tcl_SetServiceMode(oldMode);
331
332 /*
333 * Ensure that hWnd is enabled, because it can happen that we
334 * have updated the wrapper of the parent, which causes us to
335 * leave this child disabled (Windows loses sync).
336 */
337 EnableWindow(hWnd, 1);
338
339 /*
340 * Clear the interp result since anything may have happened during the
341 * modal loop.
342 */
343
344 Tcl_ResetResult(interp);
345
346 /*
347 * 3. Process the result of the dialog
348 */
349
350 if (winCode) {
351 /*
352 * User has selected a color
353 */
354 char color[100];
355
356 sprintf(color, "#%02x%02x%02x",
357 GetRValue(chooseColor.rgbResult),
358 GetGValue(chooseColor.rgbResult),
359 GetBValue(chooseColor.rgbResult));
360 Tcl_AppendResult(interp, color, NULL);
361 oldColor = chooseColor.rgbResult;
362 result = TCL_OK;
363 }
364
365 return result;
366 }
367
368 /*
369 *-------------------------------------------------------------------------
370 *
371 * ColorDlgHookProc --
372 *
373 * Provides special handling of messages for the Color common dialog
374 * box. Used to set the title when the dialog first appears.
375 *
376 * Results:
377 * The return value is 0 if the default dialog box procedure should
378 * handle the message, non-zero otherwise.
379 *
380 * Side effects:
381 * Changes the title of the dialog window.
382 *
383 *----------------------------------------------------------------------
384 */
385
386 static UINT CALLBACK
387 ColorDlgHookProc(hDlg, uMsg, wParam, lParam)
388 HWND hDlg; /* Handle to the color dialog. */
389 UINT uMsg; /* Type of message. */
390 WPARAM wParam; /* First message parameter. */
391 LPARAM lParam; /* Second message parameter. */
392 {
393 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
394 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
395
396 switch (uMsg) {
397 case WM_INITDIALOG: {
398 const char *title;
399 CHOOSECOLOR *ccPtr;
400 Tcl_DString ds;
401
402 /*
403 * Set the title string of the dialog.
404 */
405
406 ccPtr = (CHOOSECOLOR *) lParam;
407 title = (const char *) ccPtr->lCustData;
408 if ((title != NULL) && (title[0] != '\0')) {
409 (*tkWinProcs->setWindowText)(hDlg,
410 Tcl_WinUtfToTChar(title, -1, &ds));
411 Tcl_DStringFree(&ds);
412 }
413 if (tsdPtr->debugFlag) {
414 tsdPtr->debugInterp = (Tcl_Interp *) ccPtr->lpTemplateName;
415 Tcl_DoWhenIdle(SetTkDialog, (ClientData) hDlg);
416 }
417 return TRUE;
418 }
419 }
420 return FALSE;
421 }
422
423 /*
424 *----------------------------------------------------------------------
425 *
426 * Tk_GetOpenFileCmd --
427 *
428 * This procedure implements the "open file" dialog box for the
429 * Windows platform. See the user documentation for details on what
430 * it does.
431 *
432 * Results:
433 * See user documentation.
434 *
435 * Side effects:
436 * A dialog window is created the first this procedure is called.
437 *
438 *----------------------------------------------------------------------
439 */
440
441 int
442 Tk_GetOpenFileObjCmd(clientData, interp, objc, objv)
443 ClientData clientData; /* Main window associated with interpreter. */
444 Tcl_Interp *interp; /* Current interpreter. */
445 int objc; /* Number of arguments. */
446 Tcl_Obj *CONST objv[]; /* Argument objects. */
447 {
448 if (TkWinGetPlatformId() == VER_PLATFORM_WIN32_NT) {
449 return GetFileNameW(clientData, interp, objc, objv, 1);
450 } else {
451 return GetFileNameA(clientData, interp, objc, objv, 1);
452 }
453 }
454
455 /*
456 *----------------------------------------------------------------------
457 *
458 * Tk_GetSaveFileCmd --
459 *
460 * Same as Tk_GetOpenFileCmd but opens a "save file" dialog box
461 * instead
462 *
463 * Results:
464 * Same as Tk_GetOpenFileCmd.
465 *
466 * Side effects:
467 * Same as Tk_GetOpenFileCmd.
468 *
469 *----------------------------------------------------------------------
470 */
471
472 int
473 Tk_GetSaveFileObjCmd(clientData, interp, objc, objv)
474 ClientData clientData; /* Main window associated with interpreter. */
475 Tcl_Interp *interp; /* Current interpreter. */
476 int objc; /* Number of arguments. */
477 Tcl_Obj *CONST objv[]; /* Argument objects. */
478 {
479 if (TkWinGetPlatformId() == VER_PLATFORM_WIN32_NT) {
480 return GetFileNameW(clientData, interp, objc, objv, 0);
481 } else {
482 return GetFileNameA(clientData, interp, objc, objv, 0);
483 }
484 }
485
486 /*
487 *----------------------------------------------------------------------
488 *
489 * GetFileNameW --
490 *
491 * Calls GetOpenFileName() or GetSaveFileName().
492 *
493 * Results:
494 * See user documentation.
495 *
496 * Side effects:
497 * See user documentation.
498 *
499 *----------------------------------------------------------------------
500 */
501
502 static int
503 GetFileNameW(clientData, interp, objc, objv, open)
504 ClientData clientData; /* Main window associated with interpreter. */
505 Tcl_Interp *interp; /* Current interpreter. */
506 int objc; /* Number of arguments. */
507 Tcl_Obj *CONST objv[]; /* Argument objects. */
508 int open; /* 1 to call GetOpenFileName(), 0 to
509 * call GetSaveFileName(). */
510 {
511 OPENFILENAMEW ofn;
512 WCHAR file[TK_MULTI_MAX_PATH];
513 int result, winCode, oldMode, i, multi = 0;
514 char *extension, *filter, *title;
515 Tk_Window tkwin;
516 HWND hWnd;
517 Tcl_DString utfFilterString, utfDirString;
518 Tcl_DString extString, filterString, dirString, titleString;
519 Tcl_Encoding unicodeEncoding = TkWinGetUnicodeEncoding();
520 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
521 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
522 static CONST char *saveOptionStrings[] = {
523 "-defaultextension", "-filetypes", "-initialdir", "-initialfile",
524 "-parent", "-title", NULL
525 };
526 static CONST char *openOptionStrings[] = {
527 "-defaultextension", "-filetypes", "-initialdir", "-initialfile",
528 "-multiple", "-parent", "-title", NULL
529 };
530 CONST char **optionStrings;
531
532 enum options {
533 FILE_DEFAULT, FILE_TYPES, FILE_INITDIR, FILE_INITFILE,
534 FILE_MULTIPLE, FILE_PARENT, FILE_TITLE
535 };
536
537 result = TCL_ERROR;
538 file[0] = '\0';
539
540 /*
541 * Parse the arguments.
542 */
543
544 extension = NULL;
545 filter = NULL;
546 Tcl_DStringInit(&utfFilterString);
547 Tcl_DStringInit(&utfDirString);
548 tkwin = (Tk_Window) clientData;
549 title = NULL;
550
551 if (open) {
552 optionStrings = openOptionStrings;
553 } else {
554 optionStrings = saveOptionStrings;
555 }
556
557 for (i = 1; i < objc; i += 2) {
558 int index;
559 char *string;
560 Tcl_Obj *optionPtr, *valuePtr;
561
562 optionPtr = objv[i];
563 valuePtr = objv[i + 1];
564
565 if (Tcl_GetIndexFromObj(interp, optionPtr, optionStrings,
566 "option", 0, &index) != TCL_OK) {
567 goto end;
568 }
569 /*
570 * We want to maximize code sharing between the open and save file
571 * dialog implementations; in particular, the switch statement below.
572 * We use different sets of option strings from the GetIndexFromObj
573 * call above, but a single enumeration for both. The save file
574 * dialog doesn't support -multiple, but it falls in the middle of
575 * the enumeration. Ultimately, this means that when the index found
576 * by GetIndexFromObj is >= FILE_MULTIPLE, when doing a save file
577 * dialog, we have to increment the index, so that it matches the
578 * open file dialog enumeration.
579 */
580 if (!open && index >= FILE_MULTIPLE) {
581 index++;
582 }
583 if (i + 1 == objc) {
584 string = Tcl_GetStringFromObj(optionPtr, NULL);
585 Tcl_AppendResult(interp, "value for \"", string, "\" missing",
586 (char *) NULL);
587 goto end;
588 }
589
590 string = Tcl_GetStringFromObj(valuePtr, NULL);
591 switch ((enum options) index) {
592 case FILE_DEFAULT: {
593 if (string[0] == '.') {
594 string++;
595 }
596 extension = string;
597 break;
598 }
599 case FILE_TYPES: {
600 Tcl_DStringFree(&utfFilterString);
601 if (MakeFilter(interp, string, &utfFilterString) != TCL_OK) {
602 goto end;
603 }
604 filter = Tcl_DStringValue(&utfFilterString);
605 break;
606 }
607 case FILE_INITDIR: {
608 Tcl_DStringFree(&utfDirString);
609 if (Tcl_TranslateFileName(interp, string,
610 &utfDirString) == NULL) {
611 goto end;
612 }
613 break;
614 }
615 case FILE_INITFILE: {
616 Tcl_DString ds;
617
618 if (Tcl_TranslateFileName(interp, string, &ds) == NULL) {
619 goto end;
620 }
621 Tcl_UtfToExternal(NULL, unicodeEncoding, Tcl_DStringValue(&ds),
622 Tcl_DStringLength(&ds), 0, NULL, (char *) file,
623 sizeof(file), NULL, NULL, NULL);
624 break;
625 }
626 case FILE_MULTIPLE: {
627 if (Tcl_GetBooleanFromObj(interp, valuePtr,
628 &multi) != TCL_OK) {
629 return TCL_ERROR;
630 }
631 break;
632 }
633 case FILE_PARENT: {
634 tkwin = Tk_NameToWindow(interp, string, tkwin);
635 if (tkwin == NULL) {
636 goto end;
637 }
638 break;
639 }
640 case FILE_TITLE: {
641 title = string;
642 break;
643 }
644 }
645 }
646
647 if (filter == NULL) {
648 if (MakeFilter(interp, "", &utfFilterString) != TCL_OK) {
649 goto end;
650 }
651 }
652
653 Tk_MakeWindowExist(tkwin);
654 hWnd = Tk_GetHWND(Tk_WindowId(tkwin));
655
656 ZeroMemory(&ofn, sizeof(OPENFILENAMEW));
657 ofn.lStructSize = sizeof(OPENFILENAMEW);
658 ofn.hwndOwner = hWnd;
659 #ifdef _WIN64
660 ofn.hInstance = (HINSTANCE) GetWindowLongPtr(ofn.hwndOwner,
661 GWLP_HINSTANCE);
662 #else
663 ofn.hInstance = (HINSTANCE) GetWindowLong(ofn.hwndOwner,
664 GWL_HINSTANCE);
665 #endif
666 ofn.lpstrFile = (WCHAR *) file;
667 ofn.nMaxFile = TK_MULTI_MAX_PATH;
668 ofn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST
669 | OFN_NOCHANGEDIR | OFN_EXPLORER;
670 ofn.lpfnHook = (LPOFNHOOKPROC) OFNHookProcW;
671 ofn.lCustData = (LPARAM) interp;
672
673 if (open != 0) {
674 ofn.Flags |= OFN_FILEMUSTEXIST;
675 } else {
676 ofn.Flags |= OFN_OVERWRITEPROMPT;
677 }
678
679 if (tsdPtr->debugFlag != 0) {
680 ofn.Flags |= OFN_ENABLEHOOK;
681 }
682
683 if (multi != 0) {
684 ofn.Flags |= OFN_ALLOWMULTISELECT;
685 }
686
687 if (extension != NULL) {
688 Tcl_UtfToExternalDString(unicodeEncoding, extension, -1, &extString);
689 ofn.lpstrDefExt = (WCHAR *) Tcl_DStringValue(&extString);
690 }
691
692 Tcl_UtfToExternalDString(unicodeEncoding,
693 Tcl_DStringValue(&utfFilterString),
694 Tcl_DStringLength(&utfFilterString), &filterString);
695 ofn.lpstrFilter = (WCHAR *) Tcl_DStringValue(&filterString);
696
697 if (Tcl_DStringValue(&utfDirString)[0] != '\0') {
698 Tcl_UtfToExternalDString(unicodeEncoding,
699 Tcl_DStringValue(&utfDirString),
700 Tcl_DStringLength(&utfDirString), &dirString);
701 } else {
702 /*
703 * NT 5.0 changed the meaning of lpstrInitialDir, so we have
704 * to ensure that we set the [pwd] if the user didn't specify
705 * anything else.
706 */
707 Tcl_DString cwd;
708
709 Tcl_DStringFree(&utfDirString);
710 if ((Tcl_GetCwd(interp, &utfDirString) == (char *) NULL) ||
711 (Tcl_TranslateFileName(interp,
712 Tcl_DStringValue(&utfDirString), &cwd) == NULL)) {
713 Tcl_ResetResult(interp);
714 } else {
715 Tcl_UtfToExternalDString(unicodeEncoding, Tcl_DStringValue(&cwd),
716 Tcl_DStringLength(&cwd), &dirString);
717 }
718 Tcl_DStringFree(&cwd);
719 }
720 ofn.lpstrInitialDir = (WCHAR *) Tcl_DStringValue(&dirString);
721
722 if (title != NULL) {
723 Tcl_UtfToExternalDString(unicodeEncoding, title, -1, &titleString);
724 ofn.lpstrTitle = (WCHAR *) Tcl_DStringValue(&titleString);
725 }
726
727 /*
728 * Popup the dialog.
729 */
730
731 oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
732 if (open != 0) {
733 winCode = GetOpenFileNameW(&ofn);
734 } else {
735 winCode = GetSaveFileNameW(&ofn);
736 }
737 Tcl_SetServiceMode(oldMode);
738
739 /*
740 * Ensure that hWnd is enabled, because it can happen that we
741 * have updated the wrapper of the parent, which causes us to
742 * leave this child disabled (Windows loses sync).
743 */
744 EnableWindow(hWnd, 1);
745
746 /*
747 * Clear the interp result since anything may have happened during the
748 * modal loop.
749 */
750
751 Tcl_ResetResult(interp);
752
753 /*
754 * Process the results.
755 */
756
757 if (winCode != 0) {
758 if (ofn.Flags & OFN_ALLOWMULTISELECT) {
759 /*
760 * The result in custData->szFile contains many items,
761 * separated with null characters. It is terminated with
762 * two nulls in a row. The first element is the directory
763 * path.
764 */
765 char *dir;
766 char *p;
767 char *file;
768 WCHAR *files;
769 Tcl_DString ds;
770 Tcl_DString fullname, filename;
771 Tcl_Obj *returnList;
772 int count = 0;
773
774 returnList = Tcl_NewObj();
775 Tcl_IncrRefCount(returnList);
776
777 files = ofn.lpstrFile;
778 Tcl_ExternalToUtfDString(unicodeEncoding, (char *) files, -1, &ds);
779
780 /* Get directory */
781 dir = Tcl_DStringValue(&ds);
782 for (p = dir; p && *p; p++) {
783 /*
784 * Change the pathname to the Tcl "normalized" pathname, where
785 * back slashes are used instead of forward slashes
786 */
787 if (*p == '\\') {
788 *p = '/';
789 }
790 }
791
792 while (*files != '\0') {
793 while (*files != '\0') {
794 files++;
795 }
796 files++;
797 if (*files != '\0') {
798 count++;
799 Tcl_ExternalToUtfDString(unicodeEncoding,
800 (char *)files, -1, &filename);
801 file = Tcl_DStringValue(&filename);
802 for (p = file; *p != '\0'; p++) {
803 if (*p == '\\') {
804 *p = '/';
805 }
806 }
807 Tcl_DStringInit(&fullname);
808 Tcl_DStringAppend(&fullname, dir, -1);
809 Tcl_DStringAppend(&fullname, "/", -1);
810 Tcl_DStringAppend(&fullname, file, -1);
811 Tcl_ListObjAppendElement(interp, returnList,
812 Tcl_NewStringObj(Tcl_DStringValue(&fullname), -1));
813 Tcl_DStringFree(&fullname);
814 Tcl_DStringFree(&filename);
815 }
816 }
817 if (count == 0) {
818 /*
819 * Only one file was returned.
820 */
821 Tcl_ListObjAppendElement(interp, returnList,
822 Tcl_NewStringObj(dir, -1));
823 }
824 Tcl_SetObjResult(interp, returnList);
825 Tcl_DecrRefCount(returnList);
826 Tcl_DStringFree(&ds);
827 } else {
828 char *p;
829 Tcl_DString ds;
830
831 Tcl_ExternalToUtfDString(unicodeEncoding,
832 (char *) ofn.lpstrFile, -1, &ds);
833 for (p = Tcl_DStringValue(&ds); *p != '\0'; p++) {
834 /*
835 * Change the pathname to the Tcl "normalized" pathname, where
836 * back slashes are used instead of forward slashes
837 */
838 if (*p == '\\') {
839 *p = '/';
840 }
841 }
842 Tcl_AppendResult(interp, Tcl_DStringValue(&ds), NULL);
843 Tcl_DStringFree(&ds);
844 }
845 result = TCL_OK;
846 } else {
847 /*
848 * Use the CommDlgExtendedError() function to retrieve the error code.
849 * This function can return one of about two dozen codes; most of
850 * these indicate some sort of gross system failure (insufficient
851 * memory, bad window handles, etc.). Most of the error codes will be
852 * ignored; as we find we want more specific error messages for
853 * particular errors, we can extend the code as needed.
854 *
855 * We could also check for FNERR_BUFFERTOOSMALL, but we can't
856 * really do anything about it when it happens.
857 */
858
859 if (CommDlgExtendedError() == FNERR_INVALIDFILENAME) {
860 char *p;
861 Tcl_DString ds;
862
863 Tcl_ExternalToUtfDString(unicodeEncoding,
864 (char *) ofn.lpstrFile, -1, &ds);
865 for (p = Tcl_DStringValue(&ds); *p != '\0'; p++) {
866 /*
867 * Change the pathname to the Tcl "normalized" pathname,
868 * where back slashes are used instead of forward slashes
869 */
870 if (*p == '\\') {
871 *p = '/';
872 }
873 }
874 Tcl_SetResult(interp, "invalid filename \"", TCL_STATIC);
875 Tcl_AppendResult(interp, Tcl_DStringValue(&ds), "\"", NULL);
876 Tcl_DStringFree(&ds);
877 } else {
878 result = TCL_OK;
879 }
880 }
881
882 if (ofn.lpstrTitle != NULL) {
883 Tcl_DStringFree(&titleString);
884 }
885 if (ofn.lpstrInitialDir != NULL) {
886 Tcl_DStringFree(&dirString);
887 }
888 Tcl_DStringFree(&filterString);
889 if (ofn.lpstrDefExt != NULL) {
890 Tcl_DStringFree(&extString);
891 }
892
893 end:
894 Tcl_DStringFree(&utfDirString);
895 Tcl_DStringFree(&utfFilterString);
896
897 return result;
898 }
899
900 /*
901 *-------------------------------------------------------------------------
902 *
903 * OFNHookProcW --
904 *
905 * Hook procedure called only if debugging is turned on. Sets
906 * the "tk_dialog" variable when the dialog is ready to receive
907 * messages.
908 *
909 * Results:
910 * Returns 0 to allow default processing of messages to occur.
911 *
912 * Side effects:
913 * None.
914 *
915 *-------------------------------------------------------------------------
916 */
917
918 static UINT APIENTRY
919 OFNHookProcW(
920 HWND hdlg, // handle to child dialog window
921 UINT uMsg, // message identifier
922 WPARAM wParam, // message parameter
923 LPARAM lParam) // message parameter
924 {
925 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
926 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
927 OPENFILENAMEW *ofnPtr;
928
929 if (uMsg == WM_INITDIALOG) {
930 #ifdef _WIN64
931 SetWindowLongPtr(hdlg, GWLP_USERDATA, lParam);
932 #else
933 SetWindowLong(hdlg, GWL_USERDATA, lParam);
934 #endif
935 } else if (uMsg == WM_WINDOWPOSCHANGED) {
936 /*
937 * This message is delivered at the right time to enable Tk
938 * to set the debug information. Unhooks itself so it
939 * won't set the debug information every time it gets a
940 * WM_WINDOWPOSCHANGED message.
941 */
942
943 #ifdef _WIN64
944 ofnPtr = (OPENFILENAMEW *) GetWindowLongPtr(hdlg, GWLP_USERDATA);
945 #else
946 ofnPtr = (OPENFILENAMEW *) GetWindowLong(hdlg, GWL_USERDATA);
947 #endif
948 if (ofnPtr != NULL) {
949 hdlg = GetParent(hdlg);
950 tsdPtr->debugInterp = (Tcl_Interp *) ofnPtr->lCustData;
951 Tcl_DoWhenIdle(SetTkDialog, (ClientData) hdlg);
952 #ifdef _WIN64
953 SetWindowLongPtr(hdlg, GWLP_USERDATA, (LPARAM) NULL);
954 #else
955 SetWindowLong(hdlg, GWL_USERDATA, (LPARAM) NULL);
956 #endif
957 }
958 }
959 return 0;
960 }
961
962 /*
963 *----------------------------------------------------------------------
964 *
965 * GetFileNameA --
966 *
967 * Calls GetOpenFileName() or GetSaveFileName().
968 *
969 * Results:
970 * See user documentation.
971 *
972 * Side effects:
973 * See user documentation.
974 *
975 *----------------------------------------------------------------------
976 */
977
978 static int
979 GetFileNameA(clientData, interp, objc, objv, open)
980 ClientData clientData; /* Main window associated with interpreter. */
981 Tcl_Interp *interp; /* Current interpreter. */
982 int objc; /* Number of arguments. */
983 Tcl_Obj *CONST objv[]; /* Argument objects. */
984 int open; /* 1 to call GetOpenFileName(), 0 to
985 * call GetSaveFileName(). */
986 {
987 OPENFILENAME ofn;
988 TCHAR file[TK_MULTI_MAX_PATH], savePath[MAX_PATH];
989 int result, winCode, oldMode, i, multi = 0;
990 char *extension, *filter, *title;
991 Tk_Window tkwin;
992 HWND hWnd;
993 Tcl_DString utfFilterString, utfDirString;
994 Tcl_DString extString, filterString, dirString, titleString;
995 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
996 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
997 static CONST char *saveOptionStrings[] = {
998 "-defaultextension", "-filetypes", "-initialdir", "-initialfile",
999 "-parent", "-title", NULL
1000 };
1001 static CONST char *openOptionStrings[] = {
1002 "-defaultextension", "-filetypes", "-initialdir", "-initialfile",
1003 "-multiple", "-parent", "-title", NULL
1004 };
1005 CONST char **optionStrings;
1006
1007 enum options {
1008 FILE_DEFAULT, FILE_TYPES, FILE_INITDIR, FILE_INITFILE,
1009 FILE_MULTIPLE, FILE_PARENT, FILE_TITLE
1010 };
1011
1012 result = TCL_ERROR;
1013 file[0] = '\0';
1014
1015 /*
1016 * Parse the arguments.
1017 */
1018
1019 extension = NULL;
1020 filter = NULL;
1021 Tcl_DStringInit(&utfFilterString);
1022 Tcl_DStringInit(&utfDirString);
1023 tkwin = (Tk_Window) clientData;
1024 title = NULL;
1025
1026 if (open) {
1027 optionStrings = openOptionStrings;
1028 } else {
1029 optionStrings = saveOptionStrings;
1030 }
1031
1032 for (i = 1; i < objc; i += 2) {
1033 int index;
1034 char *string;
1035 Tcl_Obj *optionPtr, *valuePtr;
1036
1037 optionPtr = objv[i];
1038 valuePtr = objv[i + 1];
1039
1040 if (Tcl_GetIndexFromObj(interp, optionPtr, optionStrings,
1041 "option", 0, &index) != TCL_OK) {
1042 goto end;
1043 }
1044 /*
1045 * We want to maximize code sharing between the open and save file
1046 * dialog implementations; in particular, the switch statement below.
1047 * We use different sets of option strings from the GetIndexFromObj
1048 * call above, but a single enumeration for both. The save file
1049 * dialog doesn't support -multiple, but it falls in the middle of
1050 * the enumeration. Ultimately, this means that when the index found
1051 * by GetIndexFromObj is >= FILE_MULTIPLE, when doing a save file
1052 * dialog, we have to increment the index, so that it matches the
1053 * open file dialog enumeration.
1054 */
1055 if (!open && index >= FILE_MULTIPLE) {
1056 index++;
1057 }
1058 if (i + 1 == objc) {
1059 string = Tcl_GetStringFromObj(optionPtr, NULL);
1060 Tcl_AppendResult(interp, "value for \"", string, "\" missing",
1061 (char *) NULL);
1062 goto end;
1063 }
1064
1065 string = Tcl_GetStringFromObj(valuePtr, NULL);
1066 switch ((enum options) index) {
1067 case FILE_DEFAULT: {
1068 if (string[0] == '.') {
1069 string++;
1070 }
1071 extension = string;
1072 break;
1073 }
1074 case FILE_TYPES: {
1075 Tcl_DStringFree(&utfFilterString);
1076 if (MakeFilter(interp, string, &utfFilterString) != TCL_OK) {
1077 goto end;
1078 }
1079 filter = Tcl_DStringValue(&utfFilterString);
1080 break;
1081 }
1082 case FILE_INITDIR: {
1083 Tcl_DStringFree(&utfDirString);
1084 if (Tcl_TranslateFileName(interp, string,
1085 &utfDirString) == NULL) {
1086 goto end;
1087 }
1088 break;
1089 }
1090 case FILE_INITFILE: {
1091 Tcl_DString ds;
1092
1093 if (Tcl_TranslateFileName(interp, string, &ds) == NULL) {
1094 goto end;
1095 }
1096 Tcl_UtfToExternal(NULL, NULL, Tcl_DStringValue(&ds),
1097 Tcl_DStringLength(&ds), 0, NULL, (char *) file,
1098 sizeof(file), NULL, NULL, NULL);
1099 break;
1100 }
1101 case FILE_MULTIPLE: {
1102 if (Tcl_GetBooleanFromObj(interp, valuePtr,
1103 &multi) != TCL_OK) {
1104 return TCL_ERROR;
1105 }
1106 break;
1107 }
1108 case FILE_PARENT: {
1109 tkwin = Tk_NameToWindow(interp, string, tkwin);
1110 if (tkwin == NULL) {
1111 goto end;
1112 }
1113 break;
1114 }
1115 case FILE_TITLE: {
1116 title = string;
1117 break;
1118 }
1119 }
1120 }
1121
1122 if (filter == NULL) {
1123 if (MakeFilter(interp, "", &utfFilterString) != TCL_OK) {
1124 goto end;
1125 }
1126 }
1127
1128 Tk_MakeWindowExist(tkwin);
1129 hWnd = Tk_GetHWND(Tk_WindowId(tkwin));
1130
1131 ofn.lStructSize = sizeof(ofn);
1132 ofn.hwndOwner = hWnd;
1133 #ifdef _WIN64
1134 ofn.hInstance = (HINSTANCE) GetWindowLongPtr(ofn.hwndOwner,
1135 GWLP_HINSTANCE);
1136 #else
1137 ofn.hInstance = (HINSTANCE) GetWindowLong(ofn.hwndOwner,
1138 GWL_HINSTANCE);
1139 #endif
1140 ofn.lpstrFilter = NULL;
1141 ofn.lpstrCustomFilter = NULL;
1142 ofn.nMaxCustFilter = 0;
1143 ofn.nFilterIndex = 0;
1144 ofn.lpstrFile = (LPTSTR) file;
1145 ofn.nMaxFile = TK_MULTI_MAX_PATH;
1146 ofn.lpstrFileTitle = NULL;
1147 ofn.nMaxFileTitle = 0;
1148 ofn.lpstrInitialDir = NULL;
1149 ofn.lpstrTitle = NULL;
1150 ofn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST
1151 | OFN_NOCHANGEDIR | OFN_EXPLORER;
1152 ofn.nFileOffset = 0;
1153 ofn.nFileExtension = 0;
1154 ofn.lpstrDefExt = NULL;
1155 ofn.lpfnHook = (LPOFNHOOKPROC) OFNHookProc;
1156 ofn.lCustData = (LPARAM) interp;
1157 ofn.lpTemplateName = NULL;
1158
1159 if (open != 0) {
1160 ofn.Flags |= OFN_FILEMUSTEXIST;
1161 } else {
1162 ofn.Flags |= OFN_OVERWRITEPROMPT;
1163 }
1164
1165 if (tsdPtr->debugFlag != 0) {
1166 ofn.Flags |= OFN_ENABLEHOOK;
1167 }
1168
1169 if (multi != 0) {
1170 ofn.Flags |= OFN_ALLOWMULTISELECT;
1171 }
1172
1173 if (extension != NULL) {
1174 Tcl_UtfToExternalDString(NULL, extension, -1, &extString);
1175 ofn.lpstrDefExt = (LPTSTR) Tcl_DStringValue(&extString);
1176 }
1177 Tcl_UtfToExternalDString(NULL, Tcl_DStringValue(&utfFilterString),
1178 Tcl_DStringLength(&utfFilterString), &filterString);
1179 ofn.lpstrFilter = (LPTSTR) Tcl_DStringValue(&filterString);
1180
1181 if (Tcl_DStringValue(&utfDirString)[0] != '\0') {
1182 Tcl_UtfToExternalDString(NULL, Tcl_DStringValue(&utfDirString),
1183 Tcl_DStringLength(&utfDirString), &dirString);
1184 } else {
1185 /*
1186 * NT 5.0 changed the meaning of lpstrInitialDir, so we have
1187 * to ensure that we set the [pwd] if the user didn't specify
1188 * anything else.
1189 */
1190 Tcl_DString cwd;
1191
1192 Tcl_DStringFree(&utfDirString);
1193 if ((Tcl_GetCwd(interp, &utfDirString) == (char *) NULL) ||
1194 (Tcl_TranslateFileName(interp,
1195 Tcl_DStringValue(&utfDirString), &cwd) == NULL)) {
1196 Tcl_ResetResult(interp);
1197 } else {
1198 Tcl_UtfToExternalDString(NULL, Tcl_DStringValue(&cwd),
1199 Tcl_DStringLength(&cwd), &dirString);
1200 }
1201 Tcl_DStringFree(&cwd);
1202 }
1203 ofn.lpstrInitialDir = (LPTSTR) Tcl_DStringValue(&dirString);
1204
1205 if (title != NULL) {
1206 Tcl_UtfToExternalDString(NULL, title, -1, &titleString);
1207 ofn.lpstrTitle = (LPTSTR) Tcl_DStringValue(&titleString);
1208 }
1209
1210 /*
1211 * Popup the dialog.
1212 */
1213
1214 GetCurrentDirectory(MAX_PATH, savePath);
1215 oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
1216 if (open != 0) {
1217 winCode = GetOpenFileName(&ofn);
1218 } else {
1219 winCode = GetSaveFileName(&ofn);
1220 }
1221 Tcl_SetServiceMode(oldMode);
1222 SetCurrentDirectory(savePath);
1223
1224 /*
1225 * Ensure that hWnd is enabled, because it can happen that we
1226 * have updated the wrapper of the parent, which causes us to
1227 * leave this child disabled (Windows loses sync).
1228 */
1229 EnableWindow(hWnd, 1);
1230
1231 /*
1232 * Clear the interp result since anything may have happened during the
1233 * modal loop.
1234 */
1235
1236 Tcl_ResetResult(interp);
1237
1238 /*
1239 * Process the results.
1240 */
1241
1242 if (winCode != 0) {
1243 if (ofn.Flags & OFN_ALLOWMULTISELECT) {
1244 /*
1245 * The result in custData->szFile contains many items,
1246 * separated with null characters. It is terminated with
1247 * two nulls in a row. The first element is the directory
1248 * path.
1249 */
1250 char *dir;
1251 char *p;
1252 char *file;
1253 char *files;
1254 Tcl_DString ds;
1255 Tcl_DString fullname, filename;
1256 Tcl_Obj *returnList;
1257 int count = 0;
1258
1259 returnList = Tcl_NewObj();
1260 Tcl_IncrRefCount(returnList);
1261
1262 files = ofn.lpstrFile;
1263 Tcl_ExternalToUtfDString(NULL, (char *) files, -1, &ds);
1264
1265 /* Get directory */
1266 dir = Tcl_DStringValue(&ds);
1267 for (p = dir; p && *p; p++) {
1268 /*
1269 * Change the pathname to the Tcl "normalized" pathname, where
1270 * back slashes are used instead of forward slashes
1271 */
1272 if (*p == '\\') {
1273 *p = '/';
1274 }
1275 }
1276
1277 while (*files != '\0') {
1278 while (*files != '\0') {
1279 files++;
1280 }
1281 files++;
1282 if (*files != '\0') {
1283 count++;
1284 Tcl_ExternalToUtfDString(NULL,
1285 (char *)files, -1, &filename);
1286 file = Tcl_DStringValue(&filename);
1287 for (p = file; *p != '\0'; p++) {
1288 if (*p == '\\') {
1289 *p = '/';
1290 }
1291 }
1292 Tcl_DStringInit(&fullname);
1293 Tcl_DStringAppend(&fullname, dir, -1);
1294 Tcl_DStringAppend(&fullname, "/", -1);
1295 Tcl_DStringAppend(&fullname, file, -1);
1296 Tcl_ListObjAppendElement(interp, returnList,
1297 Tcl_NewStringObj(Tcl_DStringValue(&fullname), -1));
1298 Tcl_DStringFree(&fullname);
1299 Tcl_DStringFree(&filename);
1300 }
1301 }
1302 if (count == 0) {
1303 /*
1304 * Only one file was returned.
1305 */
1306 Tcl_ListObjAppendElement(interp, returnList,
1307 Tcl_NewStringObj(dir, -1));
1308 }
1309 Tcl_SetObjResult(interp, returnList);
1310 Tcl_DecrRefCount(returnList);
1311 Tcl_DStringFree(&ds);
1312 } else {
1313 char *p;
1314 Tcl_DString ds;
1315
1316 Tcl_ExternalToUtfDString(NULL, (char *) ofn.lpstrFile, -1, &ds);
1317 for (p = Tcl_DStringValue(&ds); *p != '\0'; p++) {
1318 /*
1319 * Change the pathname to the Tcl "normalized" pathname, where
1320 * back slashes are used instead of forward slashes
1321 */
1322 if (*p == '\\') {
1323 *p = '/';
1324 }
1325 }
1326 Tcl_AppendResult(interp, Tcl_DStringValue(&ds), NULL);
1327 Tcl_DStringFree(&ds);
1328 }
1329 result = TCL_OK;
1330 } else {
1331 /*
1332 * Use the CommDlgExtendedError() function to retrieve the error code.
1333 * This function can return one of about two dozen codes; most of
1334 * these indicate some sort of gross system failure (insufficient
1335 * memory, bad window handles, etc.). Most of the error codes will be
1336 * ignored;; as we find we want specific error messages for particular
1337 * errors, we can extend the code as needed.
1338 *
1339 * We could also check for FNERR_BUFFERTOOSMALL, but we can't
1340 * really do anything about it when it happens.
1341 */
1342 if (CommDlgExtendedError() == FNERR_INVALIDFILENAME) {
1343 char *p;
1344 Tcl_DString ds;
1345
1346 Tcl_ExternalToUtfDString(NULL, (char *) ofn.lpstrFile, -1, &ds);
1347 for (p = Tcl_DStringValue(&ds); *p != '\0'; p++) {
1348 /*
1349 * Change the pathname to the Tcl "normalized" pathname,
1350 * where back slashes are used instead of forward slashes
1351 */
1352 if (*p == '\\') {
1353 *p = '/';
1354 }
1355 }
1356 Tcl_SetResult(interp, "invalid filename \"", TCL_STATIC);
1357 Tcl_AppendResult(interp, Tcl_DStringValue(&ds), "\"", NULL);
1358 Tcl_DStringFree(&ds);
1359 } else {
1360 result = TCL_OK;
1361 }
1362 }
1363
1364 if (ofn.lpstrTitle != NULL) {
1365 Tcl_DStringFree(&titleString);
1366 }
1367 if (ofn.lpstrInitialDir != NULL) {
1368 Tcl_DStringFree(&dirString);
1369 }
1370 Tcl_DStringFree(&filterString);
1371 if (ofn.lpstrDefExt != NULL) {
1372 Tcl_DStringFree(&extString);
1373 }
1374
1375 end:
1376 Tcl_DStringFree(&utfDirString);
1377 Tcl_DStringFree(&utfFilterString);
1378
1379 return result;
1380 }
1381
1382 /*
1383 *-------------------------------------------------------------------------
1384 *
1385 * OFNHookProc --
1386 *
1387 * Hook procedure called only if debugging is turned on. Sets
1388 * the "tk_dialog" variable when the dialog is ready to receive
1389 * messages.
1390 *
1391 * Results:
1392 * Returns 0 to allow default processing of messages to occur.
1393 *
1394 * Side effects:
1395 * None.
1396 *
1397 *-------------------------------------------------------------------------
1398 */
1399
1400 static UINT APIENTRY
1401 OFNHookProc(
1402 HWND hdlg, // handle to child dialog window
1403 UINT uMsg, // message identifier
1404 WPARAM wParam, // message parameter
1405 LPARAM lParam) // message parameter
1406 {
1407 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
1408 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
1409 OPENFILENAME *ofnPtr;
1410
1411 if (uMsg == WM_INITDIALOG) {
1412 #ifdef _WIN64
1413 SetWindowLongPtr(hdlg, GWLP_USERDATA, lParam);
1414 #else
1415 SetWindowLong(hdlg, GWL_USERDATA, lParam);
1416 #endif
1417 } else if (uMsg == WM_WINDOWPOSCHANGED) {
1418 /*
1419 * This message is delivered at the right time to both
1420 * old-style and explorer-style hook procs to enable Tk
1421 * to set the debug information. Unhooks itself so it
1422 * won't set the debug information every time it gets a
1423 * WM_WINDOWPOSCHANGED message.
1424 */
1425
1426 #ifdef _WIN64
1427 ofnPtr = (OPENFILENAME *) GetWindowLongPtr(hdlg, GWLP_USERDATA);
1428 #else
1429 ofnPtr = (OPENFILENAME *) GetWindowLong(hdlg, GWL_USERDATA);
1430 #endif
1431 if (ofnPtr != NULL) {
1432 if (ofnPtr->Flags & OFN_EXPLORER) {
1433 hdlg = GetParent(hdlg);
1434 }
1435 tsdPtr->debugInterp = (Tcl_Interp *) ofnPtr->lCustData;
1436 Tcl_DoWhenIdle(SetTkDialog, (ClientData) hdlg);
1437 #ifdef _WIN64
1438 SetWindowLongPtr(hdlg, GWLP_USERDATA, (LPARAM) NULL);
1439 #else
1440 SetWindowLong(hdlg, GWL_USERDATA, (LPARAM) NULL);
1441 #endif
1442 }
1443 }
1444 return 0;
1445 }
1446
1447 /*
1448 *----------------------------------------------------------------------
1449 *
1450 * MakeFilter --
1451 *
1452 * Allocate a buffer to store the filters in a format understood by
1453 * Windows
1454 *
1455 * Results:
1456 * A standard TCL return value.
1457 *
1458 * Side effects:
1459 * ofnPtr->lpstrFilter is modified.
1460 *
1461 *----------------------------------------------------------------------
1462 */
1463 static int
1464 MakeFilter(interp, string, dsPtr)
1465 Tcl_Interp *interp; /* Current interpreter. */
1466 char *string; /* String value of the -filetypes option */
1467 Tcl_DString *dsPtr; /* Filled with windows filter string. */
1468 {
1469 char *filterStr = NULL;
1470 char *p;
1471 int pass;
1472 FileFilterList flist;
1473 FileFilter *filterPtr;
1474
1475 TkInitFileFilters(&flist);
1476 if (TkGetFileFilters(interp, &flist, Tcl_NewStringObj(string,-1), 1) != TCL_OK) {
1477 return TCL_ERROR;
1478 }
1479
1480 if (flist.filters == NULL) {
1481 /*
1482 * Use "All Files (*.*) as the default filter if none is specified
1483 */
1484 char *defaultFilter = "All Files (*.*)";
1485
1486 p = filterStr = (char*)ckalloc(30 * sizeof(char));
1487
1488 strcpy(p, defaultFilter);
1489 p+= strlen(defaultFilter);
1490
1491 *p++ = '\0';
1492 *p++ = '*';
1493 *p++ = '.';
1494 *p++ = '*';
1495 *p++ = '\0';
1496 *p++ = '\0';
1497 *p = '\0';
1498
1499 } else {
1500 unsigned need = 0;
1501 /* We format the filetype into a string understood by Windows:
1502 * {"Text Documents" {.doc .txt} {TEXT}} becomes
1503 * "Text Documents (*.doc,*.txt)\0*.doc;*.txt\0"
1504 *
1505 * See the Windows OPENFILENAME manual page for details on the filter
1506 * string format.
1507 */
1508
1509 /*
1510 * Since we may only add asterisks (*) to the filter, we need at most
1511 * twice the size of the string to format the filter
1512 */
1513 filterStr = ckalloc((unsigned int) strlen(string) * 3);
1514
1515 for (filterPtr = flist.filters, p = filterStr; filterPtr;
1516 filterPtr = filterPtr->next) {
1517 char *sep;
1518 FileFilterClause *clausePtr;
1519
1520 /*
1521 * First, put in the name of the file type
1522 */
1523 strcpy(p, filterPtr->name);
1524 p+= strlen(filterPtr->name);
1525 *p++ = ' ';
1526 *p++ = '(';
1527
1528 for (pass = 1; pass <= 2; pass++) {
1529 /*
1530 * In the first pass, we format the extensions in the
1531 * name field. In the second pass, we format the extensions in
1532 * the filter pattern field
1533 */
1534 sep = "";
1535 for (clausePtr=filterPtr->clauses;clausePtr;
1536 clausePtr=clausePtr->next) {
1537 GlobPattern *globPtr;
1538
1539
1540 for (globPtr=clausePtr->patterns; globPtr;
1541 globPtr=globPtr->next) {
1542 strcpy(p, sep);
1543 p+= strlen(sep);
1544 strcpy(p, globPtr->pattern);
1545 p+= strlen(globPtr->pattern);
1546
1547 if (pass==1) {
1548 sep = ",";
1549 } else {
1550 sep = ";";
1551 }
1552 }
1553 }
1554 if (pass == 1) {
1555 if (pass == 1) {
1556 *p ++ = ')';
1557 }
1558 }
1559 *p ++ = '\0';
1560 }
1561 }
1562
1563 /*
1564 * Windows requires the filter string to be ended by two NULL
1565 * characters.
1566 */
1567 *p++ = '\0';
1568 *p = '\0';
1569 }
1570
1571 Tcl_DStringAppend(dsPtr, filterStr, (int) (p - filterStr));
1572 ckfree((char *) filterStr);
1573
1574 TkFreeFileFilters(&flist);
1575 return TCL_OK;
1576 }
1577
1578 #ifdef USE_NEW_CHOOSEDIR
1579 /*
1580 *----------------------------------------------------------------------
1581 *
1582 * Tk_ChooseDirectoryObjCmd --
1583 *
1584 * This procedure implements the "tk_chooseDirectory" dialog box
1585 * for the Windows platform. See the user documentation for details
1586 * on what it does. Uses the newer SHBrowseForFolder explorer type
1587 * interface.
1588 *
1589 * Results:
1590 * See user documentation.
1591 *
1592 * Side effects:
1593 * A modal dialog window is created. Tcl_SetServiceMode() is
1594 * called to allow background events to be processed
1595 *
1596 *----------------------------------------------------------------------
1597
1598 The procedure tk_chooseDirectory pops up a dialog box for the user to
1599 select a directory. The following option-value pairs are possible as
1600 command line arguments:
1601
1602 -initialdir dirname
1603
1604 Specifies that the directories in directory should be displayed when the
1605 dialog pops up. If this parameter is not specified, then the directories
1606 in the current working directory are displayed. If the parameter specifies
1607 a relative path, the return value will convert the relative path to an
1608 absolute path. This option may not always work on the Macintosh. This is
1609 not a bug. Rather, the General Controls control panel on the Mac allows
1610 the end user to override the application default directory.
1611
1612 -parent window
1613
1614 Makes window the logical parent of the dialog. The dialog is displayed on
1615 top of its parent window.
1616
1617 -title titleString
1618
1619 Specifies a string to display as the title of the dialog box. If this
1620 option is not specified, then a default title will be displayed.
1621
1622 -mustexist boolean
1623
1624 Specifies whether the user may specify non-existant directories. If this
1625 parameter is true, then the user may only select directories that already
1626 exist. The default value is false.
1627
1628 New Behaviour:
1629
1630 - If mustexist = 0 and a user entered folder does not exist, a prompt will
1631 pop-up asking if the user wants another chance to change it. The old
1632 dialog just returned the bogus entry. On mustexist = 1, the entries MUST
1633 exist before exiting the box with OK.
1634
1635 Bugs:
1636
1637 - If valid abs directory name is entered into the entry box and Enter
1638 pressed, the box will close returning the name. This is inconsistent when
1639 entering relative names or names with forward slashes, which are
1640 invalidated then corrected in the callback. After correction, the box is
1641 held open to allow further modification by the user.
1642
1643 - Not sure how to implement localization of message prompts.
1644
1645 - -title is really -message.
1646 ToDo:
1647 - Fix bugs.
1648 - test to see what platforms this really works on. May require v4.71
1649 of shell32.dll everywhere (what is standard?).
1650 *
1651 */
1652 int
1653 Tk_ChooseDirectoryObjCmd(clientData, interp, objc, objv)
1654 ClientData clientData; /* Main window associated with interpreter. */
1655 Tcl_Interp *interp; /* Current interpreter. */
1656 int objc; /* Number of arguments. */
1657 Tcl_Obj *CONST objv[]; /* Argument objects. */
1658 {
1659 char path[MAX_PATH];
1660 int oldMode, result, i;
1661 LPCITEMIDLIST pidl; /* Returned by browser */
1662 BROWSEINFO bInfo; /* Used by browser */
1663 CHOOSEDIRDATA cdCBData; /* Structure to pass back and forth */
1664 LPMALLOC pMalloc; /* Used by shell */
1665
1666 Tk_Window tkwin;
1667 HWND hWnd;
1668 char *utfTitle; /* Title for window */
1669 TCHAR saveDir[MAX_PATH];
1670 Tcl_DString titleString; /* UTF Title */
1671 Tcl_DString initDirString; /* Initial directory */
1672 static CONST char *optionStrings[] = {
1673 "-initialdir", "-mustexist", "-parent", "-title", (char *) NULL
1674 };
1675 enum options {
1676 DIR_INITIAL, DIR_EXIST, DIR_PARENT, FILE_TITLE
1677 };
1678
1679 /*
1680 * Initialize
1681 */
1682 result = TCL_ERROR;
1683 path[0] = '\0';
1684 utfTitle = NULL;
1685
1686 ZeroMemory(&cdCBData, sizeof(CHOOSEDIRDATA));
1687 cdCBData.interp = interp;
1688
1689 tkwin = (Tk_Window) clientData;
1690 /*
1691 * Process the command line options
1692 */
1693 for (i = 1; i < objc; i += 2) {
1694 int index;
1695 char *string;
1696 Tcl_Obj *optionPtr, *valuePtr;
1697
1698 optionPtr = objv[i];
1699 valuePtr = objv[i + 1];
1700
1701 if (Tcl_GetIndexFromObj(interp, optionPtr, optionStrings, "option",
1702 0, &index) != TCL_OK) {
1703 goto cleanup;
1704 }
1705 if (i + 1 == objc) {
1706 string = Tcl_GetStringFromObj(optionPtr, NULL);
1707 Tcl_AppendResult(interp, "value for \"", string, "\" missing",
1708 (char *) NULL);
1709 goto cleanup;
1710 }
1711
1712 string = Tcl_GetString(valuePtr);
1713 switch ((enum options) index) {
1714 case DIR_INITIAL: {
1715 if (Tcl_TranslateFileName(interp, string,
1716 &initDirString) == NULL) {
1717 goto cleanup;
1718 }
1719 string = Tcl_DStringValue(&initDirString);
1720 /*
1721 * Convert possible relative path to full path to keep
1722 * dialog happy
1723 */
1724 GetFullPathName(string, MAX_PATH, saveDir, NULL);
1725 lstrcpyn(cdCBData.utfInitDir, saveDir, MAX_PATH);
1726 Tcl_DStringFree(&initDirString);
1727 break;
1728 }
1729 case DIR_EXIST: {
1730 if (Tcl_GetBooleanFromObj(interp, valuePtr,
1731 &cdCBData.mustExist) != TCL_OK) {
1732 goto cleanup;
1733 }
1734 break;
1735 }
1736 case DIR_PARENT: {
1737 tkwin = Tk_NameToWindow(interp, string, tkwin);
1738 if (tkwin == NULL) {
1739 goto cleanup;
1740 }
1741 break;
1742 }
1743 case FILE_TITLE: {
1744 utfTitle = string;
1745 break;
1746 }
1747 }
1748 }
1749
1750 /*
1751 * Get ready to call the browser
1752 */
1753
1754 Tk_MakeWindowExist(tkwin);
1755 hWnd = Tk_GetHWND(Tk_WindowId(tkwin));
1756
1757 /*
1758 * Setup the parameters used by SHBrowseForFolder
1759 */
1760
1761 bInfo.hwndOwner = hWnd;
1762 bInfo.pszDisplayName = path;
1763 bInfo.pidlRoot = NULL;
1764 if (lstrlen(cdCBData.utfInitDir) == 0) {
1765 GetCurrentDirectory(MAX_PATH, cdCBData.utfInitDir);
1766 }
1767 bInfo.lParam = (LPARAM) &cdCBData;
1768
1769 if (utfTitle != NULL) {
1770 Tcl_UtfToExternalDString(NULL, utfTitle, -1, &titleString);
1771 bInfo.lpszTitle = (LPTSTR) Tcl_DStringValue(&titleString);
1772 } else {
1773 bInfo.lpszTitle = "Please choose a directory, then select OK.";
1774 }
1775
1776 /*
1777 * Set flags to add edit box (needs 4.71 Shell DLLs), status text line,
1778 * validate edit box and
1779 */
1780 bInfo.ulFlags = BIF_EDITBOX | BIF_STATUSTEXT | BIF_RETURNFSANCESTORS
1781 | BIF_VALIDATE;
1782
1783 /*
1784 * Callback to handle events
1785 */
1786 bInfo.lpfn = (BFFCALLBACK) ChooseDirectoryValidateProc;
1787
1788 /*
1789 * Display dialog in background and process result.
1790 * We look to give the user a chance to change their mind
1791 * on an invalid folder if mustexist is 0;
1792 */
1793
1794 oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
1795 GetCurrentDirectory(MAX_PATH, saveDir);
1796 if (SHGetMalloc(&pMalloc) == NOERROR) {
1797 pidl = SHBrowseForFolder(&bInfo);
1798 /* Null for cancel button or invalid dir, otherwise valid*/
1799 if (pidl != NULL) {
1800 if (!SHGetPathFromIDList(pidl, path)) {
1801 Tcl_SetResult(interp, "Error: Not a file system folder\n",
1802 TCL_VOLATILE);
1803 };
1804 pMalloc->lpVtbl->Free(pMalloc, (void *) pidl);
1805 } else if (lstrlen(cdCBData.utfRetDir) > 0) {
1806 lstrcpy(path, cdCBData.utfRetDir);
1807 }
1808 pMalloc->lpVtbl->Release(pMalloc);
1809 }
1810 SetCurrentDirectory(saveDir);
1811 Tcl_SetServiceMode(oldMode);
1812
1813 /*
1814 * Ensure that hWnd is enabled, because it can happen that we
1815 * have updated the wrapper of the parent, which causes us to
1816 * leave this child disabled (Windows loses sync).
1817 */
1818 EnableWindow(hWnd, 1);
1819
1820 /*
1821 * Change the pathname to the Tcl "normalized" pathname, where
1822 * back slashes are used instead of forward slashes
1823 */
1824 Tcl_ResetResult(interp);
1825 if (*path) {
1826 char *p;
1827 Tcl_DString ds;
1828
1829 Tcl_ExternalToUtfDString(NULL, (char *) path, -1, &ds);
1830 for (p = Tcl_DStringValue(&ds); *p != '\0'; p++) {
1831 if (*p == '\\') {
1832 *p = '/';
1833 }
1834 }
1835 Tcl_AppendResult(interp, Tcl_DStringValue(&ds), NULL);
1836 Tcl_DStringFree(&ds);
1837 }
1838
1839 result = TCL_OK;
1840
1841 if (utfTitle != NULL) {
1842 Tcl_DStringFree(&titleString);
1843 }
1844
1845 cleanup:
1846 return result;
1847 }
1848
1849 /*
1850 *----------------------------------------------------------------------
1851 *
1852 * ChooseDirectoryValidateProc --
1853 *
1854 * Hook procedure called by the explorer ChooseDirectory dialog when events
1855 * occur. It is used to validate the text entry the user may have entered.
1856 *
1857 * Results:
1858 * Returns 0 to allow default processing of message, or 1 to
1859 * tell default dialog procedure not to close.
1860 *
1861 *----------------------------------------------------------------------
1862 */
1863 static UINT APIENTRY
1864 ChooseDirectoryValidateProc (
1865 HWND hwnd,
1866 UINT message,
1867 LPARAM lParam,
1868 LPARAM lpData)
1869 {
1870 TCHAR selDir[MAX_PATH];
1871 CHOOSEDIRDATA *chooseDirSharedData;
1872 Tcl_DString initDirString;
1873 char string[MAX_PATH];
1874 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
1875 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
1876
1877 chooseDirSharedData = (CHOOSEDIRDATA *)lpData;
1878
1879 #ifdef _WIN64
1880 SetWindowLongPtr(hwnd, GWLP_USERDATA, lpData);
1881 #else
1882 SetWindowLong(hwnd, GWL_USERDATA, lpData);
1883 #endif
1884
1885 if (tsdPtr->debugFlag) {
1886 tsdPtr->debugInterp = (Tcl_Interp *) chooseDirSharedData->interp;
1887 Tcl_DoWhenIdle(SetTkDialog, (ClientData) hwnd);
1888 }
1889 chooseDirSharedData->utfRetDir[0] = '\0';
1890 switch (message) {
1891 case BFFM_VALIDATEFAILED:
1892 /*
1893 * First save and check to see if it is a valid path name, if
1894 * so then make that path the one shown in the
1895 * window. Otherwise, it failed the check and should be treated
1896 * as such. Use Set/GetCurrentDirectory which allows relative
1897 * path names and names with forward slashes. Use
1898 * Tcl_TranslateFileName to make sure names like ~ are
1899 * converted correctly.
1900 */
1901 Tcl_TranslateFileName(chooseDirSharedData->interp,
1902 (char *)lParam, &initDirString);
1903 lstrcpyn (string, Tcl_DStringValue(&initDirString), MAX_PATH);
1904 Tcl_DStringFree(&initDirString);
1905
1906 if (SetCurrentDirectory((char *)string) == 0) {
1907 LPTSTR lpFilePart[MAX_PATH];
1908 /*
1909 * Get the full path name to the user entry,
1910 * at this point it doesn't exist so see if
1911 * it is supposed to. Otherwise just return it.
1912 */
1913 GetFullPathName(string, MAX_PATH,
1914 chooseDirSharedData->utfRetDir, /*unused*/ lpFilePart);
1915 if (chooseDirSharedData->mustExist) {
1916 /*
1917 * User HAS to select a valid directory.
1918 */
1919 wsprintf(selDir, TEXT("Directory '%.200s' does not exist,\nplease select or enter an existing directory."), chooseDirSharedData->utfRetDir);
1920 MessageBox(NULL, selDir, NULL, MB_ICONEXCLAMATION|MB_OK);
1921 return 1;
1922 }
1923 } else {
1924 /*
1925 * Changed to new folder OK, return immediatly with the
1926 * current directory in utfRetDir.
1927 */
1928 GetCurrentDirectory(MAX_PATH, chooseDirSharedData->utfRetDir);
1929 return 0;
1930 }
1931 return 0;
1932
1933 case BFFM_SELCHANGED:
1934 /*
1935 * Set the status window to the currently selected path.
1936 * And enable the OK button if a file system folder, otherwise
1937 * disable the OK button for things like server names.
1938 * perhaps a new switch -enablenonfolders can be used to allow
1939 * non folders to be selected.
1940 *
1941 * Not called when user changes edit box directly.
1942 */
1943
1944 if (SHGetPathFromIDList((LPITEMIDLIST) lParam, selDir)) {
1945 SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM) selDir);
1946 // enable the OK button
1947 SendMessage(hwnd, BFFM_ENABLEOK, 0, (LPARAM) 1);
1948 //EnableWindow(GetDlgItem(hwnd, IDOK), TRUE);
1949 SetCurrentDirectory(selDir);
1950 } else {
1951 // disable the OK button
1952 SendMessage(hwnd, BFFM_ENABLEOK, 0, (LPARAM) 0);
1953 //EnableWindow(GetDlgItem(hwnd, IDOK), FALSE);
1954 }
1955 UpdateWindow(hwnd);
1956 return 1;
1957
1958 case BFFM_INITIALIZED:
1959 /*
1960 * Directory browser intializing - tell it where to start from,
1961 * user specified parameter.
1962 */
1963 SetCurrentDirectory((char *) lpData);
1964 SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)lpData);
1965 SendMessage(hwnd, BFFM_ENABLEOK, 0, (LPARAM) 1);
1966 break;
1967
1968 }
1969 return 0;
1970 }
1971 #else
1972 /*
1973 *----------------------------------------------------------------------
1974 *
1975 * Tk_ChooseDirectoryObjCmd --
1976 *
1977 * This procedure implements the "tk_chooseDirectory" dialog box
1978 * for the Windows platform. See the user documentation for details
1979 * on what it does.
1980 *
1981 * Results:
1982 * See user documentation.
1983 *
1984 * Side effects:
1985 * A modal dialog window is created. Tcl_SetServiceMode() is
1986 * called to allow background events to be processed
1987 *
1988 *----------------------------------------------------------------------
1989 */
1990
1991 int
1992 Tk_ChooseDirectoryObjCmd(clientData, interp, objc, objv)
1993 ClientData clientData; /* Main window associated with interpreter. */
1994 Tcl_Interp *interp; /* Current interpreter. */
1995 int objc; /* Number of arguments. */
1996 Tcl_Obj *CONST objv[]; /* Argument objects. */
1997 {
1998 OPENFILENAME ofn;
1999 TCHAR path[MAX_PATH], savePath[MAX_PATH];
2000 ChooseDir cd;
2001 int result, mustExist, code, mode, i;
2002 Tk_Window tkwin;
2003 HWND hWnd;
2004 char *utfTitle;
2005 Tcl_DString utfDirString;
2006 Tcl_DString titleString, dirString;
2007 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
2008 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
2009 static CONST char *optionStrings[] = {
2010 "-initialdir", "-mustexist", "-parent", "-title",
2011 NULL
2012 };
2013 enum options {
2014 DIR_INITIAL, DIR_EXIST, DIR_PARENT, FILE_TITLE
2015 };
2016
2017 if (tsdPtr->WM_LBSELCHANGED == 0) {
2018 tsdPtr->WM_LBSELCHANGED = RegisterWindowMessage(LBSELCHSTRING);
2019 }
2020
2021 result = TCL_ERROR;
2022 path[0] = '\0';
2023
2024 Tcl_DStringInit(&utfDirString);
2025 mustExist = 0;
2026 tkwin = (Tk_Window) clientData;
2027 utfTitle = NULL;
2028
2029 for (i = 1; i < objc; i += 2) {
2030 int index;
2031 char *string;
2032 Tcl_Obj *optionPtr, *valuePtr;
2033
2034 optionPtr = objv[i];
2035 valuePtr = objv[i + 1];
2036
2037 if (Tcl_GetIndexFromObj(interp, optionPtr, optionStrings, "option",
2038 0, &index) != TCL_OK) {
2039 goto cleanup;
2040 }
2041 if (i + 1 == objc) {
2042 string = Tcl_GetStringFromObj(optionPtr, NULL);
2043 Tcl_AppendResult(interp, "value for \"", string, "\" missing",
2044 (char *) NULL);
2045 goto cleanup;
2046 }
2047
2048 string = Tcl_GetStringFromObj(valuePtr, NULL);
2049 switch ((enum options) index) {
2050 case DIR_INITIAL: {
2051 Tcl_DStringFree(&utfDirString);
2052 if (Tcl_TranslateFileName(interp, string,
2053 &utfDirString) == NULL) {
2054 goto cleanup;
2055 }
2056 break;
2057 }
2058 case DIR_EXIST: {
2059 if (Tcl_GetBooleanFromObj(interp, valuePtr, &mustExist) != TCL_OK) {
2060 goto cleanup;
2061 }
2062 break;
2063 }
2064 case DIR_PARENT: {
2065 tkwin = Tk_NameToWindow(interp, string, tkwin);
2066 if (tkwin == NULL) {
2067 goto cleanup;
2068 }
2069 break;
2070 }
2071 case FILE_TITLE: {
2072 utfTitle = string;
2073 break;
2074 }
2075 }
2076 }
2077
2078 Tk_MakeWindowExist(tkwin);
2079 hWnd = Tk_GetHWND(Tk_WindowId(tkwin));
2080
2081 cd.interp = interp;
2082 cd.ofnPtr = &ofn;
2083
2084 ofn.lStructSize = sizeof(ofn);
2085 ofn.hwndOwner = hWnd;
2086 #ifdef _WIN64
2087 ofn.hInstance = (HINSTANCE) GetWindowLongPtr(ofn.hwndOwner,
2088 GWLP_HINSTANCE);
2089 #else
2090 ofn.hInstance = (HINSTANCE) GetWindowLong(ofn.hwndOwner,
2091 GWL_HINSTANCE);
2092 #endif
2093 ofn.lpstrFilter = NULL;
2094 ofn.lpstrCustomFilter = NULL;
2095 ofn.nMaxCustFilter = 0;
2096 ofn.nFilterIndex = 0;
2097 ofn.lpstrFile = NULL; //(TCHAR *) path;
2098 ofn.nMaxFile = MAX_PATH;
2099 ofn.lpstrFileTitle = NULL;
2100 ofn.nMaxFileTitle = 0;
2101 ofn.lpstrInitialDir = NULL;
2102 ofn.lpstrTitle = NULL;
2103 ofn.Flags = OFN_HIDEREADONLY
2104 | OFN_ENABLEHOOK | OFN_ENABLETEMPLATE;
2105 ofn.nFileOffset = 0;
2106 ofn.nFileExtension = 0;
2107 ofn.lpstrDefExt = NULL;
2108 ofn.lCustData = (LPARAM) &cd;
2109 ofn.lpfnHook = (LPOFNHOOKPROC) ChooseDirectoryHookProc;
2110 ofn.lpTemplateName = MAKEINTRESOURCE(FILEOPENORD);
2111
2112 if (Tcl_DStringValue(&utfDirString)[0] != '\0') {
2113 Tcl_UtfToExternalDString(NULL, Tcl_DStringValue(&utfDirString),
2114 Tcl_DStringLength(&utfDirString), &dirString);
2115 } else {
2116 /*
2117 * NT 5.0 changed the meaning of lpstrInitialDir, so we have
2118 * to ensure that we set the [pwd] if the user didn't specify
2119 * anything else.
2120 */
2121 Tcl_DString cwd;
2122
2123 Tcl_DStringFree(&utfDirString);
2124 if ((Tcl_GetCwd(interp, &utfDirString) == (char *) NULL) ||
2125 (Tcl_TranslateFileName(interp,
2126 Tcl_DStringValue(&utfDirString), &cwd) == NULL)) {
2127 Tcl_ResetResult(interp);
2128 } else {
2129 Tcl_UtfToExternalDString(NULL, Tcl_DStringValue(&cwd),
2130 Tcl_DStringLength(&cwd), &dirString);
2131 }
2132 Tcl_DStringFree(&cwd);
2133 }
2134 ofn.lpstrInitialDir = (LPTSTR) Tcl_DStringValue(&dirString);
2135
2136 if (mustExist) {
2137 ofn.Flags |= OFN_PATHMUSTEXIST;
2138 }
2139 if (utfTitle != NULL) {
2140 Tcl_UtfToExternalDString(NULL, utfTitle, -1, &titleString);
2141 ofn.lpstrTitle = (LPTSTR) Tcl_DStringValue(&titleString);
2142 }
2143
2144 /*
2145 * Display dialog. The choose directory dialog doesn't preserve the
2146 * current directory, so it must be saved and restored here.
2147 */
2148
2149 GetCurrentDirectory(MAX_PATH, savePath);
2150 mode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
2151 code = GetOpenFileName(&ofn);
2152 Tcl_SetServiceMode(mode);
2153 SetCurrentDirectory(savePath);
2154
2155 /*
2156 * Ensure that hWnd is enabled, because it can happen that we
2157 * have updated the wrapper of the parent, which causes us to
2158 * leave this child disabled (Windows loses sync).
2159 */
2160 EnableWindow(hWnd, 1);
2161
2162 Tcl_ResetResult(interp);
2163 if (code != 0) {
2164 /*
2165 * Change the pathname to the Tcl "normalized" pathname, where
2166 * back slashes are used instead of forward slashes
2167 */
2168
2169 char *p;
2170 Tcl_DString ds;
2171
2172 Tcl_ExternalToUtfDString(NULL, (char *) cd.path, -1, &ds);
2173 for (p = Tcl_DStringValue(&ds); *p != '\0'; p++) {
2174 if (*p == '\\') {
2175 *p = '/';
2176 }
2177 }
2178 Tcl_AppendResult(interp, Tcl_DStringValue(&ds), NULL);
2179 Tcl_DStringFree(&ds);
2180 }
2181
2182 if (ofn.lpstrTitle != NULL) {
2183 Tcl_DStringFree(&titleString);
2184 }
2185 if (ofn.lpstrInitialDir != NULL) {
2186 Tcl_DStringFree(&dirString);
2187 }
2188 result = TCL_OK;
2189
2190 cleanup:
2191 Tcl_DStringFree(&utfDirString);
2192
2193 return result;
2194 }
2195
2196 /*
2197 *----------------------------------------------------------------------
2198 *
2199 * ChooseDirectoryHookProc --
2200 *
2201 * Hook procedure called by the ChooseDirectory dialog to modify
2202 * its default behavior. The ChooseDirectory dialog is really an
2203 * OpenFile dialog with certain controls rearranged and certain
2204 * behaviors changed. For instance, typing a name in the
2205 * ChooseDirectory dialog selects a directory, rather than
2206 * selecting a file.
2207 *
2208 * Results:
2209 * Returns 0 to allow default processing of message, or 1 to
2210 * tell default dialog procedure not to process the message.
2211 *
2212 * Side effects:
2213 * A dialog window is created the first this procedure is called.
2214 * This window is not destroyed and will be reused the next time
2215 * the application invokes the "tk_getOpenFile" or
2216 * "tk_getSaveFile" command.
2217 *
2218 *----------------------------------------------------------------------
2219 */
2220
2221 static UINT APIENTRY
2222 ChooseDirectoryHookProc(
2223 HWND hwnd,
2224 UINT message,
2225 WPARAM wParam,
2226 LPARAM lParam)
2227 {
2228 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
2229 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
2230 OPENFILENAME *ofnPtr;
2231 ChooseDir *cdPtr;
2232
2233 if (message == WM_INITDIALOG) {
2234 ofnPtr = (OPENFILENAME *) lParam;
2235 cdPtr = (ChooseDir *) ofnPtr->lCustData;
2236 cdPtr->lastCtrl = 0;
2237 cdPtr->lastIdx = 1000;
2238 cdPtr->path[0] = '\0';
2239 #ifdef _WIN64
2240 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) cdPtr);
2241 #else
2242 SetWindowLong(hwnd, GWL_USERDATA, (LONG) cdPtr);
2243 #endif
2244
2245 if (ofnPtr->lpstrInitialDir == NULL) {
2246 GetCurrentDirectory(MAX_PATH, cdPtr->path);
2247 } else {
2248 lstrcpy(cdPtr->path, ofnPtr->lpstrInitialDir);
2249 }
2250 SetDlgItemText(hwnd, edt10, cdPtr->path);
2251 SendDlgItemMessage(hwnd, edt10, EM_SETSEL, 0, -1);
2252 if (tsdPtr->debugFlag) {
2253 tsdPtr->debugInterp = cdPtr->interp;
2254 Tcl_DoWhenIdle(SetTkDialog, (ClientData) hwnd);
2255 }
2256 return 0;
2257 }
2258
2259 /*
2260 * GWL_USERDATA keeps track of cdPtr.
2261 */
2262
2263 #ifdef _WIN64
2264 cdPtr = (ChooseDir *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
2265 #else
2266 cdPtr = (ChooseDir *) GetWindowLong(hwnd, GWL_USERDATA);
2267 #endif
2268 if (cdPtr == NULL) {
2269 return 0;
2270 }
2271 ofnPtr = cdPtr->ofnPtr;
2272
2273 if (message == tsdPtr->WM_LBSELCHANGED) {
2274 /*
2275 * Called when double-clicking on directory.
2276 * If directory wasn't already open, browse that directory.
2277 * If directory was already open, return selected directory.
2278 */
2279
2280 int idCtrl, thisItem;
2281
2282 idCtrl = (int) wParam;
2283 thisItem = LOWORD(lParam);
2284
2285 GetCurrentDirectory(MAX_PATH, cdPtr->path);
2286 if (idCtrl == lst2) {
2287 if (cdPtr->lastIdx == thisItem) {
2288 EndDialog(hwnd, IDOK);
2289 return 1;
2290 }
2291 cdPtr->lastIdx = thisItem;
2292 }
2293 SetDlgItemText(hwnd, edt10, cdPtr->path);
2294 SendDlgItemMessage(hwnd, edt10, EM_SETSEL, 0, -1);
2295 } else if (message == WM_COMMAND) {
2296 int idCtrl, notifyCode;
2297
2298 idCtrl = LOWORD(wParam);
2299 notifyCode = HIWORD(wParam);
2300
2301 if ((idCtrl != IDOK) || (notifyCode != BN_CLICKED)) {
2302 /*
2303 * OK Button wasn't clicked. Do the default.
2304 */
2305
2306 if ((idCtrl == lst2) || (idCtrl == edt10)) {
2307 cdPtr->lastCtrl = idCtrl;
2308 }
2309 return 0;
2310 }
2311
2312 /*
2313 * Dialogs also get the message that OK was clicked when Enter
2314 * is pressed in some other control. Find out what window
2315 * we were really in when we got the supposed "OK", because the
2316 * behavior is different.
2317 */
2318
2319 if (cdPtr->lastCtrl == edt10) {
2320 /*
2321 * Hit Enter or clicked OK while typing a directory name in the
2322 * edit control.
2323 * If it's a new name, try to go to that directory.
2324 * If the name hasn't changed since last time, return selected
2325 * directory.
2326 */
2327
2328 int changed;
2329 TCHAR tmp[MAX_PATH];
2330
2331 if (GetDlgItemText(hwnd, edt10, tmp, MAX_PATH) == 0) {
2332 return 0;
2333 }
2334
2335 changed = lstrcmp(cdPtr->path, tmp);
2336 lstrcpy(cdPtr->path, tmp);
2337
2338 if (SetCurrentDirectory(cdPtr->path) == 0) {
2339 /*
2340 * Non-existent directory.
2341 */
2342
2343 if (ofnPtr->Flags & OFN_PATHMUSTEXIST) {
2344 /*
2345 * Directory must exist. Complain, then rehighlight text.
2346 */
2347
2348 wsprintf(tmp, _T("Cannot change directory to \"%.200s\"."),
2349 cdPtr->path);
2350 MessageBox(hwnd, tmp, NULL, MB_OK);
2351 SendDlgItemMessage(hwnd, edt10, EM_SETSEL, 0, -1);
2352 return 0;
2353 }
2354 if (changed) {
2355 /*
2356 * Directory was invalid, but we want to keep displaying
2357 * this name. Don't update the listbox that displays the
2358 * current directory heirarchy, or it'll erase the name.
2359 */
2360
2361 SendDlgItemMessage(hwnd, edt10, EM_SETSEL, 0, -1);
2362 return 0;
2363 }
2364 }
2365 if (changed == 0) {
2366 /*
2367 * Name hasn't changed since the last time we hit return
2368 * or double-clicked on a directory, so return this.
2369 */
2370
2371 EndDialog(hwnd, IDOK);
2372 return 1;
2373 }
2374
2375 cdPtr->lastCtrl = IDOK;
2376
2377 /*
2378 * The following is the magic code, determined by running
2379 * Spy++ on some other directory chooser, that it takes to
2380 * get this dialog to update the listbox to display the
2381 * current directory.
2382 */
2383
2384 SetDlgItemText(hwnd, edt1, cdPtr->path);
2385 SendMessage(hwnd, WM_COMMAND, (WPARAM) MAKELONG(cmb2, 0x8003),
2386 (LPARAM) GetDlgItem(hwnd, cmb2));
2387 return 0;
2388 } else if (idCtrl == lst2) {
2389 /*
2390 * Enter key was pressed while in listbox.
2391 * If it's a new directory, allow default behavior to open dir.
2392 * If the directory hasn't changed, return selected directory.
2393 */
2394
2395 int thisItem;
2396
2397 thisItem = (int) SendDlgItemMessage(hwnd, lst2, LB_GETCURSEL, 0, 0);
2398 if (cdPtr->lastIdx == thisItem) {
2399 GetCurrentDirectory(MAX_PATH, cdPtr->path);
2400 EndDialog(hwnd, IDOK);
2401 return 1;
2402 }
2403 } else if (idCtrl == IDOK) {
2404 /*
2405 * The OK button was clicked. Return the value currently selected
2406 * in the entry.
2407 */
2408
2409 GetCurrentDirectory(MAX_PATH, cdPtr->path);
2410 EndDialog(hwnd, IDOK);
2411 return 1;
2412 }
2413 }
2414 return 0;
2415 }
2416 #endif
2417
2418 /*
2419 *----------------------------------------------------------------------
2420 *
2421 * Tk_MessageBoxObjCmd --
2422 *
2423 * This procedure implements the MessageBox window for the
2424 * Windows platform. See the user documentation for details on what
2425 * it does.
2426 *
2427 * Results:
2428 * See user documentation.
2429 *
2430 * Side effects:
2431 * None. The MessageBox window will be destroy before this procedure
2432 * returns.
2433 *
2434 *----------------------------------------------------------------------
2435 */
2436
2437 int
2438 Tk_MessageBoxObjCmd(clientData, interp, objc, objv)
2439 ClientData clientData; /* Main window associated with interpreter. */
2440 Tcl_Interp *interp; /* Current interpreter. */
2441 int objc; /* Number of arguments. */
2442 Tcl_Obj *CONST objv[]; /* Argument objects. */
2443 {
2444 Tk_Window tkwin, parent;
2445 HWND hWnd;
2446 char *message, *title;
2447 int defaultBtn, icon, type;
2448 int i, oldMode, winCode;
2449 UINT flags;
2450 Tcl_DString messageString, titleString;
2451 Tcl_Encoding unicodeEncoding = TkWinGetUnicodeEncoding();
2452 static CONST char *optionStrings[] = {
2453 "-default", "-icon", "-message", "-parent",
2454 "-title", "-type", NULL
2455 };
2456 enum options {
2457 MSG_DEFAULT, MSG_ICON, MSG_MESSAGE, MSG_PARENT,
2458 MSG_TITLE, MSG_TYPE
2459 };
2460
2461 tkwin = (Tk_Window) clientData;
2462
2463 defaultBtn = -1;
2464 icon = MB_ICONINFORMATION;
2465 message = NULL;
2466 parent = tkwin;
2467 title = NULL;
2468 type = MB_OK;
2469
2470 for (i = 1; i < objc; i += 2) {
2471 int index;
2472 char *string;
2473 Tcl_Obj *optionPtr, *valuePtr;
2474
2475 optionPtr = objv[i];
2476 valuePtr = objv[i + 1];
2477
2478 if (Tcl_GetIndexFromObj(interp, optionPtr, optionStrings, "option",
2479 TCL_EXACT, &index) != TCL_OK) {
2480 return TCL_ERROR;
2481 }
2482 if (i + 1 == objc) {
2483 string = Tcl_GetStringFromObj(optionPtr, NULL);
2484 Tcl_AppendResult(interp, "value for \"", string, "\" missing",
2485 (char *) NULL);
2486 return TCL_ERROR;
2487 }
2488
2489 string = Tcl_GetStringFromObj(valuePtr, NULL);
2490 switch ((enum options) index) {
2491 case MSG_DEFAULT:
2492 defaultBtn = TkFindStateNumObj(interp, optionPtr, buttonMap,
2493 valuePtr);
2494 if (defaultBtn < 0) {
2495 return TCL_ERROR;
2496 }
2497 break;
2498
2499 case MSG_ICON:
2500 icon = TkFindStateNumObj(interp, optionPtr, iconMap, valuePtr);
2501 if (icon < 0) {
2502 return TCL_ERROR;
2503 }
2504 break;
2505
2506 case MSG_MESSAGE:
2507 message = string;
2508 break;
2509
2510 case MSG_PARENT:
2511 parent = Tk_NameToWindow(interp, string, tkwin);
2512 if (parent == NULL) {
2513 return TCL_ERROR;
2514 }
2515 break;
2516
2517 case MSG_TITLE:
2518 title = string;
2519 break;
2520
2521 case MSG_TYPE:
2522 type = TkFindStateNumObj(interp, optionPtr, typeMap, valuePtr);
2523 if (type < 0) {
2524 return TCL_ERROR;
2525 }
2526 break;
2527
2528 }
2529 }
2530
2531 Tk_MakeWindowExist(parent);
2532 hWnd = Tk_GetHWND(Tk_WindowId(parent));
2533
2534 flags = 0;
2535 if (defaultBtn >= 0) {
2536 int defaultBtnIdx;
2537
2538 defaultBtnIdx = -1;
2539 for (i = 0; i < NUM_TYPES; i++) {
2540 if (type == allowedTypes[i].type) {
2541 int j;
2542
2543 for (j = 0; j < 3; j++) {
2544 if (allowedTypes[i].btnIds[j] == defaultBtn) {
2545 defaultBtnIdx = j;
2546 break;
2547 }
2548 }
2549 if (defaultBtnIdx < 0) {
2550 Tcl_AppendResult(interp, "invalid default button \"",
2551 TkFindStateString(buttonMap, defaultBtn),
2552 "\"", NULL);
2553 return TCL_ERROR;
2554 }
2555 break;
2556 }
2557 }
2558 flags = buttonFlagMap[defaultBtnIdx];
2559 }
2560
2561 flags |= icon | type | MB_SYSTEMMODAL;
2562
2563 Tcl_UtfToExternalDString(unicodeEncoding, message, -1, &messageString);
2564 Tcl_UtfToExternalDString(unicodeEncoding, title, -1, &titleString);
2565
2566 oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
2567 /*
2568 * MessageBoxW exists for all platforms. Use it to allow unicode
2569 * error message to be displayed correctly where possible by the OS.
2570 */
2571 winCode = MessageBoxW(hWnd, (WCHAR *) Tcl_DStringValue(&messageString),
2572 (WCHAR *) Tcl_DStringValue(&titleString), flags);
2573 (void) Tcl_SetServiceMode(oldMode);
2574
2575 /*
2576 * Ensure that hWnd is enabled, because it can happen that we
2577 * have updated the wrapper of the parent, which causes us to
2578 * leave this child disabled (Windows loses sync).
2579 */
2580 EnableWindow(hWnd, 1);
2581
2582 Tcl_DStringFree(&messageString);
2583 Tcl_DStringFree(&titleString);
2584
2585 Tcl_SetResult(interp, TkFindStateString(buttonMap, winCode), TCL_STATIC);
2586 return TCL_OK;
2587 }
2588
2589 static void
2590 SetTkDialog(ClientData clientData)
2591 {
2592 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
2593 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
2594 char buf[32];
2595
2596 sprintf(buf, "0x%p", (HWND) clientData);
2597 Tcl_SetVar(tsdPtr->debugInterp, "tk_dialog", buf, TCL_GLOBAL_ONLY);
2598 }
2599
2600