-
Notifications
You must be signed in to change notification settings - Fork 0
/
linklabel.c
292 lines (258 loc) · 8.65 KB
/
linklabel.c
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
// Copyright (C) 2007 Andrey Gruber (aka lamer)
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include <windows.h>
#include <windowsx.h>
#include <wchar.h>
#include <commctrl.h>
#include "linklabel.h"
#define LL_CLASS "_agLinkLabel_"
#define LL_CLASSW L"_agLinkLabelW_"
#define LL_ALIGN "LL_ALIGN"
#define LL_ALIGNW L"LL_ALIGN"
LRESULT CALLBACK LLabel_WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static void LLabel_OnPaint(HWND hwnd);
static void LLabel_OnDestroy(HWND hwnd);
static BOOL LLabel_OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct);
static void LLabel_OnLButtonUp(HWND hwnd, int x, int y, UINT keyFlags);
static void LLabel_OnSetText(HWND hwnd, LPCTSTR lpszText);
static void SetLLText(HWND hwnd, char * lpText);
static void SetLLTextW(HWND hwnd, wchar_t * lpText);
static BOOL CALLBACK PropEnumProc(HWND hwnd, LPCSTR lpszString, HANDLE hData);
static BOOL CALLBACK PropEnumProcW(HWND hwnd, LPCWSTR lpszString, HANDLE hData);
static BOOL m_Registered = FALSE, m_RegisteredW = FALSE;
static BOOL RegisterLinkLable(HINSTANCE hInstance){
WNDCLASSEX wcx;
ZeroMemory(&wcx, sizeof(wcx));
wcx.cbSize = sizeof(wcx);
wcx.cbWndExtra = 8;
wcx.hCursor = LoadCursor(NULL, IDC_HAND);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.hInstance = hInstance;
wcx.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wcx.lpszClassName = LL_CLASS;
wcx.lpfnWndProc = LLabel_WndProc;
m_Registered = (BOOL)RegisterClassEx(&wcx);
if(!m_Registered)
if(GetLastError() == ERROR_CLASS_ALREADY_EXISTS)
m_Registered = TRUE;
return m_Registered;
}
static BOOL RegisterLinkLableW(HINSTANCE hInstance){
WNDCLASSEXW wcx;
ZeroMemory(&wcx, sizeof(wcx));
wcx.cbSize = sizeof(wcx);
wcx.cbWndExtra = 8;
wcx.hCursor = LoadCursor(NULL, IDC_HAND);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.hInstance = hInstance;
wcx.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wcx.lpszClassName = LL_CLASSW;
wcx.lpfnWndProc = LLabel_WndProc;
m_RegisteredW = (BOOL)RegisterClassExW(&wcx);
if(!m_RegisteredW)
if(GetLastError() == ERROR_CLASS_ALREADY_EXISTS)
m_RegisteredW = TRUE;
return m_RegisteredW;
}
HWND CreateLinkLable(HWND hParent, HINSTANCE hInstance, char * lpText, int x, int y, int w, int h, int id, BOOL rtl){
HWND hwnd;
if(!m_Registered)
if(!RegisterLinkLable(hInstance))
return NULL;
hwnd = CreateWindowEx(0, LL_CLASS, lpText, WS_CHILD | WS_VISIBLE, x, y, w, h, hParent, (HMENU)id, hInstance, NULL);
SetProp(hwnd, LL_ALIGN, (HANDLE)rtl);
if(!hwnd)
return NULL;
SetLLText(hwnd, lpText);
return hwnd;
}
HWND CreateLinkLableW(HWND hParent, HINSTANCE hInstance, wchar_t * lpText, int x, int y, int w, int h, int id, BOOL rtl){
HWND hwnd;
if(!m_RegisteredW)
if(!RegisterLinkLableW(hInstance))
return NULL;
hwnd = CreateWindowExW(0, LL_CLASSW, lpText, WS_CHILD | WS_VISIBLE, x, y, w, h, hParent, (HMENU)id, hInstance, NULL);
SetPropW(hwnd, LL_ALIGNW, (HANDLE)rtl);
if(!hwnd)
return NULL;
SetLLTextW(hwnd, lpText);
return hwnd;
}
static void SetLLText(HWND hwnd, char * lpText){
char * pText;
SIZE sz;
HDC hdc;
int state;
HFONT hFont;
RECT rc;
GetWindowRect(hwnd, &rc);
MapWindowPoints(HWND_DESKTOP, GetParent(hwnd), (LPPOINT)&rc, 2);
pText = (char *)GetWindowLongPtr(hwnd, 0);
if(pText)
free(pText);
pText = (char *)calloc(strlen(lpText) + 2, sizeof(char));
strcpy(pText, lpText);
SetWindowLongPtr(hwnd, 0, (LONG_PTR)pText);
hdc = GetDC(hwnd);
state = SaveDC(hdc);
hFont = CreateFontIndirect((LPLOGFONT)GetWindowLongPtr(hwnd, 4));
SelectFont(hdc, hFont);
GetTextExtentPoint32(hdc, lpText, strlen(lpText) + 1, &sz);
RestoreDC(hdc, state);
ReleaseDC(hwnd, hdc);
DeleteFont(hFont);
if(GetProp(hwnd, LL_ALIGN))
SetWindowPos(hwnd, 0, rc.right - sz.cx, rc.top, sz.cx, sz.cy, SWP_SHOWWINDOW);
else
SetWindowPos(hwnd, 0, 0, 0, sz.cx, sz.cy, SWP_NOMOVE | SWP_SHOWWINDOW);
}
static void SetLLTextW(HWND hwnd, wchar_t * lpText){
wchar_t * pText;
SIZE sz;
HDC hdc;
int state;
HFONT hFont;
RECT rc;
GetWindowRect(hwnd, &rc);
MapWindowPoints(HWND_DESKTOP, GetParent(hwnd), (LPPOINT)&rc, 2);
pText = (wchar_t *)GetWindowLongPtrW(hwnd, 0);
if(pText)
free(pText);
pText = (wchar_t *)calloc(wcslen(lpText) + 2, sizeof(wchar_t));
wcscpy(pText, lpText);
SetWindowLongPtrW(hwnd, 0, (LONG_PTR)pText);
hdc = GetDC(hwnd);
state = SaveDC(hdc);
hFont = CreateFontIndirectW((LPLOGFONTW)GetWindowLongPtrW(hwnd, 4));
SelectFont(hdc, hFont);
GetTextExtentPoint32W(hdc, lpText, wcslen(lpText) + 1, &sz);
RestoreDC(hdc, state);
ReleaseDC(hwnd, hdc);
DeleteFont(hFont);
if(GetPropW(hwnd, LL_ALIGNW))
SetWindowPos(hwnd, 0, rc.right - sz.cx, rc.top, sz.cx, sz.cy, SWP_SHOWWINDOW);
else
SetWindowPos(hwnd, 0, 0, 0, sz.cx, sz.cy, SWP_NOMOVE | SWP_SHOWWINDOW);
}
LRESULT CALLBACK LLabel_WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
HANDLE_MSG (hwnd, WM_PAINT, LLabel_OnPaint);
HANDLE_MSG (hwnd, WM_DESTROY, LLabel_OnDestroy);
HANDLE_MSG (hwnd, WM_CREATE, LLabel_OnCreate);
HANDLE_MSG (hwnd, WM_LBUTTONUP, LLabel_OnLButtonUp);
HANDLE_MSG (hwnd, WM_SETTEXT, LLabel_OnSetText);
default: return DefWindowProc (hwnd, msg, wParam, lParam);
}
}
static void LLabel_OnSetText(HWND hwnd, LPCTSTR lpszText)
{
if(IsWindowUnicode(hwnd))
SetLLTextW(hwnd, (wchar_t *)lpszText);
else
SetLLText(hwnd, (char *)lpszText);
}
static BOOL LLabel_OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
{
LOGFONTW *plfW;
LOGFONT *plf;
HDC hdc;
hdc = GetDC(hwnd);
if(IsWindowUnicode(hwnd)){
plfW = calloc(1, sizeof(LOGFONTW));
plfW->lfCharSet = 1;
plfW->lfUnderline = TRUE;
plfW->lfHeight = -MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72);
wcscpy(plfW->lfFaceName, L"MS Shell Dlg");
SetWindowLongPtrW(hwnd, 4, (LONG_PTR)plfW);
}
else{
plf = calloc(1, sizeof(LOGFONT));
plf->lfCharSet = 1;
plf->lfUnderline = TRUE;
plf->lfHeight = -MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72);
strcpy(plf->lfFaceName, "MS Shell Dlg");
SetWindowLongPtr(hwnd, 4, (LONG_PTR)plf);
}
ReleaseDC(hwnd, hdc);
return TRUE;
}
static void LLabel_OnLButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
{
NMHDR nmh;
nmh.hwndFrom = hwnd;
nmh.idFrom = GetDlgCtrlID(hwnd);
nmh.code = NM_CLICK;
if(IsWindowUnicode(hwnd))
SendMessageW(GetParent(hwnd), WM_NOTIFY, (WPARAM)nmh.idFrom, (LPARAM)&nmh);
else
SendMessage(GetParent(hwnd), WM_NOTIFY, (WPARAM)nmh.idFrom, (LPARAM)&nmh);
}
static void LLabel_OnDestroy(HWND hwnd)
{
if(IsWindowUnicode(hwnd)){
free((void *)GetWindowLongPtrW(hwnd, 0));
free((void *)GetWindowLongPtrW(hwnd, 4));
EnumPropsW(hwnd, PropEnumProcW);
}
else{
free((void *)GetWindowLongPtr(hwnd, 0));
free((void *)GetWindowLongPtr(hwnd, 4));
EnumProps(hwnd, PropEnumProc);
}
}
static void LLabel_OnPaint(HWND hwnd)
{
PAINTSTRUCT ps;
RECT rc;
int state;
HFONT hFont;
int style = DT_SINGLELINE | DT_VCENTER;
BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc);
state = SaveDC(ps.hdc);
SetBkMode(ps.hdc, TRANSPARENT);
hFont = CreateFontIndirectW((LPLOGFONTW)GetWindowLongPtrW(hwnd, 4));
SelectFont(ps.hdc, hFont);
SetTextColor(ps.hdc, RGB(0, 0, 255));
if(IsWindowUnicode(hwnd)){
if(GetPropW(hwnd, LL_ALIGNW))
style |= DT_RIGHT;
else
style |= DT_LEFT;
DrawTextW(ps.hdc, (wchar_t *)GetWindowLongPtrW(hwnd, 0), -1, &rc, style);
}
else{
if(GetProp(hwnd, LL_ALIGN))
style |= DT_RIGHT;
else
style |= DT_LEFT;
DrawText(ps.hdc, (char *)GetWindowLongPtr(hwnd, 0), -1, &rc, style);
}
RestoreDC(ps.hdc, state);
DeleteFont(hFont);
EndPaint(hwnd, &ps);
}
static BOOL CALLBACK PropEnumProc(HWND hwnd, LPCSTR lpszString, HANDLE hData){
if((DWORD)(VOID *)hData != 0xffffffff){
RemoveProp(hwnd, lpszString);
}
return TRUE;
}
static BOOL CALLBACK PropEnumProcW(HWND hwnd, LPCWSTR lpszString, HANDLE hData){
if((DWORD)(VOID *)hData != 0xffffffff){
RemovePropW(hwnd, lpszString);
}
return TRUE;
}