home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Code Resources / SliderCDEF 1.0 / SliderCDEF Source ƒ / SliderDrawControl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-10  |  8.8 KB  |  317 lines  |  [TEXT/KAHL]

  1. #include "SliderCDEF.h"
  2.  
  3. extern Boolean gAnimating;
  4. extern VBLRec gVBLRec;
  5.  
  6. // SliderDrawControl
  7. //
  8. // The main drawing routine for the CDEF; called in respone to the drawCtrl message
  9. // if the control is visible.
  10. void SliderDrawControl( short varCode, ControlHandle theControl, long param )
  11. {
  12.     Rect            theRect;
  13.     SliderColors    colors;
  14.     short        width;
  15.     short        height;
  16.     PenState        savedPenState;
  17.     RgnHandle        savedClip;
  18.     
  19.     // Save the pen state to be restored when drawing is finished,
  20.     // and reset the pen to its default state.
  21.     GetPenState( &savedPenState );
  22.     PenNormal();
  23.     
  24.     // Save the clipping region.
  25.     savedClip = NewRgn();
  26.     GetClip( savedClip );
  27.     
  28.     // Lock and dereference the control handle.
  29.     HLock( (Handle) theControl );
  30.     theRect = (**theControl).contrlRect;
  31.     HUnlock( (Handle) theControl );
  32.  
  33.     // Calculate the width and height of the control.
  34.     width = theRect.right - theRect.left;
  35.     height = theRect.bottom - theRect.top;
  36.     
  37.     // Return if the control's rectangle is invalid.
  38.     if ( width < 0 || height < 0 ) {
  39.         return;
  40.     }
  41.     
  42.     // Draw the control in the appropriate state.
  43.     if ( (**theControl).contrlHilite == kInactive ) {
  44.         SliderDrawControlInactive( varCode, theControl, param );
  45.     }
  46.     
  47.     // Get the colors for the control.
  48.     SliderGetControlColors( theControl, &colors );
  49.  
  50.     // Draw the control.
  51.     SliderBlitControl( theControl, &colors );
  52.  
  53.     // Draw the control value.
  54.     SliderDrawControlValue( theControl, &colors );
  55.     
  56.     // Restore the clipping region.
  57.     SetClip( savedClip );
  58.     DisposeRgn( savedClip );
  59.     
  60.     // Restore the pen state.
  61.     SetPenState( &savedPenState );
  62. }
  63.  
  64.  
  65.  
  66.  
  67. // SliderDrawControlTrack
  68. //
  69. // Draws the track for the slider.
  70. void SliderDrawControlTrack( ControlHandle theControl, SliderColors *colors )
  71. {
  72.     Rect        trackRect;
  73.     RGBColor    savedForeColor;
  74.  
  75.     // Save the foreground color.
  76.     GetForeColor( &savedForeColor );
  77.     
  78.     // Get a copy of the track rect (GWorld coordinates).
  79.     trackRect = (**((SliderDataHnd)(**theControl).contrlData)).sliderTrackRect;
  80.  
  81.     // Fill in the track.
  82.     RGBForeColor( &colors->darkColor );
  83.     PaintRect( &trackRect );
  84.     
  85.     // Outline the top and left sides of the track.
  86.     RGBForeColor( &colors->black );
  87.     MoveTo( trackRect.left, trackRect.bottom );
  88.     LineTo( trackRect.left, trackRect.top );
  89.     LineTo( trackRect.right, trackRect.top );
  90.     
  91.     // Outline the bottom and right sides of the track.
  92.     RGBForeColor( &colors->white );
  93.     LineTo( trackRect.right, trackRect.bottom );
  94.     LineTo( trackRect.left, trackRect.bottom );
  95.     
  96.     // Restore the foreground color.
  97.     RGBForeColor( &savedForeColor );
  98. }
  99.  
  100.  
  101.  
  102.  
  103. // SliderDrawControlThumb
  104. //
  105. // Draws the thumb for the slider.
  106. void SliderDrawControlThumb( ControlHandle theControl, SliderColors *colors )
  107. {
  108.     short    thumbCenter;
  109.     short    thumbWidth;
  110.     Rect        thumbRect;
  111.     RGBColor    savedForeColor;
  112.     
  113.     // Save the foreground color.
  114.     GetForeColor( &savedForeColor );
  115.  
  116.     // Get a copy of the thumb rect (GWorld coordinates).
  117.     thumbRect = (**((SliderDataHnd)(**theControl).contrlData)).sliderThumbRect;
  118.  
  119.     // Calculate the thumb width and center.
  120.     thumbWidth = (thumbRect.right - thumbRect.left);
  121.     thumbCenter = thumbRect.left + thumbWidth/2 - 1;
  122.  
  123.     // Fill in the thumb rect.    
  124.     RGBForeColor( &colors->lightColor );
  125.     PaintRect( &thumbRect );
  126.     
  127.     // Outline the top and left sides of the thumb.
  128.     RGBForeColor( &colors->black );
  129.     MoveTo( thumbRect.left, thumbRect.bottom );
  130.     LineTo( thumbRect.right, thumbRect.bottom );
  131.     LineTo( thumbRect.right, thumbRect.top );
  132.  
  133.     // Outline the bottom, right, and center line of the thumb.
  134.     RGBForeColor( &colors->darkGray );
  135.     MoveTo( thumbRect.left, thumbRect.top );
  136.     LineTo( thumbRect.left, thumbRect.bottom-1 );
  137.     LineTo( thumbRect.right-1, thumbRect.bottom-1 );
  138.     LineTo( thumbRect.right-1, thumbRect.top );
  139.     MoveTo( thumbCenter, thumbRect.bottom-1 );
  140.     LineTo( thumbCenter, thumbRect.top );
  141.     
  142.     // Add some highlights.
  143.     RGBForeColor( &colors->white );
  144.     MoveTo( thumbRect.left+1, thumbRect.bottom-2 );
  145.     LineTo( thumbRect.left+1, thumbRect.top );
  146.     LineTo( thumbCenter-1, thumbRect.top );
  147.     MoveTo( thumbCenter+1, thumbRect.bottom-2 );
  148.     LineTo( thumbCenter+1, thumbRect.top );
  149.     LineTo( thumbRect.right-2, thumbRect.top );
  150.  
  151.     // Restore the foreground color.
  152.     RGBForeColor( &savedForeColor );
  153. }
  154.  
  155.  
  156.  
  157.  
  158. // SliderDrawControlValue
  159. //
  160. // Draws the control value centered over the thumb just above or to the left of the
  161. // slider's rectangle. Note: drawing occurs outside the control's rectangle!
  162. void SliderDrawControlValue( ControlHandle theControl, SliderColors *colors )
  163. {
  164.     long        val;
  165.     short    stringWidth;
  166.     short    stringHalfWidth;
  167.     short    texth, textv;
  168.     short    thumbCenter;
  169.     Rect        thumbRect;
  170.     Rect        textRect;
  171.     FontInfo    fInfo;
  172.     Str255    valueString;
  173.     RGBColor    savedForeColor;
  174.     
  175.     // Save the foreground color.
  176.     GetForeColor( &savedForeColor );
  177.  
  178.     // Get the control's value.
  179.     val = (long) (**theControl).contrlValue;
  180.     
  181.     // Turn the value into a string.
  182.     NumToString( val , valueString );
  183.     
  184.     // Calculate the string width and half width.
  185.     stringWidth = StringWidth( valueString );
  186.     stringHalfWidth = stringWidth / 2;
  187.     
  188.     // Get the thumb rectangle (window coordinates) and
  189.     // calculate the thumb center.
  190.     thumbRect = CalcThumbRect( theControl );
  191.     thumbCenter = thumbRect.left + (thumbRect.right - thumbRect.left)/2;
  192.     
  193.     // Calculate the horizontal position of the text.
  194.     texth = thumbCenter - stringHalfWidth;
  195.     
  196.     // Set the text rectangle to the control's rectangle.
  197.     textRect = (**theControl).contrlRect;
  198.     
  199.     // Calculate the vertical position of the text.
  200.     textv = textRect.bottom = (**theControl).contrlRect.top - 2;
  201.     
  202.     // Set the text attributes.
  203.     TextFont( geneva );
  204.     TextSize( 9 );
  205.     TextFace( 0 );
  206.     
  207.     // Calculate the top of the text rectangle.
  208.     GetFontInfo( &fInfo );
  209.     textRect.top = textRect.bottom - ( fInfo.ascent + fInfo.descent );
  210.     
  211.     // Erase the text rect.
  212.     EraseRect( &textRect );
  213.     
  214.     // Set the foreground color to black and draw the text.
  215.     RGBForeColor( &colors->black );
  216.     MoveTo( texth, textv );
  217.     DrawString( valueString );
  218.  
  219.     // Restore the foreground color.
  220.     RGBForeColor( &savedForeColor );
  221. }
  222.  
  223.  
  224.  
  225.  
  226. // DrawSliderControlInactive
  227. //
  228. // Draws the control in its disabled state.
  229. void SliderDrawControlInactive( short varCode, ControlHandle theControl, long param )
  230. {
  231.     // Nothing here, yet...
  232. }
  233.  
  234.  
  235.  
  236. // SliderBlitControl
  237. //
  238. // Copy the slider to the window.
  239. void SliderBlitControl( ControlHandle theControl, SliderColors *colors )
  240. {
  241.     SliderDataHnd    sliderDataHnd;
  242.     SliderDataPtr    sliderData;
  243.     Rect            thumbRect;
  244.     short        temp;
  245.     CWindowPtr    theWindow;    
  246.     RGBColor        savedForeColor, savedBackColor;
  247.  
  248.     // Lock the control handle, and get the slider's data.
  249.     HLock( (Handle) theControl );
  250.     sliderDataHnd = (SliderDataHnd) (**theControl).contrlData;
  251.     HLock( (Handle) sliderDataHnd );
  252.     sliderData = *sliderDataHnd;
  253.  
  254.     // Get the control's parent window.
  255.     theWindow = (CWindowPtr) (**theControl).contrlOwner;
  256.     SetPort( (GrafPtr) theWindow );
  257.  
  258.     // Calculate the thumb's rectangle (window coordinates).
  259.     thumbRect = CalcThumbRect( theControl );
  260.     
  261.     // Lock down the pixels that we are drawing to.
  262.     LockPixels ( sliderData->sliderPort->portPixMap );
  263.     
  264.     // Save the foreground and background colors.
  265.     GetForeColor( &savedForeColor );
  266.     GetBackColor( &savedBackColor );
  267.     
  268.     // Set the foreground and background colors to 
  269.     // black and white, respectively, so that CopyBits
  270.     // will do the right thing.
  271.     RGBForeColor( &colors->black );
  272.     RGBBackColor( &colors->white );
  273.  
  274.     // Copy the track to the work area.
  275.     CopyBits ( (BitMap *) (*(sliderData->sliderPort->portPixMap)),
  276.         (BitMap *) (*(sliderData->sliderPort->portPixMap)),
  277.         &sliderData->sliderTrackRect, &sliderData->sliderRect, srcCopy, nil );
  278.     
  279.     // Put the thumb rect into GWorld coordinates.
  280.     // Move the thumb rect up to the top.
  281.     OffsetRect( &thumbRect, 0, -(thumbRect.top-1) );
  282.     // Get the offset of the thumb in the track (GWorld coordinates).
  283.     temp = sliderData->sliderRect.left + (thumbRect.left - (**theControl).contrlRect.left);
  284.     thumbRect.right = temp + (thumbRect.right-thumbRect.left);
  285.     thumbRect.left = temp;
  286.     // Copy the thumb to the work area.
  287.     CopyBits ( (BitMap *) (*(sliderData->sliderPort->portPixMap)),
  288.         (BitMap *) (*(sliderData->sliderPort->portPixMap)),
  289.         &sliderData->sliderThumbRect, &thumbRect, srcCopy, nil );
  290.  
  291.     // If animating, wait for the VBL task to tell us
  292.     // it's time to go, otherwise, just go.
  293.     if ( gAnimating == true ) {  
  294.         if ( gVBLRec.xDoTask == true ) {
  295.              gVBLRec.xDoTask = false;
  296.             // Blit the track.
  297.             CopyBits ( (BitMap *) (*(sliderData->sliderPort->portPixMap)),
  298.                 &((GrafPtr)theWindow)->portBits, &sliderData->sliderRect,
  299.                 &(**theControl).contrlRect, srcCopy, nil );
  300.         }
  301.     } else {
  302.         CopyBits ( (BitMap *) (*(sliderData->sliderPort->portPixMap)),
  303.             &((GrafPtr)theWindow)->portBits, &sliderData->sliderRect,
  304.             &(**theControl).contrlRect, srcCopy, nil );
  305.     }
  306.     
  307.     // Now unlock pixels.
  308.     UnlockPixels( sliderData->sliderPort->portPixMap );
  309.     
  310.     // Unlock data and control handles.
  311.     HUnlock( (Handle) sliderDataHnd );
  312.     HUnlock( (Handle) theControl );
  313.     
  314.     // Restore the foreground and background colors.
  315.     RGBForeColor( &savedForeColor );
  316.     RGBBackColor( &savedBackColor );
  317. }