]>
Commit | Line | Data |
---|---|---|
489468fe | 1 | ///////////////////////////////////////////////////////////////////////////// |
524c47aa | 2 | // Name: src/osx/carbon/filedlg.cpp |
489468fe SC |
3 | // Purpose: wxFileDialog |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #if wxUSE_FILEDLG | |
15 | ||
16 | #include "wx/filedlg.h" | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/intl.h" | |
20 | #include "wx/app.h" | |
21 | #include "wx/utils.h" | |
22 | #include "wx/dialog.h" | |
23 | #endif | |
24 | ||
25 | #include "wx/tokenzr.h" | |
26 | #include "wx/filename.h" | |
27 | ||
1f0c8f31 | 28 | #include "wx/osx/private.h" |
489468fe SC |
29 | |
30 | #ifndef __DARWIN__ | |
31 | #include <Navigation.h> | |
32 | #include "PLStringFuncs.h" | |
33 | #endif | |
34 | ||
35 | IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase) | |
36 | ||
37 | // the data we need to pass to our standard file hook routine | |
38 | // includes a pointer to the dialog, a pointer to the standard | |
39 | // file reply record (so we can inspect the current selection) | |
40 | // and a copy of the "previous" file spec of the reply record | |
41 | // so we can see if the selection has changed | |
42 | ||
43 | struct OpenUserDataRec | |
44 | { | |
45 | int currentfilter ; | |
46 | bool saveMode ; | |
47 | wxArrayString name ; | |
48 | wxArrayString extensions ; | |
49 | wxArrayLong filtermactypes ; | |
50 | wxString defaultLocation; | |
51 | CFArrayRef menuitems ; | |
52 | }; | |
53 | ||
54 | typedef struct OpenUserDataRec | |
55 | OpenUserDataRec, *OpenUserDataRecPtr; | |
56 | ||
57 | static pascal void NavEventProc( | |
58 | NavEventCallbackMessage inSelector, | |
59 | NavCBRecPtr ioParams, | |
60 | NavCallBackUserData ioUserData ); | |
61 | ||
62 | static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc); | |
63 | ||
64 | static pascal void NavEventProc( | |
65 | NavEventCallbackMessage inSelector, | |
66 | NavCBRecPtr ioParams, | |
67 | NavCallBackUserData ioUserData ) | |
68 | { | |
69 | OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ; | |
70 | if (inSelector == kNavCBEvent) | |
71 | { | |
72 | } | |
73 | else if ( inSelector == kNavCBStart ) | |
74 | { | |
75 | if (data && !(data->defaultLocation).empty()) | |
76 | { | |
77 | // Set default location for the modern Navigation APIs | |
78 | // Apple Technical Q&A 1151 | |
79 | FSRef theFile; | |
80 | wxMacPathToFSRef(data->defaultLocation, &theFile); | |
81 | AEDesc theLocation = { typeNull, NULL }; | |
82 | if (noErr == ::AECreateDesc(typeFSRef, &theFile, sizeof(FSRef), &theLocation)) | |
83 | ::NavCustomControl(ioParams->context, kNavCtlSetLocation, (void *) &theLocation); | |
84 | } | |
85 | ||
86 | if( data->extensions.GetCount() > 0 ) | |
87 | { | |
88 | NavMenuItemSpec menuItem; | |
89 | memset( &menuItem, 0, sizeof(menuItem) ); | |
90 | menuItem.version = kNavMenuItemSpecVersion; | |
91 | menuItem.menuType = data->currentfilter; | |
92 | ::NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &menuItem); | |
93 | } | |
94 | } | |
95 | else if ( inSelector == kNavCBPopupMenuSelect ) | |
96 | { | |
97 | NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ; | |
98 | const size_t numFilters = data->extensions.GetCount(); | |
99 | ||
100 | if ( menu->menuType < numFilters ) | |
101 | { | |
102 | data->currentfilter = menu->menuType ; | |
103 | if ( data->saveMode ) | |
104 | { | |
105 | int i = menu->menuType ; | |
106 | ||
107 | // isolate the first extension string | |
108 | wxString firstExtension = data->extensions[i].BeforeFirst('|').BeforeFirst(';'); | |
109 | ||
110 | wxString extension = firstExtension.AfterLast('.') ; | |
111 | wxString sfilename ; | |
112 | ||
113 | wxCFStringRef cfString( wxCFRetain( NavDialogGetSaveFileName( ioParams->context ) ) ); | |
114 | sfilename = cfString.AsString() ; | |
115 | ||
116 | int pos = sfilename.Find('.', true) ; | |
117 | if ( pos != wxNOT_FOUND && extension != wxT("*") ) | |
118 | { | |
119 | sfilename = sfilename.Left(pos+1)+extension ; | |
120 | cfString = wxCFStringRef( sfilename , wxFONTENCODING_DEFAULT ) ; | |
121 | NavDialogSetSaveFileName( ioParams->context , cfString ) ; | |
122 | } | |
123 | } | |
124 | } | |
125 | } | |
126 | } | |
127 | ||
128 | void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter ) | |
129 | { | |
130 | myData->menuitems = NULL ; | |
131 | myData->currentfilter = 0 ; | |
132 | myData->saveMode = false ; | |
133 | ||
134 | if ( !filter.empty() ) | |
135 | { | |
136 | wxString filter2(filter) ; | |
137 | int filterIndex = 0; | |
138 | bool isName = true ; | |
139 | wxString current ; | |
140 | ||
141 | for ( unsigned int i = 0; i < filter2.length() ; i++ ) | |
142 | { | |
143 | if ( filter2.GetChar(i) == wxT('|') ) | |
144 | { | |
145 | if ( isName ) | |
146 | { | |
147 | myData->name.Add( current ) ; | |
148 | } | |
149 | else | |
150 | { | |
151 | myData->extensions.Add( current ) ; | |
152 | ++filterIndex ; | |
153 | } | |
154 | ||
155 | isName = !isName ; | |
156 | current = wxEmptyString ; | |
157 | } | |
158 | else | |
159 | { | |
160 | current += filter2.GetChar(i) ; | |
161 | } | |
162 | } | |
163 | // we allow for compatibility reason to have a single filter expression (like *.*) without | |
164 | // an explanatory text, in that case the first part is name and extension at the same time | |
165 | ||
166 | wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ; | |
167 | if ( current.empty() ) | |
168 | myData->extensions.Add( myData->name[filterIndex] ) ; | |
169 | else | |
170 | myData->extensions.Add( current ) ; | |
171 | if ( filterIndex == 0 || isName ) | |
172 | myData->name.Add( current ) ; | |
173 | ||
174 | ++filterIndex ; | |
175 | ||
176 | const size_t extCount = myData->extensions.GetCount(); | |
177 | for ( size_t i = 0 ; i < extCount; i++ ) | |
178 | { | |
179 | wxUint32 fileType, creator; | |
180 | wxString extension = myData->extensions[i]; | |
181 | ||
182 | // Remove leading '*' | |
183 | if (extension.length() && (extension.GetChar(0) == '*')) | |
184 | extension = extension.Mid( 1 ); | |
185 | ||
186 | // Remove leading '.' | |
187 | if (extension.length() && (extension.GetChar(0) == '.')) | |
188 | extension = extension.Mid( 1 ); | |
189 | ||
190 | if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator )) | |
191 | myData->filtermactypes.Add( (OSType)fileType ); | |
192 | else | |
193 | myData->filtermactypes.Add( '****' ); // We'll fail safe if it's not recognized | |
194 | } | |
195 | } | |
196 | } | |
197 | ||
198 | static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data) | |
199 | { | |
200 | wxString file(filename) ; | |
201 | file.MakeUpper() ; | |
202 | ||
203 | if ( data->extensions.GetCount() > 0 ) | |
204 | { | |
205 | //for ( int i = 0 ; i < data->numfilters ; ++i ) | |
206 | int i = data->currentfilter ; | |
207 | if ( data->extensions[i].Right(2) == wxT(".*") ) | |
208 | return true ; | |
209 | ||
210 | { | |
211 | if ( type == (OSType)data->filtermactypes[i] ) | |
212 | return true ; | |
213 | ||
214 | wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ; | |
215 | while ( tokenizer.HasMoreTokens() ) | |
216 | { | |
217 | wxString extension = tokenizer.GetNextToken() ; | |
218 | if ( extension.GetChar(0) == '*' ) | |
219 | extension = extension.Mid(1) ; | |
220 | extension.MakeUpper(); | |
221 | ||
222 | if ( file.length() >= extension.length() && extension == file.Right(extension.length() ) ) | |
223 | return true ; | |
224 | } | |
225 | } | |
226 | ||
227 | return false ; | |
228 | } | |
229 | ||
230 | return true ; | |
231 | } | |
232 | ||
233 | // end wxmac | |
234 | ||
235 | wxFileDialog::wxFileDialog( | |
236 | wxWindow *parent, const wxString& message, | |
237 | const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard, | |
238 | long style, const wxPoint& pos, const wxSize& sz, const wxString& name) | |
239 | : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name) | |
240 | { | |
241 | wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ; | |
242 | } | |
243 | ||
244 | pascal Boolean CrossPlatformFilterCallback( | |
245 | AEDesc *theItem, | |
246 | void *info, | |
247 | void *callBackUD, | |
248 | NavFilterModes filterMode ) | |
249 | { | |
250 | OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ; | |
251 | ||
252 | if (filterMode == kNavFilteringBrowserList) | |
253 | { | |
254 | // We allow navigation to all folders. For files, we check against the current | |
255 | // filter string. | |
256 | // However, packages should be dealt with like files and not like folders. So | |
257 | // check if a folder is a package before deciding what to do. | |
258 | NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ; | |
259 | FSRef fsref; | |
260 | ||
261 | if ( theInfo->isFolder ) | |
262 | { | |
263 | // check bundle bit (using Finder Services - used by OS9 on some bundles) | |
264 | FSCatalogInfo catalogInfo; | |
265 | if (FSGetCatalogInfo (&fsref, kFSCatInfoFinderInfo, &catalogInfo, NULL, NULL, NULL) != noErr) | |
266 | return true; | |
267 | ||
268 | // Check bundle item (using Launch Services - used by OS-X through info.plist or APP) | |
269 | LSItemInfoRecord lsInfo; | |
270 | if (LSCopyItemInfoForRef(&fsref, kLSRequestBasicFlagsOnly, &lsInfo ) != noErr) | |
271 | return true; | |
272 | ||
273 | // If it's not a bundle, then it's a normal folder and it passes our filter | |
274 | FileInfo *fileInfo = (FileInfo *) catalogInfo.finderInfo; | |
275 | if ( !(fileInfo->finderFlags & kHasBundle) && | |
276 | !(lsInfo.flags & (kLSItemInfoIsApplication | kLSItemInfoIsPackage)) ) | |
277 | return true; | |
278 | } | |
279 | else | |
280 | { | |
281 | AECoerceDesc (theItem, typeFSRef, theItem); | |
282 | if ( AEGetDescData (theItem, &fsref, sizeof (FSRef)) == noErr) | |
283 | { | |
284 | wxString file = wxMacFSRefToPath( &fsref ) ; | |
285 | return CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ; | |
286 | } | |
287 | } | |
288 | } | |
289 | ||
290 | return true; | |
291 | } | |
292 | ||
293 | int wxFileDialog::ShowModal() | |
294 | { | |
8b558f12 JS |
295 | m_paths.Empty(); |
296 | m_fileNames.Empty(); | |
297 | ||
489468fe SC |
298 | OSErr err; |
299 | NavDialogCreationOptions dialogCreateOptions; | |
300 | ||
301 | // set default options | |
302 | ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions); | |
303 | ||
304 | // this was always unset in the old code | |
305 | dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation; | |
306 | ||
307 | wxCFStringRef message(m_message, GetFont().GetEncoding()); | |
308 | dialogCreateOptions.windowTitle = message; | |
309 | ||
310 | wxCFStringRef defaultFileName(m_fileName, GetFont().GetEncoding()); | |
311 | dialogCreateOptions.saveFileName = defaultFileName; | |
312 | ||
313 | ||
314 | NavDialogRef dialog; | |
315 | NavObjectFilterUPP navFilterUPP = NULL; | |
316 | OpenUserDataRec myData; | |
317 | myData.defaultLocation = m_dir; | |
318 | ||
319 | MakeUserDataRec(&myData , m_wildCard); | |
320 | myData.currentfilter = m_filterIndex; | |
321 | size_t numFilters = myData.extensions.GetCount(); | |
322 | if (numFilters) | |
323 | { | |
324 | CFMutableArrayRef popup = CFArrayCreateMutable( kCFAllocatorDefault , | |
325 | numFilters , &kCFTypeArrayCallBacks ) ; | |
326 | dialogCreateOptions.popupExtension = popup ; | |
327 | myData.menuitems = dialogCreateOptions.popupExtension ; | |
328 | for ( size_t i = 0 ; i < numFilters ; ++i ) | |
329 | { | |
330 | CFArrayAppendValue( popup , (CFStringRef) wxCFStringRef( myData.name[i] , GetFont().GetEncoding() ) ) ; | |
331 | } | |
332 | } | |
333 | ||
334 | if (HasFdFlag(wxFD_SAVE)) | |
335 | { | |
336 | myData.saveMode = true; | |
337 | ||
338 | dialogCreateOptions.optionFlags |= kNavDontAutoTranslate; | |
339 | dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems; | |
340 | if (!numFilters) | |
341 | dialogCreateOptions.optionFlags |= kNavNoTypePopup; | |
342 | ||
343 | // The extension is important | |
344 | if (numFilters < 2) | |
345 | dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension; | |
346 | ||
347 | if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT)) | |
348 | dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement; | |
349 | ||
350 | err = ::NavCreatePutFileDialog( | |
351 | &dialogCreateOptions, | |
352 | kNavGenericSignature, // Suppresses the 'Default' (top) menu item | |
353 | kNavGenericSignature, | |
354 | sStandardNavEventFilter, | |
355 | &myData, // for defaultLocation | |
356 | &dialog ); | |
357 | } | |
358 | else | |
359 | { | |
360 | // let the user select bundles/programs in dialogs | |
361 | dialogCreateOptions.optionFlags |= kNavSupportPackages; | |
362 | ||
363 | navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback); | |
364 | err = ::NavCreateGetFileDialog( | |
365 | &dialogCreateOptions, | |
366 | NULL, // NavTypeListHandle | |
367 | sStandardNavEventFilter, | |
368 | NULL, // NavPreviewUPP | |
369 | navFilterUPP, | |
370 | (void *) &myData, // inClientData | |
371 | &dialog ); | |
372 | } | |
373 | ||
374 | if (err == noErr) | |
375 | err = ::NavDialogRun(dialog); | |
376 | ||
377 | // clean up filter related data, etc. | |
378 | if (navFilterUPP) | |
379 | ::DisposeNavObjectFilterUPP(navFilterUPP); | |
380 | ||
381 | if (err != noErr) | |
382 | { | |
383 | ::NavDialogDispose(dialog); | |
384 | return wxID_CANCEL; | |
385 | } | |
386 | ||
387 | NavReplyRecord navReply; | |
388 | err = ::NavDialogGetReply(dialog, &navReply); | |
389 | if (err == noErr && navReply.validRecord) | |
390 | { | |
391 | AEKeyword theKeyword; | |
392 | DescType actualType; | |
393 | Size actualSize; | |
394 | FSRef theFSRef; | |
395 | wxString thePath ; | |
396 | long count; | |
397 | ||
398 | m_filterIndex = myData.currentfilter; | |
399 | ::AECountItems( &navReply.selection, &count ); | |
400 | for (long i = 1; i <= count; ++i) | |
401 | { | |
402 | err = ::AEGetNthPtr( | |
403 | &(navReply.selection), i, typeFSRef, &theKeyword, &actualType, | |
404 | &theFSRef, sizeof(theFSRef), &actualSize ); | |
405 | if (err != noErr) | |
406 | break; | |
407 | ||
408 | if (HasFdFlag(wxFD_SAVE)) | |
409 | thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName ); | |
410 | else | |
411 | thePath = wxMacFSRefToPath( &theFSRef ); | |
412 | ||
413 | if (!thePath) | |
414 | { | |
415 | ::NavDisposeReply(&navReply); | |
416 | ::NavDialogDispose(dialog); | |
417 | return wxID_CANCEL; | |
418 | } | |
419 | ||
420 | m_path = thePath; | |
421 | m_paths.Add(m_path); | |
422 | m_fileName = wxFileNameFromPath(m_path); | |
423 | m_fileNames.Add(m_fileName); | |
424 | } | |
425 | ||
426 | // set these to the first hit | |
427 | m_path = m_paths[0]; | |
428 | m_fileName = wxFileNameFromPath(m_path); | |
429 | m_dir = wxPathOnly(m_path); | |
430 | } | |
431 | ||
432 | ::NavDisposeReply(&navReply); | |
433 | ::NavDialogDispose(dialog); | |
434 | ||
435 | return (err == noErr) ? wxID_OK : wxID_CANCEL; | |
436 | } | |
437 | ||
438 | #endif // wxUSE_FILEDLG | |
439 |