From 9ba6dd4a59a6e4cffdbf9761ab43d6783cb3dbe2 Mon Sep 17 00:00:00 2001 From: Manolo Gouy Date: Tue, 12 Feb 2013 13:20:11 +0000 Subject: Mac OS Fl_Native_File_Chooser: when saving with multiple filters, changing the file type changes the output file extension in the dialog window. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@9821 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- src/Fl_Native_File_Chooser_MAC.mm | 97 +++++++++++++++++++++++++-------------- 1 file changed, 63 insertions(+), 34 deletions(-) (limited to 'src/Fl_Native_File_Chooser_MAC.mm') diff --git a/src/Fl_Native_File_Chooser_MAC.mm b/src/Fl_Native_File_Chooser_MAC.mm index 11feee852..c6efb7277 100644 --- a/src/Fl_Native_File_Chooser_MAC.mm +++ b/src/Fl_Native_File_Chooser_MAC.mm @@ -344,8 +344,7 @@ int Fl_Native_File_Chooser::filters() const { int Fl_Native_File_Chooser::get_saveas_basename(void) { char *q = strdup( [[[(NSSavePanel*)_panel URL] path] UTF8String] ); - id delegate = [(NSSavePanel*)_panel delegate]; - if (delegate != nil) { + if ( !(_options & SAVEAS_CONFIRM) ) { const char *d = [[[[(NSSavePanel*)_panel URL] path] stringByDeletingLastPathComponent] UTF8String]; int l = strlen(d) + 1; if (strcmp(d, "/") == 0) l = 1; @@ -435,17 +434,53 @@ static char *prepareMacFilter(int count, const char *filter, char **patterns) { #endif { + NSSavePanel *dialog; + BOOL saveas_confirm; } - (NSString *)panel:(id)sender userEnteredFilename:(NSString *)filename confirmed:(BOOL)okFlag; +- (void)changedPopup:(id)sender; +- (void)panel:(NSSavePanel*)p; +- (void)option:(BOOL)o; @end @implementation FLsaveDelegate - (NSString *)panel:(id)sender userEnteredFilename:(NSString *)filename confirmed:(BOOL)okFlag { - if (! okFlag) return filename; + if ( !okFlag || saveas_confirm ) return filename; // User has clicked save, and no overwrite confirmation should occur. // To get the latter, we need to change the name we return (hence the prefix): return [@ UNLIKELYPREFIX stringByAppendingString:filename]; } +- (void)changedPopup:(id)sender +// runs when the save panel popup menu changes output file type +// correspondingly changes the extension of the output file name +{ + if (fl_mac_os_version < 100600) return; // because of setNameFieldStringValue and nameFieldStringValue + char *s = strdup([[(NSPopUpButton*)sender titleOfSelectedItem] UTF8String]); + if (!s) return; + char *p = strchr(s, '('); + if (!p) p = s; + p = strchr(p, '.'); + if (!p) {free(s); return;} + p++; + while (*p == ' ') p++; + if (!p || *p == '{') {free(s); return;} + char *q = p+1; + while (*q != ' ' && *q != ')' && *q != 0) q++; + *q = 0; + NSString *ns = [NSString stringWithFormat:@"%@.%@", + [[dialog performSelector:@selector(nameFieldStringValue)] stringByDeletingPathExtension], + [NSString stringWithUTF8String:p]]; + free(s); + [dialog performSelector:@selector(setNameFieldStringValue:) withObject:ns]; +} +- (void)panel:(NSSavePanel*)p +{ + dialog = p; +} +- (void) option:(BOOL)o +{ + saveas_confirm = o; +} @end static NSPopUpButton *createPopupAccessory(NSSavePanel *panel, const char *filter, const char *title, int rank) @@ -541,7 +576,6 @@ int Fl_Native_File_Chooser::post() { _panel = [NSSavePanel savePanel]; break; } - int retval; NSString *nstitle = [NSString stringWithUTF8String: (_title ? _title : "No Title")]; [(NSSavePanel*)_panel setTitle:nstitle]; switch (_btype) { @@ -561,8 +595,9 @@ int Fl_Native_File_Chooser::post() { // SHOW THE DIALOG NSWindow *key = [NSApp keyWindow]; - if ( [(NSSavePanel*)_panel isKindOfClass:[NSOpenPanel class]] ) { - NSPopUpButton *popup = nil; + BOOL is_open_panel = [(NSSavePanel*)_panel isKindOfClass:[NSOpenPanel class]]; + NSPopUpButton *popup = nil; + if ( is_open_panel ) { if (_filt_total) { char *t = prepareMacFilter(_filt_total, _filter, _filt_patt); popup = createPopupAccessory((NSSavePanel*)_panel, t, Fl_File_Chooser::show_label, 0); @@ -571,48 +606,42 @@ int Fl_Native_File_Chooser::post() { [popup addItemWithTitle:[[NSString alloc] initWithUTF8String:Fl_File_Chooser::all_files_label]]; [popup setAction:@selector(validateVisibleColumns)]; [popup setTarget:(NSObject*)_panel]; - static FLopenDelegate *openDelegate = nil; - if (openDelegate == nil) { - // not to be ever freed - openDelegate = [[FLopenDelegate alloc] init]; - } + FLopenDelegate *openDelegate = [[[FLopenDelegate alloc] init] autorelease]; [openDelegate setPopup:popup filter_pattern:_filt_patt]; [(NSOpenPanel*)_panel setDelegate:openDelegate]; } - retval = runmodal(); - if (_filt_total) { - _filt_value = [popup indexOfSelectedItem]; - } - if ( retval == NSOKButton ) { - clear_pathnames(); - NSArray *array = [(NSOpenPanel*)_panel URLs]; - _tpathnames = [array count]; - _pathnames = new char*[_tpathnames]; - for(int i = 0; i < _tpathnames; i++) { - _pathnames[i] = strnew([[(NSURL*)[array objectAtIndex:i] path] UTF8String]); - } - } } else { - NSPopUpButton *popup = nil; + FLsaveDelegate *saveDelegate = [[[FLsaveDelegate alloc] init] autorelease]; [(NSSavePanel*)_panel setAllowsOtherFileTypes:YES]; - if ( !(_options & SAVEAS_CONFIRM) ) { - static FLsaveDelegate *saveDelegate = nil; - if (saveDelegate == nil)saveDelegate = [[FLsaveDelegate alloc] init]; // not to be ever freed - [(NSSavePanel*)_panel setDelegate:saveDelegate]; - } + [(NSSavePanel*)_panel setDelegate:saveDelegate]; + [saveDelegate option:(_options & SAVEAS_CONFIRM)]; if (_filt_total) { if (_filt_value >= _filt_total) _filt_value = _filt_total - 1; char *t = prepareMacFilter(_filt_total, _filter, _filt_patt); popup = createPopupAccessory((NSSavePanel*)_panel, t, [[(NSSavePanel*)_panel nameFieldLabel] UTF8String], _filt_value); delete[] t; + [popup setAction:@selector(changedPopup:)]; + [popup setTarget:saveDelegate]; + [saveDelegate panel:(NSSavePanel*)_panel]; [(NSSavePanel*)_panel setCanSelectHiddenExtension:YES]; } - retval = runmodal(); - if (_filt_total) { - _filt_value = [popup indexOfSelectedItem]; + } + int retval = runmodal(); + if (_filt_total) { + _filt_value = [popup indexOfSelectedItem]; + } + if ( retval == NSOKButton ) { + if (is_open_panel) { + clear_pathnames(); + NSArray *array = [(NSOpenPanel*)_panel URLs]; + _tpathnames = [array count]; + _pathnames = new char*[_tpathnames]; + for(int i = 0; i < _tpathnames; i++) { + _pathnames[i] = strnew([[(NSURL*)[array objectAtIndex:i] path] UTF8String]); + } } - if ( retval == NSOKButton ) get_saveas_basename(); + else get_saveas_basename(); } [key makeKeyWindow]; [localPool release]; -- cgit v1.2.3