]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/filedlg.cpp
added test for env var expansion
[wxWidgets.git] / src / mac / carbon / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: filedlg.cpp
3 // Purpose: wxFileDialog
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "filedlg.h"
14 #endif
15
16 #include "wx/defs.h"
17 #include "wx/app.h"
18 #include "wx/utils.h"
19 #include "wx/dialog.h"
20 #include "wx/filedlg.h"
21 #include "wx/intl.h"
22 #include "wx/tokenzr.h"
23
24 #ifndef __DARWIN__
25 #include "PLStringFuncs.h"
26 #endif
27
28 #if !USE_SHARED_LIBRARY
29 IMPLEMENT_CLASS(wxFileDialog, wxDialog)
30 #endif
31
32 // begin wxmac
33
34 #include "wx/mac/private.h"
35
36 #include <Navigation.h>
37
38 #include "MoreFiles.h"
39 #include "MoreFilesExtras.h"
40
41 extern bool gUseNavServices ;
42
43 // the data we need to pass to our standard file hook routine
44 // includes a pointer to the dialog, a pointer to the standard
45 // file reply record (so we can inspect the current selection)
46 // and a copy of the "previous" file spec of the reply record
47 // so we can see if the selection has changed
48
49 const int kwxMacFileTypes = 10 ;
50
51 struct OpenUserDataRec {
52 int currentfilter ;
53 wxString name [kwxMacFileTypes] ;
54 wxString extensions[kwxMacFileTypes] ;
55 OSType filtermactypes[kwxMacFileTypes] ;
56 int numfilters ;
57 };
58 typedef struct OpenUserDataRec
59 OpenUserDataRec, *OpenUserDataRecPtr;
60
61 static pascal void NavEventProc(
62 NavEventCallbackMessage inSelector,
63 NavCBRecPtr ioParams,
64 NavCallBackUserData ioUserData);
65
66 #if TARGET_CARBON
67 static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc);
68 #else
69 static NavEventUPP sStandardNavEventFilter = NewNavEventProc(NavEventProc);
70 #endif
71
72 static pascal void
73 NavEventProc(
74 NavEventCallbackMessage inSelector,
75 NavCBRecPtr ioParams,
76 NavCallBackUserData ioUserData )
77 {
78 OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ;
79 if (inSelector == kNavCBEvent) {
80 // In Universal Headers 3.2, Apple changed the definition of
81 /*
82 #if UNIVERSAL_INTERFACES_VERSION >= 0x0320 // Universal Headers 3.2
83 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.eventDataParms.event));
84
85 #else
86 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.event));
87 #endif
88 */
89
90 wxTheApp->MacHandleOneEvent(ioParams->eventData.eventDataParms.event);
91 } else if ( inSelector == kNavCBPopupMenuSelect )
92 {
93 NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
94 data->currentfilter = menu->menuType ;
95 }
96 }
97
98 const char * gfilters[] =
99 {
100 "*.TXT" ,
101 "*.TIF" ,
102 "*.JPG" ,
103
104 NULL
105 } ;
106
107 OSType gfiltersmac[] =
108 {
109 'TEXT' ,
110 'TIFF' ,
111 'JPEG' ,
112
113 '****'
114 } ;
115
116
117
118 void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
119 {
120 myData->currentfilter = 0 ;
121
122 if ( filter && filter[0] )
123 {
124 wxString filter2(filter) ;
125 int filterIndex = 0;
126 bool isName = true ;
127 wxString current ;
128 for( unsigned int i = 0; i < filter2.Len(); i++ )
129 {
130 if( filter2.GetChar(i) == wxT('|') )
131 {
132 if( isName ) {
133 myData->name[filterIndex] = current ;
134 }
135 else {
136 myData->extensions[filterIndex] = current.MakeUpper() ;
137 ++filterIndex ;
138 }
139 isName = !isName ;
140 current = "" ;
141 }
142 else
143 {
144 current += filter2.GetChar(i) ;
145 }
146 }
147 // we allow for compatibility reason to have a single filter expression (like *.*) without
148 // an explanatory text, in that case the first part is name and extension at the same time
149
150 wxASSERT_MSG( filterIndex == 0 || !isName , "incorrect format of format string" ) ;
151 if ( current.IsEmpty() )
152 myData->extensions[filterIndex] = myData->name[filterIndex] ;
153 else
154 myData->extensions[filterIndex] = current.MakeUpper() ;
155 ++filterIndex ;
156
157
158 myData->numfilters = filterIndex ;
159 for ( int i = 0 ; i < myData->numfilters ; i++ )
160 {
161 int j ;
162 for ( j = 0 ; gfilters[j] ; j++ )
163 {
164 if ( strcmp( myData->extensions[i] , gfilters[j] ) == 0 )
165 {
166 myData->filtermactypes[i] = gfiltersmac[j] ;
167 break ;
168 }
169 }
170 if( gfilters[j] == NULL )
171 {
172 myData->filtermactypes[i] = '****' ;
173 }
174 }
175 }
176 else
177 {
178 myData->numfilters = 0 ;
179 }
180
181 }
182
183 static Boolean CheckFile( ConstStr255Param name , OSType type , OpenUserDataRecPtr data)
184 {
185 Str255 filename ;
186
187 #if TARGET_CARBON
188 p2cstrcpy((char *)filename, name) ;
189 #else
190 PLstrcpy( filename , name ) ;
191 p2cstr( filename ) ;
192 #endif
193 wxString file(filename) ;
194 file.MakeUpper() ;
195
196 if ( data->numfilters > 0 )
197 {
198 //for ( int i = 0 ; i < data->numfilters ; ++i )
199 int i = data->currentfilter ;
200 if ( data->extensions[i].Right(2) == ".*" )
201 return true ;
202
203 {
204 if ( type == data->filtermactypes[i] )
205 return true ;
206
207 wxStringTokenizer tokenizer( data->extensions[i] , ";" ) ;
208 while( tokenizer.HasMoreTokens() )
209 {
210 wxString extension = tokenizer.GetNextToken() ;
211 if ( extension.GetChar(0) == '*' )
212 extension = extension.Mid(1) ;
213
214 if ( file.Len() >= extension.Len() && extension == file.Right(extension.Len() ) )
215 return true ;
216 }
217 }
218 return false ;
219 }
220 return true ;
221 }
222
223 #ifndef __DARWIN__
224 static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
225 {
226 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
227 // return true if this item is invisible or a file
228
229 Boolean visibleFlag;
230 Boolean folderFlag;
231
232 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
233 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
234
235 // because the semantics of the filter proc are "true means don't show
236 // it" we need to invert the result that we return
237
238 if ( !visibleFlag )
239 return true ;
240
241 if ( !folderFlag )
242 {
243 return !CheckFile( myCInfoPBPtr->hFileInfo.ioNamePtr , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
244 }
245
246 return false ;
247 }
248 #endif
249
250 // end wxmac
251
252 wxString wxFileSelector(const char *title,
253 const char *defaultDir, const char *defaultFileName,
254 const char *defaultExtension, const char *filter, int flags,
255 wxWindow *parent, int x, int y)
256 {
257 // If there's a default extension specified but no filter, we create a suitable
258 // filter.
259
260 wxString filter2("");
261 if ( defaultExtension && !filter )
262 filter2 = wxString("*.") + wxString(defaultExtension) ;
263 else if ( filter )
264 filter2 = filter;
265
266 wxString defaultDirString;
267 if (defaultDir)
268 defaultDirString = defaultDir;
269 else
270 defaultDirString = "";
271
272 wxString defaultFilenameString;
273 if (defaultFileName)
274 defaultFilenameString = defaultFileName;
275 else
276 defaultFilenameString = "";
277
278 wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y));
279
280 if ( fileDialog.ShowModal() == wxID_OK )
281 {
282 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
283 return wxBuffer;
284 }
285 else
286 return wxGetEmptyString();
287 }
288
289 WXDLLEXPORT wxString wxFileSelectorEx(const char *title,
290 const char *defaultDir,
291 const char *defaultFileName,
292 int* defaultFilterIndex,
293 const char *filter,
294 int flags,
295 wxWindow* parent,
296 int x,
297 int y)
298
299 {
300 wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "",
301 defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y));
302
303 if ( fileDialog.ShowModal() == wxID_OK )
304 {
305 *defaultFilterIndex = fileDialog.GetFilterIndex();
306 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
307 return wxBuffer;
308 }
309 else
310 return wxGetEmptyString();
311 }
312
313 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
314 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
315 long style, const wxPoint& pos)
316 {
317 wxASSERT_MSG( NavServicesAvailable() , "Navigation Services are not running" ) ;
318 m_message = message;
319 m_dialogStyle = style;
320 m_parent = parent;
321 m_path = "";
322 m_fileName = defaultFileName;
323 m_dir = defaultDir;
324 m_wildCard = wildCard;
325 m_filterIndex = 1;
326 }
327
328
329 pascal Boolean CrossPlatformFilterCallback (
330 AEDesc *theItem,
331 void *info,
332 void *callBackUD,
333 NavFilterModes filterMode
334 )
335 {
336 bool display = true;
337 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
338
339 if (filterMode == kNavFilteringBrowserList)
340 {
341 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
342 if (theItem->descriptorType == typeFSS && !theInfo->isFolder)
343 {
344 FSSpec spec;
345 memcpy( &spec , *theItem->dataHandle , sizeof(FSSpec) ) ;
346 display = CheckFile( spec.name , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
347 }
348 }
349
350 return display;
351 }
352
353 int wxFileDialog::ShowModal()
354 {
355 NavDialogOptions mNavOptions;
356 NavObjectFilterUPP mNavFilterUPP = NULL;
357 NavPreviewUPP mNavPreviewUPP = NULL ;
358 NavReplyRecord mNavReply;
359 AEDesc mDefaultLocation ;
360 bool mSelectDefault = false ;
361
362 ::NavGetDefaultDialogOptions(&mNavOptions);
363
364 mNavFilterUPP = nil;
365 mNavPreviewUPP = nil;
366 mSelectDefault = false;
367 mNavReply.validRecord = false;
368 mNavReply.replacing = false;
369 mNavReply.isStationery = false;
370 mNavReply.translationNeeded = false;
371 mNavReply.selection.descriptorType = typeNull;
372 mNavReply.selection.dataHandle = nil;
373 mNavReply.keyScript = smSystemScript;
374 mNavReply.fileTranslation = nil;
375
376 // Set default location, the location
377 // that's displayed when the dialog
378 // first appears
379
380 FSSpec location ;
381 wxMacFilename2FSSpec( m_dir , &location ) ;
382 OSErr err = noErr ;
383
384 mDefaultLocation.descriptorType = typeNull;
385 mDefaultLocation.dataHandle = nil;
386
387 err = ::AECreateDesc(typeFSS, &location, sizeof(FSSpec), &mDefaultLocation );
388
389 if ( mDefaultLocation.dataHandle ) {
390
391 if (mSelectDefault) {
392 mNavOptions.dialogOptionFlags |= kNavSelectDefaultLocation;
393 } else {
394 mNavOptions.dialogOptionFlags &= ~kNavSelectDefaultLocation;
395 }
396 }
397
398 #if TARGET_CARBON
399 c2pstrcpy((StringPtr)mNavOptions.message, m_message) ;
400 #else
401 strcpy((char *)mNavOptions.message, m_message) ;
402 c2pstr((char *)mNavOptions.message ) ;
403 #endif
404 #if TARGET_CARBON
405 c2pstrcpy((StringPtr)mNavOptions.savedFileName, m_fileName) ;
406 #else
407 strcpy((char *)mNavOptions.savedFileName, m_fileName) ;
408 c2pstr((char *)mNavOptions.savedFileName ) ;
409 #endif
410
411 if ( m_dialogStyle & wxSAVE )
412 {
413
414 mNavOptions.dialogOptionFlags |= kNavNoTypePopup ;
415 mNavOptions.dialogOptionFlags |= kNavDontAutoTranslate ;
416 mNavOptions.dialogOptionFlags |= kNavDontAddTranslateItems ;
417
418 err = ::NavPutFile(
419 &mDefaultLocation,
420 &mNavReply,
421 &mNavOptions,
422 sStandardNavEventFilter ,
423 'TEXT',
424 'TEXT',
425 0L); // User Data
426 }
427 else
428 {
429 OpenUserDataRec myData;
430 MakeUserDataRec( &myData , m_wildCard ) ;
431 NavTypeListHandle typelist = NULL ;
432
433 if ( myData.numfilters > 0 )
434 {
435 mNavOptions.popupExtension = (NavMenuItemSpecArrayHandle) NewHandle( sizeof( NavMenuItemSpec ) * myData.numfilters ) ;
436 for ( int i = 0 ; i < myData.numfilters ; ++i ) {
437 (*mNavOptions.popupExtension)[i].version = kNavMenuItemSpecVersion ;
438 (*mNavOptions.popupExtension)[i].menuCreator = 'WXNG' ;
439 (*mNavOptions.popupExtension)[i].menuType = i ;
440 #if TARGET_CARBON
441 c2pstrcpy((StringPtr)(*mNavOptions.popupExtension)[i].menuItemName, myData.name[i]) ;
442 #else
443 strcpy((char *)(*mNavOptions.popupExtension)[i].menuItemName, myData.name[i]) ;
444 c2pstr((char *)(*mNavOptions.popupExtension)[i].menuItemName ) ;
445 #endif
446 }
447 }
448
449 mNavFilterUPP = NewNavObjectFilterUPP( CrossPlatformFilterCallback ) ;
450 if ( m_dialogStyle & wxMULTIPLE )
451 mNavOptions.dialogOptionFlags |= kNavAllowMultipleFiles ;
452 else
453 mNavOptions.dialogOptionFlags &= ~kNavAllowMultipleFiles ;
454
455 err = ::NavGetFile(
456 &mDefaultLocation,
457 &mNavReply,
458 &mNavOptions,
459 sStandardNavEventFilter ,
460 mNavPreviewUPP,
461 mNavFilterUPP,
462 typelist /*inFileTypes.TypeListHandle() */,
463 &myData); // User Data
464 if ( typelist )
465 DisposeHandle( (Handle) typelist ) ;
466 }
467
468 DisposeNavObjectFilterUPP(mNavFilterUPP);
469 if ( mDefaultLocation.dataHandle != nil )
470 {
471 ::AEDisposeDesc(&mDefaultLocation);
472 }
473
474 if ( (err != noErr) && (err != userCanceledErr) ) {
475 m_path = "" ;
476 return wxID_CANCEL ;
477 }
478
479 if (mNavReply.validRecord) {
480
481 FSSpec outFileSpec ;
482 AEDesc specDesc ;
483 AEKeyword keyWord ;
484
485 long count ;
486 ::AECountItems( &mNavReply.selection , &count ) ;
487 for ( long i = 1 ; i <= count ; ++i )
488 {
489 OSErr err = ::AEGetNthDesc( &mNavReply.selection , i , typeFSS, &keyWord , &specDesc);
490 if ( err != noErr ) {
491 m_path = "" ;
492 return wxID_CANCEL ;
493 }
494 outFileSpec = **(FSSpec**) specDesc.dataHandle;
495 if (specDesc.dataHandle != nil) {
496 ::AEDisposeDesc(&specDesc);
497 }
498 m_path = wxMacFSSpec2MacFilename( &outFileSpec ) ;
499 m_paths.Add( m_path ) ;
500 m_fileName = wxFileNameFromPath(m_path);
501 m_fileNames.Add(m_fileName);
502 }
503 // set these to the first hit
504 m_path = m_paths[ 0 ] ;
505 m_fileName = wxFileNameFromPath(m_path);
506 m_dir = wxPathOnly(m_path);
507 NavDisposeReply( &mNavReply ) ;
508 return wxID_OK ;
509 }
510 return wxID_CANCEL;
511 }
512
513 // Generic file load/save dialog
514 static wxString
515 wxDefaultFileSelector(bool load, const char *what, const char *extension, const char *default_name, wxWindow *parent)
516 {
517 char *ext = (char *)extension;
518
519 char prompt[50];
520 wxString str;
521 if (load)
522 str = "Load %s file";
523 else
524 str = "Save %s file";
525 sprintf(prompt, wxGetTranslation(str), what);
526
527 if (*ext == '.') ext++;
528 char wild[60];
529 sprintf(wild, "*.%s", ext);
530
531 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
532 }
533
534 // Generic file load dialog
535 wxString
536 wxLoadFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
537 {
538 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
539 }
540
541
542 // Generic file save dialog
543 wxString
544 wxSaveFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
545 {
546 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
547 }
548
549