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

  1. //-----------------------------------------------------------------------------------
  2. // File        : TogLib.c
  3. // Purpose    : Support routines for "Tog" buttons
  4. //            : These are the key to the "correct" behavior of the buttons.  There are
  5. //            : 3 key routines here - to be called :
  6. //            :        1. To "initialize" a set of "Tog" buttons
  7. //            :        2. In response to a click on one of a set of "Tog" buttons.
  8. //            :        3. In response to a click on anything other than a "Tog" button.
  9. //            :
  10. //            : either include this file in your project, or create a library from it.
  11. //            :
  12. // Author    : Jim Stout
  13. // Date        : November 2, 1991
  14. //-----------------------------------------------------------------------------------
  15. #include "TogLib.h"
  16.  
  17. //-----------------------------------------------------------------------------------
  18. //     Initialize the group of "Tog Buttons" by setting the 'goUP' flag and storing it 
  19. //     in the refCon of the 'first' control.
  20. //     Assumes that the buttons are defined sequentially from 'first' to 'last'.  It
  21. //     really would help if at least ONE is an active control╔
  22. //-----------------------------------------------------------------------------------
  23. void initTogButtons(DialogPtr theDialog, short first, short last)
  24. {
  25.     short            i,cnt=0,selected,t;
  26.     long            goUP = TOGUP;
  27.     Rect            r;
  28.     ControlHandle    h,refHdl;
  29.     
  30.     for(i=first;i<=last;i++) {                        // step through all the controls
  31.         GetDItem(theDialog,i,&t,(Handle *)&h,&r);
  32.         if(i == first)                                // save handle for SetCRefCon later
  33.             refHdl = h;
  34.         if(GetCtlValue(h) == 1) {                    // count the controls that are 'ON'
  35.             cnt++;
  36.             selected = i;
  37.         }
  38.     }
  39.     if(cnt == 0) {                                    // must have at least one control 'ON'
  40.         for(i=first;i<=last;i++) {
  41.             GetDItem(theDialog,i,&t,(Handle *)&h,&r);
  42.             if((*h)->contrlHilite == 0)    {
  43.                 SetCtlValue(h,1);
  44.                 break;
  45.             }
  46.         }
  47.     }
  48.     else
  49.     if(cnt == 1 && selected == first)                // if only the 'first control is 'ON',
  50.         goUP = TOGDOWN;                                // then reverse the 'goUP' flag
  51.     SetCRefCon(refHdl,goUP);                        // save the 'goUP' flag    
  52. }
  53.  
  54. //-----------------------------------------------------------------------------------
  55. //     This routine simply sets the "goUP" flag to go up or left (per Tog's design). 
  56. //     This should be called if the user clicks outside of the group of "Tog Buttons".
  57. //-----------------------------------------------------------------------------------
  58. void resetTogButtons(DialogPtr theDialog, short first)
  59. {
  60.     short            t;
  61.     Rect            r;
  62.     ControlHandle    h;
  63.     
  64.     GetDItem(theDialog,first,&t,(Handle *)&h,&r);
  65.     SetCRefCon(h,TOGUP);                            // save the 'goUP' flag
  66. }
  67.  
  68. //-----------------------------------------------------------------------------------
  69. //     This routine should be called to handle a user click on a "Tog Button", it keeps
  70. //     track of the direction of the next control to turn on and will handle an
  71. //     inactive control.
  72. //-----------------------------------------------------------------------------------
  73. void setTogButtons(DialogPtr theDialog, short itemHit, short first, short last)
  74. {
  75.     short            i,cnt=0,t;
  76.     long            goUP;
  77.     Rect            r;
  78.     ControlHandle    h,refHdl;
  79.     
  80. //-----------------------------------------------------------------------------------
  81. //     CASE 1: count the controls that are 'ON' and if the 'itemHit' control is OFF, 
  82. //             then turn it ON and leave.
  83. //-----------------------------------------------------------------------------------
  84.     for(i=first;i<=last;i++) {                        // step thru all the controls
  85.         GetDItem(theDialog,i,&t,(Handle *)&h,&r);
  86.         if(i == first) {
  87.             goUP = GetCRefCon(h);                    // get the 'goUP' flag
  88.             refHdl = h;                                // save handle to reset the refCon
  89.             if(goUP == 0) {                            // initTog was NOT called !
  90.                 goUP = TOGUP;                        // assume up as the goUP direction
  91.                 SetCRefCon(refHdl,goUP);            // save the 'goUP' flag
  92.             }
  93.         }
  94.         if(GetCtlValue(h) == 1)                     // count controls that are 'ON'
  95.             cnt++;
  96.         else
  97.         if(i == itemHit) {                            // it's 'OFF', turn it 'ON'
  98.             SetCtlValue(h,1);
  99.             goUP = TOGUP;
  100.             SetCRefCon(refHdl,goUP);                // save the 'goUP' flag
  101.             return;                                    // all done            
  102.         }
  103.     }
  104.  
  105. //-----------------------------------------------------------------------------------
  106. //     CASE 2: if more than 1 control is 'ON', it is OK to turn the 'itemHit' 
  107. //     control OFF and leave.
  108. //-----------------------------------------------------------------------------------
  109.  
  110.     if(cnt > 1) {                                    // 'many' are 'ON', ok to turn 'OFF'
  111.         GetDItem(theDialog,itemHit,&t,(Handle *)&h,&r);
  112.         SetCtlValue(h,0);                            // turn it 'OFF'    
  113.         return;                                        // all done            
  114.     }
  115.  
  116. //-----------------------------------------------------------------------------------
  117. //     CASE 3: only ONE control is 'ON', must turn it 'OFF' and turn on the one above or
  118. //             below 'itemHit', based on the setting of the 'goUP' flag.
  119. //-----------------------------------------------------------------------------------
  120.     else {                                            // only ONE button is 'ON'
  121.         if(itemHit == first)                        // set 'goUP' if first or last
  122.             goUP = TOGDOWN;                                
  123.         else
  124.         if(itemHit == last)
  125.             goUP = TOGUP;
  126.         GetDItem(theDialog,itemHit+goUP,&t,(Handle *)&h,&r);
  127.         if((*h)->contrlHilite == 0) {                // if 'goUP' control is active,
  128.             SetCtlValue(h,1);                        // toggle the buttons 
  129.             GetDItem(theDialog,itemHit,&t,(Handle *)&h,&r);
  130.             SetCtlValue(h,0);
  131.         }
  132.  
  133. //-----------------------------------------------------------------------------------
  134. //     CASE 4: Life just got complicated, the control adjacent to 'itemHit' (according to
  135. //             the direction in 'goUP') was INACTIVE !
  136. //-----------------------------------------------------------------------------------
  137.         else {
  138.             if(itemHit+goUP == first)                // reverse the goUP flag if the
  139.                 goUP = TOGDOWN;                        // inactive ctrl was first or last
  140.             else
  141.             if(itemHit+goUP == last)
  142.                 goUP = TOGUP;
  143.                 
  144. //-----------------------------------------------------------------------------------
  145. //     Now find the first active control in direction 'goUP', turn it ON and turn OFF
  146. //     the one the user clicked.
  147. //-----------------------------------------------------------------------------------
  148.  
  149.             if(goUP == TOGDOWN) {
  150.                 for(i=itemHit+1;i<=last;i++) {
  151.                     GetDItem(theDialog,i,&t,(Handle *)&h,&r);
  152.                     if((*h)->contrlHilite == 0)        // found an active control !
  153.                         break;
  154.                 }
  155.             }
  156.             else {
  157.                 for(i=itemHit-1;i>=first;i--) {
  158.                     GetDItem(theDialog,i,&t,(Handle *)&h,&r);
  159.                     if((*h)->contrlHilite == 0)     // found an active control !
  160.                         break;
  161.                 }
  162.             }
  163.             SetCtlValue(h,1);                        // turn new one 'ON'
  164.             GetDItem(theDialog,itemHit,&t,(Handle *)&h,&r);
  165.             SetCtlValue(h,0);                        // turn old one 'OFF'
  166.         }
  167.     }
  168.     SetCRefCon(refHdl,goUP);                        // save the 'goUP' flag
  169. }
  170.  
  171. //-----------------------------------------------------------------------------------
  172. //     Simple but handy routine to process a group of radioButtons with one function call.
  173. //     Assumes that the buttons are defined sequentially from 'first' to 'last'.
  174. //-----------------------------------------------------------------------------------
  175. void setRadioButtons(DialogPtr theDialog, short itemHit, short first, short last)
  176. {
  177.     short            i,t;
  178.     Rect            r;
  179.     ControlHandle    h;
  180.     
  181.     for(i=first;i<=last;i++) {                        // step through the list of radio buttons
  182.         GetDItem(theDialog,i,&t,(Handle *)&h,&r);
  183.         if(i == itemHit)
  184.             SetCtlValue(h,1);                        // turn it 'ON'
  185.         else
  186.             SetCtlValue(h,0);                        // turn it 'OFF'
  187.     }
  188. }
  189.