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
|
#include "./loginGui.h"
#include "./common.h"
#include "./imageLoader.h"
#include "../resource.h"
#include "../api.h"
#include <strsafe.h>
static size_t threadStorage = TLS_OUT_OF_INDEXES;
LoginGuiObject::LoginGuiObject()
: ref(1), bitmapIcons(NULL), fontTitle(NULL), fontEditor(NULL), fontText(NULL)
{
}
LoginGuiObject::~LoginGuiObject()
{
Reset();
}
HRESULT LoginGuiObject::InitializeThread()
{
if (TLS_OUT_OF_INDEXES == threadStorage)
{
if (NULL == WASABI_API_APP)
return E_UNEXPECTED;
threadStorage = WASABI_API_APP->AllocateThreadStorage();
if (TLS_OUT_OF_INDEXES == threadStorage)
return E_UNEXPECTED;
}
LoginGuiObject *cache = (LoginGuiObject*)WASABI_API_APP->GetThreadStorage(threadStorage);
if (NULL != cache)
{
cache->AddRef();
return S_FALSE;
}
if (NULL == cache)
{
cache = new LoginGuiObject();
if (NULL == cache) return E_OUTOFMEMORY;
WASABI_API_APP->SetThreadStorage(threadStorage, cache);
}
return S_OK;
}
HRESULT LoginGuiObject::UninitializeThread()
{
if (TLS_OUT_OF_INDEXES == threadStorage)
return E_FAIL;
if (NULL == WASABI_API_APP)
return E_UNEXPECTED;
LoginGuiObject *cache = (LoginGuiObject*)WASABI_API_APP->GetThreadStorage(threadStorage);
if (NULL != cache && 0 == cache->Release())
WASABI_API_APP->SetThreadStorage(threadStorage, NULL);
return S_OK;
}
HRESULT LoginGuiObject::QueryInstance(LoginGuiObject **instance)
{
if (NULL == instance)
return E_POINTER;
if (TLS_OUT_OF_INDEXES == threadStorage)
return E_UNEXPECTED;
*instance = (LoginGuiObject*)WASABI_API_APP->GetThreadStorage(threadStorage);
if (NULL == *instance) return E_FAIL;
(*instance)->AddRef();
return S_OK;
}
ULONG LoginGuiObject::AddRef()
{
return InterlockedIncrement((LONG*)&ref);
}
ULONG LoginGuiObject::Release()
{
if (0 == ref)
return ref;
LONG r = InterlockedDecrement((LONG*)&ref);
if (0 == r)
delete(this);
return r;
}
HRESULT LoginGuiObject::Reset()
{
if (NULL != bitmapIcons)
{
DeleteObject(bitmapIcons);
bitmapIcons = NULL;
}
if (NULL != fontTitle)
{
DeleteObject(fontTitle);
fontTitle = NULL;
}
if (NULL != fontEditor)
{
DeleteObject(fontEditor);
fontEditor = NULL;
}
if (NULL != fontText)
{
DeleteObject(fontText);
fontText = NULL;
}
return S_OK;
}
HRESULT LoginGuiObject::GetIconDimensions(INT *pWidth, INT *pHeight)
{
if (NULL == bitmapIcons)
{
INT width, height;
bitmapIcons = ImageLoader_LoadBitmap(WASABI_API_ORIG_HINST, MAKEINTRESOURCE(IDR_NOTIFIERICONS_IMAGE),
TRUE, &width, &height);
if (NULL == bitmapIcons)
return E_FAIL;
if (height < 0) height = -height;
if (NULL != pWidth) *pWidth = width;
if (NULL != pHeight) *pHeight = width;
return S_OK;
}
BITMAP bm;
if (sizeof(bm) != GetObject(bitmapIcons, sizeof(bm), &bm))
return E_FAIL;
if (NULL != pWidth) *pWidth = bm.bmWidth;
if (NULL != pHeight) *pHeight = bm.bmWidth;
return S_OK;
}
HBITMAP LoginGuiObject::GetIcon(INT iconId, RECT *prcIcon)
{
if (NULL == prcIcon || iconId < 0)
return NULL;
INT width, height;
if (NULL != bitmapIcons)
{
BITMAP bm;
if (sizeof(bm) != GetObject(bitmapIcons, sizeof(bm), &bm))
bitmapIcons = NULL;
else
{
width = bm.bmWidth;
height = bm.bmHeight;
}
}
if (NULL == bitmapIcons)
{
bitmapIcons = ImageLoader_LoadBitmap(WASABI_API_ORIG_HINST, MAKEINTRESOURCE(IDR_NOTIFIERICONS_IMAGE),
TRUE, &width, &height);
if (NULL == bitmapIcons)
return NULL;
}
if (height < 0) height = -height;
if (width * (iconId + 1) > height)
return NULL;
prcIcon->left = 0;
prcIcon->right = width;
prcIcon->top = width * iconId;
prcIcon->bottom = prcIcon->top + width;
return bitmapIcons;
}
static HFONT LoginGuiObject_DuplicateFont(HFONT fontBase, INT heightDeltaPt)
{
if (NULL == fontBase) return NULL;
LOGFONT lf;
if (sizeof(lf) != GetObject(fontBase, sizeof(lf), &lf))
return NULL;
if (0 != heightDeltaPt)
{
HDC hdc = GetDCEx(NULL, NULL, DCX_WINDOW | DCX_CACHE | DCX_NORESETATTRS);
HDC hdcTmp = NULL;
if (NULL != hdc)
{
hdcTmp = CreateCompatibleDC(hdc);
ReleaseDC(NULL, hdc);
}
if (NULL == hdcTmp)
return NULL;
LONG pixelsY = GetDeviceCaps (hdcTmp, LOGPIXELSY);
HFONT fontOrig = (HFONT)SelectObject(hdcTmp, fontBase);
TEXTMETRIC tm;
if (FALSE != GetTextMetrics(hdcTmp, &tm))
{
INT basePt = MulDiv(tm.tmHeight - tm.tmInternalLeading, 72, pixelsY);
lf.lfHeight = -MulDiv((basePt + heightDeltaPt), pixelsY, 72);
}
SelectObject(hdcTmp, fontOrig);
DeleteDC(hdcTmp);
}
return CreateFontIndirect(&lf);
}
HFONT LoginGuiObject::GetTitleFont()
{
if (NULL == fontTitle)
{
HFONT fontBase = GetTextFont();
if (NULL != fontBase)
{
fontTitle = LoginGuiObject_DuplicateFont(fontBase, 3);
}
}
return fontTitle;
}
HFONT LoginGuiObject::GetEditorFont()
{
if (NULL == fontEditor)
{
HFONT fontBase = GetTextFont();
if (NULL != fontBase)
{
fontEditor = LoginGuiObject_DuplicateFont(fontBase, 0);
}
}
return fontEditor;
}
HFONT LoginGuiObject::GetTextFont()
{
if (NULL == fontText)
{
LOGFONT lf;
if (FALSE != SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0))
{
lf.lfQuality = LoginBox_GetSysFontQuality();
fontText = CreateFontIndirect(&lf);
}
}
return fontText;
}
|