]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/filedlg.cpp
Fix export.
[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 ;
104 wxString extension = data->extensions[i].AfterLast('.') ;
105 extension.MakeLower() ;
f65d4b83 106 wxString sfilename ;
685a634c 107
4891a3d5 108 wxMacCFStringHolder cfString( NavDialogGetSaveFileName( ioParams->context ) , false );
f65d4b83 109 sfilename = cfString.AsString() ;
f65d4b83 110
685a634c 111 int pos = sfilename.Find('.', true) ;
e40298d5
JS
112 if ( pos != wxNOT_FOUND )
113 {
114 sfilename = sfilename.Left(pos+1)+extension ;
a9412f8f 115 cfString.Assign( sfilename , wxFONTENCODING_DEFAULT ) ;
f65d4b83 116 NavDialogSetSaveFileName( ioParams->context , cfString ) ;
e40298d5
JS
117 }
118 }
f65d4b83 119 }
e40298d5 120 }
5b781a67
SC
121}
122
f3078f07 123void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
4d4d8bbf 124{
e40298d5
JS
125 myData->menuitems = NULL ;
126 myData->currentfilter = 0 ;
685a634c
DS
127 myData->saveMode = false ;
128
e40298d5 129 if ( filter && filter[0] )
4d4d8bbf 130 {
e40298d5
JS
131 wxString filter2(filter) ;
132 int filterIndex = 0;
133 bool isName = true ;
134 wxString current ;
f3078f07 135
e031f1df 136 for ( unsigned int i = 0; i < filter2.length() ; i++ )
e40298d5 137 {
f3078f07 138 if ( filter2.GetChar(i) == wxT('|') )
e40298d5 139 {
f3078f07
DS
140 if ( isName )
141 {
e40298d5
JS
142 myData->name.Add( current ) ;
143 }
f3078f07
DS
144 else
145 {
e40298d5
JS
146 myData->extensions.Add( current.MakeUpper() ) ;
147 ++filterIndex ;
148 }
f3078f07 149
e40298d5 150 isName = !isName ;
427ff662 151 current = wxEmptyString ;
e40298d5
JS
152 }
153 else
154 {
155 current += filter2.GetChar(i) ;
156 }
4d4d8bbf 157 }
e40298d5
JS
158 // we allow for compatibility reason to have a single filter expression (like *.*) without
159 // an explanatory text, in that case the first part is name and extension at the same time
685a634c 160
427ff662 161 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
88a7a4e1 162 if ( current.empty() )
e40298d5
JS
163 myData->extensions.Add( myData->name[filterIndex] ) ;
164 else
165 myData->extensions.Add( current.MakeUpper() ) ;
166 if ( filterIndex == 0 || isName )
167 myData->name.Add( current.MakeUpper() ) ;
685a634c 168
e40298d5 169 ++filterIndex ;
685a634c 170
2b5f62a0 171 const size_t extCount = myData->extensions.GetCount();
e40298d5
JS
172 for ( size_t i = 0 ; i < extCount; i++ )
173 {
f3078f07 174 wxUint32 fileType, creator;
5974c3cf
SC
175 wxString extension = myData->extensions[i];
176
f3078f07 177 // Remove leading '*'
6264c8e2 178 if (extension.length() && (extension.GetChar(0) == '*'))
f3078f07 179 extension = extension.Mid( 1 );
5974c3cf 180
f3078f07 181 // Remove leading '.'
6264c8e2 182 if (extension.length() && (extension.GetChar(0) == '.'))
f3078f07 183 extension = extension.Mid( 1 );
88a7a4e1 184
5974c3cf 185 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
5974c3cf 186 myData->filtermactypes.Add( (OSType)fileType );
5974c3cf 187 else
f3078f07 188 myData->filtermactypes.Add( '****' ); // We'll fail safe if it's not recognized
e40298d5
JS
189 }
190 }
4d4d8bbf 191}
bb378910 192
f65d4b83 193static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data)
4d4d8bbf 194{
a4f5b9b9 195 wxString file(filename) ;
9f92f6fb 196 file.MakeUpper() ;
685a634c 197
2b5f62a0 198 if ( data->extensions.GetCount() > 0 )
da2b4b7a 199 {
e40298d5
JS
200 //for ( int i = 0 ; i < data->numfilters ; ++i )
201 int i = data->currentfilter ;
427ff662 202 if ( data->extensions[i].Right(2) == wxT(".*") )
e40298d5 203 return true ;
685a634c 204
e40298d5
JS
205 {
206 if ( type == (OSType)data->filtermactypes[i] )
207 return true ;
685a634c 208
427ff662 209 wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
f3078f07 210 while ( tokenizer.HasMoreTokens() )
e40298d5
JS
211 {
212 wxString extension = tokenizer.GetNextToken() ;
213 if ( extension.GetChar(0) == '*' )
214 extension = extension.Mid(1) ;
685a634c 215
e031f1df 216 if ( file.length() >= extension.length() && extension == file.Right(extension.length() ) )
e40298d5
JS
217 return true ;
218 }
219 }
f3078f07 220
e40298d5 221 return false ;
da2b4b7a 222 }
f3078f07 223
da2b4b7a 224 return true ;
4d4d8bbf
SC
225}
226
a2b77260 227#if !TARGET_API_MAC_OSX
5fde6fcc 228static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
a4f5b9b9 229{
e40298d5
JS
230 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
231 // return true if this item is invisible or a file
232
233 Boolean visibleFlag;
234 Boolean folderFlag;
685a634c 235
e40298d5
JS
236 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
237 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
685a634c 238
e40298d5
JS
239 // because the semantics of the filter proc are "true means don't show
240 // it" we need to invert the result that we return
685a634c 241
e40298d5
JS
242 if ( !visibleFlag )
243 return true ;
685a634c 244
e40298d5
JS
245 if ( !folderFlag )
246 {
a4f5b9b9 247 wxString file = wxMacMakeStringFromPascal( myCInfoPBPtr->hFileInfo.ioNamePtr ) ;
f65d4b83 248 return !CheckFile( file , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
a4f5b9b9 249 }
685a634c 250
e40298d5 251 return false ;
519cb848 252}
bb378910 253#endif
519cb848
SC
254
255// end wxmac
256
f3078f07
DS
257wxFileDialog::wxFileDialog(
258 wxWindow *parent, const wxString& message,
259 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
ff3e84ff
VZ
260 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
261 : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
e9576ca5 262{
427ff662 263 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
e9576ca5
SC
264}
265
f3078f07 266pascal Boolean CrossPlatformFilterCallback(
685a634c
DS
267 AEDesc *theItem,
268 void *info,
269 void *callBackUD,
f3078f07 270 NavFilterModes filterMode )
4d4d8bbf 271{
2d4e4f80
GD
272 bool display = true;
273 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
685a634c 274
2d4e4f80
GD
275 if (filterMode == kNavFilteringBrowserList)
276 {
277 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
f65d4b83 278 if ( !theInfo->isFolder )
2d4e4f80 279 {
f387b80e
SC
280 AECoerceDesc (theItem, typeFSRef, theItem);
281
282 FSRef fsref ;
283 if ( AEGetDescData (theItem, &fsref, sizeof (FSRef)) == noErr )
f65d4b83 284 {
f65d4b83 285 memcpy( &fsref , *theItem->dataHandle , sizeof(FSRef) ) ;
a2b77260 286 wxString file = wxMacFSRefToPath( &fsref ) ;
f65d4b83
SC
287 display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
288 }
2d4e4f80
GD
289 }
290 }
685a634c 291
2d4e4f80 292 return display;
4d4d8bbf
SC
293}
294
e9576ca5
SC
295int wxFileDialog::ShowModal()
296{
a4f5b9b9
GD
297 OSErr err;
298 NavDialogCreationOptions dialogCreateOptions;
f3078f07 299
a4f5b9b9
GD
300 // set default options
301 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
685a634c 302
a4f5b9b9
GD
303 // this was always unset in the old code
304 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
685a634c 305
e71800ba
SC
306 wxMacCFStringHolder message(m_message, m_font.GetEncoding());
307 dialogCreateOptions.windowTitle = message;
308
309 wxMacCFStringHolder defaultFileName(m_fileName, m_font.GetEncoding());
310 dialogCreateOptions.saveFileName = defaultFileName;
311
312
a4f5b9b9
GD
313 NavDialogRef dialog;
314 NavObjectFilterUPP navFilterUPP = NULL;
a4f5b9b9
GD
315 OpenUserDataRec myData;
316 myData.defaultLocation = m_dir;
317
75338075
DS
318 MakeUserDataRec(&myData , m_wildCard);
319 myData.currentfilter = m_filterIndex;
320 size_t numFilters = myData.extensions.GetCount();
321 if (numFilters)
322 {
323 CFMutableArrayRef popup = CFArrayCreateMutable( kCFAllocatorDefault ,
324 numFilters , &kCFTypeArrayCallBacks ) ;
325 dialogCreateOptions.popupExtension = popup ;
326 myData.menuitems = dialogCreateOptions.popupExtension ;
327 for ( size_t i = 0 ; i < numFilters ; ++i )
328 {
329 CFArrayAppendValue( popup , (CFStringRef) wxMacCFStringHolder( myData.name[i] , m_font.GetEncoding() ) ) ;
330 }
331 }
332
ff3e84ff 333 if (HasFlag(wxFD_SAVE))
a4f5b9b9 334 {
75338075
DS
335 myData.saveMode = true;
336
685a634c
DS
337 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
338 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
f3078f07
DS
339 if (!numFilters)
340 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
685a634c
DS
341
342 // The extension is important
a485ee6f
JS
343 if (numFilters < 2)
344 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
685a634c 345
78eeb095 346#if TARGET_API_MAC_OSX
e031f1df 347 if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT))
f3078f07 348 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
78eeb095 349#endif
f3078f07
DS
350
351 err = ::NavCreatePutFileDialog(
352 &dialogCreateOptions,
353 kNavGenericSignature, // Suppresses the 'Default' (top) menu item
354 kNavGenericSignature,
355 sStandardNavEventFilter,
356 &myData, // for defaultLocation
357 &dialog );
a4f5b9b9
GD
358 }
359 else
360 {
f3078f07 361 // let the user select bundles/programs in dialogs
c11d9cb8 362 dialogCreateOptions.optionFlags |= kNavSupportPackages;
88a7a4e1 363
a4f5b9b9 364 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
f3078f07
DS
365 err = ::NavCreateGetFileDialog(
366 &dialogCreateOptions,
367 NULL, // NavTypeListHandle
368 sStandardNavEventFilter,
369 NULL, // NavPreviewUPP
370 navFilterUPP,
371 (void *) &myData, // inClientData
372 &dialog );
a4f5b9b9
GD
373 }
374
375 if (err == noErr)
376 err = ::NavDialogRun(dialog);
685a634c 377
a4f5b9b9
GD
378 // clean up filter related data, etc.
379 if (navFilterUPP)
380 ::DisposeNavObjectFilterUPP(navFilterUPP);
e71800ba 381
685a634c 382 if (err != noErr)
a4f5b9b9
GD
383 return wxID_CANCEL;
384
385 NavReplyRecord navReply;
386 err = ::NavDialogGetReply(dialog, &navReply);
685a634c 387 if (err == noErr && navReply.validRecord)
a4f5b9b9
GD
388 {
389 AEKeyword theKeyword;
390 DescType actualType;
391 Size actualSize;
392 FSRef theFSRef;
878973f1 393 wxString thePath ;
a4f5b9b9 394 long count;
f3078f07
DS
395
396 m_filterIndex = myData.currentfilter;
397 ::AECountItems( &navReply.selection, &count );
a4f5b9b9
GD
398 for (long i = 1; i <= count; ++i)
399 {
f3078f07
DS
400 err = ::AEGetNthPtr(
401 &(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
402 &theFSRef, sizeof(theFSRef), &actualSize );
685a634c 403 if (err != noErr)
a4f5b9b9
GD
404 break;
405
ff3e84ff 406 if (HasFlag(wxFD_SAVE))
f3078f07 407 thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName );
685a634c 408 else
f3078f07 409 thePath = wxMacFSRefToPath( &theFSRef );
88a7a4e1 410
a449f840
DS
411 if (!thePath)
412 {
413 ::NavDisposeReply(&navReply);
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);
685a634c 430
a4f5b9b9 431 return (err == noErr) ? wxID_OK : wxID_CANCEL;
5b781a67 432}
72c1ba98
VZ
433
434#endif // wxUSE_FILEDLG
435