home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / PASM.LZH / SCROLLBA.CPP < prev    next >
C/C++ Source or Header  |  1996-05-22  |  6KB  |  273 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //   source\owl\scrollba.cpp
  4. //   Implementation of class TScrollBar.  This defines the basic
  5. //   behavior of all scrollbar controls.
  6. //----------------------------------------------------------------------------
  7. #pragma hdrignore SECTION
  8. #include <owl\owlpch.h>
  9. #include <owl\scrollba.h>
  10.  
  11. #if !defined(SECTION) || SECTION == 1
  12.  
  13. DEFINE_RESPONSE_TABLE1(TScrollBar, TControl)
  14.   EV_WM_VSCROLL,
  15.   EV_WM_HSCROLL,
  16. END_RESPONSE_TABLE;
  17.  
  18. //
  19. // constructor for a TScrollBar object
  20. //
  21. // if the size attribute(H for horizontal scrollbars, W for vertical) is
  22. // zero, the attribute is set to the appropriate system metric
  23. //
  24. TScrollBar::TScrollBar(TWindow*        parent,
  25.                        int             id,
  26.                        int x, int y, int w, int h,
  27.                        BOOL            isHScrollBar,
  28.                        TModule*        module)
  29.   : TControl(parent, id, 0, x, y, w, h, module)
  30. {
  31.   LineMagnitude = 1;
  32.   PageMagnitude = 10;
  33.  
  34.   if (isHScrollBar) {
  35.     Attr.Style |= SBS_HORZ;
  36.  
  37.     if (Attr.H == 0)
  38.       Attr.H = GetSystemMetrics(SM_CYHSCROLL);
  39.  
  40.   } else {
  41.     Attr.Style |= SBS_VERT;
  42.  
  43.     if (Attr.W == 0)
  44.       Attr.W = GetSystemMetrics(SM_CXVSCROLL);
  45.   }
  46. }
  47.  
  48. TScrollBar::TScrollBar(TWindow*   parent,
  49.                        int        resourceId,
  50.                        TModule*   module)
  51.   : TControl(parent, resourceId, module)
  52. {
  53.   LineMagnitude = 1;
  54.   PageMagnitude = 10;
  55. }
  56.  
  57. //
  58. // transfers state information for a TScrollbar
  59. //
  60. // direction specifies whether data is to be read from or written to the
  61. // buffer, or whether the data element size is simply to be returned
  62. //
  63. // the return value is the size (in bytes) of the transfer data
  64. //
  65. UINT
  66. TScrollBar::Transfer(void* buffer, TTransferDirection direction)
  67. {
  68.   TScrollBarData* scrollBuff = (TScrollBarData*)buffer;
  69.  
  70.   if (direction == tdGetData) {
  71.     GetRange(scrollBuff->LowValue, scrollBuff->HighValue);
  72.     scrollBuff->Position = GetPosition();
  73.  
  74.   } else if (direction == tdSetData) {
  75.     SetRange(scrollBuff->LowValue, scrollBuff->HighValue);
  76.     SetPosition(scrollBuff->Position);
  77.   }
  78.  
  79.   return sizeof(TScrollBarData);
  80. }
  81.  
  82. //
  83. // Return name of predefined Windows scrollbar class
  84. //
  85. char far *
  86. TScrollBar::GetClassName()
  87. {
  88.   return "SCROLLBAR";
  89. }
  90.  
  91. //
  92. // initialize the scrollbar to the default range of 0 .. 100
  93. //
  94. void
  95. TScrollBar::SetupWindow()
  96. {
  97.   SetRange(0, 100);
  98.   TControl::SetupWindow();
  99. }
  100.  
  101. //
  102. // sets the position of the thumb
  103. //
  104. void
  105. TScrollBar::SetPosition(int thumbPos)
  106. {
  107.   int  min, max;
  108.   GetRange(min, max);
  109.  
  110.   //
  111.   // constrain "thumbPos" to be in the range "min .. max"
  112.   //
  113.   if (thumbPos > max)
  114.     thumbPos = max;
  115.  
  116.   else if (thumbPos < min)
  117.     thumbPos = min;
  118.  
  119.   if (thumbPos != GetPosition())
  120.     ::SetScrollPos(HWindow, SB_CTL, thumbPos, TRUE);
  121. }
  122.  
  123. //
  124. // changes the position of the thumb by "delta" and returns the new position
  125. //
  126. int
  127. TScrollBar::DeltaPos(int delta)
  128. {
  129.   if (delta != 0)
  130.     SetPosition(GetPosition() + delta);
  131.  
  132.   return GetPosition();
  133. }
  134.  
  135. //
  136. // changes the position of the thumb by "LineMagnitude"
  137. //
  138. void
  139. TScrollBar::SBLineUp()
  140. {
  141.   DeltaPos(-LineMagnitude);
  142. }
  143.  
  144. //
  145. // changes the position of the thumb by "LineMagnitude"
  146. //
  147. void
  148. TScrollBar::SBLineDown()
  149. {
  150.   DeltaPos(LineMagnitude);
  151. }
  152.  
  153. //
  154. // changes the position of the thumb by "PageMagnitude"
  155. //
  156. void
  157. TScrollBar::SBPageUp()
  158. {
  159.   DeltaPos(-PageMagnitude);
  160. }
  161.  
  162. //
  163. // changes the position of the thumb by "PageMagnitude"
  164. //
  165. void
  166. TScrollBar::SBPageDown()
  167. {
  168.   DeltaPos(PageMagnitude);
  169. }
  170.  
  171. //
  172. // moves the thumb to the new position
  173. //
  174. void
  175. TScrollBar::SBThumbPosition(int thumbPos)
  176. {
  177.   SetPosition(thumbPos);
  178. }
  179.  
  180. //
  181. // moves the thumb to the new position
  182. //
  183. void
  184. TScrollBar::SBThumbTrack(int thumbPos)
  185. {
  186.   SetPosition(thumbPos);
  187. }
  188.  
  189. //
  190. // moves the thumb to the top of the scrollbar
  191. //
  192. void
  193. TScrollBar::SBTop()
  194. {
  195.   int  min, max;
  196.   GetRange(min, max);
  197.   SetPosition(min);
  198. }
  199.  
  200. //
  201. // moves the thumb to the bottom of the scrollbar
  202. //
  203. void
  204. TScrollBar::SBBottom()
  205. {
  206.   int  min, max;
  207.   GetRange(min, max);
  208.   SetPosition(max);
  209. }
  210.  
  211. void
  212. TScrollBar::SBEndScroll()
  213. {
  214. }
  215.  
  216. void
  217. TScrollBar::EvHScroll(UINT scrollCode, UINT _thumbPos, HWND /*hWndCtl*/)
  218. {
  219.     int thumbPos;
  220. #ifdef __WIN32__
  221.     thumbPos = (int)((short)_thumbPos);
  222. #else
  223.     thumbPos = _thumbPos;
  224. #endif
  225.   switch (scrollCode) {
  226.     case SB_LINEDOWN:      SBLineDown(); break;
  227.     case SB_LINEUP:        SBLineUp(); break;
  228.     case SB_PAGEDOWN:      SBPageDown(); break;
  229.     case SB_PAGEUP:        SBPageUp(); break;
  230.     case SB_TOP:           SBTop(); break;
  231.     case SB_BOTTOM:        SBBottom(); break;
  232.     case SB_THUMBPOSITION: SBThumbPosition(thumbPos); break;
  233.     case SB_THUMBTRACK:    SBThumbTrack(thumbPos); break;
  234.     case SB_ENDSCROLL:     SBEndScroll();
  235.   }
  236. }
  237.  
  238. void
  239. TScrollBar::EvVScroll(UINT scrollCode, UINT thumbPos, HWND hWndCtl)
  240. {
  241.   EvHScroll(scrollCode, thumbPos, hWndCtl);
  242. }
  243.  
  244. #endif
  245. #if !defined(SECTION) || SECTION == 2
  246.  
  247. IMPLEMENT_STREAMABLE1(TScrollBar, TControl);
  248.  
  249. //
  250. // Reads an instance of TScrollBar from the passed ipstream.
  251. //
  252. void*
  253. TScrollBar::Streamer::Read(ipstream& is, uint32 /*version*/) const
  254. {
  255.   ReadBaseObject((TControl*)GetObject(), is);
  256.   is >> GetObject()->LineMagnitude
  257.      >> GetObject()->PageMagnitude;
  258.   return GetObject();
  259. }
  260.  
  261. //
  262. // Writes the TScrollBar to the passed opstream.
  263. //
  264. void
  265. TScrollBar::Streamer::Write(opstream& os) const
  266. {
  267.   WriteBaseObject((TControl*)GetObject(), os);
  268.   os << GetObject()->LineMagnitude
  269.      << GetObject()->PageMagnitude;
  270. }
  271.  
  272. #endif
  273.