]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/filedlg.cpp
revert r59246; the window parameter was there by purpose (i.e. for consistency with...
[wxWidgets.git] / src / osx / carbon / filedlg.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/filedlg.cpp
489468fe
SC
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
1f0c8f31 28#include "wx/osx/private.h"
489468fe
SC
29
30#ifndef __DARWIN__
31 #include <Navigation.h>
32 #include "PLStringFuncs.h"
33#endif
34
35IMPLEMENT_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
43struct OpenUserDataRec
44{
45 int currentfilter ;
46 bool saveMode ;
47 wxArrayString name ;
48 wxArrayString extensions ;
49 wxArrayLong filtermactypes ;
50 wxString defaultLocation;
51 CFArrayRef menuitems ;
52};
53
54typedef struct OpenUserDataRec
55OpenUserDataRec, *OpenUserDataRecPtr;
56
57static pascal void NavEventProc(
58 NavEventCallbackMessage inSelector,
59 NavCBRecPtr ioParams,
60 NavCallBackUserData ioUserData );
61
62static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc);
63
64static pascal void NavEventProc(
65 NavEventCallbackMessage inSelector,
66 NavCBRecPtr ioParams,
67 NavCallBackUserData ioUserData )
68{
69 OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ;
70 if (inSelector == kNavCBEvent)
71 {
72 }
73 else if ( inSelector == kNavCBStart )
74 {
75 if (data && !(data->defaultLocation).empty())
76 {
77 // Set default location for the modern Navigation APIs
78 // Apple Technical Q&A 1151
79 FSRef theFile;
80 wxMacPathToFSRef(data->defaultLocation, &theFile);
81 AEDesc theLocation = { typeNull, NULL };
82 if (noErr == ::AECreateDesc(typeFSRef, &theFile, sizeof(FSRef), &theLocation))
83 ::NavCustomControl(ioParams->context, kNavCtlSetLocation, (void *) &theLocation);
84 }
85
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 }
94 }
95 else if ( inSelector == kNavCBPopupMenuSelect )
96 {
97 NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
98 const size_t numFilters = data->extensions.GetCount();
99
100 if ( menu->menuType < numFilters )
101 {
102 data->currentfilter = menu->menuType ;
103 if ( data->saveMode )
104 {
105 int i = menu->menuType ;
106
107 // isolate the first extension string
108 wxString firstExtension = data->extensions[i].BeforeFirst('|').BeforeFirst(';');
109
110 wxString extension = firstExtension.AfterLast('.') ;
111 wxString sfilename ;
112
113 wxCFStringRef cfString( wxCFRetain( NavDialogGetSaveFileName( ioParams->context ) ) );
114 sfilename = cfString.AsString() ;
115
116 int pos = sfilename.Find('.', true) ;
117 if ( pos != wxNOT_FOUND && extension != wxT("*") )
118 {
119 sfilename = sfilename.Left(pos+1)+extension ;
120 cfString = wxCFStringRef( sfilename , wxFONTENCODING_DEFAULT ) ;
121 NavDialogSetSaveFileName( ioParams->context , cfString ) ;
122 }
123 }
124 }
125 }
126}
127
128void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
129{
130 myData->menuitems = NULL ;
131 myData->currentfilter = 0 ;
132 myData->saveMode = false ;
133
134 if ( !filter.empty() )
135 {
136 wxString filter2(filter) ;
137 int filterIndex = 0;
138 bool isName = true ;
139 wxString current ;
140
141 for ( unsigned int i = 0; i < filter2.length() ; i++ )
142 {
143 if ( filter2.GetChar(i) == wxT('|') )
144 {
145 if ( isName )
146 {
147 myData->name.Add( current ) ;
148 }
149 else
150 {
151 myData->extensions.Add( current ) ;
152 ++filterIndex ;
153 }
154
155 isName = !isName ;
156 current = wxEmptyString ;
157 }
158 else
159 {
160 current += filter2.GetChar(i) ;
161 }
162 }
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
165
166 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
167 if ( current.empty() )
168 myData->extensions.Add( myData->name[filterIndex] ) ;
169 else
170 myData->extensions.Add( current ) ;
171 if ( filterIndex == 0 || isName )
172 myData->name.Add( current ) ;
173
174 ++filterIndex ;
175
176 const size_t extCount = myData->extensions.GetCount();
177 for ( size_t i = 0 ; i < extCount; i++ )
178 {
179 wxUint32 fileType, creator;
180 wxString extension = myData->extensions[i];
181
182 // Remove leading '*'
183 if (extension.length() && (extension.GetChar(0) == '*'))
184 extension = extension.Mid( 1 );
185
186 // Remove leading '.'
187 if (extension.length() && (extension.GetChar(0) == '.'))
188 extension = extension.Mid( 1 );
189
190 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
191 myData->filtermactypes.Add( (OSType)fileType );
192 else
193 myData->filtermactypes.Add( '****' ); // We'll fail safe if it's not recognized
194 }
195 }
196}
197
198static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data)
199{
200 wxString file(filename) ;
201 file.MakeUpper() ;
202
203 if ( data->extensions.GetCount() > 0 )
204 {
205 //for ( int i = 0 ; i < data->numfilters ; ++i )
206 int i = data->currentfilter ;
207 if ( data->extensions[i].Right(2) == wxT(".*") )
208 return true ;
209
210 {
211 if ( type == (OSType)data->filtermactypes[i] )
212 return true ;
213
214 wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
215 while ( tokenizer.HasMoreTokens() )
216 {
217 wxString extension = tokenizer.GetNextToken() ;
218 if ( extension.GetChar(0) == '*' )
219 extension = extension.Mid(1) ;
220 extension.MakeUpper();
221
222 if ( file.length() >= extension.length() && extension == file.Right(extension.length() ) )
223 return true ;
224 }
225 }
226
227 return false ;
228 }
229
230 return true ;
231}
232
233// end wxmac
234
235wxFileDialog::wxFileDialog(
236 wxWindow *parent, const wxString& message,
237 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
238 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
239 : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
240{
241 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
242}
243
244pascal Boolean CrossPlatformFilterCallback(
245 AEDesc *theItem,
246 void *info,
247 void *callBackUD,
248 NavFilterModes filterMode )
249{
250 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
251
252 if (filterMode == kNavFilteringBrowserList)
253 {
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.
258 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
259 FSRef fsref;
260
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;
278 }
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 }
288 }
289
290 return true;
291}
292
293int wxFileDialog::ShowModal()
294{
295 OSErr err;
296 NavDialogCreationOptions dialogCreateOptions;
297
298 // set default options
299 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
300
301 // this was always unset in the old code
302 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
303
304 wxCFStringRef message(m_message, GetFont().GetEncoding());
305 dialogCreateOptions.windowTitle = message;
306
307 wxCFStringRef defaultFileName(m_fileName, GetFont().GetEncoding());
308 dialogCreateOptions.saveFileName = defaultFileName;
309
310
311 NavDialogRef dialog;
312 NavObjectFilterUPP navFilterUPP = NULL;
313 OpenUserDataRec myData;
314 myData.defaultLocation = m_dir;
315
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 {
327 CFArrayAppendValue( popup , (CFStringRef) wxCFStringRef( myData.name[i] , GetFont().GetEncoding() ) ) ;
328 }
329 }
330
331 if (HasFdFlag(wxFD_SAVE))
332 {
333 myData.saveMode = true;
334
335 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
336 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
337 if (!numFilters)
338 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
339
340 // The extension is important
341 if (numFilters < 2)
342 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
343
344 if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT))
345 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
346
347 err = ::NavCreatePutFileDialog(
348 &dialogCreateOptions,
349 kNavGenericSignature, // Suppresses the 'Default' (top) menu item
350 kNavGenericSignature,
351 sStandardNavEventFilter,
352 &myData, // for defaultLocation
353 &dialog );
354 }
355 else
356 {
357 // let the user select bundles/programs in dialogs
358 dialogCreateOptions.optionFlags |= kNavSupportPackages;
359
360 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
361 err = ::NavCreateGetFileDialog(
362 &dialogCreateOptions,
363 NULL, // NavTypeListHandle
364 sStandardNavEventFilter,
365 NULL, // NavPreviewUPP
366 navFilterUPP,
367 (void *) &myData, // inClientData
368 &dialog );
369 }
370
371 if (err == noErr)
372 err = ::NavDialogRun(dialog);
373
374 // clean up filter related data, etc.
375 if (navFilterUPP)
376 ::DisposeNavObjectFilterUPP(navFilterUPP);
377
378 if (err != noErr)
379 {
380 ::NavDialogDispose(dialog);
381 return wxID_CANCEL;
382 }
383
384 NavReplyRecord navReply;
385 err = ::NavDialogGetReply(dialog, &navReply);
386 if (err == noErr && navReply.validRecord)
387 {
388 AEKeyword theKeyword;
389 DescType actualType;
390 Size actualSize;
391 FSRef theFSRef;
392 wxString thePath ;
393 long count;
394
395 m_filterIndex = myData.currentfilter;
396 ::AECountItems( &navReply.selection, &count );
397 for (long i = 1; i <= count; ++i)
398 {
399 err = ::AEGetNthPtr(
400 &(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
401 &theFSRef, sizeof(theFSRef), &actualSize );
402 if (err != noErr)
403 break;
404
405 if (HasFdFlag(wxFD_SAVE))
406 thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName );
407 else
408 thePath = wxMacFSRefToPath( &theFSRef );
409
410 if (!thePath)
411 {
412 ::NavDisposeReply(&navReply);
413 ::NavDialogDispose(dialog);
414 return wxID_CANCEL;
415 }
416
417 m_path = thePath;
418 m_paths.Add(m_path);
419 m_fileName = wxFileNameFromPath(m_path);
420 m_fileNames.Add(m_fileName);
421 }
422
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 }
428
429 ::NavDisposeReply(&navReply);
430 ::NavDialogDispose(dialog);
431
432 return (err == noErr) ? wxID_OK : wxID_CANCEL;
433}
434
435#endif // wxUSE_FILEDLG
436