From 3a718d1ca9c3c454928087f0167397af4d121c2e Mon Sep 17 00:00:00 2001 From: Kevin Walzer Date: Mon, 5 Jun 2017 01:54:53 +0000 Subject: Fix for 9eab541213, tk_getSave on macOS; thanks to Christian Gollwitzer for patch --- macosx/tkMacOSXDialog.c | 94 +++++++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 49 deletions(-) diff --git a/macosx/tkMacOSXDialog.c b/macosx/tkMacOSXDialog.c index 2670b32..3397f71 100644 --- a/macosx/tkMacOSXDialog.c +++ b/macosx/tkMacOSXDialog.c @@ -6,7 +6,7 @@ * Copyright (c) 1996-1997 Sun Microsystems, Inc. * Copyright 2001-2009, Apple Inc. * Copyright (c) 2006-2009 Daniel A. Steffen - * Copyright (c) 2017 Christian Gollwitzer. + * Copyright (c) 2017 Christian Gollwitzer. * * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. @@ -34,7 +34,7 @@ typedef struct { NSMutableArray *fileTypeExtensions; // array of allowed extensions per name, e.g. "txt", "doc" NSMutableArray *fileTypeLabels; // displayed string, e.g. "Text document (.txt, .doc)" NSMutableArray *allAllowedExtensions; // set of all allowed extensions - NSInteger fileTypeIndex; // index of currently selected filter + NSInteger fileTypeIndex; // index of currently selected filter } filepanelFilterInfo; filepanelFilterInfo filterInfo; @@ -159,22 +159,6 @@ static const short alertNativeButtonIndexAndTypeToButtonIndex[][3] = { [TYPE_YESNOCANCEL] = {5, 6, 4}, }; -/* - * Construct a file URL from directory and filename. Either may - * be nil. If both are nil, returns nil. - */ -#if MAC_OS_X_VERSION_MIN_REQUIRED > 1050 -static NSURL *getFileURL(NSString *directory, NSString *filename) { - NSURL *url = nil; - if (directory) { - url = [NSURL fileURLWithPath:directory]; - } - if (filename) { - url = [NSURL URLWithString:filename relativeToURL:url]; - } - return url; -} -#endif #pragma mark TKApplication(TKDialog) @@ -413,7 +397,7 @@ int parseFileFilters(Tcl_Interp *interp, Tcl_Obj *fileTypesPtr, Tcl_Obj *typeVar } filterInfo.doFileTypes = (fl.filters != NULL); - + filterInfo.fileTypeIndex = 0; filterInfo.fileTypeExtensions = [NSMutableArray array]; filterInfo.fileTypeNames = [NSMutableArray array]; @@ -442,7 +426,7 @@ int parseFileFilters(Tcl_Interp *interp, Tcl_Obj *fileTypesPtr, Tcl_Obj *typeVar if (![filterInfo.allAllowedExtensions containsObject:extension]) { [filterInfo.allAllowedExtensions addObject:extension]; } - + [clauseextensions addObject:extension]; [displayextensions addObject:[@"." stringByAppendingString:extension]]; @@ -458,7 +442,7 @@ int parseFileFilters(Tcl_Interp *interp, Tcl_Obj *fileTypesPtr, Tcl_Obj *typeVar [label appendString:@")"]; [filterInfo.fileTypeLabels addObject:label]; [label release]; - + } /* Check if the typevariable exists and matches one of the names */ @@ -467,21 +451,21 @@ int parseFileFilters(Tcl_Interp *interp, Tcl_Obj *fileTypesPtr, Tcl_Obj *typeVar if (typeVariablePtr) { /* extract the variable content as a NSString */ Tcl_Obj *selectedFileTypeObj = Tcl_ObjGetVar2(interp, typeVariablePtr, NULL, TCL_GLOBAL_ONLY); - + /* check that the typevariable exists */ - if (selectedFileTypeObj != NULL) { + if (selectedFileTypeObj != NULL) { const char *selectedFileType = Tcl_GetString(selectedFileTypeObj); NSString *selectedFileTypeStr = [[NSString alloc] initWithUTF8String:selectedFileType]; NSUInteger index = [filterInfo.fileTypeNames indexOfObject:selectedFileTypeStr]; - + if (index != NSNotFound) { filterInfo.fileTypeIndex = index; filterInfo.preselectFilter = true; } } - } + } - } + } TkFreeFileFilters(&fl); return TCL_OK; @@ -587,9 +571,9 @@ Tk_GetOpenFileObjCmd( break; } } - - /* From OSX 10.11, the title string is silently ignored. - * Prepend the title to the message + + /* From OSX 10.11, the title string is silently ignored. + * Prepend the title to the message * NOTE should be conditional on OSX version, but * -mmacosx-version-min does not revert this behaviour*/ if (title) { @@ -610,7 +594,7 @@ Tk_GetOpenFileObjCmd( } [openpanel setAllowsMultipleSelection:multiple]; - + if (parseFileFilters(interp, fileTypesPtr, typeVariablePtr) != TCL_OK) { goto end; } @@ -630,19 +614,19 @@ Tk_GetOpenFileObjCmd( [accessoryView addSubview:label]; [accessoryView addSubview:popupButton]; - + if (filterInfo.preselectFilter) { - /* A specific filter was selected from the typevariable. Select it and + /* A specific filter was selected from the typevariable. Select it and * open the accessory view */ [popupButton selectItemAtIndex:filterInfo.fileTypeIndex]; - /* on OSX > 10.11, the optons are not visible by default. Ergo allow all file types + /* on OSX > 10.11, the optons are not visible by default. Ergo allow all file types [openpanel setAllowedFileTypes:filterInfo.fileTypeExtensions[filterInfo.fileTypeIndex]]; */ [openpanel setAllowedFileTypes:filterInfo.allAllowedExtensions]; } else { [openpanel setAllowedFileTypes:filterInfo.allAllowedExtensions]; } - + [openpanel setAllowsOtherFileTypes:NO]; [openpanel setAccessoryView:accessoryView]; @@ -658,7 +642,7 @@ Tk_GetOpenFileObjCmd( } Tcl_IncrRefCount(cmdObj); } - + callbackInfo->cmdObj = cmdObj; callbackInfo->interp = interp; callbackInfo->multiple = multiple; @@ -675,7 +659,10 @@ Tk_GetOpenFileObjCmd( @selector(tkFilePanelDidEnd:returnCode:contextInfo:) contextInfo:callbackInfo]; #else - [openpanel setDirectoryURL:getFileURL(directory, filename)]; + if (directory) { + [openpanel setDirectoryURL:[NSURL fileURLWithPath:directory isDirectory:YES]]; + } + [openpanel setNameFieldStringValue:filename]; [openpanel beginSheetModalForWindow:parent completionHandler:^(NSInteger returnCode) { [NSApp tkFilePanelDidEnd:openpanel @@ -688,7 +675,10 @@ Tk_GetOpenFileObjCmd( modalReturnCode = [openpanel runModalForDirectory:directory file:filename]; #else - [openpanel setDirectoryURL:getFileURL(directory, filename)]; + if (directory) { + [openpanel setDirectoryURL:[NSURL fileURLWithPath:directory isDirectory:YES]]; + } + [openpanel setNameFieldStringValue:filename]; modalReturnCode = [openpanel runModal]; #endif [NSApp tkFilePanelDidEnd:openpanel returnCode:modalReturnCode @@ -698,8 +688,8 @@ Tk_GetOpenFileObjCmd( if (parentIsKey) { [parent makeKeyWindow]; } - - if ((typeVariablePtr && (modalReturnCode == NSOKButton)) && + + if ((typeVariablePtr && (modalReturnCode == NSOKButton)) && filterInfo.doFileTypes && filterInfo.userHasSelectedFilter) { /* * The -typevariable must be set to the selected file type, if the dialog was not cancelled @@ -712,7 +702,7 @@ Tk_GetOpenFileObjCmd( Tcl_NewStringObj([selectedFilter UTF8String], -1), TCL_GLOBAL_ONLY); } - + end: return result; } @@ -826,7 +816,7 @@ Tk_GetSaveFileObjCmd( break; } } - + if (title) { [savepanel setTitle:title]; if (message) { @@ -866,7 +856,7 @@ Tk_GetSaveFileObjCmd( [accessoryView addSubview:popupButton]; [savepanel setAccessoryView:accessoryView]; - + [savepanel setAllowedFileTypes:filterInfo.fileTypeExtensions[filterInfo.fileTypeIndex]]; [savepanel setAllowsOtherFileTypes:NO]; } else if (defaultType) { @@ -905,7 +895,10 @@ Tk_GetSaveFileObjCmd( @selector(tkFilePanelDidEnd:returnCode:contextInfo:) contextInfo:callbackInfo]; #else - [savepanel setDirectoryURL:getFileURL(directory, filename)]; + if (directory) { + [savepanel setDirectoryURL:[NSURL fileURLWithPath:directory isDirectory:YES]]; + } + [savepanel setNameFieldStringValue:filename]; [savepanel beginSheetModalForWindow:parent completionHandler:^(NSInteger returnCode) { [NSApp tkFilePanelDidEnd:savepanel @@ -917,7 +910,10 @@ Tk_GetSaveFileObjCmd( #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060 modalReturnCode = [savepanel runModalForDirectory:directory file:filename]; #else - [savepanel setDirectoryURL:getFileURL(directory, filename)]; + if (directory) { + [savepanel setDirectoryURL:[NSURL fileURLWithPath:directory isDirectory:YES]]; + } + [savepanel setNameFieldStringValue:filename]; modalReturnCode = [savepanel runModal]; #if 0 NSLog(@"modal: %li", modalReturnCode); @@ -930,7 +926,7 @@ Tk_GetSaveFileObjCmd( if (parentIsKey) { [parent makeKeyWindow]; } - + if ((typeVariablePtr && (modalReturnCode == NSOKButton)) && filterInfo.doFileTypes) { /* * The -typevariable must be set to the selected file type, if the dialog was not cancelled @@ -980,7 +976,7 @@ Tk_ChooseDirectoryObjCmd( Tcl_Obj *cmdObj = NULL; FilePanelCallbackInfo callbackInfoStruct; FilePanelCallbackInfo *callbackInfo = &callbackInfoStruct; - NSString *directory = nil, *filename = nil; + NSString *directory = nil; NSString *message, *title; NSWindow *parent; NSOpenPanel *panel = [NSOpenPanel openPanel]; @@ -1056,13 +1052,13 @@ Tk_ChooseDirectoryObjCmd( parentIsKey = [parent isKeyWindow]; #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060 [panel beginSheetForDirectory:directory - file:filename + file:nil modalForWindow:parent modalDelegate:NSApp didEndSelector: @selector(tkFilePanelDidEnd:returnCode:contextInfo:) contextInfo:callbackInfo]; #else - [panel setDirectoryURL:getFileURL(directory, filename)]; + [panel setDirectoryURL:[NSURL fileURLWithPath:directory isDirectory:YES]]; [panel beginSheetModalForWindow:parent completionHandler:^(NSInteger returnCode) { [NSApp tkFilePanelDidEnd:panel @@ -1074,7 +1070,7 @@ Tk_ChooseDirectoryObjCmd( #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060 modalReturnCode = [panel runModalForDirectory:directory file:nil]; #else - [panel setDirectoryURL:getFileURL(directory, filename)]; + [panel setDirectoryURL:[NSURL fileURLWithPath:directory isDirectory:YES]]; modalReturnCode = [panel runModal]; #endif [NSApp tkFilePanelDidEnd:panel returnCode:modalReturnCode -- cgit v0.12