diff options
author | dgp <dgp@users.sourceforge.net> | 2011-10-24 19:13:06 (GMT) |
---|---|---|
committer | dgp <dgp@users.sourceforge.net> | 2011-10-24 19:13:06 (GMT) |
commit | bb5cf6ea1f9419bc18120ac76132162637a2941f (patch) | |
tree | 60ca719972f2b0a192c74284ab2f242fc3acc665 | |
parent | 01c75a2daa79c567fcf465599497d37cf16451ae (diff) | |
parent | 4136000e1510b2c72b2e60eaef06bf08c6c6c7e5 (diff) | |
download | tk-bb5cf6ea1f9419bc18120ac76132162637a2941f.zip tk-bb5cf6ea1f9419bc18120ac76132162637a2941f.tar.gz tk-bb5cf6ea1f9419bc18120ac76132162637a2941f.tar.bz2 |
Implementation of TIP #382.
-rw-r--r-- | doc/getOpenFile.n | 6 | ||||
-rw-r--r-- | library/tkfbox.tcl | 8 | ||||
-rw-r--r-- | macosx/tkMacOSXDialog.c | 24 | ||||
-rw-r--r-- | win/tkWinDialog.c | 61 |
4 files changed, 55 insertions, 44 deletions
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 diff --git a/library/tkfbox.tcl b/library/tkfbox.tcl index 4ccf3d7..78112e1 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 "-confirmoverwrite" option is only for the "save" file dialog. + # + if {$type eq "save"} { + lappend specs {-confirmoverwrite "" "" "1"} + } + # 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(-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/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)) { diff --git a/win/tkWinDialog.c b/win/tkWinDialog.c index de7e84f..6b50215 100644 --- a/win/tkWinDialog.c +++ b/win/tkWinDialog.c @@ -592,6 +592,7 @@ GetFileNameW( OFNData ofnData; int cdlgerr; int filterIndex = 0, result = TCL_ERROR, winCode, oldMode, i, multi = 0; + int inValue, confirmOverwrite = 1; char *extension = NULL, *title = NULL; Tk_Window tkwin = (Tk_Window) clientData; HWND hWnd; @@ -603,17 +604,20 @@ GetFileNameW( Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); static CONST char *saveOptionStrings[] = { "-defaultextension", "-filetypes", "-initialdir", "-initialfile", - "-parent", "-title", "-typevariable", NULL + "-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_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'; @@ -621,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; @@ -639,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", @@ -693,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) { @@ -712,6 +687,16 @@ GetFileNameW( initialTypeObj = Tcl_ObjGetVar2(interp, typeVariableObj, NULL, TCL_GLOBAL_ONLY); break; + case FILE_MULTIPLE_OR_CONFIRMOW: + if (Tcl_GetBooleanFromObj(interp, valuePtr, &inValue) != TCL_OK) { + return TCL_ERROR; + } + if (open) { + multi = inValue; + } else { + confirmOverwrite = inValue; + } + break; } } @@ -740,7 +725,7 @@ GetFileNameW( if (open != 0) { ofn.Flags |= OFN_FILEMUSTEXIST; - } else { + } else if (confirmOverwrite) { ofn.Flags |= OFN_OVERWRITEPROMPT; } if (tsdPtr->debugFlag != 0) { |