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

  1. #include "SliderCDEF.h"
  2.  
  3. // The only CDEF globals.
  4. VBLRec gVBLRec;
  5. Boolean gAnimating = false;
  6. static DefVideoRec gVideoInfoRec;
  7.  
  8.  
  9. // SliderDragControl
  10. //
  11. // Drags the control's indicator around. Called in response to the dragCntl message.
  12. long SliderDragControl( short varCode, ControlHandle theControl, long param )
  13. {
  14.     Point            startPoint, mouseLoc;
  15.     ControlPtr    theControlPtr;
  16.     Rect            slopRect, thumbRect;
  17.     long            offset;
  18.     long            prevOffset;
  19.     long            range;
  20.     Rect            trackRect;
  21.     short        trackWidth;
  22.     short        thumbWidth;
  23.     short        thumbHalfWidth;
  24.     short        thumbCenter;
  25.     float            ratio;
  26.     OSErr        err;
  27.     
  28.     // Lock the control handle and dereference.
  29.     HLock( (Handle) theControl );
  30.     theControlPtr = *theControl;
  31.  
  32.     // Calculate the thumb rectangle at the old value.
  33.     thumbRect = CalcThumbRect( theControl );
  34.     
  35.     // Calculate the track's rectangle and its width in pixels.
  36.     thumbWidth = kThumbSize;
  37.     thumbHalfWidth = thumbWidth / 2;
  38.     trackRect = theControlPtr->contrlRect;
  39.     InsetRect( &trackRect,  thumbHalfWidth + 1, 1 );
  40.     trackWidth = trackRect.right - trackRect.left;
  41.     
  42.     // Calculate the range of conrol values allowed.
  43.     range = theControlPtr->contrlMax - theControlPtr->contrlMin;
  44.     
  45.     // Calculate the ratio of control values to track width.
  46.     // This shoul d probably be done using fixed-point numbers.
  47.     ratio = (float) ((float) range) / ((float)trackWidth);
  48.  
  49.     // Calculate the slopRect for dragging the indicator.
  50.     slopRect = theControlPtr->contrlRect;
  51.     InsetRect( &slopRect, 0, -10 );
  52.  
  53.     // Get the initial mouse location.
  54.     GetMouse( &startPoint );
  55.  
  56.     // Install a notification VBL task.
  57.     err = VBLTaskSetup();
  58.     if ( err != noErr ) {
  59.         return;
  60.     }
  61.     
  62.     // Let the draw routine know the slider is animating.
  63.     gAnimating = true;
  64.  
  65.     while( StillDown() ) {
  66.         GetMouse( &mouseLoc );
  67.         offset = mouseLoc.h - startPoint.h;
  68.         if ( offset != 0 && PtInRect( mouseLoc, &slopRect ) ) {
  69.             // Calculate the new thumb rectangle.
  70.             thumbRect.left += offset;
  71.             thumbRect.right += offset;
  72.             
  73.             // Calculate the center of the thumb.
  74.             thumbCenter = thumbRect.left + thumbHalfWidth;
  75.             
  76.             // Calculate the new control value.
  77.             // (thumbCenter - trackRect.left) is how far into the track the thumbCenter is.
  78.             theControlPtr->contrlValue = (thumbCenter - trackRect.left) * ratio;
  79.  
  80.             // Make sure the new value does not exceed the control's range.
  81.             if ( theControlPtr->contrlValue > theControlPtr->contrlMax )
  82.                 theControlPtr->contrlValue = theControlPtr->contrlMax;
  83.             if ( theControlPtr->contrlValue < theControlPtr->contrlMin )
  84.                 theControlPtr->contrlValue = theControlPtr->contrlMin;
  85.             
  86.             // Draw the control with the thumb at the new value only if
  87.             // the control is visible.
  88.             if ( theControlPtr->contrlVis ) {
  89.                 SliderDrawControl( varCode, theControl, param );
  90.             }
  91.             
  92.             // Update the mouse location for the next iteration.
  93.             startPoint = mouseLoc;
  94.         }
  95.     }
  96.     
  97.     // Turn off animation.
  98.     gAnimating = false;
  99.     
  100.     // Remove the VBL task.
  101.     err = RemoveVBLTask();
  102.  
  103.     // Unlock the control handle.
  104.     HUnlock( (Handle) theControl );
  105.     
  106.     // Return a non-zero value so the control manager knows that indicator
  107.     // dragging has been handled by the CDEF.
  108.     return 1L;
  109. }
  110.  
  111.  
  112.  
  113.  
  114. // The next 3 routines come from Think Reference 2.0.
  115. OSErr VBLTaskSetup( void )
  116. {
  117.     OSErr    err;
  118.     
  119.     GetVideoDefault( &gVideoInfoRec );
  120.     
  121.     gVBLRec.xDoTask = 0;
  122.     
  123.     gVBLRec.xVBLTask.qType = vType;
  124.     gVBLRec.xVBLTask.vblAddr = (ProcPtr) MyVBLTask;
  125.     gVBLRec.xVBLTask.vblCount = 1;
  126.     gVBLRec.xVBLTask.vblPhase = 0;
  127.     gVBLRec.vblA5 = (long) CurrentA5;
  128.  
  129.     err = SlotVInstall( (QElemPtr) &gVBLRec.xVBLTask, gVideoInfoRec.sdSlot );
  130.     
  131.     return err;
  132. }
  133.  
  134.  
  135.  
  136.  
  137. OSErr RemoveVBLTask( void )
  138. {
  139.     OSErr    err;
  140.     
  141.     err = SlotVRemove( (QElemPtr) &gVBLRec.xVBLTask, gVideoInfoRec.sdSlot );
  142.     
  143.     return err;
  144. }
  145.  
  146.  
  147.  
  148.  
  149. void MyVBLTask( void )
  150. {
  151.     VBLRecPtr    recPtr;
  152.     GrafPtr        oldPort;
  153.     long            curA5;
  154.  
  155.     recPtr = (VBLRecPtr) GetVBLRec();
  156.     curA5 = SetA5( recPtr->vblA5 );
  157.  
  158.     recPtr->xDoTask = 1;
  159.     // Reset task to the correct time.
  160.     recPtr->xVBLTask.vblCount = kVBLCount;    
  161.     
  162.     curA5 = SetA5(curA5);
  163. }