]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/filedlg.cpp
Add a couple missing methods
[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 #include "wx/filedlg.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/intl.h"
18 #include "wx/app.h"
19 #include "wx/utils.h"
20 #include "wx/dialog.h"
21 #endif
22
23 #include "wx/tokenzr.h"
24 #include "wx/filename.h"
25
26 #include "wx/mac/private.h"
27
28 #ifndef __DARWIN__
29 #include <Navigation.h>
30 #include "PLStringFuncs.h"
31 #endif
32
33 #include "MoreFilesX.h"
34
35 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
36
37 extern bool gUseNavServices;
38
39 // the data we need to pass to our standard file hook routine
40 // includes a pointer to the dialog, a pointer to the standard
41 // file reply record (so we can inspect the current selection)
42 // and a copy of the "previous" file spec of the reply record
43 // so we can see if the selection has changed
44
45 struct OpenUserDataRec
46 {
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 NavEventProc(
67 NavEventCallbackMessage inSelector,
68 NavCBRecPtr ioParams,
69 NavCallBackUserData ioUserData )
70 {
71 OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ;
72 if (inSelector == kNavCBEvent)
73 {
74 }
75 else if ( inSelector == kNavCBStart )
76 {
77 if (data && !(data->defaultLocation).empty())
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 void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
126 {
127 myData->menuitems = NULL ;
128 myData->currentfilter = 0 ;
129 myData->saveMode = false ;
130
131 if ( filter && filter[0] )
132 {
133 wxString filter2(filter) ;
134 int filterIndex = 0;
135 bool isName = true ;
136 wxString current ;
137
138 for ( unsigned int i = 0; i < filter2.length() ; i++ )
139 {
140 if ( filter2.GetChar(i) == wxT('|') )
141 {
142 if ( isName )
143 {
144 myData->name.Add( current ) ;
145 }
146 else
147 {
148 myData->extensions.Add( current.MakeUpper() ) ;
149 ++filterIndex ;
150 }
151
152 isName = !isName ;
153 current = wxEmptyString ;
154 }
155 else
156 {
157 current += filter2.GetChar(i) ;
158 }
159 }
160 // we allow for compatibility reason to have a single filter expression (like *.*) without
161 // an explanatory text, in that case the first part is name and extension at the same time
162
163 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
164 if ( current.empty() )
165 myData->extensions.Add( myData->name[filterIndex] ) ;
166 else
167 myData->extensions.Add( current.MakeUpper() ) ;
168 if ( filterIndex == 0 || isName )
169 myData->name.Add( current.MakeUpper() ) ;
170
171 ++filterIndex ;
172
173 const size_t extCount = myData->extensions.GetCount();
174 for ( size_t i = 0 ; i < extCount; i++ )
175 {
176 wxUint32 fileType, creator;
177 wxString extension = myData->extensions[i];
178
179 // Remove leading '*'
180 if (extension.length() && (extension.GetChar(0) == '*'))
181 extension = extension.Mid( 1 );
182
183 // Remove leading '.'
184 if (extension.length() && (extension.GetChar(0) == '.'))
185 extension = extension.Mid( 1 );
186
187 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
188 myData->filtermactypes.Add( (OSType)fileType );
189 else
190 myData->filtermactypes.Add( '****' ); // We'll fail safe if it's not recognized
191 }
192 }
193 }
194
195 static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data)
196 {
197 wxString file(filename) ;
198 file.MakeUpper() ;
199
200 if ( data->extensions.GetCount() > 0 )
201 {
202 //for ( int i = 0 ; i < data->numfilters ; ++i )
203 int i = data->currentfilter ;
204 if ( data->extensions[i].Right(2) == wxT(".*") )
205 return true ;
206
207 {
208 if ( type == (OSType)data->filtermactypes[i] )
209 return true ;
210
211 wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
212 while ( tokenizer.HasMoreTokens() )
213 {
214 wxString extension = tokenizer.GetNextToken() ;
215 if ( extension.GetChar(0) == '*' )
216 extension = extension.Mid(1) ;
217
218 if ( file.length() >= extension.length() && extension == file.Right(extension.length() ) )
219 return true ;
220 }
221 }
222
223 return false ;
224 }
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(
260 wxWindow *parent, const wxString& message,
261 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
262 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
263 : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
264 {
265 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
266 }
267
268 pascal Boolean CrossPlatformFilterCallback(
269 AEDesc *theItem,
270 void *info,
271 void *callBackUD,
272 NavFilterModes filterMode )
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
307 // set default options
308 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
309
310 // this was always unset in the old code
311 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
312
313 wxMacCFStringHolder message(m_message, m_font.GetEncoding());
314 dialogCreateOptions.windowTitle = message;
315
316 wxMacCFStringHolder defaultFileName(m_fileName, m_font.GetEncoding());
317 dialogCreateOptions.saveFileName = defaultFileName;
318
319
320 NavDialogRef dialog;
321 NavObjectFilterUPP navFilterUPP = NULL;
322 CFArrayRef cfArray = NULL; // for popupExtension
323 OpenUserDataRec myData;
324 myData.defaultLocation = m_dir;
325
326 MakeUserDataRec(&myData , m_wildCard);
327 myData.currentfilter = m_filterIndex;
328 size_t numFilters = myData.extensions.GetCount();
329 if (numFilters)
330 {
331 CFMutableArrayRef popup = CFArrayCreateMutable( kCFAllocatorDefault ,
332 numFilters , &kCFTypeArrayCallBacks ) ;
333 dialogCreateOptions.popupExtension = popup ;
334 myData.menuitems = dialogCreateOptions.popupExtension ;
335 for ( size_t i = 0 ; i < numFilters ; ++i )
336 {
337 CFArrayAppendValue( popup , (CFStringRef) wxMacCFStringHolder( myData.name[i] , m_font.GetEncoding() ) ) ;
338 }
339 }
340
341 if (HasFlag(wxFD_SAVE))
342 {
343 myData.saveMode = true;
344
345 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
346 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
347 if (!numFilters)
348 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
349
350 // The extension is important
351 if (numFilters < 2)
352 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
353
354 #if TARGET_API_MAC_OSX
355 if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT))
356 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
357 #endif
358
359 err = ::NavCreatePutFileDialog(
360 &dialogCreateOptions,
361 kNavGenericSignature, // Suppresses the 'Default' (top) menu item
362 kNavGenericSignature,
363 sStandardNavEventFilter,
364 &myData, // for defaultLocation
365 &dialog );
366 }
367 else
368 {
369 // let the user select bundles/programs in dialogs
370 dialogCreateOptions.optionFlags |= kNavSupportPackages;
371
372 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
373 err = ::NavCreateGetFileDialog(
374 &dialogCreateOptions,
375 NULL, // NavTypeListHandle
376 sStandardNavEventFilter,
377 NULL, // NavPreviewUPP
378 navFilterUPP,
379 (void *) &myData, // inClientData
380 &dialog );
381 }
382
383 if (err == noErr)
384 err = ::NavDialogRun(dialog);
385
386 // clean up filter related data, etc.
387 if (navFilterUPP)
388 ::DisposeNavObjectFilterUPP(navFilterUPP);
389 if (cfArray)
390 ::CFRelease(cfArray);
391
392 if (err != noErr)
393 return wxID_CANCEL;
394
395 NavReplyRecord navReply;
396 err = ::NavDialogGetReply(dialog, &navReply);
397 if (err == noErr && navReply.validRecord)
398 {
399 AEKeyword theKeyword;
400 DescType actualType;
401 Size actualSize;
402 FSRef theFSRef;
403 wxString thePath ;
404 long count;
405
406 m_filterIndex = myData.currentfilter;
407 ::AECountItems( &navReply.selection, &count );
408 for (long i = 1; i <= count; ++i)
409 {
410 err = ::AEGetNthPtr(
411 &(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
412 &theFSRef, sizeof(theFSRef), &actualSize );
413 if (err != noErr)
414 break;
415
416 if (HasFlag(wxFD_SAVE))
417 thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName );
418 else
419 thePath = wxMacFSRefToPath( &theFSRef );
420
421 if (!thePath)
422 {
423 ::NavDisposeReply(&navReply);
424 return wxID_CANCEL;
425 }
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
433 // set these to the first hit
434 m_path = m_paths[0];
435 m_fileName = wxFileNameFromPath(m_path);
436 m_dir = wxPathOnly(m_path);
437 }
438
439 ::NavDisposeReply(&navReply);
440
441 return (err == noErr) ? wxID_OK : wxID_CANCEL;
442 }