]>
Commit | Line | Data |
---|---|---|
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 |
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 | ||
0f9b48d1 SC |
55 | NSArray* 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 | |
b822bdc0 SC |
119 | if ( extension.IsEmpty() ) |
120 | { | |
121 | if ( types != nil ) | |
122 | [types release]; | |
123 | return nil; | |
124 | } | |
125 | ||
126 | if ( types == nil ) | |
127 | types = [[NSMutableArray alloc] init]; | |
bd365871 | 128 | |
b822bdc0 SC |
129 | wxCFStringRef cfext(extension); |
130 | [types addObject: (NSString*)cfext.AsNSString() ]; | |
bd365871 | 131 | #if 0 |
b822bdc0 SC |
132 | // add support for classic fileType / creator here |
133 | wxUint32 fileType, creator; | |
134 | // extension -> mactypes | |
0f9b48d1 | 135 | #endif |
b822bdc0 SC |
136 | } |
137 | ||
0f9b48d1 SC |
138 | } |
139 | } | |
140 | return types; | |
141 | } | |
142 | ||
143 | int wxFileDialog::ShowModal() | |
144 | { | |
145 | int result = wxID_CANCEL; | |
bd365871 | 146 | |
0f9b48d1 | 147 | NSSavePanel *panel = nil; |
bd365871 | 148 | |
0f9b48d1 | 149 | wxCFStringRef cf( m_message ); |
bd365871 | 150 | |
1b447793 | 151 | wxCFStringRef dir( m_dir ); |
0f9b48d1 | 152 | wxCFStringRef file( m_fileName ); |
bd365871 | 153 | |
0f9b48d1 SC |
154 | m_path = wxEmptyString; |
155 | m_fileNames.Clear(); | |
724999ee KO |
156 | |
157 | wxNonOwnedWindow* parentWindow = NULL; | |
158 | int returnCode = -1; | |
159 | ||
160 | if (GetParent()) | |
161 | { | |
162 | parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent())); | |
163 | } | |
bd365871 | 164 | |
0f9b48d1 SC |
165 | if (HasFlag(wxFD_SAVE)) |
166 | { | |
167 | NSSavePanel* sPanel = [NSSavePanel savePanel]; | |
168 | // makes things more convenient: | |
169 | [sPanel setCanCreateDirectories:YES]; | |
170 | [sPanel setMessage:cf.AsNSString()]; | |
4dd9fdf8 SC |
171 | // if we should be able to descend into pacakges we must somehow |
172 | // be able to pass this in | |
0f9b48d1 | 173 | [sPanel setTreatsFilePackagesAsDirectories:NO]; |
4dd9fdf8 | 174 | [sPanel setCanSelectHiddenExtension:YES]; |
bd365871 | 175 | |
0f9b48d1 SC |
176 | if ( HasFlag(wxFD_OVERWRITE_PROMPT) ) |
177 | { | |
178 | } | |
440e5cb2 KO |
179 | |
180 | /* | |
181 | if (parentWindow) | |
724999ee KO |
182 | { |
183 | NSWindow* nativeParent = parentWindow->GetWXWindow(); | |
184 | ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init]; | |
185 | [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString() | |
186 | modalForWindow: nativeParent modalDelegate: sheetDelegate | |
187 | didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) | |
188 | contextInfo: nil]; | |
189 | [sheetDelegate waitForSheetToFinish]; | |
b2c47ad3 | 190 | returnCode = [sheetDelegate code]; |
724999ee KO |
191 | [sheetDelegate release]; |
192 | } | |
193 | else | |
440e5cb2 | 194 | */ |
724999ee | 195 | { |
b2c47ad3 | 196 | returnCode = [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ]; |
724999ee KO |
197 | } |
198 | ||
b2c47ad3 | 199 | if (returnCode == NSOKButton ) |
0f9b48d1 SC |
200 | { |
201 | panel = sPanel; | |
202 | result = wxID_OK; | |
bd365871 | 203 | |
0f9b48d1 SC |
204 | wxCFStringRef filename( [[sPanel filename] retain] ); |
205 | ||
206 | m_path = filename.AsString(); | |
207 | m_fileName = wxFileNameFromPath(m_path); | |
208 | m_dir = wxPathOnly( m_path ); | |
209 | } | |
210 | } | |
211 | else | |
212 | { | |
213 | NSArray* types = GetTypesFromFilter( m_wildCard ) ; | |
214 | NSOpenPanel* oPanel = [NSOpenPanel openPanel]; | |
215 | [oPanel setTreatsFilePackagesAsDirectories:NO]; | |
216 | [oPanel setCanChooseDirectories:NO]; | |
217 | [oPanel setResolvesAliases:YES]; | |
218 | [oPanel setCanChooseFiles:YES]; | |
219 | [oPanel setMessage:cf.AsNSString()]; | |
bd365871 | 220 | |
440e5cb2 KO |
221 | /* |
222 | if (parentWindow) | |
724999ee KO |
223 | { |
224 | NSWindow* nativeParent = parentWindow->GetWXWindow(); | |
225 | ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init]; | |
226 | [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString() | |
227 | types: types modalForWindow: nativeParent | |
228 | modalDelegate: sheetDelegate | |
229 | didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) | |
230 | contextInfo: nil]; | |
231 | [sheetDelegate waitForSheetToFinish]; | |
b2c47ad3 | 232 | returnCode = [sheetDelegate code]; |
724999ee KO |
233 | [sheetDelegate release]; |
234 | } | |
235 | else | |
440e5cb2 | 236 | */ |
724999ee | 237 | { |
b2c47ad3 | 238 | returnCode = [oPanel runModalForDirectory:dir.AsNSString() |
724999ee KO |
239 | file:file.AsNSString() types:types]; |
240 | } | |
b2c47ad3 | 241 | if (returnCode == NSOKButton ) |
0f9b48d1 SC |
242 | { |
243 | panel = oPanel; | |
244 | result = wxID_OK; | |
245 | NSArray* filenames = [oPanel filenames]; | |
246 | for ( size_t i = 0 ; i < [filenames count] ; ++ i ) | |
247 | { | |
248 | wxCFStringRef filename( [(NSString*) [filenames objectAtIndex:i] retain] ); | |
249 | wxString fnstr = filename.AsString(); | |
250 | m_paths.Add( fnstr ); | |
251 | m_fileNames.Add( wxFileNameFromPath(fnstr) ); | |
252 | if ( i == 0 ) | |
253 | { | |
254 | m_path = fnstr; | |
255 | m_fileName = wxFileNameFromPath(fnstr); | |
256 | m_dir = wxPathOnly( fnstr ); | |
257 | } | |
258 | } | |
259 | } | |
260 | if ( types != nil ) | |
261 | [types release]; | |
262 | } | |
bd365871 | 263 | |
0f9b48d1 SC |
264 | return result; |
265 | } | |
266 | ||
0f9b48d1 | 267 | #endif // wxUSE_FILEDLG |