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