home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / B-C / CVScrollBar.sit / CVScrollBar / CVScrollBar.cpp next >
Encoding:
Text File  |  1994-04-03  |  6.1 KB  |  280 lines  |  [TEXT/KAHL]

  1. //CVScrollBar.cpp
  2. //Definition of the class CVScrollBar
  3. //Date - March 17, 1994
  4. //© 1994 by Vladimir Potap'yev, Manley & Associates, Inc.
  5.  
  6.  
  7.  
  8. #ifndef    CVScrollBar_H
  9. #include "CVScrollBar.hpp"
  10. #endif
  11.  
  12.  
  13. //we need this to call contrlDefProc
  14. typedef    pascal long    (**CntrlDefProcHandle) (short varCode, ControlHandle hCntrl, short message,
  15.                                             long param);
  16.  
  17. //we need this struct to do mouse tracking when user is dragging thumb                                    
  18. typedef struct
  19. {
  20.     Rect    limitRect;
  21.     Rect    slopRect;
  22.     short    axis;
  23. }
  24.     ThumbStruct;
  25.  
  26.  
  27. //currently active scroll bar
  28. CVScrollBar        *gScrollBar;
  29.  
  30.  
  31.  
  32. //our action procedure for stationary parts
  33. pascal    void    SBarActionProcedure (ControlHandle hCntrl, short whichPart)
  34.  
  35. {
  36.     gScrollBar->DoGoodClick (whichPart);
  37.     //we need this to ensure smooth update of scroll bar
  38.     gScrollBar->Refresh ();
  39. }
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. /*******************************************    IVScrollBar    **************************************/
  47.  
  48. void    CVScrollBar :: IVScrollBar (    
  49.     
  50.     CView            *anEnclosure,
  51.     CBureaucrat        *aSupervisor,
  52.     Orientation        anOrientation,
  53.     short            aLength,
  54.     short            aHEncl,
  55.     short            aVEncl
  56. )
  57.     
  58. {
  59.     CScrollBar :: IScrollBar (anEnclosure, aSupervisor, anOrientation, aLength, aHEncl, aVEncl);
  60.     itsScrollCommand = 0L;
  61.     itsPageValue = 0;
  62. }
  63.  
  64. //    CVScrollBar :: IVScrollBar
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. /*******************************************    DoClick        ****************************************/
  73.  
  74.  
  75.             //a quick variation on PinRect
  76.             void    PinPtInRect (Rect& rect, Point& pt)
  77.             {
  78.                 pt.h = rect.left > pt.h? rect.left : pt.h;
  79.                 pt.h = rect.right < pt.h? rect.right : pt.h;
  80.                 pt.v = rect.top > pt.v? rect.top : pt.v;
  81.                 pt.v = rect.bottom < pt.v? rect.bottom : pt.v;
  82.             }
  83.     
  84.     
  85.     
  86. void    CVScrollBar :: DoClick (Point hitPt, short modifiers, long when)
  87.  
  88. {
  89.     short                whichPart;
  90.     LongPt                tmp;
  91.     
  92.     CntrlDefProcHandle    hCDEF;
  93.     ThumbStruct            thumbData;
  94.     Point                oldMousePos;
  95.     Point                newMousePos;
  96.     long                offset;
  97.     LongRect            barFrame;
  98.     Rect                updateRect;
  99.     char                savedState;
  100.     
  101.     
  102.     gScrollBar = this;
  103.     
  104.     QDToLongPt( hitPt, &tmp);
  105.     FrameToWind( &tmp, &hitPt);
  106.  
  107.                                         
  108.     whichPart = TestControl( macControl, hitPt);
  109.     
  110.     PenNormal();
  111.     
  112.     if (whichPart >= inThumb) 
  113.     {                                            
  114.         oldMousePos = hitPt;
  115.         
  116.         //let's get to the control definition procedure
  117.         hCDEF = (CntrlDefProcHandle) (**macControl).contrlDefProc;
  118.         
  119.         if (*hCDEF == NULL)        //see if master pointer is null
  120.         {
  121.             LoadResource ((Handle)hCDEF);    //resource is not in memory, load it in
  122.             FailResError ();                //make sure we are allright
  123.         }
  124.         
  125.         savedState = HGetState ((Handle)hCDEF);    //save the tag byte of the master pointer
  126.         HLock ((Handle)hCDEF);                    //lock it
  127.         HNoPurge ((Handle)hCDEF);                //make it unpurgeable
  128.         
  129.         //we need to fill our ThumbStruct so that we could track the mouse
  130.         //the topLeft corner of limitRect contains the point where mouse press was first detected
  131.         thumbData.limitRect.left = hitPt.h;
  132.         thumbData.limitRect.top = hitPt.v;
  133.         (**hCDEF) (GetCVariant (macControl), macControl, thumbCntl, (long)&thumbData);
  134.         
  135.         while (StillDown())
  136.         {
  137.             Prepare ();                                            //adjust coordinate system
  138.             GetMouse (&newMousePos);                            //get new mouse position
  139.             
  140.             if (PtInRect (newMousePos, &thumbData.slopRect))
  141.             {
  142.                 PinPtInRect (thumbData.limitRect, newMousePos);    //and keep it in line
  143.                 
  144.                 //we force Control Manager to update contrlValue field of ControlRecord
  145.                 //by sending it as a message posCntl
  146.                 //as a parameter, we pass a long that contains in high word the vertical offset
  147.                 //and in the low word horizontal offset
  148.                 offset = (newMousePos.v - oldMousePos.v) << 16;
  149.                 offset |= (newMousePos.h - oldMousePos.h);
  150.                 (**hCDEF) (GetCVariant (macControl), macControl, posCntl, offset);
  151.                 
  152.                 //now, update the scrolled area
  153.                 itsSupervisor->DoCommand (GetScrollCmd());
  154.             }
  155.             oldMousePos = newMousePos;
  156.         }
  157.         
  158.         //now restore the old state of the CDEF
  159.         HSetState ((Handle)hCDEF, savedState);
  160.     } 
  161.     else if (whichPart > 0) 
  162.     {
  163.         TrackControl(macControl, hitPt, (ProcPtr)&SBarActionProcedure);
  164.         
  165.         //we don't want the scroll bar to flicker    
  166.         GetFrame (&barFrame);
  167.         FrameToWindR (&barFrame, &updateRect);
  168.         ValidRect (&updateRect);
  169.     }
  170. }
  171.  
  172. //    CVScrollBar :: DoClick
  173.  
  174.  
  175.  
  176.  
  177.  
  178. /****************************************    DoGoodClick        **************************************/
  179.  
  180. void    CVScrollBar :: DoGoodClick (short whichPart)
  181. {
  182.     short theOldValue = GetValue();
  183.     short theNewValue = theOldValue;
  184.     short maxValue = GetMaxValue();
  185.     short minValue = GetMinValue();
  186.     
  187.     switch (whichPart)
  188.     {
  189.         case    inUpButton:        if (theOldValue > minValue)
  190.                                 {
  191.                                     theNewValue--;
  192.                                     SetValue(theNewValue);
  193.                                     itsSupervisor->DoCommand (GetScrollCmd());
  194.                                 }
  195.                                 break;
  196.                             
  197.         case     inDownButton:    if (theOldValue < maxValue)
  198.                                 {
  199.                                     theNewValue++;
  200.                                     SetValue(theNewValue);
  201.                                     itsSupervisor->DoCommand (GetScrollCmd());
  202.                                 }
  203.                                 break;
  204.                                 
  205.         case    inPageUp:        if (theOldValue > minValue)
  206.                                 {
  207.                                     theNewValue -= Min (GetPageValue (), (theOldValue - minValue));
  208.                                     SetValue (theNewValue);
  209.                                     itsSupervisor->DoCommand (GetScrollCmd());
  210.                                 }
  211.                                 break;
  212.                                 
  213.         case    inPageDown:        if (theOldValue < maxValue)
  214.                                 {
  215.                                     theNewValue += Min (GetPageValue(), (maxValue - theOldValue));
  216.                                     SetValue(theNewValue);
  217.                                     itsSupervisor->DoCommand (GetScrollCmd());
  218.                                 }
  219.                                 break;
  220.                                 
  221.         default:                break;
  222.     }
  223. }
  224.  
  225. //    CVScrollBar :: DoGoodClick
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232. /****************************************    SetScrollCmd    *************************************/
  233.  
  234. void    CVScrollBar :: SetScrollCmd (long aCommand)
  235. {
  236.     itsScrollCommand = aCommand;
  237. }
  238.  
  239. //    CVScrollBar :: SetScrollCmd
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246. /***************************************    GetScrollCmd    **************************************/
  247.  
  248. long    CVScrollBar :: GetScrollCmd (void)
  249. {
  250.     return itsScrollCommand;
  251. }
  252.  
  253. //    CVScrollBar :: GetScrollCmd
  254.  
  255.  
  256.  
  257.  
  258.  
  259. /*****************************************    SetPageValue    ***************************************/
  260.  
  261. void    CVScrollBar :: SetPageValue (short aValue)
  262.  
  263. {
  264.     itsPageValue = aValue;
  265. }
  266.  
  267. //    CVScrollBar :: SetPageValue
  268.  
  269.  
  270.  
  271.  
  272. /*****************************************    GetPageValue    ***************************************/
  273.  
  274. short    CVScrollBar :: GetPageValue (void)
  275.  
  276. {
  277.     return itsPageValue;
  278. }
  279.  
  280. //    CVScrollBar :: GetPageValue