home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-12-12 | 7.1 KB | 189 lines | [TEXT/KAHL] |
- //-----------------------------------------------------------------------------------
- // File : TogLib.c
- // Purpose : Support routines for "Tog" buttons
- // : These are the key to the "correct" behavior of the buttons. There are
- // : 3 key routines here - to be called :
- // : 1. To "initialize" a set of "Tog" buttons
- // : 2. In response to a click on one of a set of "Tog" buttons.
- // : 3. In response to a click on anything other than a "Tog" button.
- // :
- // : either include this file in your project, or create a library from it.
- // :
- // Author : Jim Stout
- // Date : November 2, 1991
- //-----------------------------------------------------------------------------------
- #include "TogLib.h"
-
- //-----------------------------------------------------------------------------------
- // Initialize the group of "Tog Buttons" by setting the 'goUP' flag and storing it
- // in the refCon of the 'first' control.
- // Assumes that the buttons are defined sequentially from 'first' to 'last'. It
- // really would help if at least ONE is an active control╔
- //-----------------------------------------------------------------------------------
- void initTogButtons(DialogPtr theDialog, short first, short last)
- {
- short i,cnt=0,selected,t;
- long goUP = TOGUP;
- Rect r;
- ControlHandle h,refHdl;
-
- for(i=first;i<=last;i++) { // step through all the controls
- GetDItem(theDialog,i,&t,(Handle *)&h,&r);
- if(i == first) // save handle for SetCRefCon later
- refHdl = h;
- if(GetCtlValue(h) == 1) { // count the controls that are 'ON'
- cnt++;
- selected = i;
- }
- }
- if(cnt == 0) { // must have at least one control 'ON'
- for(i=first;i<=last;i++) {
- GetDItem(theDialog,i,&t,(Handle *)&h,&r);
- if((*h)->contrlHilite == 0) {
- SetCtlValue(h,1);
- break;
- }
- }
- }
- else
- if(cnt == 1 && selected == first) // if only the 'first control is 'ON',
- goUP = TOGDOWN; // then reverse the 'goUP' flag
- SetCRefCon(refHdl,goUP); // save the 'goUP' flag
- }
-
- //-----------------------------------------------------------------------------------
- // This routine simply sets the "goUP" flag to go up or left (per Tog's design).
- // This should be called if the user clicks outside of the group of "Tog Buttons".
- //-----------------------------------------------------------------------------------
- void resetTogButtons(DialogPtr theDialog, short first)
- {
- short t;
- Rect r;
- ControlHandle h;
-
- GetDItem(theDialog,first,&t,(Handle *)&h,&r);
- SetCRefCon(h,TOGUP); // save the 'goUP' flag
- }
-
- //-----------------------------------------------------------------------------------
- // This routine should be called to handle a user click on a "Tog Button", it keeps
- // track of the direction of the next control to turn on and will handle an
- // inactive control.
- //-----------------------------------------------------------------------------------
- void setTogButtons(DialogPtr theDialog, short itemHit, short first, short last)
- {
- short i,cnt=0,t;
- long goUP;
- Rect r;
- ControlHandle h,refHdl;
-
- //-----------------------------------------------------------------------------------
- // CASE 1: count the controls that are 'ON' and if the 'itemHit' control is OFF,
- // then turn it ON and leave.
- //-----------------------------------------------------------------------------------
- for(i=first;i<=last;i++) { // step thru all the controls
- GetDItem(theDialog,i,&t,(Handle *)&h,&r);
- if(i == first) {
- goUP = GetCRefCon(h); // get the 'goUP' flag
- refHdl = h; // save handle to reset the refCon
- if(goUP == 0) { // initTog was NOT called !
- goUP = TOGUP; // assume up as the goUP direction
- SetCRefCon(refHdl,goUP); // save the 'goUP' flag
- }
- }
- if(GetCtlValue(h) == 1) // count controls that are 'ON'
- cnt++;
- else
- if(i == itemHit) { // it's 'OFF', turn it 'ON'
- SetCtlValue(h,1);
- goUP = TOGUP;
- SetCRefCon(refHdl,goUP); // save the 'goUP' flag
- return; // all done
- }
- }
-
- //-----------------------------------------------------------------------------------
- // CASE 2: if more than 1 control is 'ON', it is OK to turn the 'itemHit'
- // control OFF and leave.
- //-----------------------------------------------------------------------------------
-
- if(cnt > 1) { // 'many' are 'ON', ok to turn 'OFF'
- GetDItem(theDialog,itemHit,&t,(Handle *)&h,&r);
- SetCtlValue(h,0); // turn it 'OFF'
- return; // all done
- }
-
- //-----------------------------------------------------------------------------------
- // CASE 3: only ONE control is 'ON', must turn it 'OFF' and turn on the one above or
- // below 'itemHit', based on the setting of the 'goUP' flag.
- //-----------------------------------------------------------------------------------
- else { // only ONE button is 'ON'
- if(itemHit == first) // set 'goUP' if first or last
- goUP = TOGDOWN;
- else
- if(itemHit == last)
- goUP = TOGUP;
- GetDItem(theDialog,itemHit+goUP,&t,(Handle *)&h,&r);
- if((*h)->contrlHilite == 0) { // if 'goUP' control is active,
- SetCtlValue(h,1); // toggle the buttons
- GetDItem(theDialog,itemHit,&t,(Handle *)&h,&r);
- SetCtlValue(h,0);
- }
-
- //-----------------------------------------------------------------------------------
- // CASE 4: Life just got complicated, the control adjacent to 'itemHit' (according to
- // the direction in 'goUP') was INACTIVE !
- //-----------------------------------------------------------------------------------
- else {
- if(itemHit+goUP == first) // reverse the goUP flag if the
- goUP = TOGDOWN; // inactive ctrl was first or last
- else
- if(itemHit+goUP == last)
- goUP = TOGUP;
-
- //-----------------------------------------------------------------------------------
- // Now find the first active control in direction 'goUP', turn it ON and turn OFF
- // the one the user clicked.
- //-----------------------------------------------------------------------------------
-
- if(goUP == TOGDOWN) {
- for(i=itemHit+1;i<=last;i++) {
- GetDItem(theDialog,i,&t,(Handle *)&h,&r);
- if((*h)->contrlHilite == 0) // found an active control !
- break;
- }
- }
- else {
- for(i=itemHit-1;i>=first;i--) {
- GetDItem(theDialog,i,&t,(Handle *)&h,&r);
- if((*h)->contrlHilite == 0) // found an active control !
- break;
- }
- }
- SetCtlValue(h,1); // turn new one 'ON'
- GetDItem(theDialog,itemHit,&t,(Handle *)&h,&r);
- SetCtlValue(h,0); // turn old one 'OFF'
- }
- }
- SetCRefCon(refHdl,goUP); // save the 'goUP' flag
- }
-
- //-----------------------------------------------------------------------------------
- // Simple but handy routine to process a group of radioButtons with one function call.
- // Assumes that the buttons are defined sequentially from 'first' to 'last'.
- //-----------------------------------------------------------------------------------
- void setRadioButtons(DialogPtr theDialog, short itemHit, short first, short last)
- {
- short i,t;
- Rect r;
- ControlHandle h;
-
- for(i=first;i<=last;i++) { // step through the list of radio buttons
- GetDItem(theDialog,i,&t,(Handle *)&h,&r);
- if(i == itemHit)
- SetCtlValue(h,1); // turn it 'ON'
- else
- SetCtlValue(h,0); // turn it 'OFF'
- }
- }
-