]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/filedlg.cpp
Applied patch [ 1184295 ] wxDateTimePickerCtrl Create() fix for wxDP_ALLOWNONE
[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 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9576ca5
SC
13#pragma implementation "filedlg.h"
14#endif
15
3d1a4878
SC
16#include "wx/wxprec.h"
17
5fde6fcc 18#include "wx/app.h"
e9576ca5
SC
19#include "wx/utils.h"
20#include "wx/dialog.h"
21#include "wx/filedlg.h"
22#include "wx/intl.h"
fe35d097 23#include "wx/tokenzr.h"
5974c3cf 24#include "wx/filename.h"
e9576ca5 25
f11bdd03 26#ifndef __DARWIN__
03e11df5
GD
27 #include "PLStringFuncs.h"
28#endif
5b781a67 29
2f1ae414 30#if !USE_SHARED_LIBRARY
f74172ab 31IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
2f1ae414 32#endif
e9576ca5 33
519cb848
SC
34// begin wxmac
35
76a5e5d2
SC
36#include "wx/mac/private.h"
37
bb378910 38#include <Navigation.h>
5b781a67 39
a2b77260 40#include "MoreFilesX.h"
685a634c 41
5b781a67
SC
42extern bool gUseNavServices ;
43
4d4d8bbf
SC
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
4d4d8bbf 50struct OpenUserDataRec {
a4f5b9b9
GD
51 int currentfilter ;
52 bool saveMode ;
2b5f62a0
VZ
53 wxArrayString name ;
54 wxArrayString extensions ;
e40298d5 55 wxArrayLong filtermactypes ;
685a634c 56 wxString defaultLocation;
a4f5b9b9 57 CFArrayRef menuitems ;
4d4d8bbf 58};
2b5f62a0 59
4d4d8bbf 60typedef struct OpenUserDataRec
a4f5b9b9 61OpenUserDataRec, *OpenUserDataRecPtr;
4d4d8bbf 62
e40298d5
JS
63static pascal void NavEventProc(
64 NavEventCallbackMessage inSelector,
65 NavCBRecPtr ioParams,
66 NavCallBackUserData ioUserData);
5b781a67 67
a2b77260 68static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc);
5b781a67
SC
69
70static pascal void
71NavEventProc(
e40298d5
JS
72 NavEventCallbackMessage inSelector,
73 NavCBRecPtr ioParams,
74 NavCallBackUserData ioUserData )
5b781a67 75{
e40298d5 76 OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ;
a4f5b9b9 77 if (inSelector == kNavCBEvent) {
685a634c 78 }
e40298d5
JS
79 else if ( inSelector == kNavCBStart )
80 {
a4f5b9b9
GD
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 }
75338075
DS
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);
e40298d5
JS
98 }
99 else if ( inSelector == kNavCBPopupMenuSelect )
100 {
101 NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
75338075
DS
102 const size_t numFilters = data->extensions.GetCount();
103
104 if ( menu->menuType < numFilters )
e40298d5
JS
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() ;
f65d4b83 112 wxString sfilename ;
685a634c 113
4891a3d5 114 wxMacCFStringHolder cfString( NavDialogGetSaveFileName( ioParams->context ) , false );
f65d4b83 115 sfilename = cfString.AsString() ;
f65d4b83 116
685a634c 117 int pos = sfilename.Find('.', true) ;
e40298d5
JS
118 if ( pos != wxNOT_FOUND )
119 {
120 sfilename = sfilename.Left(pos+1)+extension ;
a9412f8f 121 cfString.Assign( sfilename , wxFONTENCODING_DEFAULT ) ;
f65d4b83 122 NavDialogSetSaveFileName( ioParams->context , cfString ) ;
e40298d5
JS
123 }
124 }
f65d4b83 125 }
e40298d5 126 }
5b781a67
SC
127}
128
2f1ae414 129
e40298d5 130void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
4d4d8bbf 131{
e40298d5
JS
132 myData->menuitems = NULL ;
133 myData->currentfilter = 0 ;
685a634c
DS
134 myData->saveMode = false ;
135
e40298d5 136 if ( filter && filter[0] )
4d4d8bbf 137 {
e40298d5
JS
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 ;
427ff662 154 current = wxEmptyString ;
e40298d5
JS
155 }
156 else
157 {
158 current += filter2.GetChar(i) ;
159 }
4d4d8bbf 160 }
e40298d5
JS
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
685a634c 163
427ff662 164 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
e40298d5
JS
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() ) ;
685a634c 171
e40298d5 172 ++filterIndex ;
685a634c 173
2b5f62a0 174 const size_t extCount = myData->extensions.GetCount();
e40298d5
JS
175 for ( size_t i = 0 ; i < extCount; i++ )
176 {
5974c3cf
SC
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) == '.')
e40298d5 185 {
5974c3cf 186 extension = extension.Mid(1); // Remove leading .
e40298d5 187 }
5974c3cf
SC
188
189 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
e40298d5 190 {
5974c3cf 191 myData->filtermactypes.Add( (OSType)fileType );
e40298d5 192 }
5974c3cf
SC
193 else
194 {
195 myData->filtermactypes.Add( '****' ) ; // We'll fail safe if it's not recognized
196 }
e40298d5
JS
197 }
198 }
4d4d8bbf 199}
bb378910 200
f65d4b83 201static Boolean CheckFile( const wxString &filename , OSType type , OpenUserDataRecPtr data)
4d4d8bbf 202{
a4f5b9b9 203 wxString file(filename) ;
9f92f6fb 204 file.MakeUpper() ;
685a634c 205
2b5f62a0 206 if ( data->extensions.GetCount() > 0 )
da2b4b7a 207 {
e40298d5
JS
208 //for ( int i = 0 ; i < data->numfilters ; ++i )
209 int i = data->currentfilter ;
427ff662 210 if ( data->extensions[i].Right(2) == wxT(".*") )
e40298d5 211 return true ;
685a634c 212
e40298d5
JS
213 {
214 if ( type == (OSType)data->filtermactypes[i] )
215 return true ;
685a634c 216
427ff662 217 wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
e40298d5
JS
218 while( tokenizer.HasMoreTokens() )
219 {
220 wxString extension = tokenizer.GetNextToken() ;
221 if ( extension.GetChar(0) == '*' )
222 extension = extension.Mid(1) ;
685a634c 223
e40298d5
JS
224 if ( file.Len() >= extension.Len() && extension == file.Right(extension.Len() ) )
225 return true ;
226 }
227 }
228 return false ;
da2b4b7a
GD
229 }
230 return true ;
4d4d8bbf
SC
231}
232
a2b77260 233#if !TARGET_API_MAC_OSX
5fde6fcc 234static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
a4f5b9b9 235{
e40298d5
JS
236 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
237 // return true if this item is invisible or a file
238
239 Boolean visibleFlag;
240 Boolean folderFlag;
685a634c 241
e40298d5
JS
242 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
243 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
685a634c 244
e40298d5
JS
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
685a634c 247
e40298d5
JS
248 if ( !visibleFlag )
249 return true ;
685a634c 250
e40298d5
JS
251 if ( !folderFlag )
252 {
a4f5b9b9 253 wxString file = wxMacMakeStringFromPascal( myCInfoPBPtr->hFileInfo.ioNamePtr ) ;
f65d4b83 254 return !CheckFile( file , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
a4f5b9b9 255 }
685a634c 256
e40298d5 257 return false ;
519cb848 258}
bb378910 259#endif
519cb848
SC
260
261// end wxmac
262
e9576ca5
SC
263wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
264 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
265 long style, const wxPoint& pos)
f74172ab 266 :wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos)
e9576ca5 267{
427ff662 268 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
e9576ca5
SC
269}
270
f11bdd03 271pascal Boolean CrossPlatformFilterCallback (
685a634c
DS
272 AEDesc *theItem,
273 void *info,
274 void *callBackUD,
4d4d8bbf
SC
275 NavFilterModes filterMode
276)
277{
2d4e4f80
GD
278 bool display = true;
279 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
685a634c 280
2d4e4f80
GD
281 if (filterMode == kNavFilteringBrowserList)
282 {
283 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
f65d4b83 284 if ( !theInfo->isFolder )
2d4e4f80 285 {
f65d4b83
SC
286 if (theItem->descriptorType == typeFSS )
287 {
288 FSSpec spec;
289 memcpy( &spec , *theItem->dataHandle , sizeof(FSSpec) ) ;
a4f5b9b9 290 wxString file = wxMacMakeStringFromPascal( spec.name ) ;
f65d4b83 291 display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
685a634c 292 }
f65d4b83
SC
293 else if ( theItem->descriptorType == typeFSRef )
294 {
295 FSRef fsref ;
296 memcpy( &fsref , *theItem->dataHandle , sizeof(FSRef) ) ;
a2b77260 297 wxString file = wxMacFSRefToPath( &fsref ) ;
f65d4b83
SC
298 display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
299 }
2d4e4f80
GD
300 }
301 }
685a634c 302
2d4e4f80 303 return display;
4d4d8bbf
SC
304}
305
e9576ca5
SC
306int wxFileDialog::ShowModal()
307{
a4f5b9b9
GD
308 OSErr err;
309 NavDialogCreationOptions dialogCreateOptions;
310 // set default options
311 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
685a634c 312
a4f5b9b9
GD
313 // this was always unset in the old code
314 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
685a634c 315
e71800ba
SC
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
a4f5b9b9
GD
323 NavDialogRef dialog;
324 NavObjectFilterUPP navFilterUPP = NULL;
685a634c 325 CFArrayRef cfArray = NULL; // for popupExtension
a4f5b9b9
GD
326 OpenUserDataRec myData;
327 myData.defaultLocation = m_dir;
328
75338075
DS
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
a4f5b9b9
GD
344 if (m_dialogStyle & wxSAVE)
345 {
75338075
DS
346 myData.saveMode = true;
347
348 if (!numFilters)
349 {
350 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
351 }
685a634c
DS
352 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
353 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
354
355 // The extension is important
a485ee6f
JS
356 if (numFilters < 2)
357 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
685a634c 358
78eeb095
SC
359#if TARGET_API_MAC_OSX
360 if (!(m_dialogStyle & wxOVERWRITE_PROMPT))
361 {
362 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
363 }
364#endif
a4f5b9b9 365 err = ::NavCreatePutFileDialog(&dialogCreateOptions,
75338075
DS
366 // Suppresses the 'Default' (top) menu item
367 kNavGenericSignature, kNavGenericSignature,
685a634c
DS
368 sStandardNavEventFilter,
369 &myData, // for defaultLocation
370 &dialog);
a4f5b9b9
GD
371 }
372 else
373 {
c11d9cb8
KO
374
375 //let people select bundles/programs in dialogs
376 dialogCreateOptions.optionFlags |= kNavSupportPackages;
377
a4f5b9b9
GD
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);
685a634c 390
a4f5b9b9
GD
391 // clean up filter related data, etc.
392 if (navFilterUPP)
393 ::DisposeNavObjectFilterUPP(navFilterUPP);
394 if (cfArray)
a4f5b9b9 395 ::CFRelease(cfArray);
e71800ba 396
685a634c 397 if (err != noErr)
a4f5b9b9
GD
398 return wxID_CANCEL;
399
400 NavReplyRecord navReply;
401 err = ::NavDialogGetReply(dialog, &navReply);
685a634c 402 if (err == noErr && navReply.validRecord)
a4f5b9b9
GD
403 {
404 AEKeyword theKeyword;
405 DescType actualType;
406 Size actualSize;
407 FSRef theFSRef;
878973f1 408 wxString thePath ;
75338075
DS
409
410 m_filterIndex = myData.currentfilter ;
411
a4f5b9b9
GD
412 long count;
413 ::AECountItems(&navReply.selection , &count);
414 for (long i = 1; i <= count; ++i)
415 {
d1b3039b 416 err = ::AEGetNthPtr(&(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
a4f5b9b9 417 &theFSRef, sizeof(theFSRef), &actualSize);
685a634c 418 if (err != noErr)
a4f5b9b9
GD
419 break;
420
421 if (m_dialogStyle & wxSAVE)
a2b77260 422 thePath = wxMacFSRefToPath( &theFSRef , navReply.saveFileName ) ;
685a634c 423 else
a2b77260
SC
424 thePath = wxMacFSRefToPath( &theFSRef ) ;
425
a449f840
DS
426 if (!thePath)
427 {
428 ::NavDisposeReply(&navReply);
429 return wxID_CANCEL;
a4f5b9b9
GD
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);
685a634c 442
a4f5b9b9 443 return (err == noErr) ? wxID_OK : wxID_CANCEL;
5b781a67 444}
e9576ca5 445