Subversion Repositories spk

Rev

Rev 79 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
79 cycrow 1
//************************************************************************
2
//
3
// LCDText.cpp
4
//
5
// The CLCDText class draws simple text onto the LCD.
6
// 
7
// Logitech LCD SDK
8
//
9
// Copyright 2005 Logitech Inc.
10
//************************************************************************
11
 
12
#include "LCDText.h"
13
#include <string>
14
 
15
 
16
//************************************************************************
17
//
18
// CLCDText::CLCDText
19
//
20
//************************************************************************
21
 
22
CLCDText::CLCDText()
23
{
24
    m_nTextLength = 0;
25
    m_hFont = NULL;
26
//    m_sText.clear();
27
    m_crColor = RGB(255, 255, 255); // white
28
    m_bRecalcExtent = TRUE;
29
    ZeroMemory(&m_dtp, sizeof(DRAWTEXTPARAMS));
30
    m_dtp.cbSize = sizeof(DRAWTEXTPARAMS);
31
    m_nTextFormat = m_nTextAlignment = (DT_LEFT | DT_NOPREFIX);
32
    m_nTextAlignment = DT_LEFT;
33
    ZeroMemory(&m_sizeVExtent, sizeof(m_sizeVExtent));
34
    ZeroMemory(&m_sizeHExtent, sizeof(m_sizeHExtent));
35
}
36
 
37
 
38
//************************************************************************
39
//
40
// CLCDText::~CLCDText
41
//
42
//************************************************************************
43
 
44
CLCDText::~CLCDText()
45
{
46
 
47
    if (m_hFont)
48
    {
49
        DeleteObject(m_hFont);
50
        m_hFont = NULL;
51
    }
52
}
53
 
54
 
55
//************************************************************************
56
//
57
// CLCDText::Initialize
58
//
59
//************************************************************************
60
 
61
HRESULT CLCDText::Initialize()
62
{
63
 
64
    m_hFont = (HFONT) GetStockObject(DEFAULT_GUI_FONT);
65
    if(NULL != m_hFont)
66
    {
67
        SetFontPointSize(DEFAULT_POINTSIZE);
68
    }
69
 
70
    return (NULL != m_hFont) ? S_OK : E_OUTOFMEMORY;
71
}
72
 
73
 
74
//************************************************************************
75
//
76
// CLCDText::SetFont
77
//
78
//************************************************************************
79
 
80
void CLCDText::SetFont(LOGFONT& lf)
81
{
82
    if (m_hFont)
83
    {
84
        DeleteObject(m_hFont);
85
        m_hFont = NULL;
86
    }
87
 
88
    m_hFont = CreateFontIndirect(&lf);
89
    m_bRecalcExtent = TRUE;
90
}
91
 
92
 
93
//************************************************************************
94
//
95
// CLCDText::GetFont
96
//
97
//************************************************************************
98
 
99
HFONT CLCDText::GetFont()
100
{
101
    return m_hFont;
102
}
103
 
104
 
105
//************************************************************************
106
//
107
// CLCDText::SetFontFaceName
108
//
109
//************************************************************************
110
 
111
void CLCDText::SetFontFaceName(LPCTSTR szFontName)
112
{
113
    // if NULL, uses the default gui font
114
    if (NULL == szFontName)
115
        return;
116
 
117
    LOGFONT lf;
118
    ZeroMemory(&lf, sizeof(lf));
119
    GetObject(m_hFont, sizeof(LOGFONT), &lf);
120
 
121
    _tcsncpy(lf.lfFaceName, szFontName, LF_FACESIZE);
122
 
123
    SetFont(lf);
124
}
125
 
126
 
127
//************************************************************************
128
//
129
// CLCDText::SetFontPointSize
130
//
131
//************************************************************************
132
 
133
void CLCDText::SetFontPointSize(int nPointSize)
134
{
135
    LOGFONT lf;
136
    ZeroMemory(&lf, sizeof(lf));
137
    GetObject(m_hFont, sizeof(LOGFONT), &lf);
138
 
139
    lf.lfHeight = -MulDiv(nPointSize, DEFAULT_DPI, 72);
140
 
141
    SetFont(lf);
142
}
143
 
144
 
145
//************************************************************************
146
//
147
// CLCDText::SetFontWeight
148
//
149
//************************************************************************
150
 
