]> git.saurik.com Git - wxWidgets.git/blame - src/mac/filedlg.cpp
fix for GetParent() in wxTR_HIDE_ROOT case
[wxWidgets.git] / src / mac / filedlg.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: filedlg.cpp
2f1ae414 3// Purpose: wxFileDialog
e9576ca5
SC
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "filedlg.h"
14#endif
15
16#include "wx/defs.h"
5fde6fcc 17#include "wx/app.h"
e9576ca5
SC
18#include "wx/utils.h"
19#include "wx/dialog.h"
20#include "wx/filedlg.h"
21#include "wx/intl.h"
fe35d097 22#include "wx/tokenzr.h"
e9576ca5 23
f11bdd03 24#ifndef __DARWIN__
03e11df5
GD
25 #include "PLStringFuncs.h"
26#endif
5b781a67 27
2f1ae414 28#if !USE_SHARED_LIBRARY
e9576ca5 29IMPLEMENT_CLASS(wxFileDialog, wxDialog)
2f1ae414 30#endif
e9576ca5 31
519cb848
SC
32// begin wxmac
33
76a5e5d2
SC
34#include "wx/mac/private.h"
35
bb378910 36#include <Navigation.h>
5b781a67 37
da2b4b7a
GD
38#include "MoreFiles.h"
39#include "MoreFilesExtras.h"
519cb848 40
5b781a67
SC
41extern bool gUseNavServices ;
42
4d4d8bbf
SC
43// the data we need to pass to our standard file hook routine
44// includes a pointer to the dialog, a pointer to the standard
45// file reply record (so we can inspect the current selection)
46// and a copy of the "previous" file spec of the reply record
47// so we can see if the selection has changed
48
49const int kwxMacFileTypes = 10 ;
50
51struct OpenUserDataRec {
52 int currentfilter ;
53 wxString name [kwxMacFileTypes] ;
54 wxString extensions[kwxMacFileTypes] ;
55 OSType filtermactypes[kwxMacFileTypes] ;
56 int numfilters ;
57};
58typedef struct OpenUserDataRec
59 OpenUserDataRec, *OpenUserDataRecPtr;
60
5b781a67
SC
61static pascal void NavEventProc(
62 NavEventCallbackMessage inSelector,
63 NavCBRecPtr ioParams,
64 NavCallBackUserData ioUserData);
65
66#if TARGET_CARBON
67 static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc);
68#else
69 static NavEventUPP sStandardNavEventFilter = NewNavEventProc(NavEventProc);
70#endif
71
72static pascal void
73NavEventProc(
74 NavEventCallbackMessage inSelector,
75 NavCBRecPtr ioParams,
4d4d8bbf 76 NavCallBackUserData ioUserData )
5b781a67 77{
4d4d8bbf 78 OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ;
5b781a67
SC
79 if (inSelector == kNavCBEvent) {
80 // In Universal Headers 3.2, Apple changed the definition of
81 /*
82 #if UNIVERSAL_INTERFACES_VERSION >= 0x0320 // Universal Headers 3.2
83 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.eventDataParms.event));
84
85 #else
86 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.event));
87 #endif
88 */
89
5fde6fcc 90 wxTheApp->MacHandleOneEvent(ioParams->eventData.eventDataParms.event);
4d4d8bbf
SC
91 } else if ( inSelector == kNavCBPopupMenuSelect )
92 {
93 NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
94 data->currentfilter = menu->menuType ;
5b781a67
SC
95 }
96}
97
72055702 98const char * gfilters[] =
519cb848
SC
99{
100 "*.TXT" ,
4d4d8bbf
SC
101 "*.TIF" ,
102 "*.JPG" ,
519cb848
SC
103
104 NULL
105} ;
106
107OSType gfiltersmac[] =
108{
109 'TEXT' ,
4d4d8bbf
SC
110 'TIFF' ,
111 'JPEG' ,
519cb848
SC
112
113 '****'
114} ;
115
2f1ae414 116
519cb848 117
4d4d8bbf
SC
118void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
119{
120 myData->currentfilter = 0 ;
121
122 if ( filter && filter[0] )
123 {
124 wxString filter2(filter) ;
125 int filterIndex = 0;
126 bool isName = true ;
127 wxString current ;
128 for( unsigned int i = 0; i < filter2.Len(); i++ )
129 {
130 if( filter2.GetChar(i) == wxT('|') )
131 {
132 if( isName ) {
133 myData->name[filterIndex] = current ;
134 }
135 else {
136 myData->extensions[filterIndex] = current.MakeUpper() ;
137 ++filterIndex ;
138 }
139 isName = !isName ;
140 current = "" ;
141 }
142 else
143 {
144 current += filter2.GetChar(i) ;
145 }
146 }
fe35d097 147// if ( filterIndex > 0 )
4d4d8bbf
SC
148 {
149 wxASSERT_MSG( !isName , "incorrect format of format string" ) ;
150 myData->extensions[filterIndex] = current.MakeUpper() ;
151 ++filterIndex ;
152 }
153
154 myData->numfilters = filterIndex ;
155 for ( int i = 0 ; i < myData->numfilters ; i++ )
156 {
157 int j ;
158 for ( j = 0 ; gfilters[j] ; j++ )
159 {
160 if ( strcmp( myData->extensions[i] , gfilters[j] ) == 0 )
161 {
162 myData->filtermactypes[i] = gfiltersmac[j] ;
163 break ;
164 }
165 }
166 if( gfilters[j] == NULL )
167 {
168 myData->filtermactypes[i] = '****' ;
169 }
170 }
171 }
172 else
173 {
174 myData->numfilters = 0 ;
175 }
176
177}
bb378910 178
4d4d8bbf
SC
179static Boolean CheckFile( ConstStr255Param name , OSType type , OpenUserDataRecPtr data)
180{
da2b4b7a
GD
181 Str255 filename ;
182
183#if TARGET_CARBON
184 p2cstrcpy((char *)filename, name) ;
185#else
186 PLstrcpy( filename , name ) ;
9f92f6fb 187 p2cstr( filename ) ;
da2b4b7a 188#endif
9f92f6fb
SC
189 wxString file(filename) ;
190 file.MakeUpper() ;
191
da2b4b7a
GD
192 if ( data->numfilters > 0 )
193 {
194 //for ( int i = 0 ; i < data->numfilters ; ++i )
fe35d097
SC
195 int i = data->currentfilter ;
196 if ( data->extensions[i].Right(2) == ".*" )
197 return true ;
da2b4b7a 198
fe35d097
SC
199 {
200 if ( type == data->filtermactypes[i] )
201 return true ;
da2b4b7a 202
fe35d097
SC
203 wxStringTokenizer tokenizer( data->extensions[i] , ";" ) ;
204 while( tokenizer.HasMoreTokens() )
205 {
206 wxString extension = tokenizer.GetNextToken() ;
207 if ( extension.GetChar(0) == '*' )
208 extension = extension.Mid(1) ;
209
210 if ( file.Len() >= extension.Len() && extension == file.Right(extension.Len() ) )
211 return true ;
212 }
213 }
214 return false ;
da2b4b7a
GD
215 }
216 return true ;
4d4d8bbf
SC
217}
218
bb378910 219#ifndef __DARWIN__
5fde6fcc 220static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
519cb848 221{
519cb848
SC
222 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
223 // return true if this item is invisible or a file
224
225 Boolean visibleFlag;
226 Boolean folderFlag;
227
228 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
229 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
230
231 // because the semantics of the filter proc are "true means don't show
232 // it" we need to invert the result that we return
233
234 if ( !visibleFlag )
235 return true ;
236
237 if ( !folderFlag )
238 {
4d4d8bbf 239 return !CheckFile( myCInfoPBPtr->hFileInfo.ioNamePtr , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
519cb848
SC
240 }
241
242 return false ;
243}
bb378910 244#endif
519cb848
SC
245
246// end wxmac
247
248wxString wxFileSelector(const char *title,
e9576ca5
SC
249 const char *defaultDir, const char *defaultFileName,
250 const char *defaultExtension, const char *filter, int flags,
251 wxWindow *parent, int x, int y)
252{
253 // If there's a default extension specified but no filter, we create a suitable
254 // filter.
255
256 wxString filter2("");
257 if ( defaultExtension && !filter )
258 filter2 = wxString("*.") + wxString(defaultExtension) ;
259 else if ( filter )
260 filter2 = filter;
261
262 wxString defaultDirString;
263 if (defaultDir)
264 defaultDirString = defaultDir;
265 else
266 defaultDirString = "";
267
268 wxString defaultFilenameString;
269 if (defaultFileName)
270 defaultFilenameString = defaultFileName;
271 else
272 defaultFilenameString = "";
273
274 wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y));
275
276 if ( fileDialog.ShowModal() == wxID_OK )
277 {
278 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
279 return wxBuffer;
280 }
281 else
8be97d65 282 return wxGetEmptyString();
e9576ca5
SC
283}
284
169935ad 285WXDLLEXPORT wxString wxFileSelectorEx(const char *title,
e9576ca5
SC
286 const char *defaultDir,
287 const char *defaultFileName,
288 int* defaultFilterIndex,
289 const char *filter,
290 int flags,
291 wxWindow* parent,
292 int x,
293 int y)
294
295{
296 wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "",
297 defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y));
298
299 if ( fileDialog.ShowModal() == wxID_OK )
300 {
301 *defaultFilterIndex = fileDialog.GetFilterIndex();
302 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
303 return wxBuffer;
304 }
305 else
8be97d65 306 return wxGetEmptyString();
e9576ca5
SC
307}
308
309wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
310 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
311 long style, const wxPoint& pos)
312{
76a5e5d2 313 wxASSERT_MSG( NavServicesAvailable() , "Navigation Services are not running" ) ;
e9576ca5
SC
314 m_message = message;
315 m_dialogStyle = style;
316 m_parent = parent;
317 m_path = "";
318 m_fileName = defaultFileName;
319 m_dir = defaultDir;
320 m_wildCard = wildCard;
321 m_filterIndex = 1;
322}
323
4d4d8bbf 324
f11bdd03 325pascal Boolean CrossPlatformFilterCallback (
4d4d8bbf
SC
326 AEDesc *theItem,
327 void *info,
328 void *callBackUD,
329 NavFilterModes filterMode
330)
331{
332 bool display = true;
333 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
334
335 if (filterMode == kNavFilteringBrowserList)
336 {
337 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
338 if (theItem->descriptorType == typeFSS && !theInfo->isFolder)
339 {
340 FSSpec spec;
fe35d097 341 memcpy( &spec , *theItem->dataHandle , sizeof(FSSpec) ) ;
4d4d8bbf
SC
342 display = CheckFile( spec.name , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
343 }
344 }
345
346 return display;
347}
348
e9576ca5
SC
349int wxFileDialog::ShowModal()
350{
5b781a67
SC
351 NavDialogOptions mNavOptions;
352 NavObjectFilterUPP mNavFilterUPP = NULL;
353 NavPreviewUPP mNavPreviewUPP = NULL ;
354 NavReplyRecord mNavReply;
355 AEDesc mDefaultLocation ;
356 bool mSelectDefault = false ;
357
358 ::NavGetDefaultDialogOptions(&mNavOptions);
359
360 mNavFilterUPP = nil;
361 mNavPreviewUPP = nil;
362 mSelectDefault = false;
363 mNavReply.validRecord = false;
364 mNavReply.replacing = false;
365 mNavReply.isStationery = false;
366 mNavReply.translationNeeded = false;
367 mNavReply.selection.descriptorType = typeNull;
368 mNavReply.selection.dataHandle = nil;
369 mNavReply.keyScript = smSystemScript;
370 mNavReply.fileTranslation = nil;
371
372 // Set default location, the location
373 // that's displayed when the dialog
374 // first appears
375
376 FSSpec location ;
bedaf53e 377 wxMacFilename2FSSpec( m_dir , &location ) ;
5b781a67
SC
378 OSErr err = noErr ;
379
380 mDefaultLocation.descriptorType = typeNull;
381 mDefaultLocation.dataHandle = nil;
382
383 err = ::AECreateDesc(typeFSS, &location, sizeof(FSSpec), &mDefaultLocation );
384
385 if ( mDefaultLocation.dataHandle ) {
386
387 if (mSelectDefault) {
388 mNavOptions.dialogOptionFlags |= kNavSelectDefaultLocation;
389 } else {
390 mNavOptions.dialogOptionFlags &= ~kNavSelectDefaultLocation;
391 }
392 }
393
03e11df5
GD
394#if TARGET_CARBON
395 c2pstrcpy((StringPtr)mNavOptions.message, m_message) ;
396#else
5b781a67
SC
397 strcpy((char *)mNavOptions.message, m_message) ;
398 c2pstr((char *)mNavOptions.message ) ;
03e11df5
GD
399#endif
400#if TARGET_CARBON
401 c2pstrcpy((StringPtr)mNavOptions.savedFileName, m_fileName) ;
402#else
5b781a67
SC
403 strcpy((char *)mNavOptions.savedFileName, m_fileName) ;
404 c2pstr((char *)mNavOptions.savedFileName ) ;
03e11df5 405#endif
5b781a67
SC
406
407 if ( m_dialogStyle & wxSAVE )
408 {
409
410 mNavOptions.dialogOptionFlags |= kNavNoTypePopup ;
411 mNavOptions.dialogOptionFlags |= kNavDontAutoTranslate ;
412 mNavOptions.dialogOptionFlags |= kNavDontAddTranslateItems ;
413
414 err = ::NavPutFile(
415 &mDefaultLocation,
416 &mNavReply,
417 &mNavOptions,
418 sStandardNavEventFilter ,
419 'TEXT',
420 'TEXT',
421 0L); // User Data
422 }
423 else
424 {
4d4d8bbf
SC
425 OpenUserDataRec myData;
426 MakeUserDataRec( &myData , m_wildCard ) ;
427 NavTypeListHandle typelist = NULL ;
428
429 if ( myData.numfilters > 0 )
430 {
431 mNavOptions.popupExtension = (NavMenuItemSpecArrayHandle) NewHandle( sizeof( NavMenuItemSpec ) * myData.numfilters ) ;
432 for ( int i = 0 ; i < myData.numfilters ; ++i ) {
433 (*mNavOptions.popupExtension)[i].version = kNavMenuItemSpecVersion ;
434 (*mNavOptions.popupExtension)[i].menuCreator = 'WXNG' ;
435 (*mNavOptions.popupExtension)[i].menuType = i ;
436 #if TARGET_CARBON
437 c2pstrcpy((StringPtr)(*mNavOptions.popupExtension)[i].menuItemName, myData.name[i]) ;
438 #else
439 strcpy((char *)(*mNavOptions.popupExtension)[i].menuItemName, myData.name[i]) ;
440 c2pstr((char *)(*mNavOptions.popupExtension)[i].menuItemName ) ;
441 #endif
442 }
443 }
444
f11bdd03 445 mNavFilterUPP = NewNavObjectFilterUPP( CrossPlatformFilterCallback ) ;
5b781a67
SC
446 if ( m_dialogStyle & wxMULTIPLE )
447 mNavOptions.dialogOptionFlags |= kNavAllowMultipleFiles ;
448 else
449 mNavOptions.dialogOptionFlags &= ~kNavAllowMultipleFiles ;
450
451 err = ::NavGetFile(
452 &mDefaultLocation,
453 &mNavReply,
454 &mNavOptions,
455 sStandardNavEventFilter ,
456 mNavPreviewUPP,
457 mNavFilterUPP,
4d4d8bbf
SC
458 typelist /*inFileTypes.TypeListHandle() */,
459 &myData); // User Data
460 if ( typelist )
461 DisposeHandle( (Handle) typelist ) ;
5b781a67
SC
462 }
463
4d4d8bbf 464 DisposeNavObjectFilterUPP(mNavFilterUPP);
5b781a67
SC
465 if ( mDefaultLocation.dataHandle != nil )
466 {
467 ::AEDisposeDesc(&mDefaultLocation);
468 }
469
470 if ( (err != noErr) && (err != userCanceledErr) ) {
471 m_path = "" ;
472 return wxID_CANCEL ;
473 }
474
475 if (mNavReply.validRecord) {
476
477 FSSpec outFileSpec ;
478 AEDesc specDesc ;
fe35d097 479 AEKeyword keyWord ;
5b781a67
SC
480
481 long count ;
482 ::AECountItems( &mNavReply.selection , &count ) ;
483 for ( long i = 1 ; i <= count ; ++i )
484 {
fe35d097 485 OSErr err = ::AEGetNthDesc( &mNavReply.selection , i , typeFSS, &keyWord , &specDesc);
5b781a67
SC
486 if ( err != noErr ) {
487 m_path = "" ;
488 return wxID_CANCEL ;
489 }
490 outFileSpec = **(FSSpec**) specDesc.dataHandle;
491 if (specDesc.dataHandle != nil) {
492 ::AEDisposeDesc(&specDesc);
493 }
494
495
496 // outFolderDirID = thePB.dirInfo.ioDrDirID;
bedaf53e 497 m_path = wxMacFSSpec2MacFilename( &outFileSpec ) ;
5b781a67
SC
498 m_paths.Add( m_path ) ;
499 m_fileNames.Add(m_fileName);
500 }
501 // set these to the first hit
502 m_path = m_paths[ 0 ] ;
503 m_fileName = wxFileNameFromPath(m_path);
504 m_dir = wxPathOnly(m_path);
fe35d097 505 NavDisposeReply( &mNavReply ) ;
5b781a67
SC
506 return wxID_OK ;
507 }
508 return wxID_CANCEL;
5b781a67 509}
e9576ca5
SC
510
511// Generic file load/save dialog
169935ad 512static wxString
e9576ca5
SC
513wxDefaultFileSelector(bool load, const char *what, const char *extension, const char *default_name, wxWindow *parent)
514{
515 char *ext = (char *)extension;
516
517 char prompt[50];
518 wxString str;
519 if (load)
520 str = "Load %s file";
521 else
522 str = "Save %s file";
523 sprintf(prompt, wxGetTranslation(str), what);
524
525 if (*ext == '.') ext++;
526 char wild[60];
527 sprintf(wild, "*.%s", ext);
528
529 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
530}
531
532// Generic file load dialog
169935ad 533wxString
e9576ca5
SC
534wxLoadFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
535{
536 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
537}
538
539
540// Generic file save dialog
169935ad 541wxString
e9576ca5
SC
542wxSaveFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
543{
544 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
545}
546
547