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