]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/filedlg.cpp
Fixing compilation on Mac, and attempting to better calculate the bottom position...
[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
a2b77260 33#include "MoreFilesX.h"
685a634c 34
f3078f07
DS
35IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
36
37extern bool gUseNavServices;
5b781a67 38
4d4d8bbf
SC
39// the data we need to pass to our standard file hook routine
40// includes a pointer to the dialog, a pointer to the standard
41// file reply record (so we can inspect the current selection)
42// and a copy of the "previous" file spec of the reply record
43// so we can see if the selection has changed
44
f3078f07
DS
45struct OpenUserDataRec
46{
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
f3078f07
DS
59static pascal void NavEventProc(
60 NavEventCallbackMessage inSelector,
61 NavCBRecPtr ioParams,
62 NavCallBackUserData ioUserData );
5b781a67 63
f3078f07 64static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc);
5b781a67 65
f3078f07
DS
66static pascal void NavEventProc(
67 NavEventCallbackMessage inSelector,
68 NavCBRecPtr ioParams,
69 NavCallBackUserData ioUserData )
5b781a67 70{
e40298d5 71 OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ;
f3078f07
DS
72 if (inSelector == kNavCBEvent)
73 {
685a634c 74 }
e40298d5
JS
75 else if ( inSelector == kNavCBStart )
76 {
de6185e2 77 if (data && !(data->defaultLocation).empty())
a4f5b9b9
GD
78 {
79 // Set default location for the modern Navigation APIs
80 // Apple Technical Q&A 1151
81 FSSpec theFSSpec;
82 wxMacFilename2FSSpec(data->defaultLocation, &theFSSpec);
f3078f07 83 AEDesc theLocation = { typeNull, NULL };
a4f5b9b9
GD
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
f3078f07 125void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
4d4d8bbf 126{
e40298d5
JS
127 myData->menuitems = NULL ;
128 myData->currentfilter = 0 ;
685a634c
DS
129 myData->saveMode = false ;
130
e40298d5 131 if ( filter && filter[0] )
4d4d8bbf 132 {
e40298d5
JS
133 wxString filter2(filter) ;
134 int filterIndex = 0;
135 bool isName = true ;
136 wxString current ;
f3078f07 137
e031f1df 138 for ( unsigned int i = 0; i < filter2.length() ; i++ )
e40298d5 139 {
f3078f07 140 if ( filter2.GetChar(i) == wxT('|') )
e40298d5 141 {
f3078f07
DS
142 if ( isName )
143 {
e40298d5
JS
144 myData->name.Add( current ) ;
145 }
f3078f07
DS
146 else
147 {
e40298d5
JS
148 myData->extensions.Add( current.MakeUpper() ) ;
149 ++filterIndex ;
150 }
f3078f07 151
e40298d5 152 isName = !isName ;
427ff662 153 current = wxEmptyString ;
e40298d5
JS
154 }
155 else
156 {
157 current += filter2.GetChar(i) ;
158 }
4d4d8bbf 159 }
e40298d5
JS
160 // we allow for compatibility reason to have a single filter expression (like *.*) without
161 // an explanatory text, in that case the first part is name and extension at the same time
685a634c 162
427ff662 163 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
88a7a4e1 164 if ( current.empty() )
e40298d5
JS
165 myData->extensions.Add( myData->name[filterIndex] ) ;
166 else
167 myData->extensions.Add( current.MakeUpper() ) ;
168 if ( filterIndex == 0 || isName )
169 myData->name.Add( current.MakeUpper() ) ;
685a634c 170
e40298d5 171 ++filterIndex ;
685a634c 172
2b5f62a0 173 const size_t extCount = myData->extensions.GetCount();
e40298d5
JS
174 for ( size_t i = 0 ; i < extCount; i++ )
175 {
f3078f07 176 wxUint32 fileType, creator;
5974c3cf
SC
177 wxString extension = myData->extensions[i];
178
f3078f07 179 // Remove leading '*'
6264c8e2 180 if (extension.length() && (extension.GetChar(0) == '*'))
f3078f07 181 extension = extension.Mid( 1 );
5974c3cf 182
f3078f07 183 // Remove leading '.'
6264c8e2 184 if (extension.length() && (extension.GetChar(0) == '.'))
f3078f07 185 extension = extension.Mid( 1 );
88a7a4e1 186
5974c3cf 187 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
5974c3cf 188 myData->filtermactypes.Add( (OSType)fileType );
5974c3cf 189 else
f3078f07 190 myData->filtermactypes.Add( '****' ); // We'll fail safe if it's not recognized
e40298d5
JS
191 }
192 }
4d4d8bbf 193}
bb378910 194
f65d4b83 195static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data)
4d4d8bbf 196{
a4f5b9b9 197 wxString file(filename) ;
9f92f6fb 198 file.MakeUpper() ;
685a634c 199
2b5f62a0 200 if ( data->extensions.GetCount() > 0 )
da2b4b7a 201 {
e40298d5
JS
202 //for ( int i = 0 ; i < data->numfilters ; ++i )
203 int i = data->currentfilter ;
427ff662 204 if ( data->extensions[i].Right(2) == wxT(".*") )
e40298d5 205 return true ;
685a634c 206
e40298d5
JS
207 {
208 if ( type == (OSType)data->filtermactypes[i] )
209 return true ;
685a634c 210
427ff662 211 wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
f3078f07 212 while ( tokenizer.HasMoreTokens() )
e40298d5
JS
213 {
214 wxString extension = tokenizer.GetNextToken() ;
215 if ( extension.GetChar(0) == '*' )
216 extension = extension.Mid(1) ;
685a634c 217
e031f1df 218 if ( file.length() >= extension.length() && extension == file.Right(extension.length() ) )
e40298d5
JS
219 return true ;
220 }
221 }
f3078f07 222
e40298d5 223 return false ;
da2b4b7a 224 }
f3078f07 225
da2b4b7a 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
f3078f07
DS
259wxFileDialog::wxFileDialog(
260 wxWindow *parent, const wxString& message,
261 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
ff3e84ff
VZ
262 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
263 : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
e9576ca5 264{
427ff662 265 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
e9576ca5
SC
266}
267
f3078f07 268pascal Boolean CrossPlatformFilterCallback(
685a634c
DS
269 AEDesc *theItem,
270 void *info,
271 void *callBackUD,
f3078f07 272 NavFilterModes filterMode )
4d4d8bbf 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 {
f3078f07 282 if (theItem->descriptorType == typeFSS)
f65d4b83
SC
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;
f3078f07 306
a4f5b9b9
GD
307 // set default options
308 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
685a634c 309
a4f5b9b9
GD
310 // this was always unset in the old code
311 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
685a634c 312
e71800ba
SC
313 wxMacCFStringHolder message(m_message, m_font.GetEncoding());
314 dialogCreateOptions.windowTitle = message;
315
316 wxMacCFStringHolder defaultFileName(m_fileName, m_font.GetEncoding());
317 dialogCreateOptions.saveFileName = defaultFileName;
318
319
a4f5b9b9
GD
320 NavDialogRef dialog;
321 NavObjectFilterUPP navFilterUPP = NULL;
685a634c 322 CFArrayRef cfArray = NULL; // for popupExtension
a4f5b9b9
GD
323 OpenUserDataRec myData;
324 myData.defaultLocation = m_dir;
325
75338075
DS
326 MakeUserDataRec(&myData , m_wildCard);
327 myData.currentfilter = m_filterIndex;
328 size_t numFilters = myData.extensions.GetCount();
329 if (numFilters)
330 {
331 CFMutableArrayRef popup = CFArrayCreateMutable( kCFAllocatorDefault ,
332 numFilters , &kCFTypeArrayCallBacks ) ;
333 dialogCreateOptions.popupExtension = popup ;
334 myData.menuitems = dialogCreateOptions.popupExtension ;
335 for ( size_t i = 0 ; i < numFilters ; ++i )
336 {
337 CFArrayAppendValue( popup , (CFStringRef) wxMacCFStringHolder( myData.name[i] , m_font.GetEncoding() ) ) ;
338 }
339 }
340
ff3e84ff 341 if (HasFlag(wxFD_SAVE))
a4f5b9b9 342 {
75338075
DS
343 myData.saveMode = true;
344
685a634c
DS
345 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
346 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
f3078f07
DS
347 if (!numFilters)
348 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
685a634c
DS
349
350 // The extension is important
a485ee6f
JS
351 if (numFilters < 2)
352 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
685a634c 353
78eeb095 354#if TARGET_API_MAC_OSX
e031f1df 355 if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT))
f3078f07 356 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
78eeb095 357#endif
f3078f07
DS
358
359 err = ::NavCreatePutFileDialog(
360 &dialogCreateOptions,
361 kNavGenericSignature, // Suppresses the 'Default' (top) menu item
362 kNavGenericSignature,
363 sStandardNavEventFilter,
364 &myData, // for defaultLocation
365 &dialog );
a4f5b9b9
GD
366 }
367 else
368 {
f3078f07 369 // let the user select bundles/programs in dialogs
c11d9cb8 370 dialogCreateOptions.optionFlags |= kNavSupportPackages;
88a7a4e1 371
a4f5b9b9 372 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
f3078f07
DS
373 err = ::NavCreateGetFileDialog(
374 &dialogCreateOptions,
375 NULL, // NavTypeListHandle
376 sStandardNavEventFilter,
377 NULL, // NavPreviewUPP
378 navFilterUPP,
379 (void *) &myData, // inClientData
380 &dialog );
a4f5b9b9
GD
381 }
382
383 if (err == noErr)
384 err = ::NavDialogRun(dialog);
685a634c 385
a4f5b9b9
GD
386 // clean up filter related data, etc.
387 if (navFilterUPP)
388 ::DisposeNavObjectFilterUPP(navFilterUPP);
389 if (cfArray)
a4f5b9b9 390 ::CFRelease(cfArray);
e71800ba 391
685a634c 392 if (err != noErr)
a4f5b9b9
GD
393 return wxID_CANCEL;
394
395 NavReplyRecord navReply;
396 err = ::NavDialogGetReply(dialog, &navReply);
685a634c 397 if (err == noErr && navReply.validRecord)
a4f5b9b9
GD
398 {
399 AEKeyword theKeyword;
400 DescType actualType;
401 Size actualSize;
402 FSRef theFSRef;
878973f1 403 wxString thePath ;
a4f5b9b9 404 long count;
f3078f07
DS
405
406 m_filterIndex = myData.currentfilter;
407 ::AECountItems( &navReply.selection, &count );
a4f5b9b9
GD
408 for (long i = 1; i <= count; ++i)
409 {
f3078f07
DS
410 err = ::AEGetNthPtr(
411 &(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
412 &theFSRef, sizeof(theFSRef), &actualSize );
685a634c 413 if (err != noErr)
a4f5b9b9
GD
414 break;
415
ff3e84ff 416 if (HasFlag(wxFD_SAVE))
f3078f07 417 thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName );
685a634c 418 else
f3078f07 419 thePath = wxMacFSRefToPath( &theFSRef );
88a7a4e1 420
a449f840
DS
421 if (!thePath)
422 {
423 ::NavDisposeReply(&navReply);
424 return wxID_CANCEL;
a4f5b9b9 425 }
f3078f07 426
a4f5b9b9
GD
427 m_path = thePath;
428 m_paths.Add(m_path);
429 m_fileName = wxFileNameFromPath(m_path);
430 m_fileNames.Add(m_fileName);
431 }
f3078f07 432
a4f5b9b9
GD
433 // set these to the first hit
434 m_path = m_paths[0];
435 m_fileName = wxFileNameFromPath(m_path);
436 m_dir = wxPathOnly(m_path);
437 }
f3078f07 438
a4f5b9b9 439 ::NavDisposeReply(&navReply);
685a634c 440
a4f5b9b9 441 return (err == noErr) ? wxID_OK : wxID_CANCEL;
5b781a67 442}