home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 11.ddi / OWLSRC.PAK / SCROLLBA.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  5.6 KB  |  267 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.   switch (scrollCode) {
  220.     case SB_LINEDOWN:      SBLineDown(); break;
  221.     case SB_LINEUP:        SBLineUp(); break;
  222.     case SB_PAGEDOWN:      SBPageDown(); break;
  223.     case SB_PAGEUP:        SBPageUp(); break;
  224.     case SB_TOP:           SBTop(); break;
  225.     case SB_BOTTOM:        SBBottom(); break;
  226.     case SB_THUMBPOSITION: SBThumbPosition(thumbPos); break;
  227.     case SB_THUMBTRACK:    SBThumbTrack(thumbPos); break;
  228.     case SB_ENDSCROLL:     SBEndScroll();
  229.   }
  230. }
  231.  
  232. void
  233. TScrollBar::EvVScroll(UINT scrollCode, UINT thumbPos, HWND hWndCtl)
  234. {
  235.   EvHScroll(scrollCode, thumbPos, hWndCtl);
  236. }
  237.  
  238. #endif
  239. #if !defined(SECTION) || SECTION == 2
  240.  
  241. IMPLEMENT_STREAMABLE1(TScrollBar, TControl);
  242.  
  243. //
  244. // Reads an instance of TScrollBar from the passed ipstream.
  245. //
  246. void*
  247. TScrollBar::Streamer::Read(ipstream& is, uint32 /*version*/) const
  248. {
  249.   ReadBaseObject((TControl*)GetObject(), is);
  250.   is >> GetObject()->LineMagnitude
  251.      >> GetObject()->PageMagnitude;
  252.   return GetObject();
  253. }
  254.  
  255. //
  256. // Writes the TScrollBar to the passed opstream.
  257. //
  258. void
  259. TScrollBar::Streamer::Write(opstream& os) const
  260. {
  261.   WriteBaseObject((TControl*)GetObject(), os);
  262.   os << GetObject()->LineMagnitude
  263.      << GetObject()->PageMagnitude;
  264. }
  265.  
  266. #endif
  267.