]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: filedlg.cpp | |
685a634c | 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 |
65571936 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
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" |
5974c3cf | 23 | #include "wx/filename.h" |
e9576ca5 | 24 | |
f11bdd03 | 25 | #ifndef __DARWIN__ |
03e11df5 GD |
26 | #include "PLStringFuncs.h" |
27 | #endif | |
5b781a67 | 28 | |
2f1ae414 | 29 | #if !USE_SHARED_LIBRARY |
f74172ab | 30 | IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase) |
2f1ae414 | 31 | #endif |
e9576ca5 | 32 | |
519cb848 SC |
33 | // begin wxmac |
34 | ||
76a5e5d2 SC |
35 | #include "wx/mac/private.h" |
36 | ||
bb378910 | 37 | #include <Navigation.h> |
5b781a67 | 38 | |
a2b77260 | 39 | #include "MoreFilesX.h" |
685a634c | 40 | |
5b781a67 SC |
41 | extern bool gUseNavServices ; |
42 | ||
4d4d8bbf SC |
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 | ||
4d4d8bbf | 49 | struct OpenUserDataRec { |
a4f5b9b9 GD |
50 | int currentfilter ; |
51 | bool saveMode ; | |
2b5f62a0 VZ |
52 | wxArrayString name ; |
53 | wxArrayString extensions ; | |
e40298d5 | 54 | wxArrayLong filtermactypes ; |
685a634c | 55 | wxString defaultLocation; |
a4f5b9b9 | 56 | CFArrayRef menuitems ; |
4d4d8bbf | 57 | }; |
2b5f62a0 | 58 | |
4d4d8bbf | 59 | typedef struct OpenUserDataRec |
a4f5b9b9 | 60 | OpenUserDataRec, *OpenUserDataRecPtr; |
4d4d8bbf | 61 | |
e40298d5 JS |
62 | static pascal void NavEventProc( |
63 | NavEventCallbackMessage inSelector, | |
64 | NavCBRecPtr ioParams, | |
65 | NavCallBackUserData ioUserData); | |
5b781a67 | 66 | |
a2b77260 | 67 | static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc); |
5b781a67 SC |
68 | |
69 | static pascal void | |
70 | NavEventProc( | |
e40298d5 JS |
71 | NavEventCallbackMessage inSelector, |
72 | NavCBRecPtr ioParams, | |
73 | NavCallBackUserData ioUserData ) | |
5b781a67 | 74 | { |
e40298d5 | 75 | OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ; |
a4f5b9b9 | 76 | if (inSelector == kNavCBEvent) { |
685a634c | 77 | } |
e40298d5 JS |
78 | else if ( inSelector == kNavCBStart ) |
79 | { | |
a4f5b9b9 GD |
80 | if (data && !(data->defaultLocation).IsEmpty()) |
81 | { | |
82 | // Set default location for the modern Navigation APIs | |
83 | // Apple Technical Q&A 1151 | |
84 | FSSpec theFSSpec; | |
85 | wxMacFilename2FSSpec(data->defaultLocation, &theFSSpec); | |
86 | AEDesc theLocation = {typeNull, NULL}; | |
87 | if (noErr == ::AECreateDesc(typeFSS, &theFSSpec, sizeof(FSSpec), &theLocation)) | |
88 | ::NavCustomControl(ioParams->context, kNavCtlSetLocation, (void *) &theLocation); | |
89 | } | |
75338075 DS |
90 | |
91 | NavMenuItemSpec menuItem; | |
92 | menuItem.version = kNavMenuItemSpecVersion; | |
93 | menuItem.menuCreator = 'WXNG'; | |
94 | menuItem.menuType = data->currentfilter; | |
95 | wxMacStringToPascal( data->name[data->currentfilter] , (StringPtr)(menuItem.menuItemName) ) ; | |
96 | ::NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &menuItem); | |
e40298d5 JS |
97 | } |
98 | else if ( inSelector == kNavCBPopupMenuSelect ) | |
99 | { | |
100 | NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ; | |
75338075 DS |
101 | const size_t numFilters = data->extensions.GetCount(); |
102 | ||
103 | if ( menu->menuType < numFilters ) | |
e40298d5 JS |
104 | { |
105 | data->currentfilter = menu->menuType ; | |
106 | if ( data->saveMode ) | |
107 | { | |
108 | int i = menu->menuType ; | |
109 | wxString extension = data->extensions[i].AfterLast('.') ; | |
110 | extension.MakeLower() ; | |
f65d4b83 | 111 | wxString sfilename ; |
685a634c | 112 | |
4891a3d5 | 113 | wxMacCFStringHolder cfString( NavDialogGetSaveFileName( ioParams->context ) , false ); |
f65d4b83 | 114 | sfilename = cfString.AsString() ; |
f65d4b83 | 115 | |
685a634c | 116 | int pos = sfilename.Find('.', true) ; |
e40298d5 JS |
117 | if ( pos != wxNOT_FOUND ) |
118 | { | |
119 | sfilename = sfilename.Left(pos+1)+extension ; | |
a9412f8f | 120 | cfString.Assign( sfilename , wxFONTENCODING_DEFAULT ) ; |
f65d4b83 | 121 | NavDialogSetSaveFileName( ioParams->context , cfString ) ; |
e40298d5 JS |
122 | } |
123 | } | |
f65d4b83 | 124 | } |
e40298d5 | 125 | } |
5b781a67 SC |
126 | } |
127 | ||
2f1ae414 | 128 | |
e40298d5 | 129 | void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter ) |
4d4d8bbf | 130 | { |
e40298d5 JS |
131 | myData->menuitems = NULL ; |
132 | myData->currentfilter = 0 ; | |
685a634c DS |
133 | myData->saveMode = false ; |
134 | ||
e40298d5 | 135 | if ( filter && filter[0] ) |
4d4d8bbf | 136 | { |
e40298d5 JS |
137 | wxString filter2(filter) ; |
138 | int filterIndex = 0; | |
139 | bool isName = true ; | |
140 | wxString current ; | |
141 | for( unsigned int i = 0; i < filter2.Len() ; i++ ) | |
142 | { | |
143 | if( filter2.GetChar(i) == wxT('|') ) | |
144 | { | |
145 | if( isName ) { | |
146 | myData->name.Add( current ) ; | |
147 | } | |
148 | else { | |
149 | myData->extensions.Add( current.MakeUpper() ) ; | |
150 | ++filterIndex ; | |
151 | } | |
152 | isName = !isName ; | |
427ff662 | 153 | current = wxEmptyString ; |
e40298d5 JS |
154 | } |
155 | else | |
156 | { | |
157 | current += filter2.GetChar(i) ; | |
158 | } | |
4d4d8bbf | 159 | } |
e40298d5 JS |
160 | // we allow for compatibility reason to have a single filter expression (like *.*) without |
161 | // an explanatory text, in that case the first part is name and extension at the same time | |
685a634c | 162 | |
427ff662 | 163 | wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ; |
e40298d5 JS |
164 | if ( current.IsEmpty() ) |
165 | myData->extensions.Add( myData->name[filterIndex] ) ; | |
166 | else | |
167 | myData->extensions.Add( current.MakeUpper() ) ; | |
168 | if ( filterIndex == 0 || isName ) | |
169 | myData->name.Add( current.MakeUpper() ) ; | |
685a634c | 170 | |
e40298d5 | 171 | ++filterIndex ; |
685a634c | 172 | |
2b5f62a0 | 173 | const size_t extCount = myData->extensions.GetCount(); |
e40298d5 JS |
174 | for ( size_t i = 0 ; i < extCount; i++ ) |
175 | { | |
5974c3cf SC |
176 | wxUint32 fileType; |
177 | wxUint32 creator; | |
178 | wxString extension = myData->extensions[i]; | |
179 | ||
180 | if (extension.GetChar(0) == '*') | |
181 | extension = extension.Mid(1); // Remove leading * | |
182 | ||
183 | if (extension.GetChar(0) == '.') | |
e40298d5 | 184 | { |
5974c3cf | 185 | extension = extension.Mid(1); // Remove leading . |
e40298d5 | 186 | } |
5974c3cf SC |
187 | |
188 | if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator )) | |
e40298d5 | 189 | { |
5974c3cf | 190 | myData->filtermactypes.Add( (OSType)fileType ); |
e40298d5 | 191 | } |
5974c3cf SC |
192 | else |
193 | { | |
194 | myData->filtermactypes.Add( '****' ) ; // We'll fail safe if it's not recognized | |
195 | } | |
e40298d5 JS |
196 | } |
197 | } | |
4d4d8bbf | 198 | } |
bb378910 | 199 | |
f65d4b83 | 200 | static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data) |
4d4d8bbf | 201 | { |
a4f5b9b9 | 202 | wxString file(filename) ; |
9f92f6fb | 203 | file.MakeUpper() ; |
685a634c | 204 | |
2b5f62a0 | 205 | if ( data->extensions.GetCount() > 0 ) |
da2b4b7a | 206 | { |
e40298d5 JS |
207 | //for ( int i = 0 ; i < data->numfilters ; ++i ) |
208 | int i = data->currentfilter ; | |
427ff662 | 209 | if ( data->extensions[i].Right(2) == wxT(".*") ) |
e40298d5 | 210 | return true ; |
685a634c | 211 | |
e40298d5 JS |
212 | { |
213 | if ( type == (OSType)data->filtermactypes[i] ) | |
214 | return true ; | |
685a634c | 215 | |
427ff662 | 216 | wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ; |
e40298d5 JS |
217 | while( tokenizer.HasMoreTokens() ) |
218 | { | |
219 | wxString extension = tokenizer.GetNextToken() ; | |
220 | if ( extension.GetChar(0) == '*' ) | |
221 | extension = extension.Mid(1) ; | |
685a634c | 222 | |
e40298d5 JS |
223 | if ( file.Len() >= extension.Len() && extension == file.Right(extension.Len() ) ) |
224 | return true ; | |
225 | } | |
226 | } | |
227 | return false ; | |
da2b4b7a GD |
228 | } |
229 | return true ; | |
4d4d8bbf SC |
230 | } |
231 | ||
a2b77260 | 232 | #if !TARGET_API_MAC_OSX |
5fde6fcc | 233 | static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr) |
a4f5b9b9 | 234 | { |
e40298d5 JS |
235 | OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ; |
236 | // return true if this item is invisible or a file | |
237 | ||
238 | Boolean visibleFlag; | |
239 | Boolean folderFlag; | |
685a634c | 240 | |
e40298d5 JS |
241 | visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible); |
242 | folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10); | |
685a634c | 243 | |
e40298d5 JS |
244 | // because the semantics of the filter proc are "true means don't show |
245 | // it" we need to invert the result that we return | |
685a634c | 246 | |
e40298d5 JS |
247 | if ( !visibleFlag ) |
248 | return true ; | |
685a634c | 249 | |
e40298d5 JS |
250 | if ( !folderFlag ) |
251 | { | |
a4f5b9b9 | 252 | wxString file = wxMacMakeStringFromPascal( myCInfoPBPtr->hFileInfo.ioNamePtr ) ; |
f65d4b83 | 253 | return !CheckFile( file , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ; |
a4f5b9b9 | 254 | } |
685a634c | 255 | |
e40298d5 | 256 | return false ; |
519cb848 | 257 | } |
bb378910 | 258 | #endif |
519cb848 SC |
259 | |
260 | // end wxmac | |
261 | ||
e9576ca5 SC |
262 | wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message, |
263 | const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard, | |
264 | long style, const wxPoint& pos) | |
f74172ab | 265 | :wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos) |
e9576ca5 | 266 | { |
427ff662 | 267 | wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ; |
e9576ca5 SC |
268 | } |
269 | ||
f11bdd03 | 270 | pascal Boolean CrossPlatformFilterCallback ( |
685a634c DS |
271 | AEDesc *theItem, |
272 | void *info, | |
273 | void *callBackUD, | |
4d4d8bbf SC |
274 | NavFilterModes filterMode |
275 | ) | |
276 | { | |
2d4e4f80 GD |
277 | bool display = true; |
278 | OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ; | |
685a634c | 279 | |
2d4e4f80 GD |
280 | if (filterMode == kNavFilteringBrowserList) |
281 | { | |
282 | NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ; | |
f65d4b83 | 283 | if ( !theInfo->isFolder ) |
2d4e4f80 | 284 | { |
f65d4b83 SC |
285 | if (theItem->descriptorType == typeFSS ) |
286 | { | |
287 | FSSpec spec; | |
288 | memcpy( &spec , *theItem->dataHandle , sizeof(FSSpec) ) ; | |
a4f5b9b9 | 289 | wxString file = wxMacMakeStringFromPascal( spec.name ) ; |
f65d4b83 | 290 | display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ; |
685a634c | 291 | } |
f65d4b83 SC |
292 | else if ( theItem->descriptorType == typeFSRef ) |
293 | { | |
294 | FSRef fsref ; | |
295 | memcpy( &fsref , *theItem->dataHandle , sizeof(FSRef) ) ; | |
a2b77260 | 296 | wxString file = wxMacFSRefToPath( &fsref ) ; |
f65d4b83 SC |
297 | display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ; |
298 | } | |
2d4e4f80 GD |
299 | } |
300 | } | |
685a634c | 301 | |
2d4e4f80 | 302 | return display; |
4d4d8bbf SC |
303 | } |
304 | ||
e9576ca5 SC |
305 | int wxFileDialog::ShowModal() |
306 | { | |
a4f5b9b9 GD |
307 | OSErr err; |
308 | NavDialogCreationOptions dialogCreateOptions; | |
309 | // set default options | |
310 | ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions); | |
685a634c | 311 | |
a4f5b9b9 GD |
312 | // this was always unset in the old code |
313 | dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation; | |
685a634c | 314 | |
e71800ba SC |
315 | wxMacCFStringHolder message(m_message, m_font.GetEncoding()); |
316 | dialogCreateOptions.windowTitle = message; | |
317 | ||
318 | wxMacCFStringHolder defaultFileName(m_fileName, m_font.GetEncoding()); | |
319 | dialogCreateOptions.saveFileName = defaultFileName; | |
320 | ||
321 | ||
a4f5b9b9 GD |
322 | NavDialogRef dialog; |
323 | NavObjectFilterUPP navFilterUPP = NULL; | |
685a634c | 324 | CFArrayRef cfArray = NULL; // for popupExtension |
a4f5b9b9 GD |
325 | OpenUserDataRec myData; |
326 | myData.defaultLocation = m_dir; | |
327 | ||
75338075 DS |
328 | MakeUserDataRec(&myData , m_wildCard); |
329 | myData.currentfilter = m_filterIndex; | |
330 | size_t numFilters = myData.extensions.GetCount(); | |
331 | if (numFilters) | |
332 | { | |
333 | CFMutableArrayRef popup = CFArrayCreateMutable( kCFAllocatorDefault , | |
334 | numFilters , &kCFTypeArrayCallBacks ) ; | |
335 | dialogCreateOptions.popupExtension = popup ; | |
336 | myData.menuitems = dialogCreateOptions.popupExtension ; | |
337 | for ( size_t i = 0 ; i < numFilters ; ++i ) | |
338 | { | |
339 | CFArrayAppendValue( popup , (CFStringRef) wxMacCFStringHolder( myData.name[i] , m_font.GetEncoding() ) ) ; | |
340 | } | |
341 | } | |
342 | ||
a4f5b9b9 GD |
343 | if (m_dialogStyle & wxSAVE) |
344 | { | |
75338075 DS |
345 | myData.saveMode = true; |
346 | ||
347 | if (!numFilters) | |
348 | { | |
349 | dialogCreateOptions.optionFlags |= kNavNoTypePopup; | |
350 | } | |
685a634c DS |
351 | dialogCreateOptions.optionFlags |= kNavDontAutoTranslate; |
352 | dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems; | |
353 | ||
354 | // The extension is important | |
a485ee6f JS |
355 | if (numFilters < 2) |
356 | dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension; | |
685a634c | 357 | |
78eeb095 SC |
358 | #if TARGET_API_MAC_OSX |
359 | if (!(m_dialogStyle & wxOVERWRITE_PROMPT)) | |
360 | { | |
361 | dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement; | |
362 | } | |
363 | #endif | |
a4f5b9b9 | 364 | err = ::NavCreatePutFileDialog(&dialogCreateOptions, |
75338075 DS |
365 | // Suppresses the 'Default' (top) menu item |
366 | kNavGenericSignature, kNavGenericSignature, | |
685a634c DS |
367 | sStandardNavEventFilter, |
368 | &myData, // for defaultLocation | |
369 | &dialog); | |
a4f5b9b9 GD |
370 | } |
371 | else | |
372 | { | |
c11d9cb8 KO |
373 | |
374 | //let people select bundles/programs in dialogs | |
375 | dialogCreateOptions.optionFlags |= kNavSupportPackages; | |
376 | ||
a4f5b9b9 GD |
377 | navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback); |
378 | err = ::NavCreateGetFileDialog(&dialogCreateOptions, | |
379 | NULL, // NavTypeListHandle | |
380 | sStandardNavEventFilter, | |
381 | NULL, // NavPreviewUPP | |
382 | navFilterUPP, | |
383 | (void *) &myData, // inClientData | |
384 | &dialog); | |
385 | } | |
386 | ||
387 | if (err == noErr) | |
388 | err = ::NavDialogRun(dialog); | |
685a634c | 389 | |
a4f5b9b9 GD |
390 | // clean up filter related data, etc. |
391 | if (navFilterUPP) | |
392 | ::DisposeNavObjectFilterUPP(navFilterUPP); | |
393 | if (cfArray) | |
a4f5b9b9 | 394 | ::CFRelease(cfArray); |
e71800ba | 395 | |
685a634c | 396 | if (err != noErr) |
a4f5b9b9 GD |
397 | return wxID_CANCEL; |
398 | ||
399 | NavReplyRecord navReply; | |
400 | err = ::NavDialogGetReply(dialog, &navReply); | |
685a634c | 401 | if (err == noErr && navReply.validRecord) |
a4f5b9b9 GD |
402 | { |
403 | AEKeyword theKeyword; | |
404 | DescType actualType; | |
405 | Size actualSize; | |
406 | FSRef theFSRef; | |
878973f1 | 407 | wxString thePath ; |
75338075 DS |
408 | |
409 | m_filterIndex = myData.currentfilter ; | |
410 | ||
a4f5b9b9 GD |
411 | long count; |
412 | ::AECountItems(&navReply.selection , &count); | |
413 | for (long i = 1; i <= count; ++i) | |
414 | { | |
d1b3039b | 415 | err = ::AEGetNthPtr(&(navReply.selection), i, typeFSRef, &theKeyword, &actualType, |
a4f5b9b9 | 416 | &theFSRef, sizeof(theFSRef), &actualSize); |
685a634c | 417 | if (err != noErr) |
a4f5b9b9 GD |
418 | break; |
419 | ||
420 | if (m_dialogStyle & wxSAVE) | |
a2b77260 | 421 | thePath = wxMacFSRefToPath( &theFSRef , navReply.saveFileName ) ; |
685a634c | 422 | else |
a2b77260 SC |
423 | thePath = wxMacFSRefToPath( &theFSRef ) ; |
424 | ||
a449f840 DS |
425 | if (!thePath) |
426 | { | |
427 | ::NavDisposeReply(&navReply); | |
428 | return wxID_CANCEL; | |
a4f5b9b9 GD |
429 | } |
430 | m_path = thePath; | |
431 | m_paths.Add(m_path); | |
432 | m_fileName = wxFileNameFromPath(m_path); | |
433 | m_fileNames.Add(m_fileName); | |
434 | } | |
435 | // set these to the first hit | |
436 | m_path = m_paths[0]; | |
437 | m_fileName = wxFileNameFromPath(m_path); | |
438 | m_dir = wxPathOnly(m_path); | |
439 | } | |
440 | ::NavDisposeReply(&navReply); | |
685a634c | 441 | |
a4f5b9b9 | 442 | return (err == noErr) ? wxID_OK : wxID_CANCEL; |
5b781a67 | 443 | } |
e9576ca5 | 444 |