home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Today - The Disc! 8 / cdrt08.iso / mac / Shareware / HyperCard / demoCdef 120 ƒ / demo Source ƒ / dialogAssist.c < prev    next >
Encoding:
Text File  |  1994-12-12  |  28.9 KB  |  1,065 lines  |  [TEXT/KAHL]

  1. //----------------------------------------------------------------------------------
  2. //    File        : dialogAssist.c
  3. //    Date        : April 4, 1994
  4. //    Author        : Jim Stout
  5. //    Purpose        : Dialog utilities that make use of ModalDialog a bit easier.
  6. //
  7. //                    I got really tired of writing all those GetDItem/SetDItem calls
  8. //                    and then having to mix them with Control Manager and Dialog
  9. //                    Manager calls (that have various syntax rules). So, these are
  10. //                    are a start at some easier routines.
  11. //
  12. //                    Also, there are routines here for handling keys, limits on
  13. //                    editText DITL items, mouseClicks, cursors and dimming items -
  14. //                    and lots of other stuff.  Take a look at the dialogAssist.h
  15. //                    file prototypes - the routine names are pretty self explanatory.
  16. //----------------------------------------------------------------------------------
  17.  
  18. #include <GestaltEqu.h>
  19. #include <Traps.h>
  20. #include "dialogAssist.h"
  21. #include "dimText.h"
  22.  
  23. #define BEEP 1
  24.  
  25. #pragma mark _ControlRoutines
  26.  
  27. //----------------------------------------------------------------------------------
  28. //    Function: daToggleCheck
  29. //     Purpose: toggles the state of a checkbox.
  30. //                Does nothing if called for a non-checkbox item.
  31. //
  32. //   returns: void
  33. //----------------------------------------------------------------------------------
  34. void daToggleCheck (DialogPtr d, short i)
  35. {
  36.     short            t;
  37.     ControlHandle    h;
  38.     Rect            r;
  39.  
  40.     GetDItem(d, i, &t, (Handle *)&h, &r);
  41.     if(h && (t & chkCtrl)) {
  42.         SetCtlValue(h, !GetCtlValue(h));
  43.     }
  44. }
  45.  
  46. //----------------------------------------------------------------------------------
  47. //    Function: daToggleRadio
  48. //     Purpose: set radio button "i" ON and turns the others off.
  49. //                N.B. This routine assumes that radio buttons are
  50. //                sequentially numbered from "first" to "last" 
  51. //                Does nothing if called for a non-radioBtn item.
  52. //
  53. //   returns: void
  54. //----------------------------------------------------------------------------------
  55. void daToggleRadio (DialogPtr d, short i, short first, short last)
  56. {
  57.     short            t,inx;
  58.     ControlHandle    h;
  59.     Rect            r;
  60.  
  61.     for(inx=first;inx<=last;inx++) {
  62.         GetDItem(d, inx, &t, (Handle *)&h, &r);
  63.         if(h && (t & radCtrl)) {
  64.             if(inx == i)
  65.                 SetCtlValue(h, ON);
  66.             else
  67.                 SetCtlValue(h, OFF);
  68.         }
  69.     }
  70. }
  71.  
  72. //----------------------------------------------------------------------------------
  73. //    Function: daGetRadio
  74. //     Purpose: Return the number of the "on" radio button in a set.
  75. //                N.B. This routine assumes that radio buttons are
  76. //                sequentially numbered from "first" to "last" 
  77. //                Does nothing if called for a non-radioBtn item.
  78. //
  79. //   returns: void
  80. //----------------------------------------------------------------------------------
  81. short daGetRadio (DialogPtr d, short first, short last)
  82. {
  83.     short            t,inx;
  84.     ControlHandle    h;
  85.     Rect            r;
  86.  
  87.     for(inx=first;inx<=last;inx++) {
  88.         GetDItem(d, inx, &t, (Handle *)&h, &r);
  89.         if(h && (t & radCtrl)) {
  90.             if(GetCtlValue(h))
  91.                 return(inx);
  92.         }
  93.     }
  94.     return(0);
  95. }
  96. //----------------------------------------------------------------------------------
  97. //    Function: daToggleCtl
  98. //     Purpose: toggles the state of a control.
  99. //                Does nothing if called for a non-control item.
  100. //
  101. //   returns: void
  102. //----------------------------------------------------------------------------------
  103. void daToggleCtl (DialogPtr d, short i)
  104. {
  105.     short            t;
  106.     ControlHandle    h;
  107.     Rect            r;
  108.  
  109.     GetDItem(d, i, &t, (Handle *)&h, &r);
  110.     if(h && (t & ctrlItem)) {
  111.         SetCtlValue(h, !GetCtlValue(h));
  112.     }
  113. }
  114. //----------------------------------------------------------------------------------
  115. //    Function: daGetCtlHandle
  116. //     Purpose: get the handle to a control item in a dialog.
  117. //                Does nothing if called for a non-control item.
  118. //
  119. //   returns: value of control
  120. //----------------------------------------------------------------------------------
  121. ControlHandle daGetCtlHandle (DialogPtr d, short i)
  122. {
  123.     short            t;
  124.     ControlHandle    h;
  125.     Rect            r;
  126.  
  127.     GetDItem(d, i, &t, (Handle *)&h, &r);
  128.     if(h && (t & ctrlItem)) {
  129.         return (h);
  130.     }
  131.     return(0);
  132. }
  133.  
  134. //----------------------------------------------------------------------------------
  135. //    Function: daGetCtlMax
  136. //     Purpose: get the maximum value of a control item in a dialog.
  137. //                Does nothing if called for a non-control item.
  138. //
  139. //   returns: value of control
  140. //----------------------------------------------------------------------------------
  141. short daGetCtlMax (DialogPtr d, short i)
  142. {
  143.     short            t;
  144.     ControlHandle    h;
  145.     Rect            r;
  146.  
  147.     GetDItem(d, i, &t, (Handle *)&h, &r);
  148.     if(h && (t & ctrlItem)) {
  149.         return ((**h).contrlMax);
  150.     }
  151.     return(0);
  152. }
  153. //----------------------------------------------------------------------------------
  154. //    Function: daGetCtlMin
  155. //     Purpose: get the minimum value of a control item in a dialog.
  156. //                Does nothing if called for a non-control item.
  157. //
  158. //   returns: value of control
  159. //----------------------------------------------------------------------------------
  160. short daGetCtlMin (DialogPtr d, short i)
  161. {
  162.     short            t;
  163.     ControlHandle    h;
  164.     Rect            r;
  165.  
  166.     GetDItem(d, i, &t, (Handle *)&h, &r);
  167.     if(h && (t & ctrlItem)) {
  168.         return ((**h).contrlMin);
  169.     }
  170.     return(0);
  171. }
  172.  
  173. //----------------------------------------------------------------------------------
  174. //    Function: daGetCtlValue
  175. //     Purpose: get the value of a control item in a dialog.
  176. //                Does nothing if called for a non-control item.
  177. //
  178. //   returns: value of control
  179. //----------------------------------------------------------------------------------
  180. short daGetCtlValue (DialogPtr d, short i)
  181. {
  182.     short            t;
  183.     ControlHandle    h;
  184.     Rect            r;
  185.  
  186.     GetDItem(d, i, &t, (Handle *)&h, &r);
  187.     if(h && (t & ctrlItem)) {
  188.         return (GetCtlValue(h));
  189.     }
  190.     return(0);
  191. }
  192. //----------------------------------------------------------------------------------
  193. //    Function: daSetCtlValue
  194. //     Purpose: set the value of a control item in a dialog.
  195. //                Does nothing if called for a non-control item.
  196. //
  197. //   returns: void
  198. //----------------------------------------------------------------------------------
  199.  
  200. void daSetCtlValue (DialogPtr d, short i, short newVal)
  201. {
  202.     short            t;
  203.     ControlHandle    h;
  204.     Rect            r;
  205.  
  206.     GetDItem(d, i, &t, (Handle *)&h, &r);
  207.     if(h && (t & ctrlItem)) {
  208.         SetCtlValue(h, newVal);
  209.     }
  210. }
  211.  
  212. //----------------------------------------------------------------------------------
  213. //    Function: daGetCtlTitle
  214. //     Purpose: Get the title of a control item in a dialog.
  215. //                Does nothing if called for a non-control item.
  216. //
  217. //   returns: void
  218. //----------------------------------------------------------------------------------
  219. void daGetCtlTitle (DialogPtr d, short i, Str255 theTitle)
  220. {
  221.     short            t;
  222.     ControlHandle    h;
  223.     Rect            r;
  224.  
  225.     GetDItem(d, i, &t, (Handle *)&h, &r);
  226.     if(h && (t & ctrlItem)) {
  227.         pStrCopy((char *)theTitle,(char *)(*h)->contrlTitle);
  228.     }
  229. }
  230. //----------------------------------------------------------------------------------
  231. //    Function: daSetCtlTitle
  232. //     Purpose: set the title of a control item in a dialog.
  233. //                Does nothing if called for a non-control item.
  234. //
  235. //   returns: void
  236. //----------------------------------------------------------------------------------
  237. void daSetCtlTitle (DialogPtr d, short i, Str255 newTitle)
  238. {
  239.     short            t;
  240.     ControlHandle    h;
  241.     Rect            r;
  242.  
  243.     GetDItem(d, i, &t, (Handle *)&h, &r);
  244.     if(h && (t & ctrlItem)) {
  245.         SetCTitle(h, newTitle);
  246.     }
  247. }
  248.  
  249. //----------------------------------------------------------------------------------
  250. //    Function: daGetCtlRefCon
  251. //     Purpose: get the refCon of a control item in a dialog.
  252. //                Does nothing if called for a non-control item.
  253. //
  254. //   returns: refCon
  255. //----------------------------------------------------------------------------------
  256. long daGetCtlRefCon (DialogPtr d, short i)
  257. {
  258.     short            t;
  259.     ControlHandle    h;
  260.     Rect            r;
  261.  
  262.     GetDItem(d, i, &t, (Handle *)&h, &r);
  263.     if(h && (t & ctrlItem))
  264.         return(GetCRefCon(h));
  265.     else
  266.         return(0L);
  267. }
  268.  
  269. //----------------------------------------------------------------------------------
  270. //    Function: daSetCtlRefCon
  271. //     Purpose: set the refCon of a control item in a dialog.
  272. //                Does nothing if called for a non-control item.
  273. //
  274. //----------------------------------------------------------------------------------
  275. void daSetCtlRefCon(DialogPtr d, short i,long theValue)
  276. {
  277.     short            theType;
  278.     Rect            theRect;
  279.     Handle            theCtl;
  280.     
  281.     short            t;
  282.     ControlHandle    h;
  283.     Rect            r;
  284.  
  285.     GetDItem(d, i, &t, (Handle *)&h, &r);
  286.     if(h && (t & ctrlItem))
  287.         SetCRefCon(h,theValue);
  288. }
  289.  
  290. //----------------------------------------------------------------------------------
  291. //    Function: daGetCtlRect
  292. //     Purpose: get the rect of a control item in a dialog.
  293. //                Does nothing if called for a non-control item.
  294. //
  295. //----------------------------------------------------------------------------------
  296. void daGetCtlRect(DialogPtr d, short i, Rect * r)
  297. {
  298.     short            t;
  299.     ControlHandle    h;
  300.     Rect            r1;
  301.     
  302.     GetDItem(d, i, &t, (Handle *)&h, &r1);
  303.     if(h && (t & ctrlItem))
  304.         *r = (*h)->contrlRect;
  305. }
  306.  
  307. //----------------------------------------------------------------------------------
  308. //    Function: daSetCtlRect
  309. //     Purpose: set the rect of a control item in a dialog.
  310. //                Does nothing if called for a non-control item.
  311. //
  312. //----------------------------------------------------------------------------------
  313. void daSetCtlRect(DialogPtr d, short i, Rect * newRect)
  314. {
  315.     short            t,h,v;
  316.     Rect            r;
  317.     ControlHandle    theCtl;
  318.     
  319.     GetDItem(d, i, &t, (Handle *)&theCtl, &r);
  320.     if(h && (t & ctrlItem)) {
  321.         h = (*newRect).right - (*newRect).left;
  322.         v = (*newRect).bottom - (*newRect).top;
  323.         SizeControl(theCtl, h, v);
  324.     }
  325. }
  326.  
  327. //----------------------------------------------------------------------------------
  328. //    Function: daIsHidden
  329. //     Purpose: find out if a dialog item is hidden.
  330. //            :
  331. //
  332. //   returns: true if item is hidden
  333. //----------------------------------------------------------------------------------
  334. Boolean daIsHidden(DialogPtr d, short i)
  335. {
  336.     short            t;
  337.     ControlHandle    h;
  338.     Rect            r;
  339.  
  340.     GetDItem(d, i, &t, (Handle *)&h, &r);
  341.     if(r.left > 8192)
  342.         return(true);
  343.     return(false);
  344. }
  345. #pragma mark _MouseRoutines
  346.  
  347. //----------------------------------------------------------------------------------
  348. //    Function: daMouseItem
  349. //     Purpose: check entire DITL list for a mouse click in an item.
  350. //            :
  351. //   returns: item clicked
  352. //----------------------------------------------------------------------------------
  353. short daMouseWhere (DialogPtr d, EventRecord * theEvt)
  354. {
  355.     short            inx,max,t;
  356.     ControlHandle    h;
  357.     Rect            r;
  358.     Point            mp;
  359.     
  360.     mp = theEvt->where;
  361.     GlobalToLocal(&mp);
  362.  
  363.     max = (**(short **) (((DialogPeek) d)->items)) +1;
  364.     for(inx=1;inx<=max;inx++) {
  365.         GetDItem(d, inx, &t, (Handle *)&h, &r);
  366.         if(PtInRect(mp, &r))
  367.             return(inx);
  368.     }
  369.     return(0);
  370. }
  371. //----------------------------------------------------------------------------------
  372. //    Function: daMouseInItem
  373. //     Purpose: check a single DITL item for a mouse click in that item.
  374. //
  375. //   returns: true if item clicked, false otherwise.
  376. //----------------------------------------------------------------------------------
  377. Boolean daMouseInItem (DialogPtr d, short i, EventRecord * theEvt)
  378. {
  379.     short            t;
  380.     ControlHandle    h;
  381.     Rect            r;
  382.     Point            mp;
  383.     
  384.     mp = theEvt->where;
  385.     GlobalToLocal(&mp);
  386.  
  387.     GetDItem(d, i, &t, (Handle *)&h, &r);
  388.     if(PtInRect(mp, &r))
  389.         return(true);
  390.     return(false);
  391. }
  392. //----------------------------------------------------------------------------------
  393. //    Function: daEditCursor
  394. //     Purpose: set IBeam cursor if mouse if over the current editText item.
  395. //                typically called from top of dialog filterProc
  396. //   returns: void
  397. //----------------------------------------------------------------------------------
  398. void daEditCursor (DialogPtr d)
  399. {
  400.     short            i,t;
  401.     ControlHandle    h;
  402.     Rect            r;
  403.     Point            mp;
  404.     
  405.     i = ((DialogPeek)d)->editField + 1;
  406.     GetMouse(&mp);
  407.     GetDItem(d, i, &t, (Handle *)&h, &r);
  408.     if(PtInRect(mp, &r))
  409.         SetCursor(*(GetCursor(1)));
  410.     else
  411.         InitCursor();
  412. }
  413. #pragma mark _TextRoutines
  414. //----------------------------------------------------------------------------------
  415. //    Function: daSetIText
  416. //     Purpose: set text into a DITL item.
  417. //                Does nothing if called for non-editText or non-statText item.
  418. //   returns: void.
  419. //----------------------------------------------------------------------------------
  420. void daSetIText (DialogPtr d, short i, Str255 newText)
  421. {
  422.     short            t;
  423.     Handle            h;
  424.     Rect            r;
  425.  
  426.     GetDItem(d, i, &t, &h, &r);
  427.     if(h && (t & statText || t & editText)) {
  428.         SetIText(h, newText);
  429.     }
  430. }
  431. //----------------------------------------------------------------------------------
  432. //    Function: daGetIText
  433. //     Purpose: get the text from a DITL item.
  434. //                Does nothing if called for non-editText or non-statText item.
  435. //   returns: void.
  436. //----------------------------------------------------------------------------------
  437. void daGetIText (DialogPtr d, short i, Str255 s)
  438. {
  439.     short            t;
  440.     Handle            h;
  441.     Rect            r;
  442.  
  443.     s[0] = 0;
  444.     GetDItem(d, i, &t, &h, &r);
  445.     if(h && (t & statText || t & editText)) {
  446.         GetIText(h, s);
  447.     }
  448. }
  449.  
  450. //----------------------------------------------------------------------------------
  451. //    Function: daSetINum
  452. //     Purpose: set a number into a DITL item.
  453. //                Does nothing if called for non-editText or non-statText item.
  454. //   returns: void.
  455. //----------------------------------------------------------------------------------
  456. void daSetINum (DialogPtr d, short i, long newNum)
  457. {
  458.     short            t;
  459.     Handle            h;
  460.     Rect            r;
  461.     Str255            s;
  462.  
  463.     GetDItem(d, i, &t, &h, &r);
  464.     if(h && (t & statText || t & editText)) {
  465.         NumToString(newNum, s);
  466.         SetIText(h, s);
  467.     }
  468. }
  469. //----------------------------------------------------------------------------------
  470. //    Function: daGetINum
  471. //     Purpose: get the contents a DITL item as a number.
  472. //                Does nothing if called for non-editText or non-statText item.
  473. //   returns: contents of text item as a long.
  474. //----------------------------------------------------------------------------------
  475. long daGetINum (DialogPtr d, short i)
  476. {
  477.     short            t;
  478.     Handle            h;
  479.     Rect            r;
  480.     Str255            s;
  481.     long            l=0L;
  482.  
  483.     GetDItem(d, i, &t, &h, &r);
  484.     if(h && (t & statText || t & editText)) {
  485.         GetIText(h, s);
  486.         StringToNum(s, &l);
  487.     }
  488.     return(l);
  489. }
  490. //----------------------------------------------------------------------------------
  491. //    Function: daSelIText
  492. //     Purpose: select the text of a DITL editText item.
  493. //                Does nothing if called for a non-editText item.
  494. //   returns: void.
  495. //----------------------------------------------------------------------------------
  496. void daSelIText(DialogPtr d, short i)
  497. {
  498.     short            t;
  499.     Handle            h;
  500.     Rect            r;
  501.  
  502.     GetDItem(d, i, &t, &h, &r);
  503.     if(h && (t & editText)) {
  504.         SelIText(d, i, 0,32767);
  505.     }
  506.  
  507. }
  508.  
  509. //----------------------------------------------------------------------------------
  510. //    Function: daSetInsert
  511. //     Purpose: set the insertion point for a DITL editText item before character
  512. //                "before".  If "before" is zero, set insertion point at end.
  513. //                Does nothing if called for a non-editText item.
  514. //   returns: void.
  515. //----------------------------------------------------------------------------------
  516. void daSetInsert(DialogPtr d, short i, short before)
  517. {
  518.     short            t;
  519.     Handle            h;
  520.     Rect            r;
  521.  
  522.     GetDItem(d, i, &t, &h, &r);
  523.     if(h && (t & editText)) {
  524.         if(before)
  525.             SelIText(d, i, before-1, before-1);
  526.         else
  527.             SelIText(d, i, 32767,32767);
  528.     }
  529.  
  530. }
  531. #pragma mark _DefaultButton
  532.  
  533. //----------------------------------------------------------------------------------
  534. //    Function: daSetDefItem
  535. //     Purpose: set default item (button) for a dialog and redraw it.
  536. //
  537. //   returns: void.
  538. //----------------------------------------------------------------------------------
  539. void daSetDefItem(DialogPtr d, short defItem)
  540. {
  541.     short            t;
  542.     Handle            h;
  543.     Rect            r;
  544.     PenState        ps;
  545.     long            cQD;
  546.     RGBColor        fg,bg;
  547.     
  548.     GetDItem(d, defItem, &t, &h, &r);
  549.     if(h && (t == 4)) {
  550.         if(((DialogPeek)d)->aDefItem != defItem) {
  551.             GetPenState(&ps);
  552.             cQD = daGestalt(gestaltQuickdrawVersion);
  553.             if(cQD >= gestalt8BitQD) {
  554.                 GetForeColor(&fg);
  555.                 GetBackColor(&bg);
  556.                 RGBForeColor(&bg);
  557.             }
  558.             else
  559.                 PenMode(srcXor);
  560.             daDrawDefault(d);
  561.             if(cQD >= gestalt8BitQD) 
  562.                 RGBForeColor(&fg);
  563.             SetPenState(&ps);
  564.             ((DialogPeek)d)->aDefItem = defItem;
  565.             daDrawDefault(d);
  566.         }
  567.     }
  568. }
  569.  
  570. //----------------------------------------------------------------------------------
  571. //    Function: daDrawDefault
  572. //     Purpose: draws the default button outline.
  573. //                Should be called from filterProc in response to udpdateEvt
  574. //   returns: void.
  575. //----------------------------------------------------------------------------------
  576. void daDrawDefault (DialogPtr d)
  577. {
  578.     short            defItem,t;
  579.     Handle            h;
  580.     Rect            r;
  581.     PenState        ps;
  582.  
  583.     defItem = ((DialogPeek)d)->aDefItem;
  584.     GetDItem(d, defItem, &t, &h, &r);
  585.     if(h && (t == 4)) {
  586.         GetPenState(&ps);
  587.         PenSize(3,3);
  588.         InsetRect(&r, -4,-4);
  589.         FrameRoundRect(&r, 16, 16);
  590.         SetPenState(&ps);
  591.     }
  592.     
  593. }
  594.  
  595. #pragma mark _KeyRoutines
  596.  
  597. //----------------------------------------------------------------------------------
  598. //    Function: daShiftSelect
  599. //     Purpose: handle shift arrow key selection.
  600. //            : call from your filter routine if the shiftkey is down and the
  601. //            : right or left arrow keys are pressed.
  602. //----------------------------------------------------------------------------------
  603. Boolean daShiftSelect(DialogPtr d, char key)
  604. {        
  605.     short start, end;
  606.     
  607.     if(key != _LEFT && key != _RIGHT)
  608.         return false;
  609.         
  610.     start = (**(*(DialogPeek)d).textH).selStart;
  611.     end = (**(*(DialogPeek)d).textH).selEnd;
  612.     if(key == _LEFT)
  613.         start--;
  614.     else
  615.     if(key == _RIGHT)
  616.         end++;
  617.     if(start >= 0)
  618.         TESetSelect(start, end,    (*(DialogPeek)d).textH);
  619.     return true;
  620. }
  621. //----------------------------------------------------------------------------------
  622. //    Function: daOptionDown
  623. //     Purpose: check for optionKey press without having an eventRecord.
  624. //
  625. //   returns: true if option key is pressed.
  626. //----------------------------------------------------------------------------------
  627. Boolean daOptionDown()
  628. {
  629.     KeyMap    theKeys;
  630.  
  631.     GetKeys(theKeys);
  632.     if(BitTst(&theKeys[1],29L))            // option key down
  633.         return(true);
  634.     return(false);
  635. }
  636. //----------------------------------------------------------------------------------
  637. //    Function: daCmdDown
  638. //     Purpose: check for commandKey press without having an eventRecord.
  639. //
  640. //   returns: true if command key is pressed.
  641. //----------------------------------------------------------------------------------
  642. Boolean daCmdDown()
  643. {
  644.     KeyMap    theKeys;
  645.  
  646.     GetKeys(theKeys);
  647.     if(BitTst(&theKeys[1],16L))            // command key down
  648.         return(true);
  649.     return(false);
  650. }
  651. //----------------------------------------------------------------------------------
  652. //    Function: daCntlDown
  653. //     Purpose: check for controlKey press without having an eventRecord.
  654. //
  655. //   returns: true if control key is pressed.
  656. //----------------------------------------------------------------------------------
  657. Boolean daCntlDown()
  658. {
  659.     KeyMap    theKeys;
  660.  
  661.     GetKeys(theKeys);
  662.     if(BitTst(&theKeys[1],28L))            // control key down
  663.         return(true);
  664.     return(false);
  665. }
  666.  
  667. //----------------------------------------------------------------------------------
  668. //    Function: daExitKey
  669. //     Purpose: check for a key press that should exit the dialog.
  670. //                Flashes default button (OK) if RETURN  or ENTER
  671. //                Flashes cancel button if ESCAPE or cmd-period.
  672. //
  673. //   returns: true if dialog should be closed.
  674. //----------------------------------------------------------------------------------
  675. Boolean daExitKey(DialogPtr    d, EventRecord *evt, short *i, short cancelItem)
  676. {
  677.     short            t,defItem;
  678.     char            key;
  679.     Handle            h;
  680.     Rect            r;
  681.     long            ticks;
  682.     Boolean            result = false;
  683.  
  684.     defItem = ((DialogPeek)d)->aDefItem;
  685.     if(!defItem)
  686.         return;
  687.     key = (evt->message & charCodeMask);
  688.     
  689.      if (key == _RETURNKEY || key == _ENTRKEY){
  690.         GetDItem(d, defItem, &t, &h, &r);
  691.          *i = defItem;
  692.         result = true;
  693.     }
  694.     else
  695.      if (key == _ESCAPEKEY || (key == _PERIODKEY && daCmdDown()) ) {
  696.         GetDItem(d, cancelItem, &t, &h, &r);
  697.          *i = cancelItem;
  698.         result = true;
  699.     }
  700.     if(h && result) {
  701.         HiliteControl((ControlHandle)h,true);
  702.         Delay(8L,&ticks);
  703.         HiliteControl((ControlHandle)h,false);
  704.     }
  705.     return(result);
  706. }
  707. //----------------------------------------------------------------------------------
  708. //    Function: daEnterNumber
  709. //     Purpose: restricts editText item to numeric input only.
  710. //
  711. //   returns: false if keystroke should be processed by ModalDialog.
  712. //----------------------------------------------------------------------------------
  713. Boolean daEnterNumber(DialogPtr d, short i, long min, long max, char key)
  714. {
  715.     Str255    s;
  716.     short    len;
  717.     long    num;
  718.     
  719.     if(daTEkey(key) || key == _PLUS || key == _MINUS)
  720.         return(false);
  721.     if(key < _ZERO || key > _NINE) {
  722. #ifdef BEEP
  723.         SysBeep(1);
  724. #endif
  725.         return(true);
  726.     }
  727.     
  728.     DlgCut(d);
  729.     daGetIText(d, i, s);
  730.     len = s[0];
  731.     s[++len] = key;
  732.     s[0] = len;
  733.     StringToNum(s, &num);
  734.     if(num > max) {
  735. #ifdef BEEP
  736.         DlgPaste(d);
  737.         SysBeep(1);
  738. #endif
  739.         return(true);
  740.     }
  741.     return(false);
  742. }
  743. //----------------------------------------------------------------------------------
  744. //    Function: daEnterPassword
  745. //     Purpose: restricts editText item to numeric input only.
  746. //
  747. //   returns: false if keystroke should be processed by ModalDialog.
  748. //----------------------------------------------------------------------------------
  749. Boolean daEnterPassword(DialogPtr d, short i, short len, char key, Str255 pw)
  750. {
  751.     Str255    s;
  752.     short    inx;
  753.     
  754.     if(key == _BACK) {                            // allow backspace to delete
  755.         daSetIText(d, i, "\p");
  756.         daGetIText(d, i, pw);
  757.         return(true);
  758.     }
  759.     if(key == _TAB)                                // let TAB work
  760.         return(false);
  761.         
  762.     if(daTEkey(key) || key == _DELKEY) {        // disallow edit keys
  763. #ifdef BEEP
  764.         SysBeep(1);
  765. #endif
  766.         return(true);
  767.     }
  768.         
  769.     if((**(*(DialogPeek)d).textH).selStart !=    // delete selection
  770.         (**(*(DialogPeek)d).textH).selEnd) {
  771.         TEDelete((*(DialogPeek)d).textH);
  772.         daSetIText(d, i, "\p");
  773.         daGetIText(d, i, pw);
  774.     }
  775.     daGetIText(d, i, s);
  776.     if(s[0] < len) {
  777.         TEKey('Ñ',(*(DialogPeek)d).textH);        // insert bullet
  778.         inx = pw[0];                            // now save real char in
  779.         pw[++inx] = key;                        // the password buffer
  780.         pw[0] = inx;
  781.     }
  782.     else
  783. #ifdef BEEP
  784.         SysBeep(1);
  785. #endif
  786.     return(true);                                // we did it, ModalDialog
  787. }
  788. //----------------------------------------------------------------------------------
  789. //    Function: daLimitText
  790. //     Purpose: restricts editText item to "count" number of characters.
  791. //
  792. //   returns: false if keystroke should be processed by ModalDialog.
  793. //----------------------------------------------------------------------------------
  794. Boolean daLimitText(DialogPtr d, short i, short count, char key)
  795. {
  796.     Str255    s;
  797.     
  798.     if(daTEkey(key))
  799.         return(false);
  800.         
  801.     daGetIText(d, i, s);
  802.     if(s[0] >= count) {
  803. #ifdef BEEP
  804.         SysBeep(1);
  805. #endif
  806.         return(true);
  807.     }
  808.     else
  809.         return(false);
  810. }
  811. //----------------------------------------------------------------------------------
  812. //    Function: daTEkey
  813. //     Purpose: check for "edit" type keystroke.
  814. //
  815. //   returns: true if key is an "edit" key.
  816. //----------------------------------------------------------------------------------
  817. Boolean daTEkey(char key)
  818. {
  819.     if(key == _UP || key == _DOWN || key == _RIGHT || key == _LEFT ||
  820.         key == _PAGEUP || key == _PAGEDOWN || key == _BACK || key == _TAB)
  821.         return(true);
  822.     return(false);
  823. }
  824. //----------------------------------------------------------------------------------
  825. //    Function: daForwardDelete
  826. //     Purpose: process a forward delete key in an editText item.
  827. //
  828. //   returns: void
  829. //----------------------------------------------------------------------------------
  830. Boolean daForwardDel(DialogPtr d, char key)
  831. {
  832.     if(key != _DELKEY)
  833.         return(false);
  834.         
  835.     if((**(*(DialogPeek)d).textH).selStart ==
  836.         (**(*(DialogPeek)d).textH).selEnd) {
  837.         TESetSelect((**(*(DialogPeek)d).textH).selStart,
  838.                     (**(*(DialogPeek)d).textH).selEnd+1,
  839.                     (*(DialogPeek)d).textH);
  840.     }
  841.     TEDelete((*(DialogPeek)d).textH);
  842.     return(true);
  843. }
  844. #pragma mark _DrawRoutines
  845.  
  846. //----------------------------------------------------------------------------------
  847. //    Function: daSetIDraw
  848. //     Purpose: set a userItem drawProc.
  849. //
  850. //   returns: void
  851. //----------------------------------------------------------------------------------
  852. void daSetIDraw (DialogPtr d, short i, ProcPtr drawIt)
  853. {
  854.     short            t;
  855.     Handle            h;
  856.     Rect            r;
  857.  
  858.     GetDItem(d, i, &t, &h, &r);
  859.     if(t == userItem || t == itemDisable) {
  860.         SetDItem(d, i, t, (Handle)drawIt, &r);
  861.     }
  862. }
  863.  
  864. //----------------------------------------------------------------------------------
  865. //    Function: daDrawIFrame
  866. //     Purpose: frame a userItem.
  867. //
  868. //   returns: void
  869. //----------------------------------------------------------------------------------
  870. pascal void daDrawIFrame(DialogPtr d, short i)
  871. {
  872.     short            t;
  873.     Handle            h;
  874.     Rect            r;
  875.  
  876.     GetDItem(d, i, &t, &h, &r);
  877.     FrameRect(&r);
  878. }
  879.  
  880. #pragma mark _ItemDimming
  881. //----------------------------------------------------------------------------------
  882. //    Function: daGetDim
  883. //     Purpose: return state of DITL item.
  884. //
  885. //   returns: true if dimmed (inactive control or disabled item)
  886. //----------------------------------------------------------------------------------
  887. Boolean    daGetDim (DialogPtr d, short i)
  888. {
  889.     short            t;
  890.     Handle            h;
  891.     Rect            r;
  892.  
  893.     GetDItem(d, i, &t, &h, &r);
  894.     if(h) {
  895.         if(t & ctrlItem) {
  896.             return(!(*(ControlHandle)h)->contrlHilite);    
  897.         }
  898.         else
  899.             return(t & itemDisable);
  900.     }
  901. }
  902. //----------------------------------------------------------------------------------
  903. //    Function: daDimItems
  904. //     Purpose: dim a series of DITL items from first to last (must be sequential) 
  905. //                and draws them in gray.
  906. //
  907. //   returns: void
  908. //----------------------------------------------------------------------------------
  909. void daDimItems (DialogPtr d, short first, short last, Boolean dim)
  910. {
  911.     short            inx;
  912.  
  913.     for(inx=first;inx<=last;inx++)
  914.         daDimOne(d, inx, dim);
  915. }
  916. //----------------------------------------------------------------------------------
  917. //    Function: daDimOne
  918. //     Purpose: dim (disable or inactivate) a single item.
  919. //
  920. //   returns: void
  921. //----------------------------------------------------------------------------------
  922. void daDimOne (DialogPtr d, short i, Boolean dim)
  923. {
  924.     short            t;
  925.     ControlHandle    h;
  926.     Rect            r;
  927.  
  928.     GetDItem(d, i, &t, (Handle *)&h, &r);
  929.     if(h) {    
  930.         if(t & ctrlItem) {
  931.             if(dim)
  932.                 HiliteControl(h, DIM);
  933.             else
  934.                 HiliteControl(h, NOTDIM);
  935.         }
  936.         else
  937.         if((t & statText) || (t & editText))
  938.              dimText(d, i, dim);                // in dimText.c
  939.     }
  940. }
  941.  
  942. #pragma mark _UtilityRoutines
  943. //----------------------------------------------------------------------------------
  944. //    Function: daGestalt
  945. //     Purpose: call Gestalt if available.
  946. //
  947. //   returns: Gestalt response
  948. //----------------------------------------------------------------------------------
  949. long daGestalt (OSType selector)
  950. {
  951.     OSErr        err;
  952.     long        gResult;
  953.     
  954.     if(trapAvailable(_Gestalt)) {
  955.         err = Gestalt(selector,&gResult);
  956.         if(err == noErr)
  957.             return(gResult);
  958.     }
  959.     return(0L);
  960. }
  961.  
  962. //----------------------------------------------------------------------------------
  963. //    Function: daHasFeature
  964. //     Purpose: call Gestalt to determine a given feature.
  965. //
  966. //   returns: true if feature present
  967. //----------------------------------------------------------------------------------
  968. Boolean daHasFeature(OSType selector, short bitToCheck)
  969. {    
  970.     OSErr        err;
  971.     long        gResult;
  972.     Boolean        result = false;
  973.     
  974.     if(trapAvailable(_Gestalt)) {
  975.         err = Gestalt(selector,&gResult);
  976.         if(err == noErr) {
  977.             if(BitTst(&gResult, 31-bitToCheck))
  978.                 result = true;
  979.         }
  980.     }
  981.     return(result);
  982. }
  983. //----------------------------------------------------------------------------------
  984. //    Generic routine to see if a given trap is available
  985. //----------------------------------------------------------------------------------
  986. static Boolean    trapAvailable(short theTrap)
  987. {
  988.     TrapType    tType;
  989.     
  990.     tType = getTrapType(theTrap);
  991.     
  992.     if(tType == ToolTrap) {
  993.         theTrap &= 0x07ff;
  994.         if(theTrap >= numToolBoxTraps())
  995.             theTrap = _Unimplemented;
  996.     }
  997.     return(( NGetTrapAddress(theTrap, tType) !=
  998.         NGetTrapAddress(_Unimplemented, ToolTrap) ));
  999. }
  1000.  
  1001. //----------------------------------------------------------------------------------
  1002. //    Needed for "trapAvailable" routine
  1003. //----------------------------------------------------------------------------------
  1004. static TrapType getTrapType(theTrap)
  1005. short    theTrap;
  1006. {
  1007.     if((theTrap &= 0x0800))
  1008.         return(ToolTrap);
  1009.     return(OSTrap);
  1010. }
  1011. //----------------------------------------------------------------------------------
  1012. //    Needed for "trapAvailable" routine
  1013. //----------------------------------------------------------------------------------
  1014. static short numToolBoxTraps()
  1015. {
  1016.     if(NGetTrapAddress(_InitGraf, ToolTrap) ==
  1017.         NGetTrapAddress(0xaa6e, ToolTrap))
  1018.         return(0x0200);
  1019.     return(0x0400);
  1020. }
  1021. //----------------------------------------------------------------------------------
  1022. //    Concatenates pascal strings - adds p2 to p1
  1023. //----------------------------------------------------------------------------------
  1024. extern void pStrCat(p1,p2)
  1025. register char *p1, *p2;
  1026. {
  1027.     register int    i,j,k=0;
  1028.  
  1029.     i = p1[0];
  1030.     j = p2[0];
  1031.     p1[0] = i+j;
  1032.     
  1033.     while (++k<=j) p1[++i] = p2[k];
  1034. }
  1035.  
  1036. //----------------------------------------------------------------------------------
  1037. //    Copies pascal string - source to dest
  1038. //----------------------------------------------------------------------------------
  1039. extern void pStrCopy(char * source, char * dest) 
  1040. {
  1041.     short len;
  1042.     
  1043.     len = *dest++ = *source++;
  1044.     while (--len>=0)
  1045.         *dest++ = *source++;
  1046. }
  1047. extern void centerDialog(DialogPtr d)
  1048. {
  1049.     short        windW,windH;
  1050.     short        rectW,rectH;
  1051.     Rect    wRect;
  1052.     
  1053.     wRect = d->portRect;
  1054.     
  1055.     windW = wRect.right-wRect.left;
  1056.     windH = wRect.bottom-wRect.top;
  1057.     
  1058.     rectW = screenBits.bounds.right - screenBits.bounds.left;
  1059.     rectH = screenBits.bounds.bottom - screenBits.bounds.top;
  1060.     
  1061.     MoveWindow    (d,
  1062.                 screenBits.bounds.left+(rectW-windW)/2,
  1063.                 screenBits.bounds.top+(rectH-windH)/3,
  1064.                 false);
  1065. }