151
void CLCDText::SetFontWeight(int nWeight)
152
{
153
    LOGFONT lf;
154
    ZeroMemory(&lf, sizeof(lf));
155
    GetObject(m_hFont, sizeof(LOGFONT), &lf);
156
 
157
    lf.lfWeight = nWeight;
158
 
159
    SetFont(lf);
160
}
161
 
162
 
163
//************************************************************************
164
//
165
// CLCDText::SetText
166
//
167
//************************************************************************
168
 
242 cycrow 169
void CLCDText::SetText ( const std::wstring &szText )
79 cycrow 170
{
171
    m_sText = szText;
172
    m_nTextLength = m_sText.size();
173
    m_dtp.iLeftMargin = 0;
174
    m_dtp.iRightMargin = 0;
175
    m_bRecalcExtent = TRUE;
176
}
177
void CLCDText::SetText(LPCTSTR szText)
178
{
179
    LCDUIASSERT(NULL != szText);
180
    if(szText && _tcscmp(m_sText.c_str(), szText))
181
    {
182
        m_sText.assign(szText);
183
        m_nTextLength = m_sText.size();
184
        m_dtp.iLeftMargin = 0;
185
        m_dtp.iRightMargin = 0;
186
        m_bRecalcExtent = TRUE;
187
    }
188
}
189
 
190
 
191
//************************************************************************
192
//
193
// CLCDText::GetText
194
//
195
//************************************************************************
196
 
197
LPCTSTR CLCDText::GetText()
198
{
199
    return m_sText.c_str();
200
}
201
 
202
 
203
//************************************************************************
204
//
205
// CLCDText::SetWordWrap
206
//
207
//************************************************************************
208
 
209
void CLCDText::SetWordWrap(BOOL bEnable)
210
{
211
    if (bEnable)
212
    {
213
        m_nTextFormat |= DT_WORDBREAK;
214
    }
215
    else
216
    {
217
        m_nTextFormat &= ~DT_WORDBREAK;
218
    }
219
    m_bRecalcExtent = TRUE;
220
}
221
 
222
 
223
//************************************************************************
224
//
225
// CLCDText::SetLeftMargin
226
//
227
//************************************************************************
228
 
229
void CLCDText::SetLeftMargin(int nLeftMargin)
230
{
231
    m_dtp.iLeftMargin = nLeftMargin;
232
}
233
 
234
 
235
//************************************************************************
236
//
237
// CLCDText::SetRightMargin
238
//
239
//************************************************************************
240
 
241
void CLCDText::SetRightMargin(int nRightMargin)
242
{
243
    m_dtp.iRightMargin = nRightMargin;
244
}
245
 
246
 
247
//************************************************************************
248
//
249
// CLCDText::GetLeftMargin
250
//
251
//************************************************************************
252
 
253
int CLCDText::GetLeftMargin(void)
254
{
255
    return m_dtp.iLeftMargin;
256
}
257
 
258
 
259
//************************************************************************
260
//
261
// CLCDText::GetRightMargin
262
//
263
//************************************************************************
264
 
265
int CLCDText::GetRightMargin(void)
266
{
267
    return m_dtp.iRightMargin;
268
}
269
 
270
 
271
//************************************************************************
272
//
273
// CLCDText::GetVExtent
274
//
275
//************************************************************************
276
 
277
SIZE& CLCDText::GetVExtent()
278
{
279
    return m_sizeVExtent;
280
}
281
 
282
 
283
//************************************************************************
284
//
285
// CLCDText::GetHExtent
286
//
287
//************************************************************************
288
 
289
SIZE& CLCDText::GetHExtent()
290
{
291
    return m_sizeHExtent;
292
}
293
 
294
 
295
//************************************************************************
296
//
297
// CLCDText::SetAlignment
298
//
299
//************************************************************************
300
 
301
void CLCDText::SetAlignment(int nAlignment)
302
{
303
    m_nTextFormat &= ~m_nTextAlignment;
304
    m_nTextFormat |= nAlignment;
305
    m_nTextAlignment = nAlignment;
306
}
307
 
308
 
309
//************************************************************************
310
//
311
// CLCDText::DrawText
312
//
313
//************************************************************************
314
 
