]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/filedlg.cpp
use correct scale when drawing
[wxWidgets.git] / src / osx / carbon / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/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/wxprec.h"
13
14 #if wxUSE_FILEDLG
15
16 #include "wx/filedlg.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/intl.h"
20 #include "wx/app.h"
21 #include "wx/utils.h"
22 #include "wx/dialog.h"
23 #endif
24
25 #include "wx/tokenzr.h"
26 #include "wx/filename.h"
27
28 #include "wx/osx/private.h"
29 #include "wx/modalhook.h"
30
31 #ifndef __DARWIN__
32 #include <Navigation.h>
33 #include "PLStringFuncs.h"
34 #endif
35
36 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
37
38 // the data we need to pass to our standard file hook routine
39 // includes a pointer to the dialog, a pointer to the standard
40 // file reply record (so we can inspect the current selection)
41 // and a copy of the "previous" file spec of the reply record
42 // so we can see if the selection has changed
43
44 class OpenUserDataRec
45 {
46 public:
47 OpenUserDataRec( wxFileDialog* dialog );
48
49 bool FilterCallback( AEDesc *theItem, void *info, NavFilterModes filterMode );
50 void EventProc( NavEventCallbackMessage inSelector, NavCBRecPtr ioParams );
51
52 int GetCurrentFilter() const {return m_currentfilter;}
53 CFArrayRef GetMenuItems() const { return m_menuitems;}
54
55
56 private:
57 void EventProcCBEvent( NavCBRecPtr ioParams );
58 void EventProcCBEventMouseDown( NavCBRecPtr ioParams);
59 void EventProcCBStart( NavCBRecPtr ioParams );
60 void EventProcCBPopupMenuSelect( NavCBRecPtr ioParams );
61 void EventProcCBCustomize( NavCBRecPtr ioParams );
62 void EventProcCBAdjustRect( NavCBRecPtr ioParams );
63 bool CheckFile( const wxString &filename , OSType type);
64 void MakeUserDataRec( const wxString& filter);
65
66 wxFileDialog* m_dialog;
67 int m_currentfilter;
68 wxString m_defaultLocation;
69 wxArrayString m_extensions;
70 wxArrayLong m_filtermactypes;
71 CFMutableArrayRef m_menuitems;
72 wxArrayString m_name;
73 bool m_saveMode;
74 SInt16 m_lastRight;
75 SInt16 m_lastBottom;
76 bool m_controlAdded;
77 };
78
79 OpenUserDataRec::OpenUserDataRec( wxFileDialog* d)
80 {
81 m_dialog = d;
82 m_controlAdded = false;
83 m_saveMode = m_dialog->HasFdFlag(wxFD_SAVE);
84
85 m_defaultLocation = m_dialog->GetDirectory();
86 MakeUserDataRec(m_dialog->GetWildcard());
87 m_currentfilter = m_dialog->GetFilterIndex();
88
89 m_menuitems = NULL;
90
91 size_t numFilters = m_extensions.GetCount();
92 if (numFilters)
93 {
94 m_menuitems = CFArrayCreateMutable( kCFAllocatorDefault ,
95 numFilters , &kCFTypeArrayCallBacks ) ;
96 for ( size_t i = 0 ; i < numFilters ; ++i )
97 {
98 CFArrayAppendValue( m_menuitems , (CFStringRef) wxCFStringRef( m_name[i] ) ) ;
99 }
100 }
101 m_lastRight = m_lastBottom = 0;
102 }
103
104 void OpenUserDataRec::EventProc(NavEventCallbackMessage inSelector,NavCBRecPtr ioParams)
105 {
106 switch (inSelector)
107 {
108 case kNavCBEvent:
109 EventProcCBEvent(ioParams);
110 break;
111 case kNavCBStart:
112 EventProcCBStart(ioParams);
113 break;
114 case kNavCBPopupMenuSelect:
115 EventProcCBPopupMenuSelect(ioParams);
116 break;
117 case kNavCBCustomize:
118 EventProcCBCustomize(ioParams);
119 break;
120 case kNavCBAdjustRect:
121 EventProcCBAdjustRect(ioParams);
122 break;
123 default:
124 break;
125 }
126 }
127
128 void OpenUserDataRec::EventProcCBEvent(NavCBRecPtr callBackParms)
129 {
130 switch (callBackParms->eventData.eventDataParms.event->what)
131 {
132 case mouseDown:
133 {
134 EventProcCBEventMouseDown(callBackParms);
135 break;
136 }
137 }
138 }
139
140 void OpenUserDataRec::EventProcCBEventMouseDown(NavCBRecPtr callBackParms)
141 {
142 EventRecord *evt = callBackParms->eventData.eventDataParms.event;
143 Point where = evt->where;
144 QDGlobalToLocalPoint(GetWindowPort(callBackParms->window), &where);
145
146 ControlRef whichControl = FindControlUnderMouse(where, callBackParms->window, NULL);
147 if (whichControl != NULL)
148 {
149 ControlKind theKind;
150 GetControlKind(whichControl, &theKind);
151
152 // Moving the focus if we clicked in an focusable control
153 if ((theKind.kind == kControlKindEditUnicodeText) ||
154 (theKind.kind == kControlKindEditText) ||
155 (theKind.kind == kControlKindDataBrowser) ||
156 (theKind.kind == kControlKindListBox))
157 {
158 ControlRef currentlyFocusedControl;
159 GetKeyboardFocus(callBackParms->window, &currentlyFocusedControl);
160 if (currentlyFocusedControl != whichControl)
161 SetKeyboardFocus(callBackParms->window, whichControl, kControlFocusNextPart);
162 }
163 HandleControlClick(whichControl, where, evt->modifiers, NULL);
164 }
165 }
166
167 void OpenUserDataRec::EventProcCBStart(NavCBRecPtr ioParams)
168 {
169 if (!m_defaultLocation.empty())
170 {
171 // Set default location for the modern Navigation APIs
172 // Apple Technical Q&A 1151
173 FSRef theFile;
174 wxMacPathToFSRef(m_defaultLocation, &theFile);
175 AEDesc theLocation = { typeNull, NULL };
176 if (noErr == ::AECreateDesc(typeFSRef, &theFile, sizeof(FSRef), &theLocation))
177 ::NavCustomControl(ioParams->context, kNavCtlSetLocation, (void *) &theLocation);
178 }
179
180 if( m_extensions.GetCount() > 0 )
181 {
182 NavMenuItemSpec menuItem;
183 memset( &menuItem, 0, sizeof(menuItem) );
184 menuItem.version = kNavMenuItemSpecVersion;
185 menuItem.menuType = m_currentfilter;
186 ::NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &menuItem);
187 }
188
189 if (m_dialog->GetExtraControl())
190 {
191 m_controlAdded = true;
192 ControlRef ref = m_dialog->GetExtraControl()->GetPeer()->GetControlRef();
193 NavCustomControl(ioParams->context, kNavCtlAddControl, ref);
194 }
195
196 }
197
198 void OpenUserDataRec::EventProcCBPopupMenuSelect(NavCBRecPtr ioParams)
199 {
200 NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
201 const size_t numFilters = m_extensions.GetCount();
202
203 if ( menu->menuType < numFilters )
204 {
205 m_currentfilter = menu->menuType ;
206 if ( m_saveMode )
207 {
208 int i = menu->menuType ;
209
210 // isolate the first extension string
211 wxString firstExtension = m_extensions[i].BeforeFirst('|').BeforeFirst(';');
212
213 wxString extension = firstExtension.AfterLast('.') ;
214 wxString sfilename ;
215
216 wxCFStringRef cfString( wxCFRetain( NavDialogGetSaveFileName( ioParams->context ) ) );
217 sfilename = cfString.AsString() ;
218
219 int pos = sfilename.Find('.', true) ;
220 if ( pos != wxNOT_FOUND && extension != wxT("*") )
221 {
222 sfilename = sfilename.Left(pos+1)+extension ;
223 cfString = wxCFStringRef( sfilename , wxFONTENCODING_DEFAULT ) ;
224 NavDialogSetSaveFileName( ioParams->context , cfString ) ;
225 }
226 }
227 }
228 }
229
230 void OpenUserDataRec::EventProcCBCustomize(NavCBRecPtr ioParams)
231 {
232 wxWindow* control = m_dialog->GetExtraControl();
233
234 if ( control )
235 {
236 SInt16 neededRight, neededBottom;
237
238 wxSize size = m_dialog->GetExtraControl()->GetSize();
239 neededRight = ioParams->customRect.left + size.x;
240 neededBottom = ioParams->customRect.top + size.y;
241
242 if (ioParams->customRect.right == 0 && ioParams->customRect.bottom == 0)
243 {
244 ioParams->customRect.right = neededRight;
245 ioParams->customRect.bottom = neededBottom;
246 }
247 else
248 {
249 if ( ioParams->customRect.right != m_lastRight )
250 {
251 if ( ioParams->customRect.right < neededRight )
252 ioParams->customRect.right = neededRight;
253 }
254 if ( ioParams->customRect.bottom != m_lastBottom )
255 {
256 if ( ioParams->customRect.bottom < neededBottom )
257 ioParams->customRect.bottom = neededBottom;
258 }
259 }
260 m_lastRight = ioParams->customRect.right;
261 m_lastBottom = ioParams->customRect.bottom;
262 }
263 }
264
265 void OpenUserDataRec::EventProcCBAdjustRect(NavCBRecPtr ioParams)
266 {
267 wxWindow* control = m_dialog->GetExtraControl();
268
269 if ( control && m_controlAdded)
270 {
271 control->SetSize(ioParams->customRect.left , ioParams->customRect.top,
272 ioParams->customRect.right - ioParams->customRect.left,
273 ioParams->customRect.bottom - ioParams->customRect.top);
274 }
275 }
276
277 void OpenUserDataRec::MakeUserDataRec( const wxString& filter )
278 {
279 if ( !filter.empty() )
280 {
281 wxString filter2(filter) ;
282 int filterIndex = 0;
283 bool isName = true ;
284 wxString current ;
285
286 for ( unsigned int i = 0; i < filter2.length() ; i++ )
287 {
288 if ( filter2.GetChar(i) == wxT('|') )
289 {
290 if ( isName )
291 {
292 m_name.Add( current ) ;
293 }
294 else
295 {
296 m_extensions.Add( current ) ;
297 ++filterIndex ;
298 }
299
300 isName = !isName ;
301 current = wxEmptyString ;
302 }
303 else
304 {
305 current += filter2.GetChar(i) ;
306 }
307 }
308 // we allow for compatibility reason to have a single filter expression (like *.*) without
309 // an explanatory text, in that case the first part is name and extension at the same time
310
311 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
312 if ( current.empty() )
313 m_extensions.Add( m_name[filterIndex] ) ;
314 else
315 m_extensions.Add( current ) ;
316 if ( filterIndex == 0 || isName )
317 m_name.Add( current ) ;
318
319 ++filterIndex ;
320
321 const size_t extCount = m_extensions.GetCount();
322 for ( size_t i = 0 ; i < extCount; i++ )
323 {
324 wxUint32 fileType, creator;
325 wxString extension = m_extensions[i];
326
327 // Remove leading '*'
328 if ( !extension.empty() && (extension.GetChar(0) == '*') )
329 extension = extension.Mid( 1 );
330
331 // Remove leading '.'
332 if ( !extension.empty() && (extension.GetChar(0) == '.') )
333 extension = extension.Mid( 1 );
334
335 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
336 m_filtermactypes.Add( (OSType)fileType );
337 else
338 m_filtermactypes.Add( '****' ); // We'll fail safe if it's not recognized
339 }
340 }
341 }
342
343 bool OpenUserDataRec::CheckFile( const wxString &filename , OSType type)
344 {
345 wxString file(filename) ;
346 file.MakeUpper() ;
347
348 if ( m_extensions.GetCount() > 0 )
349 {
350 //for ( int i = 0 ; i < data->numfilters ; ++i )
351 int i = m_currentfilter ;
352 if ( m_extensions[i].Right(2) == wxT(".*") )
353 return true ;
354
355 {
356 if ( type == (OSType)m_filtermactypes[i] )
357 return true ;
358
359 wxStringTokenizer tokenizer( m_extensions[i] , wxT(";") ) ;
360 while ( tokenizer.HasMoreTokens() )
361 {
362 wxString extension = tokenizer.GetNextToken() ;
363 if ( extension.GetChar(0) == '*' )
364 extension = extension.Mid(1) ;
365 extension.MakeUpper();
366
367 if ( file.length() >= extension.length() && extension == file.Right(extension.length() ) )
368 return true ;
369 }
370 }
371
372 return false ;
373 }
374
375 return true ;
376 }
377
378 bool OpenUserDataRec::FilterCallback(
379 AEDesc *theItem,
380 void *info,
381 NavFilterModes filterMode )
382 {
383 if (filterMode == kNavFilteringBrowserList)
384 {
385 // We allow navigation to all folders. For files, we check against the current
386 // filter string.
387 // However, packages should be dealt with like files and not like folders. So
388 // check if a folder is a package before deciding what to do.
389 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
390 FSRef fsref;
391
392 if ( theInfo->isFolder )
393 {
394 // check bundle bit (using Finder Services - used by OS9 on some bundles)
395 FSCatalogInfo catalogInfo;
396 if (FSGetCatalogInfo (&fsref, kFSCatInfoFinderInfo, &catalogInfo, NULL, NULL, NULL) != noErr)
397 return true;
398
399 // Check bundle item (using Launch Services - used by OS-X through info.plist or APP)
400 LSItemInfoRecord lsInfo;
401 if (LSCopyItemInfoForRef(&fsref, kLSRequestBasicFlagsOnly, &lsInfo ) != noErr)
402 return true;
403
404 // If it's not a bundle, then it's a normal folder and it passes our filter
405 FileInfo *fileInfo = (FileInfo *) catalogInfo.finderInfo;
406 if ( !(fileInfo->finderFlags & kHasBundle) &&
407 !(lsInfo.flags & (kLSItemInfoIsApplication | kLSItemInfoIsPackage)) )
408 return true;
409 }
410 else
411 {
412 AECoerceDesc (theItem, typeFSRef, theItem);
413 if ( AEGetDescData (theItem, &fsref, sizeof (FSRef)) == noErr)
414 {
415 wxString file = wxMacFSRefToPath( &fsref ) ;
416 return CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType ) ;
417 }
418 }
419 }
420
421 return true;
422 }
423
424 // end wxmac
425
426 pascal Boolean CrossPlatformFilterCallback(
427 AEDesc *theItem,
428 void *info,
429 void *callBackUD,
430 NavFilterModes filterMode );
431
432 pascal Boolean CrossPlatformFilterCallback(
433 AEDesc *theItem,
434 void *info,
435 void *callBackUD,
436 NavFilterModes filterMode )
437 {
438 OpenUserDataRec* data = (OpenUserDataRec*) callBackUD ;
439 return data->FilterCallback(theItem,info,filterMode);
440 }
441
442 static pascal void NavEventProc(
443 NavEventCallbackMessage inSelector,
444 NavCBRecPtr ioParams,
445 NavCallBackUserData ioUserData );
446
447 static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc);
448
449 static pascal void NavEventProc(
450 NavEventCallbackMessage inSelector,
451 NavCBRecPtr ioParams,
452 NavCallBackUserData ioUserData )
453 {
454 OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ;
455 data->EventProc(inSelector, ioParams);
456 }
457
458
459 wxFileDialog::wxFileDialog(
460 wxWindow *parent, const wxString& message,
461 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
462 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
463 : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
464 {
465 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
466 }
467
468 void wxFileDialog::SetupExtraControls(WXWindow nativeWindow)
469 {
470 wxTopLevelWindow::Create( GetParent(), nativeWindow );
471
472 if (HasExtraControlCreator())
473 {
474 CreateExtraControl();
475 }
476 }
477
478 int wxFileDialog::ShowModal()
479 {
480 WX_HOOK_MODAL_DIALOG();
481
482 m_paths.Empty();
483 m_fileNames.Empty();
484
485 OSErr err;
486 NavDialogCreationOptions dialogCreateOptions;
487
488 // set default options
489 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
490
491 // this was always unset in the old code
492 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
493
494 wxCFStringRef message(m_message, GetFont().GetEncoding());
495 dialogCreateOptions.windowTitle = message;
496
497 wxCFStringRef defaultFileName(m_fileName, GetFont().GetEncoding());
498 dialogCreateOptions.saveFileName = defaultFileName;
499
500 NavDialogRef dialog;
501 NavObjectFilterUPP navFilterUPP = NULL;
502 OpenUserDataRec myData( this );
503
504 dialogCreateOptions.popupExtension = myData.GetMenuItems();
505
506 if (HasFdFlag(wxFD_SAVE))
507 {
508 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
509 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
510 if (dialogCreateOptions.popupExtension == NULL)
511 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
512
513 // The extension is important
514 if ( dialogCreateOptions.popupExtension == NULL || CFArrayGetCount(dialogCreateOptions.popupExtension)<2)
515 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
516
517 if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT))
518 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
519
520 err = ::NavCreatePutFileDialog(
521 &dialogCreateOptions,
522 kNavGenericSignature, // Suppresses the 'Default' (top) menu item
523 kNavGenericSignature,
524 sStandardNavEventFilter,
525 &myData, // for defaultLocation
526 &dialog );
527 }
528 else
529 {
530 // let the user select bundles/programs in dialogs
531 dialogCreateOptions.optionFlags |= kNavSupportPackages;
532
533 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
534 err = ::NavCreateGetFileDialog(
535 &dialogCreateOptions,
536 NULL, // NavTypeListHandle
537 sStandardNavEventFilter,
538 NULL, // NavPreviewUPP
539 navFilterUPP,
540 (void *) &myData, // inClientData
541 &dialog );
542 }
543
544 SetupExtraControls(NavDialogGetWindow(dialog));
545
546 if (err == noErr)
547 {
548 wxDialog::OSXBeginModalDialog();
549 err = ::NavDialogRun(dialog);
550 wxDialog::OSXEndModalDialog();
551 }
552
553 // clean up filter related data, etc.
554 if (navFilterUPP)
555 ::DisposeNavObjectFilterUPP(navFilterUPP);
556
557 if (err != noErr)
558 {
559 ::NavDialogDispose(dialog);
560 return wxID_CANCEL;
561 }
562
563 NavReplyRecord navReply;
564 err = ::NavDialogGetReply(dialog, &navReply);
565 if (err == noErr && navReply.validRecord)
566 {
567 AEKeyword theKeyword;
568 DescType actualType;
569 Size actualSize;
570 FSRef theFSRef;
571 wxString thePath ;
572 long count;
573
574 m_filterIndex = myData.GetCurrentFilter();
575 ::AECountItems( &navReply.selection, &count );
576 for (long i = 1; i <= count; ++i)
577 {
578 err = ::AEGetNthPtr(
579 &(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
580 &theFSRef, sizeof(theFSRef), &actualSize );
581 if (err != noErr)
582 break;
583
584 if (HasFdFlag(wxFD_SAVE))
585 thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName );
586 else
587 thePath = wxMacFSRefToPath( &theFSRef );
588
589 if (!thePath)
590 {
591 ::NavDisposeReply(&navReply);
592 ::NavDialogDispose(dialog);
593 return wxID_CANCEL;
594 }
595
596 m_path = thePath;
597 m_paths.Add(m_path);
598 m_fileName = wxFileNameFromPath(m_path);
599 m_fileNames.Add(m_fileName);
600 }
601
602 // set these to the first hit
603 m_path = m_paths[0];
604 m_fileName = wxFileNameFromPath(m_path);
605 m_dir = wxPathOnly(m_path);
606 }
607
608 UnsubclassWin();
609 ::NavDisposeReply(&navReply);
610 ::NavDialogDispose(dialog);
611
612 return (err == noErr) ? wxID_OK : wxID_CANCEL;
613 }
614
615 bool wxFileDialog::SupportsExtraControl() const
616 {
617 return true;
618 }
619
620 #endif // wxUSE_FILEDLG
621