]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/filedlg.cpp
revert last change (r48897)
[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 NavMenuItemSpec menuItem;
87 menuItem.version = kNavMenuItemSpecVersion;
88 menuItem.menuCreator = 'WXNG';
89 menuItem.menuType = data->currentfilter;
90 wxMacStringToPascal( data->name[data->currentfilter] , (StringPtr)(menuItem.menuItemName) ) ;
91 ::NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &menuItem);
92 }
93 else if ( inSelector == kNavCBPopupMenuSelect )
94 {
95 NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
96 const size_t numFilters = data->extensions.GetCount();
97
98 if ( menu->menuType < numFilters )
99 {
100 data->currentfilter = menu->menuType ;
101 if ( data->saveMode )
102 {
103 int i = menu->menuType ;
104
105 // isolate the first extension string
106 wxString firstExtension = data->extensions[i].BeforeFirst('|').BeforeFirst(';');
107
108 wxString extension = firstExtension.AfterLast('.') ;
109 wxString sfilename ;
110
111 wxMacCFStringHolder cfString( NavDialogGetSaveFileName( ioParams->context ) , false );
112 sfilename = cfString.AsString() ;
113
114 int pos = sfilename.Find('.', true) ;
115 if ( pos != wxNOT_FOUND )
116 {
117 sfilename = sfilename.Left(pos+1)+extension ;
118 cfString.Assign( sfilename , wxFONTENCODING_DEFAULT ) ;
119 NavDialogSetSaveFileName( ioParams->context , cfString ) ;
120 }
121 }
122 }
123 }
124 }
125
126 void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
127 {
128 myData->menuitems = NULL ;
129 myData->currentfilter = 0 ;
130 myData->saveMode = false ;
131
132 if ( !filter.empty() )
133 {
134 wxString filter2(filter) ;
135 int filterIndex = 0;
136 bool isName = true ;
137 wxString current ;
138
139 for ( unsigned int i = 0; i < filter2.length() ; i++ )
140 {
141 if ( filter2.GetChar(i) == wxT('|') )
142 {
143 if ( isName )
144 {
145 myData->name.Add( current ) ;
146 }
147 else
148 {
149 myData->extensions.Add( current ) ;
150 ++filterIndex ;
151 }
152
153 isName = !isName ;
154 current = wxEmptyString ;
155 }
156 else
157 {
158 current += filter2.GetChar(i) ;
159 }
160 }
161 // we allow for compatibility reason to have a single filter expression (like *.*) without
162 // an explanatory text, in that case the first part is name and extension at the same time
163
164 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
165 if ( current.empty() )
166 myData->extensions.Add( myData->name[filterIndex] ) ;
167 else
168 myData->extensions.Add( current ) ;
169 if ( filterIndex == 0 || isName )
170 myData->name.Add( current ) ;
171
172 ++filterIndex ;
173
174 const size_t extCount = myData->extensions.GetCount();
175 for ( size_t i = 0 ; i < extCount; i++ )
176 {
177 wxUint32 fileType, creator;
178 wxString extension = myData->extensions[i];
179
180 // Remove leading '*'
181 if (extension.length() && (extension.GetChar(0) == '*'))
182 extension = extension.Mid( 1 );
183
184 // Remove leading '.'
185 if (extension.length() && (extension.GetChar(0) == '.'))
186 extension = extension.Mid( 1 );
187
188 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
189 myData->filtermactypes.Add( (OSType)fileType );
190 else
191 myData->filtermactypes.Add( '****' ); // We'll fail safe if it's not recognized
192 }
193 }
194 }
195
196 static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data)
197 {
198 wxString file(filename) ;
199 file.MakeUpper() ;
200
201 if ( data->extensions.GetCount() > 0 )
202 {
203 //for ( int i = 0 ; i < data->numfilters ; ++i )
204 int i = data->currentfilter ;
205 if ( data->extensions[i].Right(2) == wxT(".*") )
206 return true ;
207
208 {
209 if ( type == (OSType)data->filtermactypes[i] )
210 return true ;
211
212 wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
213 while ( tokenizer.HasMoreTokens() )
214 {
215 wxString extension = tokenizer.GetNextToken() ;
216 if ( extension.GetChar(0) == '*' )
217 extension = extension.Mid(1) ;
218 extension.MakeUpper();
219
220 if ( file.length() >= extension.length() && extension == file.Right(extension.length() ) )
221 return true ;
222 }
223 }
224
225 return false ;
226 }
227
228 return true ;
229 }
230
231 #if !TARGET_API_MAC_OSX
232 static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
233 {
234 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
235 // return true if this item is invisible or a file
236
237 Boolean visibleFlag;
238 Boolean folderFlag;
239
240 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
241 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
242
243 // because the semantics of the filter proc are "true means don't show
244 // it" we need to invert the result that we return
245
246 if ( !visibleFlag )
247 return true ;
248
249 if ( !folderFlag )
250 {
251 wxString file = wxMacMakeStringFromPascal( myCInfoPBPtr->hFileInfo.ioNamePtr ) ;
252 return !CheckFile( file , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
253 }
254
255 return false ;
256 }
257 #endif
258
259 // end wxmac
260
261 wxFileDialog::wxFileDialog(
262 wxWindow *parent, const wxString& message,
263 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
264 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
265 : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
266 {
267 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
268 }
269
270 pascal Boolean CrossPlatformFilterCallback(
271 AEDesc *theItem,
272 void *info,
273 void *callBackUD,
274 NavFilterModes filterMode )
275 {
276 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
277
278 if (filterMode == kNavFilteringBrowserList)
279 {
280 // We allow navigation to all folders. For files, we check against the current
281 // filter string.
282 // However, packages should be dealt with like files and not like folders. So
283 // check if a folder is a package before deciding what to do.
284 FSRef fsref;
285 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
286 AECoerceDesc (theItem, typeFSRef, theItem);
287 if ( AEGetDescData (theItem, &fsref, sizeof (FSRef)) != noErr)
288 return true;
289
290 if ( theInfo->isFolder )
291 {
292 // check bundle bit (using Finder Services - used by OS9 on some bundles)
293 FSCatalogInfo catalogInfo;
294 if (FSGetCatalogInfo (&fsref, kFSCatInfoFinderInfo, &catalogInfo, NULL, NULL, NULL) != noErr)
295 return true;
296
297 // Check bundle item (using Launch Services - used by OS-X through info.plist or APP)
298 LSItemInfoRecord lsInfo;
299 if (LSCopyItemInfoForRef(&fsref, kLSRequestBasicFlagsOnly, &lsInfo ) != noErr)
300 return true;
301
302 // If it's not a bundle, then it's a normal folder and it passes our filter
303 FileInfo *fileInfo = (FileInfo *) catalogInfo.finderInfo;
304 if ( !(fileInfo->finderFlags & kHasBundle) &&
305 !(lsInfo.flags & (kLSItemInfoIsApplication | kLSItemInfoIsPackage)) )
306 return true;
307 }
308
309 wxString file = wxMacFSRefToPath( &fsref ) ;
310 return CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
311
312 }
313
314 return true;
315 }
316
317 int wxFileDialog::ShowModal()
318 {
319 OSErr err;
320 NavDialogCreationOptions dialogCreateOptions;
321
322 // set default options
323 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
324
325 // this was always unset in the old code
326 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
327
328 wxMacCFStringHolder message(m_message, m_font.GetEncoding());
329 dialogCreateOptions.windowTitle = message;
330
331 wxMacCFStringHolder defaultFileName(m_fileName, m_font.GetEncoding());
332 dialogCreateOptions.saveFileName = defaultFileName;
333
334
335 NavDialogRef dialog;
336 NavObjectFilterUPP navFilterUPP = NULL;
337 OpenUserDataRec myData;
338 myData.defaultLocation = m_dir;
339
340 MakeUserDataRec(&myData , m_wildCard);
341 myData.currentfilter = m_filterIndex;
342 size_t numFilters = myData.extensions.GetCount();
343 if (numFilters)
344 {
345 CFMutableArrayRef popup = CFArrayCreateMutable( kCFAllocatorDefault ,
346 numFilters , &kCFTypeArrayCallBacks ) ;
347 dialogCreateOptions.popupExtension = popup ;
348 myData.menuitems = dialogCreateOptions.popupExtension ;
349 for ( size_t i = 0 ; i < numFilters ; ++i )
350 {
351 CFArrayAppendValue( popup , (CFStringRef) wxMacCFStringHolder( myData.name[i] , m_font.GetEncoding() ) ) ;
352 }
353 }
354
355 if (HasFdFlag(wxFD_SAVE))
356 {
357 myData.saveMode = true;
358
359 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
360 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
361 if (!numFilters)
362 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
363
364 // The extension is important
365 if (numFilters < 2)
366 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
367
368 #if TARGET_API_MAC_OSX
369 if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT))
370 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
371 #endif
372
373 err = ::NavCreatePutFileDialog(
374 &dialogCreateOptions,
375 kNavGenericSignature, // Suppresses the 'Default' (top) menu item
376 kNavGenericSignature,
377 sStandardNavEventFilter,
378 &myData, // for defaultLocation
379 &dialog );
380 }
381 else
382 {
383 // let the user select bundles/programs in dialogs
384 dialogCreateOptions.optionFlags |= kNavSupportPackages;
385
386 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
387 err = ::NavCreateGetFileDialog(
388 &dialogCreateOptions,
389 NULL, // NavTypeListHandle
390 sStandardNavEventFilter,
391 NULL, // NavPreviewUPP
392 navFilterUPP,
393 (void *) &myData, // inClientData
394 &dialog );
395 }
396
397 if (err == noErr)
398 err = ::NavDialogRun(dialog);
399
400 // clean up filter related data, etc.
401 if (navFilterUPP)
402 ::DisposeNavObjectFilterUPP(navFilterUPP);
403
404 if (err != noErr)
405 return wxID_CANCEL;
406
407 NavReplyRecord navReply;
408 err = ::NavDialogGetReply(dialog, &navReply);
409 if (err == noErr && navReply.validRecord)
410 {
411 AEKeyword theKeyword;
412 DescType actualType;
413 Size actualSize;
414 FSRef theFSRef;
415 wxString thePath ;
416 long count;
417
418 m_filterIndex = myData.currentfilter;
419 ::AECountItems( &navReply.selection, &count );
420 for (long i = 1; i <= count; ++i)
421 {
422 err = ::AEGetNthPtr(
423 &(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
424 &theFSRef, sizeof(theFSRef), &actualSize );
425 if (err != noErr)
426 break;
427
428 if (HasFdFlag(wxFD_SAVE))
429 thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName );
430 else
431 thePath = wxMacFSRefToPath( &theFSRef );
432
433 if (!thePath)
434 {
435 ::NavDisposeReply(&navReply);
436 return wxID_CANCEL;
437 }
438
439 m_path = thePath;
440 m_paths.Add(m_path);
441 m_fileName = wxFileNameFromPath(m_path);
442 m_fileNames.Add(m_fileName);
443 }
444
445 // set these to the first hit
446 m_path = m_paths[0];
447 m_fileName = wxFileNameFromPath(m_path);
448 m_dir = wxPathOnly(m_path);
449 }
450
451 ::NavDisposeReply(&navReply);
452
453 return (err == noErr) ? wxID_OK : wxID_CANCEL;
454 }
455
456 #endif // wxUSE_FILEDLG
457