home *** CD-ROM | disk | FTP | other *** search
- /*==============================================================================
- Project: POV-Ray
-
- Version: 3
-
- File: DialogUtils.c
-
- Description:
- Various System 7 dialog handling routines.
- ------------------------------------------------------------------------------
- Author:
- Eduard [esp] Schwan
- ------------------------------------------------------------------------------
- from Persistence of Vision(tm) Ray Tracer
- Copyright 1996 Persistence of Vision Team
- ------------------------------------------------------------------------------
- NOTICE: This source code file is provided so that users may experiment
- with enhancements to POV-Ray and to port the software to platforms other
- than those supported by the POV-Ray Team. There are strict rules under
- which you are permitted to use this file. The rules are in the file
- named POVLEGAL.DOC which should be distributed with this file. If
- POVLEGAL.DOC is not available or for more info please contact the POV-Ray
- Team Coordinator by leaving a message in CompuServe's Graphics Developer's
- Forum. The latest version of POV-Ray may be found there as well.
-
- This program is based on the popular DKB raytracer version 2.12.
- DKBTrace was originally written by David K. Buck.
- DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
- ------------------------------------------------------------------------------
- Change History:
- 941221 [esp] Created
- ==============================================================================*/
-
- #define DIALOGUTILS_C
-
- /*==== my header =====*/
- #include "DialogUtils.h"
-
-
- /*==== Macintosh-specific headers ====*/
- #include <Types.h>
- #include <Controls.h>
- #include <Dialogs.h>
- #include <Files.h>
- #include <Memory.h> /* DisposePtr*/
- #include <Resources.h>
- #include <Windows.h>
- #include <errors.h> /* dupFNErr, etc */
- #include <string.h> /* strcpy/cat */
- #include <strings.h> /* p2cstr */
- #include <TextUtils.h> /* NumToString */
-
- /*==== other headers ====*/
-
- #include "CursorUtils.h"
-
- /*==== General definitions ====*/
-
- typedef void(*HandleDlgEventFn_t)(EventRecord * eventPtr);
-
-
- /*==== Global variables (external scope) ====*/
-
-
- /*==== Global variables (local scope) ====*/
-
- ModalFilterUPP gModalFilterPtr = NULL;
- static DialogPtr gCurrentDlgPtr = NULL;
- static HandleDlgEventFn_t gHandleDlgEventFn;
-
-
-
- // ---------------------------------------------------------------------
- // OutlineButtonDrawProc
- // ---------------------------------------------------------------------
- pascal void OutlineButtonDrawProc(DialogPtr theDialog, short theItem)
- {
- #pragma unused (theItem)
- PenState SavePen;
- GrafPtr oldPort;
- short itemType;
- Handle itemHandle;
- Rect dispRect;
-
- // remember original port, and set to dialog port
- GetPort(&oldPort);
- SetPort(theDialog);
-
- GetPenState(&SavePen);
- PenNormal();
-
- /* use 'ok' (#1) item's rectangle */
- GetDialogItem(theDialog, ok, &itemType, &itemHandle, &dispRect);
-
- InsetRect(&dispRect, -4, -4);
-
- if ((**(ControlHandle)itemHandle).contrlHilite != 0)
- { // draw gray outline
- PenPat(&qd.gray);
- }
- else
- { // draw solid outline
- PenPat(&qd.black);
- }
-
- // draw outline
- PenSize(3, 3);
- FrameRoundRect(&dispRect, 16, 16);
-
- // restore
- SetPenState(&SavePen);
- SetPort(oldPort);
-
- } // OutlineButtonDrawProc
-
-
-
- // ---------------------------------------------------------------------
- // InstallUserDrawProc
- // ---------------------------------------------------------------------
- void InstallUserDrawProc(DialogPtr theDialog, short theItem, void * theDrawProc)
- {
- short itemtype;
- Rect itemrect;
- Handle itemHandle;
- UserItemUPP drawProcUPP;
-
- drawProcUPP = NewUserItemProc((ProcPtr)theDrawProc);
- GetDialogItem(theDialog, theItem, &itemtype, &itemHandle, &itemrect);
- SetDialogItem(theDialog, theItem, itemtype, (Handle)drawProcUPP, &itemrect);
- } // InstallUserDrawProc
-
-
-
- // ---------------------------------------------------------------------
- void EraseItemRect(DialogPtr theDialog, short theDItem)
- {
- short itemtype;
- Rect itemrect;
- Handle itemHandle;
-
- GetDialogItem(theDialog, theDItem, &itemtype, &itemHandle, &itemrect);
- SetPort((GrafPtr)theDialog);
- EraseRect(&itemrect);
- InvalRect(&itemrect);
- } // EraseItemRect
-
-
-
- // ---------------------------------------------------------------------
- // Slide a DITL on top of an existing dialog, adding new items to it
- // If relItem is zero, just overlays, otherwise positions relative to relItem
- OSErr OverlayDITLOnDialog(DialogPtr theDialog, short theNewDITLID, short relItem)
- {
- OSErr anError = noErr;
- Handle theNewDITL;
-
- if (theDialog)
- {
- theNewDITL = GetResource('DITL', theNewDITLID);
- anError = ResError();
- if (theNewDITL && !anError)
- {
- if (relItem == 0)
- AppendDITL(theDialog, theNewDITL, overlayDITL);
- else
- AppendDITL(theDialog, theNewDITL, -relItem); // IM VI, 3-22
- anError = ResError();
- ReleaseResource(theNewDITL);
- }
- }
- return anError;
- } // OverlayDITLOnDialog
-
-
- // ---------------------------------------------------------------------
- // TruncSTItem
- // ---------------------------------------------------------------------
- void TruncStaticTextItem(DialogPtr theDialog, short itemID)
- {
- short width; /*width available for string*/
- short stItemType; /*needed for GetDialogItem*/
- Handle stItemHdl; /*needed for GetDialogItem*/
- Rect stItemRect; /*needed for GetDialogItem*/
- Str255 aString;
-
- // find the custom static text in dialog
- GetDialogItem(theDialog, itemID, &stItemType, &stItemHdl, &stItemRect);
-
- // get the text string
- GetDialogItemText((Handle)stItemHdl, aString);
-
- // truncate string to fit into rect
- width = stItemRect.right - stItemRect.left - 2;
- TruncString(width, aString, truncMiddle); /* Script Mgr call */
-
- // stuff new shortened string into display field
- SetDialogItemText((Handle)stItemHdl, aString);
- } // TruncStaticTextItem
-
-
- // ---------------------------------------------------------------------
- // SetCurrMModalDialog
- // ---------------------------------------------------------------------
- void SetCurrMModalDialog(DialogPtr dp, void * handlerFn)
- {
- gCurrentDlgPtr = dp;
- gHandleDlgEventFn = (HandleDlgEventFn_t)handlerFn;
- } // SetCurrMModalDialog
-
-
- // ---------------------------------------------------------------------
- // GetCurrMModalDialog
- // ---------------------------------------------------------------------
- WindowPtr GetCurrMModalDialog(void)
- {
- return (WindowPtr)gCurrentDlgPtr;
- } // GetCurrMModalDialog
-
-
- // ---------------------------------------------------------------------
- // IsMModalDialogDisplayed
- // ---------------------------------------------------------------------
- Boolean IsMModalDialogDisplayed(void)
- {
- return (GetCurrMModalDialog() != NULL);
- } // IsMModalDialogDisplayed
-
-
- // ---------------------------------------------------------------------
- // GetMModalDialog
- // ---------------------------------------------------------------------
- DialogPtr GetMModalDialog(short dialogID)
- {
- if (!IsMModalDialogDisplayed())
- {
- (void)CompactMem(FreeMem()); // force a purge, get some memory
-
- // get the dialog
- gCurrentDlgPtr = GetNewDialog(dialogID, (Ptr)0L, (WindowPtr)-1L);
- }
- return gCurrentDlgPtr;
- } // GetMModalDialog
-
-
- // ---------------------------------------------------------------------
- // ShowMModalDialog
- // ---------------------------------------------------------------------
- void ShowMModalDialog(void)
- {
- if (GetCurrMModalDialog())
- {
- // it could still be an I-beam or watch!
- ShowArrowCursor();
- ShowWindow(GetCurrMModalDialog());
- SelectWindow(GetCurrMModalDialog());
- SetPort(GetCurrMModalDialog());
- DrawDialog(GetCurrMModalDialog());
- }
- } // ShowMModalDialog
-
-
- // ---------------------------------------------------------------------
- // CloseMModalDialog
- // ---------------------------------------------------------------------
- void CloseMModalDialog(void)
- {
- if (IsMModalDialogDisplayed())
- {
- DisposeDialog(GetCurrMModalDialog());
- SetCurrMModalDialog((DialogPtr)NULL, (HandleDlgEventFn_t)NULL);
- }
- } // CloseMModalDialog
-
-
- // ---------------------------------------------------------------------
- // HandleMModalDialogEvent
- // ---------------------------------------------------------------------
- void HandleMModalDialogEvent(EventRecord *eventPtr)
- {
- if (gHandleDlgEventFn) // if it exists,
- (*gHandleDlgEventFn)(eventPtr); // call it!
- } // HandleMModalDialogEvent
-
-
-
- // ---------------------------------------------------------------------
- // SetupStdModalFilter
- // ---------------------------------------------------------------------
- void SetupStdModalFilter(void)
- {
- // retrieve std one from system
- GetStdFilterProc(&gModalFilterPtr);
- } // SetupStdModalFilter
-
- // ---------------------------------------------------------------------
- // GetStdModalFilter
- // ---------------------------------------------------------------------
- // use this as filterproc in all ModalDialog calls
- ModalFilterUPP GetStdModalFilter(void)
- {
- return gModalFilterPtr;
- } // GetStdModalFilter
-
- // ---------------------------------------------------------------------
- // SetupDefaultDialogButtons
- // ---------------------------------------------------------------------
- // sets the default handlers for the OK and Cancel items in a dialog box.
- // This also makes the dialog mgr auto-outline the default (OK) button.
- void SetupDefaultDialogButtons(DialogPtr pDialog, short defaultItem, short cancelItem)
- {
- // System 7-only nifty setter-uppers
- if (cancelItem > 0)
- SetDialogCancelItem(pDialog, cancelItem);
- if (defaultItem > 0)
- SetDialogDefaultItem(pDialog, defaultItem);
- } // SetupDefaultDialogButtons()
-
-
- // ---------------------------------------------------------------------
- // GrabDItemHandle
- // ---------------------------------------------------------------------
- // returns the ControlHandle for the item in the dialog box
- ControlHandle GrabDItemHandle(DialogPtr theDialog, short theGetItem)
- {
- short itemtype;
- Rect itemrect;
- Handle tempHandle;
-
- GetDialogItem(theDialog, theGetItem, &itemtype, &tempHandle, &itemrect);
- return((ControlHandle)tempHandle);
- } // GrabDItemHandle
-
-
- // ---------------------------------------------------------------------
- // GetDialogItemValue
- // ---------------------------------------------------------------------
- // returns the value for the control in the dialog box
- short GetDialogItemValue(DialogPtr theDialog, short theGetItem)
- {
- Handle tempHandle;
-
- tempHandle = (Handle)GrabDItemHandle(theDialog, theGetItem);
- if (tempHandle)
- return GetControlValue((ControlHandle)tempHandle);
- else
- return 0;
- } // GetDialogItemValue
-
-
- // ---------------------------------------------------------------------
- // SetDialogItemValue
- // ---------------------------------------------------------------------
- // sets the value for the control in the dialog box
- void SetDialogItemValue(DialogPtr theDialog, short theGetItem, short theVal)
- {
- Handle tempHandle;
-
- tempHandle = (Handle)GrabDItemHandle(theDialog, theGetItem);
- if (tempHandle)
- SetControlValue((ControlHandle)tempHandle, theVal);
- } // SetDialogItemValue
-
-
- // ---------------------------------------------------------------------
- // FlashDItem
- // ---------------------------------------------------------------------
- void FlashDItem(DialogPtr theDialog, short theItem)
- {
- long whoCares;
- ControlHandle h;
- // find a handle to this control
- h = GrabDItemHandle(theDialog, theItem);
- // flash the control as if somebody clicked it
- HiliteControl(h, 1); // turn ON hilite
- Delay(8, &whoCares); // wait 8 ticks (8/60ths of a second)
- HiliteControl(h, 0); // turn hiliting back off
- } // FlashDItem
-
-
- // ---------------------------------------------------------------------
- // GetBestDialogPos
- // ---------------------------------------------------------------------
- // Find the best x,y point for a dialog, by finding which screen
- // the status window is on and offsetting from there. Used by
- // dialog routines like SFGetFile and DIBadMount.
- void GetBestDialogPos(Point * wherep, WindowPtr otherWindow)
- {
- Rect sfRect;
- GDHandle thGDev;
-
- // find rect of screen that status window is on..
- if (otherWindow == NULL)
- SetRect(&sfRect, 0, 0, 0, 0);
- else
- GetGlobalWindowRect((WindowPtr)otherWindow, &sfRect);
- thGDev = GetClosestGDevice(&sfRect);
- sfRect = (**thGDev).gdRect;
-
- // ..and offset SF Dialog on that screen
- wherep->h = sfRect.left + 40;
- wherep->v = sfRect.top + 80;
- } // GetBestDialogPos
-
-
- // ---------------------------------------------------------------------
- // DisplayModalDialog
- // ---------------------------------------------------------------------
- // Displays a modal dialog. param ^0 is the error #, ^1 is the string
- short DisplayModalDialog(short dlogID, short defaultItem, short cancelItem,
- char * s, short err,
- WindCentering_t doCentering,
- WindPositioning_t whereToShow)
- {
- short itemHit = 0;
- DialogPtr myDialog;
- char numstr[10];
-
- (void)CompactMem(FreeMem()); // force a purge, get some memory
-
- ShowArrowCursor(); // it could still be an I-beam or watch!
-
- // get the dialog
- myDialog = GetNewDialog(dlogID, (Ptr)0L, (WindowPtr)-1L);
-
- if (!myDialog)
- {
- SysBeep(2); // can't get dialog, at least beep
- SysBeep(2);
- }
- else
- {
- // convert error string to P-style if not null
- if (s != NULL)
- c2pstr(s);
-
- // see if there is an error number
- numstr[0] = '\0';
- if (err != 0)
- NumToString(err, (StringPtr)numstr);
-
- // stick parms into dialog
- ParamText((StringPtr)numstr, (StringPtr)s, "\p", "\p");
-
- // put it where it should be
- PositionWindow(myDialog, doCentering, whereToShow, (WindowPtr)NULL /*gp2wWindow*/ );
- // set the standard action for OK/cancel buttons
- SetupDefaultDialogButtons(myDialog, defaultItem, cancelItem);
- // show it
- ShowWindow(myDialog);
- // prompt the user
- ModalDialog(GetStdModalFilter(), &itemHit);
- // all done with it
- DisposeDialog(myDialog);
- }
-
- return itemHit;
-
- } // DisplayModalDialog
-
-