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