Author: manolo
Date: 2013-02-12 05:58:07 -0800 (Tue, 12 Feb 2013)
New Revision: 9822
Log:
Mac OS fltk3::NativeFileChooser: when saving with multiple filters, changing 
the file type changes
the output file extension in the dialog window.

Modified:
   branches/branch-3.0/src/fltk3/cocoaNativeFileChooser.mm

Modified: branches/branch-3.0/src/fltk3/cocoaNativeFileChooser.mm
===================================================================
--- branches/branch-3.0/src/fltk3/cocoaNativeFileChooser.mm     2013-02-12 
13:20:11 UTC (rev 9821)
+++ branches/branch-3.0/src/fltk3/cocoaNativeFileChooser.mm     2013-02-12 
13:58:07 UTC (rev 9822)
@@ -353,8 +353,7 @@
 
 int fltk3::NativeFileChooser::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;
@@ -444,17 +443,53 @@
 <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)
@@ -550,7 +585,6 @@
       _panel =  [NSSavePanel savePanel];
       break;
   }
-  int retval;
   NSString *nstitle = [NSString stringWithUTF8String: (_title ? _title : "No 
Title")];
   [(NSSavePanel*)_panel setTitle:nstitle];
   switch (_btype) {
@@ -570,7 +604,9 @@
   
   // SHOW THE DIALOG
   NSWindow *key = [NSApp keyWindow];
-  if ( [(NSSavePanel*)_panel isKindOfClass:[NSOpenPanel class]] ) {
+  BOOL is_open_panel = [(NSSavePanel*)_panel isKindOfClass:[NSOpenPanel 
class]];
+  NSPopUpButton *popup = nil;
+  if ( is_open_panel ) {
     NSPopUpButton *popup = nil;
     if (_filt_total) {
       char *t = prepareMacFilter(_filt_total, _filter, _filt_patt);
@@ -580,48 +616,42 @@
       [popup addItemWithTitle:[[NSString alloc] 
initWithUTF8String:fltk3::FileChooser::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];

_______________________________________________
fltk-commit mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-commit

Reply via email to