home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / SCROLLBA.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  5.6 KB  |  270 lines

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