OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
slider.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of TSlider, slider UI widget abstract base class.
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/slider.h>
10#include <owl/dc.h>
11#include <owl/commctrl.h>
12
13namespace owl {
14
16
17
18DEFINE_RESPONSE_TABLE1(TSlider, TScrollBar)
22
23//
24/// Constructs a slider object setting Pos and ThumbRgn to 0, TicGap to Range
25/// divided by 10, SlotThick to 17, Snap to true, and Sliding to false. Sets Attr.W
26/// and Attr.H to the values in X and Y. ThumbResId is set to thumbResId.
27//
28TSlider::TSlider(TWindow* parent, int id, int x, int y, int w, int h, TResId thumbResId, TModule* module)
29:
30 TScrollBar(parent, id, x, y, w, h, true, module),
31 ThumbResId(thumbResId),
32 ThumbRect(0, 0, 0, 0) // This will get setup when bitmap is loaded.
33{
35 SetRange(0, 100);
36 Pos = 0;
37 ThumbRgn = nullptr;
38 TicGap = Range/10; // Setup 10 evenly spaced tics by default, no array
39 Tics = nullptr;
40 TicCount = 0;
41 SlotThick = 4; // Default for all sliders
42 Snap = false;
43 SelStart = SelEnd = 0;
44
45 Sliding = false;
46}
47
48//
49/// Constructor for a TSlider object created from resource
50//
52:
53 TScrollBar(parent, resId, module),
54 ThumbResId(thumbResId),
55 ThumbRect(0, 0, 0, 0) // This will get setup when bitmap is loaded.
56{
58 SetRange(0, 100);
59 Pos = 0;
60 ThumbRgn = nullptr;
61 TicGap = Range/10; // Setup 10 evenly spaced tics by default, no array.
62 Tics = nullptr;
63 TicCount = 0;
64 SlotThick = 4; // Default for all sliders
65 Snap = false;
66 SelStart = SelEnd = 0;
67
68 Sliding = false;
69}
70
71//
72/// Constructs a slider object to encapsulate (alias) an existing control.
73//
75:
76 TScrollBar(hWnd, module),
77 ThumbResId(0),
78 ThumbRect(0, 0, 0, 0) // This will get setup when bitmap is loaded.
79{
81 int rMin = static_cast<int>(SendMessage(TBM_GETRANGEMIN));
82 int rMax = static_cast<int>(SendMessage(TBM_GETRANGEMAX));
84 Pos = static_cast<int>(SendMessage(TBM_GETPOS));
86
87 ThumbRgn = nullptr;
88 TicGap = Range/10; // Setup 10 evenly spaced tics by default, no array.
89 Tics = nullptr;
90 TicCount = 0;
91 SlotThick = 4; // Default for all sliders
92 Snap = TicGap > 0;
93 SelStart = SelEnd = 0;
94
95 Sliding = false;
96}
97
98//
99/// Destructs a TSlider object and deletes ThumbRgn.
100//
102{
103 delete[] Tics;
104 delete ThumbRgn;
105}
106
107//
108/// Checks and sets the slider range.
109/// Sets the slider to the range between minValue and maxValue. Overloads TScrollBar's virtual
110/// function.
111//
112void
114{
115 Min = minValue;
116 Max = maxValue;
117 if (Max > Min)
118 Range = Max - Min;
119 else if (Min > Max)
120 Range = Min - Max;
121 else
122 Range = 1;
123
124 if (GetHandle()) {
125// TParam2 p2 = (Attr.Style & TBS_VERT) ? MkParam2(maxValue, minValue) : MkParam2(minValue, maxValue);
127 SendMessage(TBM_SETRANGE, redraw, p2); // Swapped for vertical
128 }
129}
130
131//
132/// Sets the position of the thumb and always redraws.
133/// Moves the thumb to the position specified in thumbPos. If thumbPos is outside
134/// the present range of the slider, the thumb is moved to the closest position
135/// within the specified range. Overloads TScrollBar's virtual function.
136///
137/// Always redraws.
138//
139void
141{
142 SetPosition(pos, true);
143}
144
145//
146/// Sets the position of the thumb and always redraws.
147/// Moves the thumb to the position specified in thumbPos. If thumbPos is outside
148/// the present range of the slider, the thumb is moved to the closest position
149/// within the specified range. Overloads TScrollBar's virtual function.
150///
151/// Redraw is optional.
152//
153void
155{
156 // Constrain pos to be in the range "Min .. Max" & snap to tics if enabled
157 //
158 pos = SnapPos(pos);
159
160 // Slide thumb to new position, converting pos to pixels
161 //
162 if (GetHandle()) {
164 }
165 Pos = pos;
166}
167
168//
169/// Sets the slider's ruler. Each slider has a built-in ruler that is drawn with the
170/// slider. The ruler, which can be blank or have tick marks on it, can be created
171/// so that it forces the thumb to snap to the tick positions automatically.
172/// \note Snapping is not supported in native currently
173//
174void
176{
177 TicGap = ticGap;
178 Snap = snap;
179 delete[] Tics;
180 Tics = nullptr;
181
182 if (GetHandle()) {
184 }
185}
186
187//
188/// Sets the ruler's custom tics and snap. Snapping is not currently supported in
189/// native.
190//\ todo need to add multple tics support
191//
192void
194{
195 PRECONDITION(tics || ticCount == 0); // A 0 tics array is only OK if no tics
196
197 // Alloc the array if the size is different, or we dont have one. Then copy
198 // the tic positions
199 //
200 if (ticCount > TicCount || !Tics) {
201 delete[] Tics;
202 Tics = ticCount ? new int[ticCount] : nullptr;
203 }
204 for (int i = 0; i < ticCount; i++)
205 if (tics[i] >= Min && tics[i] <= Max) // Ignore out of range tics
206 Tics[i] = tics[i];
207 TicCount = ticCount;
208
209 Snap = snap;
210
211 if (GetHandle()) {
213 for (int i = 0; i < TicCount; i++)
215 }
216}
217
218//
219/// Set a selection range for the slider. Requires that TBS_ENABLESELRANGE style
220/// attribute be set.
221//
222void
223TSlider::SetSel(int start, int end, bool redraw)
224{
225 // Save selection state
226 //
227 SelStart = start;
228 SelEnd = end;
229
230 if (GetHandle()) {
232 }
233}
234
235//
236// Get the selection range from the slider.
237// Requires that TBS_ENABLESELRANGE style attribute be set.
238//
239void
240TSlider::GetSel(int& start, int& end)
241{
242 if (GetHandle()) {
243 start = static_cast<int>(SendMessage(TBM_GETSELSTART));
244 end = static_cast<int>(SendMessage(TBM_GETSELEND));
245 }
246 // Resync selection state
247 //
248 SelStart = start;
249 SelEnd = end;
250}
251
252//----------------------------------------------------------------------------
253// Protected implementation
254
255//
256/// Returns the windows system class name that this slider is basing itself on.
257//
262
263//
264/// Calls TScrollBar::SetupWindow and SetupThumbRgn to set up the slider window.
265//
266void
268{
270
271
272 SetRange(Min, Max);
273 if (Tics)
274 SetRuler(Tics, TicCount, Snap);
275 else
276 SetRuler(TicGap, Snap);
277 SetSel(SelStart, SelEnd, false);
278 SetPosition(Pos, true);
279
280}
281
282//
283/// Constrains pos so it is in the range from Min to Max and (if snapping is
284/// enabled) performs snapping by rounding pos to the nearest TicGap.
285//
286int
288{
289 if (pos > Max)
290 pos = Max;
291
292 else if (pos < Min)
293 pos = Min;
294
295 return Snap && TicGap > 0 ? (((pos-Min)+TicGap/2)/TicGap)*TicGap + Min : pos;
296}
297
298//
299/// Overrides the handling in TScrollBar.
300/// In particular, TSlider does not support the code in TScrollBar to retrieve 32-bit positions,
301/// so we here simply resort to the 16-bit position value passed with the TB_THUMBPOSITION and
302/// TB_THUMBTRACK notifications.
303//
304// TODO: For Windows Vista and later, handle the TRBN_THUMBPOSCHANGING notification instead.
305// This new notification carries 32-bit data in the accompanying structure NMTRBTHUMBPOSCHANGING.
306//
307void
309{
310 PRECONDITION(hCtl == GetHandle()); // Only handle messages for ourselves.
311 switch (scrollCode)
312 {
313 case TB_THUMBPOSITION:
315 break;
316
317 case TB_THUMBTRACK:
319 break;
320
321 default:
323 }
324}
325
326//
327/// Overrides the handling in TScrollBar.
328/// See EvHScroll for details.
329//
330void
332{
333 // Simply forward to EvHScroll, since the code is identical.
334 //
336}
337
338
340
341#if OWL_PERSISTENT_STREAMS
342
343//
344//
345//
346void*
347TSlider::Streamer::Read(ipstream& is, uint32 version) const
348{
349 TSlider* o = GetObject();
351 is >> o->Min
352 >> o->Max
353 >> o->Pos
354 >> o->ThumbResId
355 >> o->ThumbRect;
356 if (version == 1) {
358 is >> tmpRect; // dummy CaretRect for compatibilty with stream v1
359 }
360 is >> o->TicGap
361 >> o->Snap;
362
363 // In stream version 2 and above, a tic array with count is written, as well
364 // as a selection range
365 //
366 if (version >= 2) {
367 is >> o->TicCount;
368 o->Tics = o->TicCount ? new int[o->TicCount] : 0;
369 for (int i = 0; i < o->TicCount; i++)
370 is >> o->Tics[i];
371
372 is >> o->SelStart
373 >> o->SelEnd;
374 }
375
376 o->SetRange(o->Min, o->Max); // let it calculate Range
377 return o;
378}
379
380//
381//
382//
383void
384TSlider::Streamer::Write(opstream& os) const
385{
386 TSlider* o = GetObject();
387 WriteBaseObject((TScrollBar*)o, os);
388 os << o->Min
389 << o->Max
390 << o->Pos
391 << o->ThumbResId
392 << o->ThumbRect
393 << o->TicGap
394 << o->Snap
395 << o->TicCount;
396
397 // In stream version 2 and above, a tic array with count is written, as well
398 // as a selection range
399 //
400 os << o->TicCount;
401 for (int i = 0; i < o->TicCount; i++)
402 os << o->Tics[i];
403 int d1,d2;
404 o->GetSel(d1,d2); // Force retrieval of current selection before writing
405 os << o->SelStart
406 << o->SelEnd;
407}
408
409#endif
410
411} // OWL namespace
412/* ========================================================================== */
413
#define PRECONDITION(condition)
Definition checks.h:227
ObjectWindows dynamic-link libraries (DLLs) construct an instance of TModule, which acts as an object...
Definition module.h:75
TRect is a mathematical class derived from tagRect.
Definition geometry.h:308
TScrollBar objects represent standalone vertical and horizontal scroll bar controls.
Definition scrollba.h:41
virtual void SBThumbPosition(int thumbPos)
Calls SetPosition to move the thumb.
Definition scrollba.cpp:322
void SetupWindow() override
Sets the scroll bar's range to 0, 100. To redefine this range, call SetRange.
Definition scrollba.cpp:130
virtual void SBThumbTrack(int thumbPos)
Calls SetPosition to move the thumb as it is being dragged to a new position.
Definition scrollba.cpp:331
void EvHScroll(uint scrollCode, uint thumbPos, HWND hWndCtl)
Response table handler that calls the virtual function (SBBottom, SBLineDown and so on) in response t...
Definition scrollba.cpp:375
An abstract base class derived from TScrollBar, TSlider defines the basic behavior of sliders (contro...
Definition slider.h:55
void SetRange(int minValue, int maxValue, bool redraw=true) override
Checks and sets the slider range.
Definition slider.cpp:113
void SetSel(int start, int end, bool redraw=true)
Set a selection range for the slider.
Definition slider.cpp:223
~TSlider() override
Destructs a TSlider object and deletes ThumbRgn.
Definition slider.cpp:101
void SetPosition(int thumbPos)
Sets the position of the thumb and always redraws.
Definition slider.cpp:140
void GetSel(int &start, int &end)
Definition slider.cpp:240
TSlider(TWindow *parent, int id, int x, int y, int w, int h, TResId thumbResId, TModule *module=0)
Constructs a slider object setting Pos and ThumbRgn to 0, TicGap to Range divided by 10,...
Definition slider.cpp:28
void EvVScroll(uint scrollCode, uint thumbPos, HWND hWndCtl)
Overrides the handling in TScrollBar.
Definition slider.cpp:331
int SnapPos(int pos)
Constrains pos so it is in the range from Min to Max and (if snapping is enabled) performs snapping b...
Definition slider.cpp:287
void SetRuler(int ticGap, bool snap=false)
Sets the slider's ruler.
Definition slider.cpp:175
void SetupWindow() override
Calls TScrollBar::SetupWindow and SetupThumbRgn to set up the slider window.
Definition slider.cpp:267
auto GetWindowClassName() -> TWindowClassName override
Returns the windows system class name that this slider is basing itself on.
Definition slider.cpp:258
void EvHScroll(uint scrollCode, uint thumbPos, HWND hWndCtl)
Overrides the handling in TScrollBar.
Definition slider.cpp:308
Type-safe encapsulation of a Windows class name, a union between ATOM and LPCTSTR.
Definition module.h:47
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
TResult SendMessage(TMsgId, TParam1=0, TParam2=0) const
Sends a message (msg) to a specified window or windows.
Definition window.cpp:3288
HWND THandle
TWindow encapsulates an HWND.
Definition window.h:418
HWND GetHandle() const
Returns the handle of the window.
Definition window.h:2020
ipstream, a specialized input stream derivative of pstream, is the base class for reading (extracting...
Definition objstrm.h:391
Definition of classes for CommonControl encapsulation.
Definition of GDI DC encapsulation classes: TDC, TWindowDC, TScreenDC, TDesktopDC,...
#define DEFINE_RESPONSE_TABLE1(cls, base)
Macro to define a response table for a class with one base.
Definition eventhan.h:492
#define IMPLEMENT_ABSTRACT_STREAMABLE1(cls, base1)
Definition objstrm.h:1719
void ReadBaseObject(Base *base, ipstream &in)
Definition objstrm.h:1159
void WriteBaseObject(Base *base, opstream &out)
Definition objstrm.h:1150
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
void InitializeCommonControls(uint controlFlags)
Wrapper for the Windows API function InitCommmonControlsEx.
Definition commctrl.cpp:19
EV_WM_HSCROLL
Definition notetab.cpp:55
uint32 MkUint32(uint16 lo, uint16 hi)
Definition defs.h:261
unsigned long uint32
Definition number.h:34
TParam2 MkParam2(const T1 &lo, const T2 &hi)
Definition dispatch.h:65
OWL_DIAGINFO
Definition animctrl.cpp:14
END_RESPONSE_TABLE
Definition button.cpp:26
unsigned short uint16
Definition number.h:33
EV_WM_VSCROLL
Definition scrollba.cpp:18
unsigned int uint
Definition number.h:25
Definition of classes TSlider & TVSlider.