support wxFD_MULTIPLE
[wxWidgets.git] / src / osx / cocoa / filedlg.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/filedlg.mm
3 // Purpose:     wxFileDialog for wxCocoa
4 // Author:      Ryan Norton
5 // Modified by:
6 // Created:     2004-10-02
7 // RCS-ID:      $Id: filedlg.mm 40007 2006-07-05 13:10:46Z SC $
8 // Copyright:   (c) Ryan Norton
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #if wxUSE_FILEDLG
24
25 #include "wx/filedlg.h"
26
27 #ifndef WX_PRECOMP
28     #include "wx/msgdlg.h"
29     #include "wx/app.h"
30 #endif
31
32 #include "wx/filename.h"
33 #include "wx/tokenzr.h"
34
35 #include "wx/osx/private.h"
36
37 // ============================================================================
38 // implementation
39 // ============================================================================
40
41 // Open Items:
42 // - support for old style MacOS creator / type combos
43 // - parameter support for descending into packages as directories (setTreatsFilePackagesAsDirectories)
44
45 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
46
47 wxFileDialog::wxFileDialog(
48     wxWindow *parent, const wxString& message,
49     const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
50     long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
51     : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
52 {
53 }
54
55 bool wxFileDialog::SupportsExtraControl() const
56 {
57     return true;
58 }
59
60 NSArray* CopyTypesFromFilter( const wxString filter )
61 {
62     NSMutableArray* types = nil;
63     if ( !filter.empty() )
64     {
65         wxArrayString names ;
66         wxArrayString extensions;
67
68         wxString filter2(filter) ;
69         int filterIndex = 0;
70         bool isName = true ;
71         wxString current ;
72
73         for ( unsigned int i = 0; i < filter2.length() ; i++ )
74         {
75             if ( filter2.GetChar(i) == wxT('|') )
76             {
77                 if ( isName )
78                 {
79                     names.Add( current ) ;
80                 }
81                 else
82                 {
83                     extensions.Add( current ) ;
84                     ++filterIndex ;
85                 }
86
87                 isName = !isName ;
88                 current = wxEmptyString ;
89             }
90             else
91             {
92                 current += filter2.GetChar(i) ;
93             }
94         }
95         // we allow for compatibility reason to have a single filter expression (like *.*) without
96         // an explanatory text, in that case the first part is name and extension at the same time
97
98         wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
99         if ( current.empty() )
100             extensions.Add( names[filterIndex] ) ;
101         else
102             extensions.Add( current ) ;
103         if ( filterIndex == 0 || isName )
104             names.Add( current ) ;
105
106         ++filterIndex ;
107
108         const size_t extCount = extensions.GetCount();
109         for ( size_t i = 0 ; i < extCount; i++ )
110         {
111             wxString extensiongroup = extensions[i];
112             wxStringTokenizer tokenizer( extensiongroup , wxT(";") ) ;
113             while ( tokenizer.HasMoreTokens() )
114             {
115                 wxString extension = tokenizer.GetNextToken() ;
116                 // Remove leading '*'
117                 if (extension.length() && (extension.GetChar(0) == '*'))
118                     extension = extension.Mid( 1 );
119
120                 // Remove leading '.'
121                 if (extension.length() && (extension.GetChar(0) == '.'))
122                     extension = extension.Mid( 1 );
123
124                 // Remove leading '*', this is for handling *.*
125                 if (extension.length() && (extension.GetChar(0) == '*'))
126                     extension = extension.Mid( 1 );
127
128                 if ( extension.IsEmpty() )
129                 {
130                     [types release];
131                     types = nil;
132                     return nil;
133                 }
134
135                 if ( types == nil )
136                     types = [[NSMutableArray alloc] init];
137
138                 wxCFStringRef cfext(extension);
139                 [types addObject: (NSString*)cfext.AsNSString()  ];
140 #if 0
141                 // add support for classic fileType / creator here
142                 wxUint32 fileType, creator;
143                 // extension -> mactypes
144 #endif
145             }
146
147         }
148     }
149     return types;
150 }
151
152 void wxFileDialog::ShowWindowModal()
153 {
154     wxCFStringRef cf( m_message );
155     wxCFStringRef dir( m_dir );
156     wxCFStringRef file( m_fileName );
157
158     wxNonOwnedWindow* parentWindow = NULL;
159     
160     m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
161
162     if (GetParent())
163         parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
164
165     wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
166     
167     if (HasFlag(wxFD_SAVE))
168     {
169         NSSavePanel* sPanel = [NSSavePanel savePanel];
170
171         SetupExtraControls(sPanel);
172
173         // makes things more convenient:
174         [sPanel setCanCreateDirectories:YES];
175         [sPanel setMessage:cf.AsNSString()];
176         // if we should be able to descend into pacakges we must somehow
177         // be able to pass this in
178         [sPanel setTreatsFilePackagesAsDirectories:NO];
179         [sPanel setCanSelectHiddenExtension:YES];
180         
181         NSWindow* nativeParent = parentWindow->GetWXWindow();
182         ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
183         [sheetDelegate setImplementation: this];
184         [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
185             modalForWindow: nativeParent modalDelegate: sheetDelegate
186             didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
187             contextInfo: nil];
188     }
189     else 
190     {
191         NSArray* types = CopyTypesFromFilter( m_wildCard ) ;
192         NSOpenPanel* oPanel = [NSOpenPanel openPanel];
193         
194         SetupExtraControls(oPanel);
195
196         [oPanel setTreatsFilePackagesAsDirectories:NO];
197         [oPanel setCanChooseDirectories:NO];
198         [oPanel setResolvesAliases:YES];
199         [oPanel setCanChooseFiles:YES];
200         [oPanel setMessage:cf.AsNSString()];
201         if ( HasFlag(wxFD_MULTIPLE) )
202             [oPanel setAllowsMultipleSelection:YES];
203     
204         NSWindow* nativeParent = parentWindow->GetWXWindow();
205         ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
206         [sheetDelegate setImplementation: this];
207         [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
208             types: types modalForWindow: nativeParent
209             modalDelegate: sheetDelegate
210             didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
211             contextInfo: nil];
212         [types release];
213         types = nil;
214     }
215 }
216
217 void wxFileDialog::SetupExtraControls(WXWindow nativeWindow)
218 {
219     NSSavePanel* panel = (NSSavePanel*) nativeWindow;
220     
221     wxNonOwnedWindow::Create( GetParent(), nativeWindow );
222     
223     if (HasExtraControlCreator())
224     {
225         CreateExtraControl();
226         wxWindow* control = GetExtraControl();
227         if ( control )
228         {
229             NSView* accView = control->GetHandle();
230             [accView removeFromSuperview];
231             [panel setAccessoryView:accView];
232         }
233         else
234         {
235             [panel setAccessoryView:nil];
236         }
237     }
238 }
239
240 int wxFileDialog::ShowModal()
241 {
242     wxCFStringRef cf( m_message );
243
244     wxCFStringRef dir( m_dir );
245     wxCFStringRef file( m_fileName );
246
247     m_path = wxEmptyString;
248     m_fileNames.Clear();
249     m_paths.Clear();
250     // since we don't support retrieving the matching filter
251     m_filterIndex = -1;
252
253     wxNonOwnedWindow* parentWindow = NULL;
254     int returnCode = -1;
255
256     if (GetParent())
257     {
258         parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
259     }
260
261     if (HasFlag(wxFD_SAVE))
262     {
263         NSSavePanel* sPanel = [NSSavePanel savePanel];
264
265         SetupExtraControls(sPanel);
266
267         // makes things more convenient:
268         [sPanel setCanCreateDirectories:YES];
269         [sPanel setMessage:cf.AsNSString()];
270         // if we should be able to descend into pacakges we must somehow
271         // be able to pass this in
272         [sPanel setTreatsFilePackagesAsDirectories:NO];
273         [sPanel setCanSelectHiddenExtension:YES];
274
275         if ( HasFlag(wxFD_OVERWRITE_PROMPT) )
276         {
277         }
278
279         returnCode = [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ];
280         ModalFinishedCallback(sPanel, returnCode);
281
282         UnsubclassWin();
283         [sPanel setAccessoryView:nil];
284     }
285     else
286     {
287         NSArray* types = CopyTypesFromFilter( m_wildCard ) ;
288         NSOpenPanel* oPanel = [NSOpenPanel openPanel];
289         
290         SetupExtraControls(oPanel);
291                 
292         [oPanel setTreatsFilePackagesAsDirectories:NO];
293         [oPanel setCanChooseDirectories:NO];
294         [oPanel setResolvesAliases:YES];
295         [oPanel setCanChooseFiles:YES];
296         [oPanel setMessage:cf.AsNSString()];
297         if ( HasFlag(wxFD_MULTIPLE) )
298             [oPanel setAllowsMultipleSelection:YES];
299
300         returnCode = [oPanel runModalForDirectory:dir.AsNSString()
301                         file:file.AsNSString() types:types];
302
303         ModalFinishedCallback(oPanel, returnCode);
304         
305         UnsubclassWin();
306         [oPanel setAccessoryView:nil];
307         
308         [types release];
309         types = nil;
310     }
311
312     return GetReturnCode();
313 }
314
315 void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode)
316 {
317     int result = wxID_CANCEL;
318     if (HasFlag(wxFD_SAVE))
319     {
320         if (returnCode == NSOKButton )
321         {
322             NSSavePanel* sPanel = (NSSavePanel*)panel;
323             result = wxID_OK;
324
325             m_path = wxCFStringRef::AsString([sPanel filename]);
326             m_fileName = wxFileNameFromPath(m_path);
327             m_dir = wxPathOnly( m_path );
328         }
329     }
330     else
331     {
332         NSOpenPanel* oPanel = (NSOpenPanel*)panel;
333         if (returnCode == NSOKButton )
334         {
335             panel = oPanel;
336             result = wxID_OK;
337             NSArray* filenames = [oPanel filenames];
338             for ( size_t i = 0 ; i < [filenames count] ; ++ i )
339             {
340                 wxString fnstr = wxCFStringRef::AsString([filenames objectAtIndex:i]);
341                 m_paths.Add( fnstr );
342                 m_fileNames.Add( wxFileNameFromPath(fnstr) );
343                 if ( i == 0 )
344                 {
345                     m_path = fnstr;
346                     m_fileName = wxFileNameFromPath(fnstr);
347                     m_dir = wxPathOnly( fnstr );
348                 }
349             }
350         }
351     }
352     SetReturnCode(result);
353     
354     if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
355         SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED  );
356     
357     [(NSSavePanel*) panel setAccessoryView:nil];
358 }
359
360 #endif // wxUSE_FILEDLG