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