]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/filedlg.cpp
Fixed some broken things related to context help, fixed memory leak
[wxWidgets.git] / src / mac / carbon / 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"
17#include "wx/utils.h"
18#include "wx/dialog.h"
19#include "wx/filedlg.h"
20#include "wx/intl.h"
21
2f1ae414 22#if !USE_SHARED_LIBRARY
e9576ca5 23IMPLEMENT_CLASS(wxFileDialog, wxDialog)
2f1ae414 24#endif
e9576ca5 25
519cb848
SC
26// begin wxmac
27
28#include "morefile.h"
29#include "moreextr.h"
30#include "fullpath.h"
31#include "fspcompa.h"
32#include "PLStringFuncs.h"
33
34char * gfilters[] =
35{
36 "*.TXT" ,
37
38 NULL
39} ;
40
41OSType gfiltersmac[] =
42{
43 'TEXT' ,
44
45 '****'
46} ;
47
2f1ae414
SC
48// the data we need to pass to our standard file hook routine
49// includes a pointer to the dialog, a pointer to the standard
50// file reply record (so we can inspect the current selection)
51// and a copy of the "previous" file spec of the reply record
52// so we can see if the selection has changed
53
54const int kwxMacFileTypes = 10 ;
55
56struct OpenUserDataRec {
57 StandardFileReply *sfrPtr;
58 FSSpec oldSelectionFSSpec;
59 char filter[kwxMacFileTypes][10] ;
60 OSType filtermactypes[kwxMacFileTypes] ;
61 int numfilters ;
62 DialogPtr theDlgPtr;
63};
64typedef struct OpenUserDataRec
65 OpenUserDataRec, *OpenUserDataRecPtr;
66
67#if !TARGET_CARBON
68
519cb848
SC
69static void wxMacSetupStandardFile(short newVRefNum, long newDirID)
70{
71 enum
72 { SFSaveDisk = 0x214, CurDirStore = 0x398 };
73 *(short *) SFSaveDisk = -1 * newVRefNum;
74 *(long *) CurDirStore = newDirID;
75}
76
77static void wxMacSetupStandardFileFromPath( const char* s )
78{
79 Str255 volume ;
80 Str255 path ;
81 short vRefNum ;
82 long dirRef ;
83 short i,j ;
84 Boolean isDirectory ;
85
86 for (i=0 ; (s[i]!=0) && (s[i]!=':') ;i++)
87 {
88 volume[i]=s[i] ;
89 }
90 volume[i]=':' ;
91 volume[i+1]=0 ;
92
93 // then copy the rest of the filename
94
95 for (j=0;(s[i]!=0);i++,j++)
96 {
97 path[j]=s[i] ;
98 }
99 path[j]=0 ;
100
101 c2pstr((Ptr) volume) ;
102 c2pstr((Ptr) path) ;
103
104 SetVol(volume, 0) ;
105 GetVol( NULL, &vRefNum ) ;
106
107 GetDirectoryID( vRefNum , fsRtDirID , path , &dirRef , &isDirectory ) ;
108 wxMacSetupStandardFile(vRefNum, dirRef) ;
109}
110
111enum {
112 kSelectItem = 10, // select button item number
113 kSFGetFileDlgID = 251, // dialog resource number
114 kStrListID = 251, // our strings
115 kSelectStrNum = 1, // word 'Select: ' for button
116 kDesktopStrNum = 2, // word 'Desktop' for button
117 kSelectNoQuoteStrNum = 3, // word 'Select: ' for button
118
119 kUseQuotes = true, // parameter for SetButtonName
120 kDontUseQuotes = false
121};
122
519cb848
SC
123static void GetLabelString(StringPtr theStr, short stringNum)
124{
125 GetIndString(theStr, kStrListID, stringNum);
126}
127
128static void CopyPStr(StringPtr src, StringPtr dest)
129{
130 BlockMoveData(src, dest, 1 + src[0]);
131}
132
133static char GetSelectKey(void)
134{
135 // this is the key used to trigger the select button
136
137 // NOT INTERNATIONAL SAVVY; should at least grab it from resources
138
139 return 's';
140}
141
142// FlashButton briefly highlights the dialog button
143// as feedback for key equivalents
144
145static void FlashButton(DialogPtr theDlgPtr, short buttonID)
146{
147 short buttonType;
148 Handle buttonHandle;
149 Rect buttonRect;
150 unsigned long finalTicks;
151
152 GetDialogItem(theDlgPtr, buttonID, &buttonType, &buttonHandle, &buttonRect);
153 HiliteControl((ControlHandle) buttonHandle, kControlButtonPart);
154 Delay(10, &finalTicks);
155 HiliteControl((ControlHandle) buttonHandle, 0);
156}
157
158static Boolean SameFSSpec(FSSpecPtr spec1, FSSpecPtr spec2)
159{
160 return (spec1->vRefNum == spec2->vRefNum
161 && spec1->parID == spec2->parID
162 && EqualString(spec1->name, spec2->name, false, false));
163}
164// MyModalDialogFilter maps a key to the Select button, and handles
165// flashing of the button when the key is hit
166
167static pascal Boolean SFGetFolderModalDialogFilter(DialogPtr theDlgPtr, EventRecord *eventRec,
168 short *item, Ptr dataPtr)
169{
170#pragma unused (dataPtr)
171
172 // make certain the proper dialog is showing, 'cause standard file
173 // can nest dialogs but calls the same filter for each
174
175 if (((WindowPeek) theDlgPtr)->refCon == sfMainDialogRefCon)
176 {
177 // check if the select button was hit
178 /*
179 if ((eventRec->what == keyDown)
180 && (eventRec->modifiers & cmdKey)
181 && ((eventRec->message & charCodeMask) == GetSelectKey()))
182 {
183 *item = kSelectItem;
184 FlashButton(theDlgPtr, kSelectItem);
185 return true;
186 }
187 */
188 }
189
190 return false;
191}
2f1ae414 192#endif
519cb848
SC
193
194void ExtendedOpenFile( ConstStr255Param message , ConstStr255Param path , const char *filter , FileFilterYDUPP fileFilter, StandardFileReply *theSFR)
195{
196 Point thePt;
197 OpenUserDataRec myData;
198 FSSpec tempSpec;
199 Boolean folderFlag;
200 Boolean wasAliasedFlag;
201 DlgHookYDUPP dlgHookUPP;
202 ModalFilterYDUPP myModalFilterUPP;
203 OSErr err;
204 SFTypeList types ;
205
206
207 // presumably we're running System 7 or later so CustomGetFile is
208 // available
209
210 // set initial contents of Select button to a space
211
2f1ae414 212 memcpy( theSFR->sfFile.name , "\p " , 2 ) ;
519cb848
SC
213
214 // point the user data parameter at the reply record so we can get to it later
215
216 myData.sfrPtr = theSFR;
217 if ( filter && filter[0] )
218 {
219 myData.numfilters = 1 ;
220 for ( int i = 0 ; i < myData.numfilters ; i++ )
221 {
222 int j ;
223
224 strcpy( myData.filter[i] , filter ) ;
225 for( j = 0 ; myData.filter[i][j] ; j++ )
226 {
227 myData.filter[i][j] = toupper( myData.filter[i][j] ) ;
228 }
229 for ( j = 0 ; gfilters[j] ; j++ )
230 {
231 if ( strcmp( myData.filter[i] , gfilters[j] ) == 0 )
232 {
233 myData.filtermactypes[i] = gfiltersmac[j] ;
234 break ;
235 }
236 }
237 if( gfilters[j] == NULL )
238 {
239 myData.filtermactypes[i] = '****' ;
240 }
241 }
242 }
243 else
244 {
245 myData.numfilters = 0 ;
246 }
247 // display the dialog
2f1ae414
SC
248
249#if !TARGET_CARBON
519cb848
SC
250
251 dlgHookUPP = NULL ;
252// dlgHookUPP = NewDlgHookYDProc(SFGetFolderDialogHook);
253 myModalFilterUPP = NewModalFilterYDProc(SFGetFolderModalDialogFilter);
254
255 thePt.h = thePt.v = -1; // center dialog
256
257 ParamText( message , NULL , NULL , NULL ) ;
258
259 CustomGetFile( fileFilter,
260 -1, // show all types
261 NULL,
262 theSFR,
263 kSFGetFileDlgID,
264 thePt, // top left point
265 dlgHookUPP,
266 myModalFilterUPP,
267 nil, // activate list
268 nil, // activate proc
269 &myData);
270
271 DisposeRoutineDescriptor(dlgHookUPP);
272 DisposeRoutineDescriptor(myModalFilterUPP);
2f1ae414
SC
273#else
274#endif
519cb848
SC
275 // if cancel wasn't pressed and no fatal error occurred...
276
277 if (theSFR->sfGood)
278 {
279 // if no name is in the reply record file spec,
280 // use the file spec of the parent folder
281
282 if (theSFR->sfFile.name[0] == '\0')
283 {
284 err = FSMakeFSSpec(theSFR->sfFile.vRefNum, theSFR->sfFile.parID,
285 "\p", &tempSpec);
286 if (err == noErr)
287 {
288 theSFR->sfFile = tempSpec;
289 }
290 else
291 {
292 // no name to return, forget it
293
294 theSFR->sfGood = false;
295 }
296 }
297
298 // if there is now a name in the file spec, check if it's
299 // for a folder or a volume
300
301 if (theSFR->sfFile.name[0] != '\0')
302 {
303 // the parID of the root of a disk is always fsRtParID == 1
304
305 if (theSFR->sfFile.parID == fsRtParID)
306 {
307 theSFR->sfIsVolume = true;
308 theSFR->sfIsFolder = false; // it would be reasonable for this to be true, too
309 }
310
311 // we have a valid FSSpec, now let's make sure it's not for an alias file
312
313 err = ResolveAliasFile(&theSFR->sfFile, true, &folderFlag, &wasAliasedFlag);
314 if (err != noErr)
315 {
316 theSFR->sfGood = false;
317 }
318
319 // did the alias resolve to a folder?
320
321 if (folderFlag && ! theSFR->sfIsVolume)
322 {
323 theSFR->sfIsFolder = true;
324 }
325 }
326 }
327}
328
329static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, Ptr dataPtr)
330{
331 Str255 filename ;
332 OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
333 // return true if this item is invisible or a file
334
335 Boolean visibleFlag;
336 Boolean folderFlag;
337
338 visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
339 folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
340
341 // because the semantics of the filter proc are "true means don't show
342 // it" we need to invert the result that we return
343
344 if ( !visibleFlag )
345 return true ;
346
347 if ( !folderFlag )
348 {
349 if ( data->numfilters > 0 )
350 {
351 PLstrcpy( filename ,myCInfoPBPtr->hFileInfo.ioNamePtr ) ;
352 if ( filename[0] >= 4 )
353 {
354 for( int j = 1 ; j <= filename[0] ; j++ )
355 {
356 filename[j] = toupper( filename[j] ) ;
357 }
358 for ( int i = 0 ; i < data->numfilters ; ++i )
359 {
360 if ( myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType == data->filtermactypes[i] )
361 return false ;
362
363 if ( strncmp( (char*) filename + 1 + filename[0] - 4 ,
364 & data->filter[i][ strlen(data->filter[i]) - 4 ] , 4 ) == 0 )
365 return false ;
366 }
367 }
368 return true ;
369 }
370 }
371
372 return false ;
373}
374
375// end wxmac
376
377wxString wxFileSelector(const char *title,
e9576ca5
SC
378 const char *defaultDir, const char *defaultFileName,
379 const char *defaultExtension, const char *filter, int flags,
380 wxWindow *parent, int x, int y)
381{
382 // If there's a default extension specified but no filter, we create a suitable
383 // filter.
384
385 wxString filter2("");
386 if ( defaultExtension && !filter )
387 filter2 = wxString("*.") + wxString(defaultExtension) ;
388 else if ( filter )
389 filter2 = filter;
390
391 wxString defaultDirString;
392 if (defaultDir)
393 defaultDirString = defaultDir;
394 else
395 defaultDirString = "";
396
397 wxString defaultFilenameString;
398 if (defaultFileName)
399 defaultFilenameString = defaultFileName;
400 else
401 defaultFilenameString = "";
402
403 wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y));
404
405 if ( fileDialog.ShowModal() == wxID_OK )
406 {
407 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
408 return wxBuffer;
409 }
410 else
8be97d65 411 return wxGetEmptyString();
e9576ca5
SC
412}
413
169935ad 414WXDLLEXPORT wxString wxFileSelectorEx(const char *title,
e9576ca5
SC
415 const char *defaultDir,
416 const char *defaultFileName,
417 int* defaultFilterIndex,
418 const char *filter,
419 int flags,
420 wxWindow* parent,
421 int x,
422 int y)
423
424{
425 wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "",
426 defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y));
427
428 if ( fileDialog.ShowModal() == wxID_OK )
429 {
430 *defaultFilterIndex = fileDialog.GetFilterIndex();
431 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
432 return wxBuffer;
433 }
434 else
8be97d65 435 return wxGetEmptyString();
e9576ca5
SC
436}
437
438wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
439 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
440 long style, const wxPoint& pos)
441{
442 m_message = message;
443 m_dialogStyle = style;
444 m_parent = parent;
445 m_path = "";
446 m_fileName = defaultFileName;
447 m_dir = defaultDir;
448 m_wildCard = wildCard;
449 m_filterIndex = 1;
450}
451
452int wxFileDialog::ShowModal()
453{
519cb848
SC
454 if ( m_dialogStyle & wxSAVE )
455 {
456 StandardFileReply reply ;
457 Str255 prompt ;
458 Str255 filename ;
459
460 strcpy((char *)prompt, m_message) ;
461 c2pstr((char *)prompt ) ;
462
463 strcpy((char *)filename, m_fileName) ;
464 c2pstr((char *)filename ) ;
2f1ae414 465 #if !TARGET_CARBON
519cb848
SC
466
467 StandardPutFile( prompt , filename , &reply ) ;
2f1ae414
SC
468
469 #else
470 #endif
519cb848
SC
471 if ( reply.sfGood == false )
472 {
473 m_path = "" ;
474 return wxID_CANCEL ;
475 }
476 else
477 {
478 m_path = wxMacFSSpec2UnixFilename( &reply.sfFile ) ;
479 return wxID_OK ;
480 }
481 }
482 else
483 {
484 OSType types = '????' ;
485 Str255 prompt ;
486 Str255 path ;
487
488 strcpy((char *)prompt, m_message) ;
489 c2pstr((char *)prompt ) ;
490
491 strcpy((char *)path, m_path ) ;
492 c2pstr((char *)path ) ;
493
519cb848 494 StandardFileReply reply ;
2f1ae414
SC
495 FileFilterYDUPP crossPlatformFileFilterUPP = 0 ;
496 #if !TARGET_CARBON
519cb848
SC
497 crossPlatformFileFilterUPP =
498 NewFileFilterYDProc(CrossPlatformFileFilter);
2f1ae414 499 #endif
519cb848
SC
500
501 ExtendedOpenFile( prompt , path , m_wildCard , crossPlatformFileFilterUPP, &reply);
2f1ae414
SC
502 #if !TARGET_CARBON
503 DisposeFileFilterYDUPP(crossPlatformFileFilterUPP);
504 #endif
519cb848
SC
505 if ( reply.sfGood == false )
506 {
507 m_path = "" ;
508 return wxID_CANCEL ;
509 }
510 else
511 {
512 m_path = wxMacFSSpec2UnixFilename( &reply.sfFile ) ;
513 return wxID_OK ;
514 }
515 }
e9576ca5
SC
516 return wxID_CANCEL;
517}
518
519// Generic file load/save dialog
169935ad 520static wxString
e9576ca5
SC
521wxDefaultFileSelector(bool load, const char *what, const char *extension, const char *default_name, wxWindow *parent)
522{
523 char *ext = (char *)extension;
524
525 char prompt[50];
526 wxString str;
527 if (load)
528 str = "Load %s file";
529 else
530 str = "Save %s file";
531 sprintf(prompt, wxGetTranslation(str), what);
532
533 if (*ext == '.') ext++;
534 char wild[60];
535 sprintf(wild, "*.%s", ext);
536
537 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
538}
539
540// Generic file load dialog
169935ad 541wxString
e9576ca5
SC
542wxLoadFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
543{
544 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
545}
546
547
548// Generic file save dialog
169935ad 549wxString
e9576ca5
SC
550wxSaveFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
551{
552 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
553}
554
555