]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/filedlg.cpp
SetValue is not adding a line if values does not exist
[wxWidgets.git] / src / mac / carbon / 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
65571936 9// Licence: wxWindows licence
e9576ca5
SC
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 }
75338075
DS
108
109 NavMenuItemSpec menuItem;
110 menuItem.version = kNavMenuItemSpecVersion;
111 menuItem.menuCreator = 'WXNG';
112 menuItem.menuType = data->currentfilter;
113 wxMacStringToPascal( data->name[data->currentfilter] , (StringPtr)(menuItem.menuItemName) ) ;
114 ::NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &menuItem);
115
a4f5b9b9 116#else
e40298d5
JS
117 if ( data->menuitems )
118 NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &(*data->menuitems)[data->currentfilter]);
f65d4b83 119#endif
e40298d5
JS
120 }
121 else if ( inSelector == kNavCBPopupMenuSelect )
122 {
123 NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
f65d4b83 124#if TARGET_CARBON
75338075
DS
125 const size_t numFilters = data->extensions.GetCount();
126
127 if ( menu->menuType < numFilters )
f65d4b83 128#else
e40298d5 129 if ( menu->menuCreator == 'WXNG' )
f65d4b83 130#endif
e40298d5
JS
131 {
132 data->currentfilter = menu->menuType ;
133 if ( data->saveMode )
134 {
135 int i = menu->menuType ;
136 wxString extension = data->extensions[i].AfterLast('.') ;
137 extension.MakeLower() ;
f65d4b83 138 wxString sfilename ;
685a634c 139
f65d4b83 140#if TARGET_CARBON
4891a3d5 141 wxMacCFStringHolder cfString( NavDialogGetSaveFileName( ioParams->context ) , false );
f65d4b83
SC
142 sfilename = cfString.AsString() ;
143#else
e40298d5
JS
144 Str255 filename ;
145 // get the current filename
146 NavCustomControl(ioParams->context, kNavCtlGetEditFileName, &filename);
f65d4b83
SC
147 sfilename = wxMacMakeStringFromPascal( filename ) ;
148#endif
149
685a634c 150 int pos = sfilename.Find('.', true) ;
e40298d5
JS
151 if ( pos != wxNOT_FOUND )
152 {
153 sfilename = sfilename.Left(pos+1)+extension ;
f65d4b83 154#if TARGET_CARBON
a9412f8f 155 cfString.Assign( sfilename , wxFONTENCODING_DEFAULT ) ;
f65d4b83
SC
156 NavDialogSetSaveFileName( ioParams->context , cfString ) ;
157#else
427ff662 158 wxMacStringToPascal( sfilename , filename ) ;
e40298d5 159 NavCustomControl(ioParams->context, kNavCtlSetEditFileName, &filename);
f65d4b83 160#endif
e40298d5
JS
161 }
162 }
f65d4b83 163 }
e40298d5 164 }
5b781a67
SC
165}
166
2f1ae414 167
e40298d5 168void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
4d4d8bbf 169{
e40298d5
JS
170 myData->menuitems = NULL ;
171 myData->currentfilter = 0 ;
685a634c
DS
172 myData->saveMode = false ;
173
e40298d5 174 if ( filter && filter[0] )
4d4d8bbf 175 {
e40298d5
JS
176 wxString filter2(filter) ;
177 int filterIndex = 0;
178 bool isName = true ;
179 wxString current ;
180 for( unsigned int i = 0; i < filter2.Len() ; i++ )
181 {
182 if( filter2.GetChar(i) == wxT('|') )
183 {
184 if( isName ) {
185 myData->name.Add( current ) ;
186 }
187 else {
188 myData->extensions.Add( current.MakeUpper() ) ;
189 ++filterIndex ;
190 }
191 isName = !isName ;
427ff662 192 current = wxEmptyString ;
e40298d5
JS
193 }
194 else
195 {
196 current += filter2.GetChar(i) ;
197 }
4d4d8bbf 198 }
e40298d5
JS
199 // we allow for compatibility reason to have a single filter expression (like *.*) without
200 // an explanatory text, in that case the first part is name and extension at the same time
685a634c 201
427ff662 202 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
e40298d5
JS
203 if ( current.IsEmpty() )
204 myData->extensions.Add( myData->name[filterIndex] ) ;
205 else
206 myData->extensions.Add( current.MakeUpper() ) ;
207 if ( filterIndex == 0 || isName )
208 myData->name.Add( current.MakeUpper() ) ;
685a634c 209
e40298d5 210 ++filterIndex ;
685a634c 211
2b5f62a0 212 const size_t extCount = myData->extensions.GetCount();
e40298d5
JS
213 for ( size_t i = 0 ; i < extCount; i++ )
214 {
5974c3cf
SC
215 wxUint32 fileType;
216 wxUint32 creator;
217 wxString extension = myData->extensions[i];
218
219 if (extension.GetChar(0) == '*')
220 extension = extension.Mid(1); // Remove leading *
221
222 if (extension.GetChar(0) == '.')
e40298d5 223 {
5974c3cf 224 extension = extension.Mid(1); // Remove leading .
e40298d5 225 }
5974c3cf
SC
226
227 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
e40298d5 228 {
5974c3cf 229 myData->filtermactypes.Add( (OSType)fileType );
e40298d5 230 }
5974c3cf
SC
231 else
232 {
233 myData->filtermactypes.Add( '****' ) ; // We'll fail safe if it's not recognized
234 }
e40298d5
JS
235 }
236 }
4d4d8bbf 237}
bb378910 238
f65d4b83 239static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data)
4d4d8bbf 240{
a4f5b9b9 241 wxString file(filename) ;
9f92f6fb 242 file.MakeUpper() ;
685a634c 243
2b5f62a0 244 if ( data->extensions.GetCount() > 0 )
da2b4b7a 245 {
e40298d5
JS
246 //for ( int i = 0 ; i < data->numfilters ; ++i )
247 int i = data->currentfilter ;
427ff662 248 if ( data->extensions[i].Right(2) == wxT(".*") )
e40298d5 249 return true ;
685a634c 250
e40298d5
JS
251 {
252 if ( type == (OSType)data->filtermactypes[i] )
253 return true ;
685a634c 254
427ff662 255 wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
e40298d5
JS
256 while( tokenizer.HasMoreTokens() )
257 {
258 wxString extension = tokenizer.GetNextToken() ;
259 if ( extension.GetChar(0) == '*' )
260 extension = extension.Mid(1) ;
685a634c 261
e40298d5
JS
262 if ( file.Len() >= extension.Len() && extension == file.Right(extension.Len() ) )
263 return true ;
264 }
265 }
266 return false ;
da2b4b7a
GD
267 }
268 return true ;
4d4d8bbf
SC
269}
270
bb378910 271#ifndef __DARWIN__
5fde6fcc 272static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
a4f5b9b9 273{
e40298d5
JS
274 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
275 // return true if this item is invisible or a file
276
277 Boolean visibleFlag;
278 Boolean folderFlag;
685a634c 279
e40298d5
JS
280 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
281 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
685a634c 282
e40298d5
JS
283 // because the semantics of the filter proc are "true means don't show
284 // it" we need to invert the result that we return
685a634c 285
e40298d5
JS
286 if ( !visibleFlag )
287 return true ;
685a634c 288
e40298d5
JS
289 if ( !folderFlag )
290 {
a4f5b9b9 291 wxString file = wxMacMakeStringFromPascal( myCInfoPBPtr->hFileInfo.ioNamePtr ) ;
f65d4b83 292 return !CheckFile( file , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
a4f5b9b9 293 }
685a634c 294
e40298d5 295 return false ;
519cb848 296}
bb378910 297#endif
519cb848
SC
298
299// end wxmac
300
e9576ca5
SC
301wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
302 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
303 long style, const wxPoint& pos)
f74172ab 304 :wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos)
e9576ca5 305{
427ff662 306 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
e9576ca5
SC
307}
308
f11bdd03 309pascal Boolean CrossPlatformFilterCallback (
685a634c
DS
310 AEDesc *theItem,
311 void *info,
312 void *callBackUD,
4d4d8bbf
SC
313 NavFilterModes filterMode
314)
315{
2d4e4f80
GD
316 bool display = true;
317 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
685a634c 318
2d4e4f80
GD
319 if (filterMode == kNavFilteringBrowserList)
320 {
321 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
f65d4b83 322 if ( !theInfo->isFolder )
2d4e4f80 323 {
f65d4b83
SC
324 if (theItem->descriptorType == typeFSS )
325 {
326 FSSpec spec;
327 memcpy( &spec , *theItem->dataHandle , sizeof(FSSpec) ) ;
a4f5b9b9 328 wxString file = wxMacMakeStringFromPascal( spec.name ) ;
f65d4b83 329 display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
685a634c 330 }
613b6bb9 331 #if TARGET_CARBON
f65d4b83
SC
332 else if ( theItem->descriptorType == typeFSRef )
333 {
334 FSRef fsref ;
335 memcpy( &fsref , *theItem->dataHandle , sizeof(FSRef) ) ;
305a0f60
SC
336
337
338
339 CFURLRef fullURLRef;
340 fullURLRef = ::CFURLCreateFromFSRef(NULL, &fsref);
341#ifdef __UNIX__
342 CFURLPathStyle pathstyle = kCFURLPOSIXPathStyle;
343#else
344 CFURLPathStyle pathstyle = kCFURLHFSPathStyle;
345#endif
346 CFStringRef cfString = CFURLCopyFileSystemPath(fullURLRef, pathstyle);
e71800ba 347 ::CFRelease( fullURLRef ) ;
305a0f60
SC
348 wxString file = wxMacCFStringHolder(cfString).AsString(wxFont::GetDefaultEncoding());
349
f65d4b83
SC
350 display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
351 }
613b6bb9 352#endif
2d4e4f80
GD
353 }
354 }
685a634c 355
2d4e4f80 356 return display;
4d4d8bbf
SC
357}
358
e9576ca5
SC
359int wxFileDialog::ShowModal()
360{
f65d4b83 361#if TARGET_CARBON
a4f5b9b9
GD
362 OSErr err;
363 NavDialogCreationOptions dialogCreateOptions;
364 // set default options
365 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
685a634c 366
a4f5b9b9
GD
367 // this was always unset in the old code
368 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
685a634c 369
e71800ba
SC
370 wxMacCFStringHolder message(m_message, m_font.GetEncoding());
371 dialogCreateOptions.windowTitle = message;
372
373 wxMacCFStringHolder defaultFileName(m_fileName, m_font.GetEncoding());
374 dialogCreateOptions.saveFileName = defaultFileName;
375
376
a4f5b9b9
GD
377 NavDialogRef dialog;
378 NavObjectFilterUPP navFilterUPP = NULL;
685a634c 379 CFArrayRef cfArray = NULL; // for popupExtension
a4f5b9b9
GD
380 OpenUserDataRec myData;
381 myData.defaultLocation = m_dir;
382
75338075
DS
383 MakeUserDataRec(&myData , m_wildCard);
384 myData.currentfilter = m_filterIndex;
385 size_t numFilters = myData.extensions.GetCount();
386 if (numFilters)
387 {
388 CFMutableArrayRef popup = CFArrayCreateMutable( kCFAllocatorDefault ,
389 numFilters , &kCFTypeArrayCallBacks ) ;
390 dialogCreateOptions.popupExtension = popup ;
391 myData.menuitems = dialogCreateOptions.popupExtension ;
392 for ( size_t i = 0 ; i < numFilters ; ++i )
393 {
394 CFArrayAppendValue( popup , (CFStringRef) wxMacCFStringHolder( myData.name[i] , m_font.GetEncoding() ) ) ;
395 }
396 }
397
a4f5b9b9
GD
398 if (m_dialogStyle & wxSAVE)
399 {
75338075
DS
400 myData.saveMode = true;
401
402 if (!numFilters)
403 {
404 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
405 }
685a634c
DS
406 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
407 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
408
409 // The extension is important
a485ee6f
JS
410 if (numFilters < 2)
411 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
685a634c 412
78eeb095
SC
413#if TARGET_API_MAC_OSX
414 if (!(m_dialogStyle & wxOVERWRITE_PROMPT))
415 {
416 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
417 }
418#endif
a4f5b9b9 419 err = ::NavCreatePutFileDialog(&dialogCreateOptions,
75338075
DS
420 // Suppresses the 'Default' (top) menu item
421 kNavGenericSignature, kNavGenericSignature,
685a634c
DS
422 sStandardNavEventFilter,
423 &myData, // for defaultLocation
424 &dialog);
a4f5b9b9
GD
425 }
426 else
427 {
c11d9cb8
KO
428
429 //let people select bundles/programs in dialogs
430 dialogCreateOptions.optionFlags |= kNavSupportPackages;
431
a4f5b9b9
GD
432 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
433 err = ::NavCreateGetFileDialog(&dialogCreateOptions,
434 NULL, // NavTypeListHandle
435 sStandardNavEventFilter,
436 NULL, // NavPreviewUPP
437 navFilterUPP,
438 (void *) &myData, // inClientData
439 &dialog);
440 }
441
442 if (err == noErr)
443 err = ::NavDialogRun(dialog);
685a634c 444
a4f5b9b9
GD
445 // clean up filter related data, etc.
446 if (navFilterUPP)
447 ::DisposeNavObjectFilterUPP(navFilterUPP);
448 if (cfArray)
a4f5b9b9 449 ::CFRelease(cfArray);
e71800ba 450
685a634c 451 if (err != noErr)
a4f5b9b9
GD
452 return wxID_CANCEL;
453
454 NavReplyRecord navReply;
455 err = ::NavDialogGetReply(dialog, &navReply);
685a634c 456 if (err == noErr && navReply.validRecord)
a4f5b9b9
GD
457 {
458 AEKeyword theKeyword;
459 DescType actualType;
460 Size actualSize;
461 FSRef theFSRef;
878973f1 462 wxString thePath ;
75338075
DS
463
464 m_filterIndex = myData.currentfilter ;
465
a4f5b9b9
GD
466 long count;
467 ::AECountItems(&navReply.selection , &count);
468 for (long i = 1; i <= count; ++i)
469 {
d1b3039b 470 err = ::AEGetNthPtr(&(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
a4f5b9b9 471 &theFSRef, sizeof(theFSRef), &actualSize);
685a634c 472 if (err != noErr)
a4f5b9b9
GD
473 break;
474
0c3d85e6 475 CFURLRef fullURLRef = 0 ;
a4f5b9b9
GD
476 if (m_dialogStyle & wxSAVE)
477 {
a4f5b9b9 478 CFURLRef parentURLRef = ::CFURLCreateFromFSRef(NULL, &theFSRef);
685a634c 479
a4f5b9b9
GD
480 if (parentURLRef)
481 {
a449f840 482 fullURLRef =
a4f5b9b9 483 ::CFURLCreateCopyAppendingPathComponent(NULL,
685a634c
DS
484 parentURLRef,
485 navReply.saveFileName,
a4f5b9b9
GD
486 false);
487 ::CFRelease(parentURLRef);
a4f5b9b9
GD
488 }
489 }
685a634c 490 else
a4f5b9b9 491 {
a449f840
DS
492 fullURLRef = ::CFURLCreateFromFSRef(NULL, &theFSRef);
493 }
494#ifdef __UNIX__
495 CFURLPathStyle pathstyle = kCFURLPOSIXPathStyle;
496#else
497 CFURLPathStyle pathstyle = kCFURLHFSPathStyle;
498#endif
499 CFStringRef cfString = CFURLCopyFileSystemPath(fullURLRef, pathstyle);
a9412f8f 500 thePath = wxMacCFStringHolder(cfString).AsString(m_font.GetEncoding());
a449f840
DS
501 if (!thePath)
502 {
503 ::NavDisposeReply(&navReply);
504 return wxID_CANCEL;
a4f5b9b9
GD
505 }
506 m_path = thePath;
507 m_paths.Add(m_path);
508 m_fileName = wxFileNameFromPath(m_path);
509 m_fileNames.Add(m_fileName);
510 }
511 // set these to the first hit
512 m_path = m_paths[0];
513 m_fileName = wxFileNameFromPath(m_path);
514 m_dir = wxPathOnly(m_path);
515 }
516 ::NavDisposeReply(&navReply);
685a634c 517
a4f5b9b9
GD
518 return (err == noErr) ? wxID_OK : wxID_CANCEL;
519#else // TARGET_CARBON
520
521 NavDialogOptions mNavOptions;
e40298d5
JS
522 NavObjectFilterUPP mNavFilterUPP = NULL;
523 NavPreviewUPP mNavPreviewUPP = NULL ;
524 NavReplyRecord mNavReply;
525 AEDesc mDefaultLocation ;
526 bool mSelectDefault = false ;
f65d4b83 527 OSStatus err = noErr ;
e40298d5 528 // setup dialog
685a634c 529
e40298d5
JS
530 mNavFilterUPP = nil;
531 mNavPreviewUPP = nil;
532 mSelectDefault = false;
f65d4b83
SC
533 mDefaultLocation.descriptorType = typeNull;
534 mDefaultLocation.dataHandle = nil;
535
f65d4b83
SC
536 NavGetDefaultDialogOptions(&mNavOptions);
537 wxMacStringToPascal( m_message , (StringPtr)mNavOptions.message ) ;
538 wxMacStringToPascal( m_fileName , (StringPtr)mNavOptions.savedFileName ) ;
539
2d4e4f80
GD
540 // Set default location, the location
541 // that's displayed when the dialog
542 // first appears
685a634c 543
2d4e4f80
GD
544 FSSpec location ;
545 wxMacFilename2FSSpec( m_dir , &location ) ;
685a634c 546
2d4e4f80 547 err = ::AECreateDesc(typeFSS, &location, sizeof(FSSpec), &mDefaultLocation );
5b781a67 548
685a634c
DS
549 if ( mDefaultLocation.dataHandle )
550 {
551 if (mSelectDefault)
552 {
2d4e4f80
GD
553 mNavOptions.dialogOptionFlags |= kNavSelectDefaultLocation;
554 } else {
555 mNavOptions.dialogOptionFlags &= ~kNavSelectDefaultLocation;
556 }
557 }
5b781a67 558
f65d4b83
SC
559 memset( &mNavReply , 0 , sizeof( mNavReply ) ) ;
560 mNavReply.validRecord = false;
561 mNavReply.replacing = false;
562 mNavReply.isStationery = false;
563 mNavReply.translationNeeded = false;
564 mNavReply.selection.descriptorType = typeNull;
565 mNavReply.selection.dataHandle = nil;
566 mNavReply.keyScript = smSystemScript;
567 mNavReply.fileTranslation = nil;
568 mNavReply.version = kNavReplyRecordVersion ;
685a634c 569
7ed2b47b 570 // zero all data
685a634c 571
7ed2b47b
SC
572 m_path = wxEmptyString ;
573 m_fileName = wxEmptyString ;
574 m_paths.Empty();
575 m_fileNames.Empty();
576
e40298d5 577 OpenUserDataRec myData;
2d4e4f80
GD
578 MakeUserDataRec( &myData , m_wildCard ) ;
579 myData.currentfilter = m_filterIndex ;
580 if ( myData.extensions.GetCount() > 0 )
581 {
582 mNavOptions.popupExtension = (NavMenuItemSpecArrayHandle) NewHandle( sizeof( NavMenuItemSpec ) * myData.extensions.GetCount() ) ;
583 myData.menuitems = mNavOptions.popupExtension ;
685a634c 584 for ( size_t i = 0 ; i < myData.extensions.GetCount() ; ++i )
2d4e4f80
GD
585 {
586 (*mNavOptions.popupExtension)[i].version = kNavMenuItemSpecVersion ;
587 (*mNavOptions.popupExtension)[i].menuCreator = 'WXNG' ;
f65d4b83 588 // TODO : according to the new docs -1 to 10 are reserved for the OS
2d4e4f80 589 (*mNavOptions.popupExtension)[i].menuType = i ;
427ff662 590 wxMacStringToPascal( myData.name[i] , (StringPtr)(*mNavOptions.popupExtension)[i].menuItemName ) ;
2d4e4f80
GD
591 }
592 }
593 if ( m_dialogStyle & wxSAVE )
594 {
595 myData.saveMode = true ;
2b5f62a0 596
2d4e4f80
GD
597 mNavOptions.dialogOptionFlags |= kNavDontAutoTranslate ;
598 mNavOptions.dialogOptionFlags |= kNavDontAddTranslateItems ;
685a634c 599
2d4e4f80
GD
600 err = ::NavPutFile(
601 &mDefaultLocation,
602 &mNavReply,
603 &mNavOptions,
604 sStandardNavEventFilter ,
605 NULL,
606 kNavGenericSignature,
e40298d5 607 &myData); // User Data
2d4e4f80
GD
608 m_filterIndex = myData.currentfilter ;
609 }
610 else
611 {
612 myData.saveMode = false ;
4d4d8bbf 613
2d4e4f80
GD
614 mNavFilterUPP = NewNavObjectFilterUPP( CrossPlatformFilterCallback ) ;
615 if ( m_dialogStyle & wxMULTIPLE )
616 mNavOptions.dialogOptionFlags |= kNavAllowMultipleFiles ;
617 else
618 mNavOptions.dialogOptionFlags &= ~kNavAllowMultipleFiles ;
685a634c 619
2d4e4f80
GD
620 err = ::NavGetFile(
621 &mDefaultLocation,
622 &mNavReply,
623 &mNavOptions,
624 sStandardNavEventFilter ,
625 mNavPreviewUPP,
626 mNavFilterUPP,
627 NULL ,
628 &myData);
629 m_filterIndex = myData.currentfilter ;
630 }
685a634c 631
2d4e4f80
GD
632 DisposeNavObjectFilterUPP(mNavFilterUPP);
633 if ( mDefaultLocation.dataHandle != nil )
634 {
635 ::AEDisposeDesc(&mDefaultLocation);
636 }
685a634c 637
2d4e4f80 638 if ( (err != noErr) && (err != userCanceledErr) ) {
2d4e4f80
GD
639 return wxID_CANCEL ;
640 }
5b781a67 641
685a634c
DS
642 if (mNavReply.validRecord)
643 {
2d4e4f80
GD
644 FSSpec outFileSpec ;
645 AEDesc specDesc ;
646 AEKeyword keyWord ;
685a634c 647
2d4e4f80
GD
648 long count ;
649 ::AECountItems( &mNavReply.selection , &count ) ;
650 for ( long i = 1 ; i <= count ; ++i )
651 {
652 OSErr err = ::AEGetNthDesc( &mNavReply.selection , i , typeFSS, &keyWord , &specDesc);
685a634c
DS
653 if ( err != noErr )
654 {
427ff662 655 m_path = wxT("") ;
2d4e4f80 656 return wxID_CANCEL ;
685a634c 657 }
2d4e4f80
GD
658 outFileSpec = **(FSSpec**) specDesc.dataHandle;
659 if (specDesc.dataHandle != nil) {
660 ::AEDisposeDesc(&specDesc);
661 }
662 m_path = wxMacFSSpec2MacFilename( &outFileSpec ) ;
a4f5b9b9 663
2d4e4f80 664 m_paths.Add( m_path ) ;
24fe8dc7 665 m_fileName = wxFileNameFromPath(m_path);
2d4e4f80
GD
666 m_fileNames.Add(m_fileName);
667 }
668 // set these to the first hit
669 m_path = m_paths[ 0 ] ;
670 m_fileName = wxFileNameFromPath(m_path);
671 m_dir = wxPathOnly(m_path);
672 NavDisposeReply( &mNavReply ) ;
673 return wxID_OK ;
674 }
675 return wxID_CANCEL;
a4f5b9b9 676#endif // TARGET_CARBON
5b781a67 677}
e9576ca5 678