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