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