]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/filedlg.cpp
cleanup
[wxWidgets.git] / src / mac / carbon / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/filedlg.cpp
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
28 #include "wx/mac/private.h"
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 wxMacCFStringHolder cfString( NavDialogGetSaveFileName( ioParams->context ) , false );
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.Assign( 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 {
295 OSErr err;
296 NavDialogCreationOptions dialogCreateOptions;
297
298 // set default options
299 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
300
301 // this was always unset in the old code
302 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
303
304 wxMacCFStringHolder message(m_message, m_font.GetEncoding());
305 dialogCreateOptions.windowTitle = message;
306
307 wxMacCFStringHolder defaultFileName(m_fileName, m_font.GetEncoding());
308 dialogCreateOptions.saveFileName = defaultFileName;
309
310
311 NavDialogRef dialog;
312 NavObjectFilterUPP navFilterUPP = NULL;
313 OpenUserDataRec myData;
314 myData.defaultLocation = m_dir;
315
316 MakeUserDataRec(&myData , m_wildCard);
317 myData.currentfilter = m_filterIndex;
318 size_t numFilters = myData.extensions.GetCount();
319 if (numFilters)
320 {
321 CFMutableArrayRef popup = CFArrayCreateMutable( kCFAllocatorDefault ,
322 numFilters , &kCFTypeArrayCallBacks ) ;
323 dialogCreateOptions.popupExtension = popup ;
324 myData.menuitems = dialogCreateOptions.popupExtension ;
325 for ( size_t i = 0 ; i < numFilters ; ++i )
326 {
327 CFArrayAppendValue( popup , (CFStringRef) wxMacCFStringHolder( myData.name[i] , m_font.GetEncoding() ) ) ;
328 }
329 }
330
331 if (HasFdFlag(wxFD_SAVE))
332 {
333 myData.saveMode = true;
334
335 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
336 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
337 if (!numFilters)
338 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
339
340 // The extension is important
341 if (numFilters < 2)
342 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
343
344 #if TARGET_API_MAC_OSX
345 if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT))
346 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
347 #endif
348
349 err = ::NavCreatePutFileDialog(
350 &dialogCreateOptions,
351 kNavGenericSignature, // Suppresses the 'Default' (top) menu item
352 kNavGenericSignature,
353 sStandardNavEventFilter,
354 &myData, // for defaultLocation
355 &dialog );
356 }
357 else
358 {
359 // let the user select bundles/programs in dialogs
360 dialogCreateOptions.optionFlags |= kNavSupportPackages;
361
362 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
363 err = ::NavCreateGetFileDialog(
364 &dialogCreateOptions,
365 NULL, // NavTypeListHandle
366 sStandardNavEventFilter,
367 NULL, // NavPreviewUPP
368 navFilterUPP,
369 (void *) &myData, // inClientData
370 &dialog );
371 }
372
373 if (err == noErr)
374 err = ::NavDialogRun(dialog);
375
376 // clean up filter related data, etc.
377 if (navFilterUPP)
378 ::DisposeNavObjectFilterUPP(navFilterUPP);
379
380 if (err != noErr)
381 {
382 ::NavDialogDispose(dialog);
383 return wxID_CANCEL;
384 }
385
386 NavReplyRecord navReply;
387 err = ::NavDialogGetReply(dialog, &navReply);
388 if (err == noErr && navReply.validRecord)
389 {
390 AEKeyword theKeyword;
391 DescType actualType;
392 Size actualSize;
393 FSRef theFSRef;
394 wxString thePath ;
395 long count;
396
397 m_filterIndex = myData.currentfilter;
398 ::AECountItems( &navReply.selection, &count );
399 for (long i = 1; i <= count; ++i)
400 {
401 err = ::AEGetNthPtr(
402 &(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
403 &theFSRef, sizeof(theFSRef), &actualSize );
404 if (err != noErr)
405 break;
406
407 if (HasFdFlag(wxFD_SAVE))
408 thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName );
409 else
410 thePath = wxMacFSRefToPath( &theFSRef );
411
412 if (!thePath)
413 {
414 ::NavDisposeReply(&navReply);
415 ::NavDialogDispose(dialog);
416 return wxID_CANCEL;
417 }
418
419 m_path = thePath;
420 m_paths.Add(m_path);
421 m_fileName = wxFileNameFromPath(m_path);
422 m_fileNames.Add(m_fileName);
423 }
424
425 // set these to the first hit
426 m_path = m_paths[0];
427 m_fileName = wxFileNameFromPath(m_path);
428 m_dir = wxPathOnly(m_path);
429 }
430
431 ::NavDisposeReply(&navReply);
432 ::NavDialogDispose(dialog);
433
434 return (err == noErr) ? wxID_OK : wxID_CANCEL;
435 }
436
437 #endif // wxUSE_FILEDLG
438