]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: filedlg.cpp | |
2f1ae414 | 3 | // Purpose: wxFileDialog |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
e9576ca5 SC |
9 | // Licence: wxWindows licence |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "filedlg.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/defs.h" | |
5fde6fcc | 17 | #include "wx/app.h" |
e9576ca5 SC |
18 | #include "wx/utils.h" |
19 | #include "wx/dialog.h" | |
20 | #include "wx/filedlg.h" | |
21 | #include "wx/intl.h" | |
fe35d097 | 22 | #include "wx/tokenzr.h" |
e9576ca5 | 23 | |
f11bdd03 | 24 | #ifndef __DARWIN__ |
03e11df5 GD |
25 | #include "PLStringFuncs.h" |
26 | #endif | |
5b781a67 | 27 | |
2f1ae414 | 28 | #if !USE_SHARED_LIBRARY |
e9576ca5 | 29 | IMPLEMENT_CLASS(wxFileDialog, wxDialog) |
2f1ae414 | 30 | #endif |
e9576ca5 | 31 | |
519cb848 SC |
32 | // begin wxmac |
33 | ||
76a5e5d2 SC |
34 | #include "wx/mac/private.h" |
35 | ||
bb378910 | 36 | #include <Navigation.h> |
5b781a67 | 37 | |
2d4e4f80 GD |
38 | #ifdef __DARWIN__ |
39 | # include "MoreFilesX.h" | |
40 | #else | |
41 | # include "MoreFiles.h" | |
42 | # include "MoreFilesExtras.h" | |
43 | #endif | |
44 | ||
5b781a67 SC |
45 | extern bool gUseNavServices ; |
46 | ||
4d4d8bbf SC |
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 | ||
4d4d8bbf SC |
53 | struct OpenUserDataRec { |
54 | int currentfilter ; | |
e40298d5 | 55 | bool saveMode ; |
2b5f62a0 VZ |
56 | wxArrayString name ; |
57 | wxArrayString extensions ; | |
e40298d5 | 58 | wxArrayLong filtermactypes ; |
2b5f62a0 | 59 | NavMenuItemSpecArrayHandle menuitems ; |
4d4d8bbf | 60 | }; |
2b5f62a0 | 61 | |
4d4d8bbf | 62 | typedef struct OpenUserDataRec |
e40298d5 | 63 | OpenUserDataRec, *OpenUserDataRecPtr; |
4d4d8bbf | 64 | |
e40298d5 JS |
65 | static pascal void NavEventProc( |
66 | NavEventCallbackMessage inSelector, | |
67 | NavCBRecPtr ioParams, | |
68 | NavCallBackUserData ioUserData); | |
5b781a67 SC |
69 | |
70 | #if TARGET_CARBON | |
e40298d5 | 71 | static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc); |
5b781a67 | 72 | #else |
e40298d5 | 73 | static NavEventUPP sStandardNavEventFilter = NewNavEventProc(NavEventProc); |
5b781a67 SC |
74 | #endif |
75 | ||
76 | static pascal void | |
77 | NavEventProc( | |
e40298d5 JS |
78 | NavEventCallbackMessage inSelector, |
79 | NavCBRecPtr ioParams, | |
80 | NavCallBackUserData ioUserData ) | |
5b781a67 | 81 | { |
e40298d5 JS |
82 | OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ; |
83 | if (inSelector == kNavCBEvent) { | |
ac9b5f98 | 84 | #if !TARGET_CARBON |
5fde6fcc | 85 | wxTheApp->MacHandleOneEvent(ioParams->eventData.eventDataParms.event); |
ac9b5f98 | 86 | #endif |
e40298d5 JS |
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); | |
427ff662 | 107 | wxString sfilename = wxMacMakeStringFromPascal( filename ) ; |
e40298d5 JS |
108 | int pos = sfilename.Find('.',TRUE) ; |
109 | if ( pos != wxNOT_FOUND ) | |
110 | { | |
111 | sfilename = sfilename.Left(pos+1)+extension ; | |
427ff662 | 112 | wxMacStringToPascal( sfilename , filename ) ; |
e40298d5 JS |
113 | NavCustomControl(ioParams->context, kNavCtlSetEditFileName, &filename); |
114 | } | |
115 | } | |
116 | } | |
117 | } | |
5b781a67 SC |
118 | } |
119 | ||
427ff662 | 120 | const wxChar * gfilters[] = |
519cb848 | 121 | { |
427ff662 SC |
122 | wxT("*.TXT") , |
123 | wxT("*.TIF") , | |
124 | wxT("*.JPG") , | |
e40298d5 JS |
125 | |
126 | NULL | |
519cb848 SC |
127 | } ; |
128 | ||
129 | OSType gfiltersmac[] = | |
130 | { | |
e40298d5 JS |
131 | 'TEXT' , |
132 | 'TIFF' , | |
133 | 'JPEG' , | |
134 | ||
135 | '****' | |
519cb848 SC |
136 | } ; |
137 | ||
2f1ae414 | 138 | |
519cb848 | 139 | |
e40298d5 | 140 | void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter ) |
4d4d8bbf | 141 | { |
e40298d5 JS |
142 | myData->menuitems = NULL ; |
143 | myData->currentfilter = 0 ; | |
144 | myData->saveMode = FALSE ; | |
145 | ||
146 | if ( filter && filter[0] ) | |
4d4d8bbf | 147 | { |
e40298d5 JS |
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 ; | |
427ff662 | 164 | current = wxEmptyString ; |
e40298d5 JS |
165 | } |
166 | else | |
167 | { | |
168 | current += filter2.GetChar(i) ; | |
169 | } | |
4d4d8bbf | 170 | } |
e40298d5 JS |
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 | ||
427ff662 | 174 | wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ; |
e40298d5 JS |
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 | ||
2b5f62a0 | 184 | |
2b5f62a0 | 185 | const size_t extCount = myData->extensions.GetCount(); |
e40298d5 JS |
186 | for ( size_t i = 0 ; i < extCount; i++ ) |
187 | { | |
188 | int j ; | |
189 | for ( j = 0 ; gfilters[j] ; j++ ) | |
190 | { | |
427ff662 | 191 | if ( myData->extensions[i] == gfilters[j] ) |
e40298d5 JS |
192 | { |
193 | myData->filtermactypes.Add( gfiltersmac[j] ) ; | |
194 | break ; | |
195 | } | |
196 | } | |
197 | if( gfilters[j] == NULL ) | |
198 | { | |
199 | myData->filtermactypes.Add( '****' ) ; | |
200 | } | |
201 | } | |
202 | } | |
4d4d8bbf | 203 | } |
bb378910 | 204 | |
4d4d8bbf SC |
205 | static Boolean CheckFile( ConstStr255Param name , OSType type , OpenUserDataRecPtr data) |
206 | { | |
427ff662 | 207 | /* |
e40298d5 | 208 | Str255 filename ; |
da2b4b7a GD |
209 | |
210 | #if TARGET_CARBON | |
211 | p2cstrcpy((char *)filename, name) ; | |
212 | #else | |
213 | PLstrcpy( filename , name ) ; | |
9f92f6fb | 214 | p2cstr( filename ) ; |
da2b4b7a | 215 | #endif |
9f92f6fb | 216 | wxString file(filename) ; |
427ff662 SC |
217 | */ |
218 | wxString file = wxMacMakeStringFromPascal( name ) ; | |
9f92f6fb SC |
219 | file.MakeUpper() ; |
220 | ||
2b5f62a0 | 221 | if ( data->extensions.GetCount() > 0 ) |
da2b4b7a | 222 | { |
e40298d5 JS |
223 | //for ( int i = 0 ; i < data->numfilters ; ++i ) |
224 | int i = data->currentfilter ; | |
427ff662 | 225 | if ( data->extensions[i].Right(2) == wxT(".*") ) |
e40298d5 JS |
226 | return true ; |
227 | ||
228 | { | |
229 | if ( type == (OSType)data->filtermactypes[i] ) | |
230 | return true ; | |
231 | ||
427ff662 | 232 | wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ; |
e40298d5 JS |
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 ; | |
da2b4b7a GD |
244 | } |
245 | return true ; | |
4d4d8bbf SC |
246 | } |
247 | ||
bb378910 | 248 | #ifndef __DARWIN__ |
5fde6fcc | 249 | static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr) |
e40298d5 JS |
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 ; | |
519cb848 | 272 | } |
bb378910 | 273 | #endif |
519cb848 SC |
274 | |
275 | // end wxmac | |
276 | ||
e9576ca5 SC |
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 | { | |
427ff662 | 281 | wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ; |
e9576ca5 SC |
282 | m_message = message; |
283 | m_dialogStyle = style; | |
284 | m_parent = parent; | |
427ff662 | 285 | m_path = wxT(""); |
e9576ca5 SC |
286 | m_fileName = defaultFileName; |
287 | m_dir = defaultDir; | |
288 | m_wildCard = wildCard; | |
2b5f62a0 | 289 | m_filterIndex = 0; |
e9576ca5 SC |
290 | } |
291 | ||
f11bdd03 | 292 | pascal Boolean CrossPlatformFilterCallback ( |
4d4d8bbf SC |
293 | AEDesc *theItem, |
294 | void *info, | |
295 | void *callBackUD, | |
296 | NavFilterModes filterMode | |
297 | ) | |
298 | { | |
2d4e4f80 GD |
299 | bool display = true; |
300 | OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ; | |
301 | ||
302 | if (filterMode == kNavFilteringBrowserList) | |
303 | { | |
304 | NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ; | |
305 | if (theItem->descriptorType == typeFSS && !theInfo->isFolder) | |
306 | { | |
e40298d5 | 307 | FSSpec spec; |
2d4e4f80 GD |
308 | memcpy( &spec , *theItem->dataHandle , sizeof(FSSpec) ) ; |
309 | display = CheckFile( spec.name , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ; | |
310 | } | |
311 | } | |
312 | ||
313 | return display; | |
4d4d8bbf SC |
314 | } |
315 | ||
e9576ca5 SC |
316 | int wxFileDialog::ShowModal() |
317 | { | |
e40298d5 JS |
318 | NavDialogOptions mNavOptions; |
319 | NavObjectFilterUPP mNavFilterUPP = NULL; | |
320 | NavPreviewUPP mNavPreviewUPP = NULL ; | |
321 | NavReplyRecord mNavReply; | |
322 | AEDesc mDefaultLocation ; | |
323 | bool mSelectDefault = false ; | |
324 | ||
e40298d5 JS |
325 | // setup dialog |
326 | ||
2d4e4f80 | 327 | ::NavGetDefaultDialogOptions(&mNavOptions); |
e40298d5 JS |
328 | |
329 | mNavFilterUPP = nil; | |
330 | mNavPreviewUPP = nil; | |
331 | mSelectDefault = false; | |
2d4e4f80 | 332 | mNavReply.validRecord = false; |
e40298d5 | 333 | mNavReply.replacing = false; |
2d4e4f80 | 334 | mNavReply.isStationery = false; |
e40298d5 | 335 | mNavReply.translationNeeded = false; |
2d4e4f80 GD |
336 | mNavReply.selection.descriptorType = typeNull; |
337 | mNavReply.selection.dataHandle = nil; | |
e40298d5 | 338 | mNavReply.keyScript = smSystemScript; |
2d4e4f80 | 339 | mNavReply.fileTranslation = nil; |
e40298d5 | 340 | |
2d4e4f80 GD |
341 | // Set default location, the location |
342 | // that's displayed when the dialog | |
343 | // first appears | |
e40298d5 | 344 | |
2d4e4f80 GD |
345 | FSSpec location ; |
346 | wxMacFilename2FSSpec( m_dir , &location ) ; | |
347 | OSErr err = noErr ; | |
e40298d5 | 348 | |
2d4e4f80 GD |
349 | mDefaultLocation.descriptorType = typeNull; |
350 | mDefaultLocation.dataHandle = nil; | |
5b781a67 | 351 | |
2d4e4f80 | 352 | err = ::AECreateDesc(typeFSS, &location, sizeof(FSSpec), &mDefaultLocation ); |
5b781a67 | 353 | |
2d4e4f80 | 354 | if ( mDefaultLocation.dataHandle ) { |
e40298d5 | 355 | |
2d4e4f80 GD |
356 | if (mSelectDefault) { |
357 | mNavOptions.dialogOptionFlags |= kNavSelectDefaultLocation; | |
358 | } else { | |
359 | mNavOptions.dialogOptionFlags &= ~kNavSelectDefaultLocation; | |
360 | } | |
361 | } | |
427ff662 SC |
362 | wxMacStringToPascal( m_message , (StringPtr)mNavOptions.message ) ; |
363 | wxMacStringToPascal( m_fileName , (StringPtr)mNavOptions.savedFileName ) ; | |
5b781a67 | 364 | |
7ed2b47b SC |
365 | // zero all data |
366 | ||
367 | m_path = wxEmptyString ; | |
368 | m_fileName = wxEmptyString ; | |
369 | m_paths.Empty(); | |
370 | m_fileNames.Empty(); | |
371 | ||
e40298d5 | 372 | OpenUserDataRec myData; |
2d4e4f80 GD |
373 | MakeUserDataRec( &myData , m_wildCard ) ; |
374 | myData.currentfilter = m_filterIndex ; | |
375 | if ( myData.extensions.GetCount() > 0 ) | |
376 | { | |
377 | mNavOptions.popupExtension = (NavMenuItemSpecArrayHandle) NewHandle( sizeof( NavMenuItemSpec ) * myData.extensions.GetCount() ) ; | |
378 | myData.menuitems = mNavOptions.popupExtension ; | |
379 | for ( size_t i = 0 ; i < myData.extensions.GetCount() ; ++i ) | |
380 | { | |
381 | (*mNavOptions.popupExtension)[i].version = kNavMenuItemSpecVersion ; | |
382 | (*mNavOptions.popupExtension)[i].menuCreator = 'WXNG' ; | |
383 | (*mNavOptions.popupExtension)[i].menuType = i ; | |
427ff662 | 384 | wxMacStringToPascal( myData.name[i] , (StringPtr)(*mNavOptions.popupExtension)[i].menuItemName ) ; |
2d4e4f80 GD |
385 | } |
386 | } | |
387 | if ( m_dialogStyle & wxSAVE ) | |
388 | { | |
389 | myData.saveMode = true ; | |
2b5f62a0 | 390 | |
2d4e4f80 GD |
391 | mNavOptions.dialogOptionFlags |= kNavDontAutoTranslate ; |
392 | mNavOptions.dialogOptionFlags |= kNavDontAddTranslateItems ; | |
e40298d5 | 393 | |
2d4e4f80 GD |
394 | err = ::NavPutFile( |
395 | &mDefaultLocation, | |
396 | &mNavReply, | |
397 | &mNavOptions, | |
398 | sStandardNavEventFilter , | |
399 | NULL, | |
400 | kNavGenericSignature, | |
e40298d5 | 401 | &myData); // User Data |
2d4e4f80 GD |
402 | m_filterIndex = myData.currentfilter ; |
403 | } | |
404 | else | |
405 | { | |
406 | myData.saveMode = false ; | |
4d4d8bbf | 407 | |
2d4e4f80 GD |
408 | mNavFilterUPP = NewNavObjectFilterUPP( CrossPlatformFilterCallback ) ; |
409 | if ( m_dialogStyle & wxMULTIPLE ) | |
410 | mNavOptions.dialogOptionFlags |= kNavAllowMultipleFiles ; | |
411 | else | |
412 | mNavOptions.dialogOptionFlags &= ~kNavAllowMultipleFiles ; | |
e40298d5 | 413 | |
2d4e4f80 GD |
414 | err = ::NavGetFile( |
415 | &mDefaultLocation, | |
416 | &mNavReply, | |
417 | &mNavOptions, | |
418 | sStandardNavEventFilter , | |
419 | mNavPreviewUPP, | |
420 | mNavFilterUPP, | |
421 | NULL , | |
422 | &myData); | |
423 | m_filterIndex = myData.currentfilter ; | |
424 | } | |
e40298d5 | 425 | |
2d4e4f80 GD |
426 | DisposeNavObjectFilterUPP(mNavFilterUPP); |
427 | if ( mDefaultLocation.dataHandle != nil ) | |
428 | { | |
429 | ::AEDisposeDesc(&mDefaultLocation); | |
430 | } | |
e40298d5 | 431 | |
2d4e4f80 | 432 | if ( (err != noErr) && (err != userCanceledErr) ) { |
2d4e4f80 GD |
433 | return wxID_CANCEL ; |
434 | } | |
5b781a67 | 435 | |
2d4e4f80 | 436 | if (mNavReply.validRecord) { |
e40298d5 | 437 | |
2d4e4f80 GD |
438 | FSSpec outFileSpec ; |
439 | AEDesc specDesc ; | |
440 | AEKeyword keyWord ; | |
e40298d5 | 441 | |
2d4e4f80 GD |
442 | long count ; |
443 | ::AECountItems( &mNavReply.selection , &count ) ; | |
444 | for ( long i = 1 ; i <= count ; ++i ) | |
445 | { | |
446 | OSErr err = ::AEGetNthDesc( &mNavReply.selection , i , typeFSS, &keyWord , &specDesc); | |
447 | if ( err != noErr ) { | |
427ff662 | 448 | m_path = wxT("") ; |
2d4e4f80 | 449 | return wxID_CANCEL ; |
e40298d5 | 450 | } |
2d4e4f80 GD |
451 | outFileSpec = **(FSSpec**) specDesc.dataHandle; |
452 | if (specDesc.dataHandle != nil) { | |
453 | ::AEDisposeDesc(&specDesc); | |
454 | } | |
455 | m_path = wxMacFSSpec2MacFilename( &outFileSpec ) ; | |
456 | m_paths.Add( m_path ) ; | |
24fe8dc7 | 457 | m_fileName = wxFileNameFromPath(m_path); |
2d4e4f80 GD |
458 | m_fileNames.Add(m_fileName); |
459 | } | |
460 | // set these to the first hit | |
461 | m_path = m_paths[ 0 ] ; | |
462 | m_fileName = wxFileNameFromPath(m_path); | |
463 | m_dir = wxPathOnly(m_path); | |
464 | NavDisposeReply( &mNavReply ) ; | |
465 | return wxID_OK ; | |
466 | } | |
467 | return wxID_CANCEL; | |
5b781a67 | 468 | } |
e9576ca5 | 469 |