]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/filedlg.cpp
wx/log.h belongs to pch system
[wxWidgets.git] / src / mac / carbon / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/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/mac/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 struct 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
54 typedef struct OpenUserDataRec
55 OpenUserDataRec, *OpenUserDataRecPtr;
56
57 static pascal void NavEventProc(
58 NavEventCallbackMessage inSelector,
59 NavCBRecPtr ioParams,
60 NavCallBackUserData ioUserData );
61
62 static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc);
63
64 static 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 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);
92 }
93 else if ( inSelector == kNavCBPopupMenuSelect )
94 {
95 NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
96 const size_t numFilters = data->extensions.GetCount();
97
98 if ( menu->menuType < numFilters )
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() ;
106 wxString sfilename ;
107
108 wxMacCFStringHolder cfString( NavDialogGetSaveFileName( ioParams->context ) , false );
109 sfilename = cfString.AsString() ;
110
111 int pos = sfilename.Find('.', true) ;
112 if ( pos != wxNOT_FOUND )
113 {
114 sfilename = sfilename.Left(pos+1)+extension ;
115 cfString.Assign( sfilename , wxFONTENCODING_DEFAULT ) ;
116 NavDialogSetSaveFileName( ioParams->context , cfString ) ;
117 }
118 }
119 }
120 }
121 }
122
123 void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
124 {
125 myData->menuitems = NULL ;
126 myData->currentfilter = 0 ;
127 myData->saveMode = false ;
128
129 if ( filter && filter[0] )
130 {
131 wxString filter2(filter) ;
132 int filterIndex = 0;
133 bool isName = true ;
134 wxString current ;
135
136 for ( unsigned int i = 0; i < filter2.length() ; i++ )
137 {
138 if ( filter2.GetChar(i) == wxT('|') )
139 {
140 if ( isName )
141 {
142 myData->name.Add( current ) ;
143 }
144 else
145 {
146 myData->extensions.Add( current.MakeUpper() ) ;
147 ++filterIndex ;
148 }
149
150 isName = !isName ;
151 current = wxEmptyString ;
152 }
153 else
154 {
155 current += filter2.GetChar(i) ;
156 }
157 }
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
160
161 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
162 if ( current.empty() )
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() ) ;
168
169 ++filterIndex ;
170
171 const size_t extCount = myData->extensions.GetCount();
172 for ( size_t i = 0 ; i < extCount; i++ )
173 {
174 wxUint32 fileType, creator;
175 wxString extension = myData->extensions[i];
176
177 // Remove leading '*'
178 if (extension.length() && (extension.GetChar(0) == '*'))
179 extension = extension.Mid( 1 );
180
181 // Remove leading '.'
182 if (extension.length() && (extension.GetChar(0) == '.'))
183 extension = extension.Mid( 1 );
184
185 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
186 myData->filtermactypes.Add( (OSType)fileType );
187 else
188 myData->filtermactypes.Add( '****' ); // We'll fail safe if it's not recognized
189 }
190 }
191 }
192
193 static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data)
194 {
195 wxString file(filename) ;
196 file.MakeUpper() ;
197
198 if ( data->extensions.GetCount() > 0 )
199 {
200 //for ( int i = 0 ; i < data->numfilters ; ++i )
201 int i = data->currentfilter ;
202 if ( data->extensions[i].Right(2) == wxT(".*") )
203 return true ;
204
205 {
206 if ( type == (OSType)data->filtermactypes[i] )
207 return true ;
208
209 wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
210 while ( tokenizer.HasMoreTokens() )
211 {
212 wxString extension = tokenizer.GetNextToken() ;
213 if ( extension.GetChar(0) == '*' )
214 extension = extension.Mid(1) ;
215
216 if ( file.length() >= extension.length() && extension == file.Right(extension.length() ) )
217 return true ;
218 }
219 }
220
221 return false ;
222 }
223
224 return true ;
225 }
226
227 #if !TARGET_API_MAC_OSX
228 static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
229 {
230 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
231 // return true if this item is invisible or a file
232
233 Boolean visibleFlag;
234 Boolean folderFlag;
235
236 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
237 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
238
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
241
242 if ( !visibleFlag )
243 return true ;
244
245 if ( !folderFlag )
246 {
247 wxString file = wxMacMakeStringFromPascal( myCInfoPBPtr->hFileInfo.ioNamePtr ) ;
248 return !CheckFile( file , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
249 }
250
251 return false ;
252 }
253 #endif
254
255 // end wxmac
256
257 wxFileDialog::wxFileDialog(
258 wxWindow *parent, const wxString& message,
259 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
260 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
261 : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
262 {
263 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
264 }
265
266 pascal Boolean CrossPlatformFilterCallback(
267 AEDesc *theItem,
268 void *info,
269 void *callBackUD,
270 NavFilterModes filterMode )
271 {
272 bool display = true;
273 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
274
275 if (filterMode == kNavFilteringBrowserList)
276 {
277 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
278 if ( !theInfo->isFolder )
279 {
280 AECoerceDesc (theItem, typeFSRef, theItem);
281
282 FSRef fsref ;
283 if ( AEGetDescData (theItem, &fsref, sizeof (FSRef)) == noErr )
284 {
285 memcpy( &fsref , *theItem->dataHandle , sizeof(FSRef) ) ;
286 wxString file = wxMacFSRefToPath( &fsref ) ;
287 display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
288 }
289 }
290 }
291
292 return display;
293 }
294
295 int wxFileDialog::ShowModal()
296 {
297 OSErr err;
298 NavDialogCreationOptions dialogCreateOptions;
299
300 // set default options
301 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
302
303 // this was always unset in the old code
304 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
305
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
313 NavDialogRef dialog;
314 NavObjectFilterUPP navFilterUPP = NULL;
315 OpenUserDataRec myData;
316 myData.defaultLocation = m_dir;
317
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
333 if (HasFdFlag(wxFD_SAVE))
334 {
335 myData.saveMode = true;
336
337 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
338 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
339 if (!numFilters)
340 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
341
342 // The extension is important
343 if (numFilters < 2)
344 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
345
346 #if TARGET_API_MAC_OSX
347 if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT))
348 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
349 #endif
350
351 err = ::NavCreatePutFileDialog(
352 &dialogCreateOptions,
353 kNavGenericSignature, // Suppresses the 'Default' (top) menu item
354 kNavGenericSignature,
355 sStandardNavEventFilter,
356 &myData, // for defaultLocation
357 &dialog );
358 }
359 else
360 {
361 // let the user select bundles/programs in dialogs
362 dialogCreateOptions.optionFlags |= kNavSupportPackages;
363
364 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
365 err = ::NavCreateGetFileDialog(
366 &dialogCreateOptions,
367 NULL, // NavTypeListHandle
368 sStandardNavEventFilter,
369 NULL, // NavPreviewUPP
370 navFilterUPP,
371 (void *) &myData, // inClientData
372 &dialog );
373 }
374
375 if (err == noErr)
376 err = ::NavDialogRun(dialog);
377
378 // clean up filter related data, etc.
379 if (navFilterUPP)
380 ::DisposeNavObjectFilterUPP(navFilterUPP);
381
382 if (err != noErr)
383 return wxID_CANCEL;
384
385 NavReplyRecord navReply;
386 err = ::NavDialogGetReply(dialog, &navReply);
387 if (err == noErr && navReply.validRecord)
388 {
389 AEKeyword theKeyword;
390 DescType actualType;
391 Size actualSize;
392 FSRef theFSRef;
393 wxString thePath ;
394 long count;
395
396 m_filterIndex = myData.currentfilter;
397 ::AECountItems( &navReply.selection, &count );
398 for (long i = 1; i <= count; ++i)
399 {
400 err = ::AEGetNthPtr(
401 &(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
402 &theFSRef, sizeof(theFSRef), &actualSize );
403 if (err != noErr)
404 break;
405
406 if (HasFdFlag(wxFD_SAVE))
407 thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName );
408 else
409 thePath = wxMacFSRefToPath( &theFSRef );
410
411 if (!thePath)
412 {
413 ::NavDisposeReply(&navReply);
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
431 return (err == noErr) ? wxID_OK : wxID_CANCEL;
432 }
433
434 #endif // wxUSE_FILEDLG
435