]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/filedlg.mm
Correct IsMaximized() in wxOSX for non-resizable windows.
[wxWidgets.git] / src / osx / cocoa / filedlg.mm
CommitLineData
0f9b48d1
SC
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"
b822bdc0 33#include "wx/tokenzr.h"
0f9b48d1
SC
34
35#include "wx/osx/private.h"
36
37// ============================================================================
38// implementation
39// ============================================================================
40
4dd9fdf8
SC
41// Open Items:
42// - support for old style MacOS creator / type combos
43// - parameter support for descending into packages as directories (setTreatsFilePackagesAsDirectories)
44
0f9b48d1
SC
45IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
46
47wxFileDialog::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
0f9b48d1
SC
55NSArray* GetTypesFromFilter( const wxString filter )
56{
57 NSMutableArray* types = nil;
58 if ( !filter.empty() )
59 {
60 wxArrayString names ;
61 wxArrayString extensions;
bd365871 62
0f9b48d1
SC
63 wxString filter2(filter) ;
64 int filterIndex = 0;
65 bool isName = true ;
66 wxString current ;
67
68 for ( unsigned int i = 0; i < filter2.length() ; i++ )
69 {
70 if ( filter2.GetChar(i) == wxT('|') )
71 {
72 if ( isName )
73 {
74 names.Add( current ) ;
75 }
76 else
77 {
78 extensions.Add( current ) ;
79 ++filterIndex ;
80 }
81
82 isName = !isName ;
83 current = wxEmptyString ;
84 }
85 else
86 {
87 current += filter2.GetChar(i) ;
88 }
89 }
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
92
93 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
94 if ( current.empty() )
95 extensions.Add( names[filterIndex] ) ;
96 else
97 extensions.Add( current ) ;
98 if ( filterIndex == 0 || isName )
99 names.Add( current ) ;
100
101 ++filterIndex ;
102
103 const size_t extCount = extensions.GetCount();
104 for ( size_t i = 0 ; i < extCount; i++ )
105 {
b822bdc0
SC
106 wxString extensiongroup = extensions[i];
107 wxStringTokenizer tokenizer( extensiongroup , wxT(";") ) ;
108 while ( tokenizer.HasMoreTokens() )
0f9b48d1 109 {
b822bdc0
SC
110 wxString extension = tokenizer.GetNextToken() ;
111 // Remove leading '*'
112 if (extension.length() && (extension.GetChar(0) == '*'))
113 extension = extension.Mid( 1 );
bd365871 114
b822bdc0
SC
115 // Remove leading '.'
116 if (extension.length() && (extension.GetChar(0) == '.'))
117 extension = extension.Mid( 1 );
bd365871 118
05c218ee
KO
119 // Remove leading '*', this is for handling *.*
120 if (extension.length() && (extension.GetChar(0) == '*'))
121 extension = extension.Mid( 1 );
122
b822bdc0
SC
123 if ( extension.IsEmpty() )
124 {
125 if ( types != nil )
126 [types release];
127 return nil;
128 }
129
130 if ( types == nil )
131 types = [[NSMutableArray alloc] init];
bd365871 132
b822bdc0
SC
133 wxCFStringRef cfext(extension);
134 [types addObject: (NSString*)cfext.AsNSString() ];
bd365871 135#if 0
b822bdc0
SC
136 // add support for classic fileType / creator here
137 wxUint32 fileType, creator;
138 // extension -> mactypes
0f9b48d1 139#endif
b822bdc0
SC
140 }
141
0f9b48d1
SC
142 }
143 }
144 return types;
145}
146
bfa92264 147void wxFileDialog::ShowWindowModal()
0f9b48d1 148{
bfa92264
KO
149 wxCFStringRef cf( m_message );
150 wxCFStringRef dir( m_dir );
151 wxCFStringRef file( m_fileName );
152
153 wxNonOwnedWindow* parentWindow = NULL;
154
155 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
156
157 if (GetParent())
158 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
159
160 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
161
162 if (HasFlag(wxFD_SAVE))
163 {
164 NSSavePanel* sPanel = [NSSavePanel savePanel];
165 // makes things more convenient:
166 [sPanel setCanCreateDirectories:YES];
167 [sPanel setMessage:cf.AsNSString()];
168 // if we should be able to descend into pacakges we must somehow
169 // be able to pass this in
170 [sPanel setTreatsFilePackagesAsDirectories:NO];
171 [sPanel setCanSelectHiddenExtension:YES];
172
173 NSWindow* nativeParent = parentWindow->GetWXWindow();
174 ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
175 [sheetDelegate setImplementation: this];
176 [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
177 modalForWindow: nativeParent modalDelegate: sheetDelegate
178 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
179 contextInfo: nil];
180 }
181 else
182 {
183 NSArray* types = GetTypesFromFilter( m_wildCard ) ;
184 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
185 [oPanel setTreatsFilePackagesAsDirectories:NO];
186 [oPanel setCanChooseDirectories:NO];
187 [oPanel setResolvesAliases:YES];
188 [oPanel setCanChooseFiles:YES];
189 [oPanel setMessage:cf.AsNSString()];
190
191 NSWindow* nativeParent = parentWindow->GetWXWindow();
192 ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
193 [sheetDelegate setImplementation: this];
194 [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
195 types: types modalForWindow: nativeParent
196 modalDelegate: sheetDelegate
197 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
198 contextInfo: nil];
199 }
200}
bd365871 201
bfa92264
KO
202int wxFileDialog::ShowModal()
203{
0f9b48d1 204 NSSavePanel *panel = nil;
bd365871 205
0f9b48d1 206 wxCFStringRef cf( m_message );
bd365871 207
1b447793 208 wxCFStringRef dir( m_dir );
0f9b48d1 209 wxCFStringRef file( m_fileName );
bd365871 210
0f9b48d1
SC
211 m_path = wxEmptyString;
212 m_fileNames.Clear();
8b558f12 213 m_paths.Clear();
2efd54a4
SC
214 // since we don't support retrieving the matching filter
215 m_filterIndex = -1;
03647350 216
724999ee
KO
217 wxNonOwnedWindow* parentWindow = NULL;
218 int returnCode = -1;
03647350
VZ
219
220 if (GetParent())
724999ee
KO
221 {
222 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
223 }
bd365871 224
0f9b48d1
SC
225 if (HasFlag(wxFD_SAVE))
226 {
227 NSSavePanel* sPanel = [NSSavePanel savePanel];
228 // makes things more convenient:
229 [sPanel setCanCreateDirectories:YES];
230 [sPanel setMessage:cf.AsNSString()];
4dd9fdf8
SC
231 // if we should be able to descend into pacakges we must somehow
232 // be able to pass this in
0f9b48d1 233 [sPanel setTreatsFilePackagesAsDirectories:NO];
4dd9fdf8 234 [sPanel setCanSelectHiddenExtension:YES];
bd365871 235
0f9b48d1
SC
236 if ( HasFlag(wxFD_OVERWRITE_PROMPT) )
237 {
238 }
440e5cb2 239
bfa92264
KO
240 returnCode = [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ];
241 ModalFinishedCallback(sPanel, returnCode);
0f9b48d1
SC
242 }
243 else
244 {
245 NSArray* types = GetTypesFromFilter( m_wildCard ) ;
246 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
247 [oPanel setTreatsFilePackagesAsDirectories:NO];
248 [oPanel setCanChooseDirectories:NO];
249 [oPanel setResolvesAliases:YES];
250 [oPanel setCanChooseFiles:YES];
251 [oPanel setMessage:cf.AsNSString()];
bd365871 252
bfa92264 253 returnCode = [oPanel runModalForDirectory:dir.AsNSString()
724999ee 254 file:file.AsNSString() types:types];
bfa92264
KO
255
256 ModalFinishedCallback(oPanel, returnCode);
257
258 if ( types != nil )
259 [types release];
260 }
261
262 return GetReturnCode();
263}
264
265void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode)
266{
267 int result = wxID_CANCEL;
268 if (HasFlag(wxFD_SAVE))
269 {
270 if (returnCode == NSOKButton )
271 {
272 NSSavePanel* sPanel = (NSSavePanel*)panel;
273 result = wxID_OK;
274
275 m_path = wxCFStringRef::AsString([sPanel filename]);
276 m_fileName = wxFileNameFromPath(m_path);
277 m_dir = wxPathOnly( m_path );
724999ee 278 }
bfa92264
KO
279 }
280 else
281 {
282 NSOpenPanel* oPanel = (NSOpenPanel*)panel;
b2c47ad3 283 if (returnCode == NSOKButton )
0f9b48d1
SC
284 {
285 panel = oPanel;
286 result = wxID_OK;
287 NSArray* filenames = [oPanel filenames];
288 for ( size_t i = 0 ; i < [filenames count] ; ++ i )
289 {
f66ecdc4 290 wxString fnstr = wxCFStringRef::AsString([filenames objectAtIndex:i]);
0f9b48d1
SC
291 m_paths.Add( fnstr );
292 m_fileNames.Add( wxFileNameFromPath(fnstr) );
293 if ( i == 0 )
294 {
295 m_path = fnstr;
296 m_fileName = wxFileNameFromPath(fnstr);
297 m_dir = wxPathOnly( fnstr );
298 }
299 }
300 }
0f9b48d1 301 }
bfa92264
KO
302 SetReturnCode(result);
303
304 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
305 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
0f9b48d1
SC
306}
307
0f9b48d1 308#endif // wxUSE_FILEDLG