home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / Level 1 Extensions 29Sep94 / RadioButton.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  8.0 KB  |  300 lines  |  [TEXT/KAHL]

  1. /* RadioButton.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    System Dependency Library for Building Portable Software               */
  5. /*    Macintosh Version                                                      */
  6. /*    Written by Thomas R. Lawrence, 1993 - 1994.                            */
  7. /*                                                                           */
  8. /*    This file is Public Domain; it may be used for any purpose whatsoever  */
  9. /*    without restriction.                                                   */
  10. /*                                                                           */
  11. /*    This package is distributed in the hope that it will be useful,        */
  12. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  13. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   */
  14. /*                                                                           */
  15. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  16. /*                                                                           */
  17. /*****************************************************************************/
  18.  
  19. #include "MiscInfo.h"
  20. #include "Audit.h"
  21. #include "Debug.h"
  22. #include "Definitions.h"
  23.  
  24. #include "RadioButton.h"
  25. #include "Screen.h"
  26. #include "EventLoop.h"
  27. #include "Memory.h"
  28. #include "DataMunging.h"
  29.  
  30.  
  31. struct RadioButtonRec
  32.     {
  33.         WinType*        Window;
  34.         char*                Name;
  35.         OrdType            X;
  36.         OrdType            Y;
  37.         OrdType            Width;
  38.         OrdType            Height;
  39.         MyBoolean        State;
  40.     };
  41.  
  42.  
  43. static unsigned char        oNormalUnselected[] =
  44.     {
  45.         0x1F,0x80,0x60,0x60,0x40,0x20,0x80,0x10,0x80,0x10,0x80,0x10,
  46.         0x80,0x10,0x80,0x10,0x80,0x10,0x40,0x20,0x60,0x60,0x1F,0x80
  47.     };
  48.  
  49. static unsigned char        oNormalSelected[] =
  50.     {
  51.         0x1F,0x80,0x60,0x60,0x40,0x20,0x8F,0x10,0x9F,0x90,0x9F,0x90,
  52.         0x9F,0x90,0x9F,0x90,0x8F,0x10,0x40,0x20,0x60,0x60,0x1F,0x80
  53.     };
  54.  
  55. static unsigned char        oMouseDownUnselected[] =
  56.     {
  57.         0x1F,0x80,0x7F,0xE0,0x60,0x60,0xC0,0x30,0xC0,0x30,0xC0,0x30,
  58.         0xC0,0x30,0xC0,0x30,0xC0,0x30,0x60,0x60,0x7F,0xE0,0x1F,0x80
  59.     };
  60.  
  61. static unsigned char        oMouseDownSelected[] =
  62.     {
  63.         0x1F,0x80,0x7F,0xE0,0x60,0x60,0xCF,0x30,0xDF,0xB0,0xDF,0xB0,
  64.         0xDF,0xB0,0xDF,0xB0,0xCF,0x30,0x60,0x60,0x7F,0xE0,0x1F,0x80
  65.     };
  66.  
  67. static long                        BitmapReferenceCount = 0;
  68.  
  69. static Bitmap*                NormalUnselected;
  70. static Bitmap*                NormalSelected;
  71. static Bitmap*                MouseDownUnselected;
  72. static Bitmap*                MouseDownSelected;
  73.  
  74.  
  75. /* allocate a new radio button.  Name is null terminated */
  76. RadioButtonRec*            NewRadioButton(WinType* Window, char* Name,
  77.                                             OrdType X, OrdType Y, OrdType Width, OrdType Height)
  78.     {
  79.         RadioButtonRec*            TheButton;
  80.  
  81.         if (BitmapReferenceCount == 0)
  82.             {
  83.                 NormalUnselected = MakeBitmap(oNormalUnselected,12,12,2);
  84.                 if (NormalUnselected == NIL)
  85.                     {
  86.                      FailurePoint1:
  87.                         return NIL;
  88.                     }
  89.                 NormalSelected = MakeBitmap(oNormalSelected,12,12,2);
  90.                 if (NormalSelected == NIL)
  91.                     {
  92.                      FailurePoint2:
  93.                         DisposeBitmap(NormalUnselected);
  94.                         goto FailurePoint1;
  95.                     }
  96.                 MouseDownUnselected = MakeBitmap(oMouseDownUnselected,12,12,2);
  97.                 if (MouseDownUnselected == NIL)
  98.                     {
  99.                      FailurePoint3:
  100.                         DisposeBitmap(NormalSelected);
  101.                         goto FailurePoint2;
  102.                     }
  103.                 MouseDownSelected = MakeBitmap(oMouseDownSelected,12,12,2);
  104.                 if (MouseDownSelected == NIL)
  105.                     {
  106.                      FailurePoint4:
  107.                         DisposeBitmap(MouseDownUnselected);
  108.                         goto FailurePoint3;
  109.                     }
  110.             }
  111.         BitmapReferenceCount += 1;
  112.         TheButton = (RadioButtonRec*)AllocPtrCanFail(sizeof(RadioButtonRec),"RadioButton");
  113.         if (TheButton == NIL)
  114.             {
  115.              OtherFailurePoint:
  116.                 BitmapReferenceCount -= 1;
  117.                 if (BitmapReferenceCount == 0)
  118.                     {
  119.                         DisposeBitmap(MouseDownSelected);
  120.                         goto FailurePoint4;
  121.                     }
  122.                  else
  123.                     {
  124.                         return NIL;
  125.                     }
  126.             }
  127.         TheButton->Window = Window;
  128.         TheButton->Name = StringToBlockCopy(Name);
  129.         if (TheButton->Name == NIL)
  130.             {
  131.                 ReleasePtr((char*)TheButton);
  132.                 goto OtherFailurePoint;
  133.             }
  134.         TheButton->X = X;
  135.         TheButton->Y = Y;
  136.         TheButton->Width = Width;
  137.         TheButton->Height = Height;
  138.         TheButton->State = False;
  139.         return TheButton;
  140.     }
  141.  
  142.  
  143. /* dispose of radio button and any internal data structures. */
  144. void                                DisposeRadioButton(RadioButtonRec* TheButton)
  145.     {
  146.         BitmapReferenceCount -= 1;
  147.         ERROR(BitmapReferenceCount < 0,PRERR(ForceAbort,
  148.             "DisposeRadioButton:  bitmap reference count is negative"));
  149.         if (BitmapReferenceCount == 0)
  150.             {
  151.                 DisposeBitmap(NormalUnselected);
  152.                 DisposeBitmap(NormalSelected);
  153.                 DisposeBitmap(MouseDownUnselected);
  154.                 DisposeBitmap(MouseDownSelected);
  155.             }
  156.         ReleasePtr(TheButton->Name);
  157.         ReleasePtr((char*)TheButton);
  158.     }
  159.  
  160.  
  161. /* find out where the radio button is located */
  162. OrdType                            GetRadioButtonXLoc(RadioButtonRec* TheButton)
  163.     {
  164.         CheckPtrExistence(TheButton);
  165.         return TheButton->X;
  166.     }
  167.  
  168.  
  169. /* find out where the radio button is located */
  170. OrdType                            GetRadioButtonYLoc(RadioButtonRec* TheButton)
  171.     {
  172.         CheckPtrExistence(TheButton);
  173.         return TheButton->Y;
  174.     }
  175.  
  176.  
  177. /* find out where the radio button is located */
  178. OrdType                            GetRadioButtonWidth(RadioButtonRec* TheButton)
  179.     {
  180.         CheckPtrExistence(TheButton);
  181.         return TheButton->Width;
  182.     }
  183.  
  184.  
  185. /* find out where the radio button is located */
  186. OrdType                            GetRadioButtonHeight(RadioButtonRec* TheButton)
  187.     {
  188.         CheckPtrExistence(TheButton);
  189.         return TheButton->Height;
  190.     }
  191.  
  192.  
  193. /* change the location of the radio button */
  194. void                                SetRadioButtonLocation(RadioButtonRec* TheButton,
  195.                                             OrdType X, OrdType Y, OrdType Width, OrdType Height)
  196.     {
  197.         CheckPtrExistence(TheButton);
  198.         TheButton->X = X;
  199.         TheButton->Y = Y;
  200.         TheButton->Width = Width;
  201.         TheButton->Height = Height;
  202.     }
  203.  
  204.  
  205. /* internal routine for drawing button */
  206. static void                    InternalRedrawRadio(RadioButtonRec* TheButton, MyBoolean Hilited)
  207.     {
  208.         OrdType            YStart;
  209.  
  210.         CheckPtrExistence(TheButton);
  211.         SetClipRect(TheButton->Window,TheButton->X,TheButton->Y,
  212.             TheButton->Width,TheButton->Height);
  213.         YStart = (TheButton->Height - 12) / 2 + TheButton->Y;
  214.         if (TheButton->State)
  215.             {
  216.                 if (Hilited)
  217.                     {
  218.                         DrawBitmap(TheButton->Window,TheButton->X,YStart,MouseDownSelected);
  219.                     }
  220.                  else
  221.                     {
  222.                         DrawBitmap(TheButton->Window,TheButton->X,YStart,NormalSelected);
  223.                     }
  224.             }
  225.          else
  226.             {
  227.                 if (Hilited)
  228.                     {
  229.                         DrawBitmap(TheButton->Window,TheButton->X,YStart,MouseDownUnselected);
  230.                     }
  231.                  else
  232.                     {
  233.                         DrawBitmap(TheButton->Window,TheButton->X,YStart,NormalUnselected);
  234.                     }
  235.             }
  236.         DrawTextLine(TheButton->Window,GetScreenFont(),9,TheButton->Name,
  237.             PtrSize(TheButton->Name),TheButton->X + 12 + 6,YStart,ePlain);
  238.     }
  239.  
  240.  
  241. /* do a full redraw of the button */
  242. void                                RedrawRadioButton(RadioButtonRec* TheButton)
  243.     {
  244.         InternalRedrawRadio(TheButton,False);
  245.     }
  246.  
  247.  
  248. /* handle a mouse down in the button.  returns True if the state changed. */
  249. MyBoolean                        RadioButtonMouseDown(RadioButtonRec* TheButton, OrdType X, OrdType Y)
  250.     {
  251.         MyBoolean                Inside;
  252.         MyBoolean                OldInside;
  253.  
  254.         OldInside = False;
  255.         do
  256.             {
  257.                 Inside = RadioButtonHitTest(TheButton,X,Y);
  258.                 if (OldInside != Inside)
  259.                     {
  260.                         InternalRedrawRadio(TheButton,Inside);
  261.                         OldInside = Inside;
  262.                     }
  263.             } while (GetAnEvent(&X,&Y,NIL,NIL,NIL,NIL) != eMouseUp);
  264.         if (Inside && !TheButton->State)
  265.             {
  266.                 TheButton->State = True;
  267.             }
  268.          else
  269.             {
  270.                 Inside = False; /* radio buttons do NOT toggle */
  271.             }
  272.         InternalRedrawRadio(TheButton,False);
  273.         return Inside;
  274.     }
  275.  
  276.  
  277. /* force the state of the button to be a certain value */
  278. void                                SetRadioButtonState(RadioButtonRec* TheButton, MyBoolean TheState)
  279.     {
  280.         CheckPtrExistence(TheButton);
  281.         TheButton->State = TheState;
  282.         RedrawRadioButton(TheButton);
  283.     }
  284.  
  285.  
  286. /* get the state of the button */
  287. MyBoolean                        GetRadioButtonState(RadioButtonRec* TheButton)
  288.     {
  289.         CheckPtrExistence(TheButton);
  290.         return TheButton->State;
  291.     }
  292.  
  293.  
  294. /* see if the location is in the radio button's box */
  295. MyBoolean                        RadioButtonHitTest(RadioButtonRec* TheButton, OrdType X, OrdType Y)
  296.     {
  297.         return (X >= TheButton->X) && (Y >= TheButton->Y)
  298.             && (X < TheButton->X + TheButton->Width) && (Y < TheButton->Y + TheButton->Height);
  299.     }
  300.