Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
HandySimple.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
5namespace Stroika::Frameworks::Led {
6
7 /*
8 @METHOD: GetTextExtent<WORDWRAPPEDTEXTIMAGER,SIMPLETEXTIMAGER,TEXTSTORE>
9 @DESCRIPTION: <p>Trivial wrapper on TrivialImager<TEXTSTORE,IMAGER> etc, except it handles
10 case of word wrapping as a parameter. So this is roughly a replacement for the Win32 SDK
11 routine GetTextExtent() - except its implemented by Led (and so UNICODE friendly, for example).</p>
12 <p>Note - this is done as a template - rather than directly as a function - so as to avoid forcing
13 people who include LedHandySimple from also including all these other modules required for this.</p>
14 <p>See also DrawTextBox<WORDWRAPPEDTEXTIMAGER,SIMPLETEXTIMAGER,TEXTSTORE></p>
15 */
16 template <typename WORDWRAPPEDTEXTIMAGER, typename SIMPLETEXTIMAGER, typename TEXTSTORE>
17 Led_Size GetTextExtent (Tablet* tablet, const Led_tString& text, const Led_Rect& r, bool wordWrap)
18 {
19 RequireNotNull (tablet);
20 Led_Size textExtent (0, 0);
21 if (wordWrap) {
22 if (r.right > r.left) {
23 TrivialWordWrappedImager<TEXTSTORE, WORDWRAPPEDTEXTIMAGER> imager (tablet, r, text);
24 textExtent.v = imager.GetHeight ();
25 textExtent.h = r.GetWidth ();
26 }
27 }
28 else {
29 TrivialImager<TEXTSTORE, SIMPLETEXTIMAGER> imager (tablet, r, text);
30 textExtent.v = imager.GetHeightOfRows (0, imager.GetRowCount ());
31 textExtent.h = imager.CalculateLongestRowInWindowPixelWidth ();
32 }
33 return (textExtent);
34 }
35
36 /*
37 @METHOD: DrawTextBox<WORDWRAPPEDTEXTIMAGER,SIMPLETEXTIMAGER,TEXTSTORE>
38 @DESCRIPTION: <p>Trivial wrapper on TrivialImager<TEXTSTORE,IMAGER> etc, except it handles
39 case of word wrapping as a parameter. So this is roughly a replacement for the Win32 SDK
40 routine DrawText () - except its implemented by Led (and so UNICODE friendly, for example).</p>
41 <p>Note - this is done as a template - rather than directly as a function - so as to avoid forcing
42 people who include Led_MFC from also including all these other modules required for this. There is
43 a global function version of this function (@'Led_DrawText') which will be enabled/included in
44 your program if you define @'qSupportDrawTextGetTextExtent'.</p>
45 <p>See also GetTextExtent<WORDWRAPPEDTEXTIMAGER,SIMPLETEXTIMAGER,TEXTSTORE></p>
46 */
47 template <typename WORDWRAPPEDTEXTIMAGER, typename SIMPLETEXTIMAGER, typename TEXTSTORE>
48 void DrawTextBox (Tablet* tablet, const Led_tString& text, const Led_Rect& r, bool wordWrap)
49 {
50 RequireNotNull (tablet);
51 if (wordWrap) {
52 TrivialWordWrappedImager<TEXTSTORE, WORDWRAPPEDTEXTIMAGER> imager (tablet, r, text);
53 imager.Draw (r, false);
54 }
55 else {
56 TrivialImager<TEXTSTORE, SIMPLETEXTIMAGER> imager (tablet, r, text);
57 imager.Draw (r, false);
58 }
59 }
60
61 // class WaterMarkHelper<TEXTSTORE,WORDPROCESSOR>
62 template <typename TEXTSTORE, typename WORDPROCESSOR>
63 WaterMarkHelper<TEXTSTORE, WORDPROCESSOR>::WaterMarkHelper (const Led_tString& watermMarkText)
64 : fWatermarkColor (Color::kYellow)
65 , fWatermarkText (watermMarkText)
66 , fCachedImager (NULL)
67 , fCachedIntoRect (Led_Rect (0, 0, 0, 0))
68 , fCachedIntoTablet (NULL)
69 {
70 }
71 template <typename TEXTSTORE, typename WORDPROCESSOR>
72 WaterMarkHelper<TEXTSTORE, WORDPROCESSOR>::~WaterMarkHelper ()
73 {
74 delete fCachedImager;
75 }
76 template <typename TEXTSTORE, typename WORDPROCESSOR>
77 Color WaterMarkHelper<TEXTSTORE, WORDPROCESSOR>::GetWatermarkColor () const
78 {
79 return fWatermarkColor;
80 }
81 template <typename TEXTSTORE, typename WORDPROCESSOR>
82 void WaterMarkHelper<TEXTSTORE, WORDPROCESSOR>::SetWatermarkColor (const Color& c)
83 {
84 fWatermarkColor = c;
85 }
86 template <typename TEXTSTORE, typename WORDPROCESSOR>
87 void WaterMarkHelper<TEXTSTORE, WORDPROCESSOR>::DrawWatermark (Tablet* tablet, const Led_Rect& intoRect, const Led_Rect& subsetToDraw)
88 {
89 /*
90 * Draw a watermark static text label to indicate in DEMO MODE.
91 *
92 * Dynamicly choose the size of the text - biggest that will fit (within reason).
93 * Be sure to properly adjust clip region cuz this gets called many times - once for each row potentially, os
94 * respect the 'subsetToDraw'.
95 */
96 if (fCachedImager != NULL) {
97 // del and set null if anything important changed...
98 if (fCachedIntoRect != intoRect or fCachedIntoTablet != tablet) {
99 delete fCachedImager;
100 fCachedImager = NULL;
101 }
102 }
103 Led_Rect centeredRect = intoRect; // find appropriate small/centered rect for this text
104
105 // It looks a bit better if we inset the text
106 centeredRect.left += 5;
107 centeredRect.right -= 5;
108 if (centeredRect.right <= centeredRect.left) {
109 centeredRect.right = centeredRect.left + 1;
110 }
111 if (fCachedImager == NULL) {
112 fCachedImager = new MyTrivImager (tablet, centeredRect, fWatermarkText);
113 fCachedIntoRect = centeredRect;
114 fCachedIntoTablet = tablet;
115 IncrementalFontSpecification extraStyles = GetStaticDefaultFont ();
116 extraStyles.SetTextColor (GetWatermarkColor ());
117 fCachedImager->SetStyleInfo (0, fCachedImager->GetLength (), extraStyles);
118
119 // Try and adjust the fontsize so it fits well, and then patch the windowrect so centered.
120 {
121 extraStyles.SetPointSize (72);
122 fCachedImager->SetStyleInfo (0, fCachedImager->GetLength (), extraStyles);
123
124 while (fCachedImager->GetRowCount () > 1) {
125 extraStyles.SetPointSize (extraStyles.GetPointSize () / 2);
126 fCachedImager->SetStyleInfo (0, fCachedImager->GetLength (), extraStyles);
127
128 if (extraStyles.GetPointSize () <= 12) {
129 break;
130 }
131 }
132 // Now center the rect of the text...
133 fCachedImager->SetJustification (0, fCachedImager->GetLength (), eCenterJustify);
134
135 // Now center vertically
136 DistanceType rowHeight = fCachedImager->GetHeightOfRows (0, 1);
137 Led_Rect wr = fCachedImager->GetWindowRect ();
138 wr.top += (wr.GetHeight () - rowHeight) / 2;
139 wr.bottom = wr.top + rowHeight;
140 fCachedImager->SetWindowRect (wr);
141 }
142 }
143
144 Tablet::ClipNarrowAndRestore clipFurtherTo (tablet, intoRect * subsetToDraw);
145
146 bool printing = true; // not really printing - but set this to disable erase call
147 fCachedImager->Draw (subsetToDraw, printing);
148 }
149
150}
#define RequireNotNull(p)
Definition Assertions.h:347