OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
textgadg.cpp
Go to the documentation of this file.
1//------------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1992, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of TTextGadget.
7/// Implementation of TDynamicTextGadget.
8//------------------------------------------------------------------------------
9#include <owl/pch.h>
10#include <owl/textgadg.h>
11#include <owl/gadgetwi.h>
12#include <owl/uimetric.h>
13#include <owl/uihelper.h>
14
15namespace owl {
16
18
19//
20/// Constructs a TTextGadget object with the specified ID, border style, and
21/// alignment. Sets Margins.Left and Margins.Right to 2. Sets Text and TextLen to 0.
22/// Unless a font is passed (or a null-pointer is passed) the gadget will use the
23/// font of the containing gadget window.
24//
30 TFont* font)
31:
33 Font(font)
34{
35 Margins.Left = Margins.Right = TUIMetric::CxFixedFrame;
36 Align = align;
37 NumChars = numChars;
38 Text = nullptr;
39 TextLen = 0;
40 // init colors to same as reset methods
43
44 SetShrinkWrap(false, true);
46}
47
48//
49/// String-aware overload
50//
52 int id,
56 const tstring& text,
57 TFont* font
58 )
59 : TGadget(id, borderStyle),
60 Font(font)
61{
62 Margins.Left = Margins.Right = TUIMetric::CxFixedFrame;
63 Align = align;
64 NumChars = numChars;
65 Text = nullptr;
66 TextLen = 0;
67 // init colors to same as reset methods
70
71 SetShrinkWrap(false, true);
73}
74
75//
76/// Destruct this text gadget and delete the text it is holding
77//
79{
80 delete[] Text;
81 delete Font;
82}
83
84//
85/// Invalidate the working portion of this gadget--in this case just the
86/// InnerRect
87///
88/// Calls TGadget::GetInnerRect to compute the area of the text for the gadget and
89/// then TGadget::InvalidateRect to invalidate the rectangle in the parent window.
90void
99
100//
101/// Returns the effective font used to render the text for this gadget.
102/// If no font was passed to the constructor, the font of the gadget window is returned.
103//
104const TFont&
106{
108 return Font ? *Font : GetGadgetWindow()->GetFont();
109}
110
111//
112/// Sets the font to be used by the gadget. If repaint is true, calls
113/// TGadgetWindow::GadgetChangedSize to recalculate the size of the gadget.
114//
115void
117{
118 delete Font;
119 Font = new TFont(font);
120 if (GetGadgetWindow() && repaint)
122}
123
124//
125/// Set the text for this gadget
126//
127/// If the text stored in Text is not the same as the new text, SetText deletes the
128/// text stored in Text. Then, it sets TextLen to the length of the new string. If
129/// no text exists, it sets both Text and TextLen to 0 and then calls Invalidate to
130/// invalidate the rectangle.
131//
132void
134{
135 // Skip processing if new text is the same as current
136 //
137 if (text && Text && _tcscmp(text, Text) == 0)
138 return;
139
140 delete[] Text;
141
142 if (text) {
143 Text = strnewdup(text);
144 TextLen = static_cast<uint>(::_tcslen(Text));
145 }
146 else {
147 Text = nullptr;
148 TextLen = 0;
149 }
150
151 if (GetGadgetWindow())
153
154 Invalidate();
155}
156
157//
158/// Respond to the virtual call to let this gadget's Window know how big this
159/// text gadget wants to be based on the text size.
160//
161/// If shrink-wrapping is requested, GetDesiredSize returns the size needed to
162/// accommodate the borders, margins, and text; otherwise, if shrink-wrapping is not
163/// requested, it returns the gadget's current width and height.
164//
165void
167{
169 const TFont& f = GetFont();
170
171 if (ShrinkWrapWidth)
172 size.cx += f.GetTextExtent(Text).cx;
173 else {
174 int left, right, top, bottom;
175 GetOuterSizes(left, right, top, bottom);
176
177 int newW = f.GetMaxWidth() * NumChars;
178 size.cx += newW + left + right - Bounds.Width(); // Old bounds already considered
179 }
180
181 if (ShrinkWrapHeight)
182 size.cy += f.GetHeight() + 2;
183}
184
185//
186/// Paint the text gadget by painting gadget borders, & then painting text in
187/// the InnerRect. Empty or 0 text blanks the gadget.
188//
189/// Calls TGadget::PaintBorder to paint the border. Calls TGadget::GetInnerRect to
190/// calculate the area of the text gadget's rectangle. If the text is left-aligned,
191/// Paint calls dc.GetTextExtent to compute the width and height of a line of the
192/// text. To set the background color, Paint calls dc.GetSysColor and sets the
193/// default background color to face shading (COLOR_BTNFACE). To set the button text
194/// color, Paint calls dc.SetTextColor and sets the default button text color to
195/// COLOR_BTNTEXT. To draw the text, Paint calls dc.ExtTextOut and passes the
196/// parameters ETO_CLIPPED (so the text is clipped to fit the rectangle) and
197/// ETO_OPAQUE (so the rectangle is filled with the current background color).
198//
199void
201{
202 PaintBorder(dc);
203
206
207 dc.SelectObject(GetFont());
208
210 if(!GetEnabled())
212
214 if(!Text){
215 if (!transparent)
216 {
219 dc.SetBkColor(color);
220 }
221 }
222 else
223 {
224 // Create a UI Face object for this button & let it paint the button face
225 //
228 if (GetGadgetWindow()->GetStyle() & SS_NOPREFIX)
230 TUIFace face(innerRect, Text, BkgndColor, format);
231
232 TPoint dstPt(innerRect.TopLeft());
233 dc.SetBkColor(BkgndColor);
235 if (!GetEnabled())
236 face.Paint(dc, dstPt, TUIFace::Disabled, false, !transparent);
237 else
238 face.Paint(dc, dstPt, TUIFace::Normal, false, !transparent);
240 }
241 dc.RestoreFont();
242}
243
244//
245/// Resets the background color of the gadget to the default TColor::Sys3dFace
246//
248{
249 BkgndColor = TColor::Sys3dFace;
250}
251
252//
253/// Resets the text color of the gadget to the default TColor::SysBtnText
254//
256{
257 TextColor = TColor::SysBtnText;
258}
259
260//------------------------------------------------------------------------------
261// class TDynamicTextGadgetEnabler
262// ~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~
263//
264class TDynamicTextGadgetEnabler : public TCommandEnabler {
265 public:
266 TDynamicTextGadgetEnabler(TWindow::THandle hReceiver,
268 :
270 Gadget(g)
271 {
272 }
273
274 // Override TCommandEnabler virtuals
275 //
276 void Enable(bool);
277 void SetText(LPCTSTR);
278 void SetCheck(int){}
279
280 protected:
281 TDynamicTextGadget* Gadget;
282};
283//
284void TDynamicTextGadgetEnabler::Enable(bool enable)
285{
287 if(Gadget->GetEnabled() != enable)
288 Gadget->SetEnabled(enable);
289}
290//
291void TDynamicTextGadgetEnabler::SetText(LPCTSTR text)
292{
293 if(!Gadget->GetText() || _tcscmp(Gadget->GetText(), text) != 0)
294 Gadget->SetText(text);
295}
296//
297//--------------------------------------------------------
298// TDynamicTextGadget
299//
305
306//
307// String-aware overload
308//
310 int id,
311 TBorderStyle style,
314 const tstring& text,
315 TFont* font
316 )
317 : TTextGadget(id, style, align, numChars, text, font)
318{}
319
320//
322{
324
325 // Must send, not post here, since a ptr to a temp is passed
326 //
327 // This might be called during idle processing before the
328 // HWND has created. Therefore, confirm handle exists.
329 //
330 if (GetGadgetWindow()->GetHandle()){
331 TDynamicTextGadgetEnabler ge(*GetGadgetWindow(), this);
333 }
334}
335//
336} // OWL namespace
337/* ========================================================================== */
338
#define PRECONDITION(condition)
Definition checks.h:227
Class wrapper for management of color values.
Definition color.h:245
static const TColor SysBtnText
The symbolic system color value for the text on buttons.
Definition color.h:342
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 Sys3dFace
The symbolic system color value for the face color of 3-dimensional display elements.
Definition color.h:339
Base class for an extensible interface for auto enabling/disabling of commands (menu items,...
Definition window.h:209
uint GetId() const
Retrieves the id of the command.
Definition window.h:1695
virtual void Enable(bool enable=true)
Enables or disables the command sender.
Definition window.cpp:301
TDC is the root class for GDI DC wrappers.
Definition dc.h:64
void SelectObject(const TBrush &brush)
Selects the given GDI brush object into this DC.
Definition dc.cpp:113
bool TextRect(int x1, int y1, int x2, int y2)
Fills the given rectangle, clipping any text to the rectangle.
Definition dc.h:1878
virtual TColor SetBkColor(const TColor &color)
Sets the current background color of this DC to the given color value or the nearest available.
Definition dc.cpp:405
virtual void RestoreFont()
Restores the original GDI font object to this DC.
Definition dc.cpp:246
virtual TColor SetTextColor(const TColor &color)
Sets the current text color of this DC to the given color value.
Definition dc.cpp:417
Small class will send EvEnable to window, You can set text by handling EvCommandEnable message.
Definition textgadg.h:113
TDynamicTextGadget(int id=0, TBorderStyle=Recessed, TAlign=Left, uint numChars=10, LPCTSTR text=0, TFont *font=0)
Definition textgadg.cpp:300
virtual void CommandEnable()
Definition textgadg.cpp:321
TFont derived from TGdiObject provides constructors for creating font objects from explicit informati...
Definition gdiobjec.h:296
int GetHeight() const
Returns the height of the font.
Definition gdiobjec.h:1173
TSize GetTextExtent(const tstring &text) const
Returns the extent of a given string using this particular font.
Definition font.cpp:194
int GetMaxWidth() const
Returns the maximum width of the characters in the font.
Definition gdiobjec.h:1205
TGadget is the base class for the following derived gadget classes:
Definition gadget.h:120
void GetOuterSizes(int &left, int &right, int &top, int &bottom)
Get the four total outer sizes in pixels which consists of the margins plus the borders.
Definition gadget.cpp:496
void GetInnerRect(TRect &rect)
Computes the area of the gadget's rectangle excluding the borders and margins.
Definition gadget.cpp:514
void InvalidateRect(const TRect &rect, bool erase=true)
Invalidate a rectangle in our containing window.
Definition gadget.cpp:392
TGadgetWindow * GetGadgetWindow()
Return a pointer to the owning or parent window for the gadget.
Definition gadget.h:536
bool GetEnabled() const
Determines whether keyboard and mouse input have been enabled for the specified gadget.
Definition gadget.h:458
void SetShrinkWrap(bool shrinkWrapWidth, bool shrinkWrapHeight)
Simple set accessor to set whether shrinkwrapping is performed horizontally and/or vertically.
Definition gadget.cpp:165
TBorderStyle
Gadget border styles.
Definition gadget.h:127
virtual void SetEnabled(bool enabled)
Enables or disables keyboard and mouse input for the gadget.
Definition gadget.cpp:194
virtual void GetDesiredSize(TSize &size)
Request by the gadget window to query the gadget's desired size.
Definition gadget.cpp:479
virtual void PaintBorder(TDC &dc)
Self sent by method Paint().
Definition gadget.cpp:432
static uint GetFlatStyle()
Returns the flat style.
Definition gadgetwi.h:459
@ FlatXPTheme
Windows XP theme styles.
Definition gadgetwi.h:198
void GadgetChangedSize(TGadget &gadget)
Used to notify the gadget window that a gadget has changed its size, GadgetChangedSize calls LayoutSe...
const TFont & GetFont() const
Returns the font being used by this gadget window.
Definition gadgetwi.h:423
TPoint is a support class, derived from tagPOINT.
Definition geometry.h:87
TRect is a mathematical class derived from tagRect.
Definition geometry.h:308
The tagSIZE struct is defined as.
Definition geometry.h:234
Derived from TGadget, TTextGadget is a text gadget object.
Definition textgadg.h:37
TAlign
Enumerates the text-alignment attributes.
Definition textgadg.h:42
~TTextGadget()
Destruct this text gadget and delete the text it is holding.
Definition textgadg.cpp:78
void Invalidate()
Invalidate the working portion of this gadget–in this case just the InnerRect.
Definition textgadg.cpp:91
LPCTSTR GetText() const
Returns the text for the gadget.
Definition textgadg.h:144
void Paint(TDC &dc)
Paint the text gadget by painting gadget borders, & then painting text in the InnerRect.
Definition textgadg.cpp:200
virtual TColor GetEnabledColor() const
Definition textgadg.h:148
void GetDesiredSize(TSize &size)
Respond to the virtual call to let this gadget's Window know how big this text gadget wants to be bas...
Definition textgadg.cpp:166
void ResetTextColor()
Resets the text color of the gadget to the default TColor::SysBtnText.
Definition textgadg.cpp:255
void ResetBkgndColor()
Resets the background color of the gadget to the default TColor::Sys3dFace.
Definition textgadg.cpp:247
void SetFont(const TFont &, bool repaint=true)
Sets the font to be used by the gadget.
Definition textgadg.cpp:116
void SetText(LPCTSTR text)
Set the text for this gadget.
Definition textgadg.cpp:133
TTextGadget(int id=0, TBorderStyle=Recessed, TAlign=Left, uint numChars=10, LPCTSTR text=0, TFont *font=0)
Constructs a TTextGadget object with the specified ID, border style, and alignment.
Definition textgadg.cpp:25
const TFont & GetFont() const
Returns the effective font used to render the text for this gadget.
Definition textgadg.cpp:105
TUIFace assists in painting UI elements in various states.
Definition uihelper.h:367
@ Disabled
Disabled or Unavailable state (embossed 3d no color)
Definition uihelper.h:376
@ Normal
Normal state.
Definition uihelper.h:373
static const TUIMetric CxFixedFrame
Definition uimetric.h:41
TResult HandleMessage(TMsgId, TParam1=0, TParam2=0)
Dispatches the given message using the response table.
Definition window.cpp:1392
HWND THandle
TWindow encapsulates an HWND.
Definition window.h:418
#define _tcscmp
Definition cygwin.h:75
#define _tcslen
Definition cygwin.h:74
#define WM_COMMAND_ENABLE
Definition dispatch.h:4103
Definition of TGadgetList, TGadgetWindow & TGadgetWindowFont A list holding gadgets,...
char * strnewdup(const char *s, size_t minAllocSize=0)
Definition memory.cpp:25
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
LPARAM TParam2
Second parameter type.
Definition dispatch.h:55
OWL_DIAGINFO
Definition animctrl.cpp:14
std::string tstring
Definition defs.h:79
unsigned int uint
Definition number.h:25
Definition of text gadget class TGadget.
Definition of the UI Helper Classes: TUIHandle, TUIBorder, TUIFace, TUIPart.
Definition of TUIMetric, a UI metrics provider class.