home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / POV-Ray 3.0.2 / src / MacSource / DialogUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-14  |  13.6 KB  |  461 lines  |  [TEXT/CWIE]

  1. /*==============================================================================
  2. Project:    POV-Ray
  3.  
  4. Version:    3
  5.  
  6. File:    DialogUtils.c
  7.  
  8. Description:
  9.     Various System 7 dialog handling routines.
  10. ------------------------------------------------------------------------------
  11. Author:
  12.     Eduard [esp] Schwan
  13. ------------------------------------------------------------------------------
  14.     from Persistence of Vision(tm) Ray Tracer
  15.     Copyright 1996 Persistence of Vision Team
  16. ------------------------------------------------------------------------------
  17.     NOTICE: This source code file is provided so that users may experiment
  18.     with enhancements to POV-Ray and to port the software to platforms other 
  19.     than those supported by the POV-Ray Team.  There are strict rules under
  20.     which you are permitted to use this file.  The rules are in the file
  21.     named POVLEGAL.DOC which should be distributed with this file. If 
  22.     POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  23.     Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  24.     Forum.  The latest version of POV-Ray may be found there as well.
  25.  
  26.     This program is based on the popular DKB raytracer version 2.12.
  27.     DKBTrace was originally written by David K. Buck.
  28.     DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  29. ------------------------------------------------------------------------------
  30. Change History:
  31.     941221    [esp]    Created
  32. ==============================================================================*/
  33.  
  34. #define DIALOGUTILS_C
  35.  
  36. /*==== my header =====*/
  37. #include "DialogUtils.h"
  38.  
  39.  
  40. /*==== Macintosh-specific headers ====*/
  41. #include <Types.h>
  42. #include <Controls.h>
  43. #include <Dialogs.h>
  44. #include <Files.h>
  45. #include <Memory.h>            /* DisposePtr*/
  46. #include <Resources.h>
  47. #include <Windows.h>
  48. #include <errors.h>            /* dupFNErr, etc */
  49. #include <string.h>            /* strcpy/cat */
  50. #include <strings.h>        /* p2cstr */
  51. #include <TextUtils.h>        /* NumToString */
  52.  
  53. /*==== other headers ====*/
  54.  
  55. #include "CursorUtils.h"
  56.  
  57. /*==== General definitions ====*/
  58.  
  59. typedef void(*HandleDlgEventFn_t)(EventRecord * eventPtr);
  60.  
  61.  
  62. /*==== Global variables (external scope) ====*/
  63.  
  64.  
  65. /*==== Global variables (local scope) ====*/
  66.  
  67. ModalFilterUPP                gModalFilterPtr = NULL;
  68. static DialogPtr            gCurrentDlgPtr = NULL;
  69. static HandleDlgEventFn_t    gHandleDlgEventFn;
  70.  
  71.  
  72.  
  73. // ---------------------------------------------------------------------
  74. // OutlineButtonDrawProc
  75. // ---------------------------------------------------------------------
  76. pascal void OutlineButtonDrawProc(DialogPtr theDialog, short theItem)
  77. {
  78. #pragma unused (theItem)
  79.     PenState    SavePen;
  80.     GrafPtr        oldPort;
  81.     short        itemType;
  82.     Handle        itemHandle;
  83.     Rect        dispRect;
  84.  
  85.     // remember original port, and set to dialog port
  86.     GetPort(&oldPort);
  87.     SetPort(theDialog);
  88.  
  89.     GetPenState(&SavePen);
  90.     PenNormal();
  91.  
  92.     /* use 'ok' (#1) item's rectangle */
  93.     GetDialogItem(theDialog, ok, &itemType, &itemHandle, &dispRect);
  94.  
  95.     InsetRect(&dispRect, -4, -4);
  96.  
  97.     if ((**(ControlHandle)itemHandle).contrlHilite != 0)
  98.     {    // draw gray outline
  99.         PenPat(&qd.gray);
  100.     }
  101.     else
  102.     {    // draw solid outline
  103.         PenPat(&qd.black);
  104.     }
  105.  
  106.     // draw outline
  107.     PenSize(3, 3);
  108.     FrameRoundRect(&dispRect, 16, 16);
  109.  
  110.     // restore
  111.     SetPenState(&SavePen);
  112.     SetPort(oldPort);
  113.  
  114. } // OutlineButtonDrawProc
  115.  
  116.  
  117.  
  118. // ---------------------------------------------------------------------
  119. // InstallUserDrawProc
  120. // ---------------------------------------------------------------------
  121. void InstallUserDrawProc(DialogPtr theDialog, short theItem, void * theDrawProc)
  122. {
  123.     short        itemtype;
  124.     Rect        itemrect;
  125.     Handle        itemHandle;
  126.     UserItemUPP    drawProcUPP;
  127.  
  128.     drawProcUPP = NewUserItemProc((ProcPtr)theDrawProc);
  129.     GetDialogItem(theDialog, theItem, &itemtype, &itemHandle, &itemrect);
  130.     SetDialogItem(theDialog, theItem, itemtype, (Handle)drawProcUPP, &itemrect);
  131. } // InstallUserDrawProc
  132.  
  133.  
  134.  
  135. // ---------------------------------------------------------------------
  136. void EraseItemRect(DialogPtr theDialog, short theDItem)
  137.     {
  138.     short    itemtype;
  139.     Rect    itemrect;
  140.     Handle    itemHandle;
  141.  
  142.     GetDialogItem(theDialog, theDItem, &itemtype, &itemHandle, &itemrect);
  143.     SetPort((GrafPtr)theDialog);
  144.     EraseRect(&itemrect);
  145.     InvalRect(&itemrect);
  146.     } // EraseItemRect
  147.  
  148.  
  149.  
  150. // ---------------------------------------------------------------------
  151. // Slide a DITL on top of an existing dialog, adding new items to it
  152. // If relItem is zero, just overlays, otherwise positions relative to relItem
  153. OSErr OverlayDITLOnDialog(DialogPtr theDialog, short theNewDITLID, short relItem)
  154.     {
  155.     OSErr    anError = noErr;
  156.     Handle    theNewDITL;
  157.  
  158.     if (theDialog)
  159.         {
  160.         theNewDITL = GetResource('DITL', theNewDITLID);
  161.         anError = ResError();
  162.         if (theNewDITL && !anError)
  163.             {
  164.             if (relItem == 0)
  165.                 AppendDITL(theDialog, theNewDITL, overlayDITL);
  166.             else
  167.                 AppendDITL(theDialog, theNewDITL, -relItem); // IM VI, 3-22
  168.             anError = ResError();
  169.             ReleaseResource(theNewDITL);
  170.             }
  171.         }
  172.     return anError;
  173.     } // OverlayDITLOnDialog
  174.  
  175.  
  176. // ---------------------------------------------------------------------
  177. // TruncSTItem
  178. // ---------------------------------------------------------------------
  179. void TruncStaticTextItem(DialogPtr theDialog, short itemID)
  180.     {
  181.     short    width;            /*width available for string*/
  182.     short    stItemType;        /*needed for GetDialogItem*/
  183.     Handle    stItemHdl;        /*needed for GetDialogItem*/
  184.     Rect    stItemRect;        /*needed for GetDialogItem*/
  185.     Str255    aString;
  186.  
  187.     // find the custom static text in dialog
  188.     GetDialogItem(theDialog, itemID, &stItemType, &stItemHdl, &stItemRect);
  189.  
  190.     // get the text string
  191.     GetDialogItemText((Handle)stItemHdl, aString);
  192.  
  193.     // truncate string to fit into rect
  194.     width = stItemRect.right - stItemRect.left - 2;
  195.     TruncString(width, aString, truncMiddle); /* Script Mgr call */
  196.  
  197.     // stuff new shortened string into display field
  198.     SetDialogItemText((Handle)stItemHdl, aString);
  199.     } // TruncStaticTextItem
  200.  
  201.  
  202. // ---------------------------------------------------------------------
  203. // SetCurrMModalDialog
  204. // ---------------------------------------------------------------------
  205. void SetCurrMModalDialog(DialogPtr dp, void * handlerFn)
  206.     {
  207.     gCurrentDlgPtr = dp;
  208.     gHandleDlgEventFn = (HandleDlgEventFn_t)handlerFn;
  209.     } // SetCurrMModalDialog
  210.  
  211.  
  212. // ---------------------------------------------------------------------
  213. // GetCurrMModalDialog
  214. // ---------------------------------------------------------------------
  215. WindowPtr GetCurrMModalDialog(void)
  216.     {
  217.     return (WindowPtr)gCurrentDlgPtr;
  218.     } // GetCurrMModalDialog
  219.  
  220.  
  221. // ---------------------------------------------------------------------
  222. // IsMModalDialogDisplayed
  223. // ---------------------------------------------------------------------
  224. Boolean IsMModalDialogDisplayed(void)
  225.     {
  226.     return (GetCurrMModalDialog() != NULL);
  227.     } // IsMModalDialogDisplayed
  228.  
  229.  
  230. // ---------------------------------------------------------------------
  231. // GetMModalDialog
  232. // ---------------------------------------------------------------------
  233. DialogPtr GetMModalDialog(short dialogID)
  234.     {
  235.     if (!IsMModalDialogDisplayed())
  236.         {
  237.         (void)CompactMem(FreeMem());    // force a purge, get some memory
  238.  
  239.         // get the dialog
  240.         gCurrentDlgPtr = GetNewDialog(dialogID, (Ptr)0L, (WindowPtr)-1L);
  241.         }
  242.     return gCurrentDlgPtr;
  243.     } // GetMModalDialog
  244.  
  245.  
  246. // ---------------------------------------------------------------------
  247. // ShowMModalDialog
  248. // ---------------------------------------------------------------------
  249. void ShowMModalDialog(void)
  250.     {
  251.         if (GetCurrMModalDialog())
  252.             {
  253.             // it could still be an I-beam or watch!
  254.             ShowArrowCursor();
  255.             ShowWindow(GetCurrMModalDialog());
  256.             SelectWindow(GetCurrMModalDialog());
  257.             SetPort(GetCurrMModalDialog());
  258.             DrawDialog(GetCurrMModalDialog());
  259.             }
  260.     } // ShowMModalDialog
  261.  
  262.  
  263. // ---------------------------------------------------------------------
  264. // CloseMModalDialog
  265. // ---------------------------------------------------------------------
  266. void CloseMModalDialog(void)
  267.     {
  268.     if (IsMModalDialogDisplayed())
  269.         {
  270.         DisposeDialog(GetCurrMModalDialog());
  271.         SetCurrMModalDialog((DialogPtr)NULL, (HandleDlgEventFn_t)NULL);
  272.         }
  273.     } // CloseMModalDialog
  274.  
  275.  
  276. // ---------------------------------------------------------------------
  277. // HandleMModalDialogEvent
  278. // ---------------------------------------------------------------------
  279. void HandleMModalDialogEvent(EventRecord *eventPtr)
  280.     {
  281.     if (gHandleDlgEventFn)                // if it exists,
  282.         (*gHandleDlgEventFn)(eventPtr);    // call it!
  283.     } // HandleMModalDialogEvent
  284.  
  285.  
  286.  
  287. // ---------------------------------------------------------------------
  288. // SetupStdModalFilter
  289. // ---------------------------------------------------------------------
  290. void SetupStdModalFilter(void)
  291.     {
  292.         // retrieve std one from system
  293.         GetStdFilterProc(&gModalFilterPtr);
  294.     } // SetupStdModalFilter
  295.  
  296. // ---------------------------------------------------------------------
  297. // GetStdModalFilter
  298. // ---------------------------------------------------------------------
  299. // use this as filterproc in all ModalDialog calls
  300. ModalFilterUPP GetStdModalFilter(void)
  301.     {
  302.         return gModalFilterPtr;
  303.     } // GetStdModalFilter
  304.  
  305. // ---------------------------------------------------------------------
  306. // SetupDefaultDialogButtons
  307. // ---------------------------------------------------------------------
  308. // sets the default handlers for the OK and Cancel items in a dialog box.
  309. // This also makes the dialog mgr auto-outline the default (OK) button.
  310. void SetupDefaultDialogButtons(DialogPtr pDialog, short defaultItem, short cancelItem)
  311.     {
  312.     // System 7-only nifty setter-uppers
  313.     if (cancelItem > 0)
  314.         SetDialogCancelItem(pDialog, cancelItem);
  315.     if (defaultItem > 0)
  316.         SetDialogDefaultItem(pDialog, defaultItem);
  317.     } // SetupDefaultDialogButtons()
  318.  
  319.  
  320. // ---------------------------------------------------------------------
  321. // GrabDItemHandle
  322. // ---------------------------------------------------------------------
  323. // returns the ControlHandle for the item in the dialog box
  324. ControlHandle GrabDItemHandle(DialogPtr theDialog, short theGetItem)
  325. {
  326.     short    itemtype;
  327.     Rect    itemrect;
  328.     Handle    tempHandle;
  329.     
  330.     GetDialogItem(theDialog, theGetItem, &itemtype, &tempHandle, &itemrect);
  331.     return((ControlHandle)tempHandle);
  332. } // GrabDItemHandle
  333.  
  334.  
  335. // ---------------------------------------------------------------------
  336. // GetDialogItemValue
  337. // ---------------------------------------------------------------------
  338. // returns the value for the control in the dialog box
  339. short GetDialogItemValue(DialogPtr theDialog, short theGetItem)
  340. {
  341.     Handle    tempHandle;
  342.     
  343.     tempHandle = (Handle)GrabDItemHandle(theDialog, theGetItem);
  344.     if (tempHandle)
  345.         return GetControlValue((ControlHandle)tempHandle);
  346.     else
  347.         return 0;
  348. } // GetDialogItemValue
  349.  
  350.  
  351. // ---------------------------------------------------------------------
  352. // SetDialogItemValue
  353. // ---------------------------------------------------------------------
  354. // sets the value for the control in the dialog box
  355. void SetDialogItemValue(DialogPtr theDialog, short theGetItem, short theVal)
  356. {
  357.     Handle    tempHandle;
  358.     
  359.     tempHandle = (Handle)GrabDItemHandle(theDialog, theGetItem);
  360.     if (tempHandle)
  361.         SetControlValue((ControlHandle)tempHandle,    theVal);
  362. } // SetDialogItemValue
  363.  
  364.  
  365. // ---------------------------------------------------------------------
  366. // FlashDItem
  367. // ---------------------------------------------------------------------
  368. void FlashDItem(DialogPtr theDialog, short theItem)
  369.     {
  370.     long            whoCares;
  371.     ControlHandle    h;
  372.     // find a handle to this control
  373.     h = GrabDItemHandle(theDialog, theItem);
  374.     // flash the control as if somebody clicked it
  375.     HiliteControl(h, 1);    // turn ON hilite
  376.     Delay(8, &whoCares);    // wait 8 ticks (8/60ths of a second)
  377.     HiliteControl(h, 0);    // turn hiliting back off
  378.     } // FlashDItem
  379.  
  380.  
  381. // ---------------------------------------------------------------------
  382. // GetBestDialogPos
  383. // ---------------------------------------------------------------------
  384. // Find the best x,y point for a dialog, by finding which screen
  385. // the status window is on and offsetting from there.  Used by
  386. // dialog routines like SFGetFile and DIBadMount.
  387. void GetBestDialogPos(Point * wherep, WindowPtr otherWindow)
  388. {
  389.     Rect        sfRect;
  390.     GDHandle    thGDev;
  391.  
  392.     // find rect of screen that status window is on..
  393.     if (otherWindow == NULL)
  394.         SetRect(&sfRect, 0, 0, 0, 0);
  395.     else
  396.         GetGlobalWindowRect((WindowPtr)otherWindow, &sfRect);
  397.     thGDev = GetClosestGDevice(&sfRect);
  398.     sfRect = (**thGDev).gdRect;
  399.  
  400.     // ..and offset SF Dialog on that screen
  401.     wherep->h = sfRect.left + 40;
  402.     wherep->v = sfRect.top + 80;
  403. } // GetBestDialogPos
  404.  
  405.  
  406. // ---------------------------------------------------------------------
  407. // DisplayModalDialog
  408. // ---------------------------------------------------------------------
  409. // Displays a modal dialog. param ^0 is the error #, ^1 is the string
  410. short DisplayModalDialog(short dlogID, short defaultItem, short cancelItem,
  411.                     char * s, short err,
  412.                     WindCentering_t doCentering,
  413.                     WindPositioning_t whereToShow)
  414. {
  415.     short        itemHit = 0;
  416.     DialogPtr    myDialog;
  417.     char        numstr[10];
  418.  
  419.     (void)CompactMem(FreeMem());    // force a purge, get some memory
  420.  
  421.     ShowArrowCursor(); // it could still be an I-beam or watch!
  422.  
  423.     // get the dialog
  424.     myDialog = GetNewDialog(dlogID, (Ptr)0L, (WindowPtr)-1L);
  425.  
  426.     if (!myDialog)
  427.         {
  428.         SysBeep(2); // can't get dialog, at least beep
  429.         SysBeep(2);
  430.         }
  431.     else
  432.     {
  433.         // convert error string to P-style if not null
  434.         if (s != NULL)
  435.             c2pstr(s);
  436.  
  437.         // see if there is an error number
  438.         numstr[0] = '\0';
  439.         if (err != 0)
  440.             NumToString(err, (StringPtr)numstr);
  441.  
  442.         // stick parms into dialog
  443.         ParamText((StringPtr)numstr, (StringPtr)s, "\p", "\p");
  444.  
  445.         // put it where it should be
  446.         PositionWindow(myDialog, doCentering, whereToShow, (WindowPtr)NULL /*gp2wWindow*/ );
  447.         // set the standard action for OK/cancel buttons
  448.         SetupDefaultDialogButtons(myDialog, defaultItem, cancelItem);
  449.         // show it
  450.         ShowWindow(myDialog);
  451.         // prompt the user
  452.         ModalDialog(GetStdModalFilter(), &itemHit);
  453.         // all done with it
  454.         DisposeDialog(myDialog);
  455.     }
  456.  
  457.     return itemHit;
  458.  
  459. } // DisplayModalDialog
  460.  
  461.