Attachment "tk-HEAD-tkWinDialog.patch" to
ticket [3160703f]
added by
zoro2
2011-01-18 17:59:10.
--- win/tkWinDialog.c 2010-11-24 09:08:08.000000000 +0100
+++ win/tkWinDialog.c 2011-01-18 10:45:19.000000000 +0100
@@ -53,6 +53,11 @@
#define OPENFILENAME_SIZE_VERSION_400 76
#endif
+#ifdef __WIN32__
+ TCHAR * (*tcl_WinUtfToTChar) _ANSI_ARGS_((CONST char * str, int len, Tcl_DString * dsPtr)); /* 0 */
+ char * (*tcl_WinTCharToUtf) _ANSI_ARGS_((CONST TCHAR * str, int len, Tcl_DString * dsPtr)); /* 1 */
+#endif /* __WIN32__ */
+
/*
* The following structure is used by the new Tk_ChooseDirectoryObjCmd to pass
* data between it and its callback. Unique to Windows platform.
@@ -66,6 +71,14 @@
* callback */
} CHOOSEDIRDATA;
+typedef struct ChooseDirDataW {
+ wchar_t utfInitDir[MAX_PATH]; /* Initial folder to use */
+ wchar_t utfRetDir[MAX_PATH]; /* Returned folder to use */
+ Tcl_Interp *interp;
+ int mustExist; /* true if file must exist to return from
+ * callback */
+} CHOOSEDIRDATAW;
+
typedef struct ThreadSpecificData {
int debugFlag; /* Flags whether we should output debugging
* information while displaying a builtin
@@ -195,6 +208,8 @@
static UINT APIENTRY ChooseDirectoryValidateProc(HWND hdlg, UINT uMsg,
LPARAM wParam, LPARAM lParam);
+static UINT APIENTRY ChooseDirectoryValidateProcW(HWND hdlg, UINT uMsg,
+ LPARAM wParam, LPARAM lParam);
static UINT CALLBACK ColorDlgHookProc(HWND hDlg, UINT uMsg, WPARAM wParam,
LPARAM lParam);
static int GetFileName(ClientData clientData,
@@ -1248,6 +1263,211 @@
TkFreeFileFilters(&flist);
return TCL_OK;
}
+
+/*
+ * Use Unicode version of Tk_ChooseDirectory for handling non-ANSI characters properly
+ * Added BitRock SL; licensed under the Tcl license terms
+ */
+ int
+Tk_ChooseDirectoryObjCmdW(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. */
+{
+ wchar_t path[MAX_PATH];
+ int oldMode, result, i;
+ LPCITEMIDLIST pidl; /* Returned by browser */
+ BROWSEINFOW bInfo; /* Used by browser */
+ CHOOSEDIRDATAW cdCBData; /* Structure to pass back and forth */
+ LPMALLOC pMalloc; /* Used by shell */
+
+ Tk_Window tkwin;
+ HWND hWnd;
+ char *utfTitle; /* Title for window */
+ wchar_t saveDir[MAX_PATH];
+ Tcl_DString titleString; /* UTF Title */
+ Tcl_DString initDirString, dirString; /* Initial directory */
+ Tcl_Encoding unicodeEncoding = TkWinGetUnicodeEncoding(); /* XXXXXX */
+ static CONST char *optionStrings[] = {
+ "-initialdir", "-mustexist", "-parent", "-title", (char *) NULL
+ };
+ enum options {
+ DIR_INITIAL, DIR_EXIST, DIR_PARENT, FILE_TITLE
+ };
+
+ /*
+ * Initialize
+ */
+ result = TCL_ERROR;
+ path[0] = '\0';
+ path[1] = '\0';
+ utfTitle = NULL;
+
+ ZeroMemory(&cdCBData, sizeof(CHOOSEDIRDATAW));
+ cdCBData.interp = interp;
+
+ tkwin = (Tk_Window) clientData;
+ /*
+ * Process the command line options
+ */
+ 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 cleanup;
+ }
+ if (i + 1 == objc) {
+ string = Tcl_GetStringFromObj(optionPtr, NULL);
+ Tcl_AppendResult(interp, "value for \"", string, "\" missing",
+ (char *) NULL);
+ goto cleanup;
+ }
+
+ string = Tcl_GetString(valuePtr);
+ switch ((enum options) index) {
+ case DIR_INITIAL: {
+ if (Tcl_TranslateFileName(interp, string,
+ &initDirString) == NULL) {
+ goto cleanup;
+ }
+
+ Tcl_WinUtfToTChar(Tcl_DStringValue(&initDirString),-1,&dirString);
+
+ /*string = Tcl_DStringValue(&initDirString);*/
+ /*
+ * Convert possible relative path to full path to keep
+ * dialog happy
+ */
+ GetFullPathNameW((WCHAR *) Tcl_DStringValue(&dirString), MAX_PATH, saveDir, NULL);
+ wcsncpy(cdCBData.utfInitDir, saveDir, MAX_PATH);
+ Tcl_DStringFree(&initDirString);
+ Tcl_DStringFree(&dirString);
+ break;
+ }
+ case DIR_EXIST: {
+ if (Tcl_GetBooleanFromObj(interp, valuePtr,
+ &cdCBData.mustExist) != TCL_OK) {
+ goto cleanup;
+ }
+ break;
+ }
+ case DIR_PARENT: {
+ tkwin = Tk_NameToWindow(interp, string, tkwin);
+ if (tkwin == NULL) {
+ goto cleanup;
+ }
+ break;
+ }
+ case FILE_TITLE: {
+ utfTitle = string;
+ break;
+ }
+ }
+ }
+
+ /*
+ * Get ready to call the browser
+ */
+
+ Tk_MakeWindowExist(tkwin);
+ hWnd = Tk_GetHWND(Tk_WindowId(tkwin));
+
+ /*
+ * Setup the parameters used by SHBrowseForFolder
+ */
+
+ bInfo.hwndOwner = hWnd;
+ bInfo.pszDisplayName = path;
+ bInfo.pidlRoot = NULL;
+ if (wcslen(cdCBData.utfInitDir) == 0) {
+ GetCurrentDirectoryW(MAX_PATH, cdCBData.utfInitDir);
+ }
+ bInfo.lParam = (LPARAM) &cdCBData;
+
+ if (utfTitle != NULL) {
+ bInfo.lpszTitle = (WCHAR*) Tcl_WinUtfToTChar(utfTitle,-1,&titleString);
+ } else {
+ bInfo.lpszTitle = L"Please choose a directory, then select OK.";
+ }
+
+ /*
+ * Set flags to add edit box (needs 4.71 Shell DLLs), status text line,
+ * validate edit box and
+ */
+ bInfo.ulFlags = BIF_EDITBOX | BIF_STATUSTEXT | BIF_RETURNFSANCESTORS
+ | BIF_VALIDATE;
+ /*
+ * Callback to handle events
+ */
+ bInfo.lpfn = (BFFCALLBACK) ChooseDirectoryValidateProcW;
+
+ /*
+ * Display dialog in background and process result.
+ * We look to give the user a chance to change their mind
+ * on an invalid folder if mustexist is 0;
+ */
+
+ oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
+ GetCurrentDirectoryW(MAX_PATH, saveDir);
+ if (SHGetMalloc(&pMalloc) == NOERROR) {
+ pidl = SHBrowseForFolderW(&bInfo);
+ /* Null for cancel button or invalid dir, otherwise valid*/
+ if (pidl != NULL) {
+ if (!SHGetPathFromIDListW(pidl, path)) {
+ Tcl_SetResult(interp, "Error: Not a file system folder\n",
+ TCL_VOLATILE);
+ };
+ pMalloc->lpVtbl->Free(pMalloc, (void *) pidl);
+ } else if (wcslen(cdCBData.utfRetDir) > 0) {
+ wcscpy(path, cdCBData.utfRetDir);
+ }
+ pMalloc->lpVtbl->Release(pMalloc);
+ }
+ SetCurrentDirectoryW(saveDir);
+ 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);
+
+ /*
+ * Change the pathname to the Tcl "normalized" pathname, where
+ * back slashes are used instead of forward slashes
+ */
+ Tcl_ResetResult(interp);
+ if (*path) {
+ Tcl_DString ds;
+ wchar_t *p;
+ for (p = path; *p != '\0'; p++) {
+ if (*p == '\\') {
+ *p = '/';
+ }
+ }
+ Tcl_WinTCharToUtf((const char *)path,-1,&ds);
+ Tcl_AppendResult(interp, Tcl_DStringValue(&ds), NULL);
+ Tcl_DStringFree(&ds);
+ }
+
+ result = TCL_OK;
+
+ if (utfTitle != NULL) {
+ Tcl_DStringFree(&titleString);
+ }
+
+ cleanup:
+ return result;
+}
+
/*
*----------------------------------------------------------------------
@@ -1324,7 +1544,7 @@
*/
int
-Tk_ChooseDirectoryObjCmd(
+Tk_ChooseDirectoryObjCmdA(
ClientData clientData, /* Main window associated with interpreter. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
@@ -1532,7 +1752,21 @@
cleanup:
return result;
}
-
+
+int
+Tk_ChooseDirectoryObjCmd(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 Tk_ChooseDirectoryObjCmdW(clientData, interp, objc, objv);
+ } else {
+ return Tk_ChooseDirectoryObjCmdA(clientData, interp, objc, objv);
+ }
+}
+
/*
*----------------------------------------------------------------------
*
@@ -1549,6 +1783,149 @@
*----------------------------------------------------------------------
*/
+
+static UINT APIENTRY
+ChooseDirectoryValidateProcW (
+ HWND hwnd,
+ UINT message,
+ LPARAM lParam,
+ LPARAM lpData)
+{
+ wchar_t selDir[MAX_PATH];
+ CHOOSEDIRDATAW *chooseDirSharedData;
+ Tcl_DString initDirString,dirString;
+ wchar_t string[MAX_PATH];
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
+
+ chooseDirSharedData = (CHOOSEDIRDATAW *)lpData;
+
+#ifdef _WIN64
+ SetWindowLongPtrW(hwnd, GWLP_USERDATA, lpData);
+#else
+ SetWindowLongW(hwnd, GWL_USERDATA, lpData);
+#endif
+
+ if (tsdPtr->debugFlag) {
+ tsdPtr->debugInterp = (Tcl_Interp *) chooseDirSharedData->interp;
+ Tcl_DoWhenIdle(SetTkDialog, (ClientData) hwnd);
+ }
+ chooseDirSharedData->utfRetDir[0] = '\0';
+ chooseDirSharedData->utfRetDir[1] = '\0';
+ switch (message) {
+ case BFFM_VALIDATEFAILED:
+ /*
+ * First save and check to see if it is a valid path name, if
+ * so then make that path the one shown in the
+ * window. Otherwise, it failed the check and should be treated
+ * as such. Use Set/GetCurrentDirectory which allows relative
+ * path names and names with forward slashes. Use
+ * Tcl_TranslateFileName to make sure names like ~ are
+ * converted correctly.
+ */
+ Tcl_WinTCharToUtf((const char *)lParam,-1,&initDirString);
+ Tcl_TranslateFileName(chooseDirSharedData->interp,
+ Tcl_DStringValue(&initDirString), &dirString);
+ wcsncpy(string, (WCHAR*) Tcl_DStringValue(&dirString), MAX_PATH);
+ Tcl_DStringFree(&initDirString);
+
+ if (SetCurrentDirectoryW((WCHAR *)string) == 0) {
+ /*LPTSTR lpFilePart[MAX_PATH];*/
+
+ /*
+ * Get the full path name to the user entry,
+ * at this point it doesn't exist so see if
+ * it is supposed to. Otherwise just return it.
+ */
+ GetFullPathNameW(string, MAX_PATH,
+ chooseDirSharedData->utfRetDir, /*unused*/ NULL);
+ if (chooseDirSharedData->mustExist) {
+ /*
+ * User HAS to select a valid directory.
+ */
+ swprintf(selDir, L"Directory '%.200s' does not exist,\nplease select or enter an existing directory.", chooseDirSharedData->utfRetDir);
+ MessageBoxW(NULL, selDir, NULL, MB_ICONEXCLAMATION|MB_OK);
+ chooseDirSharedData->utfRetDir[0] = '\0';
+ chooseDirSharedData->utfRetDir[1] = '\0';
+ return 1;
+ }
+ } else {
+ /*
+ * Changed to new folder OK, return immediatly with the
+ * current directory in utfRetDir.
+ */
+ GetCurrentDirectoryW(MAX_PATH, chooseDirSharedData->utfRetDir);
+ return 0;
+ }
+ return 0;
+
+ case BFFM_SELCHANGED:
+ /*
+ * Set the status window to the currently selected path.
+ * And enable the OK button if a file system folder, otherwise
+ * disable the OK button for things like server names.
+ * perhaps a new switch -enablenonfolders can be used to allow
+ * non folders to be selected.
+ *
+ * Not called when user changes edit box directly.
+ */
+
+ if (SHGetPathFromIDListW((LPITEMIDLIST) lParam, selDir)) {
+ SendMessageW(hwnd, BFFM_SETSTATUSTEXTW, 0, (LPARAM) selDir);
+ // enable the OK button
+ SendMessage(hwnd, BFFM_ENABLEOK, 0, (LPARAM) 1);
+ SetCurrentDirectoryW(selDir);
+ } else {
+ // disable the OK button
+ SendMessage(hwnd, BFFM_ENABLEOK, 0, (LPARAM) 0);
+ }
+ UpdateWindow(hwnd);
+ return 1;
+
+ case BFFM_INITIALIZED: {
+ /*
+ * Directory browser intializing - tell it where to start from,
+ * user specified parameter.
+ */
+ wchar_t *initDir = chooseDirSharedData->utfInitDir;
+ SetCurrentDirectoryW(initDir);
+ if (*initDir == L'\\') {
+
+ /*
+ * BFFM_SETSELECTION only understands UNC paths as pidls,
+ * so convert path to pidl using IShellFolder interface.
+ */
+ LPMALLOC pMalloc;
+ LPSHELLFOLDER psfFolder;
+
+ if (SUCCEEDED(SHGetMalloc(&pMalloc))) {
+ if (SUCCEEDED(SHGetDesktopFolder(&psfFolder))) {
+ LPITEMIDLIST pidlMain;
+ ULONG ulCount, ulAttr;
+ if (SUCCEEDED(psfFolder->lpVtbl->ParseDisplayName(
+ psfFolder, hwnd, NULL,
+ initDir,
+ &ulCount, &pidlMain, &ulAttr))
+ && (pidlMain != NULL)) {
+ SendMessage(hwnd, BFFM_SETSELECTION, FALSE,
+ (LPARAM)pidlMain);
+ pMalloc->lpVtbl->Free(pMalloc, pidlMain);
+ }
+ psfFolder->lpVtbl->Release(psfFolder);
+ }
+ pMalloc->lpVtbl->Release(pMalloc);
+ }
+ } else {
+ SendMessageW(hwnd, BFFM_SETSELECTIONW, TRUE, (LPARAM)initDir);
+ }
+ SendMessage(hwnd, BFFM_ENABLEOK, 0, (LPARAM) 1);
+ break;
+ }
+
+ }
+ return 0;
+}
+
static UINT APIENTRY
ChooseDirectoryValidateProc(
HWND hwnd,
Only in ./win: .tkWinDialog.c.swp