From 7571c73ea570497dcb0c8900ba99fcef9f4cbbfe Mon Sep 17 00:00:00 2001 From: dgp Date: Tue, 4 Oct 2011 17:53:34 +0000 Subject: Contributed implementation patch for TIP 382. --- library/tkfbox.tcl | 8 +++++++- win/tkWinDialog.c | 14 ++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/library/tkfbox.tcl b/library/tkfbox.tcl index 4ccf3d7..ffffb3e 100644 --- a/library/tkfbox.tcl +++ b/library/tkfbox.tcl @@ -983,6 +983,12 @@ proc ::tk::dialog::file::Config {dataName type argList} { lappend specs {-multiple "" "" "0"} } + # The "-nocomplain" option is only available for the "save" file dialog. + # + if {$type eq "save"} { + lappend specs {-nocomplain "" "" "0"} + } + # 2: default values depending on the type of the dialog # if {![info exists data(selectPath)]} { @@ -1859,7 +1865,7 @@ proc ::tk::dialog::file::Done {w {selectFilePath ""}} { set Priv(selectFile) $data(selectFile) set Priv(selectPath) $data(selectPath) - if {($data(type) eq "save") && [file exists $selectFilePath]} { + if {($data(type) eq "save") && !$data(-nocomplain) && [file exists $selectFilePath]} { set reply [tk_messageBox -icon warning -type yesno -parent $w \ -message [mc "File \"%1\$s\" already exists.\nDo you want\ to overwrite it?" $selectFilePath]] diff --git a/win/tkWinDialog.c b/win/tkWinDialog.c index de7e84f..aca8e5d 100644 --- a/win/tkWinDialog.c +++ b/win/tkWinDialog.c @@ -591,7 +591,7 @@ GetFileNameW( WCHAR file[TK_MULTI_MAX_PATH]; OFNData ofnData; int cdlgerr; - int filterIndex = 0, result = TCL_ERROR, winCode, oldMode, i, multi = 0; + int filterIndex = 0, result = TCL_ERROR, winCode, oldMode, i, multi = 0, noComplain = 0; char *extension = NULL, *title = NULL; Tk_Window tkwin = (Tk_Window) clientData; HWND hWnd; @@ -603,7 +603,7 @@ GetFileNameW( Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); static CONST char *saveOptionStrings[] = { "-defaultextension", "-filetypes", "-initialdir", "-initialfile", - "-parent", "-title", "-typevariable", NULL + "-parent", "-title", "-typevariable", "-nocomplain", NULL }; static CONST char *openOptionStrings[] = { "-defaultextension", "-filetypes", "-initialdir", "-initialfile", @@ -613,7 +613,8 @@ GetFileNameW( enum options { FILE_DEFAULT, FILE_TYPES, FILE_INITDIR, FILE_INITFILE, - FILE_MULTIPLE, FILE_PARENT, FILE_TITLE, FILE_TYPEVARIABLE + FILE_MULTIPLE, FILE_PARENT, FILE_TITLE, FILE_TYPEVARIABLE, + FILE_NOCOMPLAIN }; file[0] = '\0'; @@ -712,6 +713,11 @@ GetFileNameW( initialTypeObj = Tcl_ObjGetVar2(interp, typeVariableObj, NULL, TCL_GLOBAL_ONLY); break; + case FILE_NOCOMPLAIN: + if (Tcl_GetBooleanFromObj(interp, valuePtr, &noComplain) != TCL_OK) { + return TCL_ERROR; + } + break; } } @@ -740,7 +746,7 @@ GetFileNameW( if (open != 0) { ofn.Flags |= OFN_FILEMUSTEXIST; - } else { + } else if (noComplain == 0) { ofn.Flags |= OFN_OVERWRITEPROMPT; } if (tsdPtr->debugFlag != 0) { -- cgit v0.12 From 7f0c75ac2e61b2457146f5b2a39e252b250196b5 Mon Sep 17 00:00:00 2001 From: dgp Date: Wed, 5 Oct 2011 19:18:20 +0000 Subject: Revise the proposed "-nocomplain" option to "-confirmoverwrite" --- library/tkfbox.tcl | 4 ++-- win/tkWinDialog.c | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/library/tkfbox.tcl b/library/tkfbox.tcl index ffffb3e..214a31b 100644 --- a/library/tkfbox.tcl +++ b/library/tkfbox.tcl @@ -986,7 +986,7 @@ proc ::tk::dialog::file::Config {dataName type argList} { # The "-nocomplain" option is only available for the "save" file dialog. # if {$type eq "save"} { - lappend specs {-nocomplain "" "" "0"} + lappend specs {-confirmoverwrite "" "" "1"} } # 2: default values depending on the type of the dialog @@ -1865,7 +1865,7 @@ proc ::tk::dialog::file::Done {w {selectFilePath ""}} { set Priv(selectFile) $data(selectFile) set Priv(selectPath) $data(selectPath) - if {($data(type) eq "save") && !$data(-nocomplain) && [file exists $selectFilePath]} { + if {($data(type) eq "save") && $data(-confirmoverwrite) && [file exists $selectFilePath]} { set reply [tk_messageBox -icon warning -type yesno -parent $w \ -message [mc "File \"%1\$s\" already exists.\nDo you want\ to overwrite it?" $selectFilePath]] diff --git a/win/tkWinDialog.c b/win/tkWinDialog.c index aca8e5d..457f0e9 100644 --- a/win/tkWinDialog.c +++ b/win/tkWinDialog.c @@ -591,7 +591,8 @@ GetFileNameW( WCHAR file[TK_MULTI_MAX_PATH]; OFNData ofnData; int cdlgerr; - int filterIndex = 0, result = TCL_ERROR, winCode, oldMode, i, multi = 0, noComplain = 0; + int filterIndex = 0, result = TCL_ERROR, winCode, oldMode, i, multi = 0; + int confirmOverwrite = 1; char *extension = NULL, *title = NULL; Tk_Window tkwin = (Tk_Window) clientData; HWND hWnd; @@ -602,8 +603,9 @@ GetFileNameW( ThreadSpecificData *tsdPtr = (ThreadSpecificData *) Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); static CONST char *saveOptionStrings[] = { - "-defaultextension", "-filetypes", "-initialdir", "-initialfile", - "-parent", "-title", "-typevariable", "-nocomplain", NULL + "-confirmoverwrite", "-defaultextension", "-filetypes", + "-initialdir", "-initialfile", "-parent", "-title", + "-typevariable", "-nocomplain", NULL }; static CONST char *openOptionStrings[] = { "-defaultextension", "-filetypes", "-initialdir", "-initialfile", @@ -612,9 +614,9 @@ GetFileNameW( CONST char **optionStrings; enum options { - FILE_DEFAULT, FILE_TYPES, FILE_INITDIR, FILE_INITFILE, - FILE_MULTIPLE, FILE_PARENT, FILE_TITLE, FILE_TYPEVARIABLE, - FILE_NOCOMPLAIN + FILE_CONFOW, FILE_DEFAULT, FILE_TYPES, FILE_INITDIR, + FILE_INITFILE, FILE_MULTIPLE, FILE_PARENT, FILE_TITLE, + FILE_TYPEVARIABLE }; file[0] = '\0'; @@ -713,8 +715,8 @@ GetFileNameW( initialTypeObj = Tcl_ObjGetVar2(interp, typeVariableObj, NULL, TCL_GLOBAL_ONLY); break; - case FILE_NOCOMPLAIN: - if (Tcl_GetBooleanFromObj(interp, valuePtr, &noComplain) != TCL_OK) { + case FILE_CONFOW: + if (Tcl_GetBooleanFromObj(interp, valuePtr, &confirmOverwrite) != TCL_OK) { return TCL_ERROR; } break; @@ -746,7 +748,7 @@ GetFileNameW( if (open != 0) { ofn.Flags |= OFN_FILEMUSTEXIST; - } else if (noComplain == 0) { + } else if (confirmOverwrite) { ofn.Flags |= OFN_OVERWRITEPROMPT; } if (tsdPtr->debugFlag != 0) { -- cgit v0.12 From 4579ddeec583015c60b677739c143a3224d61c07 Mon Sep 17 00:00:00 2001 From: dgp Date: Mon, 17 Oct 2011 19:54:09 +0000 Subject: Fix up the implementation to account for shared options enumeration. --- library/tkfbox.tcl | 2 +- win/tkWinDialog.c | 63 +++++++++++++++++------------------------------------- 2 files changed, 21 insertions(+), 44 deletions(-) diff --git a/library/tkfbox.tcl b/library/tkfbox.tcl index 214a31b..78112e1 100644 --- a/library/tkfbox.tcl +++ b/library/tkfbox.tcl @@ -983,7 +983,7 @@ proc ::tk::dialog::file::Config {dataName type argList} { lappend specs {-multiple "" "" "0"} } - # The "-nocomplain" option is only available for the "save" file dialog. + # The "-confirmoverwrite" option is only for the "save" file dialog. # if {$type eq "save"} { lappend specs {-confirmoverwrite "" "" "1"} diff --git a/win/tkWinDialog.c b/win/tkWinDialog.c index 457f0e9..6b50215 100644 --- a/win/tkWinDialog.c +++ b/win/tkWinDialog.c @@ -592,7 +592,7 @@ GetFileNameW( OFNData ofnData; int cdlgerr; int filterIndex = 0, result = TCL_ERROR, winCode, oldMode, i, multi = 0; - int confirmOverwrite = 1; + int inValue, confirmOverwrite = 1; char *extension = NULL, *title = NULL; Tk_Window tkwin = (Tk_Window) clientData; HWND hWnd; @@ -603,20 +603,21 @@ GetFileNameW( ThreadSpecificData *tsdPtr = (ThreadSpecificData *) Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); static CONST char *saveOptionStrings[] = { - "-confirmoverwrite", "-defaultextension", "-filetypes", - "-initialdir", "-initialfile", "-parent", "-title", - "-typevariable", "-nocomplain", NULL + "-defaultextension", "-filetypes", "-initialdir", "-initialfile", + "-parent", "-title", "-typevariable", + "-confirmoverwrite", + NULL }; static CONST char *openOptionStrings[] = { "-defaultextension", "-filetypes", "-initialdir", "-initialfile", - "-multiple", "-parent", "-title", "-typevariable", NULL + "-parent", "-title", "-typevariable", + "-multiple", + NULL }; - CONST char **optionStrings; - enum options { - FILE_CONFOW, FILE_DEFAULT, FILE_TYPES, FILE_INITDIR, - FILE_INITFILE, FILE_MULTIPLE, FILE_PARENT, FILE_TITLE, - FILE_TYPEVARIABLE + FILE_DEFAULT, FILE_TYPES, FILE_INITDIR, FILE_INITFILE, + FILE_PARENT, FILE_TITLE, FILE_TYPEVARIABLE, + FILE_MULTIPLE_OR_CONFIRMOW }; file[0] = '\0'; @@ -624,16 +625,6 @@ GetFileNameW( Tcl_DStringInit(&utfFilterString); Tcl_DStringInit(&utfDirString); - /* - * Parse the arguments. - */ - - if (open) { - optionStrings = openOptionStrings; - } else { - optionStrings = saveOptionStrings; - } - for (i = 1; i < objc; i += 2) { int index; char *string; @@ -642,26 +633,12 @@ GetFileNameW( optionPtr = objv[i]; valuePtr = objv[i + 1]; - if (Tcl_GetIndexFromObj(interp, optionPtr, optionStrings, + if (Tcl_GetIndexFromObj(interp, optionPtr, + open ? openOptionStrings : saveOptionStrings, "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_GetString(optionPtr); Tcl_AppendResult(interp, "value for \"", string, "\" missing", @@ -696,11 +673,6 @@ GetFileNameW( sizeof(file), NULL, NULL, NULL); Tcl_DStringFree(&ds); 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) { @@ -715,10 +687,15 @@ GetFileNameW( initialTypeObj = Tcl_ObjGetVar2(interp, typeVariableObj, NULL, TCL_GLOBAL_ONLY); break; - case FILE_CONFOW: - if (Tcl_GetBooleanFromObj(interp, valuePtr, &confirmOverwrite) != TCL_OK) { + case FILE_MULTIPLE_OR_CONFIRMOW: + if (Tcl_GetBooleanFromObj(interp, valuePtr, &inValue) != TCL_OK) { return TCL_ERROR; } + if (open) { + multi = inValue; + } else { + confirmOverwrite = inValue; + } break; } } -- cgit v0.12 From 5281d93c05b1c0faec54131ade30cab376fea319 Mon Sep 17 00:00:00 2001 From: dgp Date: Mon, 17 Oct 2011 20:10:41 +0000 Subject: Implementation for Carbon. --- macosx/tkMacOSXDialog.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/macosx/tkMacOSXDialog.c b/macosx/tkMacOSXDialog.c index 7328f0e..c99cdbc 100644 --- a/macosx/tkMacOSXDialog.c +++ b/macosx/tkMacOSXDialog.c @@ -84,7 +84,8 @@ static int NavServicesGetFile(Tcl_Interp *interp, OpenFileData *ofd, AEDesc *initialDescPtr, char *initialFile, AEDescList *selectDescPtr, CFStringRef title, CFStringRef message, - const char *initialType, int multiple, int isOpen, + const char *initialType, int multiple, + int confirmOverwrite, int isOpen, Tk_Window parent); static int HandleInitialDirectory(Tcl_Interp *interp, char *initialFile, char *initialDir, FSRef *dirRef, @@ -366,7 +367,7 @@ Tk_GetOpenFileObjCmd( TCL_GLOBAL_ONLY); } result = NavServicesGetFile(interp, &ofd, initialPtr, NULL, &selectDesc, - title, message, initialtype, multiple, OPEN_FILE, parent); + title, message, initialtype, multiple, false, OPEN_FILE, parent); if (typeVariablePtr) { FileFilter *filterPtr = ofd.fl.filters; @@ -422,6 +423,7 @@ Tk_GetSaveFileObjCmd( Tcl_Obj *CONST objv[]) /* Argument objects. */ { int i, result = TCL_ERROR; + int confirmOverwrite = 1; char *initialFile = NULL; Tk_Window parent = NULL; AEDesc initialDesc = {typeNull, NULL}; @@ -431,11 +433,13 @@ Tk_GetSaveFileObjCmd( OpenFileData ofd; static const char *saveOptionStrings[] = { "-defaultextension", "-filetypes", "-initialdir", "-initialfile", - "-message", "-parent", "-title", "-typevariable", NULL + "-message", "-parent", "-title", "-typevariable", + "-confirmoverwrite", NULL }; enum saveOptions { SAVE_DEFAULT, SAVE_FILETYPES, SAVE_INITDIR, SAVE_INITFILE, SAVE_MESSAGE, SAVE_PARENT, SAVE_TITLE, SAVE_TYPEVARIABLE, + SAVE_CONFIRMOW }; if (!fileDlgInited) { @@ -508,6 +512,12 @@ Tk_GetSaveFileObjCmd( title = CFStringCreateWithBytes(NULL, (unsigned char *) choice, choiceLen, kCFStringEncodingUTF8, false); break; + case SAVE_CONFIRMOW: + if (Tcl_GetBooleanFromObj(interp, objv[i + 1], &confirmOverwrite) + != TCL_OK) { + return TCL_ERROR; + } + break; } } @@ -515,7 +525,7 @@ Tk_GetSaveFileObjCmd( initialPtr = &initialDesc; } result = NavServicesGetFile(interp, &ofd, initialPtr, initialFile, NULL, - title, message, NULL, false, SAVE_FILE, parent); + title, message, NULL, false, confirmOverwrite, SAVE_FILE, parent); TkFreeFileFilters(&ofd.fl); end: if (initialDesc.dataHandle) { @@ -627,7 +637,7 @@ Tk_ChooseDirectoryObjCmd( initialPtr = &initialDesc; } result = NavServicesGetFile(interp, &ofd, initialPtr, NULL, NULL, title, - message, NULL, false, CHOOSE_FOLDER, parent); + message, NULL, false, false, CHOOSE_FOLDER, parent); TkFreeFileFilters(&ofd.fl); end: if (initialDesc.dataHandle) { @@ -778,6 +788,7 @@ NavServicesGetFile( CFStringRef message, const char *initialtype, int multiple, + int confirmOverwrite, int isOpen, Tk_Window parent) { @@ -799,6 +810,9 @@ NavServicesGetFile( if (multiple) { options.optionFlags |= kNavAllowMultipleFiles; } + if (!confirmOverwrite) { + options.optionFlags |= kNavDontConfirmReplacement; + } options.modality = kWindowModalityAppModal; if (parent && ((TkWindow *) parent)->window != None && TkMacOSXHostToplevelExists(parent)) { -- cgit v0.12 From 4136000e1510b2c72b2e60eaef06bf08c6c6c7e5 Mon Sep 17 00:00:00 2001 From: dgp Date: Wed, 19 Oct 2011 15:03:24 +0000 Subject: add docs --- doc/getOpenFile.n | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/getOpenFile.n b/doc/getOpenFile.n index f95fc49..8162078 100644 --- a/doc/getOpenFile.n +++ b/doc/getOpenFile.n @@ -35,6 +35,12 @@ whether the existing file should be overwritten or not. The following \fIoption\-value\fR pairs are possible as command line arguments to these two commands: .TP +\fB\-confirmoverwrite\fR \fIboolean\fR +Configures how the Save dialog reacts when the selected file already +exists, and saving would overwrite it. A true value requests a +confirmation dialog be presented to the user. A false value requests +that the overwrite take place without confirmation. Default value is true. +.TP \fB\-defaultextension\fR \fIextension\fR Specifies a string that will be appended to the filename if the user enters a filename without an extension. The default value is the empty -- cgit v0.12