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