home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWL1.PAK / SCROLLBA.CPP < prev    next >
Text File  |  1995-08-29  |  6KB  |  207 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. /* --------------------------------------------------------
  4.   SCROLLBA.CPP
  5.   Defines type TScrollBar.  This defines the basic
  6.   behavior of all scrollbar controls.
  7.   -------------------------------------------------------- */
  8.  
  9. #include "scrollba.h"
  10.  
  11. /* Constructor for a TScrollBar object.  Initializes its data members
  12.   (including its creation attributes) using parameters passed and
  13.   default values. If the size attribute (H for horizontal scrollbars,
  14.   W for vertical) is zero, the attribute is set to the appropriate
  15.   system metric. */
  16. TScrollBar::TScrollBar(PTWindowsObject AParent, int AnId, int X, int Y,
  17.                        int W, int H, BOOL IsHScrollBar, PTModule AModule)
  18.            : TControl(AParent, AnId, NULL, X, Y, W, H, AModule)
  19. {
  20.   LineMagnitude = 1;
  21.   PageMagnitude = 10;
  22.   if ( IsHScrollBar )
  23.   {
  24.     Attr.Style |= SBS_HORZ;
  25.     if ( Attr.H == 0 )
  26.       Attr.H = GetSystemMetrics(SM_CYHSCROLL);
  27.   }
  28.   else
  29.   {
  30.     Attr.Style |= SBS_VERT;
  31.     if ( Attr.W == 0 )
  32.       Attr.W = GetSystemMetrics(SM_CXVSCROLL);
  33.   }
  34. }
  35.  
  36. TScrollBar::TScrollBar(PTWindowsObject AParent, int ResourceId, PTModule AModule)
  37.                        : TControl(AParent, ResourceId, AModule)
  38. {
  39.   LineMagnitude = 1;
  40.   PageMagnitude = 10;
  41. }
  42.  
  43. /* Transfers state information for a TScrollbar. The TransferFlag passed
  44.    specifies whether data is to be read from or written to the passed
  45.   buffer, or whether the data element size is simply to be returned.
  46.   The return value is the size (in bytes) of the transfer data. */
  47. WORD TScrollBar::Transfer(Pvoid DataPtr, WORD TransferFlag)
  48. {
  49.   TScrollBarData *NewPtr = (TScrollBarData *)DataPtr;
  50.  
  51.   if ( TransferFlag == TF_GETDATA )
  52.   {
  53.     GetRange(NewPtr->LowValue, NewPtr->HighValue);
  54.     NewPtr->Position = GetPosition();
  55.   }
  56.   else if ( TransferFlag == TF_SETDATA )
  57.   {
  58.     SetRange(NewPtr->LowValue, NewPtr->HighValue);
  59.     SetPosition(NewPtr->Position);
  60.   }
  61.   return sizeof(TScrollBarData);
  62. }
  63.  
  64. /* Sets up an associated scrollbar by setting its range to 0..100. */
  65. void TScrollBar::SetupWindow()
  66. {
  67.   SetRange(0, 100);
  68.   TControl::SetupWindow();
  69. }
  70.  
  71. /* Retrieves the range of values that the associated scrollbar can
  72.   return. */
  73. void TScrollBar::GetRange(int& LoVal, int& HiVal)
  74. {
  75.   GetScrollRange(HWindow, SB_CTL, &LoVal, &HiVal);
  76. }
  77.  
  78. /* Returns the position of the thumb of the associated scrollbar. */
  79. int TScrollBar::GetPosition()
  80. {
  81.   return GetScrollPos(HWindow, SB_CTL);
  82. }
  83.  
  84. /* Sets the range of values that the associated scrollbar can return. */
  85. void TScrollBar::SetRange(int LoVal, int HiVal)
  86. {
  87.   SetScrollRange(HWindow, SB_CTL, LoVal, HiVal, FALSE);
  88. }
  89.  
  90. /* Sets the position of the thumb of the associated scrollbar. */
  91. void TScrollBar::SetPosition(int ThumbPos)
  92. {
  93.   int LoVal , HiVal;
  94.  
  95.   GetRange(LoVal, HiVal);
  96.   if ( ThumbPos > HiVal )
  97.     ThumbPos = HiVal;
  98.   else if ( ThumbPos < LoVal )
  99.     ThumbPos = LoVal;
  100.   if ( ThumbPos != GetPosition() )
  101.     SetScrollPos(HWindow, SB_CTL, ThumbPos, TRUE);
  102. }
  103.  
  104. /* Changes the position (by Delta) of the thumb of the associated
  105.   scrollbar.  Returns the new position. */
  106. int TScrollBar::DeltaPos(int Delta)
  107. {
  108.     if ( Delta != 0 )
  109.     SetPosition(GetPosition() + Delta);
  110.     return GetPosition();
  111. }
  112.  
  113. /* Responds to an SB_LINEUP notification message which the associated
  114.   scrollbar sent to its parent.  Changes the position (by LineMagnitude)
  115.   of the thumb of the associated scrollbar. */
  116. void TScrollBar::SBLineUp(TMessage&)
  117. {
  118.     DeltaPos(0 - LineMagnitude);
  119. }
  120.  
  121. /* Responds to an SB_LINEDOWN notification message which the associated
  122.   scrollbar sent to its parent.  Changes the position (by LineMagnitude)
  123.   of the thumb. */
  124. void TScrollBar::SBLineDown(TMessage&)
  125. {
  126.     DeltaPos(LineMagnitude);
  127. }
  128.  
  129. /* Responds to an SB_PAGEUP notification message which the associated
  130.   scrollbar sent to its parent.  Changes the position (by PageMagnitude)
  131.   of the thumb. */
  132. void TScrollBar::SBPageUp(TMessage&)
  133. {
  134.     DeltaPos(0 - PageMagnitude);
  135. }
  136.  
  137. /* Responds to an SB_PAGEDOWN notification message which the associated
  138.   scrollbar sent to its parent.  Changes the position (by PageMagnitude)
  139.   of the thumb. */
  140. void TScrollBar::SBPageDown(TMessage&)
  141. {
  142.     DeltaPos(PageMagnitude);
  143. }
  144.  
  145. /* Responds to an SB_THUMBPOSITION notification message which the
  146.   associated scrollbar sent to its parent.  Moves the thumb of the
  147.   scrollbar to the new position. */
  148. void TScrollBar::SBThumbPosition(TMessage& Msg)
  149. {
  150.     SetPosition(LOWORD(Msg.LParam));
  151. }
  152.  
  153. /* Responds to an SB_THUMBTRACK notification message which the
  154.    associated scrollbar sent to its parent.  Draws the thumb in the
  155.    current position on the track. */
  156. void TScrollBar::SBThumbTrack(TMessage& Msg)
  157. {
  158.     SetPosition(LOWORD(Msg.LParam));
  159. }
  160.  
  161. /* Responds to an SB_TOP notification message which the associated
  162.   scrollbar sent to its parent.  Moves the thumb to the top of the
  163.   scrollbar. */
  164. void TScrollBar::SBTop(TMessage&)
  165. {
  166.     int Lo, Hi;
  167.  
  168.     GetRange(Lo, Hi);
  169.     SetPosition(Lo);
  170. }
  171.  
  172. /* Responds to an SB_BOTTOM notification message which the associated
  173.   scrollbar sent to its parent.  Moves the thumb to the bottom of the
  174.   scrollbar. */
  175. void TScrollBar::SBBottom(TMessage&)
  176. {
  177.     int Lo , Hi;
  178.  
  179.     GetRange(Lo, Hi);
  180.     SetPosition(Hi);
  181. }
  182.  
  183. /* Reads an instance of TScrollBar from the passed ipstream. */
  184. void *TScrollBar::read(ipstream& is)
  185. {
  186.   TWindow::read(is);
  187.   is >> LineMagnitude
  188.      >> PageMagnitude;
  189.   return this;
  190. }
  191.  
  192. /* Writes the TScrollBar to the passed opstream. */
  193. void TScrollBar::write(opstream& os)
  194. {
  195.   TWindow::write(os);
  196.   os << LineMagnitude
  197.      << PageMagnitude;
  198. }
  199.  
  200. TStreamable *TScrollBar::build()
  201. {
  202.   return new TScrollBar(streamableInit);
  203. }
  204.  
  205. TStreamableClass RegScrollBar("TScrollBar", TScrollBar::build,
  206.                       __DELTA(TScrollBar));
  207.