315
void CLCDText::DrawText(CLCDGfx &rGfx)
316
{
317
    // draw the text
318
    RECT rBoundary = { 0, 0,0 + GetLogicalSize().cx, 0 + GetLogicalSize().cy }; 
319
    DrawTextEx(rGfx.GetHDC(), (LPTSTR)m_sText.c_str(), static_cast<int>(m_nTextLength), &rBoundary, m_nTextFormat, &m_dtp);
320
 
321
//    LCDUITRACE(_T("Drawing %s at (%d,%d)-(%d-%d) lmargin=%d, rmargin=%d\n"),
322
//         m_sText.c_str(), m_Origin.x, m_Origin.y, GetWidth(), GetHeight(),
323
//         m_dtp.iLeftMargin, m_dtp.iRightMargin);
324
 
325
    if (m_bInverted)
326
    {
327
        InvertRect(rGfx.GetHDC(), &rBoundary);
328
    }
329
}
330
 
331
 
332
//************************************************************************
333
//
334
// CLCDText::OnDraw
335
//
336
//************************************************************************
337
 
338
void CLCDText::OnDraw(CLCDGfx &rGfx)
339
{
340
 
341
    if (GetBackgroundMode() == OPAQUE)
342
    {
343
        // clear the clipped area
344
        RECT rcClp = { 0, 0, m_Size.cx, m_Size.cy };
345
        FillRect(rGfx.GetHDC(), &rcClp, (HBRUSH) GetStockObject(BLACK_BRUSH));
346
 
347
        // clear the logical area
348
        RECT rcLog = { 0, 0, m_sizeLogical.cx, m_sizeLogical.cy };
349
        FillRect(rGfx.GetHDC(), &rcLog, (HBRUSH) GetStockObject(BLACK_BRUSH));
350
    }
351
 
352
    if (m_nTextLength)
353
    {
354
 
355
        // map mode text, with transparency
356
        int nOldMapMode = SetMapMode(rGfx.GetHDC(), MM_TEXT);
357
        int nOldBkMode = SetBkMode(rGfx.GetHDC(), GetBackgroundMode()); 
358
 
359
        // select current font
360
        HFONT hOldFont = (HFONT)SelectObject(rGfx.GetHDC(), m_hFont);   
361
 
362
        // select color
363
        COLORREF crOldTextColor = SetTextColor(rGfx.GetHDC(), m_crColor);
364
 
365
        if (m_bRecalcExtent)
366
        {
367
            int nTextFormat;
368
 
369
            RECT rExtent;
370
            // calculate vertical extent with word wrap
371
            nTextFormat = (m_nTextFormat | DT_WORDBREAK | DT_CALCRECT);
372
            rExtent.left = rExtent.top = 0;
373
            rExtent.right = GetWidth();
374
            rExtent.bottom = GetHeight();
375
            DrawTextEx(rGfx.GetHDC(), (LPTSTR)m_sText.c_str(), static_cast<int>(m_nTextLength), &rExtent, nTextFormat, &m_dtp);
376
            m_sizeVExtent.cx = rExtent.right;
377
            m_sizeVExtent.cy = rExtent.bottom;
378
 
379
            // calculate horizontal extent w/o word wrap
380
            nTextFormat = (m_nTextFormat | DT_CALCRECT);
381
            rExtent.left = rExtent.top = 0;
382
            rExtent.right = GetWidth();
383
            rExtent.bottom = GetHeight();
384
            DrawTextEx(rGfx.GetHDC(), (LPTSTR)m_sText.c_str(), static_cast<int>(m_nTextLength), &rExtent, nTextFormat, &m_dtp);
385
            m_sizeHExtent.cx = rExtent.right;
386
            m_sizeHExtent.cy = rExtent.bottom;
387
 
388
            m_bRecalcExtent = FALSE;
389
        }
390
 
391
        if (IsVisible())
392
        {
393
            DrawText(rGfx);
394
        }
395
 
396
        // restores
397
        SetMapMode(rGfx.GetHDC(), nOldMapMode);
398
        SetTextColor(rGfx.GetHDC(), crOldTextColor);
399
        SetBkMode(rGfx.GetHDC(), nOldBkMode);
400
        SelectObject(rGfx.GetHDC(), hOldFont);
401
    }
402
}
403
 
404
//** end of LCDText.cpp **************************************************
405