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