1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/filedlg.mm
3 // Purpose: wxFileDialog for wxCocoa
7 // RCS-ID: $Id: filedlg.mm 40007 2006-07-05 13:10:46Z SC $
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
25 #include "wx/filedlg.h"
28 #include "wx/msgdlg.h"
32 #include "wx/filename.h"
34 #include "wx/osx/private.h"
36 // ============================================================================
38 // ============================================================================
41 // - support for old style MacOS creator / type combos
42 // - parameter support for descending into packages as directories (setTreatsFilePackagesAsDirectories)
44 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
46 wxFileDialog::wxFileDialog(
47 wxWindow *parent, const wxString& message,
48 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
49 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
50 : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
55 NSArray* GetTypesFromFilter( const wxString filter )
57 NSMutableArray* types = nil;
58 if ( !filter.empty() )
61 wxArrayString extensions;
63 wxString filter2(filter) ;
68 for ( unsigned int i = 0; i < filter2.length() ; i++ )
70 if ( filter2.GetChar(i) == wxT('|') )
74 names.Add( current ) ;
78 extensions.Add( current ) ;
83 current = wxEmptyString ;
87 current += filter2.GetChar(i) ;
90 // we allow for compatibility reason to have a single filter expression (like *.*) without
91 // an explanatory text, in that case the first part is name and extension at the same time
93 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
94 if ( current.empty() )
95 extensions.Add( names[filterIndex] ) ;
97 extensions.Add( current ) ;
98 if ( filterIndex == 0 || isName )
99 names.Add( current ) ;
103 const size_t extCount = extensions.GetCount();
104 for ( size_t i = 0 ; i < extCount; i++ )
106 wxString extension = extensions[i];
108 // Remove leading '*'
109 if (extension.length() && (extension.GetChar(0) == '*'))
110 extension = extension.Mid( 1 );
112 // Remove leading '.'
113 if (extension.length() && (extension.GetChar(0) == '.'))
114 extension = extension.Mid( 1 );
116 if ( extension.IsEmpty() )
125 types = [[NSMutableArray alloc] init];
127 wxCFStringRef cfext(extension);
128 [types addObject: (NSString*)cfext.AsNSString() ];
130 // add support for classic fileType / creator here
131 wxUint32 fileType, creator;
132 // extension -> mactypes
139 int wxFileDialog::ShowModal()
141 int result = wxID_CANCEL;
143 NSSavePanel *panel = nil;
145 wxCFStringRef cf( m_message );
147 wxCFStringRef dir( m_dir );
148 wxCFStringRef file( m_fileName );
150 m_path = wxEmptyString;
153 if (HasFlag(wxFD_SAVE))
155 NSSavePanel* sPanel = [NSSavePanel savePanel];
156 // makes things more convenient:
157 [sPanel setCanCreateDirectories:YES];
158 [sPanel setMessage:cf.AsNSString()];
159 // if we should be able to descend into pacakges we must somehow
160 // be able to pass this in
161 [sPanel setTreatsFilePackagesAsDirectories:NO];
162 [sPanel setCanSelectHiddenExtension:YES];
164 if ( HasFlag(wxFD_OVERWRITE_PROMPT) )
168 if ( [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ] == NSOKButton )
173 wxCFStringRef filename( [[sPanel filename] retain] );
175 m_path = filename.AsString();
176 m_fileName = wxFileNameFromPath(m_path);
177 m_dir = wxPathOnly( m_path );
182 NSArray* types = GetTypesFromFilter( m_wildCard ) ;
183 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
184 [oPanel setTreatsFilePackagesAsDirectories:NO];
185 [oPanel setCanChooseDirectories:NO];
186 [oPanel setResolvesAliases:YES];
187 [oPanel setCanChooseFiles:YES];
188 [oPanel setMessage:cf.AsNSString()];
190 if ( [oPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() types:types] == NSOKButton )
194 NSArray* filenames = [oPanel filenames];
195 for ( size_t i = 0 ; i < [filenames count] ; ++ i )
197 wxCFStringRef filename( [(NSString*) [filenames objectAtIndex:i] retain] );
198 wxString fnstr = filename.AsString();
199 m_paths.Add( fnstr );
200 m_fileNames.Add( wxFileNameFromPath(fnstr) );
204 m_fileName = wxFileNameFromPath(fnstr);
205 m_dir = wxPathOnly( fnstr );
216 #endif // wxUSE_FILEDLG