Menu

[r193]: / trunk / src / hyperlink.cpp  Maximize  Restore  History

Download this file

398 lines (350 with data), 9.8 kB

  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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Module ID: hyperlink.cpp
* Title : CHyperLink definition.
*
* Author : Olivier Langlois <olanglois@sympatico.ca>
* Date : November 15, 2005
*
* To read the article describing this class, visit
* http://www3.sympatico.ca/olanglois/hyperlinkdemo.htm
*
* Note: Strongly inspired by Neal Stublen code
* Minor ideas come from Chris Maunder and Paul DiLascia code
*
* Revision :
*
* 001 26-Nov-2005 - Olivier Langlois
* - Added changes to make CHyperLink compatible with UNICODE
* - Use dynamic memory allocation for the URL string
* - Use of the MAKEINTATOM macro
*/
#include "stdafx.h"
#include "hyperlink.h"
#include "shellapi.h"
/*
* If you do not wish to link with the windebug module,
* comment out the next include directive and uncomment the
* define directive.
*/
//#include "win/windebug.h"
#define LASTERRORDISPLAYR(a) (a)
/*
* Defines
*/
#ifndef IDC_HAND
#define IDC_HAND MAKEINTRESOURCE(32649)
#endif
#define PROP_OBJECT_PTR MAKEINTATOM(ga.atom)
#define PROP_ORIGINAL_PROC MAKEINTATOM(ga.atom)
/*
* typedefs
*/
class CGlobalAtom
{
public:
CGlobalAtom(void)
{ atom = GlobalAddAtom(TEXT("_Hyperlink_Object_Pointer_")
TEXT("\\{AFEED740-CC6D-47c5-831D-9848FD916EEF}")); }
~CGlobalAtom(void)
{ DeleteAtom(atom); }
ATOM atom;
};
/*
* Local variables
*/
static CGlobalAtom ga;
/*
* Static members initilisation
*/
COLORREF CHyperLink::g_crLinkColor = RGB( 0, 0, 255); // Blue;
COLORREF CHyperLink::g_crVisitedColor = RGB( 128, 0, 128); // Purple;
HCURSOR CHyperLink::g_hLinkCursor = NULL;
HFONT CHyperLink::g_UnderlineFont = NULL;
int CHyperLink::g_counter = 0;
/*
* Macros and inline functions
*/
inline bool PTINRECT( LPCRECT r, const POINT &pt )
{ return ( pt.x >= r->left && pt.x < r->right && pt.y >= r->top && pt.y < r->bottom ); }
inline void INFLATERECT( PRECT r, int dx, int dy )
{ r->left -= dx; r->right += dx; r->top -= dy; r->bottom += dy; }
/////////////////////////////////////////////////////////////////////////////
// CHyperLink
CHyperLink::CHyperLink(void)
{
m_bOverControl = FALSE; // Cursor not yet over control
m_bVisited = FALSE; // Hasn't been visited yet.
m_StdFont = NULL;
m_pfnOrigCtlProc = NULL;
m_strURL = NULL;
}
CHyperLink::~CHyperLink(void)
{
delete [] m_strURL;
}
/*-----------------------------------------------------------------------------
* Public functions
*/
/*
* Function CHyperLink::ConvertStaticToHyperlink
*/
BOOL CHyperLink::ConvertStaticToHyperlink(HWND hwndCtl, LPCTSTR strURL)
{
if( !(setURL(strURL)) )
return FALSE;
// Subclass the parent so we can color the controls as we desire.
HWND hwndParent = GetParent(hwndCtl);
if (NULL != hwndParent)
{
WNDPROC pfnOrigProc = (WNDPROC) GetWindowLongPtr(hwndParent, GWLP_WNDPROC);
if (pfnOrigProc != _HyperlinkParentProc)
{
SetProp( hwndParent, PROP_ORIGINAL_PROC, (HANDLE)pfnOrigProc );
SetWindowLongPtr( hwndParent, GWLP_WNDPROC,
(LONG) (WNDPROC) _HyperlinkParentProc );
}
}
// Make sure the control will send notifications.
LONG_PTR Style = GetWindowLongPtr(hwndCtl, GWL_STYLE);
SetWindowLongPtr(hwndCtl, GWL_STYLE, Style | SS_NOTIFY);
// Create an updated font by adding an underline.
m_StdFont = (HFONT) SendMessage(hwndCtl, WM_GETFONT, 0, 0);
if( g_counter++ == 0 )
{
createGlobalResources();
}
// Subclass the existing control.
m_pfnOrigCtlProc = (WNDPROC) GetWindowLongPtr(hwndCtl, GWLP_WNDPROC);
SetProp(hwndCtl, PROP_OBJECT_PTR, (HANDLE) this);
SetWindowLongPtr(hwndCtl, GWLP_WNDPROC, (LONG) (WNDPROC) _HyperlinkProc);
return TRUE;
}
/*
* Function CHyperLink::ConvertStaticToHyperlink
*/
BOOL CHyperLink::ConvertStaticToHyperlink(HWND hwndParent, UINT uiCtlId,
LPCTSTR strURL)
{
return ConvertStaticToHyperlink(GetDlgItem(hwndParent, uiCtlId), strURL);
}
/*
* Function CHyperLink::setURL
*/
BOOL CHyperLink::setURL(LPCTSTR strURL)
{
if( m_strURL )
{
delete [] m_strURL;
}
if( (m_strURL = new TCHAR[lstrlen(strURL)+1])==0 )
{
return FALSE;
}
lstrcpy(m_strURL, strURL);
return TRUE;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* Private functions
*/
/*
* Function CHyperLink::_HyperlinkParentProc
*/
LRESULT CALLBACK CHyperLink::_HyperlinkParentProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
WNDPROC pfnOrigProc = (WNDPROC) GetProp(hwnd, PROP_ORIGINAL_PROC);
switch (message)
{
case WM_CTLCOLORSTATIC:
{
HDC hdc = (HDC) wParam;
HWND hwndCtl = (HWND) lParam;
CHyperLink *pHyperLink = (CHyperLink *)GetProp(hwndCtl,
PROP_OBJECT_PTR);
if(pHyperLink)
{
LRESULT lr = CallWindowProc(pfnOrigProc, hwnd, message,
wParam, lParam);
if (!pHyperLink->m_bVisited)
{
// This is the most common case for static branch prediction
// optimization
SetTextColor(hdc, CHyperLink::g_crLinkColor);
}
else
{
SetTextColor(hdc, CHyperLink::g_crVisitedColor);
}
return lr;
}
break;
}
case WM_DESTROY:
{
SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG) pfnOrigProc);
RemoveProp(hwnd, PROP_ORIGINAL_PROC);
break;
}
}
return CallWindowProc(pfnOrigProc, hwnd, message, wParam, lParam);
}
/*
* Function CHyperLink::Navigate
*/
inline void CHyperLink::Navigate(void)
{
SHELLEXECUTEINFO sei;
::ZeroMemory(&sei,sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof( SHELLEXECUTEINFO ); // Set Size
sei.lpVerb = TEXT( "open" ); // Set Verb
sei.lpFile = m_strURL; // Set Target To Open
sei.nShow = SW_SHOWNORMAL; // Show Normal
LASTERRORDISPLAYR(ShellExecuteEx(&sei));
m_bVisited = TRUE;
}
/*
* Function CHyperLink::DrawFocusRect
*/
inline void CHyperLink::DrawFocusRect(HWND hwnd)
{
HWND hwndParent = ::GetParent(hwnd);
if( hwndParent )
{
// calculate where to draw focus rectangle, in screen coords
RECT rc;
GetWindowRect(hwnd, &rc);
INFLATERECT(&rc,1,1); // add one pixel all around
// convert to parent window client coords
::ScreenToClient(hwndParent, (LPPOINT)&rc);
::ScreenToClient(hwndParent, ((LPPOINT)&rc)+1);
HDC dcParent = GetDC(hwndParent); // parent window's DC
::DrawFocusRect(dcParent, &rc); // draw it!
ReleaseDC(hwndParent,dcParent);
}
}
/*
* Function CHyperLink::_HyperlinkProc
*
* Note: Processed messages are not passed back to the static control
* procedure. It does work fine but be aware that it could cause
* some problems if the static control is already subclassed.
* Consider the example where the static control would be already
* subclassed with the ToolTip control that needs to process mouse
* messages. In that situation, the ToolTip control would not work
* as expected.
*/
LRESULT CALLBACK CHyperLink::_HyperlinkProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
CHyperLink *pHyperLink = (CHyperLink *)GetProp(hwnd, PROP_OBJECT_PTR);
switch (message)
{
case WM_MOUSEMOVE:
{
if ( pHyperLink->m_bOverControl )
{
// This is the most common case for static branch prediction
// optimization
RECT rect;
GetClientRect(hwnd,&rect);
POINT pt = { LOWORD(lParam), HIWORD(lParam) };
if (!PTINRECT(&rect,pt))
{
ReleaseCapture();
}
}
else
{
pHyperLink->m_bOverControl = TRUE;
SendMessage(hwnd, WM_SETFONT,
(WPARAM)CHyperLink::g_UnderlineFont, FALSE);
InvalidateRect(hwnd, NULL, FALSE);
pHyperLink->OnSelect();
SetCapture(hwnd);
}
return 0;
}
case WM_SETCURSOR:
{
SetCursor(CHyperLink::g_hLinkCursor);
return TRUE;
}
case WM_CAPTURECHANGED:
{
pHyperLink->m_bOverControl = FALSE;
pHyperLink->OnDeselect();
SendMessage(hwnd, WM_SETFONT,
(WPARAM)pHyperLink->m_StdFont, FALSE);
InvalidateRect(hwnd, NULL, FALSE);
return 0;
}
case WM_KEYUP:
{
if( wParam != VK_SPACE )
{
break;
}
}
// Fall through
case WM_LBUTTONUP:
{
pHyperLink->Navigate();
return 0;
}
case WM_SETFOCUS: // Fall through
case WM_KILLFOCUS:
{
if( message == WM_SETFOCUS )
{
pHyperLink->OnSelect();
}
else // WM_KILLFOCUS
{
pHyperLink->OnDeselect();
}
CHyperLink::DrawFocusRect(hwnd);
return 0;
}
case WM_DESTROY:
{
SetWindowLongPtr(hwnd, GWLP_WNDPROC,
(LONG) pHyperLink->m_pfnOrigCtlProc);
SendMessage(hwnd, WM_SETFONT, (WPARAM) pHyperLink->m_StdFont, 0);
if( --CHyperLink::g_counter <= 0 )
{
destroyGlobalResources();
}
RemoveProp(hwnd, PROP_OBJECT_PTR);
break;
}
}
return CallWindowProc(pHyperLink->m_pfnOrigCtlProc, hwnd, message,
wParam, lParam);
}
/*
* Function CHyperLink::createUnderlineFont
*/
void CHyperLink::createUnderlineFont(void)
{
LOGFONT lf;
GetObject(m_StdFont, sizeof(lf), &lf);
lf.lfUnderline = TRUE;
g_UnderlineFont = CreateFontIndirect(&lf);
}
/*
* Function CHyperLink::createLinkCursor
*/
void CHyperLink::createLinkCursor(void)
{
g_hLinkCursor = ::LoadCursor(NULL, IDC_HAND); // Load Windows' hand cursor
if( !g_hLinkCursor ) // if not available, use the standard Arrow cursor
{
/*
* There exist an alternative way to get the IDC_HAND by loading winhlp32.exe but I
* estimated that it didn't worth the trouble as IDC_HAND is supported since Win98.
* I consider that if a user is happy with 10 years old OS, he won't bother to have
* an arrow cursor.
*/
g_hLinkCursor = ::LoadCursor(NULL, IDC_ARROW);
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.