]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/cocoa/filedlg.mm
Disable some wxWebView tests that fail on the buildbot but not locally.
[wxWidgets.git] / src / osx / cocoa / filedlg.mm
index 12fe95f056f285d8f9a18ed1e787b560e6ea47b3..a1c79b7fc51150881b913791457c7765d4123ae2 100644 (file)
@@ -4,7 +4,7 @@
 // Author:      Ryan Norton
 // Modified by:
 // Created:     2004-10-02
 // Author:      Ryan Norton
 // Modified by:
 // Created:     2004-10-02
-// RCS-ID:      $Id: filedlg.mm 40007 2006-07-05 13:10:46Z SC $
+// RCS-ID:      $Id$
 // Copyright:   (c) Ryan Norton
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 // Copyright:   (c) Ryan Norton
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
@@ -27,6 +27,9 @@
 #ifndef WX_PRECOMP
     #include "wx/msgdlg.h"
     #include "wx/app.h"
 #ifndef WX_PRECOMP
     #include "wx/msgdlg.h"
     #include "wx/app.h"
+    #include "wx/sizer.h"
+    #include "wx/stattext.h"
+    #include "wx/choice.h"
 #endif
 
 #include "wx/filename.h"
 #endif
 
 #include "wx/filename.h"
@@ -62,7 +65,7 @@
 
 - (id) init
 {
 
 - (id) init
 {
-    [super init];
+    self = [super init];
     _dialog = NULL;
     return self;
 }
     _dialog = NULL;
     return self;
 }
@@ -160,6 +163,14 @@ wxFileDialog::wxFileDialog(
     long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
     : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
 {
     long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
     : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
 {
+    m_filterIndex = -1;
+    m_sheetDelegate = [[ModalDialogDelegate alloc] init];
+    [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
+}
+
+wxFileDialog::~wxFileDialog()
+{
+    [m_sheetDelegate release];
 }
 
 bool wxFileDialog::SupportsExtraControl() const
 }
 
 bool wxFileDialog::SupportsExtraControl() const
@@ -304,10 +315,8 @@ void wxFileDialog::ShowWindowModal()
         [sPanel setAllowsOtherFileTypes:NO];
         
         NSWindow* nativeParent = parentWindow->GetWXWindow();
         [sPanel setAllowsOtherFileTypes:NO];
         
         NSWindow* nativeParent = parentWindow->GetWXWindow();
-        ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
-        [sheetDelegate setImplementation: this];
         [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
         [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
-            modalForWindow: nativeParent modalDelegate: sheetDelegate
+            modalForWindow: nativeParent modalDelegate: m_sheetDelegate
             didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
             contextInfo: nil];
     }
             didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
             contextInfo: nil];
     }
@@ -325,11 +334,9 @@ void wxFileDialog::ShowWindowModal()
         [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )];
         
         NSWindow* nativeParent = parentWindow->GetWXWindow();
         [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )];
         
         NSWindow* nativeParent = parentWindow->GetWXWindow();
-        ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
-        [sheetDelegate setImplementation: this];
         [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
             types: types modalForWindow: nativeParent
         [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
             types: types modalForWindow: nativeParent
-            modalDelegate: sheetDelegate
+            modalDelegate: m_sheetDelegate
             didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
             contextInfo: nil];
     }
             didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
             contextInfo: nil];
     }
@@ -377,11 +384,8 @@ wxWindow* wxFileDialog::CreateFilterPanel(wxWindow *extracontrol)
     return extrapanel;
 }
 
     return extrapanel;
 }
 
-// An item has been selected in the file filter wxChoice:
-void wxFileDialog::OnFilterSelected( wxCommandEvent &WXUNUSED(event) )
+void wxFileDialog::DoOnFilterSelected(int index)
 {
 {
-    int index = m_filterChoice->GetSelection();
-
     NSArray* types = GetTypesFromExtension(m_filterExtensions[index],m_currentExtensions);
     NSSavePanel* panel = (NSSavePanel*) GetWXWindow();
     if ( m_delegate )
     NSArray* types = GetTypesFromExtension(m_filterExtensions[index],m_currentExtensions);
     NSSavePanel* panel = (NSSavePanel*) GetWXWindow();
     if ( m_delegate )
@@ -390,6 +394,12 @@ void wxFileDialog::OnFilterSelected( wxCommandEvent &WXUNUSED(event) )
         [panel setAllowedFileTypes:types];
 }
 
         [panel setAllowedFileTypes:types];
 }
 
