OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
dc.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 class TDC
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/dc.h>
10
11namespace owl {
12
14DIAG_DECLARE_GROUP(OwlGDI); // General GDI diagnostic group
15
16//
17/// Sets OrgBrush, OrgPen, OrgFont, OrgBitmap, and OrgPalette to 0, and sets
18/// ShouldDelete to true. This function is for internal use by the TDC constructors.
19//
20void
22{
23 OrgBrush = nullptr;
24 OrgPen = nullptr;
25 OrgFont = nullptr;
26 OrgPalette = nullptr;
27 OrgBitmap = nullptr;
28 OrgTextBrush = 0;
29}
30
31//
32/// Creates a DC object "borrowing" the handle of an existing DC. The Handle data
33/// member is set to the given handle argument.
34//
36:
38{
39 Init();
40 TRACEX(OwlGDI, OWL_CDLEVEL, _T("TDC constructed @") << (void*)this <<
41 _T(" from handle ") << static_cast<void*>(handle));
42}
43
44//
45// Following two ctors are for use by derived classes only
46
47/// Constructor for use by derived classes only
48//
50{
51 Init();
52 TRACEX(OwlGDI, OWL_CDLEVEL, _T("TDC constructed @") << (void*)this);
53}
54
55//
56/// Constructor for use by derived classes only
57//
59:
61{
62 Init();
63 TRACEX(OwlGDI, OWL_CDLEVEL, _T("TDC constructed @") << (void*)this <<
64 _T(" from handle") << static_cast<void*>(handle));
65}
66
67//
68/// Default dtor does not delete Handle
69/// Calls RestoreObjects.
70//
72{
74 TRACEX(OwlGDI, OWL_CDLEVEL, _T("TDC destructed @") << (void*)this);
75}
76
77//
78/// Returns the attributes of the DC object.
79//
80HDC
82{
83 return HDC(Handle);
84}
85
86//
87/// Selects the given GDI pen object into this DC. The previously selected pen is saved in
88/// the protected data member OrgPen, and can be restored by a call to RestorePen.
89//
90void
92{
93 TRACEX(OwlGDI, 1, _T("TDC::SelectPen @") << (void*)this <<
94 _T(" pen @") << (void*)&pen);
95 HPEN oldPen = static_cast<HPEN>(::SelectObject(GetHDC(), pen));
96 if (oldPen) {
98 if (reinterpret_cast<UINT_PTR>(oldPen) > 1)
99 {
100 if (!OrgPen)
101 OrgPen = oldPen;
102 else
104 }
105 }
106}
107
108//
109/// Selects the given GDI brush object into this DC. The previously selected brush is saved in
110/// the protected data member OrgBrush, and can be restored by a call to RestoreBrush.
111//
112void
114{
115 TRACEX(OwlGDI, 1, _T("TDC::SelectBrush @") << (void*)this <<
116 _T(" brush @") << (void*)&brush);
117 HBRUSH oldBrush = reinterpret_cast<HBRUSH>(::SelectObject(GetHDC(), brush));
118 if (oldBrush) {
120 if (reinterpret_cast<UINT_PTR>(oldBrush) > 1)
121 {
122 if (!OrgBrush)
123 OrgBrush = oldBrush;
124 else
126 }
127 }
128}
129
130//
131/// Selects the given GDI font object into this DC. The previously selected font is saved in
132/// the protected data member OrgFont, and can be restored by a call to RestoreFont.
133//
134void
136{
137 TRACEX(OwlGDI, 1, _T("TDC::SelectFont @") << (void*)this <<
138 _T(" font @") << (void*)&font);
139 HFONT oldFont = reinterpret_cast<HFONT>(::SelectObject(GetHDC(), font));
140 if (oldFont) {
142 if (reinterpret_cast<UINT_PTR>(oldFont) > 1)
143 {
144 if (!OrgFont)
145 OrgFont = oldFont;
146 else
148 }
149 }
150}
151
152//
153/// Selects the given GDI palette object into this DC. The previously selected palette is saved in
154/// the protected data member OrgPalette, and can be restored by a call to RestorePalette.
155///
156/// If `forceBackgound` is set to `false` (the default), the selected logical palette is a
157/// foreground palette when the window has input focus. If `forceBackground` is `true`, the
158/// selected palette is always a background palette, whether the window has focus or not.
159//
160void
162{
163 TRACEX(OwlGDI, 1, _T("TDC::SelectPalette @") << (void*)this <<
164 _T(" palette @") << (void*)&palette);
166 if (oldPalette) {
168 if (reinterpret_cast<UINT_PTR>(oldPalette) > 1)
169 {
170 if (!OrgPalette)
171 OrgPalette = oldPalette;
172 else
174 }
175 }
176}
177
178//
179/// Selects the given GDI bitmap object into this DC. The previously selected bitmap is saved in
180/// the protected data member OrgBitmap, and can be restored by a call to RestoreBitmap.
181//
182void
184{
185 TRACEX(OwlGDI, 1, _T("TDC::SelectBitmap @") << (void*)this <<
186 _T(" bitmap @") << (void*)&bitmap);
188 if (oldBitmap) {
190 if (reinterpret_cast<UINT_PTR>(oldBitmap) > 1)
191 {
192 if (!OrgBitmap)
193 OrgBitmap = oldBitmap;
194 else
196 }
197 }
198}
199
200//
201/// Selects into the DC a predefined stock pen, brush, font, or palette.
202/// For more information about the available stock objects and their indexes, see MSDN:
203/// http://msdn.microsoft.com/en-us/library/windows/desktop/dd144925.aspx
204//
205void
207{
209 TRACEX(OwlGDI, 1, _T("TDC::SelectStockObject @") << (void*)this <<
210 _T(" index ") << index);
212 if (reinterpret_cast<UINT_PTR>(oldObj) > 1)
214}
215
216//
217/// Restores the original GDI pen object to this DC.
218//
219void
221{
222 TRACEX(OwlGDI, 1, _T("TDC::RestorePen @") << (void*)this);
223 if (OrgPen) {
224 TGdiObject::RefDec(::SelectObject(GetHDC(), OrgPen), false);
225 OrgPen = nullptr;
226 }
227}
228
229//
230/// Restores the original GDI brush object to this DC.
231//
232void
234{
235 TRACEX(OwlGDI, 1, _T("TDC::RestoreBrush @") << (void*)this);
236 if (OrgBrush) {
237 TGdiObject::RefDec(::SelectObject(GetHDC(), OrgBrush), false);
238 OrgBrush = nullptr;
239 }
240}
241
242//
243/// Restores the original GDI font object to this DC.
244//
245void
247{
248 TRACEX(OwlGDI, 1, _T("TDC::RestoreFont @") << (void*)this);
249 if (OrgFont) {
250 TGdiObject::RefDec(::SelectObject(GetHDC(), OrgFont), false);
251 OrgFont = nullptr;
252 }
253}
254
255//
256/// Restores the original GDI palette object to this DC.
257//
258void
260{
261 TRACEX(OwlGDI, 1, _T("TDC::RestorePalette @") << (void*)this);
262 if (OrgPalette) {
263 TGdiObject::RefDec(::SelectPalette(GetHDC(), OrgPalette, false), false);
264 OrgPalette = nullptr;
265 }
266}
267
268//
269/// Restores the original GDI bitmap object into this DC.
270//
271void
273{
274 TRACEX(OwlGDI, 1, _T("TDC::RestoreBitmap @") << (void*)this);
275 if (OrgBitmap) {
276 TGdiObject::RefDec(::SelectObject(GetHDC(), OrgBitmap), false);
277 OrgBitmap = nullptr;
278 }
279}
280
281//
282/// Restores the original GDI text brush object to this DC.
283//
284void
286{
287 TRACEX(OwlGDI, 1, _T("TDC::RestoreTextBrush @") << (void*)this);
288 if (OrgTextBrush) {
289 TGdiObject::RefDec(::SelectObject(GetHDC(), OrgTextBrush), false);
290 OrgTextBrush = 0;
291 }
292}
293
294//
295/// Restores all the original GDI objects to this DC.
296//
297void
299{
300 if (Handle) {
301 RestorePen();
302 RestoreBrush();
303 RestoreFont();
307 }
308}
309
310//
311/// Returns a handle to the currently selected object of the given objectType
312/// associated with this DC. Returns 0 if the call fails. objectType can be OBJ_PEN,
313/// OBJ_BRUSH, OBJ_PAL, OBJ_FONT, or OBJ_BITMAP.
314//
315/// Subset of Win32 GetCurrentObject , just a straight
316/// call for normal win32
317//
318HANDLE
320{
321 return ::GetCurrentObject(GetHDC(), objectType);
322}
323
324// !CQ add ROP arg to allow this to be used by docking drag frame painting
325
326//
327/// Draws a frame of the specified size and thickness with the given brush. The old
328/// brush is restored after completion.
329//
330void
332{
334
335 int width = r.Width() - xThick;
336 int height = r.Height() - yThick;
337
338 PatBlt(r.left, r.top, xThick, height, rop); // left
339 PatBlt(r.left+xThick, r.top, width, yThick, rop); // top
340 PatBlt(r.left, r.top+height, width, yThick, rop); // bottom
341 PatBlt(r.left+width, r.top+yThick, xThick, height, rop); // right
342
343 RestoreBrush();
344}
345
346//
347/// Saves the current state of this DC on a context stack. The saved state can be
348/// restored later with RestoreDC. Returns a value specifying the saved DC or 0 if
349/// the call fails.
350//
351int
353{
354 return ::SaveDC(GetHDC());
355}
356
357//
358/// Restores the given savedDC. Returns true if the context is successfully
359/// restored; otherwise, returns false.
360//
361bool
363{
364 return ::RestoreDC(GetHDC(), savedIndex);
365}
366
367//
368/// Used under WIN3.1 or later, GetDeviceCaps returns capability information about
369/// this DC. The index argument specifies the type of information required.
370/// \todo Document the possible values for the index
371//
372int
373TDC::GetDeviceCaps(int index) const
374{
375 return ::GetDeviceCaps(GetAttributeHDC(), index);
376}
377
378//
379/// Functional style overload
380//
383{
384 TEXTMETRIC t;
385 bool r = GetTextMetrics(t);
386 if (!r) throw TXGdi(IDS_GDIFAILURE, GetHDC());
387 return t;
388}
389
390//
391/// Updates this DC using data in the given devMode structure. Returns true if the
392/// call is successful; otherwise, returns false.
393//
394bool
396{
397 return ::ResetDC(GetHDC(), &devMode) != nullptr;
398}
399
400//
401/// Sets the current background color of this DC to the given color value or the
402/// nearest available. Returns 0x80000000 if the call fails.
403//
404TColor
406{
407 if (GetHDC() != GetAttributeHDC())
409 return ::SetBkColor(GetAttributeHDC(), color);
410}
411
412//
413/// Sets the current text color of this DC to the given color value. The text color
414/// determines the color displayed by TDC::TextOut and TDC::ExtTextOut.
415//
416TColor
418{
419 if (GetHDC() != GetAttributeHDC())
421 return ::SetTextColor(GetAttributeHDC(), color);
422}
423
424//
425/// Sets the current window mapping mode of this DC to mode. Returns the previous
426/// mapping mode value. The mapping mode defines how logical coordinates are mapped
427/// to device coordinates. It also controls the orientation of the device's x- and
428/// y-axes. See TDC::GetMapMode for a complete list of mapping modes.
429//
430int
432{
433 if (GetHDC() != GetAttributeHDC())
434 ::SetMapMode(GetHDC(), mode);
435 return ::SetMapMode(GetAttributeHDC(), mode);
436}
437
438//
439/// Sets this DC's viewport origin to the given origin value, and saves the previous
440/// origin in oldOrg. Returns true if the call is successful; otherwise, returns
441/// false.
442//
443bool
445{
446 if (GetHDC() != GetAttributeHDC())
448 return ::SetViewportOrgEx(GetAttributeHDC(), point.x, point.y, oldOrg);
449}
450
451//
452/// Modifies this DC's viewport origin relative to the current values. The delta x-
453/// and y-components are added to the previous origin and the resulting point
454/// becomes the new viewport origin. The previous origin is saved in oldOrg. Returns
455/// true if the call is successful; otherwise, returns false.
456//
457bool
459{
460 if (GetHDC() != GetAttributeHDC())
462 return ::OffsetViewportOrgEx(GetAttributeHDC(), delta.x, delta.y, oldOrg);
463}
464
465//
466/// Sets this DC's viewport x- and y-extents to the given extent values. The
467/// previous extents are saved in oldExtent. Returns true if the call is successful;
468/// otherwise, returns false. The extent value determines the amount of stretching
469/// or compression needed in the logical coordinate system to fit the device
470/// coordinate system. extent also determines the relative orientation of the two
471/// coordinate systems.
472//
473bool
475{
476 if (GetHDC() != GetAttributeHDC())
478 return ::SetViewportExtEx(GetAttributeHDC(), extent.cx, extent.cy, oldExtent);
479}
480
481//
482/// Modifies this DC's viewport extents relative to the current values. The new
483/// extents are derived as follows:
484/// \code
485/// xNewVE = (xOldVE * xNum)/ xDenom
486/// yNewVE = (I * yNum)/ yDenom
487/// \endcode
488/// The previous extents are saved in oldExtent. Returns true if the call is
489/// successful; otherwise, returns false.
490//
491bool
494{
495 if (GetHDC() != GetAttributeHDC())
497 return ::ScaleViewportExtEx(GetAttributeHDC(), xNum, xDenom, yNum, yDenom, oldExtent);
498}
499
500//
501/// Sets the origin of the window associated with this DC to the given origin value,
502/// and saves the previous origin in oldOrg. Returns true if the call is successful;
503/// otherwise, returns false.
504//
505bool
507{
508 if (GetHDC() != GetAttributeHDC())
510 return ::SetWindowOrgEx(GetAttributeHDC(), point.x, point.y, oldOrg);
511}
512
513//
514/// Modifies this DC's window origin relative to the current values. The delta x-
515/// and y-components are added to the previous origin and the resulting point
516/// becomes the new window origin. The previous origin is saved in oldOrg. Returns
517/// true if the call is successful; otherwise, returns false.
518//
519bool
521{
522 if (GetHDC() != GetAttributeHDC())
524 return ::OffsetWindowOrgEx(GetAttributeHDC(), delta.x, delta.y, oldOrg);
525}
526
527//
528/// Sets this DC's window x- and y-extents to the given extent values. The previous
529/// extents are saved in oldExtent. Returns true if the call is successful;
530/// otherwise, returns false. The extent value determines the amount of stretching
531/// or compression needed in the logical coordinate system to fit the device
532/// coordinate system. extent also determines the relative orientation of the two
533/// coordinate systems.
534//
535bool
537{
538 if (GetHDC() != GetAttributeHDC())
540 return ::SetWindowExtEx(GetAttributeHDC(), extent.cx, extent.cy, oldExtent);
541}
542
543//
544/// Modifies this DC's window extents relative to the current values. The new
545/// extents are derived as follows:
546/// \code
547/// xNewWE = (xOldWE * xNum)/ xDenom
548/// yNewWE = (yOldWE * yNum)/ yDenom
549/// \endcode
550/// The previous extents are saved in oldExtent. Returns true if the call is
551/// successful; otherwise, returns false.
552//
553bool
555{
556 if (GetHDC() != GetAttributeHDC())
558 return ::ScaleWindowExtEx(GetAttributeHDC(), xNum, xDenom, yNum, yDenom, oldExtent);
559}
560
561//
562/// Draws up to count characters of the given null-terminated string in the current
563/// font on this DC. If count is -1 (the default), the entire string is written.
564/// The (x, y) or p arguments specify the logical coordinates of the reference point
565/// that is used to align the first character, depending on the current
566/// text-alignment mode. This mode can be inspected with TDC::GetTextAlign and
567/// changed with TDC::SetTextAlign. By default, the current position is neither used
568/// nor altered by TextOut. However, the align mode can be set to TA_UPDATECP, which
569/// makes Windows use and update the current position. In this mode, TextOut ignores
570/// the reference point argument(s).
571/// TextOut returns true if the call is successful; otherwise, it returns false.
572//
573bool
574TDC::TextOut(int x, int y, const tstring& str, int count)
575{
576 return ::TextOut(GetHDC(), x, y, str.c_str(), count>=0 ? count : static_cast<int>(str.length()));
577}
578
579//
580/// Draws up to count characters of the given null-terminated string in the current
581/// font on this DC. If count is -1, the whole string is written.
582/// The count (or the string length, if count is -1) may not exceed 8192.
583/// An optional rectangle r can be specified for clipping, opaquing, or both, as
584/// determined by the options value. If options is set to ETO_CLIPPED, the rectangle
585/// is used for clipping the drawn text. If options is set to ETO_OPAQUE, the
586/// current background color is used to fill the rectangle. Both options can be used
587/// if ETO_CLIPPED is OR'd with ETO_OPAQUE.
588/// The (x, y) orp arguments specify the logical coordinates of the reference point
589/// that is used to align the first character. The current text-alignment mode can
590/// be inspected with TDC::GetTextAlign and changed with TDC::SetTextAlign. By
591/// default, the current position is neither used nor altered by ExtTextOut.
592/// However, if the align mode is set to TA_UPDATECP, ExtTextOut ignores the
593/// reference point argument(s) and uses or updates the current position as the
594/// reference point.
595/// The dx argument is an optional array of values used to set the distances between
596/// the origins (upper left corners) of adjacent character cells. For example, dx[i]
597/// represents the number of logical units separating the origins of character cells
598/// i and i+1. If dx is 0, ExtTextOut uses the default inter-character spacings.
599/// ExtTextOut returns true if the call is successful; otherwise, it returns false.
600/// \see http://msdn.microsoft.com/en-us/library/dd162713.aspx
601//
602bool
604 const tstring& str, int count, const int * dx)
605{
606 const int strLength = static_cast<int>(str.length());
607 const int realCount = count >= 0 ? count : strLength;
608 PRECONDITION(realCount <= std::min(strLength, 8192));
609 return ::ExtTextOut(GetHDC(), x, y, options, rect, str.c_str(), realCount, dx);
610}
611
612//
613/// Draws up to count characters of the given null-terminated string in the current
614/// font on this DC. If count is -1, the whole string is written.
615/// Tabs are expanded according to the given arguments. The positions array
616/// specifies numPositions tab stops given in device units. The tab stops must have
617/// strictly increasing values in the array. If numPositions and positions are both
618/// 0, tabs are expanded to eight times the average character width. If numPositions
619/// is 1, all tab stops are taken to be positions[0] apart. tabOrigin specifies the
620/// x-coordinate in logical units from which tab expansion will start.
621/// The p argument specifies the logical coordinates of the reference point that is
622/// used to align the first character depending on the current text-alignment mode.
623/// This mode can be inspected with TDC::GetTextAlign and changed with
624/// TDC::SetTextAlign. By default, the current position is neither used nor altered
625/// by TabbedTextOut. However, if the align mode is set to TA_UPDATECP,
626/// TabbedTextOut ignores the reference point argument(s) and uses/updates the
627/// current position as the reference point.
628/// The size argument in the second version of TabbedTextOut reports the dimensions
629/// (size.y = height and size.x = width) of the string in logical units.
630/// TabbedTextOut returns true if the call is successful; otherwise, it returns
631/// false.
632//
633bool
634TDC::TabbedTextOut(const TPoint& p, const tstring& str, int count,
635 int numPositions, const int* positions, int tabOrigin, TSize& size)
636{
637 LONG r = ::TabbedTextOut(GetHDC(), p.x, p.y, str.c_str(),
638 count >= 0 ? count : static_cast<int>(str.length()),
640 const_cast<int*>(positions), // Cast for legacy compatibility.
641 tabOrigin);
642 size = TSize(static_cast<DWORD>(r));
643 return r != 0;
644}
645
646//
647/// Overload for const TRect&
648/// For obvious reasons, this overload does not support the DT_CALCRECT format flag.
649/// If the given format contains the DT_CALCRECT flag, the function returns 0.
650/// Otherwise, see the documentation for the overload for non-const TRect.
651//
652int
653TDC::DrawText(const tstring& str, int count, const TRect& rect, uint16 format)
654{
655 if ((format & DT_CALCRECT) != 0) return 0;
656 TRect r = rect;
657 return DrawText(str, count, r, format); // Forward to virtual overload for non-const TRect.
658}
659
660//
661/// Formats and draws in the given rectangle, r, up to count characters of the
662/// null-terminated string using the current font for this DC. If count is -1, the
663/// whole string is written. The rectangle must be specified in logical units.
664/// Formatting is controlled with the format argument, which can be various
665/// combinations of the following values:
666/// - \c \b DT_BOTTOM Specifies bottom-justified text. This value must be combined (bitwise
667/// OR'd) with DT_SINGLELINE.
668/// - \c \b DT_CALCRECT Determines the width and height of the rectangle. If there are
669/// multiple lines of text, DrawText uses the width of r (the rectangle argument)
670/// and extends the base of the rectangle to bound the last line of text. If there
671/// is only one line of text, DrawText uses a modified value for the right side of r
672/// so that it bounds the last character in the line. In both cases, DrawText
673/// returns the height of the formatted text but does not draw the text.
674/// - \c \b DT_CENTER Centers text horizontally.
675/// - \c \b DT_EXPANDTABS Expands tab characters. The default number of characters per tab
676/// is eight.
677/// - \c \b DT_EXTERNALLEADING Includes the font external leading in line height. Normally,
678/// external leading is not included in the height of a line of text.
679/// - \c \b DT_LEFT Aligns text flush-left.
680/// - \c \b DT_NOCLIP Draws without clipping. DrawText is somewhat faster when DT_NOCLIP is
681/// used.
682/// - \c \b DT_NOPREFIX Turns off processing of prefix characters. Normally, DrawText
683/// interprets the prefix character & as a directive to underscore the character
684/// that follows, and the prefix characters && as a directive to print a single &.
685/// By specifying DT_NOPREFIX, this processing is turned off.
686/// - \c \b DT_RIGHT Aligns text flush-right.
687/// - \c \b DT_SINGLELINE Specifies single line only. Carriage returns and linefeeds do not
688/// break the line.
689/// - \c \b DT_TABSTOP Sets tab stops. Bits 15-8 (the high-order byte of the low-order
690/// word) of the format argument are the number of characters for each tab. The
691/// default number of characters per tab is eight.
692/// - \c \b DT_TOP Specifies top-justified text (single line only).
693/// - \c \b DT_VCENTER Specifies vertically centered text (single line only).
694/// - \c \b DT_WORDBREAK Specifies word breaking. Lines are automatically broken between
695/// words if a word would extend past the edge of the rectangle specified by r. A
696/// carriage return/line sequence will also break the line.
697///
698/// \note Note that the DT_CALCRECT, DT_EXTERNALLEADING, DT_INTERNAL, DT_NOCLIP, and
699/// DT_NOPREFIX values cannot be used with the DT_TABSTOP value.
700/// DrawText uses this DC's currently selected font, text color, and background
701/// color to draw the text. Unless the DT_NOCLIP format is used, DrawText clips the
702/// text so that it does not appear outside the given rectangle. All formatting is
703/// assumed to have multiple lines unless the DT_SINGLELINE format is given.
704/// If the selected font is too large for the specified rectangle, DrawText does not
705/// attempt to substitute a smaller font.
706/// If the function succeeds, the return value is the height of the text. If the
707/// function fails, the return value is zero.
708//
709int
710TDC::DrawText(const tstring& str, int count, TRect& rect, uint16 format)
711{
712 return ::DrawText(GetHDC(), str.c_str(), count, &rect, format);
713}
714
715//
716/// Formats and draws in the given rectangle, r, up to count characters of the
717/// null-terminated string using the current font for this DC. If count is -1, the
718/// whole string is written. The rectangle must be specified in logical units.
719/// Formatting is controlled with the format argument, which can be various
720/// combinations of the following values:
721/// - \c \b DT_BOTTOM Specifies bottom-justified text. This value must be combined (bitwise
722/// OR'd) with DT_SINGLELINE.
723/// - \c \b DT_CALCRECT Determines the width and height of the rectangle. If there are
724/// multiple lines of text, DrawText uses the width of r (the rectangle argument)
725/// and extends the base of the rectangle to bound the last line of text. If there
726/// is only one line of text, DrawText uses a modified value for the right side of r
727/// so that it bounds the last character in the line. In both cases, DrawText
728/// returns the height of the formatted text but does not draw the text.
729/// - \c \b DT_CENTER Centers text horizontally.
730/// - \c \b DT_EDITCONTROL Duplicates the text-displaying characteristics of a multiline
731/// edit control. Specifically, the average character width is calculated in the
732/// same manner as for an edit control, and the function does not display a
733/// partially visible last line.
734/// - \c \b DT_END_ELLIPSIS or DT_PATH_ELLIPSIS Replaces part of the given string with
735/// ellipses, if necessary, so that the result fits in the specified rectangle. The
736/// given string is not modified unless the DT_MODIFYSTRING flag is specified.You
737/// can specify DT_END_ELLIPSIS to replace characters at the end of the string, or
738/// - \c \b DT_PATH_ELLIPSIS to replace characters in the middle of the string. If the
739/// string contains backslash (\‍) characters, DT_PATH_ELLIPSIS preserves as much as
740/// possible of the text after the last backslash.
741/// - \c \b DT_EXPANDTABS Expands tab characters. The default number of characters per tab
742/// is eight.
743/// - \c \b DT_EXTERNALLEADING Includes the font external leading in line height. Normally,
744/// external leading is not included in the height of a line of text.
745/// - \c \b DT_INTERNAL Uses the system font to calculate text metrics.
746/// - \c \b DT_LEFT Aligns text flush-left.
747/// - \c \b DT_MODIFYSTRING Modifies the given string to match the displayed text. This flag
748/// has no effect unless the DT_END_ELLIPSIS or DT_PATH_ELLIPSIS flag is specified.
749/// - \c \b DT_NOCLIP Draws without clipping. DrawText is somewhat faster when DT_NOCLIP is
750/// used.
751/// - \c \b DT_NOPREFIX Turns off processing of prefix characters. Normally, DrawText
752/// interprets the prefix character & as a directive to underscore the character
753/// that follows, and the prefix characters && as a directive to print a single &.
754/// By specifying DT_NOPREFIX, this processing is turned off.
755/// - \c \b DT_RIGHT Aligns text flush-right.
756/// - \c \b DT_RTLREADING Layout in right to left reading order for bi-directional text when
757/// the font selected into the hdc is a Hebrew or Arabic font. The default reading
758/// order for all text is left to right.
759/// - \c \b DT_SINGLELINE Specifies single line only. Carriage returns and linefeeds do not
760/// break the line.
761/// - \c \b DT_TABSTOP Sets tab stops. Bits 15-8 (the high-order byte of the low-order
762/// word) of the format argument are the number of characters for each tab. The
763/// default number of characters per tab is eight.
764/// - \c \b DT_TOP Specifies top-justified text (single line only).
765/// - \c \b DT_VCENTER Specifies vertically centered text (single line only).
766/// - \c \b DT_WORDBREAK Specifies word breaking. Lines are automatically broken between
767/// words if a word would extend past the edge of the rectangle specified by r. A
768/// carriage return/line sequence will also break the line.
769/// - \c \b DT_WORD_ELLIPSIS Truncates text that does not fit in the rectangle and adds
770/// ellipses.
771///
772/// \note Note that the DT_CALCRECT, DT_EXTERNALLEADING, DT_INTERNAL, DT_NOCLIP, and
773/// DT_NOPREFIX values cannot be used with the DT_TABSTOP value.
774/// DrawText uses this DC's currently selected font, text color, and background
775/// color to draw the text. Unless the DT_NOCLIP format is used, DrawText clips the
776/// text so that it does not appear outside the given rectangle. All formatting is
777/// assumed to have multiple lines unless the DT_SINGLELINE format is given.
778/// If the selected font is too large for the specified rectangle, DrawText does not
779/// attempt to substitute a smaller font.
780/// If the function succeeds, the return value is the height of the text. If the
781/// function fails, the return value is zero.
782/// Windows NT: To get extended error information, call GetLastError.
783//
784int
787{
788 if (params)
789 params->cbSize = sizeof(DRAWTEXTPARAMS);
790 return ::DrawTextEx(GetHDC(), str, count, rect, format, params);
791}
792
793//
794//
795//
796bool //JBC
798{
799 RECT rect;
800 rect.left = lpRect.Left();
801 rect.top = lpRect.Top();
802 rect.right = lpRect.Right();
803 rect.bottom = lpRect.Bottom();
804 return ::DrawFrameControl(static_cast<HDC>(Handle), &rect, nType, nState);
805}
806
807//
808/// Draws in the given rectangle (r) up to count characters of gray text from string
809/// using the given brush, brush, and the current font for this DC. If count is -1
810/// and string is null-terminated, the whole string is written. The rectangle must
811/// be specified in logical units. If brush is 0, the text is grayed with the same
812/// brush used to draw window text on this DC. Gray text is primarily used to
813/// indicate disabled commands and menu items.
814/// GrayString writes the selected text to a memory bitmap, grays the bitmap, then
815/// displays the result. The graying is performed regardless of the current brush
816/// and background color.
817/// The outputFunc pointer to a function can specify the procedure instance of an
818/// application-supplied drawing function and is defined as
819/// \code
820/// typedef BOOL (CALLBACK* GRAYSTRINGPROC)(HDC, LPARAM, int);
821/// \endcode
822/// If outputFunc is 0, GrayString uses TextOut and string is assumed to be a
823/// normal, null-terminated character string. If string cannot be handled by TextOut
824/// (for example, the string is stored as a bitmap), you must provide a suitable
825/// drawing function via outputFunc.
826/// If the device supports a solid gray color, it is possible to draw gray strings
827/// directly without using GraySring. Call GetSysColor to find the color value; for
828/// example, G of COLOR_GRAYTEXT. If G is nonzero (non-black), you can set the text
829/// color with SetTextColor(G) and then use any convenient text-drawing function.
830/// GrayString returns true if the call is successful; otherwise, it returns false.
831/// Failure can result if TextOut or outputFunc return false, or if there is
832/// insufficient memory to create the bitmap.
833//
834bool
836 const tstring& str, int count, const TRect& rect)
837{
838 return ::GrayString(GetHDC(), brush, outputFunc, reinterpret_cast<LPARAM>(str.c_str()),
839 count >= 0 ? count : 0, rect.left, rect.top, rect.Width(), rect.Height());
840}
841
842//
843// For use with CopyText.
844//
845struct TGetTextFace
846{
847 const TDC& dc;
848 TGetTextFace(const TDC& d) : dc(d) {}
849
850 int operator() (LPTSTR buf, int buf_size)
851 {return dc.GetTextFace(buf_size - 1, buf);}
852};
853
856{
857 return CopyText(GetTextFaceLength(), TGetTextFace(*this));
858}
859
860// DLN (stripped from TUIHandle)
861// Draws an edge using system ::DrawEdge if available, otherwise
862// does it the hard way
863bool
864TDC::DrawEdge(const TRect& frame, uint edge, uint flags)
865{
866 static int hasDrawEdge = true;
867
868 // Try once to see if the API call is available. If not, do ourselves.
869 //
870 if (hasDrawEdge) {
871 if (::DrawEdge(*this, static_cast<LPRECT>(const_cast<TRect*>(&frame)), edge, flags))
872 return true;
873 if (::GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
874 hasDrawEdge = false;
875 else
876 return false;
877 }
878
879 // ::DrawEdge is not available, do the drawing ourselves
880 //
881 TRect f(frame); // working frame rectangle
882
883 // If mono is set, draw a thin, flat, black (windowFrame) frame
884 //
885 if (flags & Mono)
886 {
887 if (edge & EdgeOuter)
888 {
889// KSM PaintFrame(*this, f, flags, TColor::SysWindowFrame, TColor::SysWindowFrame);
891
892 f.Inflate(-1,-1);
893 }
894 if (flags & Fill)
895 { // !CQ repeated code--nest else?
898 PatBlt(f);
899 RestoreBrush();
900 }
901 return true;
902 }
903
904 // If flat is set, draw a thin, flat, shadow frame
905 //
906 if (flags & Flat)
907 {
908 if (edge & EdgeOuter)
909 {
911 f.Inflate(-1,-1);
912 }
913 if (flags & Fill)
914 { // !CQ repeated code--nest else?
917 PatBlt(f);
918 RestoreBrush();
919 }
920 return true;
921 }
922
923 // Draw outer edge if indicated, adjusting rect afterwards
924 //
925 if (edge & EdgeOuter) {
926 static TColor tlColors[] = {
927 TColor::Sys3dLight, // EdgeRaised
928 TColor::Sys3dHilight, // EdgeRaised + Soft
929 TColor::Sys3dShadow, // EdgeSunken
930 TColor::Sys3dDkShadow, // EdgeSunken + Soft
931 };
932 static TColor brColors[] = {
933 TColor::Sys3dDkShadow, // EdgeRaised
934 TColor::Sys3dDkShadow, // EdgeRaised + Soft
935 TColor::Sys3dHilight, // EdgeSunken
936 TColor::Sys3dHilight, // EdgeSunken + Soft
937 };
938 int ci = ((edge & SunkenOuter) ? 2 : 0) | ((flags & Soft) ? 1 : 0);
939//KSM PaintFrame(*this, f, flags, tlColors[ci], brColors[ci]);
940 PaintFrame(f, flags, tlColors[ci], brColors[ci]);
941 f.Inflate(-1,-1);
942 }
943
944 // Draw inner edge if indicated, adjusting rect afterwards
945 //
946 if (edge & EdgeInner) {
947 static TColor tlColors[] = {
948 TColor::Sys3dHilight, // EdgeRaised
949 TColor::Sys3dLight, // EdgeRaised + Soft
950 TColor::Sys3dDkShadow, // EdgeSunken
951 TColor::Sys3dShadow, // EdgeSunken + Soft
952 };
953 static TColor brColors[] = {
954 TColor::Sys3dShadow, // EdgeRaised
955 TColor::Sys3dShadow, // EdgeRaised + Soft
956 TColor::Sys3dLight, // EdgeSunken
957 TColor::Sys3dLight, // EdgeSunken + Soft
958 };
959 int ci = ((edge & SunkenOuter) ? 2 : 0) | ((flags & Soft) ? 1 : 0);
960//KSM PaintFrame(*this, f, flags, tlColors[ci], brColors[ci]);
961 PaintFrame(f, flags, tlColors[ci], brColors[ci]);
962 f.Inflate(-1,-1);
963 }
964
965 // Fill interior if indicated
966 //
967 if (flags & Fill) {
970 PatBlt(f);
971 RestoreBrush();
972 }
973
974// !CQ not really usefull since frame is not returned
975// if (flags & Adjust)
976// frame = f;
977
978 return true;
979}
980
981// DLN ripped from TUIHandle
982// Paint a 2-color single pixel thick frame, bevel corners get their own color
983//
984/*void
985TDC::PaintFrame(const TRect& fr, uint flags, const TColor& tlColor, const TColor& brColor)
986{
987 if (flags & (Top | Left)) {
988 TBrush brsh(tlColor);
989 dc.SelectObject(brsh);
990 if (flags & Top)
991 dc.PatBlt(fr.left, fr.top, fr.Width()-1, 1);
992 if (flags & Left)
993 dc.PatBlt(fr.left, fr.top+1, 1, fr.Height()-2);
994 dc.RestoreBrush();
995 }
996
997 if (flags & (Bottom | Right)) {
998 TBrush brsh(brColor);
999 dc.SelectObject(brsh);
1000 if (flags & Bottom)
1001 dc.PatBlt(fr.left, fr.bottom-1, fr.Width(), 1);
1002 if (flags & Right)
1003 dc.PatBlt(fr.right-1, fr.top, 1, fr.Height()-1);
1004 dc.RestoreBrush();
1005 }
1006}
1007*/
1008void
1009TDC::PaintFrame(const TRect& fr, uint flags, const TColor& tlColor, const TColor& brColor)
1010{
1011 if (flags & (Top | Left)) {
1014 if (flags & Top)
1015 PatBlt(fr.left, fr.top, fr.Width()-1, 1);
1016 if (flags & Left)
1017 PatBlt(fr.left, fr.top+1, 1, fr.Height()-2);
1018 RestoreBrush();
1019 }
1020
1021 if (flags & (Bottom | Right)) {
1024 if (flags & Bottom)
1025 PatBlt(fr.left, fr.bottom-1, fr.Width(), 1);
1026 if (flags & Right)
1027 PatBlt(fr.right-1, fr.top, 1, fr.Height()-1);
1028 RestoreBrush();
1029 }
1030}
1031} // OWL namespace
1032/* ========================================================================== */
#define PRECONDITION(condition)
Definition checks.h:227
#define DIAG_DECLARE_GROUP(group)
Definition checks.h:404
#define TRACEX(group, level, message)
Definition checks.h:263
TBitmap is the GDI bitmap class derived from TGdiObject.
Definition gdiobjec.h:510
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 Sys3dHilight
The symbolic system color value for highlighted 3-dimensional display elements (for edges facing the ...
Definition color.h:344
static const TColor Sys3dDkShadow
The symbolic system color value for dark shadow regions of 3-dimensional display elements.
Definition color.h:345
static const TColor SysWindowFrame
The symbolic system color value for the frame around each window.
Definition color.h:330
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
static const TColor Sys3dLight
The symbolic system color value for the light color for 3-dimensional display elements (for edges fac...
Definition color.h:346
static const TColor SysWindow
The symbolic system color value for the background of each window.
Definition color.h:329
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
int GetTextFaceLength() const
Retrieves the length of the typeface name for the current font on this DC.
Definition dc.h:3156
int GetTextFace(int count, TCHAR *facename) const
Retrieves the typeface name for the current font on this DC.
Definition dc.h:3166
void RestoreBitmap()
Restores the original GDI bitmap object into this DC.
Definition dc.cpp:272
@ EdgeOuter
Mask for outer edge bits.
Definition dc.h:112
@ SunkenOuter
Sunken outer edge only.
Definition dc.h:109
@ EdgeInner
Mask for inner edge bits.
Definition dc.h:113
virtual bool TextOut(int x, int y, const tstring &str, int count=-1)
Draws up to count characters of the given null-terminated string in the current font on this DC.
Definition dc.cpp:574
virtual int DrawTextEx(TCHAR *str, int count, TRect *=nullptr, uint format=0, LPDRAWTEXTPARAMS=nullptr)
Formats and draws in the given rectangle, r, up to count characters of the null-terminated string usi...
Definition dc.cpp:785
bool DrawEdge(const TRect &frame, uint edge, uint flags)
Definition dc.cpp:864
void Init()
Sets OrgBrush, OrgPen, OrgFont, OrgBitmap, and OrgPalette to 0, and sets ShouldDelete to true.
Definition dc.cpp:21
virtual int GetDeviceCaps(int index) const
Used under WIN3.1 or later, GetDeviceCaps returns capability information about this DC.
Definition dc.cpp:373
virtual bool ScaleWindowExt(int xNum, int xDenom, int yNum, int yDenom, TSize *oldExtent=nullptr)
Modifies this DC's window extents relative to the current values.
Definition dc.cpp:554
void SelectObject(const TBrush &brush)
Selects the given GDI brush object into this DC.
Definition dc.cpp:113
void RestorePalette()
Restores the original GDI palette object to this DC.
Definition dc.cpp:259
virtual bool ScaleViewportExt(int xNum, int xDenom, int yNum, int yDenom, TSize *oldExtent=nullptr)
Modifies this DC's viewport extents relative to the current values.
Definition dc.cpp:492
TEXTMETRIC GetTextMetrics() const
Functional style overload.
Definition dc.cpp:382
virtual int SetMapMode(int mode)
Sets the current window mapping mode of this DC to mode.
Definition dc.cpp:431
void RestorePen()
Restores the original GDI pen object to this DC.
Definition dc.cpp:220
void PaintFrame(const TRect &fr, uint flags, const TColor &tlColor, const TColor &brColor)
Definition dc.cpp:1009
bool OffsetWindowOrg(const TPoint &delta, TPoint *oldOrg=nullptr)
Modifies this DC's window origin relative to the current values.
Definition dc.cpp:520
@ Fill
Fill in middle.
Definition dc.h:79
@ Right
Definition dc.h:70
@ Bottom
Definition dc.h:71
@ Left
Definition dc.h:68
@ Top
Definition dc.h:69
@ Soft
Soft edge look for buttons.
Definition dc.h:80
@ Flat
Flat instead of 3d for use in non-3d windows.
Definition dc.h:82
@ Mono
Monochrome.
Definition dc.h:83
TDC()
for derived classes only
Definition dc.cpp:49
virtual bool RestoreDC(int savedDC=-1)
Restores the given savedDC.
Definition dc.cpp:362
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 HDC GetAttributeHDC() const
Returns the attributes of the DC object.
Definition dc.cpp:81
HANDLE GetCurrentObject(uint objectType) const
Returns a handle to the currently selected object of the given objectType associated with this DC.
Definition dc.cpp:319
tstring GetTextFace() const
Definition dc.cpp:855
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 bool ResetDC(const DEVMODE &devMode)
Updates this DC using data in the given devMode structure.
Definition dc.cpp:395
virtual bool GrayString(const TBrush &brush, GRAYSTRINGPROC outputFunc, const tstring &str, int count, const TRect &r)
Draws in the given rectangle (r) up to count characters of gray text from string using the given brus...
Definition dc.cpp:835
virtual bool SetWindowExt(const TSize &extent, TSize *oldExtent=nullptr)
Sets this DC's window x- and y-extents to the given extent values.
Definition dc.cpp:536
virtual bool TabbedTextOut(const TPoint &p, const tstring &str, int count, int numPositions, const int *positions, int tabOrigin, TSize &size)
Draws up to count characters of the given null-terminated string in the current font on this DC.
Definition dc.cpp:634
void RestoreBrush()
Restores the original GDI brush object to this DC.
Definition dc.cpp:233
virtual void RestoreFont()
Restores the original GDI font object to this DC.
Definition dc.cpp:246
void RestoreObjects()
Restores all the original GDI objects to this DC.
Definition dc.cpp:298
virtual void SelectStockObject(int index)
Selects into the DC a predefined stock pen, brush, font, or palette.
Definition dc.cpp:206
bool DrawFrameControl(TRect lpRect, UINT nType, UINT nState)
Definition dc.cpp:797
virtual bool OffsetViewportOrg(const TPoint &delta, TPoint *oldOrg=nullptr)
Modifies this DC's viewport origin relative to the current values.
Definition dc.cpp:458
virtual TColor SetTextColor(const TColor &color)
Sets the current text color of this DC to the given color value.
Definition dc.cpp:417
bool SetWindowOrg(const TPoint &origin, TPoint *oldOrg=nullptr)
Sets the origin of the window associated with this DC to the given origin value, and saves the previo...
Definition dc.cpp:506
void RestoreTextBrush()
Restores the original GDI text brush object to this DC.
Definition dc.cpp:285
void OWLFastWindowFrame(TBrush &brush, TRect &r, int xWidth, int yWidth, uint32 rop=PATCOPY)
Draws a frame of the specified size and thickness with the given brush.
Definition dc.cpp:331
bool PatBlt(int x, int y, int w, int h, uint32 rop=PATCOPY)
Definition dc.h:2448
virtual ~TDC()
Default dtor does not delete Handle Calls RestoreObjects.
Definition dc.cpp:71
HANDLE Handle
< make this function available to derivatives
Definition gdibase.h:81
virtual bool SetViewportExt(const TSize &extent, TSize *oldExtent=nullptr)
Sets this DC's viewport x- and y-extents to the given extent values.
Definition dc.cpp:474
HDC GetHDC() const
Return the handle of the device context.
Definition dc.h:981
virtual int SaveDC() const
Saves the current state of this DC on a context stack.
Definition dc.cpp:352
int DrawText(const tstring &str, int count, const TRect &, uint16 format=0)
Overload for const TRect& For obvious reasons, this overload does not support the DT_CALCRECT format ...
Definition dc.cpp:653
TFont derived from TGdiObject provides constructors for creating font objects from explicit informati...
Definition gdiobjec.h:296
Root and abstract class for Windows object wrappers.
Definition gdibase.h:79
static void RefInc(HANDLE handle)
Increments by 1 the reference count of the object associated with the given handle.
Definition gdiobjec.cpp:178
static void RefDec(HANDLE handle, bool wantDelete)
Decrements this object's reference count by 1 and deletes the object when the reference count reaches...
Definition gdiobjec.cpp:200
TPalette is the GDI Palette class derived from TGdiObject.
Definition gdiobjec.h:413
TPen is derived from TGdiObject.
Definition gdiobjec.h:138
TPoint is a support class, derived from tagPOINT.
Definition geometry.h:87
TRect is a mathematical class derived from tagRect.
Definition geometry.h:308
int Height() const
Returns the height of this rectangle (bottom - top).
Definition geometry.h:1048
TRect & Inflate(int dx, int dy)
Inflates a rectangle inflated by the given delta arguments.
Definition geometry.cpp:129
int Width() const
Returns the width of this rectangle (right - left).
Definition geometry.h:1039
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
Definition of GDI DC encapsulation classes: TDC, TWindowDC, TScreenDC, TDesktopDC,...
TAutoDelete
Flag for Handle ctors to control Handle deletion in dtor.
Definition gdibase.h:70
@ NoAutoDelete
Definition gdibase.h:70
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
unsigned long uint32
Definition number.h:34
tstring CopyText(int size, TGetText get_text)
Copies text from a C-string (null-terminated character array) into a string object,...
Definition defs.h:317
OWL_DIAGINFO
Definition animctrl.cpp:14
unsigned short uint16
Definition number.h:33
std::string tstring
Definition defs.h:79
unsigned int uint
Definition number.h:25
#define OWL_CDLEVEL
Definition defs.h:171