]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/filedlg.cpp
Finally fixed control scrolling
[wxWidgets.git] / src / mac / carbon / filedlg.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
88a7a4e1 2// Name: src/mac/carbon/filedlg.cpp
685a634c 3// Purpose: wxFileDialog
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3d1a4878
SC
12#include "wx/wxprec.h"
13
72c1ba98
VZ
14#if wxUSE_FILEDLG
15
88a7a4e1
WS
16#include "wx/filedlg.h"
17
18#ifndef WX_PRECOMP
19 #include "wx/intl.h"
670f9935 20 #include "wx/app.h"
de6185e2 21 #include "wx/utils.h"
fdf565fe 22 #include "wx/dialog.h"
88a7a4e1
WS
23#endif
24
fe35d097 25#include "wx/tokenzr.h"
5974c3cf 26#include "wx/filename.h"
e9576ca5 27
76a5e5d2
SC
28#include "wx/mac/private.h"
29
768c6e8b 30#ifndef __DARWIN__
670f9935
WS
31 #include <Navigation.h>
32 #include "PLStringFuncs.h"
768c6e8b 33#endif
5b781a67 34
f3078f07
DS
35IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
36
4d4d8bbf
SC
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
f3078f07
DS
43struct OpenUserDataRec
44{
a4f5b9b9
GD
45 int currentfilter ;
46 bool saveMode ;
2b5f62a0
VZ
47 wxArrayString name ;
48 wxArrayString extensions ;
e40298d5 49 wxArrayLong filtermactypes ;
685a634c 50 wxString defaultLocation;
a4f5b9b9 51 CFArrayRef menuitems ;
4d4d8bbf 52};
2b5f62a0 53
4d4d8bbf 54typedef struct OpenUserDataRec
a4f5b9b9 55OpenUserDataRec, *OpenUserDataRecPtr;
4d4d8bbf 56
f3078f07
DS
57static pascal void NavEventProc(
58 NavEventCallbackMessage inSelector,
59 NavCBRecPtr ioParams,
60 NavCallBackUserData ioUserData );
5b781a67 61
f3078f07 62static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc);
5b781a67 63
f3078f07
DS
64static pascal void NavEventProc(
65 NavEventCallbackMessage inSelector,
66 NavCBRecPtr ioParams,
67 NavCallBackUserData ioUserData )
5b781a67 68{
e40298d5 69 OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ;
f3078f07
DS
70 if (inSelector == kNavCBEvent)
71 {
685a634c 72 }
e40298d5
JS
73 else if ( inSelector == kNavCBStart )
74 {
de6185e2 75 if (data && !(data->defaultLocation).empty())
a4f5b9b9
GD
76 {
77 // Set default location for the modern Navigation APIs
78 // Apple Technical Q&A 1151
f387b80e
SC
79 FSRef theFile;
80 wxMacPathToFSRef(data->defaultLocation, &theFile);
f3078f07 81 AEDesc theLocation = { typeNull, NULL };
f387b80e 82 if (noErr == ::AECreateDesc(typeFSRef, &theFile, sizeof(FSRef), &theLocation))
a4f5b9b9
GD
83 ::NavCustomControl(ioParams->context, kNavCtlSetLocation, (void *) &theLocation);
84 }
75338075
DS
85
86 NavMenuItemSpec menuItem;
87 menuItem.version = kNavMenuItemSpecVersion;
88 menuItem.menuCreator = 'WXNG';
89 menuItem.menuType = data->currentfilter;
90 wxMacStringToPascal( data->name[data->currentfilter] , (StringPtr)(menuItem.menuItemName) ) ;
91 ::NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &menuItem);
e40298d5
JS
92 }
93 else if ( inSelector == kNavCBPopupMenuSelect )
94 {
95 NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
75338075
DS
96 const size_t numFilters = data->extensions.GetCount();
97
98 if ( menu->menuType < numFilters )
e40298d5
JS
99 {
100 data->currentfilter = menu->menuType ;
101 if ( data->saveMode )
102 {
103 int i = menu->menuType ;
f7aeba1c
VZ
104
105 // isolate the first extension string
106 wxString firstExtension = data->extensions[i].BeforeFirst('|').BeforeFirst(';');
107
108 wxString extension = firstExtension.AfterLast('.') ;
f65d4b83 109 wxString sfilename ;
685a634c 110
4891a3d5 111 wxMacCFStringHolder cfString( NavDialogGetSaveFileName( ioParams->context ) , false );
f65d4b83 112 sfilename = cfString.AsString() ;
f65d4b83 113
685a634c 114 int pos = sfilename.Find('.', true) ;
e40298d5
JS
115 if ( pos != wxNOT_FOUND )
116 {
117 sfilename = sfilename.Left(pos+1)+extension ;
a9412f8f 118 cfString.Assign( sfilename , wxFONTENCODING_DEFAULT ) ;
f65d4b83 119 NavDialogSetSaveFileName( ioParams->context , cfString ) ;
e40298d5
JS
120 }
121 }
f65d4b83 122 }
e40298d5 123 }
5b781a67
SC
124}
125
f3078f07 126void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
4d4d8bbf 127{
e40298d5
JS
128 myData->menuitems = NULL ;
129 myData->currentfilter = 0 ;
685a634c
DS
130 myData->saveMode = false ;
131
c3b8bf3f 132 if ( !filter.empty() )
4d4d8bbf 133 {
e40298d5
JS
134 wxString filter2(filter) ;
135 int filterIndex = 0;
136 bool isName = true ;
137 wxString current ;
f3078f07 138
e031f1df 139 for ( unsigned int i = 0; i < filter2.length() ; i++ )
e40298d5 140 {
f3078f07 141 if ( filter2.GetChar(i) == wxT('|') )
e40298d5 142 {
f3078f07
DS
143 if ( isName )
144 {
e40298d5
JS
145 myData->name.Add( current ) ;
146 }
f3078f07
DS
147 else
148 {
25fab0dc 149 myData->extensions.Add( current ) ;
e40298d5
JS
150 ++filterIndex ;
151 }
f3078f07 152
e40298d5 153 isName = !isName ;
427ff662 154 current = wxEmptyString ;
e40298d5
JS
155 }
156 else
157 {
158 current += filter2.GetChar(i) ;
159 }
4d4d8bbf 160 }
e40298d5
JS
161 // we allow for compatibility reason to have a single filter expression (like *.*) without
162 // an explanatory text, in that case the first part is name and extension at the same time
685a634c 163
427ff662 164 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
88a7a4e1 165 if ( current.empty() )
e40298d5
JS
166 myData->extensions.Add( myData->name[filterIndex] ) ;
167 else
25fab0dc 168 myData->extensions.Add( current ) ;
e40298d5 169 if ( filterIndex == 0 || isName )
25fab0dc 170 myData->name.Add( current ) ;
685a634c 171
e40298d5 172 ++filterIndex ;
685a634c 173
2b5f62a0 174 const size_t extCount = myData->extensions.GetCount();
e40298d5
JS
175 for ( size_t i = 0 ; i < extCount; i++ )
176 {
f3078f07 177 wxUint32 fileType, creator;
5974c3cf
SC
178 wxString extension = myData->extensions[i];
179
f3078f07 180 // Remove leading '*'
6264c8e2 181 if (extension.length() && (extension.GetChar(0) == '*'))
f3078f07 182 extension = extension.Mid( 1 );
5974c3cf 183
f3078f07 184 // Remove leading '.'
6264c8e2 185 if (extension.length() && (extension.GetChar(0) == '.'))
f3078f07 186 extension = extension.Mid( 1 );
88a7a4e1 187
5974c3cf 188 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
5974c3cf 189 myData->filtermactypes.Add( (OSType)fileType );
5974c3cf 190 else
f3078f07 191 myData->filtermactypes.Add( '****' ); // We'll fail safe if it's not recognized
e40298d5
JS
192 }
193 }
4d4d8bbf 194}
bb378910 195
f65d4b83 196static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data)
4d4d8bbf 197{
a4f5b9b9 198 wxString file(filename) ;
9f92f6fb 199 file.MakeUpper() ;
685a634c 200
2b5f62a0 201 if ( data->extensions.GetCount() > 0 )
da2b4b7a 202 {
e40298d5
JS
203 //for ( int i = 0 ; i < data->numfilters ; ++i )
204 int i = data->currentfilter ;
427ff662 205 if ( data->extensions[i].Right(2) == wxT(".*") )
e40298d5 206 return true ;
685a634c 207
e40298d5
JS
208 {
209 if ( type == (OSType)data->filtermactypes[i] )
210 return true ;
685a634c 211
427ff662 212 wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
f3078f07 213 while ( tokenizer.HasMoreTokens() )
e40298d5
JS
214 {
215 wxString extension = tokenizer.GetNextToken() ;
216 if ( extension.GetChar(0) == '*' )
217 extension = extension.Mid(1) ;
25fab0dc 218 extension.MakeUpper();
685a634c 219
e031f1df 220 if ( file.length() >= extension.length() && extension == file.Right(extension.length() ) )
e40298d5
JS
221 return true ;
222 }
223 }
f3078f07 224
e40298d5 225 return false ;
da2b4b7a 226 }
f3078f07 227
da2b4b7a 228 return true ;
4d4d8bbf
SC
229}
230
a2b77260 231#if !TARGET_API_MAC_OSX
5fde6fcc 232static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
a4f5b9b9 233{
e40298d5
JS
234 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
235 // return true if this item is invisible or a file
236
237 Boolean visibleFlag;
238 Boolean folderFlag;
685a634c 239
e40298d5
JS
240 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
241 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
685a634c 242
e40298d5
JS
243 // because the semantics of the filter proc are "true means don't show
244 // it" we need to invert the result that we return
685a634c 245
e40298d5
JS
246 if ( !visibleFlag )
247 return true ;
685a634c 248
e40298d5
JS
249 if ( !folderFlag )
250 {
a4f5b9b9 251 wxString file = wxMacMakeStringFromPascal( myCInfoPBPtr->hFileInfo.ioNamePtr ) ;
f65d4b83 252 return !CheckFile( file , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
a4f5b9b9 253 }
685a634c 254
e40298d5 255 return false ;
519cb848 256}
bb378910 257#endif
519cb848
SC
258
259// end wxmac
260
f3078f07
DS
261wxFileDialog::wxFileDialog(
262 wxWindow *parent, const wxString& message,
263 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
ff3e84ff
VZ
264 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
265 : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
e9576ca5 266{
427ff662 267 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
e9576ca5
SC
268}
269
f3078f07 270pascal Boolean CrossPlatformFilterCallback(
685a634c
DS
271 AEDesc *theItem,
272 void *info,
273 void *callBackUD,
f3078f07 274 NavFilterModes filterMode )
4d4d8bbf 275{
2d4e4f80 276 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
685a634c 277
2d4e4f80
GD
278 if (filterMode == kNavFilteringBrowserList)
279 {
80442e94
VZ
280 // We allow navigation to all folders. For files, we check against the current
281 // filter string.
282 // However, packages should be dealt with like files and not like folders. So
283 // check if a folder is a package before deciding what to do.
284 FSRef fsref;
2d4e4f80 285 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
80442e94
VZ
286 AECoerceDesc (theItem, typeFSRef, theItem);
287 if ( AEGetDescData (theItem, &fsref, sizeof (FSRef)) != noErr)
288 return true;
f7aeba1c 289
80442e94
VZ
290 if ( theInfo->isFolder )
291 {
292 // check bundle bit (using Finder Services - used by OS9 on some bundles)
293 FSCatalogInfo catalogInfo;
294 if (FSGetCatalogInfo (&fsref, kFSCatInfoFinderInfo, &catalogInfo, NULL, NULL, NULL) != noErr)
295 return true;
296
297 // Check bundle item (using Launch Services - used by OS-X through info.plist or APP)
298 LSItemInfoRecord lsInfo;
299 if (LSCopyItemInfoForRef(&fsref, kLSRequestBasicFlagsOnly, &lsInfo ) != noErr)
300 return true;
301
302 // If it's not a bundle, then it's a normal folder and it passes our filter
303 FileInfo *fileInfo = (FileInfo *) catalogInfo.finderInfo;
304 if ( !(fileInfo->finderFlags & kHasBundle) &&
305 !(lsInfo.flags & (kLSItemInfoIsApplication | kLSItemInfoIsPackage)) )
306 return true;
2d4e4f80 307 }
80442e94
VZ
308
309 wxString file = wxMacFSRefToPath( &fsref ) ;
310 return CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
311
2d4e4f80 312 }
685a634c 313
80442e94 314 return true;
4d4d8bbf
SC
315}
316
e9576ca5
SC
317int wxFileDialog::ShowModal()
318{
a4f5b9b9
GD
319 OSErr err;
320 NavDialogCreationOptions dialogCreateOptions;
f3078f07 321
a4f5b9b9
GD
322 // set default options
323 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
685a634c 324
a4f5b9b9
GD
325 // this was always unset in the old code
326 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
685a634c 327
e71800ba
SC
328 wxMacCFStringHolder message(m_message, m_font.GetEncoding());
329 dialogCreateOptions.windowTitle = message;
330
331 wxMacCFStringHolder defaultFileName(m_fileName, m_font.GetEncoding());
332 dialogCreateOptions.saveFileName = defaultFileName;
333
334
a4f5b9b9
GD
335 NavDialogRef dialog;
336 NavObjectFilterUPP navFilterUPP = NULL;
a4f5b9b9
GD
337 OpenUserDataRec myData;
338 myData.defaultLocation = m_dir;
339
75338075
DS
340 MakeUserDataRec(&myData , m_wildCard);
341 myData.currentfilter = m_filterIndex;
342 size_t numFilters = myData.extensions.GetCount();
343 if (numFilters)
344 {
345 CFMutableArrayRef popup = CFArrayCreateMutable( kCFAllocatorDefault ,
346 numFilters , &kCFTypeArrayCallBacks ) ;
347 dialogCreateOptions.popupExtension = popup ;
348 myData.menuitems = dialogCreateOptions.popupExtension ;
349 for ( size_t i = 0 ; i < numFilters ; ++i )
350 {
351 CFArrayAppendValue( popup , (CFStringRef) wxMacCFStringHolder( myData.name[i] , m_font.GetEncoding() ) ) ;
352 }
353 }
354
b014db05 355 if (HasFdFlag(wxFD_SAVE))
a4f5b9b9 356 {
75338075
DS
357 myData.saveMode = true;
358
685a634c
DS
359 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
360 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
f3078f07
DS
361 if (!numFilters)
362 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
685a634c
DS
363
364 // The extension is important
a485ee6f
JS
365 if (numFilters < 2)
366 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
685a634c 367
78eeb095 368#if TARGET_API_MAC_OSX
e031f1df 369 if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT))
f3078f07 370 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
78eeb095 371#endif
f3078f07
DS
372
373 err = ::NavCreatePutFileDialog(
374 &dialogCreateOptions,
375 kNavGenericSignature, // Suppresses the 'Default' (top) menu item
376 kNavGenericSignature,
377 sStandardNavEventFilter,
378 &myData, // for defaultLocation
379 &dialog );
a4f5b9b9
GD
380 }
381 else
382 {
f3078f07 383 // let the user select bundles/programs in dialogs
c11d9cb8 384 dialogCreateOptions.optionFlags |= kNavSupportPackages;
88a7a4e1 385
a4f5b9b9 386 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
f3078f07
DS
387 err = ::NavCreateGetFileDialog(
388 &dialogCreateOptions,
389 NULL, // NavTypeListHandle
390 sStandardNavEventFilter,
391 NULL, // NavPreviewUPP
392 navFilterUPP,
393 (void *) &myData, // inClientData
394 &dialog );
a4f5b9b9
GD
395 }
396
397 if (err == noErr)
398 err = ::NavDialogRun(dialog);
685a634c 399
a4f5b9b9
GD
400 // clean up filter related data, etc.
401 if (navFilterUPP)
402 ::DisposeNavObjectFilterUPP(navFilterUPP);
e71800ba 403
685a634c 404 if (err != noErr)
a4f5b9b9
GD
405 return wxID_CANCEL;
406
407 NavReplyRecord navReply;
408 err = ::NavDialogGetReply(dialog, &navReply);
685a634c 409 if (err == noErr && navReply.validRecord)
a4f5b9b9
GD
410 {
411 AEKeyword theKeyword;
412 DescType actualType;
413 Size actualSize;
414 FSRef theFSRef;
878973f1 415 wxString thePath ;
a4f5b9b9 416 long count;
f3078f07
DS
417
418 m_filterIndex = myData.currentfilter;
419 ::AECountItems( &navReply.selection, &count );
a4f5b9b9
GD
420 for (long i = 1; i <= count; ++i)
421 {
f3078f07
DS
422 err = ::AEGetNthPtr(
423 &(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
424 &theFSRef, sizeof(theFSRef), &actualSize );
685a634c 425 if (err != noErr)
a4f5b9b9
GD
426 break;
427
b014db05 428 if (HasFdFlag(wxFD_SAVE))
f3078f07 429 thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName );
685a634c 430 else
f3078f07 431 thePath = wxMacFSRefToPath( &theFSRef );
88a7a4e1 432
a449f840
DS
433 if (!thePath)
434 {
435 ::NavDisposeReply(&navReply);
436 return wxID_CANCEL;
a4f5b9b9 437 }
f3078f07 438
a4f5b9b9
GD
439 m_path = thePath;
440 m_paths.Add(m_path);
441 m_fileName = wxFileNameFromPath(m_path);
442 m_fileNames.Add(m_fileName);
443 }
f3078f07 444
a4f5b9b9
GD
445 // set these to the first hit
446 m_path = m_paths[0];
447 m_fileName = wxFileNameFromPath(m_path);
448 m_dir = wxPathOnly(m_path);
449 }
f3078f07 450
a4f5b9b9 451 ::NavDisposeReply(&navReply);
685a634c 452
a4f5b9b9 453 return (err == noErr) ? wxID_OK : wxID_CANCEL;
5b781a67 454}
72c1ba98
VZ
455
456#endif // wxUSE_FILEDLG
457