Repository navigation
Expand file tree
/
Copy pathUIEdit.cpp
More file actions
563 lines (493 loc) · 14.6 KB
/
Copy pathUIEdit.cpp
File metadata and controls
563 lines (493 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#include "stdafx.h"
#include "UIEdit.h"
namespace DuiLib
{
class CEditWnd : public CWindowWnd
{
public:
CEditWnd();
void Init(CEditUI* pOwner);
RECT CalPos();
LPCTSTR GetWindowClassName() const;
LPCTSTR GetSuperClassName() const;
void OnFinalMessage(HWND hWnd);
LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
LRESULT OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnEditChanged(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
protected:
CEditUI* m_pOwner;
HBRUSH m_hBkBrush;
bool m_bInit;
};
CEditWnd::CEditWnd() : m_pOwner(NULL), m_hBkBrush(NULL), m_bInit(false)
{
}
void CEditWnd::Init(CEditUI* pOwner)
{
m_pOwner = pOwner;
RECT rcPos = CalPos();
UINT uStyle = WS_CHILD | ES_AUTOHSCROLL;
if( m_pOwner->IsPasswordMode() ) uStyle |= ES_PASSWORD;
Create(m_pOwner->GetManager()->GetPaintWindow(), NULL, uStyle, 0, rcPos);
HFONT hFont=NULL;
int iFontIndex=m_pOwner->GetFont();
if (iFontIndex!=-1)
hFont=m_pOwner->GetManager()->GetFont(iFontIndex);
if (hFont==NULL)
hFont=m_pOwner->GetManager()->GetDefaultFontInfo()->hFont;
SetWindowFont(m_hWnd, hFont, TRUE);
Edit_LimitText(m_hWnd, m_pOwner->GetMaxChar());
if( m_pOwner->IsPasswordMode() ) Edit_SetPasswordChar(m_hWnd, m_pOwner->GetPasswordChar());
Edit_SetText(m_hWnd, m_pOwner->GetText());
Edit_SetModify(m_hWnd, FALSE);
SendMessage(EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, MAKELPARAM(0, 0));
Edit_Enable(m_hWnd, m_pOwner->IsEnabled() == true);
Edit_SetReadOnly(m_hWnd, m_pOwner->IsReadOnly() == true);
//Styls
LONG styleValue = ::GetWindowLong(m_hWnd, GWL_STYLE);
styleValue |= pOwner->GetWindowStyls();
::SetWindowLong(GetHWND(), GWL_STYLE, styleValue);
::ShowWindow(m_hWnd, SW_SHOWNOACTIVATE);
::SetFocus(m_hWnd);
m_bInit = true;
}
RECT CEditWnd::CalPos()
{
CDuiRect rcPos = m_pOwner->GetPos();
RECT rcInset = m_pOwner->GetTextPadding();
rcPos.left += rcInset.left;
rcPos.top += rcInset.top;
rcPos.right -= rcInset.right;
rcPos.bottom -= rcInset.bottom;
LONG lEditHeight = m_pOwner->GetManager()->GetFontInfo(m_pOwner->GetFont())->tm.tmHeight;
if( lEditHeight < rcPos.GetHeight() ) {
rcPos.top += (rcPos.GetHeight() - lEditHeight) / 2;
rcPos.bottom = rcPos.top + lEditHeight;
}
return rcPos;
}
LPCTSTR CEditWnd::GetWindowClassName() const
{
return _T("EditWnd");
}
LPCTSTR CEditWnd::GetSuperClassName() const
{
return WC_EDIT;
}
void CEditWnd::OnFinalMessage(HWND /*hWnd*/)
{
m_pOwner->Invalidate();
// Clear reference and die
if( m_hBkBrush != NULL ) ::DeleteObject(m_hBkBrush);
m_pOwner->m_pWindow = NULL;
delete this;
}
LRESULT CEditWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT lRes = 0;
BOOL bHandled = TRUE;
if( uMsg == WM_KILLFOCUS ) lRes = OnKillFocus(uMsg, wParam, lParam, bHandled);
else if( uMsg == OCM_COMMAND ) {
if( GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE ) lRes = OnEditChanged(uMsg, wParam, lParam, bHandled);
else if( GET_WM_COMMAND_CMD(wParam, lParam) == EN_UPDATE ) {
RECT rcClient;
::GetClientRect(m_hWnd, &rcClient);
::InvalidateRect(m_hWnd, &rcClient, FALSE);
}
}
else if( uMsg == WM_KEYDOWN && TCHAR(wParam) == VK_RETURN ) {
m_pOwner->GetManager()->SendNotify(m_pOwner, DUI_MSGTYPE_RETURN);
}
else if( uMsg == OCM__BASE + WM_CTLCOLOREDIT || uMsg == OCM__BASE + WM_CTLCOLORSTATIC ) {
if( m_pOwner->GetNativeEditBkColor() == 0xFFFFFFFF ) return NULL;
::SetBkMode((HDC)wParam, TRANSPARENT);
DWORD dwTextColor = m_pOwner->GetTextColor();
::SetTextColor((HDC)wParam, RGB(GetBValue(dwTextColor),GetGValue(dwTextColor),GetRValue(dwTextColor)));
if( m_hBkBrush == NULL ) {
DWORD clrColor = m_pOwner->GetNativeEditBkColor();
m_hBkBrush = ::CreateSolidBrush(RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
}
return (LRESULT)m_hBkBrush;
}
else bHandled = FALSE;
if( !bHandled ) return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
return lRes;
}
LRESULT CEditWnd::OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
LRESULT lRes = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
PostMessage(WM_CLOSE);
return lRes;
}
LRESULT CEditWnd::OnEditChanged(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
if( !m_bInit ) return 0;
if( m_pOwner == NULL ) return 0;
// Copy text back
int cchLen = ::GetWindowTextLength(m_hWnd) + 1;
LPTSTR pstr = static_cast<LPTSTR>(_alloca(cchLen * sizeof(TCHAR)));
ASSERT(pstr);
if( pstr == NULL ) return 0;
::GetWindowText(m_hWnd, pstr, cchLen);
m_pOwner->m_sText = pstr;
m_pOwner->GetManager()->SendNotify(m_pOwner, DUI_MSGTYPE_TEXTCHANGED);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////
//
//
CEditUI::CEditUI() : m_pWindow(NULL), m_uMaxChar(255), m_bReadOnly(false),
m_bPasswordMode(false), m_cPasswordChar(_T('*')), m_uButtonState(0),
m_dwEditbkColor(0xFFFFFFFF), m_iWindowStyls(0)
{
SetTextPadding(CDuiRect(4, 3, 4, 3));
SetBkColor(0xFFFFFFFF);
}
LPCTSTR CEditUI::GetClass() const
{
return _T("EditUI");
}
LPVOID CEditUI::GetInterface(LPCTSTR pstrName)
{
if( _tcscmp(pstrName, DUI_CTR_EDIT) == 0 ) return static_cast<CEditUI*>(this);
return CLabelUI::GetInterface(pstrName);
}
UINT CEditUI::GetControlFlags() const
{
if( !IsEnabled() ) return CControlUI::GetControlFlags();
return UIFLAG_SETCURSOR | UIFLAG_TABSTOP;
}
void CEditUI::DoEvent(TEventUI& event)
{
if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
if( m_pParent != NULL ) m_pParent->DoEvent(event);
else CLabelUI::DoEvent(event);
return;
}
if( event.Type == UIEVENT_SETCURSOR && IsEnabled() )
{
::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_IBEAM)));
return;
}
if( event.Type == UIEVENT_WINDOWSIZE )
{
if( m_pWindow != NULL ) m_pManager->SetFocusNeeded(this);
}
if( event.Type == UIEVENT_SCROLLWHEEL )
{
if( m_pWindow != NULL ) return;
}
if( event.Type == UIEVENT_SETFOCUS && IsEnabled() )
{
if( m_pWindow ) return;
m_pWindow = new CEditWnd();
ASSERT(m_pWindow);
m_pWindow->Init(this);
Invalidate();
}
if( event.Type == UIEVENT_KILLFOCUS && IsEnabled() )
{
Invalidate();
}
if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK || event.Type == UIEVENT_RBUTTONDOWN)
{
if( IsEnabled() ) {
GetManager()->ReleaseCapture();
if( IsFocused() && m_pWindow == NULL )
{
m_pWindow = new CEditWnd();
ASSERT(m_pWindow);
m_pWindow->Init(this);
if( PtInRect(&m_rcItem, event.ptMouse) )
{
int nSize = GetWindowTextLength(*m_pWindow);
if( nSize == 0 )
nSize = 1;
Edit_SetSel(*m_pWindow, 0, nSize);
}
}
else if( m_pWindow != NULL )
{
#if 1
int nSize = GetWindowTextLength(*m_pWindow);
if( nSize == 0 )
nSize = 1;
Edit_SetSel(*m_pWindow, 0, nSize);
#else
POINT pt = event.ptMouse;
pt.x -= m_rcItem.left + m_rcTextPadding.left;
pt.y -= m_rcItem.top + m_rcTextPadding.top;
::SendMessage(*m_pWindow, WM_LBUTTONDOWN, event.wParam, MAKELPARAM(pt.x, pt.y));
#endif
}
}
return;
}
if( event.Type == UIEVENT_MOUSEMOVE )
{
return;
}
if( event.Type == UIEVENT_BUTTONUP )
{
return;
}
if( event.Type == UIEVENT_CONTEXTMENU )
{
return;
}
if( event.Type == UIEVENT_MOUSEENTER )
{
if( IsEnabled() ) {
m_uButtonState |= UISTATE_HOT;
Invalidate();
}
return;
}
if( event.Type == UIEVENT_MOUSELEAVE )
{
if( IsEnabled() ) {
m_uButtonState &= ~UISTATE_HOT;
Invalidate();
}
return;
}
CLabelUI::DoEvent(event);
}
void CEditUI::SetEnabled(bool bEnable)
{
CControlUI::SetEnabled(bEnable);
if( !IsEnabled() ) {
m_uButtonState = 0;
}
}
void CEditUI::SetText(LPCTSTR pstrText)
{
m_sText = pstrText;
if( m_pWindow != NULL ) Edit_SetText(*m_pWindow, m_sText);
Invalidate();
}
void CEditUI::SetMaxChar(UINT uMax)
{
m_uMaxChar = uMax;
if( m_pWindow != NULL ) Edit_LimitText(*m_pWindow, m_uMaxChar);
}
UINT CEditUI::GetMaxChar()
{
return m_uMaxChar;
}
void CEditUI::SetReadOnly(bool bReadOnly)
{
if( m_bReadOnly == bReadOnly ) return;
m_bReadOnly = bReadOnly;
if( m_pWindow != NULL ) Edit_SetReadOnly(*m_pWindow, m_bReadOnly);
Invalidate();
}
bool CEditUI::IsReadOnly() const
{
return m_bReadOnly;
}
void CEditUI::SetNumberOnly(bool bNumberOnly)
{
if( bNumberOnly )
{
m_iWindowStyls |= ES_NUMBER;
}
else
{
m_iWindowStyls &= ~ES_NUMBER;
}
}
bool CEditUI::IsNumberOnly() const
{
return m_iWindowStyls&ES_NUMBER ? true:false;
}
int CEditUI::GetWindowStyls() const
{
return m_iWindowStyls;
}
HWND CEditUI::GetNativeEditHWND() const
{
if (m_pWindow == NULL) return NULL;
return m_pWindow->GetHWND();
}
void CEditUI::SetPasswordMode(bool bPasswordMode)
{
if( m_bPasswordMode == bPasswordMode ) return;
m_bPasswordMode = bPasswordMode;
Invalidate();
}
bool CEditUI::IsPasswordMode() const
{
return m_bPasswordMode;
}
void CEditUI::SetPasswordChar(TCHAR cPasswordChar)
{
if( m_cPasswordChar == cPasswordChar ) return;
m_cPasswordChar = cPasswordChar;
if( m_pWindow != NULL ) Edit_SetPasswordChar(*m_pWindow, m_cPasswordChar);
Invalidate();
}
TCHAR CEditUI::GetPasswordChar() const
{
return m_cPasswordChar;
}
LPCTSTR CEditUI::GetNormalImage()
{
return m_diNormal.sDrawString;
}
void CEditUI::SetNormalImage(LPCTSTR pStrImage)
{
if( m_diNormal.sDrawString == pStrImage && m_diNormal.pImageInfo != NULL ) return;
m_diNormal.Clear();
m_diNormal.sDrawString = pStrImage;
Invalidate();
}
LPCTSTR CEditUI::GetHotImage()
{
return m_diHot.sDrawString;
}
void CEditUI::SetHotImage(LPCTSTR pStrImage)
{
if( m_diHot.sDrawString == pStrImage && m_diHot.pImageInfo != NULL ) return;
m_diHot.Clear();
m_diHot.sDrawString = pStrImage;
Invalidate();
}
LPCTSTR CEditUI::GetFocusedImage()
{
return m_diFocused.sDrawString;
}
void CEditUI::SetFocusedImage(LPCTSTR pStrImage)
{
if( m_diFocused.sDrawString == pStrImage && m_diFocused.pImageInfo != NULL ) return;
m_diFocused.Clear();
m_diFocused.sDrawString = pStrImage;
Invalidate();
}
LPCTSTR CEditUI::GetDisabledImage()
{
return m_diDisabled.sDrawString;
}
void CEditUI::SetDisabledImage(LPCTSTR pStrImage)
{
if( m_diDisabled.sDrawString == pStrImage && m_diDisabled.pImageInfo != NULL ) return;
m_diDisabled.Clear();
m_diDisabled.sDrawString = pStrImage;
Invalidate();
}
void CEditUI::SetNativeEditBkColor(DWORD dwBkColor)
{
m_dwEditbkColor = dwBkColor;
}
DWORD CEditUI::GetNativeEditBkColor() const
{
return m_dwEditbkColor;
}
void CEditUI::SetSel(long nStartChar, long nEndChar)
{
if( m_pWindow != NULL ) Edit_SetSel(*m_pWindow, nStartChar,nEndChar);
}
void CEditUI::SetSelAll()
{
SetSel(0,-1);
}
void CEditUI::SetReplaceSel(LPCTSTR lpszReplace)
{
if( m_pWindow != NULL ) Edit_ReplaceSel(*m_pWindow, lpszReplace);
}
void CEditUI::SetPos(RECT rc, bool bNeedInvalidate)
{
CControlUI::SetPos(rc, bNeedInvalidate);
if( m_pWindow != NULL ) {
RECT rcPos = m_pWindow->CalPos();
::SetWindowPos(m_pWindow->GetHWND(), NULL, rcPos.left, rcPos.top, rcPos.right - rcPos.left,
rcPos.bottom - rcPos.top, SWP_NOZORDER | SWP_NOACTIVATE);
}
}
void CEditUI::Move(SIZE szOffset, bool bNeedInvalidate)
{
CControlUI::Move(szOffset, bNeedInvalidate);
if( m_pWindow != NULL ) {
RECT rcPos = m_pWindow->CalPos();
::SetWindowPos(m_pWindow->GetHWND(), NULL, rcPos.left, rcPos.top, rcPos.right - rcPos.left,
rcPos.bottom - rcPos.top, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
}
void CEditUI::SetVisible(bool bVisible)
{
CControlUI::SetVisible(bVisible);
if( !IsVisible() && m_pWindow != NULL ) m_pManager->SetFocus(NULL);
}
void CEditUI::SetInternVisible(bool bVisible)
{
if( !IsVisible() && m_pWindow != NULL ) m_pManager->SetFocus(NULL);
}
SIZE CEditUI::EstimateSize(SIZE szAvailable)
{
if( m_cxyFixed.cy == 0 ) return CDuiSize(m_cxyFixed.cx, m_pManager->GetFontInfo(GetFont())->tm.tmHeight + 6);
return CControlUI::EstimateSize(szAvailable);
}
void CEditUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
if( _tcscmp(pstrName, _T("readonly")) == 0 ) SetReadOnly(_tcscmp(pstrValue, _T("true")) == 0);
else if( _tcscmp(pstrName, _T("numberonly")) == 0 ) SetNumberOnly(_tcscmp(pstrValue, _T("true")) == 0);
else if( _tcscmp(pstrName, _T("password")) == 0 ) SetPasswordMode(_tcscmp(pstrValue, _T("true")) == 0);
else if( _tcscmp(pstrName, _T("maxchar")) == 0 ) SetMaxChar(_ttoi(pstrValue));
else if( _tcscmp(pstrName, _T("normalimage")) == 0 ) SetNormalImage(pstrValue);
else if( _tcscmp(pstrName, _T("hotimage")) == 0 ) SetHotImage(pstrValue);
else if( _tcscmp(pstrName, _T("focusedimage")) == 0 ) SetFocusedImage(pstrValue);
else if( _tcscmp(pstrName, _T("disabledimage")) == 0 ) SetDisabledImage(pstrValue);
else if( _tcscmp(pstrName, _T("nativebkcolor")) == 0 ) {
if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
LPTSTR pstr = NULL;
DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
SetNativeEditBkColor(clrColor);
}
else CLabelUI::SetAttribute(pstrName, pstrValue);
}
void CEditUI::PaintStatusImage(HDC hDC)
{
if( IsFocused() ) m_uButtonState |= UISTATE_FOCUSED;
else m_uButtonState &= ~ UISTATE_FOCUSED;
if( !IsEnabled() ) m_uButtonState |= UISTATE_DISABLED;
else m_uButtonState &= ~ UISTATE_DISABLED;
if( (m_uButtonState & UISTATE_DISABLED) != 0 ) {
if( DrawImage(hDC, m_diDisabled) ) return;
}
else if( (m_uButtonState & UISTATE_FOCUSED) != 0 ) {
if( DrawImage(hDC, m_diFocused) ) return;
}
else if( (m_uButtonState & UISTATE_HOT) != 0 ) {
if( DrawImage(hDC, m_diHot) ) return;
}
if( DrawImage(hDC, m_diNormal) ) return;
}
void CEditUI::PaintText(HDC hDC)
{
if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
if( m_sText.IsEmpty() ) return;
CDuiString sText = m_sText;
if( m_bPasswordMode ) {
sText.Empty();
LPCTSTR p = m_sText.GetData();
while( *p != _T('\0') ) {
sText += m_cPasswordChar;
p = ::CharNext(p);
}
}
RECT rc = m_rcItem;
rc.left += m_rcTextPadding.left;
rc.right -= m_rcTextPadding.right;
rc.top += m_rcTextPadding.top;
rc.bottom -= m_rcTextPadding.bottom;
if( IsEnabled() ) {
CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwTextColor, \
m_iFont, DT_SINGLELINE | m_uTextStyle);
}
else {
CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwDisabledTextColor, \
m_iFont, DT_SINGLELINE | m_uTextStyle);
}
}
}