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