soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SHotKeyCtrl.cpp
1#include "souistd.h"
2#include "control/SHotKeyCtrl.h"
3
4SNSBEGIN
5
7{
8 m_wInvalidModifier = Mod_None;
9 m_wInvalidComb = HKCOMB_NONE;
10 m_bInSetting = FALSE;
11 m_bFocusable = TRUE;
12 m_evtSet.addEvent(EVENTID(EventSetHotKey));
13}
14
18
19void SHotKeyCtrl::OnLButtonDown(UINT nFlags, CPoint pt)
20{
21 __baseCls::OnLButtonDown(nFlags, pt);
23}
24
26{
27 SPainter painter;
28 BeforePaint(pRT, painter);
29 CRect rcClient;
30 GetTextRect(&rcClient);
31 SStringT str = GetWindowText();
32 UINT uAlign = GetTextAlign();
33 pRT->DrawText(str, str.GetLength(), &rcClient, uAlign);
34 AfterPaint(pRT, painter);
35}
36
38{
39 SStringT str = GetWindowText();
40 SIZE szTxt;
41 pRT->MeasureText(str, str.GetLength(), &szTxt);
42
43 CRect rcClient;
44 GetTextRect(&rcClient);
45
46 UINT uAlign = GetTextAlign();
47 int x = rcClient.left + szTxt.cx;
48 int y = rcClient.top + (rcClient.Height() - szTxt.cy) / 2;
49 if (uAlign & DT_CENTER)
50 x += (rcClient.Width() - szTxt.cx) / 2;
51 else if (uAlign & DT_RIGHT)
52 x = rcClient.right;
53
54 SetCaretPos(x, y);
55}
56
58{
59 UINT uAlign = SWindow::GetTextAlign();
60 uAlign &= ~DT_BOTTOM;
61 uAlign |= DT_VCENTER | DT_SINGLELINE;
62 return uAlign;
63}
64
65void SHotKeyCtrl::OnSetFocus(SWND wndOld)
66{
68 BeforePaintEx(pRT);
69 SIZE szTxt;
70 pRT->MeasureText(_T("A"), 1, &szTxt);
71
72 CreateCaret(NULL, 1, szTxt.cy);
73
74 UpdateCaret(pRT);
75
77
78 GetContainer()->EnableIME(FALSE);
79 ShowCaret(TRUE);
80
81 __baseCls::OnSetFocus(wndOld);
82}
83
84void SHotKeyCtrl::OnKillFocus(SWND wndFocus)
85{
86 ShowCaret(FALSE);
87 GetContainer()->EnableIME(TRUE);
88 __baseCls::OnKillFocus(wndFocus);
89}
90
92{
93 BOOL bAlt = GetKeyState(VK_MENU) & 0x8000;
94 BOOL bCtrl = GetKeyState(VK_CONTROL) & 0x8000;
95 BOOL bShift = GetKeyState(VK_SHIFT) & 0x8000;
96
97 WORD wCombKey = 0;
98 WORD wCombMask = HKCOMB_NONE;
99 if (!bAlt && !bCtrl && !bShift)
100 wCombKey = Mod_None;
101 else if (bAlt && !bCtrl && !bShift)
102 wCombKey = Mod_Alt, wCombMask = HKCOMB_A;
103 else if (!bAlt && bCtrl && !bShift)
104 wCombKey = Mod_Ctrl, wCombMask = HKCOMB_C;
105 else if (!bAlt && !bCtrl && bShift)
106 wCombKey = Mod_Shift, wCombMask = HKCOMB_S;
107 else if (bAlt && bCtrl && !bShift)
108 wCombKey = Mod_CA, wCombMask = HKCOMB_CA;
109 else if (bAlt && !bCtrl && bShift)
110 wCombKey = Mod_SA, wCombMask = HKCOMB_SA;
111 else if (!bAlt && bCtrl && bShift)
112 wCombKey = Mod_SC, wCombMask = HKCOMB_SC;
113 else
114 wCombKey = Mod_SCA, wCombMask = HKCOMB_SCA;
115 if (wCombMask & m_wInvalidComb)
117 else
118 m_wModifier = wCombKey;
119
120 EventSetHotKey ev(this);
121 ev.wModifiers = m_wModifier;
122 ev.vKey = m_wVK;
123 FireEvent(ev);
124}
125
126void SHotKeyCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
127{
128 if (!m_bInSetting)
129 {
130 m_bInSetting = TRUE;
131 m_wVK = 0;
133 }
134 SStringT strKey = GetKeyName(nChar);
135 if (!strKey.IsEmpty())
136 {
137 m_wVK = nChar;
138 }
141 BeforePaintEx(pRT);
142 UpdateCaret(pRT);
144 Invalidate();
145}
146
147void SHotKeyCtrl::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
148{
149 if (!m_bInSetting)
150 return;
151
153 BeforePaintEx(pRT);
154
155 if (nChar == m_wVK)
156 {
157 m_bInSetting = FALSE;
158 }
159 else if (m_wVK == 0 && (GetKeyState(VK_SHIFT) & 0x8000) == 0 && (GetKeyState(VK_MENU) & 0x8000) == 0 && (GetKeyState(VK_CONTROL) & 0x8000) == 0)
160 {
161 m_bInSetting = FALSE;
163 UpdateCaret(pRT);
164 Invalidate();
165 }
166 else if (nChar == VK_SHIFT || nChar == VK_MENU || nChar == VK_CONTROL)
167 {
169 UpdateCaret(pRT);
170 Invalidate();
171 }
173}
174
175void SHotKeyCtrl::OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
176{
177 if (GetKeyState(VK_MENU) & 0x8000 || nChar == VK_F10)
178 OnKeyDown(nChar, nRepCnt, nFlags);
179 else
180 SetMsgHandled(FALSE);
181}
182
183void SHotKeyCtrl::OnSysKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
184{
185 if (nChar == VK_MENU || GetKeyState(VK_MENU) & 0x8000 || nChar == VK_F10)
186 OnKeyUp(nChar, nRepCnt, nFlags);
187 else
188 SetMsgHandled(FALSE);
189}
190
191void SHotKeyCtrl::SetRule(WORD wInvalidComp, WORD wModifier)
192{
193 m_wInvalidComb = wInvalidComp;
194 m_wInvalidModifier = wModifier;
195}
196
197void SHotKeyCtrl::SetHotKey(WORD vKey, WORD wModifiers)
198{
199 m_wVK = vKey;
200 m_wModifier = wModifiers;
202 BeforePaintEx(pRT);
203 UpdateCaret(pRT);
205 Invalidate();
206}
207
208void SHotKeyCtrl::GetHotKey(WORD *wKey, WORD *wModifers) const
209{
210 if (wKey)
211 *wKey = m_wVK;
212 if (wModifers)
213 *wModifers = m_wModifier;
214}
215
216SStringT SHotKeyCtrl::GetWindowText(BOOL bRawText)
217{
218 return FormatHotkey();
219}
220
221HRESULT SHotKeyCtrl::OnAttrInvalidComb(const SStringW &value, BOOL bLoading)
222{
223 SStringWList arrComb;
224 SplitString(value, L'|', arrComb);
225 for (UINT i = 0; i < arrComb.GetCount(); i++)
226 {
227 DWORD dwKey = TranslateAccelKey(S_CW2T(arrComb[i]));
228 switch (HIWORD(dwKey))
229 {
230 case Mod_Alt:
231 m_wInvalidComb |= HKCOMB_A;
232 break;
233 case Mod_Ctrl:
234 m_wInvalidComb |= HKCOMB_C;
235 break;
236 case Mod_Shift:
237 m_wInvalidComb |= HKCOMB_S;
238 break;
239 case Mod_CA:
240 m_wInvalidComb |= HKCOMB_CA;
241 break;
242 case Mod_SA:
243 m_wInvalidComb |= HKCOMB_SA;
244 break;
245 case Mod_SC:
246 m_wInvalidComb |= HKCOMB_SC;
247 break;
248 case Mod_SCA:
249 m_wInvalidComb |= HKCOMB_SCA;
250 break;
251 }
252 }
253 return bLoading ? S_OK : S_FALSE;
254}
255
256HRESULT SHotKeyCtrl::OnAttrInvalidSysKey(const SStringW &value, BOOL bLoading)
257{
258 SStringWList arrComb;
259 SplitString(value, L'|', arrComb);
260 for (UINT i = 0; i < arrComb.GetCount(); i++)
261 {
262 DWORD dwKey = TranslateAccelKey(S_CW2T(arrComb[i]));
263 switch (HIWORD(dwKey))
264 {
265 case Mod_Alt:
266 m_wInvalidComb = HKCOMB_A | HKCOMB_CA | HKCOMB_SA | HKCOMB_SCA;
267 break;
268 case Mod_Ctrl:
269 m_wInvalidComb = HKCOMB_C | HKCOMB_CA | HKCOMB_SC | HKCOMB_SCA;
270 break;
271 case Mod_Shift:
272 m_wInvalidComb = HKCOMB_S | HKCOMB_SA | HKCOMB_SC | HKCOMB_SCA;
273 break;
274 }
275 }
276 return bLoading ? S_OK : S_FALSE;
277}
278
279HRESULT SHotKeyCtrl::OnAttrInvalidModifier(const SStringW &value, BOOL bLoading)
280{
281 DWORD dwKey = TranslateAccelKey(S_CW2T(value));
282 m_wInvalidModifier = HIWORD(dwKey);
283 return S_FALSE;
284}
285
286HRESULT SHotKeyCtrl::OnAttrHotKey(const SStringW &value, BOOL bLoading)
287{
288 DWORD dwKey = TranslateAccelKey(S_CW2T(value));
289 m_wModifier = HIWORD(dwKey);
290 m_wVK = LOWORD(dwKey);
291 return bLoading ? S_OK : S_FALSE;
292}
293
294SNSEND
@ GRT_NODRAW
static DWORD TranslateAccelKey(LPCTSTR pszKeyName)
Translates a string to an accelerator key value.
static SStringT GetKeyName(WORD vk)
Converts a virtual key code to its string representation.
SStringT FormatHotkey()
Formats the accelerator key as a string.
void SetHotKey(WORD wKey, WORD wModifiers) OVERRIDE
Set the hotkey.
void UpdateCaret(IRenderTarget *pRT)
Update the caret position.
virtual ~SHotKeyCtrl(void)
Destructor.
SHotKeyCtrl(void)
Constructor.
void OnLButtonDown(UINT nFlags, CPoint pt)
Handle left mouse button down event.
WORD m_wInvalidComb
void SetRule(WORD wInvalidComp, WORD wModifier) OVERRIDE
Set the rules for invalid combinations and modifiers.
HRESULT OnAttrInvalidComb(const SStringW &value, BOOL bLoading)
Handle custom attribute "invalidComb".
HRESULT OnAttrInvalidModifier(const SStringW &value, BOOL bLoading)
Handle custom attribute "invalidModifier".
HRESULT OnAttrInvalidSysKey(const SStringW &value, BOOL bLoading)
Handle custom attribute "invalidSysKey".
void OnPaint(IRenderTarget *pRT)
Paint the control.
void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
Handle key down event.
HRESULT OnAttrHotKey(const SStringW &value, BOOL bLoading)
Handle custom attribute "hotKey".
void OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
Handle system key down event.
UINT GetTextAlign()
Get the text alignment.
BOOL m_bInSetting
void OnSysKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
Handle system key up event.
void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
Handle key up event.
virtual SStringT GetWindowText(BOOL bRawText=FALSE)
Get the window text.
void UpdateModifier()
Update the modifier keys.
WORD m_wInvalidModifier
void OnKillFocus(SWND wndFocus)
Handle kill focus event.
void OnSetFocus(SWND wndOld)
Handle set focus event.
void GetHotKey(WORD *wKey, WORD *wModifiers) SCONST OVERRIDE
Get the hotkey.
Helper class for painting.
Definition SWnd.h:178
A class representing an ASCII string.
Definition sstringw.h:96
BOOL FireEvent(IEvtArgs *evt) OVERRIDE
Fires an event.
Definition Swnd.cpp:1540
void SetCaretPos(int x, int y) OVERRIDE
Sets the caret position.
Definition Swnd.cpp:3077
void ReleaseRenderTarget(IRenderTarget *pRT)
Releases the RenderTarget obtained via GetRenderTarget.
Definition Swnd.cpp:2372
UINT GetTextAlign() const
Retrieves the text alignment of the window.
Definition Swnd.cpp:218
ISwndContainer * GetContainer() OVERRIDE
Retrieves the container associated with this window.
Definition Swnd.cpp:679
virtual void BeforePaint(IRenderTarget *pRT, SPainter &painter)
Prepare rendering environment.
Definition Swnd.cpp:1755
BOOL m_bFocusable
Definition SWnd.h:2609
virtual void GetTextRect(LPRECT pRect)
Calculate text display rectangle.
Definition Swnd.cpp:1961
virtual void AfterPaint(IRenderTarget *pRT, SPainter &painter)
Restore rendering environment.
Definition Swnd.cpp:1776
void ShowCaret(BOOL bShow) OVERRIDE
Shows or hides the caret.
Definition Swnd.cpp:3066
void Invalidate() OVERRIDE
Invalidates the entire window.
Definition Swnd.cpp:1437
BOOL CreateCaret(HBITMAP pBmp, int nWid, int nHeight) OVERRIDE
Creates a caret.
Definition Swnd.cpp:3038
void SetMsgHandled(BOOL bHandled)
Sets the message handled flag.
Definition Swnd.cpp:212
SEventSet m_evtSet
Definition SWnd.h:2581
SWND m_swnd
Member variables representing various properties of the window.
Definition SWnd.h:2577
void BeforePaintEx(IRenderTarget *pRT)
Prepares the drawing environment for the current window's RenderTarget, starting from the top-level w...
Definition Swnd.cpp:1767
IRenderTarget * GetRenderTarget(LPCRECT pRc=NULL, GrtFlag gdcFlags=GRT_NODRAW, BOOL bClientRT=TRUE)
Retrieves a memory DC compatible with the SWND window.
Definition Swnd.cpp:2320
Interface for rendering target objects.
Definition SRender-i.h:1440
HRESULT DrawText(LPCTSTR pszText, int cchLen, LPRECT pRc, UINT uFormat) PURE
Draw text within a rectangle.
HRESULT MeasureText(LPCTSTR pszText, int cchLen, SIZE *psz) PURE
Measure the size of the text.
void OnSetSwndFocus(SWND swnd) PURE
Sets the focus to the specified Swnd object.
void EnableIME(BOOL bEnable) PURE
Enables or disables the input method editor (IME).