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