]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/filedlg.cpp
Fixup Blit so it can be used with a source that is a wxBufferedDC,
[wxWidgets.git] / src / mac / carbon / filedlg.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
88a7a4e1 2// Name: src/mac/carbon/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
88a7a4e1
WS
14#include "wx/filedlg.h"
15
16#ifndef WX_PRECOMP
17 #include "wx/intl.h"
670f9935 18 #include "wx/app.h"
de6185e2 19 #include "wx/utils.h"
fdf565fe 20 #include "wx/dialog.h"
88a7a4e1
WS
21#endif
22
fe35d097 23#include "wx/tokenzr.h"
5974c3cf 24#include "wx/filename.h"
e9576ca5 25
76a5e5d2
SC
26#include "wx/mac/private.h"
27
768c6e8b 28#ifndef __DARWIN__
670f9935
WS
29 #include <Navigation.h>
30 #include "PLStringFuncs.h"
768c6e8b 31#endif
5b781a67 32
f3078f07
DS
33IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
34
4d4d8bbf
SC
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
f3078f07
DS
41struct OpenUserDataRec
42{
a4f5b9b9
GD
43 int currentfilter ;
44 bool saveMode ;
2b5f62a0
VZ
45 wxArrayString name ;
46 wxArrayString extensions ;
e40298d5 47 wxArrayLong filtermactypes ;
685a634c 48 wxString defaultLocation;
a4f5b9b9 49 CFArrayRef menuitems ;
4d4d8bbf 50};
2b5f62a0 51
4d4d8bbf 52typedef struct OpenUserDataRec
a4f5b9b9 53OpenUserDataRec, *OpenUserDataRecPtr;
4d4d8bbf 54
f3078f07
DS
55static pascal void NavEventProc(
56 NavEventCallbackMessage inSelector,
57 NavCBRecPtr ioParams,
58 NavCallBackUserData ioUserData );
5b781a67 59
f3078f07 60static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc);
5b781a67 61
f3078f07
DS
62static pascal void NavEventProc(
63 NavEventCallbackMessage inSelector,
64 NavCBRecPtr ioParams,
65 NavCallBackUserData ioUserData )
5b781a67 66{
e40298d5 67 OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ;
f3078f07
DS
68 if (inSelector == kNavCBEvent)
69 {
685a634c 70 }
e40298d5
JS
71 else if ( inSelector == kNavCBStart )
72 {
de6185e2 73 if (data && !(data->defaultLocation).empty())
a4f5b9b9
GD
74 {
75 // Set default location for the modern Navigation APIs
76 // Apple Technical Q&A 1151
77 FSSpec theFSSpec;
78 wxMacFilename2FSSpec(data->defaultLocation, &theFSSpec);
f3078f07 79 AEDesc theLocation = { typeNull, NULL };
a4f5b9b9
GD
80 if (noErr == ::AECreateDesc(typeFSS, &theFSSpec, sizeof(FSSpec), &theLocation))
81 ::NavCustomControl(ioParams->context, kNavCtlSetLocation, (void *) &theLocation);
82 }
75338075
DS
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);
e40298d5
JS
90 }
91 else if ( inSelector == kNavCBPopupMenuSelect )
92 {
93 NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
75338075
DS
94 const size_t numFilters = data->extensions.GetCount();
95
96 if ( menu->menuType < numFilters )
e40298d5
JS
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() ;
f65d4b83 104 wxString sfilename ;
685a634c 105
4891a3d5 106 wxMacCFStringHolder cfString( NavDialogGetSaveFileName( ioParams->context ) , false );
f65d4b83 107 sfilename = cfString.AsString() ;
f65d4b83 108
685a634c 109 int pos = sfilename.Find('.', true) ;
e40298d5
JS
110 if ( pos != wxNOT_FOUND )
111 {
112 sfilename = sfilename.Left(pos+1)+extension ;
a9412f8f 113 cfString.Assign( sfilename , wxFONTENCODING_DEFAULT ) ;
f65d4b83 114 NavDialogSetSaveFileName( ioParams->context , cfString ) ;
e40298d5
JS
115 }
116 }
f65d4b83 117 }
e40298d5 118 }
5b781a67
SC
119}
120
f3078f07 121void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
4d4d8bbf 122{
e40298d5
JS
123 myData->menuitems = NULL ;
124 myData->currentfilter = 0 ;
685a634c
DS
125 myData->saveMode = false ;
126
e40298d5 127 if ( filter && filter[0] )
4d4d8bbf 128 {
e40298d5
JS
129 wxString filter2(filter) ;
130 int filterIndex = 0;
131 bool isName = true ;
132 wxString current ;
f3078f07 133
e031f1df 134 for ( unsigned int i = 0; i < filter2.length() ; i++ )
e40298d5 135 {
f3078f07 136 if ( filter2.GetChar(i) == wxT('|') )
e40298d5 137 {
f3078f07
DS
138 if ( isName )
139 {
e40298d5
JS
140 myData->name.Add( current ) ;
141 }
f3078f07
DS
142 else
143 {
e40298d5
JS
144 myData->extensions.Add( current.MakeUpper() ) ;
145 ++filterIndex ;
146 }
f3078f07 147
e40298d5 148 isName = !isName ;
427ff662 149 current = wxEmptyString ;
e40298d5
JS
150 }
151 else
152 {
153 current += filter2.GetChar(i) ;
154 }
4d4d8bbf 155 }
e40298d5
JS
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
685a634c 158
427ff662 159 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
88a7a4e1 160 if ( current.empty() )
e40298d5
JS
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() ) ;
685a634c 166
e40298d5 167 ++filterIndex ;
685a634c 168
2b5f62a0 169 const size_t extCount = myData->extensions.GetCount();
e40298d5
JS
170 for ( size_t i = 0 ; i < extCount; i++ )
171 {
f3078f07 172 wxUint32 fileType, creator;
5974c3cf
SC
173 wxString extension = myData->extensions[i];
174
f3078f07 175 // Remove leading '*'
6264c8e2 176 if (extension.length() && (extension.GetChar(0) == '*'))
f3078f07 177 extension = extension.Mid( 1 );
5974c3cf 178
f3078f07 179 // Remove leading '.'
6264c8e2 180 if (extension.length() && (extension.GetChar(0) == '.'))
f3078f07 181 extension = extension.Mid( 1 );
88a7a4e1 182
5974c3cf 183 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
5974c3cf 184 myData->filtermactypes.Add( (OSType)fileType );
5974c3cf 185 else
f3078f07 186 myData->filtermactypes.Add( '****' ); // We'll fail safe if it's not recognized
e40298d5
JS
187 }
188 }
4d4d8bbf 189}
bb378910 190
f65d4b83 191static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data)
4d4d8bbf 192{
a4f5b9b9 193 wxString file(filename) ;
9f92f6fb 194 file.MakeUpper() ;
685a634c 195
2b5f62a0 196 if ( data->extensions.GetCount() > 0 )
da2b4b7a 197 {
e40298d5
JS
198 //for ( int i = 0 ; i < data->numfilters ; ++i )
199 int i = data->currentfilter ;
427ff662 200 if ( data->extensions[i].Right(2) == wxT(".*") )
e40298d5 201 return true ;
685a634c 202
e40298d5
JS
203 {
204 if ( type == (OSType)data->filtermactypes[i] )
205 return true ;
685a634c 206
427ff662 207 wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
f3078f07 208 while ( tokenizer.HasMoreTokens() )
e40298d5
JS
209 {
210 wxString extension = tokenizer.GetNextToken() ;
211 if ( extension.GetChar(0) == '*' )
212 extension = extension.Mid(1) ;
685a634c 213
e031f1df 214 if ( file.length() >= extension.length() && extension == file.Right(extension.length() ) )
e40298d5
JS
215 return true ;
216 }
217 }
f3078f07 218
e40298d5 219 return false ;
da2b4b7a 220 }
f3078f07 221
da2b4b7a 222 return true ;
4d4d8bbf
SC
223}
224
a2b77260 225#if !TARGET_API_MAC_OSX
5fde6fcc 226static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
a4f5b9b9 227{
e40298d5
JS
228 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
229 // return true if this item is invisible or a file
230
231 Boolean visibleFlag;
232 Boolean folderFlag;
685a634c 233
e40298d5
JS
234 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
235 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
685a634c 236
e40298d5
JS
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
685a634c 239
e40298d5
JS
240 if ( !visibleFlag )
241 return true ;
685a634c 242
e40298d5
JS
243 if ( !folderFlag )
244 {
a4f5b9b9 245 wxString file = wxMacMakeStringFromPascal( myCInfoPBPtr->hFileInfo.ioNamePtr ) ;
f65d4b83 246 return !CheckFile( file , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
a4f5b9b9 247 }
685a634c 248
e40298d5 249 return false ;
519cb848 250}
bb378910 251#endif
519cb848
SC
252
253// end wxmac
254
f3078f07
DS
255wxFileDialog::wxFileDialog(
256 wxWindow *parent, const wxString& message,
257 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
ff3e84ff
VZ
258 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
259 : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
e9576ca5 260{
427ff662 261 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
e9576ca5
SC
262}
263
f3078f07 264pascal Boolean CrossPlatformFilterCallback(
685a634c
DS
265 AEDesc *theItem,
266 void *info,
267 void *callBackUD,
f3078f07 268 NavFilterModes filterMode )
4d4d8bbf 269{
2d4e4f80
GD
270 bool display = true;
271 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
685a634c 272
2d4e4f80
GD
273 if (filterMode == kNavFilteringBrowserList)
274 {
275 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
f65d4b83 276 if ( !theInfo->isFolder )
2d4e4f80 277 {
f3078f07 278 if (theItem->descriptorType == typeFSS)
f65d4b83
SC
279 {
280 FSSpec spec;
281 memcpy( &spec , *theItem->dataHandle , sizeof(FSSpec) ) ;
a4f5b9b9 282 wxString file = wxMacMakeStringFromPascal( spec.name ) ;
f65d4b83 283 display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
685a634c 284 }
f65d4b83
SC
285 else if ( theItem->descriptorType == typeFSRef )
286 {
287 FSRef fsref ;
288 memcpy( &fsref , *theItem->dataHandle , sizeof(FSRef) ) ;
a2b77260 289 wxString file = wxMacFSRefToPath( &fsref ) ;
f65d4b83
SC
290 display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
291 }
2d4e4f80
GD
292 }
293 }
685a634c 294
2d4e4f80 295 return display;
4d4d8bbf
SC
296}
297
e9576ca5
SC
298int wxFileDialog::ShowModal()
299{
a4f5b9b9
GD
300 OSErr err;
301 NavDialogCreationOptions dialogCreateOptions;
f3078f07 302
a4f5b9b9
GD
303 // set default options
304 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
685a634c 305
a4f5b9b9
GD
306 // this was always unset in the old code
307 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
685a634c 308
e71800ba
SC
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
a4f5b9b9
GD
316 NavDialogRef dialog;
317 NavObjectFilterUPP navFilterUPP = NULL;
a4f5b9b9
GD
318 OpenUserDataRec myData;
319 myData.defaultLocation = m_dir;
320
75338075
DS
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
ff3e84ff 336 if (HasFlag(wxFD_SAVE))
a4f5b9b9 337 {
75338075
DS
338 myData.saveMode = true;
339
685a634c
DS
340 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
341 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
f3078f07
DS
342 if (!numFilters)
343 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
685a634c
DS
344
345 // The extension is important
a485ee6f
JS
346 if (numFilters < 2)
347 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
685a634c 348
78eeb095 349#if TARGET_API_MAC_OSX
e031f1df 350 if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT))
f3078f07 351 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
78eeb095 352#endif
f3078f07
DS
353
354 err = ::NavCreatePutFileDialog(
355 &dialogCreateOptions,
356 kNavGenericSignature, // Suppresses the 'Default' (top) menu item
357 kNavGenericSignature,
358 sStandardNavEventFilter,
359 &myData, // for defaultLocation
360 &dialog );
a4f5b9b9
GD
361 }
362 else
363 {
f3078f07 364 // let the user select bundles/programs in dialogs
c11d9cb8 365 dialogCreateOptions.optionFlags |= kNavSupportPackages;
88a7a4e1 366
a4f5b9b9 367 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
f3078f07
DS
368 err = ::NavCreateGetFileDialog(
369 &dialogCreateOptions,
370 NULL, // NavTypeListHandle
371 sStandardNavEventFilter,
372 NULL, // NavPreviewUPP
373 navFilterUPP,
374 (void *) &myData, // inClientData
375 &dialog );
a4f5b9b9
GD
376 }
377
378 if (err == noErr)
379 err = ::NavDialogRun(dialog);
685a634c 380
a4f5b9b9
GD
381 // clean up filter related data, etc.
382 if (navFilterUPP)
383 ::DisposeNavObjectFilterUPP(navFilterUPP);
e71800ba 384
685a634c 385 if (err != noErr)
a4f5b9b9
GD
386 return wxID_CANCEL;
387
388 NavReplyRecord navReply;
389 err = ::NavDialogGetReply(dialog, &navReply);
685a634c 390 if (err == noErr && navReply.validRecord)
a4f5b9b9
GD
391 {
392 AEKeyword theKeyword;
393 DescType actualType;
394 Size actualSize;
395 FSRef theFSRef;
878973f1 396 wxString thePath ;
a4f5b9b9 397 long count;
f3078f07
DS
398
399 m_filterIndex = myData.currentfilter;
400 ::AECountItems( &navReply.selection, &count );
a4f5b9b9
GD
401 for (long i = 1; i <= count; ++i)
402 {
f3078f07
DS
403 err = ::AEGetNthPtr(
404 &(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
405 &theFSRef, sizeof(theFSRef), &actualSize );
685a634c 406 if (err != noErr)
a4f5b9b9
GD
407 break;
408
ff3e84ff 409 if (HasFlag(wxFD_SAVE))
f3078f07 410 thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName );
685a634c 411 else
f3078f07 412 thePath = wxMacFSRefToPath( &theFSRef );
88a7a4e1 413
a449f840
DS
414 if (!thePath)
415 {
416 ::NavDisposeReply(&navReply);
417 return wxID_CANCEL;
a4f5b9b9 418 }
f3078f07 419
a4f5b9b9
GD
420 m_path = thePath;
421 m_paths.Add(m_path);
422 m_fileName = wxFileNameFromPath(m_path);
423 m_fileNames.Add(m_fileName);
424 }
f3078f07 425
a4f5b9b9
GD
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 }
f3078f07 431
a4f5b9b9 432 ::NavDisposeReply(&navReply);
685a634c 433
a4f5b9b9 434 return (err == noErr) ? wxID_OK : wxID_CANCEL;
5b781a67 435}