]> git.saurik.com Git - wxWidgets.git/blame - src/mac/filedlg.cpp
made wxFFile a bit more safe: don't crash when Tell() and Length() are called on...
[wxWidgets.git] / src / mac / filedlg.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: filedlg.cpp
685a634c 3// Purpose: wxFileDialog
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
e9576ca5
SC
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "filedlg.h"
14#endif
15
16#include "wx/defs.h"
5fde6fcc 17#include "wx/app.h"
e9576ca5
SC
18#include "wx/utils.h"
19#include "wx/dialog.h"
20#include "wx/filedlg.h"
21#include "wx/intl.h"
fe35d097 22#include "wx/tokenzr.h"
5974c3cf 23#include "wx/filename.h"
e9576ca5 24
f11bdd03 25#ifndef __DARWIN__
03e11df5
GD
26 #include "PLStringFuncs.h"
27#endif
5b781a67 28
2f1ae414 29#if !USE_SHARED_LIBRARY
f74172ab 30IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
2f1ae414 31#endif
e9576ca5 32
519cb848
SC
33// begin wxmac
34
76a5e5d2
SC
35#include "wx/mac/private.h"
36
bb378910 37#include <Navigation.h>
5b781a67 38
2d4e4f80
GD
39#ifdef __DARWIN__
40# include "MoreFilesX.h"
41#else
42# include "MoreFiles.h"
43# include "MoreFilesExtras.h"
44#endif
685a634c 45
5b781a67
SC
46extern bool gUseNavServices ;
47
4d4d8bbf
SC
48// the data we need to pass to our standard file hook routine
49// includes a pointer to the dialog, a pointer to the standard
50// file reply record (so we can inspect the current selection)
51// and a copy of the "previous" file spec of the reply record
52// so we can see if the selection has changed
53
4d4d8bbf 54struct OpenUserDataRec {
a4f5b9b9
GD
55 int currentfilter ;
56 bool saveMode ;
2b5f62a0
VZ
57 wxArrayString name ;
58 wxArrayString extensions ;
e40298d5 59 wxArrayLong filtermactypes ;
685a634c 60 wxString defaultLocation;
f65d4b83 61#if TARGET_CARBON
a4f5b9b9 62 CFArrayRef menuitems ;
f65d4b83 63#else
2b5f62a0 64 NavMenuItemSpecArrayHandle menuitems ;
f65d4b83 65#endif
4d4d8bbf 66};
2b5f62a0 67
4d4d8bbf 68typedef struct OpenUserDataRec
a4f5b9b9 69OpenUserDataRec, *OpenUserDataRecPtr;
4d4d8bbf 70
e40298d5
JS
71static pascal void NavEventProc(
72 NavEventCallbackMessage inSelector,
73 NavCBRecPtr ioParams,
74 NavCallBackUserData ioUserData);
5b781a67
SC
75
76#if TARGET_CARBON
e40298d5 77 static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc);
5b781a67 78#else
e40298d5 79 static NavEventUPP sStandardNavEventFilter = NewNavEventProc(NavEventProc);
5b781a67
SC
80#endif
81
82static pascal void
83NavEventProc(
e40298d5
JS
84 NavEventCallbackMessage inSelector,
85 NavCBRecPtr ioParams,
86 NavCallBackUserData ioUserData )
5b781a67 87{
e40298d5 88 OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ;
a4f5b9b9 89 if (inSelector == kNavCBEvent) {
f65d4b83 90#if TARGET_CARBON
685a634c 91#else
a4f5b9b9 92 wxTheApp->MacHandleOneEvent(ioParams->eventData.eventDataParms.event);
ac9b5f98 93#endif
685a634c 94 }
e40298d5
JS
95 else if ( inSelector == kNavCBStart )
96 {
f65d4b83 97#if TARGET_CARBON
a4f5b9b9
GD
98 if (data && !(data->defaultLocation).IsEmpty())
99 {
100 // Set default location for the modern Navigation APIs
101 // Apple Technical Q&A 1151
102 FSSpec theFSSpec;
103 wxMacFilename2FSSpec(data->defaultLocation, &theFSSpec);
104 AEDesc theLocation = {typeNull, NULL};
105 if (noErr == ::AECreateDesc(typeFSS, &theFSSpec, sizeof(FSSpec), &theLocation))
106 ::NavCustomControl(ioParams->context, kNavCtlSetLocation, (void *) &theLocation);
107 }
108#else
e40298d5
JS
109 if ( data->menuitems )
110 NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &(*data->menuitems)[data->currentfilter]);
f65d4b83 111#endif
e40298d5
JS
112 }
113 else if ( inSelector == kNavCBPopupMenuSelect )
114 {
115 NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
f65d4b83
SC
116#if TARGET_CARBON
117#else
e40298d5 118 if ( menu->menuCreator == 'WXNG' )
f65d4b83 119#endif
e40298d5
JS
120 {
121 data->currentfilter = menu->menuType ;
122 if ( data->saveMode )
123 {
124 int i = menu->menuType ;
125 wxString extension = data->extensions[i].AfterLast('.') ;
126 extension.MakeLower() ;
f65d4b83 127 wxString sfilename ;
685a634c 128
f65d4b83 129#if TARGET_CARBON
4891a3d5 130 wxMacCFStringHolder cfString( NavDialogGetSaveFileName( ioParams->context ) , false );
f65d4b83
SC
131 sfilename = cfString.AsString() ;
132#else
e40298d5
JS
133 Str255 filename ;
134 // get the current filename
135 NavCustomControl(ioParams->context, kNavCtlGetEditFileName, &filename);
f65d4b83
SC
136 sfilename = wxMacMakeStringFromPascal( filename ) ;
137#endif
138
685a634c 139 int pos = sfilename.Find('.', true) ;
e40298d5
JS
140 if ( pos != wxNOT_FOUND )
141 {
142 sfilename = sfilename.Left(pos+1)+extension ;
f65d4b83
SC
143#if TARGET_CARBON
144 cfString = sfilename ;
145 NavDialogSetSaveFileName( ioParams->context , cfString ) ;
146#else
427ff662 147 wxMacStringToPascal( sfilename , filename ) ;
e40298d5 148 NavCustomControl(ioParams->context, kNavCtlSetEditFileName, &filename);
f65d4b83 149#endif
e40298d5
JS
150 }
151 }
f65d4b83 152 }
e40298d5 153 }
5b781a67
SC
154}
155
2f1ae414 156
e40298d5 157void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
4d4d8bbf 158{
e40298d5
JS
159 myData->menuitems = NULL ;
160 myData->currentfilter = 0 ;
685a634c
DS
161 myData->saveMode = false ;
162
e40298d5 163 if ( filter && filter[0] )
4d4d8bbf 164 {
e40298d5
JS
165 wxString filter2(filter) ;
166 int filterIndex = 0;
167 bool isName = true ;
168 wxString current ;
169 for( unsigned int i = 0; i < filter2.Len() ; i++ )
170 {
171 if( filter2.GetChar(i) == wxT('|') )
172 {
173 if( isName ) {
174 myData->name.Add( current ) ;
175 }
176 else {
177 myData->extensions.Add( current.MakeUpper() ) ;
178 ++filterIndex ;
179 }
180 isName = !isName ;
427ff662 181 current = wxEmptyString ;
e40298d5
JS
182 }
183 else
184 {
185 current += filter2.GetChar(i) ;
186 }
4d4d8bbf 187 }
e40298d5
JS
188 // we allow for compatibility reason to have a single filter expression (like *.*) without
189 // an explanatory text, in that case the first part is name and extension at the same time
685a634c 190
427ff662 191 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
e40298d5
JS
192 if ( current.IsEmpty() )
193 myData->extensions.Add( myData->name[filterIndex] ) ;
194 else
195 myData->extensions.Add( current.MakeUpper() ) ;
196 if ( filterIndex == 0 || isName )
197 myData->name.Add( current.MakeUpper() ) ;
685a634c 198
e40298d5 199 ++filterIndex ;
685a634c 200
2b5f62a0 201 const size_t extCount = myData->extensions.GetCount();
e40298d5
JS
202 for ( size_t i = 0 ; i < extCount; i++ )
203 {
5974c3cf
SC
204 wxUint32 fileType;
205 wxUint32 creator;
206 wxString extension = myData->extensions[i];
207
208 if (extension.GetChar(0) == '*')
209 extension = extension.Mid(1); // Remove leading *
210
211 if (extension.GetChar(0) == '.')
e40298d5 212 {
5974c3cf 213 extension = extension.Mid(1); // Remove leading .
e40298d5 214 }
5974c3cf
SC
215
216 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
e40298d5 217 {
5974c3cf 218 myData->filtermactypes.Add( (OSType)fileType );
e40298d5 219 }
5974c3cf
SC
220 else
221 {
222 myData->filtermactypes.Add( '****' ) ; // We'll fail safe if it's not recognized
223 }
e40298d5
JS
224 }
225 }
4d4d8bbf 226}
bb378910 227
f65d4b83 228static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data)
4d4d8bbf 229{
a4f5b9b9 230 wxString file(filename) ;
9f92f6fb 231 file.MakeUpper() ;
685a634c 232
2b5f62a0 233 if ( data->extensions.GetCount() > 0 )
da2b4b7a 234 {
e40298d5
JS
235 //for ( int i = 0 ; i < data->numfilters ; ++i )
236 int i = data->currentfilter ;
427ff662 237 if ( data->extensions[i].Right(2) == wxT(".*") )
e40298d5 238 return true ;
685a634c 239
e40298d5
JS
240 {
241 if ( type == (OSType)data->filtermactypes[i] )
242 return true ;
685a634c 243
427ff662 244 wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
e40298d5
JS
245 while( tokenizer.HasMoreTokens() )
246 {
247 wxString extension = tokenizer.GetNextToken() ;
248 if ( extension.GetChar(0) == '*' )
249 extension = extension.Mid(1) ;
685a634c 250
e40298d5
JS
251 if ( file.Len() >= extension.Len() && extension == file.Right(extension.Len() ) )
252 return true ;
253 }
254 }
255 return false ;
da2b4b7a
GD
256 }
257 return true ;
4d4d8bbf
SC
258}
259
bb378910 260#ifndef __DARWIN__
5fde6fcc 261static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
a4f5b9b9 262{
e40298d5
JS
263 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
264 // return true if this item is invisible or a file
265
266 Boolean visibleFlag;
267 Boolean folderFlag;
685a634c 268
e40298d5
JS
269 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
270 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
685a634c 271
e40298d5
JS
272 // because the semantics of the filter proc are "true means don't show
273 // it" we need to invert the result that we return
685a634c 274
e40298d5
JS
275 if ( !visibleFlag )
276 return true ;
685a634c 277
e40298d5
JS
278 if ( !folderFlag )
279 {
a4f5b9b9 280 wxString file = wxMacMakeStringFromPascal( myCInfoPBPtr->hFileInfo.ioNamePtr ) ;
f65d4b83 281 return !CheckFile( file , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
a4f5b9b9 282 }
685a634c 283
e40298d5 284 return false ;
519cb848 285}
bb378910 286#endif
519cb848
SC
287
288// end wxmac
289
e9576ca5
SC
290wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
291 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
292 long style, const wxPoint& pos)
f74172ab 293 :wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos)
e9576ca5 294{
427ff662 295 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
e9576ca5
SC
296}
297
f11bdd03 298pascal Boolean CrossPlatformFilterCallback (
685a634c
DS
299 AEDesc *theItem,
300 void *info,
301 void *callBackUD,
4d4d8bbf
SC
302 NavFilterModes filterMode
303)
304{
2d4e4f80
GD
305 bool display = true;
306 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
685a634c 307
2d4e4f80
GD
308 if (filterMode == kNavFilteringBrowserList)
309 {
310 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
f65d4b83 311 if ( !theInfo->isFolder )
2d4e4f80 312 {
f65d4b83
SC
313 if (theItem->descriptorType == typeFSS )
314 {
315 FSSpec spec;
316 memcpy( &spec , *theItem->dataHandle , sizeof(FSSpec) ) ;
a4f5b9b9 317 wxString file = wxMacMakeStringFromPascal( spec.name ) ;
f65d4b83 318 display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
685a634c 319 }
613b6bb9 320 #if TARGET_CARBON
f65d4b83
SC
321 else if ( theItem->descriptorType == typeFSRef )
322 {
323 FSRef fsref ;
324 memcpy( &fsref , *theItem->dataHandle , sizeof(FSRef) ) ;
325 wxString file ;
326 const short maxpath = 1024 ;
327 FSRefMakePath( &fsref , (UInt8*) file.GetWriteBuf(maxpath+1),maxpath) ;
328 file.UngetWriteBuf() ;
329 display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
330 }
613b6bb9 331#endif
2d4e4f80
GD
332 }
333 }
685a634c 334
2d4e4f80 335 return display;
4d4d8bbf
SC
336}
337
e9576ca5
SC
338int wxFileDialog::ShowModal()
339{
f65d4b83 340#if TARGET_CARBON
a4f5b9b9
GD
341 OSErr err;
342 NavDialogCreationOptions dialogCreateOptions;
343 // set default options
344 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
685a634c 345
a4f5b9b9
GD
346 // this was always unset in the old code
347 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
685a634c 348
a4f5b9b9 349#if wxUSE_UNICODE
685a634c
DS
350 // tried using wxMacCFStringHolder in the code below, but it seems
351 // the CFStrings were being released before the save dialog was called,
a4f5b9b9 352 // causing a crash - open dialog works fine with or without wxMacCFStringHolder
878973f1
GD
353 CFStringRef titleRef = ::CFStringCreateWithCharacters( kCFAllocatorDefault,
354 (const unsigned short*)m_message.wc_str(),
355 m_message.Len() );
f65d4b83 356#else
685a634c 357 CFStringRef titleRef = ::CFStringCreateWithCString(NULL,
a4f5b9b9
GD
358 m_message.c_str(),
359 CFStringGetSystemEncoding());
f65d4b83 360#endif
a4f5b9b9
GD
361 dialogCreateOptions.windowTitle = titleRef;
362#if wxUSE_UNICODE
878973f1
GD
363 CFStringRef defaultFileNameRef = ::CFStringCreateWithCharacters( kCFAllocatorDefault,
364 (const unsigned short*)m_fileName.wc_str(),
365 m_fileName.Len() );
a4f5b9b9 366#else
685a634c
DS
367 CFStringRef defaultFileNameRef = ::CFStringCreateWithCString(NULL,
368 m_fileName.c_str(),
a4f5b9b9
GD
369 CFStringGetSystemEncoding());
370#endif
371 dialogCreateOptions.saveFileName = defaultFileNameRef;
372 NavDialogRef dialog;
373 NavObjectFilterUPP navFilterUPP = NULL;
685a634c 374 CFArrayRef cfArray = NULL; // for popupExtension
a4f5b9b9
GD
375 OpenUserDataRec myData;
376 myData.defaultLocation = m_dir;
377
378 if (m_dialogStyle & wxSAVE)
379 {
685a634c
DS
380 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
381 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
382 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
383
384 // The extension is important
385 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
386
a4f5b9b9 387 err = ::NavCreatePutFileDialog(&dialogCreateOptions,
685a634c
DS
388 'TEXT',
389 'TEXT',
390 sStandardNavEventFilter,
391 &myData, // for defaultLocation
392 &dialog);
a4f5b9b9
GD
393 }
394 else
395 {
396 MakeUserDataRec(&myData , m_wildCard);
685a634c
DS
397 size_t numfilters = myData.extensions.GetCount();
398 if (numfilters > 0)
a4f5b9b9 399 {
685a634c
DS
400 CFMutableArrayRef popup = CFArrayCreateMutable( kCFAllocatorDefault ,
401 numfilters , &kCFTypeArrayCallBacks ) ;
402 dialogCreateOptions.popupExtension = popup ;
403 myData.menuitems = dialogCreateOptions.popupExtension ;
404 for ( size_t i = 0 ; i < numfilters ; ++i )
405 {
406 CFArrayAppendValue( popup , (CFStringRef) wxMacCFStringHolder( myData.name[i] ) ) ;
407 }
408 }
409
a4f5b9b9
GD
410 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
411 err = ::NavCreateGetFileDialog(&dialogCreateOptions,
412 NULL, // NavTypeListHandle
413 sStandardNavEventFilter,
414 NULL, // NavPreviewUPP
415 navFilterUPP,
416 (void *) &myData, // inClientData
417 &dialog);
418 }
419
420 if (err == noErr)
421 err = ::NavDialogRun(dialog);
685a634c 422
a4f5b9b9
GD
423 // clean up filter related data, etc.
424 if (navFilterUPP)
425 ::DisposeNavObjectFilterUPP(navFilterUPP);
426 if (cfArray)
427 {
428 CFIndex n = ::CFArrayGetCount(cfArray);
429 for (CFIndex i = 0; i < n; i++)
430 {
431 CFStringRef str = (CFStringRef) ::CFArrayGetValueAtIndex(cfArray, i);
432 if (str)
433 ::CFRelease(str);
434 }
435 ::CFRelease(cfArray);
436 }
437 if (titleRef)
438 ::CFRelease(titleRef);
439 if (defaultFileNameRef)
440 ::CFRelease(defaultFileNameRef);
685a634c 441 if (err != noErr)
a4f5b9b9
GD
442 return wxID_CANCEL;
443
444 NavReplyRecord navReply;
445 err = ::NavDialogGetReply(dialog, &navReply);
685a634c 446 if (err == noErr && navReply.validRecord)
a4f5b9b9
GD
447 {
448 AEKeyword theKeyword;
449 DescType actualType;
450 Size actualSize;
451 FSRef theFSRef;
878973f1 452 wxString thePath ;
a4f5b9b9
GD
453 long count;
454 ::AECountItems(&navReply.selection , &count);
455 for (long i = 1; i <= count; ++i)
456 {
457 err = ::AEGetNthPtr(&(navReply.selection), 1, typeFSRef, &theKeyword, &actualType,
458 &theFSRef, sizeof(theFSRef), &actualSize);
685a634c 459 if (err != noErr)
a4f5b9b9
GD
460 break;
461
a449f840 462 CFURLRef fullURLRef;
a4f5b9b9
GD
463 if (m_dialogStyle & wxSAVE)
464 {
a4f5b9b9 465 CFURLRef parentURLRef = ::CFURLCreateFromFSRef(NULL, &theFSRef);
685a634c 466
a4f5b9b9
GD
467 if (parentURLRef)
468 {
a449f840 469 fullURLRef =
a4f5b9b9 470 ::CFURLCreateCopyAppendingPathComponent(NULL,
685a634c
DS
471 parentURLRef,
472 navReply.saveFileName,
a4f5b9b9
GD
473 false);
474 ::CFRelease(parentURLRef);
a4f5b9b9
GD
475 }
476 }
685a634c 477 else
a4f5b9b9 478 {
a449f840
DS
479 fullURLRef = ::CFURLCreateFromFSRef(NULL, &theFSRef);
480 }
481#ifdef __UNIX__
482 CFURLPathStyle pathstyle = kCFURLPOSIXPathStyle;
483#else
484 CFURLPathStyle pathstyle = kCFURLHFSPathStyle;
485#endif
486 CFStringRef cfString = CFURLCopyFileSystemPath(fullURLRef, pathstyle);
487 thePath = wxMacCFStringHolder(cfString).AsString();
488 if (!thePath)
489 {
490 ::NavDisposeReply(&navReply);
491 return wxID_CANCEL;
a4f5b9b9
GD
492 }
493 m_path = thePath;
494 m_paths.Add(m_path);
495 m_fileName = wxFileNameFromPath(m_path);
496 m_fileNames.Add(m_fileName);
497 }
498 // set these to the first hit
499 m_path = m_paths[0];
500 m_fileName = wxFileNameFromPath(m_path);
501 m_dir = wxPathOnly(m_path);
502 }
503 ::NavDisposeReply(&navReply);
685a634c 504
a4f5b9b9
GD
505 return (err == noErr) ? wxID_OK : wxID_CANCEL;
506#else // TARGET_CARBON
507
508 NavDialogOptions mNavOptions;
e40298d5
JS
509 NavObjectFilterUPP mNavFilterUPP = NULL;
510 NavPreviewUPP mNavPreviewUPP = NULL ;
511 NavReplyRecord mNavReply;
512 AEDesc mDefaultLocation ;
513 bool mSelectDefault = false ;
f65d4b83 514 OSStatus err = noErr ;
e40298d5 515 // setup dialog
685a634c 516
e40298d5
JS
517 mNavFilterUPP = nil;
518 mNavPreviewUPP = nil;
519 mSelectDefault = false;
f65d4b83
SC
520 mDefaultLocation.descriptorType = typeNull;
521 mDefaultLocation.dataHandle = nil;
522
f65d4b83
SC
523 NavGetDefaultDialogOptions(&mNavOptions);
524 wxMacStringToPascal( m_message , (StringPtr)mNavOptions.message ) ;
525 wxMacStringToPascal( m_fileName , (StringPtr)mNavOptions.savedFileName ) ;
526
2d4e4f80
GD
527 // Set default location, the location
528 // that's displayed when the dialog
529 // first appears
685a634c 530
2d4e4f80
GD
531 FSSpec location ;
532 wxMacFilename2FSSpec( m_dir , &location ) ;
685a634c 533
2d4e4f80 534 err = ::AECreateDesc(typeFSS, &location, sizeof(FSSpec), &mDefaultLocation );
5b781a67 535
685a634c
DS
536 if ( mDefaultLocation.dataHandle )
537 {
538 if (mSelectDefault)
539 {
2d4e4f80
GD
540 mNavOptions.dialogOptionFlags |= kNavSelectDefaultLocation;
541 } else {
542 mNavOptions.dialogOptionFlags &= ~kNavSelectDefaultLocation;
543 }
544 }
5b781a67 545
f65d4b83
SC
546 memset( &mNavReply , 0 , sizeof( mNavReply ) ) ;
547 mNavReply.validRecord = false;
548 mNavReply.replacing = false;
549 mNavReply.isStationery = false;
550 mNavReply.translationNeeded = false;
551 mNavReply.selection.descriptorType = typeNull;
552 mNavReply.selection.dataHandle = nil;
553 mNavReply.keyScript = smSystemScript;
554 mNavReply.fileTranslation = nil;
555 mNavReply.version = kNavReplyRecordVersion ;
685a634c 556
7ed2b47b 557 // zero all data
685a634c 558
7ed2b47b
SC
559 m_path = wxEmptyString ;
560 m_fileName = wxEmptyString ;
561 m_paths.Empty();
562 m_fileNames.Empty();
563
e40298d5 564 OpenUserDataRec myData;
2d4e4f80
GD
565 MakeUserDataRec( &myData , m_wildCard ) ;
566 myData.currentfilter = m_filterIndex ;
567 if ( myData.extensions.GetCount() > 0 )
568 {
569 mNavOptions.popupExtension = (NavMenuItemSpecArrayHandle) NewHandle( sizeof( NavMenuItemSpec ) * myData.extensions.GetCount() ) ;
570 myData.menuitems = mNavOptions.popupExtension ;
685a634c 571 for ( size_t i = 0 ; i < myData.extensions.GetCount() ; ++i )
2d4e4f80
GD
572 {
573 (*mNavOptions.popupExtension)[i].version = kNavMenuItemSpecVersion ;
574 (*mNavOptions.popupExtension)[i].menuCreator = 'WXNG' ;
f65d4b83 575 // TODO : according to the new docs -1 to 10 are reserved for the OS
2d4e4f80 576 (*mNavOptions.popupExtension)[i].menuType = i ;
427ff662 577 wxMacStringToPascal( myData.name[i] , (StringPtr)(*mNavOptions.popupExtension)[i].menuItemName ) ;
2d4e4f80
GD
578 }
579 }
580 if ( m_dialogStyle & wxSAVE )
581 {
582 myData.saveMode = true ;
2b5f62a0 583
2d4e4f80
GD
584 mNavOptions.dialogOptionFlags |= kNavDontAutoTranslate ;
585 mNavOptions.dialogOptionFlags |= kNavDontAddTranslateItems ;
685a634c 586
2d4e4f80
GD
587 err = ::NavPutFile(
588 &mDefaultLocation,
589 &mNavReply,
590 &mNavOptions,
591 sStandardNavEventFilter ,
592 NULL,
593 kNavGenericSignature,
e40298d5 594 &myData); // User Data
2d4e4f80
GD
595 m_filterIndex = myData.currentfilter ;
596 }
597 else
598 {
599 myData.saveMode = false ;
4d4d8bbf 600
2d4e4f80
GD
601 mNavFilterUPP = NewNavObjectFilterUPP( CrossPlatformFilterCallback ) ;
602 if ( m_dialogStyle & wxMULTIPLE )
603 mNavOptions.dialogOptionFlags |= kNavAllowMultipleFiles ;
604 else
605 mNavOptions.dialogOptionFlags &= ~kNavAllowMultipleFiles ;
685a634c 606
2d4e4f80
GD
607 err = ::NavGetFile(
608 &mDefaultLocation,
609 &mNavReply,
610 &mNavOptions,
611 sStandardNavEventFilter ,
612 mNavPreviewUPP,
613 mNavFilterUPP,
614 NULL ,
615 &myData);
616 m_filterIndex = myData.currentfilter ;
617 }
685a634c 618
2d4e4f80
GD
619 DisposeNavObjectFilterUPP(mNavFilterUPP);
620 if ( mDefaultLocation.dataHandle != nil )
621 {
622 ::AEDisposeDesc(&mDefaultLocation);
623 }
685a634c 624
2d4e4f80 625 if ( (err != noErr) && (err != userCanceledErr) ) {
2d4e4f80
GD
626 return wxID_CANCEL ;
627 }
5b781a67 628
685a634c
DS
629 if (mNavReply.validRecord)
630 {
2d4e4f80
GD
631 FSSpec outFileSpec ;
632 AEDesc specDesc ;
633 AEKeyword keyWord ;
685a634c 634
2d4e4f80
GD
635 long count ;
636 ::AECountItems( &mNavReply.selection , &count ) ;
637 for ( long i = 1 ; i <= count ; ++i )
638 {
639 OSErr err = ::AEGetNthDesc( &mNavReply.selection , i , typeFSS, &keyWord , &specDesc);
685a634c
DS
640 if ( err != noErr )
641 {
427ff662 642 m_path = wxT("") ;
2d4e4f80 643 return wxID_CANCEL ;
685a634c 644 }
2d4e4f80
GD
645 outFileSpec = **(FSSpec**) specDesc.dataHandle;
646 if (specDesc.dataHandle != nil) {
647 ::AEDisposeDesc(&specDesc);
648 }
649 m_path = wxMacFSSpec2MacFilename( &outFileSpec ) ;
a4f5b9b9 650
2d4e4f80 651 m_paths.Add( m_path ) ;
24fe8dc7 652 m_fileName = wxFileNameFromPath(m_path);
2d4e4f80
GD
653 m_fileNames.Add(m_fileName);
654 }
655 // set these to the first hit
656 m_path = m_paths[ 0 ] ;
657 m_fileName = wxFileNameFromPath(m_path);
658 m_dir = wxPathOnly(m_path);
659 NavDisposeReply( &mNavReply ) ;
660 return wxID_OK ;
661 }
662 return wxID_CANCEL;
a4f5b9b9 663#endif // TARGET_CARBON
5b781a67 664}
e9576ca5 665