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

  1. #include "SliderCDEF.h"
  2.  
  3. // SliderPositionControl
  4. //
  5. // Positions the indicator in the control track in response to a mouseUp after
  6. // dragging the indicator. The routine adjusts the contrlValue field and redraws
  7. // the control to reflect the new value. This is not really necessary for this
  8. // slider, since the contrlValue and control's appearance are constantly updated
  9. // during custom dragging of the indicator.
  10. void SliderPositionControl( short varCode, ControlHandle theControl, long param )
  11. {
  12.     Point            thePoint;
  13.     ControlPtr    theControlPtr;
  14.     Rect            thumbRect;
  15.     long            prevOffset;
  16.     long            range;
  17.     Rect            trackRect;
  18.     short        trackWidth;
  19.     short        thumbWidth;
  20.     short        thumbHalfWidth;
  21.     short        thumbCenter;
  22.     float            ratio;
  23.     
  24.     // Lock the control handle and dereference.
  25.     HLock( (Handle) theControl );
  26.     theControlPtr = *theControl;
  27.     
  28.     // Get the offset from param.
  29.     thePoint.v = HiWord( param );
  30.     thePoint.h = LoWord( param );
  31.     
  32.     // Have'nt done vertical sliders yet.
  33.     if ( thePoint.h == 0 ) return;
  34.  
  35.     // Calculate the thumb rectangle at the old value.
  36.     thumbRect = CalcThumbRect( theControl );
  37.     
  38.     // Calculate the thumb width and half-width.
  39.     thumbWidth = kThumbSize;
  40.     thumbHalfWidth = thumbWidth / 2;
  41.  
  42.     // Calculate the track's rectangle and its width in pixels.
  43.     trackRect = theControlPtr->contrlRect;
  44.     InsetRect( &trackRect,  thumbHalfWidth + 1, 1 );
  45.     trackWidth = trackRect.right - trackRect.left;
  46.     
  47.     // Calculate the range of conrol values allowed.
  48.     range = theControlPtr->contrlMax - theControlPtr->contrlMin;
  49.     
  50.     // Calculate the ratio of control values to track width.
  51.     // This would be better with fixed-point math.
  52.     ratio = (float) ((float) range) / ((float)trackWidth);
  53.  
  54.     // Calculate the new thumb rectangle.
  55.     thumbRect.left += thePoint.h;
  56.     thumbRect.right += thePoint.h;
  57.     
  58.     // Calculate the new center of the thumb.
  59.     thumbCenter = thumbRect.left + thumbHalfWidth;
  60.     
  61.     // Calculate the new control value.
  62.     // (thumbCenter - trackRect.left) is how far into the track the thumbCenter is.
  63.     theControlPtr->contrlValue = (thumbCenter - trackRect.left) * ratio;
  64.     
  65.     // Reajust the value if it's above the max or below the min.
  66.     if ( theControlPtr->contrlValue > theControlPtr->contrlMax ) {
  67.         theControlPtr->contrlValue = theControlPtr->contrlMax;
  68.     }
  69.     if ( theControlPtr->contrlValue < theControlPtr->contrlMin ) {
  70.         theControlPtr->contrlValue = theControlPtr->contrlMin;
  71.     }
  72.     
  73.     // Redraw the control with the new value if it's visible.
  74.     if ( (**theControl).contrlVis ) {
  75.         SliderDrawControl( varCode, theControl, param );
  76.     }
  77.  
  78.     // Unlock the control handle.
  79.     HUnlock( (Handle) theControl );
  80. }