]> git.saurik.com Git - wxWidgets.git/blame - src/mac/filedlg.cpp
Added effects.h to filelist.txt so it will get installed, fixed a
[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 }
d1aba6db
SC
147 // we allow for compatibility reason to have a single filter expression (like *.*) without
148 // an explanatory text, in that case the first part is name and extension at the same time
149
150 wxASSERT_MSG( filterIndex == 0 || !isName , "incorrect format of format string" ) ;
151 if ( current.IsEmpty() )
152 myData->extensions[filterIndex] = myData->name[filterIndex] ;
153 else
154 myData->extensions[filterIndex] = current.MakeUpper() ;
155 ++filterIndex ;
156
4d4d8bbf
SC
157
158 myData->numfilters = filterIndex ;
159 for ( int i = 0 ; i < myData->numfilters ; i++ )
160 {
161 int j ;
162 for ( j = 0 ; gfilters[j] ; j++ )
163 {
164 if ( strcmp( myData->extensions[i] , gfilters[j] ) == 0 )
165 {
166 myData->filtermactypes[i] = gfiltersmac[j] ;
167 break ;
168 }
169 }
170 if( gfilters[j] == NULL )
171 {
172 myData->filtermactypes[i] = '****' ;
173 }
174 }
175 }
176 else
177 {
178 myData->numfilters = 0 ;
179 }
180
181}
bb378910 182
4d4d8bbf
SC
183static Boolean CheckFile( ConstStr255Param name , OSType type , OpenUserDataRecPtr data)
184{
da2b4b7a
GD
185 Str255 filename ;
186
187#if TARGET_CARBON
188 p2cstrcpy((char *)filename, name) ;
189#else
190 PLstrcpy( filename , name ) ;
9f92f6fb 191 p2cstr( filename ) ;
da2b4b7a 192#endif
9f92f6fb
SC
193 wxString file(filename) ;
194 file.MakeUpper() ;
195
da2b4b7a
GD
196 if ( data->numfilters > 0 )
197 {
198 //for ( int i = 0 ; i < data->numfilters ; ++i )
fe35d097
SC
199 int i = data->currentfilter ;
200 if ( data->extensions[i].Right(2) == ".*" )
201 return true ;
da2b4b7a 202
fe35d097
SC
203 {
204 if ( type == data->filtermactypes[i] )
205 return true ;
da2b4b7a 206
fe35d097
SC
207 wxStringTokenizer tokenizer( data->extensions[i] , ";" ) ;
208 while( tokenizer.HasMoreTokens() )
209 {
210 wxString extension = tokenizer.GetNextToken() ;
211 if ( extension.GetChar(0) == '*' )
212 extension = extension.Mid(1) ;
213
214 if ( file.Len() >= extension.Len() && extension == file.Right(extension.Len() ) )
215 return true ;
216 }
217 }
218 return false ;
da2b4b7a
GD
219 }
220 return true ;
4d4d8bbf
SC
221}
222
bb378910 223#ifndef __DARWIN__
5fde6fcc 224static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
519cb848 225{
519cb848
SC
226 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
227 // return true if this item is invisible or a file
228
229 Boolean visibleFlag;
230 Boolean folderFlag;
231
232 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
233 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
234
235 // because the semantics of the filter proc are "true means don't show
236 // it" we need to invert the result that we return
237
238 if ( !visibleFlag )
239 return true ;
240
241 if ( !folderFlag )
242 {
4d4d8bbf 243 return !CheckFile( myCInfoPBPtr->hFileInfo.ioNamePtr , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
519cb848
SC
244 }
245
246 return false ;
247}
bb378910 248#endif
519cb848
SC
249
250// end wxmac
251
252wxString wxFileSelector(const char *title,
e9576ca5
SC
253 const char *defaultDir, const char *defaultFileName,
254 const char *defaultExtension, const char *filter, int flags,
255 wxWindow *parent, int x, int y)
256{
257 // If there's a default extension specified but no filter, we create a suitable
258 // filter.
259
260 wxString filter2("");
261 if ( defaultExtension && !filter )
262 filter2 = wxString("*.") + wxString(defaultExtension) ;
263 else if ( filter )
264 filter2 = filter;
265
266 wxString defaultDirString;
267 if (defaultDir)
268 defaultDirString = defaultDir;
269 else
270 defaultDirString = "";
271
272 wxString defaultFilenameString;
273 if (defaultFileName)
274 defaultFilenameString = defaultFileName;
275 else
276 defaultFilenameString = "";
277
278 wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y));
279
280 if ( fileDialog.ShowModal() == wxID_OK )
281 {
282 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
283 return wxBuffer;
284 }
285 else
8be97d65 286 return wxGetEmptyString();
e9576ca5
SC
287}
288
169935ad 289WXDLLEXPORT wxString wxFileSelectorEx(const char *title,
e9576ca5
SC
290 const char *defaultDir,
291 const char *defaultFileName,
292 int* defaultFilterIndex,
293 const char *filter,
294 int flags,
295 wxWindow* parent,
296 int x,
297 int y)
298
299{
300 wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "",
301 defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y));
302
303 if ( fileDialog.ShowModal() == wxID_OK )
304 {
305 *defaultFilterIndex = fileDialog.GetFilterIndex();
306 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
307 return wxBuffer;
308 }
309 else
8be97d65 310 return wxGetEmptyString();
e9576ca5
SC
311}
312
313wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
314 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
315 long style, const wxPoint& pos)
316{
76a5e5d2 317 wxASSERT_MSG( NavServicesAvailable() , "Navigation Services are not running" ) ;
e9576ca5
SC
318 m_message = message;
319 m_dialogStyle = style;
320 m_parent = parent;
321 m_path = "";
322 m_fileName = defaultFileName;
323 m_dir = defaultDir;
324 m_wildCard = wildCard;
325 m_filterIndex = 1;
326}
327
4d4d8bbf 328
f11bdd03 329pascal Boolean CrossPlatformFilterCallback (
4d4d8bbf
SC
330 AEDesc *theItem,
331 void *info,
332 void *callBackUD,
333 NavFilterModes filterMode
334)
335{
336 bool display = true;
337 OpenUserDataRecPtr data = (OpenUserDataRecPtr) callBackUD ;
338
339 if (filterMode == kNavFilteringBrowserList)
340 {
341 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
342 if (theItem->descriptorType == typeFSS && !theInfo->isFolder)
343 {
344 FSSpec spec;
fe35d097 345 memcpy( &spec , *theItem->dataHandle , sizeof(FSSpec) ) ;
4d4d8bbf
SC
346 display = CheckFile( spec.name , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
347 }
348 }
349
350 return display;
351}
352
e9576ca5
SC
353int wxFileDialog::ShowModal()
354{
5b781a67
SC
355 NavDialogOptions mNavOptions;
356 NavObjectFilterUPP mNavFilterUPP = NULL;
357 NavPreviewUPP mNavPreviewUPP = NULL ;
358 NavReplyRecord mNavReply;
359 AEDesc mDefaultLocation ;
360 bool mSelectDefault = false ;
361
362 ::NavGetDefaultDialogOptions(&mNavOptions);
363
364 mNavFilterUPP = nil;
365 mNavPreviewUPP = nil;
366 mSelectDefault = false;
367 mNavReply.validRecord = false;
368 mNavReply.replacing = false;
369 mNavReply.isStationery = false;
370 mNavReply.translationNeeded = false;
371 mNavReply.selection.descriptorType = typeNull;
372 mNavReply.selection.dataHandle = nil;
373 mNavReply.keyScript = smSystemScript;
374 mNavReply.fileTranslation = nil;
375
376 // Set default location, the location
377 // that's displayed when the dialog
378 // first appears
379
380 FSSpec location ;
bedaf53e 381 wxMacFilename2FSSpec( m_dir , &location ) ;
5b781a67
SC
382 OSErr err = noErr ;
383
384 mDefaultLocation.descriptorType = typeNull;
385 mDefaultLocation.dataHandle = nil;
386
387 err = ::AECreateDesc(typeFSS, &location, sizeof(FSSpec), &mDefaultLocation );
388
389 if ( mDefaultLocation.dataHandle ) {
390
391 if (mSelectDefault) {
392 mNavOptions.dialogOptionFlags |= kNavSelectDefaultLocation;
393 } else {
394 mNavOptions.dialogOptionFlags &= ~kNavSelectDefaultLocation;
395 }
396 }
397
03e11df5
GD
398#if TARGET_CARBON
399 c2pstrcpy((StringPtr)mNavOptions.message, m_message) ;
400#else
5b781a67
SC
401 strcpy((char *)mNavOptions.message, m_message) ;
402 c2pstr((char *)mNavOptions.message ) ;
03e11df5
GD
403#endif
404#if TARGET_CARBON
405 c2pstrcpy((StringPtr)mNavOptions.savedFileName, m_fileName) ;
406#else
5b781a67
SC
407 strcpy((char *)mNavOptions.savedFileName, m_fileName) ;
408 c2pstr((char *)mNavOptions.savedFileName ) ;
03e11df5 409#endif
5b781a67
SC
410
411 if ( m_dialogStyle & wxSAVE )
412 {
413
414 mNavOptions.dialogOptionFlags |= kNavNoTypePopup ;
415 mNavOptions.dialogOptionFlags |= kNavDontAutoTranslate ;
416 mNavOptions.dialogOptionFlags |= kNavDontAddTranslateItems ;
417
418 err = ::NavPutFile(
419 &mDefaultLocation,
420 &mNavReply,
421 &mNavOptions,
422 sStandardNavEventFilter ,
423 'TEXT',
424 'TEXT',
425 0L); // User Data
426 }
427 else
428 {
4d4d8bbf
SC
429 OpenUserDataRec myData;
430 MakeUserDataRec( &myData , m_wildCard ) ;
431 NavTypeListHandle typelist = NULL ;
432
433 if ( myData.numfilters > 0 )
434 {
435 mNavOptions.popupExtension = (NavMenuItemSpecArrayHandle) NewHandle( sizeof( NavMenuItemSpec ) * myData.numfilters ) ;
436 for ( int i = 0 ; i < myData.numfilters ; ++i ) {
437 (*mNavOptions.popupExtension)[i].version = kNavMenuItemSpecVersion ;
438 (*mNavOptions.popupExtension)[i].menuCreator = 'WXNG' ;
439 (*mNavOptions.popupExtension)[i].menuType = i ;
440 #if TARGET_CARBON
441 c2pstrcpy((StringPtr)(*mNavOptions.popupExtension)[i].menuItemName, myData.name[i]) ;
442 #else
443 strcpy((char *)(*mNavOptions.popupExtension)[i].menuItemName, myData.name[i]) ;
444 c2pstr((char *)(*mNavOptions.popupExtension)[i].menuItemName ) ;
445 #endif
446 }
447 }
448
f11bdd03 449 mNavFilterUPP = NewNavObjectFilterUPP( CrossPlatformFilterCallback ) ;
5b781a67
SC
450 if ( m_dialogStyle & wxMULTIPLE )
451 mNavOptions.dialogOptionFlags |= kNavAllowMultipleFiles ;
452 else
453 mNavOptions.dialogOptionFlags &= ~kNavAllowMultipleFiles ;
454
455 err = ::NavGetFile(
456 &mDefaultLocation,
457 &mNavReply,
458 &mNavOptions,
459 sStandardNavEventFilter ,
460 mNavPreviewUPP,
461 mNavFilterUPP,
4d4d8bbf
SC
462 typelist /*inFileTypes.TypeListHandle() */,
463 &myData); // User Data
464 if ( typelist )
465 DisposeHandle( (Handle) typelist ) ;
5b781a67
SC
466 }
467
4d4d8bbf 468 DisposeNavObjectFilterUPP(mNavFilterUPP);
5b781a67
SC
469 if ( mDefaultLocation.dataHandle != nil )
470 {
471 ::AEDisposeDesc(&mDefaultLocation);
472 }
473
474 if ( (err != noErr) && (err != userCanceledErr) ) {
475 m_path = "" ;
476 return wxID_CANCEL ;
477 }
478
479 if (mNavReply.validRecord) {
480
481 FSSpec outFileSpec ;
482 AEDesc specDesc ;
fe35d097 483 AEKeyword keyWord ;
5b781a67
SC
484
485 long count ;
486 ::AECountItems( &mNavReply.selection , &count ) ;
487 for ( long i = 1 ; i <= count ; ++i )
488 {
fe35d097 489 OSErr err = ::AEGetNthDesc( &mNavReply.selection , i , typeFSS, &keyWord , &specDesc);
5b781a67
SC
490 if ( err != noErr ) {
491 m_path = "" ;
492 return wxID_CANCEL ;
493 }
494 outFileSpec = **(FSSpec**) specDesc.dataHandle;
495 if (specDesc.dataHandle != nil) {
496 ::AEDisposeDesc(&specDesc);
497 }
bedaf53e 498 m_path = wxMacFSSpec2MacFilename( &outFileSpec ) ;
5b781a67 499 m_paths.Add( m_path ) ;
24fe8dc7
SC
500 m_fileName = wxFileNameFromPath(m_path);
501 m_fileNames.Add(m_fileName);
502 }
503 // set these to the first hit
504 m_path = m_paths[ 0 ] ;
505 m_fileName = wxFileNameFromPath(m_path);
506 m_dir = wxPathOnly(m_path);
507 NavDisposeReply( &mNavReply ) ;
508 return wxID_OK ;
5b781a67
SC
509 }
510 return wxID_CANCEL;
5b781a67 511}
e9576ca5
SC
512
513// Generic file load/save dialog
169935ad 514static wxString
e9576ca5
SC
515wxDefaultFileSelector(bool load, const char *what, const char *extension, const char *default_name, wxWindow *parent)
516{
517 char *ext = (char *)extension;
518
519 char prompt[50];
520 wxString str;
521 if (load)
522 str = "Load %s file";
523 else
524 str = "Save %s file";
525 sprintf(prompt, wxGetTranslation(str), what);
526
527 if (*ext == '.') ext++;
528 char wild[60];
529 sprintf(wild, "*.%s", ext);
530
531 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
532}
533
534// Generic file load dialog
169935ad 535wxString
e9576ca5
SC
536wxLoadFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
537{
538 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
539}
540
541
542// Generic file save dialog
169935ad 543wxString
e9576ca5
SC
544wxSaveFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
545{
546 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
547}
548
549