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

  1. #include "SliderCDEF.h"
  2.  
  3. // SliderInitControl
  4. //
  5. // Performs initialization for the control definition function such as putting some
  6. // data in the contrlData or contrlRfCon fields or setting the contrlAction field to
  7. // (Ptr) -1L so that the control manager will call the CDEF with the autoTrack
  8. // message.
  9. void SliderInitControl( short varCode, ControlHandle theControl, long param )
  10. {
  11.     // Allocate the slider's data and place a handle to the data structure
  12.     // in the contrlData field.
  13.     HLock( (Handle) theControl );
  14.     (**theControl).contrlData = NewHandle( sizeof( SliderDataRec ) );
  15.     if ( (**theControl).contrlData != nil ) {
  16.         // Create the offscreen graphics world for the slider.
  17.         SliderCreateOffscreenWorld( theControl );
  18.     }
  19.     HUnlock( (Handle) theControl );
  20. }
  21.  
  22.  
  23.  
  24. // SliderCreateOffscreenWorld
  25. //
  26. // Create the offscreen graphics world for the slider control.
  27. void SliderCreateOffscreenWorld( ControlHandle theControl )
  28. {
  29.     SliderDataHnd    sliderData;
  30.     short        err;
  31.     
  32.     // Lock down the control handle.
  33.     HLock( (Handle) theControl );
  34.     
  35.     // Get the handle to the slider's data and lock it.
  36.     sliderData = (SliderDataHnd) (**theControl).contrlData;    
  37.     HLock( (Handle) sliderData );
  38.     
  39.     // Set up the slider's offscreen graphics worlds for drawing.
  40.     // Get the current port and device.
  41.     GetGWorld( &(**sliderData).currPort, &(**sliderData).currDev );
  42.  
  43.     // Calculate the size of the offscreen graphics world. Start with size
  44.     // of the control's rect.
  45.     (**sliderData).sliderPortRect = (**theControl).contrlRect;
  46.     
  47.     // Unlock the control handle, we're done with it.
  48.     HUnlock( (Handle) theControl );
  49.  
  50.     // Move the rect to the top-left corner, (0,0).
  51.     OffsetRect( &(**sliderData).sliderPortRect, -(**sliderData).sliderPortRect.left,
  52.         -(**sliderData).sliderPortRect.top );
  53.  
  54.     // Make this the rectangle for the slider track.
  55.     //    -----------------------------
  56.     //    |        track            |
  57.     //    -----------------------------
  58.     (**sliderData).sliderTrackRect = (**sliderData).sliderPortRect;
  59.     
  60.     // Add room at the end for an entire image of the
  61.     // slider (track with thumb in it).
  62.     //    ----------------------------------------------------------
  63.     //    |        track            |    entire slider image        |
  64.     //    ----------------------------------------------------------
  65.     (**sliderData).sliderPortRect.right += (**sliderData).sliderPortRect.right;
  66.     (**sliderData).sliderRect = (**sliderData).sliderPortRect;
  67.     (**sliderData).sliderRect.left = (**sliderData).sliderTrackRect.right;
  68.  
  69.     // Add room at the end of the track for an image of the
  70.     // slider's thumb.
  71.     //    ------------------------------------------------------------------------
  72.     //    |        track            |    entire slider image        |    thumb    |
  73.     //    ------------------------------------------------------------------------
  74.     (**sliderData).sliderPortRect.right += kThumbSize;
  75.     (**sliderData).sliderThumbRect = (**sliderData).sliderPortRect;
  76.     (**sliderData).sliderThumbRect.left = (**sliderData).sliderRect.right;
  77.  
  78.     // Create offscreen graphics world.
  79.     err = NewGWorld( &(**sliderData).sliderPort, 0, &(**sliderData).sliderPortRect, nil, nil, 0 );
  80.  
  81.     // Unlock the slider's data.
  82.     HUnlock( (Handle) sliderData );
  83.  
  84.     // Draw the control's track and the control's indicator in the offscreen graphics world.
  85.     SliderDrawControlParts( theControl );    
  86. }
  87.     
  88.  
  89.  
  90.  
  91. // SliderDrawControlParts
  92. //
  93. // Draw each of the slider's parts into the slider's GWorld.
  94. void SliderDrawControlParts( ControlHandle theControl )
  95. {
  96.     SliderDataHnd    sliderDataHnd;
  97.     SliderDataPtr    sliderData;
  98.     short        err;
  99.     SliderColors    colors;
  100.     
  101.     // Get a ptr to the slider's data.
  102.     sliderDataHnd = (SliderDataHnd) (**theControl).contrlData;
  103.     HLock( (Handle) sliderDataHnd );
  104.     sliderData = *sliderDataHnd;
  105.  
  106.     // Lock down Pixels that we are drawing to so that memory will not  move
  107.     LockPixels ( sliderData->sliderPort->portPixMap );
  108.     
  109.     // Setup drawing area to be the offscreen graphics world
  110.     SetGWorld ( sliderData->sliderPort, nil );
  111.     
  112.     // Clear Rectangle, so that CopyBits will not copy extra background
  113.     EraseRect ( &sliderData->sliderPortRect );
  114.  
  115.     // Draw the track.
  116.     SliderGetControlColors( theControl, &colors );
  117.     SliderDrawControlTrack( theControl, &colors );
  118.     SliderDrawControlThumb( theControl, &colors );
  119.  
  120.     // Done drawing, now reset port etc.
  121.     SetGWorld ( sliderData->currPort, sliderData->currDev );
  122.  
  123.     // Now unlock pixels.
  124.     UnlockPixels( sliderData->sliderPort->portPixMap );
  125.     
  126.     // Unlock the slider's data.
  127.     HUnlock( (Handle) sliderDataHnd );
  128. }