



In MacApp 3.0 it is relatively easy to add extra dialog items to Standard Get File and Standard Put File. However, youhave to use normal 'DLOG' and 'DITL' resources to ac-complish this. It would be much nicer if you were able to add 'View' resources to Standard File.
Using MacApp 2.0, Danie Underwood had already created a number of patches for his MacApp Drafter application that implemented this feature. We decided to implement his ideas in MacApp 3.0 and add some extra features.
You will find that these changes are easy to add to your application. You just have to subclass your application object from TSFPApplication and your document from TSFPFileBasedDocument and override a couple of methods.
As a bonus, file filtering is also implemented as a method. There is no longer a need to write a low level file filter function. Just override the TSFPApplication.FileFilter() method.
The prototype of these callbacks is as follows under System 7.0:
typedef pascal short (*DlgHookYDProcPtr)(short item,
DialogPtr theDialog, void *yourDataPtr);
typedef pascal Boolean (*ModalFilterYDProcPtr)(DialogPtr theDialog,
EventRecord *theEvent, short *itemHit, void *yourDataPtr);
typedef pascal Boolean (*FileFilterYDProcPtr)(ParmBlkPtr PB,
void *yourDataPtr);
typedef pascal void (*ActivateYDProcPtr)(DialogPtr theDialog,
short itemNo, Boolean activating, void *yourDataPtr);
For System 6.0, the same type of callbacks are present except for the activate callback, but they lack the last parameter. This last parameter (void *yourDataPtr) allows you to pass a user defined data structure to your callback.
The first use of Standard File routines is in TApplication.ChooseDocument() that is called whenever the user chooses "Open" from the File menu. It calls TApplication.GetStandardFileParameters() to get a reference to a file filter routine, a modal dialog filter routine and a dialog hook routine. It then uses either CustomGetFile() or SFPGetFile() to pose the Standard Get File dialogs.
The second use is in TFileHandler.RequestFileName(). This one calls TFileHandler.SFPutParms() to get a modal dialog filter routine and a dialog hook routine and subsequently calls CustomPutFile() or SFPPutFile().
MacApp uses a trick so that the same call back routines can be used in both the Custom…() and the SFP…() cases. In the SFP…() case, it packages the call back routines in a CallBack data structure. This data structure actually contains some assembler code that reserves space for the extra parameter, pushes that extra parameter and jumps to the original callback.
Note that there are some bugs in how this is implemented in MacApp 3.0. First of all, it can't handle the situation where you have more than one callback (as will be the case in our code). Only the modal dialog filter routine was packaged, not the other ones. A second bug could occur if you returned NULL for this modal dialog filter routine in your override of GetStandardFileParameters() or SFPutParms(). The CallBack data structure would make the code jump to zero. Not a good idea. The first thing we had to do was fix these potential problems.
Based on the use of Standard File in MacApp 3.0, we chose the following strategy to implement our changes:
The method ExtraViewID() that determines the ID of the view to add gets the command number used to open or save as a parameter. This means you can test this parameter to add a different dialog if you have more than one command number to open a file. An example is if you want to open a help file as well as a normal document. You could install a view with a "Search Help" button.
The methods necessary to implement these changes are done in the TWindow subclass TSFPWindow. For completeness, TSFPWindow also keeps a Boolean field fNeedRefresh. If you set this field, an sfHookRebuildList event will be generated in the dialog hook routine.
Whenever one of the Custom…() or SFP…() routines is called, the system loads one of the 'DLOG' resources that contains the dialog items for that routine (sfGetDialogID, getDlgID, sfPutDialogID, putDlgID).
Now, if we know the number of the 'View' resource to add to the dialog, we can change the rectangle of that dialog to accommodate our added items. This is done in TSFPWindow.LocateAndResize().
The view hierarchy that we add to this window must always have one top view of class TSFPView and an identifier 'DLOG'. This is needed so that we can:
The dialog hook routine is called SFPDialogHook(). During sfHookFirstCall processing in our dialog hook procedure we create an instance of our special window TSFPWindow. We also create our view hierarchy and adapt the size and location of Standard File's dialog (in TSFPWindow.DoLocateAndResize()).
During sfHookNullEvent we call TSFPView.WantToUpdate() with information on the currently selected file or folder. We also determine whether the Save/Open button is currently enabled and pass this information to our view in TSFPView.SetOkEnable(). In sfHookLastCall we can clean up any views that we have added:
static pascal short SFPDialogHook(short item, DialogPtr theDialog, void *)
{
short returnItem = item;
switch (item) {
case sfHookFirstCall:
// set reference to Standard File Dialog
pSFPDialog = theDialog;
// Install the MacApp world.
if (! pSFPWindow && pSFPViewID) {
pSFPWindow = new TSFPWindow;
if (pSFPWindow) {
pSFPWindow->ISFPWindow(NULL, GrafPtr(theDialog));
pSFPWindow->SavePortInfo();
gViewServer->DoCreateViews(NULL, pSFPWindow,
pSFPViewID, gZeroVPt);
pSFPWindow->DoLocateAndResize();
pSFPWindow->GetSFPView()->Show(true, false);
pSFPWindow->RestorePortInfo();
}
}
break;
case sfHookNullEvent:
if (pSFPWindow) {
// see if list of files needs updating
if (pSFPWindow->GetNeedRefresh()) {
pSFPWindow->SetNeedRefresh(false);
returnItem = sfHookRebuildList;
} else {
pSFPWindow->SavePortInfo();
// get added view
TSFPView *view = pSFPWindow->GetSFPView();
// focus on window
pSFPWindow->InvalidateFocus();
pSFPWindow->Focus();
// update view depending on OK state
Handle dialogItem;
CRect itsBox;
short itemType;
::GetDItem(theDialog, pSFPGetPutOK, itemType,
dialogItem, itsBox);
Boolean okEnabled =
((**ControlHandle(dialogItem)).contrlHilite
!= 255);
view->SetOkEnable(okEnabled);
// update view with information on current reply
view->WantToUpdate(pSFPReply,
pSFPStandardFileReply);
pSFPWindow->RestorePortInfo();
}
}
break;
case sfHookLastCall:
if (pSFPWindow) {
// throw out allocated view hierarchy
pSFPWindow->Free();
pSFPWindow = NULL;
}
break;
} // end of switch()
return returnItem;
}
The dialog filter routine is called SFPDialogFilter(). In our dialog filter callback we detect mouseDowns inside our added view and pass these events directly to that added view. We handle update events for our special TSFPWindow (which shares the window manager port of Standard File) by drawing our added view hierarchy and passing on the update event to Standard File:
static pascal Boolean SFPDialogFilter(DialogPtr theDialog,
EventRecord& theEvent, short& itemHit, void *yourDataPtr)
{
Boolean result = false;
switch (theEvent.what) {
case mouseDown: {
// mouse down events the MacApp way.
Boolean oldObjectPerm;
oldObjectPerm = AllocateObjectsFromPerm(FALSE);
TToolboxEvent* theToolBoxEvent = new TToolboxEvent;
AllocateObjectsFromPerm(oldObjectPerm);
theToolBoxEvent->IToolboxEvent(gApplication, theEvent);
// get mouse location
CPoint theMouse = theEvent.where;
::SetPort(theDialog);
::GlobalToLocal(theMouse);
TSFPView *view = pSFPWindow->GetSFPView();
pSFPWindow->InvalidateFocus();
pSFPWindow->Focus();
// convert to local coordinates
VPoint theVMouse = theMouse;
view->SuperToLocal(theVMouse);
if (view->ContainsMouse(theVMouse) &&
view->HandleMouseDown(theVMouse, theToolBoxEvent,
gStdHysteresis)) {
result = true;
}
theToolBoxEvent->Free();
}
break;
case updateEvt:
if (WindowPtr(theEvent.message) != theDialog) {
// update MacApp windows
gApplication->UpdateAllWindows();
result = true;
} else {
// update MacApp part and pass update to Stand File
pSFPWindow->InvalidateFocus();
pSFPWindow->Focus();
pSFPWindow->Update();
}
break;
}
return result;
}
Last, our file filter call back SFPFileFilter() will get information about the file to test, makes that into a TFile object and calls a method of our TSFPApplication:
pascal Boolean SFPFileFilter(ParmBlkPtr p, void *)
{
// no #define for stationary bit
const short isStationery = 0x0800;
// get current volume
ParamBlockRec oldVol;
oldVol.volumeParam.ioNamePtr = NULL;
::PBGetVol(&oldVol, false);
// get current directory
WDPBRec WDRec;
WDRec.ioNamePtr = NULL;
WDRec.ioVRefNum = p->fileParam.ioVRefNum;
WDRec.ioWDProcID = 'ERIK';
WDRec.ioWDDirID = *kCurDirStorePtr;
::PBOpenWD(&WDRec, false);
::PBSetVol(&oldVol, false);
// name of file to test is passed in
CStr63 name = p->ioParam.ioNamePtr;
// create TFile
TFile *aFile = new TFile;
aFile->SpecifyWithTrio(p->fileParam.ioVRefNum,
WDRec.ioWDDirID, name);
::PBCloseWD(&WDRec, false);
// set type and creator
FInfo finderInfo;
if (aFile->GetFinderInfo(finderInfo) == noErr) {
aFile->fFileType = finderInfo.fdType;
aFile->fCreator = finderInfo.fdCreator;
if (finderInfo.fdFlags & isStationery) {
aFile->fStationery = TRUE;
}
}
// pass it to the application object
Boolean returnVal =
((TSFPApplication *)gApplication)->FileFilter(aFile);
aFile->Free();
return returnVal;
}
In Standard Get File, we show a 'preview' of AppleLink documents, where the preview consists of the sender followed by '•', followed by the subject, e.g. "SCHALK1 • Not So Standard File". As you can see in the sources, we define a subclass TOpenAppleLink of TSFPView with a method TSFPView.WantToUpdate(). We also subclass TSFPApplication and implement TMySFPApplication.ExtraViewID() to return the ID of the 'View' to add. As a bonus, we also change TSFPApplication.FileFilter() so that we filter out all files that have already been opened (see Figure 1):
pascal short TMySFPApplication::ExtraViewID(CommandNumber)
{
return kOpenAppleLinkViewID;
}
pascal void TOpenAppleLink::WantToUpdate(SFReply &aSFReply,
StandardFileReply& aCustomReply)
{
CStr255 aPreview;
// create "originator • subject"
this->CreatePreView(aSFReply, aCustomReply, aPreview);
// and set in text field
this->SetPreviewText(aPreview);
}
pascal Boolean TMySFPApplication::FileFilter(TFile *aFile)
{
if (inherited::FileFilter(aFile) ||
(aFile->fCreator != 'GEOL') ||
gApplication->FindDocument(aFile)) {
return true;
}
return false;
}
The second part customizes Standard Put File. We add a couple of radio buttons and a popup menu to select the type of file to save. Again we use a subclass TSaveAppleLink of TSFPView. We subclass our TMySFPFileBasedDocument from TSFPFileBasedDocument and give it an ExtraViewID() method to yield the id of the 'View' resource to add. Note that you would have methods in your TSaveAppleLink class to let your application know which type of document was selected. This is not implemented (see Figure 2).
pascal short TMySFPFileBasedDocument::ExtraViewID(CommandNumber)
{
return kSaveAppleLinkViewID;
}
The views themselves are created in a ViewEdit file. Don't forget to give them a top-level view of a subclass of TSFPView and to set its identifier to 'DLOG'



