]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/filedlg.cpp
more cleanup
[wxWidgets.git] / src / mac / carbon / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #include "wx/app.h"
15 #include "wx/utils.h"
16 #include "wx/dialog.h"
17 #include "wx/filedlg.h"
18 #include "wx/intl.h"
19 #include "wx/tokenzr.h"
20 #include "wx/filename.h"
21
22 #ifndef __DARWIN__
23 #include "PLStringFuncs.h"
24 #endif
25
26 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
27
28 // begin wxmac
29
30 #include "wx/mac/private.h"
31
32 #ifndef __DARWIN__
33 #include <Navigation.h>
34 #endif
35
36 #include "MoreFilesX.h"
37
38 extern bool gUseNavServices ;
39
40 // the data we need to pass to our standard file hook routine
41 // includes a pointer to the dialog, a pointer to the standard
42 // file reply record (so we can inspect the current selection)
43 // and a copy of the "previous" file spec of the reply record
44 // so we can see if the selection has changed
45
46 struct OpenUserDataRec {
47 int currentfilter ;
48 bool saveMode ;
49 wxArrayString name ;
50 wxArrayString extensions ;
51 wxArrayLong filtermactypes ;
52 wxString defaultLocation;
53 CFArrayRef menuitems ;
54 };
55
56 typedef struct OpenUserDataRec
57 OpenUserDataRec, *OpenUserDataRecPtr;
58
59 static pascal void NavEventProc(
60 NavEventCallbackMessage inSelector,
61 NavCBRecPtr ioParams,
62 NavCallBackUserData ioUserData);
63
64 static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc);
65
66 static pascal void
67 NavEventProc(
68 NavEventCallbackMessage inSelector,
69 NavCBRecPtr ioParams,
70 NavCallBackUserData ioUserData )
71 {
72 OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ;
73 if (inSelector == kNavCBEvent) {
74 }
75 else if ( inSelector == kNavCBStart )
76 {
77 if (data && !(data->defaultLocation).IsEmpty())
78 {
79 // Set default location for the modern Navigation APIs
80 // Apple Technical Q&A 1151
81 FSSpec theFSSpec;
82 wxMacFilename2FSSpec(data->defaultLocation, &theFSSpec);
83 AEDesc theLocation = {typeNull, NULL};
84 if (noErr == ::AECreateDesc(typeFSS, &theFSSpec, sizeof(FSSpec), &theLocation))
85 ::NavCustomControl(ioParams->context, kNavCtlSetLocation, (void *) &theLocation);
86 }
87
88 NavMenuItemSpec menuItem;
89 menuItem.version = kNavMenuItemSpecVersion;
90 menuItem.menuCreator = 'WXNG';
91 menuItem.menuType = data->currentfilter;
92 wxMacStringToPascal( data->name[data->currentfilter] , (StringPtr)(menuItem.menuItemName) ) ;
93 ::NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &menuItem);
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 wxString extension = data->extensions[i].AfterLast('.') ;
107 extension.MakeLower() ;
108 wxString sfilename ;
109
110 wxMacCFStringHolder cfString( NavDialogGetSaveFileName( ioParams->context ) , false );
111 sfilename = cfString.AsString() ;
112
113 int pos = sfilename.Find('.', true) ;
114 if ( pos != wxNOT_FOUND )
115 {
116 sfilename = sfilename.Left(pos+1)+extension ;
117 cfString.Assign( sfilename , wxFONTENCODING_DEFAULT ) ;
118 NavDialogSetSaveFileName( ioParams->context , cfString ) ;
119 }
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 && filter[0] )
133 {
134 wxString filter2(filter) ;
135 int filterIndex = 0;
136 bool isName = true ;
137 wxString current ;
138 for( unsigned int i = 0; i < filter2.Len() ; i++ )
139 {
140 if( filter2.GetChar(i) == wxT('|') )
141 {
142 if( isName ) {
143 myData->name.Add( current ) ;
144 }
145 else {
146 myData->extensions.Add( current.MakeUpper() ) ;
147 ++filterIndex ;
148 }
149 isName = !isName ;
150 current = wxEmptyString ;
151 }
152 else
153 {
154 current += filter2.GetChar(i) ;
155 }
156 }
157 // we allow for compatibility reason to have a single filter expression (like *.*) without
158 // an explanatory text, in that case the first part is name and extension at the same time
159
160 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
161 if ( current.IsEmpty() )
162 myData->extensions.Add( myData->name[filterIndex] ) ;
163 else
164 myData->extensions.Add( current.MakeUpper() ) ;
165 if ( filterIndex == 0 || isName )
166 myData->name.Add( current.MakeUpper() ) ;
167
168 ++filterIndex ;
169
170 const size_t extCount = myData->extensions.GetCount();
171 for ( size_t i = 0 ; i < extCount; i++ )
172 {
173 wxUint32 fileType;
174 wxUint32 creator;
175 wxString extension = myData->extensions[i];
176
177 if (extension.GetChar(0) == '*')
178 extension = extension.Mid(1); // Remove leading *
179
180 if (extension.GetChar(0) == '.')
181 {
182 extension = extension.Mid(1); // Remove leading .
183 }
184
185 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
186 {
187 myData->filtermactypes.Add( (OSType)fileType );
188 }
189 else
190 {
191 myData->filtermactypes.Add( '****' ) ; // We'll fail safe if it's not recognized
192 }
193 }
194 }
195 }
196
197 static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data)
198 {
199 wxString file(filename) ;
200 file.MakeUpper() ;
201
202 if ( data->extensions.GetCount() > 0 )
203 {
204 //for ( int i = 0 ; i < data->numfilters ; ++i )
205 int i = data->currentfilter ;
206 if ( data->extensions[i].Right(2) == wxT(".*") )
207 return true ;
208
209 {
210 if ( type == (OSType)data->filtermactypes[i] )
211 return true ;
212
213 wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
214 while( tokenizer.HasMoreTokens() )
215 {
216 wxString extension = tokenizer.GetNextToken() ;
217 if ( extension.GetChar(0) == '*' )
218 extension = extension.Mid(1) ;
219
220 if ( file.Len() >= extension.Len() && extension == file.Right(extension.Len() ) )
221 return true ;
222 }
223 }
224 return false ;
225 }
226 return true ;
227 }
228
229 #if !TARGET_API_MAC_OSX
230 static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
231 {
232 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
233 // return true if this item is invisible or a file
234
235 Boolean visibleFlag;
236 Boolean folderFlag;
237
238 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
239 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
240
241 // because the semantics of the filter proc are "true means don't show
242 // it" we need to invert the result that we return
243
244 if ( !visibleFlag )
245 return true ;
246
247 if ( !folderFlag )
248 {
249 wxString file = wxMacMakeStringFromPascal( myCInfoPBPtr->hFileInfo.ioNamePtr ) ;
250 return !CheckFile( file , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
251 }
252
253 return false ;
254 }
255 #endif
256
257 // end wxmac
258
259 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
260 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
261 long style, const wxPoint& pos)
262 :wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos)
263 {
264 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
265 }
266
267 pascal Boolean CrossPlatformFilterCallback (
268 AEDesc *theItem,
269 void *info,
270 void *callBackUD,
271 NavFilterModes filterMode
272 )
273 {
274 bool display = true;
275 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
276
277 if (filterMode == kNavFilteringBrowserList)
278 {
279 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
280 if ( !theInfo->isFolder )
281 {
282 if (theItem->descriptorType == typeFSS )
283 {
284 FSSpec spec;
285 memcpy( &spec , *theItem->dataHandle , sizeof(FSSpec) ) ;
286 wxString file = wxMacMakeStringFromPascal( spec.name ) ;
287 display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
288 }
289 else if ( theItem->descriptorType == typeFSRef )
290 {
291 FSRef fsref ;
292 memcpy( &fsref , *theItem->dataHandle , sizeof(FSRef) ) ;
293 wxString file = wxMacFSRefToPath( &fsref ) ;
294 display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
295 }
296 }
297 }
298
299 return display;
300 }
301
302 int wxFileDialog::ShowModal()
303 {
304 OSErr err;
305 NavDialogCreationOptions dialogCreateOptions;
306 // set default options
307 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
308
309 // this was always unset in the old code
310 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
311
312 wxMacCFStringHolder message(m_message, m_font.GetEncoding());
313 dialogCreateOptions.windowTitle = message;
314
315 wxMacCFStringHolder defaultFileName(m_fileName, m_font.GetEncoding());
316 dialogCreateOptions.saveFileName = defaultFileName;
317
318
319 NavDialogRef dialog;
320 NavObjectFilterUPP navFilterUPP = NULL;
321 CFArrayRef cfArray = NULL; // for popupExtension
322 OpenUserDataRec myData;
323 myData.defaultLocation = m_dir;
324
325 MakeUserDataRec(&myData , m_wildCard);
326 myData.currentfilter = m_filterIndex;
327 size_t numFilters = myData.extensions.GetCount();
328 if (numFilters)
329 {
330 CFMutableArrayRef popup = CFArrayCreateMutable( kCFAllocatorDefault ,
331 numFilters , &kCFTypeArrayCallBacks ) ;
332 dialogCreateOptions.popupExtension = popup ;
333 myData.menuitems = dialogCreateOptions.popupExtension ;
334 for ( size_t i = 0 ; i < numFilters ; ++i )
335 {
336 CFArrayAppendValue( popup , (CFStringRef) wxMacCFStringHolder( myData.name[i] , m_font.GetEncoding() ) ) ;
337 }
338 }
339
340 if (m_dialogStyle & wxSAVE)
341 {
342 myData.saveMode = true;
343
344 if (!numFilters)
345 {
346 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
347 }
348 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
349 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
350
351 // The extension is important
352 if (numFilters < 2)
353 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
354
355 #if TARGET_API_MAC_OSX
356 if (!(m_dialogStyle & wxOVERWRITE_PROMPT))
357 {
358 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
359 }
360 #endif
361 err = ::NavCreatePutFileDialog(&dialogCreateOptions,
362 // Suppresses the 'Default' (top) menu item
363 kNavGenericSignature, kNavGenericSignature,
364 sStandardNavEventFilter,
365 &myData, // for defaultLocation
366 &dialog);
367 }
368 else
369 {
370
371 //let people select bundles/programs in dialogs
372 dialogCreateOptions.optionFlags |= kNavSupportPackages;
373
374 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
375 err = ::NavCreateGetFileDialog(&dialogCreateOptions,
376 NULL, // NavTypeListHandle
377 sStandardNavEventFilter,
378 NULL, // NavPreviewUPP
379 navFilterUPP,
380 (void *) &myData, // inClientData
381 &dialog);
382 }
383
384 if (err == noErr)
385 err = ::NavDialogRun(dialog);
386
387 // clean up filter related data, etc.
388 if (navFilterUPP)
389 ::DisposeNavObjectFilterUPP(navFilterUPP);
390 if (cfArray)
391 ::CFRelease(cfArray);
392
393 if (err != noErr)
394 return wxID_CANCEL;
395
396 NavReplyRecord navReply;
397 err = ::NavDialogGetReply(dialog, &navReply);
398 if (err == noErr && navReply.validRecord)
399 {
400 AEKeyword theKeyword;
401 DescType actualType;
402 Size actualSize;
403 FSRef theFSRef;
404 wxString thePath ;
405
406 m_filterIndex = myData.currentfilter ;
407
408 long count;
409 ::AECountItems(&navReply.selection , &count);
410 for (long i = 1; i <= count; ++i)
411 {
412 err = ::AEGetNthPtr(&(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
413 &theFSRef, sizeof(theFSRef), &actualSize);
414 if (err != noErr)
415 break;
416
417 if (m_dialogStyle & wxSAVE)
418 thePath = wxMacFSRefToPath( &theFSRef , navReply.saveFileName ) ;
419 else
420 thePath = wxMacFSRefToPath( &theFSRef ) ;
421
422 if (!thePath)
423 {
424 ::NavDisposeReply(&navReply);
425 return wxID_CANCEL;
426 }
427 m_path = thePath;
428 m_paths.Add(m_path);
429 m_fileName = wxFileNameFromPath(m_path);
430 m_fileNames.Add(m_fileName);
431 }
432 // set these to the first hit
433 m_path = m_paths[0];
434 m_fileName = wxFileNameFromPath(m_path);
435 m_dir = wxPathOnly(m_path);
436 }
437 ::NavDisposeReply(&navReply);
438
439 return (err == noErr) ? wxID_OK : wxID_CANCEL;
440 }
441