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
|
#include "./pageAddress.h"
#include "./dataAddress.h"
#include "./common.h"
#include "./addressEncoder.h"
#include "./addressEditbox.h"
#include "./loginGui.h"
#include "../resource.h"
#include <shlwapi.h>
#include <wininet.h>
static HRESULT CALLBACK LoginPageAddress_CreateInstance(HWND hwnd, HWND hLoginbox, LoginPage **instance)
{
if (NULL == instance) return E_POINTER;
if (NULL == hwnd || NULL == hLoginbox) return E_INVALIDARG;
*instance = new LoginPageAddress(hwnd, hLoginbox);
if (NULL == instance) return E_OUTOFMEMORY;
return S_OK;
}
LoginPageAddress::LoginPageAddress(HWND hwnd, HWND hLoginbox)
: LoginPage(hwnd, hLoginbox)
{
HWND hEdit = GetDlgItem(hwnd, IDC_ADDRESS_EDIT);
if (NULL != hEdit)
AddressEditbox_AttachWindow(hEdit);
}
LoginPageAddress::~LoginPageAddress()
{
}
HWND LoginPageAddress::CreatePage(HWND hLoginbox, HWND hParent)
{
return LoginPage::CreatePage(hLoginbox, MAKEINTRESOURCE(IDD_PAGE_ADDRESS),
hParent, NULL, LoginPageAddress_CreateInstance);
}
BOOL LoginPageAddress::OnInitDialog(HWND hFocus, LPARAM param)
{
HFONT fontEdit = NULL, fontLabel = NULL;
LoginGuiObject *loginGui;
if (SUCCEEDED(LoginGuiObject::QueryInstance(&loginGui)))
{
fontLabel = loginGui->GetTextFont();
fontEdit = loginGui->GetEditorFont();
loginGui->Release();
}
if (NULL != fontLabel)
{
HWND hLabel = GetDlgItem(hwnd, IDC_ADDRESS_LABEL);
if (NULL != hLabel)
SendMessage(hLabel, WM_SETFONT, (WPARAM)fontLabel, 0L);
hLabel = GetDlgItem(hwnd, IDC_MESSAGE);
if (NULL != hLabel)
SendMessage(hLabel, WM_SETFONT, (WPARAM)fontLabel, 0L);
}
if (NULL != fontEdit)
{
HWND hEdit = GetDlgItem(hwnd, IDC_ADDRESS_EDIT);
if (NULL != hEdit)
SendMessage(hEdit, WM_SETFONT, (WPARAM)fontEdit, 0L);
}
LoginPage::OnInitDialog(hFocus, param);
return FALSE;
}
void LoginPageAddress::OnDestroy()
{
LoginPage::OnDestroy();
}
void LoginPageAddress::UpdateLayout(BOOL fRedraw)
{
LoginPage::UpdateLayout(fRedraw);
RECT pageRect;
GetPageRect(&pageRect);
UINT flags = SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW;
const INT szControls[] = { IDC_ADDRESS_LABEL, IDC_ADDRESS_EDIT, IDC_MESSAGE, };
HDWP hdwp = BeginDeferWindowPos(ARRAYSIZE(szControls));
INT baseunitX, baseunitY;
if (FALSE == LoginBox_GetWindowBaseUnits(hwnd, &baseunitX, &baseunitY))
{
baseunitY = 13;
}
HRGN invalidRegion = CreateRectRgn(0, 0, 0, 0);
HRGN tempRegion = CreateRectRgn(0, 0, 0, 0);
HWND hControl;
INT nextTop = pageRect.top;
INT x, y, cx, cy;
RECT rect;
INT maxWidth = pageRect.right - pageRect.left;
hControl = GetDlgItem(hwnd, IDC_ADDRESS_EDIT);
if (NULL != hControl)
{
HDC hdc = GetDCEx(hControl, NULL, DCX_CACHE | DCX_NORESETATTRS);
if (NULL != hdc)
{
HFONT fontControl = (HFONT)SendMessage(hControl, WM_GETFONT, 0, 0L);
HFONT fontOrig = (HFONT)SelectObject(hdc, fontControl);
maxWidth = LoginBox_GetAveStrWidth(hdc, 80);
if(maxWidth > (pageRect.right - pageRect.left))
maxWidth = pageRect.right - pageRect.left;
SelectObject(hdc, fontOrig);
ReleaseDC(hControl, hdc);
}
}
for (INT i = 0; i < ARRAYSIZE(szControls); i++)
{
hControl = GetDlgItem(hwnd, szControls[i]);
if (NULL == hControl || FALSE == GetWindowRect(hControl, &rect)) continue;
MapWindowPoints(HWND_DESKTOP, hwnd, (POINT*)&rect, 2);
x = rect.left;
y = rect.top;
cx = rect.right - rect.left;
cy = rect.bottom - rect.top;
switch(szControls[i])
{
case IDC_ADDRESS_LABEL:
case IDC_MESSAGE:
x = pageRect.left;
y = nextTop;
LoginBox_GetWindowTextSize(hControl, maxWidth, &cx, &cy);
nextTop += (cy + MulDiv(2, baseunitY, 8));
break;
case IDC_ADDRESS_EDIT:
x = pageRect.left;
y = nextTop;
cy = LoginBox_GetWindowTextHeight(hControl, 0);
{
RECT r1, r2;
GetWindowRect(hControl, &r1);
SendMessage(hControl, EM_GETRECT, 0, (LPARAM)&r2);
INT t = (r1.bottom - r1.top) - (r2.bottom - r2.top);
cy += t;
}
cx = maxWidth;
nextTop += (cy + MulDiv(7, baseunitY, 8));
break;
}
if (rect.left != x || rect.top != y ||
(rect.right - rect.left) != cx || (rect.bottom - rect.top) != cy)
{
hdwp = DeferWindowPos(hdwp, hControl, NULL, x, y, cx, cy, flags);
if (NULL == hdwp) break;
if (FALSE != fRedraw)
{
SetRectRgn(tempRegion, rect.left, rect.top, rect.right, rect.bottom);
CombineRgn(invalidRegion, invalidRegion, tempRegion, RGN_OR);
SetRectRgn(tempRegion, x, y , x + cx, y + cy);
CombineRgn(invalidRegion, invalidRegion, tempRegion, RGN_OR);
}
}
}
if (NULL != hdwp)
EndDeferWindowPos(hdwp);
if (FALSE != fRedraw)
{
RedrawWindow(hwnd, NULL, invalidRegion,
RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE | RDW_ERASENOW | RDW_ALLCHILDREN | RDW_VALIDATE);
}
if (NULL != invalidRegion)
DeleteObject(invalidRegion);
if (NULL != tempRegion)
DeleteObject(tempRegion);
}
BOOL LoginPageAddress::OnGetLoginData(LoginData **ppLoginData)
{
if (NULL == ppLoginData)
return FALSE;
HWND hEdit;
LPWSTR address;
hEdit = GetDlgItem(hwnd, IDC_ADDRESS_EDIT);
if (NULL == hEdit || FAILED(LoginBox_GetWindowText(hEdit, &address, NULL)))
address = NULL;
else
{
size_t encodedMax = lstrlen(address)*2;
LPWSTR addressEncoded = LoginBox_MallocString(encodedMax);
for(;;)
{
if (FALSE == InternetCanonicalizeUrl(address, addressEncoded, (DWORD*)&encodedMax, ICU_ENCODE_PERCENT | ICU_NO_META))
{
if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
{
LoginBox_FreeString(addressEncoded);
addressEncoded = LoginBox_MallocString(encodedMax);
if (NULL != addressEncoded)
continue;
}
}
else
{
LoginBox_FreeString(address);
address = LoginBox_CopyString(addressEncoded);
}
break;
}
HRESULT hr;
for(;;)
{
hr = AddressEncoder_EncodeString(address, addressEncoded, &encodedMax, ICU_BROWSER_MODE);
if (ENC_E_INSUFFICIENT_BUFFER == hr)
{
LoginBox_FreeString(addressEncoded);
addressEncoded = LoginBox_MallocString(encodedMax);
if (NULL == addressEncoded)
hr = E_OUTOFMEMORY;
else
continue;
}
break;
}
if (SUCCEEDED(hr))
{
LoginBox_FreeString(address);
address = addressEncoded;
addressEncoded = NULL;
}
else
LoginBox_FreeString(addressEncoded);
}
HRESULT hr = LoginDataAddress::CreateInstance(NULL, hwnd, hLoginbox,
address, (LoginDataAddress**)ppLoginData);
LoginBox_FreeString(address);
return SUCCEEDED(hr);
}
BOOL LoginPageAddress::OnSetUsername(LPCWSTR pszUsername)
{
HWND hEdit = GetDlgItem(hwnd, IDC_ADDRESS_EDIT);
if (NULL == hEdit) return FALSE;
if (NULL == pszUsername || L'\0' == *pszUsername)
return FALSE;
INT cchLen = lstrlen(pszUsername);
INT f, l;
SNDMSG(hEdit, EM_GETSEL, (WPARAM)&f, (LPARAM)&l);
if (f == l) return FALSE;
SNDMSG(hEdit, EM_REPLACESEL, FALSE, (LPARAM)pszUsername);
SNDMSG(hEdit, EM_SETSEL, (WPARAM)f, (LPARAM)f + cchLen);
return TRUE;
}
HBRUSH LoginPageAddress::OnGetStaticColor(HDC hdc, HWND hControl)
{
INT_PTR controlId = (INT_PTR)GetWindowLongPtr(hControl, GWLP_ID);
switch(controlId)
{
case IDC_MESSAGE:
SetTextColor(hdc, rgbSecondaryText);
SetBkColor(hdc, rgbBack);
return hbrBack;
}
return LoginPage::OnGetStaticColor(hdc, hControl);
}
BOOL LoginPageAddress::OnSetAddress(LPCWSTR pszAddress, BOOL replaceUsername)
{
HWND hEdit = GetDlgItem(hwnd, IDC_ADDRESS_EDIT);
if (NULL == hEdit) return FALSE;
BOOL succeeded;
LPWSTR addressDecoded;
if (SUCCEEDED(AddressEncoder_DecodeString(pszAddress, &addressDecoded)))
{
succeeded = (BOOL)SNDMSG(hEdit, WM_SETTEXT, 0, (LPARAM)addressDecoded);
LoginBox_FreeString(addressDecoded);
}
else
succeeded = (BOOL)SNDMSG(hEdit, WM_SETTEXT, 0, (LPARAM)pszAddress);
return succeeded;
}
BOOL LoginPageAddress::OnSetAddressTitle(LPCWSTR pszTitle)
{
return SetLabelText(IDC_ADDRESS_LABEL, pszTitle);
}
BOOL LoginPageAddress::OnSetMessage(LPCWSTR pszMessage)
{
HWND hLabel = GetDlgItem(hwnd, IDC_MESSAGE);
if (NULL == hLabel) return FALSE;
return SetWindowText(hLabel, pszMessage);
}
INT_PTR LoginPageAddress::DialogProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case NLPAM_SETADDRESS: MSGRESULT(hwnd, OnSetAddress((LPCWSTR)lParam, (BOOL)wParam));
case NLPAM_SETADDRESSTITLE: MSGRESULT(hwnd, OnSetAddressTitle((LPCWSTR)lParam));
case NLPAM_SETMESSAGE: MSGRESULT(hwnd, OnSetMessage((LPCWSTR)lParam));
}
return __super::DialogProc(uMsg, wParam, lParam);
}
|