home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks95 / Closure.sit / Closure / Sources / PhotoshopHeaders / DialogUtilities.c < prev    next >
Text File  |  1995-06-24  |  16KB  |  804 lines

  1. /*
  2.     File: DialogUtilities.c
  3.  
  4.     Copyright 1993-95 by Adobe Systems, Inc. All rights reserved.
  5.  
  6.     C source file for plug-in dialog utilities.
  7. */
  8.  
  9. #include "Photoshop.h"
  10. #include "DialogUtilities.h"
  11.  
  12. /*****************************************************************************/
  13.  
  14. /* The following routine locates the QuickDraw globals. */
  15.  
  16. static QDGlobals *GetQDGlobals (void)
  17.     {
  18.     
  19.     return (QDGlobals *) ((* (char **) SetCurrentA5 ()) -
  20.                                     (sizeof (QDGlobals) - sizeof (GrafPtr)));
  21.     
  22.     }
  23.  
  24. /*****************************************************************************/
  25.  
  26. /* Set the cursor to the arrow cursor. */
  27.  
  28. void SetArrowCursor ()
  29.     {
  30.     
  31.     QDGlobals *qd = GetQDGlobals ();
  32.     
  33.     SetCursor (&(qd->arrow));
  34.     
  35.     }
  36.  
  37. /*****************************************************************************/
  38.  
  39. /* Centers a dialog template 1/3 of the way down on the main screen. */
  40.  
  41. /* Centers a dialog template 1/3 of the way down on the main screen. */
  42. void CenterDialog (DialogTHndl dt)
  43.     {
  44.  
  45.     #define MenuHeight 20
  46.  
  47.     Rect screenBounds = (*GetMainDevice())->gdRect;
  48.  
  49.     short width = screenBounds.right - screenBounds.left;
  50.     short height = screenBounds.bottom - screenBounds.top;
  51.  
  52.     Rect dialogBounds = (**dt).boundsRect;
  53.  
  54.     OffsetRect (&dialogBounds, -dialogBounds.left, -dialogBounds.top);
  55.     OffsetRect (&dialogBounds, (width - dialogBounds.right) / 2,
  56.                     (height - dialogBounds.bottom - MenuHeight) / 3 + MenuHeight);
  57.     (**dt).boundsRect = dialogBounds;
  58.     
  59.     #undef menuHeight
  60.  
  61.     }
  62.  
  63. /*****************************************************************************/
  64.  
  65. /* Test for Adobe Moveable Modal Dialog if running System 6 */
  66.  
  67. void SetUpMoveableModal (DialogTHndl dt, OSType hostSig)
  68.     {
  69.  
  70.     long    response;
  71.     
  72.     if (Gestalt (gestaltSystemVersion, &response) != noErr)
  73.         response = 0;
  74.         
  75.     if (response < 0x0700)
  76.         {    /* Prior to System 7... */
  77.  
  78.         Handle    windRes = nil;
  79.         
  80.         /* We don't have to release the resource, because opening the
  81.            dialog will do the right thing with it if it exists. */
  82.  
  83.         if (hostSig == '8BIM')
  84.             windRes = GetResource ('WDEF', PSmovableDBoxProc/16);
  85.             
  86.         if (windRes != NULL)
  87.             (*dt)->procID = PSmovableDBoxProc;
  88.         else
  89.             (*dt)->procID = dBoxProc;        /* not moveable, sorry */
  90.  
  91.         }
  92.         
  93.     }
  94.  
  95. /*****************************************************************************/
  96.  
  97. typedef struct ModalData
  98.     {
  99.     
  100.     long oldRefCon;
  101.     
  102.     ModalFilterProcPtr filter;
  103.     
  104.     ProcessEventProc processEvent;
  105.     
  106.     } ModalData;
  107.  
  108. /*****************************************************************************/
  109.  
  110. /* Event filter to allow movable modal dialogs. */
  111.  
  112. static pascal Boolean DialogFilter (DialogPtr dp,
  113.                                     EventRecord *event,
  114.                                     short *item)
  115.     {
  116.  
  117.     Boolean result = FALSE;
  118.     GrafPtr savePort;
  119.     
  120.     ModalData *data = (ModalData *) GetWRefCon (dp);
  121.     
  122.     if (data->filter)
  123.         {
  124.         
  125.         SetWRefCon (dp, data->oldRefCon);
  126.         
  127.         result = (*data->filter) (dp, event, item);
  128.         
  129.         data->oldRefCon = GetWRefCon (dp);
  130.         
  131.         SetWRefCon (dp, (long) data);
  132.         
  133.         if (result)
  134.             return TRUE;
  135.         
  136.         }
  137.     
  138.     if (event->what == mouseDown)
  139.         {
  140.  
  141.         /* We have to do window-dragging ourselves */
  142.  
  143.         Point pt = event->where;
  144.         WindowPtr window;
  145.  
  146.         if (FindWindow (pt, &window) == inDrag && window == dp)
  147.             {
  148.  
  149.             Rect bounds = (*GetGrayRgn())->rgnBBox;
  150.  
  151.             InsetRect (&bounds, 4, 4);
  152.  
  153.             DragWindow (window, pt, &bounds);
  154.  
  155.             event->what = nullEvent;
  156.  
  157.             }
  158.  
  159.         }
  160.  
  161.     else if (event->what == updateEvt || event->what == activateEvt)
  162.         {
  163.  
  164.         /* Pass updates and activates out to the host */
  165.  
  166.         if (data->processEvent && (event->message != ((long) dp)))
  167.             (*data->processEvent) (event);
  168.  
  169.         }
  170.  
  171.     else if (event->what == nullEvent)
  172.         {
  173.  
  174.         /* Let the host idle */
  175.  
  176.         if (data->processEvent)
  177.             (*data->processEvent) (event);
  178.  
  179.         }
  180.         
  181.     GetPort (&savePort);
  182.     SetPort (dp);
  183.         
  184.     result = StdFilterProc (dp, event, item);
  185.     
  186.     SetPort (savePort);
  187.  
  188.     return result;
  189.  
  190.     }
  191.  
  192. /*****************************************************************************/
  193.  
  194. #ifdef __powerc
  195.  
  196. static RoutineDescriptor DialogFilterRDS = BUILD_ROUTINE_DESCRIPTOR(uppModalFilterProcInfo, &DialogFilter);
  197.  
  198. #define DialogFilterRD (&DialogFilterRDS)
  199.  
  200. #else
  201.  
  202. #define DialogFilterRD (&DialogFilter)
  203.  
  204. #endif
  205.  
  206. /*****************************************************************************/
  207.  
  208. void MoveableModalDialog (DialogPtr dp,
  209.                           ProcessEventProc processEvent,
  210.                           ModalFilterProcPtr filter,
  211.                           short *item)
  212.     {
  213.     
  214.     ModalData data;
  215.     
  216.     data.oldRefCon = GetWRefCon (dp);
  217.     data.filter = filter;
  218.     data.processEvent = processEvent;
  219.     
  220.     SetWRefCon (dp, (long) &data);
  221.     
  222.     ModalDialog (DialogFilterRD, item);
  223.     
  224.     SetWRefCon (dp, data.oldRefCon);
  225.     
  226.     }
  227.  
  228. /*****************************************************************************/
  229.  
  230. long GetMoveableWRefCon (DialogPtr dp)
  231.     {
  232.     
  233.     ModalData *data = (ModalData *) GetWRefCon (dp);
  234.     
  235.     return data->oldRefCon;
  236.     
  237.     }
  238.  
  239. /*****************************************************************************/
  240.  
  241. short ShowAlert (short alertID)
  242.     {
  243.     
  244.     Handle alertHandle;
  245.     short result;
  246.     
  247.     alertHandle = GetResource ('ALRT', alertID);
  248.     HNoPurge (alertHandle);
  249.     
  250.     CenterDialog ((DialogTHndl) alertHandle);
  251.     
  252.     result = Alert (alertID, nil);
  253.     
  254.     HPurge (alertHandle);
  255.  
  256.     return result;
  257.     
  258.     }
  259.  
  260. /*****************************************************************************/
  261.  
  262. void ShowAbout (short dialogID)
  263.     {
  264.  
  265.     short item;
  266.     DialogPtr dp;
  267.     DialogTHndl dt;
  268.  
  269.     dt = (DialogTHndl) GetResource ('DLOG', dialogID);
  270.     HNoPurge ((Handle) dt);
  271.  
  272.     CenterDialog (dt);
  273.  
  274.     dp = GetNewDialog (dialogID, nil, (WindowPtr) -1);
  275.     
  276.     SetArrowCursor ();
  277.     
  278.     ModalDialog (nil, &item);
  279.  
  280.     DisposDialog (dp);
  281.     HPurge ((Handle) dt);
  282.  
  283.     }
  284.  
  285. /*****************************************************************************/
  286.  
  287. /* UserItem to outline a button group box, except for top text */
  288.  
  289. static pascal void OutlineGroup (DialogPtr dp, short item)
  290.     {
  291.  
  292.     Rect     r, rText;
  293.     Handle     h;
  294.     short     itemType;
  295.     PenState originalPenState;
  296.  
  297.     GetPenState (&originalPenState);
  298.  
  299.     GetDialogItem (dp, item+1, &itemType, &h, &rText);    /* bounds of surrounding text */
  300.     GetDialogItem (dp, item,   &itemType, &h, &r);        /* the group box */
  301.     
  302.     PenNormal ();
  303.     PenSize (1, 1);
  304.     
  305.     MoveTo (rText.right, r.top);
  306.     LineTo (r.right, r.top);
  307.     LineTo (r.right, r.bottom);
  308.     LineTo (r.left, r.bottom);
  309.     LineTo (r.left, r.top);
  310.     LineTo (rText.left, r.top);
  311.     
  312.     SetPenState (&originalPenState);
  313.     
  314.     }
  315.  
  316. /*****************************************************************************/
  317.  
  318. #ifdef __powerc
  319.  
  320. static RoutineDescriptor OutlineGroupRDS =
  321.     BUILD_ROUTINE_DESCRIPTOR(uppUserItemProcInfo, &OutlineGroup);
  322.  
  323. #define OutlineGroupRD (&OutlineGroupRDS)
  324.  
  325. #else
  326.  
  327. #define OutlineGroupRD (&OutlineGroup)
  328.  
  329. #endif
  330.  
  331. /*****************************************************************************/
  332.  
  333. /* The following routine sets a user item to be a group box.  It expects
  334.    the next item to be the title for the group box. */
  335.  
  336. void SetOutlineGroup (DialogPtr dp, short groupItem)
  337.     {
  338.     
  339.     short itemType;
  340.     Rect r;
  341.     Handle h;
  342.  
  343.     GetDialogItem (dp, groupItem, &itemType, &h                     , &r);
  344.     SetDialogItem (dp, groupItem,  itemType, (Handle) OutlineGroupRD, &r);
  345.     
  346.     }
  347.  
  348. /*****************************************************************************/
  349.  
  350. /* The following routine selects an edit text item. */
  351.  
  352. void SelectTextItem (DialogPtr dp, short item)
  353.     {
  354.     
  355.     SelIText (dp, item, 0, 32767);
  356.     
  357.     }
  358.  
  359. /*****************************************************************************/
  360.  
  361. /* The following routine sets the text of a text item. */
  362.  
  363. void StuffText (DialogPtr dp, short item, Str255 text)
  364.     {
  365.     
  366.     Rect r;
  367.     short itemType;
  368.     Handle textHdl;
  369.     
  370.     GetDialogItem (dp, item, &itemType, &textHdl, &r);
  371.     
  372.     SetDialogItemText (textHdl, text);
  373.     
  374.     }
  375.  
  376. /*****************************************************************************/
  377.  
  378. /* The following routine extracts the text of a text item. */
  379.  
  380. void FetchText (DialogPtr dp, short item, Str255 text)
  381.     {
  382.     
  383.     Rect r;
  384.     short itemType;
  385.     Handle textHdl;
  386.     
  387.     GetDialogItem (dp, item, &itemType, &textHdl, &r);
  388.     
  389.     GetDialogItemText (textHdl, text);
  390.     
  391.     }
  392.  
  393. /*****************************************************************************/
  394.  
  395. /* The following routine stuffs a numeric value into a text field. */
  396.  
  397. void StuffNumber (DialogPtr dp, short item, long value)
  398.     {
  399.     
  400.     Str255 s;
  401.         
  402.     NumToString (value, s);
  403.     
  404.     StuffText (dp, item, s);
  405.     
  406.     }
  407.  
  408. /*****************************************************************************/
  409.  
  410. static Boolean StringToNumber (Str255 s, long *value)
  411.     {
  412.     
  413.     short i;
  414.     short j;
  415.     long x;
  416.     Boolean negative = FALSE;
  417.     Boolean isNumber = TRUE;
  418.     Boolean trailingBlanks = FALSE;
  419.     
  420.     for (i = 1, j = 0; i <= s[0] && isNumber; ++i)
  421.         {
  422.         
  423.         if (j == 0 && s [i] == ' ')
  424.             ; /* Do nothing: Leading blanks */
  425.             
  426.         else if (j > 0 && s [i] == ' ')
  427.             trailingBlanks = TRUE;
  428.             
  429.         else if (trailingBlanks && s [i] != ' ')
  430.             isNumber = FALSE;
  431.             
  432.         else if (j == 0 && !negative && s [i] == '-')
  433.             negative = TRUE;
  434.             
  435.         else if (s [i] < '0' || s [i] > '9')
  436.             isNumber = FALSE;
  437.             
  438.         else
  439.             s [++j] = s [i];
  440.         
  441.         }
  442.         
  443.     if (j == 0)
  444.         isNumber = FALSE;
  445.     else
  446.         s [0] = (char) j;
  447.     
  448.     if (isNumber)
  449.         {
  450.         
  451.         if (j <= 9)
  452.             StringToNum (s, &x);
  453.         else
  454.             x = 0x7FFFFFFF;
  455.         
  456.         if (negative)
  457.             x = -x;
  458.             
  459.         *value = x;
  460.             
  461.         }
  462.         
  463.     return isNumber;
  464.     
  465.     }
  466.  
  467. /*****************************************************************************/
  468.  
  469. /* 
  470.    Here is the corresponding routine to retrieve the value from a text
  471.    field.  It will do range checking and validate that it has been
  472.    handed a number.  If it has not been handed a number, it brings up
  473.    an appropriate error dialog, inserts an appropriately pinned value,
  474.    and selects the item.
  475.   
  476.    It returns TRUE iff it gets a valid value in the field. */
  477.  
  478. Boolean FetchNumber (DialogPtr dp,
  479.                      short item,
  480.                      long min,
  481.                      long max,
  482.                      long *value)
  483.     {
  484.     
  485.     #define badNumberID 16990
  486.     #define outOfRangeID 16991
  487.     
  488.     Str255 s;
  489.     long x;
  490.     Boolean notANumber;
  491.     
  492.     FetchText (dp, item, s);
  493.     
  494.     notANumber = !StringToNumber (s, &x);
  495.     
  496.     if (notANumber || x < min || x > max)
  497.         {
  498.         
  499.         Str255 minText;
  500.         Str255 maxText;
  501.         
  502.         NumToString (min, minText);
  503.         NumToString (max, maxText);
  504.         
  505.         ParamText (minText, maxText, nil, nil);
  506.         
  507.         (void) ShowAlert (notANumber ? badNumberID : outOfRangeID);
  508.                           
  509.         if (!notANumber)
  510.             {
  511.             
  512.             x = (x < min ? min : max);
  513.             
  514.             StuffNumber (dp, item, x);
  515.             
  516.             }
  517.             
  518.         SelectTextItem (dp, item);
  519.             
  520.         return FALSE;
  521.         
  522.         }
  523.     
  524.     else
  525.         {
  526.         
  527.         *value = x;
  528.         
  529.         return TRUE;
  530.         
  531.         }
  532.         
  533.     #undef badNumberID
  534.     #undef outOfRangeID
  535.     
  536.     }
  537.  
  538.  
  539. /*****************************************************************************/
  540.  
  541. /* Set the state of a check box (or radio button). */
  542.  
  543. void SetCheckBoxState (DialogPtr dp, short item, Boolean checkIt)
  544.     {
  545.  
  546.     Rect    r;
  547.     Handle    ctl;
  548.     short    itemType;
  549.     short    oldValue, newValue;
  550.     
  551.     GetDialogItem (dp, item, &itemType, &ctl, &r);
  552.  
  553.     oldValue =  GetCtlValue ((ControlHandle) ctl);
  554.     
  555.     newValue = checkIt ? 1 : 0;
  556.  
  557.     if (oldValue != newValue)
  558.         SetCtlValue ((ControlHandle) ctl, newValue);
  559.  
  560.     }
  561.  
  562. /*****************************************************************************/
  563.  
  564. /* Determine the state of a check box (or radio button). */
  565.  
  566. Boolean GetCheckBoxState (DialogPtr dp, short item)
  567.     {
  568.     
  569.     Rect    r;
  570.     Handle    ctl;
  571.     short    itemType;
  572.     short    oldValue;
  573.     
  574.     GetDialogItem (dp, item, &itemType, &ctl, &r);
  575.  
  576.     oldValue = GetCtlValue ((ControlHandle) ctl);
  577.     
  578.     return (oldValue != 0);
  579.     
  580.     }
  581.  
  582. /*****************************************************************************/
  583.  
  584. /* Toggle a check box and return the new state. */
  585.  
  586. Boolean ToggleCheckBoxState (DialogPtr dp, short item)
  587.     {
  588.     
  589.     Boolean newState = !GetCheckBoxState (dp, item);
  590.     
  591.     SetCheckBoxState (dp, item, newState);
  592.     
  593.     return newState;
  594.     
  595.     }
  596.  
  597. /*****************************************************************************/
  598.  
  599. /* Set a radio group (from first to last item) to reflect the selection. */
  600.  
  601. void SetRadioGroupState (DialogPtr dp,
  602.                          short first,
  603.                          short last,
  604.                          short item)
  605.     {
  606.  
  607.     short            i;
  608.     
  609.     for (i = first; i <= last; ++i)
  610.         SetCheckBoxState (dp, i, i == item);
  611.  
  612.     }
  613.  
  614. /*****************************************************************************/
  615.  
  616. /* Get the selected radio button in a group. */
  617.  
  618. short GetRadioGroupState (DialogPtr dp, short first, short last)
  619.     {
  620.     
  621.     short i;
  622.     
  623.     for (i = first; i <= last; ++i)
  624.         if (GetCheckBoxState (dp, i))
  625.             return i;
  626.             
  627.     return 0;
  628.     
  629.     }
  630.  
  631. /*****************************************************************************/
  632.  
  633. /* Set the state of a check box (or radio button). */
  634.  
  635. void SetPopUpMenuValue (DialogPtr dp, short item, short newValue)
  636.     {
  637.  
  638.     Rect    r;
  639.     Handle    ctl;
  640.     short    itemType;
  641.     short    oldValue;
  642.     
  643.     GetDialogItem (dp, item, &itemType, &ctl, &r);
  644.  
  645.     oldValue =  GetCtlValue ((ControlHandle) ctl);
  646.     
  647.     if (oldValue != newValue)
  648.         SetCtlValue ((ControlHandle) ctl, newValue);
  649.  
  650.     }
  651.  
  652. /*****************************************************************************/
  653.  
  654. /* Determine the state of a check box (or radio button). */
  655.  
  656. short GetPopUpMenuValue (DialogPtr dp, short item)
  657.     {
  658.     
  659.     Rect    r;
  660.     Handle    ctl;
  661.     short    itemType;
  662.     
  663.     GetDialogItem (dp, item, &itemType, &ctl, &r);
  664.  
  665.     return GetCtlValue ((ControlHandle) ctl);
  666.     
  667.     }
  668.  
  669. /*****************************************************************************/
  670.  
  671. /* Utility routine to disable a control. */
  672.  
  673. void DisableControl (DialogPtr dp, short item)
  674.     {
  675.  
  676.     Rect    r;
  677.     Handle    ctl;
  678.     short    itemType;
  679.     short    oldValue;
  680.     
  681.     GetDialogItem (dp, item, &itemType, &ctl, &r);
  682.  
  683.     itemType |= itemDisable;
  684.  
  685.     oldValue = GetCtlValue ((ControlHandle) ctl);
  686.     if (oldValue != 0)
  687.         SetCtlValue ((ControlHandle) ctl, 0);
  688.  
  689.     HiliteControl ((ControlHandle) ctl, 255);
  690.  
  691.     SetDialogItem (dp, item, itemType, ctl, &r);
  692.  
  693.     }
  694.  
  695. /*****************************************************************************/
  696.  
  697. /* Utility routine to enable a control. */
  698.  
  699. void EnableControl (DialogPtr dp, short item)
  700.     {
  701.  
  702.     Rect    r;
  703.     Handle    ctl;
  704.     short    itemType;
  705.     
  706.     GetDialogItem (dp, item, &itemType, &ctl, &r);
  707.  
  708.     itemType &= ~itemDisable;
  709.  
  710.     HiliteControl ((ControlHandle) ctl, 0);
  711.  
  712.     SetDialogItem (dp, item, itemType, ctl, &r);
  713.  
  714.     }
  715.  
  716. /*****************************************************************************/
  717.  
  718. /* Utility routine to enable a control. */
  719.  
  720. void EnableDisableControl (DialogPtr dp, short item, Boolean state)
  721.     {
  722.     
  723.     if (state)
  724.         EnableControl (dp, item);
  725.     else
  726.         DisableControl (dp, item);
  727.     
  728.     }
  729.  
  730. /*****************************************************************************/
  731.  
  732. /* Utility routine to invalidate an item. */
  733.  
  734. void InvalItem (DialogPtr dp, short item)
  735.     {
  736.     
  737.     Rect    r;
  738.     Handle    h;
  739.     short    itemType;
  740.     GrafPtr oldPort;
  741.     
  742.     GetDialogItem (dp, item, &itemType, &h, &r);
  743.     
  744.     GetPort (&oldPort);
  745.     
  746.     SetPort (dp);
  747.     
  748.     InvalRect (&r);
  749.     
  750.     SetPort (oldPort);
  751.     
  752.     }
  753.  
  754. /*****************************************************************************/
  755.  
  756. /* Perform standard handling for check boxes and radio buttons. For radio
  757.    buttons, we assume that the group for the radio button extends forward
  758.    and backward in the DITL as long as the item type is radio button. */
  759.    
  760. void PerformStandardDialogItemHandling (DialogPtr dp, short item)
  761.     {
  762.     
  763.     Rect r;
  764.     Handle h;
  765.     short itemType;
  766.     
  767.     short first, last, count;
  768.     
  769.     GetDialogItem (dp, item, &itemType, &h, &r);
  770.     
  771.     switch (itemType)
  772.         {
  773.         
  774.         case ctrlItem+chkCtrl:
  775.             (void) ToggleCheckBoxState (dp, item);
  776.             break;
  777.             
  778.         case ctrlItem+radCtrl:
  779.             first = last = item;
  780.             while (first > 1)
  781.                 {
  782.                 GetDialogItem (dp, first - 1, &itemType, &h, &r);
  783.                 if (itemType != ctrlItem+radCtrl)
  784.                     break;
  785.                 --first;
  786.                 }
  787.             count = CountDITL (dp);
  788.             while (last < count)
  789.                 {
  790.                 GetDialogItem (dp, last + 1, &itemType, &h, &r);
  791.                 if (itemType != ctrlItem+radCtrl)
  792.                     break;
  793.                 ++last;
  794.                 }
  795.             SetRadioGroupState (dp, first, last, item);
  796.             break;
  797.         
  798.         }
  799.     
  800.     }
  801.  
  802. /*****************************************************************************/
  803.  
  804.