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
This commit is contained in:
parent
7f9e6ae829
commit
9ba6dd4a59
@ -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) {
|
||||
<NSOpenSavePanelDelegate>
|
||||
#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,19 +606,33 @@ 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();
|
||||
}
|
||||
else {
|
||||
FLsaveDelegate *saveDelegate = [[[FLsaveDelegate alloc] init] autorelease];
|
||||
[(NSSavePanel*)_panel setAllowsOtherFileTypes:YES];
|
||||
[(NSSavePanel*)_panel setDelegate:saveDelegate];
|
||||
[saveDelegate option:(_options & SAVEAS_CONFIRM)];
|
||||
if (_filt_total) {
|
||||
_filt_value = [popup indexOfSelectedItem];
|
||||
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];
|
||||
}
|
||||
if ( retval == NSOKButton ) {
|
||||
}
|
||||
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];
|
||||
@ -592,27 +641,7 @@ int Fl_Native_File_Chooser::post() {
|
||||
_pathnames[i] = strnew([[(NSURL*)[array objectAtIndex:i] path] UTF8String]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
NSPopUpButton *popup = nil;
|
||||
[(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];
|
||||
}
|
||||
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;
|
||||
[(NSSavePanel*)_panel setCanSelectHiddenExtension:YES];
|
||||
}
|
||||
retval = runmodal();
|
||||
if (_filt_total) {
|
||||
_filt_value = [popup indexOfSelectedItem];
|
||||
}
|
||||
if ( retval == NSOKButton ) get_saveas_basename();
|
||||
else get_saveas_basename();
|
||||
}
|
||||
[key makeKeyWindow];
|
||||
[localPool release];
|
||||
|
||||
Loading…
Reference in New Issue
Block a user