OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
font.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 TFont, an encapsulation of the GDI Font object
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/gdiobjec.h>
10#include <owl/uimetric.h>
11
12#if !defined(DEFAULT_GUI_FONT) // May not be defined by 16-bit headers
13# define DEFAULT_GUI_FONT 17
14#endif
15
16#if defined(__BORLANDC__)
17# pragma option -w-ccc // Disable "Condition is always true/false"
18#endif
19
20using namespace std;
21
22namespace owl {
23
25DIAG_DECLARE_GROUP(OwlGDI); // General GDI diagnostic group
26DIAG_DECLARE_GROUP(OwlGDIOrphan); // Oprhan control tracing group
27
28//
29// Constructors
30//
31
32//
33/// Creates a TFont object and sets the Handle data member to the given borrowed
34/// handle. The ShouldDelete data member defaults to false, ensuring that the
35/// borrowed handle will not be deleted when the C++ object is destroyed.
36//
44
45//
46/// Creates a TFont object with the given values.
47//
49 int height, int width, int escapement, int orientation,
50 int weight,
54{
58 pitchAndFamily, facename.length() > 0 ? facename.c_str() : nullptr);
59 WARNX(OwlGDI, !Handle, 0, _T("Cannot create TFont (") << facename <<
60 _T(" ") << height << _T("pt)"));
61 CheckValid();
63}
64
65//
66/// Creates a TFont object with the given values. The constructor parameter list and
67/// default values match the Windows API CreateFont call.
68//
69TFont::TFont(int height, int width, int escapement, int orientation,
70 int weight,
77 const tstring& facename)
78{
82 pitchAndFamily, facename.length() > 0 ? facename.c_str() : nullptr);
83 WARNX(OwlGDI, !Handle, 0, _T("Cannot create TFont (") << facename <<
84 _T(" ") << height << _T("pt)"));
85 CheckValid();
87}
88
89//
90/// Creates a TFont object from the given LOGFONT.
91//
93{
95 WARNX(OwlGDI, !Handle, 0, "Cannot create TFont from logfont @" << static_cast<const void*>(&logFont));
96 CheckValid();
98}
99
100#if defined(OWL5_COMPAT)
101
102//
103/// Creates a TFont object from the given LOGFONT.
104/// This overload is deprecated; use the overload for a LOGFONT reference instead.
105//
107{
109 Handle = ::CreateFontIndirect((LPLOGFONT)logFont); // API typecast
110 WARNX(OwlGDI, !Handle, 0, _T("Cannot create TFont from logfont @") << static_cast<const void*>(logFont));
111 CheckValid();
113}
114
115#endif
116
117//
118/// Creates an object that shares the source object's font handle.
119///
120/// If the source object owns the underlying font handle, this object will share it, i.e. calling
121/// this constructor is equivalent to the call `TFont (src.GetHandle(), true)`. If the source
122/// object does not own the handle, neither will this object, i.e. this constructor is equivalent
123/// to the call `TFont (src.GetHandle(), false)`.
124///
125/// \note This contructor does not create a new font. To duplicate the underlying font instead call
126/// `TFont (src.GetObject())`, which will invoke `TFont (const LOGFONT&)`.
127//
129{
130#if !defined(NO_GDI_SHARE_HANDLES)
131 ShouldDelete = src.ShouldDelete;
132 Handle = src.Handle;
133 if (src.ShouldDelete)
135#else
137 src.GetObject(logFont);
139 WARNX(OwlGDI, !Handle, 0, _T("Cannot create TFont from TFont @") <<
140 hex << uint32(LPVOID(&src)));
141 CheckValid();
143#endif
144}
145
146//
147/// Retrieves information about this font when selected in the specified dc.
148//
149void
151{
152 dc.SelectObject(*this);
153 dc.GetTextMetrics(tm);
154 dc.RestoreFont();
155}
156
157//
158/// Retrieves information about this font when selected in the specified dc.
159//
162{
164 GetTextMetrics(tm, dc);
165 return tm;
166}
167
168//
169/// Retrieves information about this font when selected in a screen DC.
170//
171void
173{
174 TScreenDC dc;
175 GetTextMetrics(tm, dc);
176}
177
178//
179/// Retrieves information about this font when selected in a screen DC.
180//
183{
185 TScreenDC dc;
186 GetTextMetrics(tm, dc);
187 return tm;
188}
189
190//
191/// Returns the extent of a given string using this particular font.
192//
193TSize
195{
196 TScreenDC dc;
197 return GetTextExtent(dc, text);
198}
199
200//
201/// Returns the extent of a given string using this particular font in a
202/// particular DC.
203//
204TSize
206{
207 dc.SelectObject(*this);
208 TSize size = dc.GetTextExtent(text, static_cast<int>(text.length())); // TODO: Widen parameter type to avoid this narrowing cast.
209 dc.RestoreFont();
210 return size;
211}
212
213//
214/// Returns the font's name.
215/// Throws TXGdi on failure.
216//
219{
220 return GetObject().lfFaceName;
221}
222
223//
224/// Returns the pitch and family flags for this logical font.
225/// Throws TXGdi on failure.
226//
227uint8
229{
230 return GetObject().lfPitchAndFamily;
231}
232
233//
234/// Returns information about this font object.
235/// Throws TXGdi on failure.
236//
239{
240 LOGFONT lf;
241 bool r = TGdiObject::GetObject(sizeof(lf), &lf) != 0;
242 if (!r) throw TXGdi(IDS_GDIFAILURE, Handle);
243 return lf;
244}
245
246namespace {
247
248 //
249 // Returns the default system font for the given GUI element.
250 //
252 {
253 switch (element)
254 {
256 {
257 TFont f(reinterpret_cast<HFONT>(::GetStockObject(DEFAULT_GUI_FONT)));
258 return f.GetObject();
259 }
260
262 {
263 ICONMETRICS im = {sizeof(im)};
264 bool r = SystemParametersInfo(SPI_GETICONMETRICS, sizeof(im), &im, 0);
265 CHECK(r); InUse(r);
266 return im.lfFont;
267 }
268
269 default:
270 {
273 (element == TDefaultGuiFont::sfiCaption) ? &NONCLIENTMETRICS::lfCaptionFont :
274 (element == TDefaultGuiFont::sfiSmallCaption) ? &NONCLIENTMETRICS::lfSmCaptionFont :
275 (element == TDefaultGuiFont::sfiMenu) ? &NONCLIENTMETRICS::lfMenuFont :
276 (element == TDefaultGuiFont::sfiStatus) ? &NONCLIENTMETRICS::lfStatusFont :
277 (element == TDefaultGuiFont::sfiMessage) ? &NONCLIENTMETRICS::lfMessageFont :
278 nullptr;
279 CHECK(font);
280
281 NONCLIENTMETRICS ncm = {sizeof(ncm)};
283 CHECK(r); InUse(r);
284 return ncm.*font;
285 }
286 }
287 }
288
289} // namespace
290
291//
292/// Create a wrapper for the given system font.
293/// See TSystemFontId for valid arguments.
294/// If no argument is passed, sfiLegacyDefault is used.
295//
300
301} // OWL namespace
302/* ========================================================================== */
303
#define CHECK(condition)
Definition checks.h:239
#define WARNX(group, condition, level, message)
Definition checks.h:277
#define PRECONDITION(condition)
Definition checks.h:227
#define DIAG_DECLARE_GROUP(group)
Definition checks.h:404
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 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
virtual void RestoreFont()
Restores the original GDI font object to this DC.
Definition dc.cpp:246
bool GetTextMetrics(TEXTMETRIC &metrics) const
Fills the metrics structure with metrics data for the current font on this DC.
Definition dc.h:3175
TSystemFontId
Defines the various GUI elements for which a default font can be created.
Definition gdiobjec.h:387
@ sfiCaption
Represents NONCLIENTMETRICS::lfCaptionFont.
Definition gdiobjec.h:390
@ sfiLegacyDefault
Represents the font returned by GetStockObject (DEFAULT_GUI_FONT).
Definition gdiobjec.h:388
@ sfiIcon
Represents ICONMETRICS::lfFont.
Definition gdiobjec.h:389
TDefaultGuiFont(TSystemFontId=sfiLegacyDefault)
Create a wrapper for the given system font.
Definition font.cpp:296
TFont derived from TGdiObject provides constructors for creating font objects from explicit informati...
Definition gdiobjec.h:296
tstring GetFaceName() const
Returns the font's name.
Definition font.cpp:218
uint8 GetPitchAndFamily() const
Returns the pitch and family flags for this logical font.
Definition font.cpp:228
LOGFONT GetObject() const
Returns information about this font object.
Definition font.cpp:238
TSize GetTextExtent(const tstring &text) const
Returns the extent of a given string using this particular font.
Definition font.cpp:194
TEXTMETRIC GetTextMetrics() const
Retrieves information about this font when selected in a screen DC.
Definition font.cpp:182
TFont(HFONT handle, TAutoDelete autoDelete=NoAutoDelete)
TFont encapsulates an HFONT.
Definition font.cpp:37
GdiObject is the root, pseudo-abstract base class for ObjectWindows' GDI (Graphics Device Interface) ...
Definition gdiobjec.h:68
void CheckValid(uint resId=IDS_GDIFAILURE)
Definition gdibase.cpp:49
int GetObject(int count, void *object) const
Retrieve the object's attributes into a buffer.
Definition gdiobjec.h:1051
bool ShouldDelete
Should object delete GDI handle in dtor?
Definition gdibase.h:82
static void RefAdd(HANDLE handle, TType type)
RefAdd adds a reference entry for the object with the given handle and type.
Definition gdiobjec.cpp:137
HANDLE Handle
GDI handle of this object.
Definition gdibase.h:81
Derived from TWindowDC, TScreenDC is a DC class that provides direct access to the screen bitmap.
Definition dc.h:610
The tagSIZE struct is defined as.
Definition geometry.h:234
Describes an exception resulting from GDI failures such as creating too many TWindow device contexts ...
Definition gdibase.h:52
#define _T(x)
Definition cygwin.h:51
#define DEFAULT_GUI_FONT
Definition font.cpp:13
Definition of abstract GDI object class and derived classes.
TAutoDelete
Flag for Handle ctors to control Handle deletion in dtor.
Definition gdibase.h:70
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
unsigned char uint8
Definition number.h:32
unsigned long uint32
Definition number.h:34
void InUse(const T &arg)
Handy utility to avoid compiler warnings about unused parameters.
Definition defs.h:299
OWL_DIAGINFO
Definition animctrl.cpp:14
std::string tstring
Definition defs.h:79
Definition of TUIMetric, a UI metrics provider class.