]> git.saurik.com Git - wxWidgets.git/blob - src/mac/filedlg.cpp
added support for range types to runtime depersister
[wxWidgets.git] / src / mac / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #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, wxFileDialogBase)
30 #endif
31
32 // begin wxmac
33
34 #include "wx/mac/private.h"
35
36 #include <Navigation.h>
37
38 #ifdef __DARWIN__
39 # include "MoreFilesX.h"
40 #else
41 # include "MoreFiles.h"
42 # include "MoreFilesExtras.h"
43 #endif
44
45 extern bool gUseNavServices ;
46
47 // the data we need to pass to our standard file hook routine
48 // includes a pointer to the dialog, a pointer to the standard
49 // file reply record (so we can inspect the current selection)
50 // and a copy of the "previous" file spec of the reply record
51 // so we can see if the selection has changed
52
53 struct OpenUserDataRec {
54 int currentfilter ;
55 bool saveMode ;
56 wxArrayString name ;
57 wxArrayString extensions ;
58 wxArrayLong filtermactypes ;
59 NavMenuItemSpecArrayHandle menuitems ;
60 };
61
62 typedef struct OpenUserDataRec
63 OpenUserDataRec, *OpenUserDataRecPtr;
64
65 static pascal void NavEventProc(
66 NavEventCallbackMessage inSelector,
67 NavCBRecPtr ioParams,
68 NavCallBackUserData ioUserData);
69
70 #if TARGET_CARBON
71 static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc);
72 #else
73 static NavEventUPP sStandardNavEventFilter = NewNavEventProc(NavEventProc);
74 #endif
75
76 static pascal void
77 NavEventProc(
78 NavEventCallbackMessage inSelector,
79 NavCBRecPtr ioParams,
80 NavCallBackUserData ioUserData )
81 {
82 OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ;
83 if (inSelector == kNavCBEvent) {
84 #if !TARGET_CARBON
85 wxTheApp->MacHandleOneEvent(ioParams->eventData.eventDataParms.event);
86 #endif
87 }
88 else if ( inSelector == kNavCBStart )
89 {
90 if ( data->menuitems )
91 NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &(*data->menuitems)[data->currentfilter]);
92 }
93 else if ( inSelector == kNavCBPopupMenuSelect )
94 {
95 NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
96 if ( menu->menuCreator == 'WXNG' )
97 {
98 data->currentfilter = menu->menuType ;
99 if ( data->saveMode )
100 {
101 int i = menu->menuType ;
102 wxString extension = data->extensions[i].AfterLast('.') ;
103 extension.MakeLower() ;
104 Str255 filename ;
105 // get the current filename
106 NavCustomControl(ioParams->context, kNavCtlGetEditFileName, &filename);
107 wxString sfilename = wxMacMakeStringFromPascal( filename ) ;
108 int pos = sfilename.Find('.',TRUE) ;
109 if ( pos != wxNOT_FOUND )
110 {
111 sfilename = sfilename.Left(pos+1)+extension ;
112 wxMacStringToPascal( sfilename , filename ) ;
113 NavCustomControl(ioParams->context, kNavCtlSetEditFileName, &filename);
114 }
115 }
116 }
117 }
118 }
119
120 const wxChar * gfilters[] =
121 {
122 wxT("*.TXT") ,
123 wxT("*.TIF") ,
124 wxT("*.JPG") ,
125
126 NULL
127 } ;
128
129 OSType gfiltersmac[] =
130 {
131 'TEXT' ,
132 'TIFF' ,
133 'JPEG' ,
134
135 '****'
136 } ;
137
138
139
140 void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
141 {
142 myData->menuitems = NULL ;
143 myData->currentfilter = 0 ;
144 myData->saveMode = FALSE ;
145
146 if ( filter && filter[0] )
147 {
148 wxString filter2(filter) ;
149 int filterIndex = 0;
150 bool isName = true ;
151 wxString current ;
152 for( unsigned int i = 0; i < filter2.Len() ; i++ )
153 {
154 if( filter2.GetChar(i) == wxT('|') )
155 {
156 if( isName ) {
157 myData->name.Add( current ) ;
158 }
159 else {
160 myData->extensions.Add( current.MakeUpper() ) ;
161 ++filterIndex ;
162 }
163 isName = !isName ;
164 current = wxEmptyString ;
165 }
166 else
167 {
168 current += filter2.GetChar(i) ;
169 }
170 }
171 // we allow for compatibility reason to have a single filter expression (like *.*) without
172 // an explanatory text, in that case the first part is name and extension at the same time
173
174 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
175 if ( current.IsEmpty() )
176 myData->extensions.Add( myData->name[filterIndex] ) ;
177 else
178 myData->extensions.Add( current.MakeUpper() ) ;
179 if ( filterIndex == 0 || isName )
180 myData->name.Add( current.MakeUpper() ) ;
181
182 ++filterIndex ;
183
184
185 const size_t extCount = myData->extensions.GetCount();
186 for ( size_t i = 0 ; i < extCount; i++ )
187 {
188 int j ;
189 for ( j = 0 ; gfilters[j] ; j++ )
190 {
191 if ( myData->extensions[i] == gfilters[j] )
192 {
193 myData->filtermactypes.Add( gfiltersmac[j] ) ;
194 break ;
195 }
196 }
197 if( gfilters[j] == NULL )
198 {
199 myData->filtermactypes.Add( '****' ) ;
200 }
201 }
202 }
203 }
204
205 static Boolean CheckFile( ConstStr255Param name , OSType type , OpenUserDataRecPtr data)
206 {
207 /*
208 Str255 filename ;
209
210 #if TARGET_CARBON
211 p2cstrcpy((char *)filename, name) ;
212 #else
213 PLstrcpy( filename , name ) ;
214 p2cstr( filename ) ;
215 #endif
216 wxString file(filename) ;
217 */
218 wxString file = wxMacMakeStringFromPascal( name ) ;
219 file.MakeUpper() ;
220
221 if ( data->extensions.GetCount() > 0 )
222 {
223 //for ( int i = 0 ; i < data->numfilters ; ++i )
224 int i = data->currentfilter ;
225 if ( data->extensions[i].Right(2) == wxT(".*") )
226 return true ;
227
228 {
229 if ( type == (OSType)data->filtermactypes[i] )
230 return true ;
231
232 wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
233 while( tokenizer.HasMoreTokens() )
234 {
235 wxString extension = tokenizer.GetNextToken() ;
236 if ( extension.GetChar(0) == '*' )
237 extension = extension.Mid(1) ;
238
239 if ( file.Len() >= extension.Len() && extension == file.Right(extension.Len() ) )
240 return true ;
241 }
242 }
243 return false ;
244 }
245 return true ;
246 }
247
248 #ifndef __DARWIN__
249 static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
250 {
251 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
252 // return true if this item is invisible or a file
253
254 Boolean visibleFlag;
255 Boolean folderFlag;
256
257 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
258 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
259
260 // because the semantics of the filter proc are "true means don't show
261 // it" we need to invert the result that we return
262
263 if ( !visibleFlag )
264 return true ;
265
266 if ( !folderFlag )
267 {
268 return !CheckFile( myCInfoPBPtr->hFileInfo.ioNamePtr , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
269 }
270
271 return false ;
272 }
273 #endif
274
275 // end wxmac
276
277 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
278 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
279 long style, const wxPoint& pos)
280 :wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos)
281 {
282 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
283 }
284
285 pascal Boolean CrossPlatformFilterCallback (
286 AEDesc *theItem,
287 void *info,
288 void *callBackUD,
289 NavFilterModes filterMode
290 )
291 {
292 bool display = true;
293 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
294
295 if (filterMode == kNavFilteringBrowserList)
296 {
297 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
298 if (theItem->descriptorType == typeFSS && !theInfo->isFolder)
299 {
300 FSSpec spec;
301 memcpy( &spec , *theItem->dataHandle , sizeof(FSSpec) ) ;
302 display = CheckFile( spec.name , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
303 }
304 }
305
306 return display;
307 }
308
309 int wxFileDialog::ShowModal()
310 {
311 NavDialogOptions mNavOptions;
312 NavObjectFilterUPP mNavFilterUPP = NULL;
313 NavPreviewUPP mNavPreviewUPP = NULL ;
314 NavReplyRecord mNavReply;
315 AEDesc mDefaultLocation ;
316 bool mSelectDefault = false ;
317
318 // setup dialog
319
320 ::NavGetDefaultDialogOptions(&mNavOptions);
321
322 mNavFilterUPP = nil;
323 mNavPreviewUPP = nil;
324 mSelectDefault = false;
325 mNavReply.validRecord = false;
326 mNavReply.replacing = false;
327 mNavReply.isStationery = false;
328 mNavReply.translationNeeded = false;
329 mNavReply.selection.descriptorType = typeNull;
330 mNavReply.selection.dataHandle = nil;
331 mNavReply.keyScript = smSystemScript;
332 mNavReply.fileTranslation = nil;
333
334 // Set default location, the location
335 // that's displayed when the dialog
336 // first appears
337
338 FSSpec location ;
339 wxMacFilename2FSSpec( m_dir , &location ) ;
340 OSErr err = noErr ;
341
342 mDefaultLocation.descriptorType = typeNull;
343 mDefaultLocation.dataHandle = nil;
344
345 err = ::AECreateDesc(typeFSS, &location, sizeof(FSSpec), &mDefaultLocation );
346
347 if ( mDefaultLocation.dataHandle ) {
348
349 if (mSelectDefault) {
350 mNavOptions.dialogOptionFlags |= kNavSelectDefaultLocation;
351 } else {
352 mNavOptions.dialogOptionFlags &= ~kNavSelectDefaultLocation;
353 }
354 }
355 wxMacStringToPascal( m_message , (StringPtr)mNavOptions.message ) ;
356 wxMacStringToPascal( m_fileName , (StringPtr)mNavOptions.savedFileName ) ;
357
358 // zero all data
359
360 m_path = wxEmptyString ;
361 m_fileName = wxEmptyString ;
362 m_paths.Empty();
363 m_fileNames.Empty();
364
365 OpenUserDataRec myData;
366 MakeUserDataRec( &myData , m_wildCard ) ;
367 myData.currentfilter = m_filterIndex ;
368 if ( myData.extensions.GetCount() > 0 )
369 {
370 mNavOptions.popupExtension = (NavMenuItemSpecArrayHandle) NewHandle( sizeof( NavMenuItemSpec ) * myData.extensions.GetCount() ) ;
371 myData.menuitems = mNavOptions.popupExtension ;
372 for ( size_t i = 0 ; i < myData.extensions.GetCount() ; ++i )
373 {
374 (*mNavOptions.popupExtension)[i].version = kNavMenuItemSpecVersion ;
375 (*mNavOptions.popupExtension)[i].menuCreator = 'WXNG' ;
376 (*mNavOptions.popupExtension)[i].menuType = i ;
377 wxMacStringToPascal( myData.name[i] , (StringPtr)(*mNavOptions.popupExtension)[i].menuItemName ) ;
378 }
379 }
380 if ( m_dialogStyle & wxSAVE )
381 {
382 myData.saveMode = true ;
383
384 mNavOptions.dialogOptionFlags |= kNavDontAutoTranslate ;
385 mNavOptions.dialogOptionFlags |= kNavDontAddTranslateItems ;
386
387 err = ::NavPutFile(
388 &mDefaultLocation,
389 &mNavReply,
390 &mNavOptions,
391 sStandardNavEventFilter ,
392 NULL,
393 kNavGenericSignature,
394 &myData); // User Data
395 m_filterIndex = myData.currentfilter ;
396 }
397 else
398 {
399 myData.saveMode = false ;
400
401 mNavFilterUPP = NewNavObjectFilterUPP( CrossPlatformFilterCallback ) ;
402 if ( m_dialogStyle & wxMULTIPLE )
403 mNavOptions.dialogOptionFlags |= kNavAllowMultipleFiles ;
404 else
405 mNavOptions.dialogOptionFlags &= ~kNavAllowMultipleFiles ;
406
407 err = ::NavGetFile(
408 &mDefaultLocation,
409 &mNavReply,
410 &mNavOptions,
411 sStandardNavEventFilter ,
412 mNavPreviewUPP,
413 mNavFilterUPP,
414 NULL ,
415 &myData);
416 m_filterIndex = myData.currentfilter ;
417 }
418
419 DisposeNavObjectFilterUPP(mNavFilterUPP);
420 if ( mDefaultLocation.dataHandle != nil )
421 {
422 ::AEDisposeDesc(&mDefaultLocation);
423 }
424
425 if ( (err != noErr) && (err != userCanceledErr) ) {
426 return wxID_CANCEL ;
427 }
428
429 if (mNavReply.validRecord) {
430
431 FSSpec outFileSpec ;
432 AEDesc specDesc ;
433 AEKeyword keyWord ;
434
435 long count ;
436 ::AECountItems( &mNavReply.selection , &count ) ;
437 for ( long i = 1 ; i <= count ; ++i )
438 {
439 OSErr err = ::AEGetNthDesc( &mNavReply.selection , i , typeFSS, &keyWord , &specDesc);
440 if ( err != noErr ) {
441 m_path = wxT("") ;
442 return wxID_CANCEL ;
443 }
444 outFileSpec = **(FSSpec**) specDesc.dataHandle;
445 if (specDesc.dataHandle != nil) {
446 ::AEDisposeDesc(&specDesc);
447 }
448 m_path = wxMacFSSpec2MacFilename( &outFileSpec ) ;
449 m_paths.Add( m_path ) ;
450 m_fileName = wxFileNameFromPath(m_path);
451 m_fileNames.Add(m_fileName);
452 }
453 // set these to the first hit
454 m_path = m_paths[ 0 ] ;
455 m_fileName = wxFileNameFromPath(m_path);
456 m_dir = wxPathOnly(m_path);
457 NavDisposeReply( &mNavReply ) ;
458 return wxID_OK ;
459 }
460 return wxID_CANCEL;
461 }
462