home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Code Resources / cicnButton CDEF 1.0.0 / cicnButton.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-20  |  4.0 KB  |  172 lines  |  [TEXT/R*ch]

  1. /* ----------------------------------------------------------------------
  2.  
  3.     cicnButton CDEF
  4.     version 1.0.0
  5.     20 March 1994
  6.     
  7.     Written by: Paul Celestin
  8.     
  9.     This CDEF displays a cicn whose resource ID is derived from the
  10.     value field of the CNTL. The min, max, and refcon fields are not
  11.     used and should therefore not be set.
  12.     
  13.     940320 - 1.0.0 - initial release
  14.     
  15. ---------------------------------------------------------------------- */
  16.  
  17. # include    <Controls.h>
  18. # include    <QuickDraw.h>
  19. # include    <Resources.h>
  20.  
  21. # define    INVERTED_ICON    1
  22.  
  23. /* ----------------------------------------------------------------------
  24. prototypes
  25. ---------------------------------------------------------------------- */
  26.  
  27. pascal     long        main(short, ControlHandle, short, long);
  28.          void         drawIt(ControlHandle, short);
  29.         long         testIt(ControlHandle, Point);
  30.  
  31. /* ----------------------------------------------------------------------
  32. main - here is where it all began...
  33. ---------------------------------------------------------------------- */
  34. pascal long main
  35.     (
  36.     short            variation,
  37.     ControlHandle    theControl,
  38.     short            message,
  39.     long            param
  40.     )
  41.  
  42. {
  43.     long            returnValue = 0L;
  44.     char            state = HGetState((Handle)theControl);
  45.  
  46.     switch(message)
  47.     {
  48.         case drawCntl:
  49.             drawIt(theControl,variation);
  50.         case testCntl:
  51.             returnValue = testIt(theControl, *(Point *) ¶m);
  52.         case calcCRgns:
  53.             break;
  54.           case initCntl:
  55.               break;
  56.         case dispCntl:
  57.             break;
  58.         case posCntl:
  59.             break;
  60.         case thumbCntl:
  61.             break;
  62.         case dragCntl:
  63.             break;
  64.         case autoTrack:
  65.             break;
  66.         case calcCntlRgn:
  67.             break;
  68.         case calcThumbRgn:
  69.             break;
  70.         default:
  71.             break;
  72.     }
  73.  
  74.     HSetState((Handle)theControl,state);
  75.  
  76.     return(returnValue);                /* tell them what happened */
  77. }
  78.  
  79. /* ----------------------------------------------------------------------
  80. drawIt - here is where we actually draw the control
  81. ---------------------------------------------------------------------- */
  82. static void drawIt
  83.     (
  84.     ControlHandle    control,
  85.     short            variation
  86.     )
  87.  
  88. {
  89.     Rect             myRect;
  90.     short             myICONID;
  91.     CIconHandle        myICON;
  92.     GrafPtr            myPort;
  93.     Pattern            myGray;
  94.     PenState        oldPenState;
  95.     Str255            myTitle;
  96.     int                savedFont,
  97.                     savedSize,
  98.                     savedMode;
  99.     OSErr            myErr;
  100.  
  101.     GetPort(&myPort);                            /* save off the current port */
  102.  
  103.     if (!(*control)->contrlVis)                    /* if not visible, do nothing */
  104.         return;
  105.  
  106.     myICONID = GetCtlValue(control);            /* base ID is stored in the value field */
  107.     
  108.     if ((*control)->contrlHilite == inButton)
  109.         myICONID = myICONID + INVERTED_ICON;    /* invert while tracking */
  110.     
  111.     myRect = (*control)->contrlRect;            /* get the rectangle of the control */
  112.     
  113.     myICON = GetCIcon(myICONID);                /* get the appropriate cicn resource */
  114.     
  115.     if ( myICON == 0L )                            /* make sure the cicn exists */
  116.         return;
  117.     
  118.     EraseRect(&myRect);                            /* erase before drawing */
  119.     PlotCIcon(&myRect,myICON);                    /* draw the cicn */
  120.     
  121.     savedFont = myPort->txFont;                    /* save of values before drawing title */
  122.     savedSize = myPort->txSize;
  123.     savedMode = myPort->txMode;
  124.  
  125.     TextFont(geneva);                            /* change font to Geneva 9 point */
  126.     TextSize(9);
  127.     TextMode(srcOr);
  128.     BlockMove(((*control)->contrlTitle),myTitle,((*control)->contrlTitle)[0] + 1);            
  129.     MoveTo((myRect.right+myRect.left) / 2 - StringWidth(myTitle) / 2,myRect.bottom + 12);
  130.     DrawString(myTitle);                        /* draw the title */
  131.  
  132.     TextFont(savedFont);                        /* restore the values that were saved */
  133.     TextSize(savedSize);
  134.     TextMode(savedMode);
  135.  
  136.     if ((*control)->contrlHilite == 255)        /* gray out the cicn */
  137.     {
  138.         GetPenState(&oldPenState);
  139.         StuffHex(&myGray, "\pAA55AA55AA55AA55");
  140.         PenPat(myGray);
  141.         PenMode(srcBic);
  142.         PaintRect(&myRect);
  143.         SetPenState(&oldPenState);
  144.     }
  145.     
  146.     return;
  147. }
  148.  
  149. /* ----------------------------------------------------------------------
  150. testIt - test for mouse hits within control
  151. ---------------------------------------------------------------------- */
  152. static long testIt
  153.     (
  154.     ControlHandle        control,
  155.     Point                myPoint
  156.     )
  157.  
  158. {
  159.     Rect myRect;
  160.  
  161.     myRect = (*control)->contrlRect;
  162.     
  163.     if    (
  164.         ((*control)->contrlVis != 0) &&
  165.         ((*control)->contrlHilite != 255) &&
  166.         (PtInRect(myPoint,&myRect))
  167.         )
  168.         return(inButton);
  169.     else
  170.         return(0);
  171. }
  172.