-
Notifications
You must be signed in to change notification settings - Fork 4
/
ModelessDialog.h
176 lines (150 loc) · 4.4 KB
/
ModelessDialog.h
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
#pragma once
#define LODWORD(l) ((DWORD)((DWORDLONG)(l)))
#define HIDWORD(l) ((DWORD)(((DWORDLONG)(l)>>32)&0xFFFFFFFF))
#define MAKEDWORDLONG(a,b) ((DWORDLONG)(((DWORD)(a))|(((DWORDLONG)((DWORD)(b)))<<32)))
#define WM_CLOSEDIALOG WM_USER+100
static RECT dialogRect = {-1,-1,-1,-1};
template <class T, class TBase = CWindow>
class ATL_NO_VTABLE CModelessDialogImpl : public CDialogImplBaseT<TBase>
{
protected:
static CSimpleMap<DWORD, ULONGLONG>& table()
{
// ULONGLONG == HHOOK - the counter of number of dialogs to show
// When it's 0 a hook can be released
static CSimpleMap<DWORD, ULONGLONG> hooks;
return hooks;
}
static CSimpleArray<HWND>& windows()
{
static CSimpleArray<HWND> handles;
return handles;
}
static LRESULT FAR PASCAL GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
LPMSG lpMsg = (LPMSG)lParam;
if(nCode >= 0 && PM_REMOVE == wParam)
{
if ((lpMsg->message == WM_KEYDOWN &&
lpMsg->wParam == VK_RETURN || lpMsg->wParam == VK_ESCAPE ||
lpMsg->wParam == VK_TAB || lpMsg->wParam == VK_MENU) ||
(lpMsg->message == WM_CHAR &&
lpMsg->wParam == 'F' || lpMsg->wParam == 'R' || lpMsg->wParam == 'A' ||
lpMsg->wParam == 'f' || lpMsg->wParam == 'r' || lpMsg->wParam == 'a' ||
// Russian shortcuts
lpMsg->wParam == L'Ç' || lpMsg->wParam == L'È' || lpMsg->wParam == L'Â' ||
lpMsg->wParam == L'ç' || lpMsg->wParam == L'è' || lpMsg->wParam == L'â' ||
// Ukrainian shortcuts
lpMsg->wParam == L'Ó' || lpMsg->wParam == L'Í' ||
lpMsg->wParam == L'ó' || lpMsg->wParam == L'í'))
{
for(int i = 0, m = windows().GetSize(); i < m; ++i)
{
if(::IsDialogMessage(windows()[i], lpMsg))
{
lpMsg->message = WM_NULL;
lpMsg->lParam = 0;
lpMsg->wParam = 0;
}
}
}
}
HHOOK hook = (HHOOK)(DWORD_PTR)LODWORD(table().Lookup(::GetCurrentThreadId()));
ATLASSERT(hook);
return CallNextHookEx(hook, nCode, wParam, lParam);
}
void RemoveWindow(HWND hWnd)
{
if(!hWnd || windows().Remove(hWnd))
{
DWORD threadId = ::GetCurrentThreadId();
ULONGLONG data = table().Lookup(threadId);
if( data )
{
HHOOK hook = (HHOOK)(DWORD_PTR)LODWORD(data);
DWORD counter = HIDWORD(data);
if(1 == counter)
{
ATLASSERT( hook );
::UnhookWindowsHookEx(hook);
table().Remove(threadId);
}
else
{
--counter;
table().SetAt(threadId, MAKEDWORDLONG((DWORD_PTR)hook, counter));
}
}
}
}
bool m_valid;
int m_retCode;
public:
CModelessDialogImpl():m_valid(false) {};
HWND ShowDialog(HWND hWndParent = ::GetActiveWindow(), LPARAM dwInitParam = NULL)
{
ATLASSERT(m_hWnd == NULL);
ATLASSERT(::IsWindow(hWndParent));
_AtlWinModule.AddCreateWndData(&m_thunk.cd, (CDialogImplBaseT<TBase>*)this);
HWND hWnd = ::CreateDialogParam(_AtlBaseModule.GetResourceInstance(),
MAKEINTRESOURCE(static_cast<T*>(this)->IDD),
hWndParent,
T::StartDialogProc,
dwInitParam);
ATLASSERT(m_hWnd == hWnd);
DWORD threadId = ::GetCurrentThreadId();
if(-1 == table().FindKey(threadId))
{
HHOOK hook = ::SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)GetMsgProc,
NULL, GetCurrentThreadId());
table().Add(threadId, MAKEDWORDLONG((DWORD_PTR)hook,1));
}
else
{
ULONGLONG data = table().Lookup(threadId);
HHOOK hook = (HHOOK)(DWORD_PTR)LODWORD(data);
DWORD counter = HIDWORD(data) + 1;
table().SetAt(threadId, MAKEDWORDLONG((DWORD_PTR)hook, counter));
}
windows().Add(m_hWnd);
if (dialogRect.left != -1)
::SetWindowPos(m_hWnd, HWND_TOP, dialogRect.left, dialogRect.top, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW);
else
::ShowWindow(m_hWnd, SW_SHOW);
m_valid = true;
return hWnd;
}
INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow(), LPARAM dwInitParam = NULL )
{
MSG msg;
m_retCode = -1;
ShowDialog(hWndParent, dwInitParam);
while (GetMessage(&msg, 0, 0, 0) > 0 && m_retCode == -1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_CLOSEDIALOG) m_retCode = IDCANCEL;
}
EndDialog(m_retCode);
return m_retCode;
}
BOOL DestroyWindow()
{
ATLASSERT(m_hWnd);
RemoveWindow(m_hWnd);
m_valid = false;
::GetWindowRect(m_hWnd, &dialogRect);
return ::DestroyWindow(m_hWnd);
}
BOOL EndDialog(int nRetCode)
{
ATLASSERT(m_hWnd);
DestroyWindow();
m_retCode = nRetCode;
return ::EndDialog(m_hWnd, nRetCode);
}
BOOL IsValid()
{
return m_valid;
}
};