OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
scroller.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1991, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of class TScroller.
7/// Including bug fixes by Rich Goldstein
8//----------------------------------------------------------------------------
9#include <owl/pch.h>
10#include <owl/scroller.h>
11#include <owl/window.h>
12#include <owl/dc.h>
13#include <stdlib.h>
14
15#include <algorithm>
16
17namespace owl {
18
20
21//
22/// Constructs a TScroller object with window as the owner window, and xUnit, yUnit,
23/// xRange, and yRange as xUnit, yUnit, xRange and yRange, respectively. Initializes
24/// data members to default values. HasHScrollBar and HasVScrollBar are set
25/// according to the scroll bar attributes of the owner window.
26//
28 int xUnit,
29 int yUnit,
30 long xRange,
31 long yRange)
32{
33 Window = window;
34 XPos = YPos = 0;
35 XUnit = xUnit;
36 YUnit = yUnit;
37 XRange = xRange;
38 YRange = yRange;
39 XTotalUnits = 0;
40 YTotalUnits = 0;
41 XLine = 1; YLine = 1;
42 XPage = 1; YPage = 1;
43 AutoMode = true;
44 TrackMode = true;
45 AutoOrg = true;
48}
49
50//
51/// Destructs a TScroller object. Sets owning window's Scroller number variable to 0.
52//
54{
55 if (Window && Window->GetScroller() == this)
57}
58
59//
60/// Sets the XPage and YPage data members (amount by which to scroll on a page
61/// scroll request) to the width and height (in XUnits and
62/// YUnits) of the owner window's client area.
63//
64void
66{
69
70 if (Window && Window->GetHandle()) {
73 int width = clientRect.Width();
74 int height = clientRect.Height();
75
76 if (width && XUnit > 0) {
77 XPage = std::max(1, (width+1) / XUnit - 1);
78 if (XTotalUnits && XTotalUnits > width) {
79 SetScrollPage(SB_HORZ, (width * XRange) / XTotalUnits, true);
80 }
81 else
83 }
84
85 if (height && YUnit > 0) {
86 YPage = std::max(1, (height+1) / YUnit - 1);
87 if (YTotalUnits && YTotalUnits > height) {
88 SetScrollPage(SB_VERT, (height * YRange) / YTotalUnits, true);
89 }
90 else
92 }
93 }
94}
95
96//
97/// Sets the xRange and yRange of the TScroller to the parameters specified. Then
98/// calls SetSBarRange to synchronize the range of the owner window's scroll bars.
99//
100void
102{
103 XRange = xRange;
104 YRange = yRange;
105 SetSBarRange();
107}
108
109//
110// Sets the total units of the window
111//
112void
120
121//
122/// Sets the XUnit and YUnit data members to TheXUnit and TheYUnit, respectively.
123/// Updates XPage and YPage by calling SetPageSize.
124//
125void
127{
128 XUnit = xUnit;
129 YUnit = yUnit;
130 SetPageSize();
131}
132
133//
134/// Sets the range of the owner window's scroll bars to match the range of the
135/// TScroller and repaints as necessary
136//
137void
139{
142
143 if (Window && Window->GetHandle()) {
144 if (HasHScrollBar) {
145 int curMin, curMax;
147 int newMax = std::max(0, std::min(int(XRange - 1), static_cast<int>(INT_MAX)));
148 if (newMax != curMax || curMin != 0)
149 SetScrollRange(SB_HORZ, 0, newMax, true);
150 }
151
152 if (HasVScrollBar) {
153 int curMin, curMax;
155 int newMax = std::max(0, std::min(int(YRange - 1), static_cast<int>(INT_MAX)));
156 if (newMax != curMax || curMin != 0)
157 SetScrollRange(SB_VERT, 0, newMax, true);
158 }
159 }
160}
161
162//
163/// If TScroller_AutoOrg is true (default condition), BeginView automatically
164/// offsets the origin of the logical coordinates of the client area by XPos, YPos
165/// during a paint operation. If AutoOrg is false (for example, when the scroller is
166/// larger than 32,767 units), you must set the offset manually.
167//
168void
170{
171 long xOrg = XPos * XUnit;
172 long yOrg = YPos * YUnit;
173
174 if (AutoOrg && xOrg <= INT_MAX && yOrg <= INT_MAX) {
175 TPoint offset(int(-xOrg), int(-yOrg));
177 rect -= offset;
178 }
179}
180
181//
182/// Updates the position of the owner window's scroll bars to be coordinated with
183/// the position of the TScroller.
184//
185void
187{
188 if (Window) {
189 if (HasHScrollBar) {
190 int newPos = XPos;
193 }
194
195 if (HasVScrollBar) {
196 int newPos = YPos;
199 }
200 }
201}
202
203//
204/// Responds to the specified vertical scrollEvent by calling ScrollBy or ScrollTo.
205/// The type of scroll event is identified by the corresponding SB_ constants.
206/// The 16-bit thumbPos argument is ignored. The function retrieves the 32-bit value
207/// directly from the scroll bar instead.
208//
209void
211{
212 switch (scrollEvent) {
213 case SB_LINEDOWN:
214 ScrollBy(0, YLine);
215 break;
216
217 case SB_LINEUP:
218 ScrollBy(0, -YLine);
219 break;
220
221 case SB_PAGEDOWN:
222 ScrollBy(0, YPage);
223 break;
224
225 case SB_PAGEUP:
226 ScrollBy(0, -YPage);
227 break;
228
229 case SB_TOP:
230 ScrollTo(XPos, 0);
231 break;
232
233 case SB_BOTTOM:
235 break;
236
237 case SB_THUMBPOSITION:
239 break;
240
241 case SB_THUMBTRACK:
242 {
244 if (TrackMode)
246 if (Window && HasVScrollBar)
248 break;
249 }
250 }
251}
252
253//
254/// Responds to the specified horizontal scrollEvent by calling ScrollBy or ScrollTo.
255/// The type of scroll event is identified by the corresponding SB_ constants.
256/// The 16-bit thumbPos argument is ignored. The function retrieves the 32-bit value
257/// directly from the scroll bar instead.
258//
259void
261{
262 switch (scrollEvent) {
263 case SB_LINEDOWN:
264 ScrollBy(XLine, 0);
265 break;
266
267 case SB_LINEUP:
268 ScrollBy(-XLine, 0);
269 break;
270
271 case SB_PAGEDOWN:
272 ScrollBy(XPage, 0);
273 break;
274
275 case SB_PAGEUP:
276 ScrollBy(-XPage, 0);
277 break;
278
279 case SB_TOP:
280 ScrollTo(0, YPos);
281 break;
282
283 case SB_BOTTOM:
285 break;
286
287 case SB_THUMBPOSITION:
289 break;
290
291 case SB_THUMBTRACK:
292 {
294 if (TrackMode)
296 if (Window && HasHScrollBar)
298 break;
299 }
300 }
301}
302
303//
304/// Scrolls the rectangle to the position specified in x and y after checking boundary conditions.
305/// Causes a WM_PAINT message to be sent
306///
307/// First scrolls the contents of the client area, if a portion of the client
308/// area will remain visible
309//
310void
311TScroller::ScrollTo(long x, long y)
312{
315
316 if (Window && Window->GetHandle()) {
317 long newXPos = std::max(0L, std::min(x, long(XRange - XPage)));
318 long newYPos = std::max(0L, std::min(y, long(YRange - YPage)));
319
320 if (newXPos != XPos || newYPos != YPos) {
321 //
322 // scaling isn't needed here. if condition is met, ScrollWindow()
323 // will succeed since XPage and YPage are ints
324 //
325 // if condition isn't met, ScrollWindow() is called in EndScroll()
326 // as a result of calling UpdateWindow()
327 //
328 // EndScroll() performs the necessary scaling
329 //
330 if (AutoOrg || (abs(YPos-newYPos) < YPage && abs(XPos-newXPos) < XPage))
331 Window->ScrollWindow(static_cast<int>(XPos - newXPos) * XUnit,
332 static_cast<int>(YPos - newYPos) * YUnit, nullptr, nullptr);
333
334 else
336
337 XPos = newXPos;
338 YPos = newYPos;
340 }
341 }
342}
343
344//
345/// IsAutoMode is true if automatic scrolling is activated.
346//
347bool
349{
350 return AutoMode;
351}
352
353//
354/// Performs "auto-scrolling" (dragging the mouse from within the client
355/// client area of the Window to without results in auto-scrolling when
356/// the AutoMode data member of the Scroller is true)
357///
358/// Scrolls the owner window's display in response to the mouse being dragged from
359/// inside to outside the window. The direction and the amount by which the display
360/// is scrolled depend on the current position of the mouse.
361//
362void
364{
367
368 if (AutoMode && Window) {
371 long dx = 0, dy = 0;
372
373 GetCursorPos(&cursorPos);
376
377 if (cursorPos.y < 0)
378 dy = std::min(static_cast<long>(-YLine), std::max(static_cast<long>(-YPage), long(cursorPos.y / 10 * YLine)));
379
380 else if (cursorPos.y > clientRect.bottom)
381 dy = std::max(static_cast<long>(YLine), std::min(static_cast<long>(YPage), long((cursorPos.y-clientRect.bottom)/10 * YLine)));
382
383 if (cursorPos.x < 0)
384 dx = std::min(static_cast<long>(-XLine), std::max(static_cast<long>(-XPage), long(cursorPos.x / 10 * XLine)));
385
386 else if (cursorPos.x > clientRect.right)
387 dx = std::max(static_cast<long>(XLine), std::min(static_cast<long>(XPage), (cursorPos.x-clientRect.right)/10 * XLine));
388
389 ScrollBy(dx, dy);
390 }
391}
392
393
395
396#if OWL_PERSISTENT_STREAMS
397
398//
399// reads an instance of TScroller from the passed ipstream
400//
401void*
402TScroller::Streamer::Read(ipstream& is, uint32 /*version*/) const
403{
404 TScroller* o = GetObject();
405 is >> o->XPos >> o->YPos >>
406 o->XUnit >> o->YUnit >>
407 o->XRange >> o->YRange >>
408 o->XLine >> o->YLine >>
409 o->XPage >> o->YPage >>
410 o->AutoMode >> o->TrackMode >>
411 o->AutoOrg >>
412 o->HasHScrollBar >> o->HasVScrollBar;
413
414 o->Window = 0;
415 return o;
416}
417
418//
419// writes the TScroller to the passed opstream
420//
421void
422TScroller::Streamer::Write(opstream& os) const
423{
424 TScroller* o = GetObject();
425 os << o->XPos << o->YPos <<
426 o->XUnit << o->YUnit <<
427 o->XRange << o->YRange <<
428 o->XLine << o->YLine <<
429 o->XPage << o->YPage <<
430 o->AutoMode << o->TrackMode <<
431 o->AutoOrg <<
432 o->HasHScrollBar << o->HasVScrollBar;
433}
434
435
436#endif
437
438} // OWL namespace
439
440/* ========================================================================== */
441
#define PRECONDITION(condition)
Definition checks.h:227
TDC is the root class for GDI DC wrappers.
Definition dc.h:64
virtual bool SetViewportOrg(const TPoint &origin, TPoint *oldOrg=nullptr)
Sets this DC's viewport origin to the given origin value, and saves the previous origin in oldOrg.
Definition dc.cpp:444
TPoint is a support class, derived from tagPOINT.
Definition geometry.h:87
TRect is a mathematical class derived from tagRect.
Definition geometry.h:308
Class TScroller implements the actual scroller object.
Definition scroller.h:46
int XUnit
Specifies the amount (in logical device units) to scroll the rectangle in the horizontal (X) directio...
Definition scroller.h:98
int YUnit
Specifies the amount (in logical device units) to scroll the rectangle in the vertical (Y) direction.
Definition scroller.h:100
virtual ~TScroller()
Destructs a TScroller object. Sets owning window's Scroller number variable to 0.
Definition scroller.cpp:53
long YTotalUnits
Total number of vertical scroll units.
Definition scroller.h:97
virtual void SetRange(long xRange, long yRange)
Sets the xRange and yRange of the TScroller to the parameters specified.
Definition scroller.cpp:101
bool HasHScrollBar
Is true if scroller has horizontal scroll.
Definition scroller.h:109
int YPage
Specifies the number of logical device units per page to scroll the rectangle in the vertical (Y) dir...
Definition scroller.h:105
virtual void BeginView(TDC &dc, TRect &rect)
If TScroller_AutoOrg is true (default condition), BeginView automatically offsets the origin of the l...
Definition scroller.cpp:169
TScroller(TWindow *window, int xUnit, int yUnit, long xRange, long yRange)
Constructs a TScroller object with window as the owner window, and xUnit, yUnit, xRange,...
Definition scroller.cpp:27
long YPos
Specifies the current position of the rectangle in vertical scroll units.
Definition scroller.h:93
virtual bool IsAutoMode()
IsAutoMode is true if automatic scrolling is activated.
Definition scroller.cpp:348
virtual int SetScrollPos(int bar, int pos, bool redraw=true)
Definition scroller.h:181
long XPos
Specifies the current position of the rectangle in horizontal scroll units.
Definition scroller.h:92
virtual void SetScrollPage(int bar, int page, bool redraw=true)
Definition scroller.h:146
long YRange
Specifies the number of vertical scroll units.
Definition scroller.h:95
bool AutoOrg
Is true if scroller offsets origin.
Definition scroller.h:108
int XLine
Specifies the number of logical device units per line to scroll the rectangle in the horizontal (X) d...
Definition scroller.h:102
virtual int GetScrollPos(int bar) const
Definition scroller.h:176
virtual void VScroll(uint scrollEvent, int thumbPos)
Responds to the specified vertical scrollEvent by calling ScrollBy or ScrollTo.
Definition scroller.cpp:210
virtual void SetTotalRangeOfUnits(long xTotalUnits, long yTotalUnits)
Definition scroller.cpp:113
TWindow * Window
Points to the window whose client area scroller is to be managed.
Definition scroller.h:91
virtual void HScroll(uint scrollEvent, int thumbPos)
Responds to the specified horizontal scrollEvent by calling ScrollBy or ScrollTo.
Definition scroller.cpp:260
bool TrackMode
Is true if track scrolling is activated.
Definition scroller.h:107
void ScrollBy(long dx, long dy)
Scrolls to a position calculated using the passed delta values.
Definition scroller.h:134
virtual void ScrollTo(long x, long y)
Scrolls the rectangle to the position specified in x and y after checking boundary conditions.
Definition scroller.cpp:311
virtual int GetScrollTrackPos(int bar) const
Definition scroller.h:186
virtual void SetSBarRange()
Sets the range of the owner window's scroll bars to match the range of the TScroller and repaints as ...
Definition scroller.cpp:138
long XRange
Specifies the number of horizontal scroll units.
Definition scroller.h:94
virtual void GetScrollRange(int bar, int &minPos, int &maxPos) const
Definition scroller.h:156
bool HasVScrollBar
Is true if scroller has vertical scroll.
Definition scroller.h:110
long XTotalUnits
Total number of horizontal scroll units.
Definition scroller.h:96
bool AutoMode
Is true if automatic scrolling is activated.
Definition scroller.h:106
virtual void SetUnits(int xUnit, int yUnit)
Sets the XUnit and YUnit data members to TheXUnit and TheYUnit, respectively.
Definition scroller.cpp:126
virtual void EndView()
Updates the position of the owner window's scroll bars to be coordinated with the position of the TSc...
Definition scroller.cpp:186
virtual void SetScrollRange(int bar, int minPos, int maxPos, bool redraw=true)
Definition scroller.h:161
int YLine
Specifies the number of logical device units per line to scroll the rectangle in the vertical (Y) dir...
Definition scroller.h:103
virtual void AutoScroll()
Performs "auto-scrolling" (dragging the mouse from within the client client area of the Window to wit...
Definition scroller.cpp:363
int XPage
Specifies the number of logical device units per page to scroll the rectangle in the horizontal (X) d...
Definition scroller.h:104
virtual void SetPageSize()
Sets the XPage and YPage data members (amount by which to scroll on a page scroll request) to the wid...
Definition scroller.cpp:65
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
TWindowAttr & GetWindowAttr()
Returns the TWindowAttr structure, which contains the window's creation attributes.
Definition window.h:1886
void SetScroller(TScroller *scroller)
Sets the scroller object for this window.
Definition window.cpp:788
void ScrollWindow(int dx, int dy, const TRect *scroll=nullptr, const TRect *clip=nullptr)
Scrolls a window in the vertical (dx) and horizontal (dy) directions.
Definition window.h:3057
void UpdateWindow()
Updates the client area of the specified window by immediately sending a WM_PAINT message.
Definition window.h:2901
void ScreenToClient(TPoint &point) const
Uses the screen coordinates specified in point to calculate the client window's coordinates and then ...
Definition window.h:2192
void GetClientRect(TRect &rect) const
Gets the coordinates of the window's client area and then copies them into the object referred to by ...
Definition window.cpp:3624
virtual void Invalidate(bool erase=true)
Invalidates (mark for painting) the entire client area of a window.
Definition window.h:2822
HWND GetHandle() const
Returns the handle of the window.
Definition window.h:2020
TScroller * GetScroller()
Returns the associated scroller object for this window.
Definition window.h:1867
ipstream, a specialized input stream derivative of pstream, is the base class for reading (extracting...
Definition objstrm.h:391
Definition of GDI DC encapsulation classes: TDC, TWindowDC, TScreenDC, TDesktopDC,...
#define IMPLEMENT_STREAMABLE(cls)
Definition objstrm.h:1724
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
unsigned long uint32
Definition number.h:34
bool ToBool(const T &t)
Definition defs.h:291
OWL_DIAGINFO
Definition animctrl.cpp:14
unsigned int uint
Definition number.h:25
Definition of class TScroller.
uint32 Style
Contains the values that define the style, shape, and size of your window.
Definition window.h:331
Base window class TWindow definition, including HWND encapsulation.