]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/filedlg.cpp
ignore WinCE projects and build directories
[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 85
6239ee05
SC
86 if( data->extensions.GetCount() > 0 )
87 {
88 NavMenuItemSpec menuItem;
89 memset( &menuItem, 0, sizeof(menuItem) );
90 menuItem.version = kNavMenuItemSpecVersion;
91 menuItem.menuType = data->currentfilter;
92 ::NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &menuItem);
93 }
e40298d5
JS
94 }
95 else if ( inSelector == kNavCBPopupMenuSelect )
96 {
97 NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
75338075
DS
98 const size_t numFilters = data->extensions.GetCount();
99
100 if ( menu->menuType < numFilters )
e40298d5
JS
101 {
102 data->currentfilter = menu->menuType ;
103 if ( data->saveMode )
104 {
105 int i = menu->menuType ;
f7aeba1c
VZ
106
107 // isolate the first extension string
108 wxString firstExtension = data->extensions[i].BeforeFirst('|').BeforeFirst(';');
109
110 wxString extension = firstExtension.AfterLast('.') ;
f65d4b83 111 wxString sfilename ;
685a634c 112
dbe4a80c 113 wxCFStringRef cfString( wxCFRetain( NavDialogGetSaveFileName( ioParams->context ) ) );
f65d4b83 114 sfilename = cfString.AsString() ;
f65d4b83 115
685a634c 116 int pos = sfilename.Find('.', true) ;
6239ee05 117 if ( pos != wxNOT_FOUND && extension != wxT("*") )
e40298d5
JS
118 {
119 sfilename = sfilename.Left(pos+1)+extension ;
dbe4a80c 120 cfString = wxCFStringRef( sfilename , wxFONTENCODING_DEFAULT ) ;
f65d4b83 121 NavDialogSetSaveFileName( ioParams->context , cfString ) ;
e40298d5
JS
122 }
123 }
f65d4b83 124 }
e40298d5 125 }
5b781a67
SC
126}
127
f3078f07 128void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
4d4d8bbf 129{
e40298d5
JS
130 myData->menuitems = NULL ;
131 myData->currentfilter = 0 ;
685a634c
DS
132 myData->saveMode = false ;
133
c3b8bf3f 134 if ( !filter.empty() )
4d4d8bbf 135 {
e40298d5
JS
136 wxString filter2(filter) ;
137 int filterIndex = 0;
138 bool isName = true ;
139 wxString current ;
f3078f07 140
e031f1df 141 for ( unsigned int i = 0; i < filter2.length() ; i++ )
e40298d5 142 {
f3078f07 143 if ( filter2.GetChar(i) == wxT('|') )
e40298d5 144 {
f3078f07
DS
145 if ( isName )
146 {
e40298d5
JS
147 myData->name.Add( current ) ;
148 }
f3078f07
DS
149 else
150 {
25fab0dc 151 myData->extensions.Add( current ) ;
e40298d5
JS
152 ++filterIndex ;
153 }
f3078f07 154
e40298d5 155 isName = !isName ;
427ff662 156 current = wxEmptyString ;
e40298d5
JS
157 }
158 else
159 {
160 current += filter2.GetChar(i) ;
161 }
4d4d8bbf 162 }
e40298d5
JS
163 // we allow for compatibility reason to have a single filter expression (like *.*) without
164 // an explanatory text, in that case the first part is name and extension at the same time
685a634c 165
427ff662 166 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
88a7a4e1 167 if ( current.empty() )
e40298d5
JS
168 myData->extensions.Add( myData->name[filterIndex] ) ;
169 else
25fab0dc 170 myData->extensions.Add( current ) ;
e40298d5 171 if ( filterIndex == 0 || isName )
25fab0dc 172 myData->name.Add( current ) ;
685a634c 173
e40298d5 174 ++filterIndex ;
685a634c 175
2b5f62a0 176 const size_t extCount = myData->extensions.GetCount();
e40298d5
JS
177 for ( size_t i = 0 ; i < extCount; i++ )
178 {
f3078f07 179 wxUint32 fileType, creator;
5974c3cf
SC
180 wxString extension = myData->extensions[i];
181
f3078f07 182 // Remove leading '*'
6264c8e2 183 if (extension.length() && (extension.GetChar(0) == '*'))
f3078f07 184 extension = extension.Mid( 1 );
5974c3cf 185
f3078f07 186 // Remove leading '.'
6264c8e2 187 if (extension.length() && (extension.GetChar(0) == '.'))
f3078f07 188 extension = extension.Mid( 1 );
88a7a4e1 189
5974c3cf 190 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
5974c3cf 191 myData->filtermactypes.Add( (OSType)fileType );
5974c3cf 192 else
f3078f07 193 myData->filtermactypes.Add( '****' ); // We'll fail safe if it's not recognized
e40298d5
JS
194 }
195 }
4d4d8bbf 196}
bb378910 197
f65d4b83 198static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data)
4d4d8bbf 199{
a4f5b9b9 200 wxString file(filename) ;
9f92f6fb 201 file.MakeUpper() ;
685a634c 202
2b5f62a0 203 if ( data->extensions.GetCount() > 0 )
da2b4b7a 204 {
e40298d5
JS
205 //for ( int i = 0 ; i < data->numfilters ; ++i )
206 int i = data->currentfilter ;
427ff662 207 if ( data->extensions[i].Right(2) == wxT(".*") )
e40298d5 208 return true ;
685a634c 209
e40298d5
JS
210 {
211 if ( type == (OSType)data->filtermactypes[i] )
212 return true ;
685a634c 213
427ff662 214 wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
f3078f07 215 while ( tokenizer.HasMoreTokens() )
e40298d5
JS
216 {
217 wxString extension = tokenizer.GetNextToken() ;
218 if ( extension.GetChar(0) == '*' )
219 extension = extension.Mid(1) ;
25fab0dc 220 extension.MakeUpper();
685a634c 221
e031f1df 222 if ( file.length() >= extension.length() && extension == file.Right(extension.length() ) )
e40298d5
JS
223 return true ;
224 }
225 }
f3078f07 226
e40298d5 227 return false ;
da2b4b7a 228 }
f3078f07 229
da2b4b7a 230 return true ;
4d4d8bbf
SC
231}
232
519cb848
SC
233// end wxmac
234
f3078f07
DS
235wxFileDialog::wxFileDialog(
236 wxWindow *parent, const wxString& message,
237 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
ff3e84ff
VZ
238 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
239 : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
e9576ca5 240{
427ff662 241 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
e9576ca5
SC
242}
243
f3078f07 244pascal Boolean CrossPlatformFilterCallback(
685a634c
DS
245 AEDesc *theItem,
246 void *info,
247 void *callBackUD,
f3078f07 248 NavFilterModes filterMode )
4d4d8bbf 249{
2d4e4f80 250 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
685a634c 251
2d4e4f80
GD
252 if (filterMode == kNavFilteringBrowserList)
253 {
80442e94
VZ
254 // We allow navigation to all folders. For files, we check against the current
255 // filter string.
256 // However, packages should be dealt with like files and not like folders. So
257 // check if a folder is a package before deciding what to do.
2d4e4f80 258 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
6239ee05 259 FSRef fsref;
f7aeba1c 260
80442e94
VZ
261 if ( theInfo->isFolder )
262 {
263 // check bundle bit (using Finder Services - used by OS9 on some bundles)
264 FSCatalogInfo catalogInfo;
265 if (FSGetCatalogInfo (&fsref, kFSCatInfoFinderInfo, &catalogInfo, NULL, NULL, NULL) != noErr)
266 return true;
267
268 // Check bundle item (using Launch Services - used by OS-X through info.plist or APP)
269 LSItemInfoRecord lsInfo;
270 if (LSCopyItemInfoForRef(&fsref, kLSRequestBasicFlagsOnly, &lsInfo ) != noErr)
271 return true;
272
273 // If it's not a bundle, then it's a normal folder and it passes our filter
274 FileInfo *fileInfo = (FileInfo *) catalogInfo.finderInfo;
275 if ( !(fileInfo->finderFlags & kHasBundle) &&
276 !(lsInfo.flags & (kLSItemInfoIsApplication | kLSItemInfoIsPackage)) )
277 return true;
2d4e4f80 278 }
6239ee05
SC
279 else
280 {
281 AECoerceDesc (theItem, typeFSRef, theItem);
282 if ( AEGetDescData (theItem, &fsref, sizeof (FSRef)) == noErr)
283 {
284 wxString file = wxMacFSRefToPath( &fsref ) ;
285 return CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
286 }
287 }
2d4e4f80 288 }
685a634c 289
80442e94 290 return true;
4d4d8bbf
SC
291}
292
e9576ca5
SC
293int wxFileDialog::ShowModal()
294{
a4f5b9b9
GD
295 OSErr err;
296 NavDialogCreationOptions dialogCreateOptions;
f3078f07 297
a4f5b9b9
GD
298 // set default options
299 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
685a634c 300
a4f5b9b9
GD
301 // this was always unset in the old code
302 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
685a634c 303
34a3ed01 304 wxCFStringRef message(m_message, GetFont().GetEncoding());
e71800ba
SC
305 dialogCreateOptions.windowTitle = message;
306
34a3ed01 307 wxCFStringRef defaultFileName(m_fileName, GetFont().GetEncoding());
e71800ba
SC
308 dialogCreateOptions.saveFileName = defaultFileName;
309
310
a4f5b9b9
GD
311 NavDialogRef dialog;
312 NavObjectFilterUPP navFilterUPP = NULL;
a4f5b9b9
GD
313 OpenUserDataRec myData;
314 myData.defaultLocation = m_dir;
315
75338075
DS
316 MakeUserDataRec(&myData , m_wildCard);
317 myData.currentfilter = m_filterIndex;
318 size_t numFilters = myData.extensions.GetCount();
319 if (numFilters)
320 {
321 CFMutableArrayRef popup = CFArrayCreateMutable( kCFAllocatorDefault ,
322 numFilters , &kCFTypeArrayCallBacks ) ;
323 dialogCreateOptions.popupExtension = popup ;
324 myData.menuitems = dialogCreateOptions.popupExtension ;
325 for ( size_t i = 0 ; i < numFilters ; ++i )
326 {
34a3ed01 327 CFArrayAppendValue( popup , (CFStringRef) wxCFStringRef( myData.name[i] , GetFont().GetEncoding() ) ) ;
75338075
DS
328 }
329 }
330
b014db05 331 if (HasFdFlag(wxFD_SAVE))
a4f5b9b9 332 {
75338075
DS
333 myData.saveMode = true;
334
685a634c
DS
335 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
336 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
f3078f07
DS
337 if (!numFilters)
338 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
685a634c
DS
339
340 // The extension is important
a485ee6f
JS
341 if (numFilters < 2)
342 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
685a634c 343
e031f1df 344 if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT))
f3078f07 345 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
f3078f07
DS
346
347 err = ::NavCreatePutFileDialog(
348 &dialogCreateOptions,
349 kNavGenericSignature, // Suppresses the 'Default' (top) menu item
350 kNavGenericSignature,
351 sStandardNavEventFilter,
352 &myData, // for defaultLocation
353 &dialog );
a4f5b9b9
GD
354 }
355 else
356 {
f3078f07 357 // let the user select bundles/programs in dialogs
c11d9cb8 358 dialogCreateOptions.optionFlags |= kNavSupportPackages;
88a7a4e1 359
a4f5b9b9 360 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
f3078f07
DS
361 err = ::NavCreateGetFileDialog(
362 &dialogCreateOptions,
363 NULL, // NavTypeListHandle
364 sStandardNavEventFilter,
365 NULL, // NavPreviewUPP
366 navFilterUPP,
367 (void *) &myData, // inClientData
368 &dialog );
a4f5b9b9
GD
369 }
370
371 if (err == noErr)
372 err = ::NavDialogRun(dialog);
685a634c 373
a4f5b9b9
GD
374 // clean up filter related data, etc.
375 if (navFilterUPP)
376 ::DisposeNavObjectFilterUPP(navFilterUPP);
e71800ba 377
685a634c 378 if (err != noErr)
6239ee05
SC
379 {
380 ::NavDialogDispose(dialog);
a4f5b9b9 381 return wxID_CANCEL;
6239ee05 382 }
a4f5b9b9
GD
383
384 NavReplyRecord navReply;
385 err = ::NavDialogGetReply(dialog, &navReply);
685a634c 386 if (err == noErr && navReply.validRecord)
a4f5b9b9
GD
387 {
388 AEKeyword theKeyword;
389 DescType actualType;
390 Size actualSize;
391 FSRef theFSRef;
878973f1 392 wxString thePath ;
a4f5b9b9 393 long count;
f3078f07
DS
394
395 m_filterIndex = myData.currentfilter;
396 ::AECountItems( &navReply.selection, &count );
a4f5b9b9
GD
397 for (long i = 1; i <= count; ++i)
398 {
f3078f07
DS
399 err = ::AEGetNthPtr(
400 &(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
401 &theFSRef, sizeof(theFSRef), &actualSize );
685a634c 402 if (err != noErr)
a4f5b9b9
GD
403 break;
404
b014db05 405 if (HasFdFlag(wxFD_SAVE))
f3078f07 406 thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName );
685a634c 407 else
f3078f07 408 thePath = wxMacFSRefToPath( &theFSRef );
88a7a4e1 409
a449f840
DS
410 if (!thePath)
411 {
412 ::NavDisposeReply(&navReply);
6239ee05 413 ::NavDialogDispose(dialog);
a449f840 414 return wxID_CANCEL;
a4f5b9b9 415 }
f3078f07 416
a4f5b9b9
GD
417 m_path = thePath;
418 m_paths.Add(m_path);
419 m_fileName = wxFileNameFromPath(m_path);
420 m_fileNames.Add(m_fileName);
421 }
f3078f07 422
a4f5b9b9
GD
423 // set these to the first hit
424 m_path = m_paths[0];
425 m_fileName = wxFileNameFromPath(m_path);
426 m_dir = wxPathOnly(m_path);
427 }
f3078f07 428
a4f5b9b9 429 ::NavDisposeReply(&navReply);
6239ee05 430 ::NavDialogDispose(dialog);
685a634c 431
a4f5b9b9 432 return (err == noErr) ? wxID_OK : wxID_CANCEL;
5b781a67 433}
72c1ba98
VZ
434
435#endif // wxUSE_FILEDLG
436