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
gauge.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 TGauge, a gauge user interface widget
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/gauge.h>
10#include <owl/dc.h>
11#include <owl/uimetric.h>
12#include <strsafe.h>
13#include <vector>
14
15namespace owl {
16
18
19DEFINE_RESPONSE_TABLE1(TGauge, TControl)
22
23//
24/// General constructor for a TGauge object - Use this constructor if you
25/// are creating an object whose features might require that OWL provides
26/// the underlying implementation. For example, a vertical progress bar.
27///
28/// Constructs a TGauge object with borders that are determined by using the value
29/// of SM_CXBORDER. Sets IsHorizontal to isHorizontal. Sets border thickness and
30/// spacing between dashed borders (LEDs) to 0. Sets the range of possible values
31/// from 0 to 100.
32//
33/// \note This constructor default to a LedSpacing and LedThickness of 0,
34/// which OWL interpretes as a request for a solid/filled progress bar.
35/// You must explicitly invoke 'SetLed' with non-zero values if you
36/// want otherwise.
37//
40 int id,
41 int x, int y, int w, int h,
42 bool isHorizontal,
43 int margin,
44 TModule* module)
45:
46 TControl(parent, id, title, x, y, w, h ? h : int(TUIMetric::CyVScroll), module)
47{
49 LedSpacing = 0;
50 LedThick = 0;
51}
52
53//
54/// String-aware overload
55//
57 TWindow* parent,
58 const tstring& title,
59 int id,
60 int x, int y, int w, int h,
61 bool isHorizontal,
62 int margin,
63 TModule* module
64 )
65 : TControl(parent, id, title, x, y, w, h ? h : int(TUIMetric::CyVScroll), module)
66{
68 LedSpacing = 0;
69 LedThick = 0;
70}
71
72//
73/// Simplified constructor for a TGauge object. Creates a horizontal LED style gauge.
74///
76 int id,
77 int x, int y, int w, int h,
78 TModule* module)
79:
80 TControl(parent, id, _T(""), x, y, w, h ? h : int(TUIMetric::CyVScroll), module)
81{
82 Init(true, 1);
83 LedSpacing = ((Attr.H - 2*Margin) * 2) / 3 + 2*Margin;
84 LedThick = 100 * LedSpacing / (LedSpacing - 2*Margin);
85}
86
87//
88// Constructor for a resource gauge object.
89/// \note For this constructor to work correctly, the resource control must be "OWL_Gauge". For example:
90/// \code
91/// CONTROL "ProgressBar2", IDC_PROGRESSBAR2, "OWL_Gauge", WS_CHILD | WS_VISIBLE | WS_BORDER, 64, 120, 104, 12
92/// \endcode
93//
95 int resId,
96 TModule* module)
97:
98 TControl(parent, resId, module)
99{
100 Init(true, 1);
101 LedSpacing = 10;
102 LedThick = 90;
103}
104
106{
107 Min = 0;
108 Max = 100;
109 Step = 10;
110 Value = 0;
111 Margin = margin * TUIMetric::CxBorder;
112 IsHorizontal = isHorizontal;
113 BarColor = TColor::None;
114 Attr.Style &= ~WS_TABSTOP;
115}
116
117//
118/// Returns the class name of the gauge class
119//
121{
122 return TWindowClassName{_T("OWL_Gauge")};
123}
124
125//
126/// Sets the Min and Max data members to minValue and maxValue values returned by the
127/// constructor. If Max is less than or equal to Min, SetRange resets Max to Min +
128/// 1.
129//
130void
132{
133 if (maxValue <= minValue)
134 maxValue = minValue+1;
135
136 Min = minValue;
137 Max = maxValue;
138}
139
140//
141/// Sets the BarColor data member to the value specified in color.
142//
143void
145{
146 BarColor = color;
148 BarColor = TColor::None;
149 // add
150}
151
152//
153void
158
159//
160/// Sets the Step amount of the gauge for StepIt operations
161//
162void
164{
165 Step = step;
166}
167
168//
169/// Set the value of the gauge
170//
171/// Restricts value to be within the minimum and maximum values established for the
172/// gauge. If the current value has changed, SetValue marks the old position for
173/// repainting. Then, it sets the data member Value to the new value.
174//
175void
177{
178 // Constrain value to be in the range "Min..Max"
179 //
180 if (value > Max)
181 value = Max;
182
183 else if (value < Min)
184 value = Min;
185
186 // Paint to new position, converting value to pixels
187 //
188 if (value != Value) {
189 if (GetHandle()) {
190 Invalidate(false);
191 }
192 Value = value;
193 }
194}
195
196//
197/// Changes the value of the gauge by the given delta.
198//
199void
201{
202 if (!delta)
203 return;
204
205 // Constrain delta such that Value stays in the range "Min..Max"
206 //
207 if (delta + Value > Max)
208 delta = Max - Value;
209
210 else if (delta + Value < Min)
211 delta = Min - Value;
212
213 if (GetHandle()) {
214 // Paint to new position, converting value to pixels
215 //
216 Invalidate(false);
217 }
218
219 Value += delta;
220}
221
222//
223/// Adjusts the active gauge value by the Step increment. If the new value exceeds
224/// the Max value of the gauge, StepIt wraps the setting of the gauge to its Min
225/// value.
226//
227void
229{
230 if (GetHandle()) {
231 if (Value + Step < Max)
232 DeltaValue(Step);
233 else
234 SetValue(Min);
235 }
236}
237
238//
239/// Sets the LedSpacing and LedThick data members to the values spacing and thick.
240//
241void
243{
244 LedSpacing = spacing;
245 LedThick = thickPercent;
246}
247
248//
249/// If a system control is being used, updates it to match our member settings.
250//
251void
256
257//
258/// Paints the border (bevel and margin).
259///
260/// Paints the gauge border using the specified device context. Depending on whether
261/// the border style is raised, embossed, or recessed, PaintBorder paints the
262/// specified boundary. You can override this function if you want to implement a
263/// border style that is not supported by ObjectWindows' gauges.
264//
265void
267{
271 int w = cr.right;
272 int h = cr.bottom;
273
276 dc.PatBlt(0, 0, w, yBorder);
277 dc.PatBlt(0, yBorder, xBorder, h-yBorder);
278
283
286
287 // Walk in from the bevel painting frames as we go
288 //
289 for (int i = 0; i < Margin; i++) {
291 innerRect.Inflate(-1, -1);
292 }
293}
294
295//
296/// Overrides TWindow::Paint and paints the area and the border of the gauge. Uses
297/// the values supplied in rect and dc to paint the given rectangle on the given
298/// device context. Uses the values in LedSpacing and IsHorizontal to draw a
299/// horizontal or vertical gauge with solid or broken bars.
300//
301void
302TGauge::Paint(TDC& dc, bool /*erase*/, TRect&)
303{
304 PaintBorder(dc);
305
306 // Prepare to paint the bar or LED sequence in the well
307 //
311 int w = cr.right;
312 int h = cr.bottom;
313
314 TColor ledcolor = BarColor;
318 if(bkcolor == TColor::None)
320
323 TRect innerRect(xBorder+Margin, yBorder+Margin,
324 w-xBorder-Margin, h-yBorder-Margin);
325
326 // Draw either LEDs or a solid bar as indicated by LedSpacing
327 //
328 if (LedSpacing) {
329 if (IsHorizontal) {
330 int ledStep = (innerRect.Width()*LedSpacing)/(Max-Min);
331 int ledWidth = (ledStep*LedThick)/100;
332 int gapWidth = ledStep - ledWidth;
333 int x = innerRect.left;
334 int right = innerRect.left +
335 int((long(Value-Min)*innerRect.Width())/(Max-Min));
336 for (; x < right; x += ledStep) {
337 dc.FillRect(x, innerRect.top, x+ledWidth, innerRect.bottom, barBrush);
339 }
340 dc.FillRect(x, innerRect.top, innerRect.right, innerRect.bottom, faceBrush);
341 }
342 else {
343 int ledStep = int((long(innerRect.Height())*LedSpacing)/(Max-Min));
344 int ledHeight = int((long(ledStep)*LedThick)/100);
346 int y = innerRect.bottom;
347 int top = innerRect.top + innerRect.Height() -
348 int((long(Value-Min)*innerRect.Height())/(Max-Min));
349 for (; y > top; y -= ledStep) {
350 dc.FillRect(innerRect.left, y-ledHeight, innerRect.right, y, barBrush);
352 }
353 dc.FillRect(innerRect.left, innerRect.top, innerRect.right, y, faceBrush);
354 }
355 }
356 else {
359 if (IsHorizontal) {
360 int w = int((long(Value-Min)*innerRect.Width())/(Max-Min));
361 barRect.right = emptyRect.left = innerRect.left+w;
362 }
363 else {
364 int h = innerRect.Height() -
365 int((long(Value-Min)*innerRect.Height())/(Max-Min));
366 barRect.top = emptyRect.bottom = innerRect.top+h;
367 }
370
371 LPCTSTR c = GetCaption();
372 if (c && *c) {
373 std::vector<tchar> buff(::_tcslen(c) + 20);
374 HRESULT r = StringCbPrintf(&buff[0], buff.size(), c, Value);
375 CHECK(r == S_OK); InUse(r);
376
377 int len = static_cast<int>(::_tcslen(&buff[0]));
378 TSize extent = dc.GetTextExtent(&buff[0], len);
379 int x = innerRect.left;
380 int y = innerRect.top;
381
382 if (extent.cx < innerRect.Width())
383 x += (innerRect.Width() - extent.cx) / 2; // center text horizontally
384
385 if (extent.cy < innerRect.Height())
386 y += (innerRect.Height() - extent.cy) / 2; // center text vertically
387
388 // use ExtTextOut() to paint the text in contrasting colors to the bar
389 // and background
390 //
393 dc.ExtTextOut(x, y, ETO_CLIPPED, &barRect, &buff[0], len);
395 dc.ExtTextOut(x, y, ETO_CLIPPED, &emptyRect, &buff[0], len);
396 }
397 }
398}
399
400//
401/// Overrides the TWindow::EvEraseBkgnd function and erases the background of the
402/// gauge. Whenever the background is repainted, EvEraseBkgnd is called to avoid
403/// flickering.
404//
405bool
407{
408 return true;
409}
410
411} // OWL namespace
412/* ========================================================================== */
413
#define CHECK(condition)
Definition checks.h:239
The GDI Brush class is derived from TGdiObject.
Definition gdiobjec.h:180
Class wrapper for management of color values.
Definition color.h:245
static const TColor SysHighlight
The symbolic system color value for items selected in a control.
Definition color.h:337
static const TColor Sys3dHilight
The symbolic system color value for highlighted 3-dimensional display elements (for edges facing the ...
Definition color.h:344
static const TColor None
not-a-color
Definition color.h:318
static const TColor Sys3dShadow
The symbolic system color value for the shadow regions of 3-dimensional display elements (for edges f...
Definition color.h:340
static const TColor Sys3dFace
The symbolic system color value for the face color of 3-dimensional display elements.
Definition color.h:339
TControl unifies its derived control classes, such as TScrollBar, TControlGadget, and TButton.
Definition control.h:38
TDC is the root class for GDI DC wrappers.
Definition dc.h:64
bool FrameRect(int x1, int y1, int x2, int y2, const TBrush &brush)
Draws a border on this DC around the given rectangle, rect, using the given brush,...
Definition dc.h:1781
void SelectObject(const TBrush &brush)
Selects the given GDI brush object into this DC.
Definition dc.cpp:113
bool GetTextExtent(const tstring &str, int stringLen, TSize &size) const
Computes the extent (width and height) in logical units of the text line consisting of stringLen char...
Definition dc.h:2968
bool FillRect(int x1, int y1, int x2, int y2, const TBrush &brush)
Fills the given rectangle on this DC using the specified brush.
Definition dc.h:1802
virtual bool ExtTextOut(int x, int y, uint16 options, const TRect *r, const tstring &str, int count=-1, const int *dx=nullptr)
Draws up to count characters of the given null-terminated string in the current font on this DC.
Definition dc.cpp:603
virtual TColor SetTextColor(const TColor &color)
Sets the current text color of this DC to the given color value.
Definition dc.cpp:417
int SetBkMode(int mode)
Sets the background mode to the given mode argument, which can be either OPAQUE or TRANSPARENT.
Definition dc.h:1116
bool PatBlt(int x, int y, int w, int h, uint32 rop=PATCOPY)
Definition dc.h:2448
virtual void PaintBorder(TDC &dc)
Paints the border (bevel and margin).
Definition gauge.cpp:266
void SetupWindow() override
If a system control is being used, updates it to match our member settings.
Definition gauge.cpp:252
void Paint(TDC &, bool erase, TRect &) override
Overrides TWindow::Paint and paints the area and the border of the gauge.
Definition gauge.cpp:302
void SetValue(int value)
Set the value of the gauge.
Definition gauge.cpp:176
auto GetWindowClassName() -> TWindowClassName override
Returns the class name of the gauge class.
Definition gauge.cpp:120
TGauge(TWindow *parent, LPCTSTR title, int id, int x, int y, int w, int h=0, bool isHorizontal=true, int margin=1, TModule *module=0)
General constructor for a TGauge object - Use this constructor if you are creating an object whose fe...
Definition gauge.cpp:38
void DeltaValue(int delta)
Changes the value of the gauge by the given delta.
Definition gauge.cpp:200
void StepIt()
Adjusts the active gauge value by the Step increment.
Definition gauge.cpp:228
void SetLed(int spacing, int thickPercent=90)
Sets the LedSpacing and LedThick data members to the values spacing and thick.
Definition gauge.cpp:242
void SetBkgndColor(const TColor &color)
Definition gauge.cpp:154
void SetColor(const TColor &color)
Sets the BarColor data member to the value specified in color.
Definition gauge.cpp:144
void Init(bool isHorizontal, int margin)
Definition gauge.cpp:105
bool EvEraseBkgnd(HDC)
Overrides the TWindow::EvEraseBkgnd function and erases the background of the gauge.
Definition gauge.cpp:406
void SetRange(int minValue, int maxValue)
Sets the Min and Max data members to minValue and maxValue values returned by the constructor.
Definition gauge.cpp:131
void SetStep(int step)
Sets the Step amount of the gauge for StepIt operations.
Definition gauge.cpp:163
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
The tagSIZE struct is defined as.
Definition geometry.h:234
TUIMetric encapsulates the GetSystemMetric() API.
Definition uimetric.h:32
static const TUIMetric CyBorder
Definition uimetric.h:40
static const TUIMetric CxBorder
Definition uimetric.h:39
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
void SetBkgndColor(TColor color, bool shouldUpdate=true)
Sets the background color for the window.
Definition window.h:1925
auto GetBkgndColor() const -> TColor
Returns the overriding background color set for the window.
Definition window.h:1913
LPCTSTR GetCaption() const
Returns the Title member of TWindow.
Definition window.h:1900
TRect GetClientRect() const
Gets the coordinates of the window's client area (the area in a window you can use for drawing).
Definition window.h:2217
virtual void SetupWindow()
Performs setup following creation of an associated MS-Windows window.
Definition window.cpp:2575
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
#define _tcslen
Definition cygwin.h:74
#define _T(x)
Definition cygwin.h:51
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
Definition of class TGauge, a gauge control encapsulation & implementation.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
void InUse(const T &arg)
Handy utility to avoid compiler warnings about unused parameters.
Definition defs.h:299
OWL_DIAGINFO
Definition animctrl.cpp:14
END_RESPONSE_TABLE
Definition button.cpp:26
std::string tstring
Definition defs.h:79
EV_WM_ERASEBKGND
Definition docking.cpp:965
Definition of TUIMetric, a UI metrics provider class.