+// An item has been selected in the file filter wxChoice:
+void wxFileDialog::OnFilterSelected( wxCommandEvent &WXUNUSED(event) )
+{
+    DoOnFilterSelected( m_filterChoice->GetSelection() );
+}
+
 bool wxFileDialog::CheckFile( const wxString& filename )
 {
     if ( m_currentExtensions.GetCount() == 0 )
 bool wxFileDialog::CheckFile( const wxString& filename )
 {
     if ( m_currentExtensions.GetCount() == 0 )
@@ -426,7 +436,7 @@ void wxFileDialog::SetupExtraControls(WXWindow nativeWindow)
         accView = m_filterPanel->GetHandle();
         if( HasFlag(wxFD_OPEN) )
         {
         accView = m_filterPanel->GetHandle();
         if( HasFlag(wxFD_OPEN) )
         {
-            if ( 1 /* UMAGetSystemVersion() < 0x1060 */ )
+            if ( UMAGetSystemVersion() < 0x1060 )
             {
                 wxOpenPanelDelegate* del = [[wxOpenPanelDelegate alloc]init];
                 [del setFileDialog:this];
             {
                 wxOpenPanelDelegate* del = [[wxOpenPanelDelegate alloc]init];
                 [del setFileDialog:this];
@@ -456,6 +466,8 @@ void wxFileDialog::SetupExtraControls(WXWindow nativeWindow)
 
 int wxFileDialog::ShowModal()
 {
 
 int wxFileDialog::ShowModal()
 {
+    wxMacAutoreleasePool autoreleasepool;
+    
     wxCFStringRef cf( m_message );
 
     wxCFStringRef dir( m_dir );
     wxCFStringRef cf( m_message );
 
     wxCFStringRef dir( m_dir );
@@ -464,8 +476,6 @@ int wxFileDialog::ShowModal()
     m_path = wxEmptyString;
     m_fileNames.Clear();
     m_paths.Clear();
     m_path = wxEmptyString;
     m_fileNames.Clear();
     m_paths.Clear();
-    // since we don't support retrieving the matching filter
-    m_filterIndex = -1;
 
     wxNonOwnedWindow* parentWindow = NULL;
     int returnCode = -1;
 
     wxNonOwnedWindow* parentWindow = NULL;
     int returnCode = -1;
@@ -487,8 +497,13 @@ int wxFileDialog::ShowModal()
     }
 
     m_firstFileTypeFilter = -1;
     }
 
     m_firstFileTypeFilter = -1;
-    
-    if ( m_useFileTypeFilter )
+
+    if ( m_useFileTypeFilter
+        && m_filterIndex >= 0 && m_filterIndex < m_filterExtensions.GetCount() )
+    {
+        m_firstFileTypeFilter = m_filterIndex;
+    }
+    else if ( m_useFileTypeFilter )
     {
         types = nil;
         bool useDefault = true;
     {
         types = nil;
         bool useDefault = true;
@@ -541,7 +556,20 @@ int wxFileDialog::ShowModal()
         {
         }
 
         {
         }
 
-        returnCode = [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ];
+        /*
+        Let the file dialog know what file type should be used initially.
+        If this is not done then when setting the filter index
+        programmatically to 1 the file will still have the extension
+        of the first file type instead of the second one. E.g. when file
+        types are foo and bar, a filename "myletter" with SetDialogIndex(1)
+        would result in saving as myletter.foo, while we want myletter.bar.
+        */
+        if(m_firstFileTypeFilter > 0)
+        {
+            DoOnFilterSelected(m_firstFileTypeFilter);
+        }
+
+        returnCode = [sPanel runModalForDirectory: m_dir.IsEmpty() ? nil : dir.AsNSString() file:file.AsNSString() ];
         ModalFinishedCallback(sPanel, returnCode);
     }
     else
         ModalFinishedCallback(sPanel, returnCode);
     }
     else
@@ -559,13 +587,14 @@ int wxFileDialog::ShowModal()
 
         if ( UMAGetSystemVersion() < 0x1060 )
         {
 
         if ( UMAGetSystemVersion() < 0x1060 )
         {
-            returnCode = [oPanel runModalForDirectory:dir.AsNSString()
+            returnCode = [oPanel runModalForDirectory:m_dir.IsEmpty() ? nil : dir.AsNSString()
                                                  file:file.AsNSString() types:(m_delegate == nil ? types : nil)];
         }
         else 
         {
             [oPanel setAllowedFileTypes: (m_delegate == nil ? types : nil)];
                                                  file:file.AsNSString() types:(m_delegate == nil ? types : nil)];
         }
         else 
         {
             [oPanel setAllowedFileTypes: (m_delegate == nil ? types : nil)];
-            [oPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString() 
+            if ( !m_dir.IsEmpty() )
+                [oPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString() 
                                                isDirectory:YES]];
             returnCode = [oPanel runModal];
         }
                                                isDirectory:YES]];
             returnCode = [oPanel runModal];
         }
@@ -589,6 +618,10 @@ void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode)
             m_path = wxCFStringRef::AsString([sPanel filename]);
             m_fileName = wxFileNameFromPath(m_path);
             m_dir = wxPathOnly( m_path );
             m_path = wxCFStringRef::AsString([sPanel filename]);
             m_fileName = wxFileNameFromPath(m_path);
             m_dir = wxPathOnly( m_path );
+            if (m_filterChoice)
+            {
+                m_filterIndex = m_filterChoice->GetSelection();
+            }
         }
     }
     else
         }
     }
     else