]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/filedlg.cpp
fixed inheritance info
[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 // if ( filterIndex > 0 )
148 {
149 wxASSERT_MSG( !isName , "incorrect format of format string" ) ;
150 myData->extensions[filterIndex] = current.MakeUpper() ;
151 ++filterIndex ;
152 }
153
154 myData->numfilters = filterIndex ;
155 for ( int i = 0 ; i < myData->numfilters ; i++ )
156 {
157 int j ;
158 for ( j = 0 ; gfilters[j] ; j++ )
159 {
160 if ( strcmp( myData->extensions[i] , gfilters[j] ) == 0 )
161 {
162 myData->filtermactypes[i] = gfiltersmac[j] ;
163 break ;
164 }
165 }
166 if( gfilters[j] == NULL )
167 {
168 myData->filtermactypes[i] = '****' ;
169 }
170 }
171 }
172 else
173 {
174 myData->numfilters = 0 ;
175 }
176
177 }
178
179 static Boolean CheckFile( ConstStr255Param name , OSType type , OpenUserDataRecPtr data)
180 {
181 Str255 filename ;
182
183 #if TARGET_CARBON
184 p2cstrcpy((char *)filename, name) ;
185 #else
186 PLstrcpy( filename , name ) ;
187 p2cstr( filename ) ;
188 #endif
189 wxString file(filename) ;
190 file.MakeUpper() ;
191
192 if ( data->numfilters > 0 )
193 {
194 //for ( int i = 0 ; i < data->numfilters ; ++i )
195 int i = data->currentfilter ;
196 if ( data->extensions[i].Right(2) == ".*" )
197 return true ;
198
199 {
200 if ( type == data->filtermactypes[i] )
201 return true ;
202
203 wxStringTokenizer tokenizer( data->extensions[i] , ";" ) ;
204 while( tokenizer.HasMoreTokens() )
205 {
206 wxString extension = tokenizer.GetNextToken() ;
207 if ( extension.GetChar(0) == '*' )
208 extension = extension.Mid(1) ;
209
210 if ( file.Len() >= extension.Len() && extension == file.Right(extension.Len() ) )
211 return true ;
212 }
213 }
214 return false ;
215 }
216 return true ;
217 }
218
219 #ifndef __DARWIN__
220 static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
221 {
222 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
223 // return true if this item is invisible or a file
224
225 Boolean visibleFlag;
226 Boolean folderFlag;
227
228 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
229 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
230
231 // because the semantics of the filter proc are "true means don't show
232 // it" we need to invert the result that we return
233
234 if ( !visibleFlag )
235 return true ;
236
237 if ( !folderFlag )
238 {
239 return !CheckFile( myCInfoPBPtr->hFileInfo.ioNamePtr , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
240 }
241
242 return false ;
243 }
244 #endif
245
246 // end wxmac
247
248 wxString wxFileSelector(const char *title,
249 const char *defaultDir, const char *defaultFileName,
250 const char *defaultExtension, const char *filter, int flags,
251 wxWindow *parent, int x, int y)
252 {
253 // If there's a default extension specified but no filter, we create a suitable
254 // filter.
255
256 wxString filter2("");
257 if ( defaultExtension && !filter )
258 filter2 = wxString("*.") + wxString(defaultExtension) ;
259 else if ( filter )
260 filter2 = filter;
261
262 wxString defaultDirString;
263 if (defaultDir)
264 defaultDirString = defaultDir;
265 else
266 defaultDirString = "";
267
268 wxString defaultFilenameString;
269 if (defaultFileName)
270 defaultFilenameString = defaultFileName;
271 else
272 defaultFilenameString = "";
273
274 wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y));
275
276 if ( fileDialog.ShowModal() == wxID_OK )
277 {
278 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
279 return wxBuffer;
280 }
281 else
282 return wxGetEmptyString();
283 }
284
285 WXDLLEXPORT wxString wxFileSelectorEx(const char *title,
286 const char *defaultDir,
287 const char *defaultFileName,
288 int* defaultFilterIndex,
289 const char *filter,
290 int flags,
291 wxWindow* parent,
292 int x,
293 int y)
294
295 {
296 wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "",
297 defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y));
298
299 if ( fileDialog.ShowModal() == wxID_OK )
300 {
301 *defaultFilterIndex = fileDialog.GetFilterIndex();
302 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
303 return wxBuffer;
304 }
305 else
306 return wxGetEmptyString();
307 }
308
309 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
310 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
311 long style, const wxPoint& pos)
312 {
313 wxASSERT_MSG( NavServicesAvailable() , "Navigation Services are not running" ) ;
314 m_message = message;
315 m_dialogStyle = style;
316 m_parent = parent;
317 m_path = "";
318 m_fileName = defaultFileName;
319 m_dir = defaultDir;
320 m_wildCard = wildCard;
321 m_filterIndex = 1;
322 }
323
324
325 pascal Boolean CrossPlatformFilterCallback (
326 AEDesc *theItem,
327 void *info,
328 void *callBackUD,
329 NavFilterModes filterMode
330 )
331 {
332 bool display = true;
333 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
334
335 if (filterMode == kNavFilteringBrowserList)
336 {
337 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
338 if (theItem->descriptorType == typeFSS && !theInfo->isFolder)
339 {
340 FSSpec spec;
341 memcpy( &spec , *theItem->dataHandle , sizeof(FSSpec) ) ;
342 display = CheckFile( spec.name , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
343 }
344 }
345
346 return display;
347 }
348
349 int wxFileDialog::ShowModal()
350 {
351 NavDialogOptions mNavOptions;
352 NavObjectFilterUPP mNavFilterUPP = NULL;
353 NavPreviewUPP mNavPreviewUPP = NULL ;
354 NavReplyRecord mNavReply;
355 AEDesc mDefaultLocation ;
356 bool mSelectDefault = false ;
357
358 ::NavGetDefaultDialogOptions(&mNavOptions);
359
360 mNavFilterUPP = nil;
361 mNavPreviewUPP = nil;
362 mSelectDefault = false;
363 mNavReply.validRecord = false;
364 mNavReply.replacing = false;
365 mNavReply.isStationery = false;
366 mNavReply.translationNeeded = false;
367 mNavReply.selection.descriptorType = typeNull;
368 mNavReply.selection.dataHandle = nil;
369 mNavReply.keyScript = smSystemScript;
370 mNavReply.fileTranslation = nil;
371
372 // Set default location, the location
373 // that's displayed when the dialog
374 // first appears
375
376 FSSpec location ;
377 wxMacFilename2FSSpec( m_dir , &location ) ;
378 OSErr err = noErr ;
379
380 mDefaultLocation.descriptorType = typeNull;
381 mDefaultLocation.dataHandle = nil;
382
383 err = ::AECreateDesc(typeFSS, &location, sizeof(FSSpec), &mDefaultLocation );
384
385 if ( mDefaultLocation.dataHandle ) {
386
387 if (mSelectDefault) {
388 mNavOptions.dialogOptionFlags |= kNavSelectDefaultLocation;
389 } else {
390 mNavOptions.dialogOptionFlags &= ~kNavSelectDefaultLocation;
391 }
392 }
393
394 #if TARGET_CARBON
395 c2pstrcpy((StringPtr)mNavOptions.message, m_message) ;
396 #else
397 strcpy((char *)mNavOptions.message, m_message) ;
398 c2pstr((char *)mNavOptions.message ) ;
399 #endif
400 #if TARGET_CARBON
401 c2pstrcpy((StringPtr)mNavOptions.savedFileName, m_fileName) ;
402 #else
403 strcpy((char *)mNavOptions.savedFileName, m_fileName) ;
404 c2pstr((char *)mNavOptions.savedFileName ) ;
405 #endif
406
407 if ( m_dialogStyle & wxSAVE )
408 {
409
410 mNavOptions.dialogOptionFlags |= kNavNoTypePopup ;
411 mNavOptions.dialogOptionFlags |= kNavDontAutoTranslate ;
412 mNavOptions.dialogOptionFlags |= kNavDontAddTranslateItems ;
413
414 err = ::NavPutFile(
415 &mDefaultLocation,
416 &mNavReply,
417 &mNavOptions,
418 sStandardNavEventFilter ,
419 'TEXT',
420 'TEXT',
421 0L); // User Data
422 }
423 else
424 {
425 OpenUserDataRec myData;
426 MakeUserDataRec( &myData , m_wildCard ) ;
427 NavTypeListHandle typelist = NULL ;
428
429 if ( myData.numfilters > 0 )
430 {
431 mNavOptions.popupExtension = (NavMenuItemSpecArrayHandle) NewHandle( sizeof( NavMenuItemSpec ) * myData.numfilters ) ;
432 for ( int i = 0 ; i < myData.numfilters ; ++i ) {
433 (*mNavOptions.popupExtension)[i].version = kNavMenuItemSpecVersion ;
434 (*mNavOptions.popupExtension)[i].menuCreator = 'WXNG' ;
435 (*mNavOptions.popupExtension)[i].menuType = i ;
436 #if TARGET_CARBON
437 c2pstrcpy((StringPtr)(*mNavOptions.popupExtension)[i].menuItemName, myData.name[i]) ;
438 #else
439 strcpy((char *)(*mNavOptions.popupExtension)[i].menuItemName, myData.name[i]) ;
440 c2pstr((char *)(*mNavOptions.popupExtension)[i].menuItemName ) ;
441 #endif
442 }
443 }
444
445 mNavFilterUPP = NewNavObjectFilterUPP( CrossPlatformFilterCallback ) ;
446 if ( m_dialogStyle & wxMULTIPLE )
447 mNavOptions.dialogOptionFlags |= kNavAllowMultipleFiles ;
448 else
449 mNavOptions.dialogOptionFlags &= ~kNavAllowMultipleFiles ;
450
451 err = ::NavGetFile(
452 &mDefaultLocation,
453 &mNavReply,
454 &mNavOptions,
455 sStandardNavEventFilter ,
456 mNavPreviewUPP,
457 mNavFilterUPP,
458 typelist /*inFileTypes.TypeListHandle() */,
459 &myData); // User Data
460 if ( typelist )
461 DisposeHandle( (Handle) typelist ) ;
462 }
463
464 DisposeNavObjectFilterUPP(mNavFilterUPP);
465 if ( mDefaultLocation.dataHandle != nil )
466 {
467 ::AEDisposeDesc(&mDefaultLocation);
468 }
469
470 if ( (err != noErr) && (err != userCanceledErr) ) {
471 m_path = "" ;
472 return wxID_CANCEL ;
473 }
474
475 if (mNavReply.validRecord) {
476
477 FSSpec outFileSpec ;
478 AEDesc specDesc ;
479 AEKeyword keyWord ;
480
481 long count ;
482 ::AECountItems( &mNavReply.selection , &count ) ;
483 for ( long i = 1 ; i <= count ; ++i )
484 {
485 OSErr err = ::AEGetNthDesc( &mNavReply.selection , i , typeFSS, &keyWord , &specDesc);
486 if ( err != noErr ) {
487 m_path = "" ;
488 return wxID_CANCEL ;
489 }
490 outFileSpec = **(FSSpec**) specDesc.dataHandle;
491 if (specDesc.dataHandle != nil) {
492 ::AEDisposeDesc(&specDesc);
493 }
494
495
496 // outFolderDirID = thePB.dirInfo.ioDrDirID;
497 m_path = wxMacFSSpec2MacFilename( &outFileSpec ) ;
498 m_paths.Add( m_path ) ;
499 m_fileNames.Add(m_fileName);
500 }
501 // set these to the first hit
502 m_path = m_paths[ 0 ] ;
503 m_fileName = wxFileNameFromPath(m_path);
504 m_dir = wxPathOnly(m_path);
505 NavDisposeReply( &mNavReply ) ;
506 return wxID_OK ;
507 }
508 return wxID_CANCEL;
509 }
510
511 // Generic file load/save dialog
512 static wxString
513 wxDefaultFileSelector(bool load, const char *what, const char *extension, const char *default_name, wxWindow *parent)
514 {
515 char *ext = (char *)extension;
516
517 char prompt[50];
518 wxString str;
519 if (load)
520 str = "Load %s file";
521 else
522 str = "Save %s file";
523 sprintf(prompt, wxGetTranslation(str), what);
524
525 if (*ext == '.') ext++;
526 char wild[60];
527 sprintf(wild, "*.%s", ext);
528
529 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
530 }
531
532 // Generic file load dialog
533 wxString
534 wxLoadFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
535 {
536 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
537 }
538
539
540 // Generic file save dialog
541 wxString
542 wxSaveFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
543 {
544 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
545 }
546
547