HyperLink.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. // HyperLink.cpp : implementation file
  2. //
  3. // HyperLink static control. Will open the default browser with the given URL
  4. // when the user clicks on the link.
  5. //
  6. // Copyright (C) 1997 - 1999 Chris Maunder
  7. // All rights reserved. May not be sold for profit.
  8. //
  9. // Thanks to Pål K. Tønder for auto-size and window caption changes.
  10. //
  11. // "GotoURL" function by Stuart Patterson
  12. // As seen in the August, 1997 Windows Developer's Journal.
  13. // Copyright 1997 by Miller Freeman, Inc. All rights reserved.
  14. // Modified by Chris Maunder to use TCHARs instead of chars.
  15. //
  16. // "Default hand cursor" from Paul DiLascia's Jan 1998 MSJ article.
  17. //
  18. // 2/29/00 -- P. Shaffer standard font mod.
  19. #include "stdafx.h"
  20. #include "HyperLink.h"
  21. #include "atlconv.h" // for Unicode conversion - requires #include <afxdisp.h> // MFC OLE automation classes
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27. #define TOOLTIP_ID 1
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CHyperLink
  30. CHyperLink::CHyperLink()
  31. {
  32. m_hLinkCursor = NULL; // No cursor as yet
  33. m_crLinkColour = RGB( 0, 0, 238); // Blue
  34. m_crVisitedColour = RGB( 85, 26, 139); // Purple
  35. m_crHoverColour = RGB(255, 0, 0); // Red
  36. m_bOverControl = FALSE; // Cursor not yet over control
  37. m_bVisited = FALSE; // Hasn't been visited yet.
  38. m_nUnderline = ulHover; // Underline the link?
  39. m_bAdjustToFit = TRUE; // Resize the window to fit the text?
  40. m_strURL.Empty();
  41. m_nTimerID = 100;
  42. }
  43. CHyperLink::~CHyperLink()
  44. {
  45. m_UnderlineFont.DeleteObject();
  46. }
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CHyperLink overrides
  49. BOOL CHyperLink::DestroyWindow()
  50. {
  51. KillTimer(m_nTimerID);
  52. return CStatic::DestroyWindow();
  53. }
  54. BOOL CHyperLink::PreTranslateMessage(MSG* pMsg)
  55. {
  56. m_ToolTip.RelayEvent(pMsg);
  57. return CStatic::PreTranslateMessage(pMsg);
  58. }
  59. void CHyperLink::PreSubclassWindow()
  60. {
  61. // We want to get mouse clicks via STN_CLICKED
  62. DWORD dwStyle = GetStyle();
  63. ::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);
  64. // Set the URL as the window text
  65. if (m_strURL.IsEmpty())
  66. GetWindowText(m_strURL);
  67. // Check that the window text isn't empty. If it is, set it as the URL.
  68. CString strWndText;
  69. GetWindowText(strWndText);
  70. if (strWndText.IsEmpty())
  71. {
  72. ASSERT(!m_strURL.IsEmpty()); // Window and URL both NULL. DUH!
  73. SetWindowText(m_strURL);
  74. }
  75. CFont* pFont = GetFont();
  76. if (!pFont)
  77. {
  78. HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  79. if (hFont == NULL)
  80. hFont = (HFONT) GetStockObject(ANSI_VAR_FONT);
  81. if (hFont)
  82. pFont = CFont::FromHandle(hFont);
  83. }
  84. ASSERT(pFont->GetSafeHandle());
  85. // Create the underline font
  86. LOGFONT lf;
  87. pFont->GetLogFont(&lf);
  88. m_StdFont.CreateFontIndirect(&lf);
  89. lf.lfUnderline = (BYTE) TRUE;
  90. m_UnderlineFont.CreateFontIndirect(&lf);
  91. PositionWindow(); // Adjust size of window to fit URL if necessary
  92. SetDefaultCursor(); // Try and load up a "hand" cursor
  93. SetUnderline();
  94. // Create the tooltip
  95. CRect rect;
  96. GetClientRect(rect);
  97. m_ToolTip.Create(this);
  98. m_ToolTip.AddTool(this, m_strURL, rect, TOOLTIP_ID);
  99. CStatic::PreSubclassWindow();
  100. }
  101. BEGIN_MESSAGE_MAP(CHyperLink, CStatic)
  102. //{{AFX_MSG_MAP(CHyperLink)
  103. ON_WM_CTLCOLOR_REFLECT()
  104. ON_WM_SETCURSOR()
  105. ON_WM_MOUSEMOVE()
  106. ON_WM_TIMER()
  107. ON_CONTROL_REFLECT(STN_CLICKED, OnClicked)
  108. ON_WM_ERASEBKGND()
  109. //}}AFX_MSG_MAP
  110. END_MESSAGE_MAP()
  111. /////////////////////////////////////////////////////////////////////////////
  112. // CHyperLink message handlers
  113. void CHyperLink::OnClicked()
  114. {
  115. m_bOverControl = FALSE;
  116. int result = (int)GotoURL(m_strURL, SW_SHOW);
  117. m_bVisited = (result > HINSTANCE_ERROR);
  118. if (!m_bVisited)
  119. {
  120. MessageBeep(MB_ICONEXCLAMATION); // Unable to follow link
  121. ReportError(result);
  122. }
  123. else
  124. SetVisited(); // Repaint to show visited colour
  125. }
  126. HBRUSH CHyperLink::CtlColor(CDC* pDC, UINT nCtlColor)
  127. {
  128. ASSERT(nCtlColor == CTLCOLOR_STATIC);
  129. if (m_bOverControl)
  130. pDC->SetTextColor(m_crHoverColour);
  131. else if (m_bVisited)
  132. pDC->SetTextColor(m_crVisitedColour);
  133. else
  134. pDC->SetTextColor(m_crLinkColour);
  135. // transparent text.
  136. pDC->SetBkMode(TRANSPARENT);
  137. return (HBRUSH)GetStockObject(NULL_BRUSH);
  138. }
  139. void CHyperLink::OnMouseMove(UINT nFlags, CPoint point)
  140. {
  141. if (!m_bOverControl) // Cursor has just moved over control
  142. {
  143. m_bOverControl = TRUE;
  144. if (m_nUnderline == ulHover)
  145. SetFont(&m_UnderlineFont);
  146. Invalidate();
  147. SetTimer(m_nTimerID, 100, NULL);
  148. }
  149. CStatic::OnMouseMove(nFlags, point);
  150. }
  151. void CHyperLink::OnTimer(UINT nIDEvent)
  152. {
  153. CPoint p(GetMessagePos());
  154. ScreenToClient(&p);
  155. CRect rect;
  156. GetClientRect(rect);
  157. if (!rect.PtInRect(p))
  158. {
  159. m_bOverControl = FALSE;
  160. KillTimer(m_nTimerID);
  161. if (m_nUnderline != ulAlways)
  162. SetFont(&m_StdFont);
  163. rect.bottom+=10;
  164. InvalidateRect(rect);
  165. }
  166. CStatic::OnTimer(nIDEvent);
  167. }
  168. BOOL CHyperLink::OnSetCursor(CWnd* /*pWnd*/, UINT /*nHitTest*/, UINT /*message*/)
  169. {
  170. if (m_hLinkCursor)
  171. {
  172. ::SetCursor(m_hLinkCursor);
  173. return TRUE;
  174. }
  175. return FALSE;
  176. }
  177. BOOL CHyperLink::OnEraseBkgnd(CDC* pDC)
  178. {
  179. CRect rect;
  180. GetClientRect(rect);
  181. pDC->FillSolidRect(rect, ::GetSysColor(COLOR_3DFACE));
  182. return TRUE;
  183. }
  184. /////////////////////////////////////////////////////////////////////////////
  185. // CHyperLink operations
  186. void CHyperLink::SetURL(CString strURL)
  187. {
  188. m_strURL = strURL;
  189. if (::IsWindow(GetSafeHwnd())) {
  190. PositionWindow();
  191. m_ToolTip.UpdateTipText(strURL, this, TOOLTIP_ID);
  192. }
  193. }
  194. CString CHyperLink::GetURL() const
  195. {
  196. return m_strURL;
  197. }
  198. void CHyperLink::SetColours(COLORREF crLinkColour, COLORREF crVisitedColour,
  199. COLORREF crHoverColour /* = -1 */)
  200. {
  201. m_crLinkColour = crLinkColour;
  202. m_crVisitedColour = crVisitedColour;
  203. if (crHoverColour == -1)
  204. m_crHoverColour = ::GetSysColor(COLOR_HIGHLIGHT);
  205. else
  206. m_crHoverColour = crHoverColour;
  207. if (::IsWindow(m_hWnd))
  208. Invalidate();
  209. }
  210. COLORREF CHyperLink::GetLinkColour() const
  211. {
  212. return m_crLinkColour;
  213. }
  214. COLORREF CHyperLink::GetVisitedColour() const
  215. {
  216. return m_crVisitedColour;
  217. }
  218. COLORREF CHyperLink::GetHoverColour() const
  219. {
  220. return m_crHoverColour;
  221. }
  222. void CHyperLink::SetVisited(BOOL bVisited /* = TRUE */)
  223. {
  224. m_bVisited = bVisited;
  225. if (::IsWindow(GetSafeHwnd()))
  226. Invalidate();
  227. }
  228. BOOL CHyperLink::GetVisited() const
  229. {
  230. return m_bVisited;
  231. }
  232. void CHyperLink::SetLinkCursor(HCURSOR hCursor)
  233. {
  234. m_hLinkCursor = hCursor;
  235. if (m_hLinkCursor == NULL)
  236. SetDefaultCursor();
  237. }
  238. HCURSOR CHyperLink::GetLinkCursor() const
  239. {
  240. return m_hLinkCursor;
  241. }
  242. void CHyperLink::SetUnderline(int nUnderline /*=ulHover*/)
  243. {
  244. if (m_nUnderline == nUnderline)
  245. return;
  246. if (::IsWindow(GetSafeHwnd()))
  247. {
  248. if (nUnderline == ulAlways)
  249. SetFont(&m_UnderlineFont);
  250. else
  251. SetFont(&m_StdFont);
  252. Invalidate();
  253. }
  254. m_nUnderline = nUnderline;
  255. }
  256. int CHyperLink::GetUnderline() const
  257. {
  258. return m_nUnderline;
  259. }
  260. void CHyperLink::SetAutoSize(BOOL bAutoSize /* = TRUE */)
  261. {
  262. m_bAdjustToFit = bAutoSize;
  263. if (::IsWindow(GetSafeHwnd()))
  264. PositionWindow();
  265. }
  266. BOOL CHyperLink::GetAutoSize() const
  267. {
  268. return m_bAdjustToFit;
  269. }
  270. // Move and resize the window so that the window is the same size
  271. // as the hyperlink text. This stops the hyperlink cursor being active
  272. // when it is not directly over the text. If the text is left justified
  273. // then the window is merely shrunk, but if it is centred or right
  274. // justified then the window will have to be moved as well.
  275. //
  276. // Suggested by Pål K. Tønder
  277. void CHyperLink::PositionWindow()
  278. {
  279. if (!::IsWindow(GetSafeHwnd()) || !m_bAdjustToFit)
  280. return;
  281. // Get the current window position
  282. CRect WndRect, ClientRect;
  283. GetWindowRect(WndRect);
  284. GetClientRect(ClientRect);
  285. ClientToScreen(ClientRect);
  286. CWnd* pParent = GetParent();
  287. if (pParent)
  288. {
  289. pParent->ScreenToClient(WndRect);
  290. pParent->ScreenToClient(ClientRect);
  291. }
  292. // Get the size of the window text
  293. CString strWndText;
  294. GetWindowText(strWndText);
  295. CDC* pDC = GetDC();
  296. CFont* pOldFont = pDC->SelectObject(&m_UnderlineFont);
  297. CSize Extent = pDC->GetTextExtent(strWndText);
  298. pDC->SelectObject(pOldFont);
  299. ReleaseDC(pDC);
  300. // Adjust for window borders
  301. Extent.cx += WndRect.Width() - ClientRect.Width();
  302. Extent.cy += WndRect.Height() - ClientRect.Height();
  303. // Get the text justification via the window style
  304. DWORD dwStyle = GetStyle();
  305. // Recalc the window size and position based on the text justification
  306. if (dwStyle & SS_CENTERIMAGE)
  307. WndRect.DeflateRect(0, (WndRect.Height() - Extent.cy)/2);
  308. else
  309. WndRect.bottom = WndRect.top + Extent.cy;
  310. if (dwStyle & SS_CENTER)
  311. WndRect.DeflateRect((WndRect.Width() - Extent.cx)/2, 0);
  312. else if (dwStyle & SS_RIGHT)
  313. WndRect.left = WndRect.right - Extent.cx;
  314. else // SS_LEFT = 0, so we can't test for it explicitly
  315. WndRect.right = WndRect.left + Extent.cx;
  316. // Move the window
  317. SetWindowPos(NULL, WndRect.left, WndRect.top, WndRect.Width(), WndRect.Height(), SWP_NOZORDER);
  318. }
  319. /////////////////////////////////////////////////////////////////////////////
  320. // CHyperLink implementation
  321. // The following appeared in Paul DiLascia's Jan 1998 MSJ articles.
  322. // It loads a "hand" cursor from the winhlp32.exe module
  323. void CHyperLink::SetDefaultCursor()
  324. {
  325. if (m_hLinkCursor == NULL) // No cursor handle - load our own
  326. {
  327. // Get the windows directory
  328. CString strWndDir;
  329. GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH), MAX_PATH);
  330. strWndDir.ReleaseBuffer();
  331. strWndDir += _T("\\winhlp32.exe");
  332. // This retrieves cursor #106 from winhlp32.exe, which is a hand pointer
  333. HMODULE hModule = LoadLibrary(strWndDir);
  334. if (hModule) {
  335. HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
  336. if (hHandCursor)
  337. m_hLinkCursor = CopyCursor(hHandCursor);
  338. }
  339. FreeLibrary(hModule);
  340. }
  341. }
  342. LONG CHyperLink::GetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata)
  343. {
  344. HKEY hkey;
  345. LONG retval = RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkey);
  346. if (retval == ERROR_SUCCESS) {
  347. long datasize = MAX_PATH;
  348. TCHAR data[MAX_PATH];
  349. RegQueryValue(hkey, NULL, data, &datasize);
  350. lstrcpy(retdata,data);
  351. RegCloseKey(hkey);
  352. }
  353. return retval;
  354. }
  355. void CHyperLink::ReportError(int nError)
  356. {
  357. CString str;
  358. switch (nError) {
  359. case 0: str = "The operating system is out\nof memory or resources."; break;
  360. case SE_ERR_PNF: str = "The specified path was not found."; break;
  361. case SE_ERR_FNF: str = "The specified file was not found."; break;
  362. case ERROR_BAD_FORMAT: str = "The .EXE file is invalid\n(non-Win32 .EXE or error in .EXE image)."; break;
  363. case SE_ERR_ACCESSDENIED: str = "The operating system denied\naccess to the specified file."; break;
  364. case SE_ERR_ASSOCINCOMPLETE: str = "The filename association is\nincomplete or invalid."; break;
  365. case SE_ERR_DDEBUSY: str = "The DDE transaction could not\nbe completed because other DDE transactions\nwere being processed."; break;
  366. case SE_ERR_DDEFAIL: str = "The DDE transaction failed."; break;
  367. case SE_ERR_DDETIMEOUT: str = "The DDE transaction could not\nbe completed because the request timed out."; break;
  368. case SE_ERR_DLLNOTFOUND: str = "The specified dynamic-link library was not found."; break;
  369. case SE_ERR_NOASSOC: str = "There is no application associated\nwith the given filename extension."; break;
  370. case SE_ERR_OOM: str = "There was not enough memory to complete the operation."; break;
  371. case SE_ERR_SHARE: str = "A sharing violation occurred. ";
  372. default: str.Format(_T("Unknown Error (%d) occurred."), nError); break;
  373. }
  374. str = "Unable to open hyperlink:\n\n" + str;
  375. AfxMessageBox(str, MB_ICONEXCLAMATION | MB_OK);
  376. }
  377. HINSTANCE CHyperLink::GotoURL(LPCTSTR url, int showcmd)
  378. {
  379. TCHAR key[MAX_PATH + MAX_PATH];
  380. // First try ShellExecute()
  381. HINSTANCE result = ShellExecute(NULL, _T("open"), url, NULL,NULL, showcmd);
  382. // If it failed, get the .htm regkey and lookup the program
  383. if ((UINT)result <= HINSTANCE_ERROR) {
  384. if (GetRegKey(HKEY_CLASSES_ROOT, _T(".htm"), key) == ERROR_SUCCESS) {
  385. lstrcat(key, _T("\\shell\\open\\command"));
  386. if (GetRegKey(HKEY_CLASSES_ROOT,key,key) == ERROR_SUCCESS) {
  387. TCHAR *pos;
  388. pos = _tcsstr(key, _T("\"%1\""));
  389. if (pos == NULL) { // No quotes found
  390. pos = _tcsstr(key, _T("%1")); // Check for %1, without quotes
  391. if (pos == NULL) // No parameter at all...
  392. pos = key+lstrlen(key)-1;
  393. else
  394. *pos = '\0'; // Remove the parameter
  395. }
  396. else
  397. *pos = '\0'; // Remove the parameter
  398. lstrcat(pos, _T(" "));
  399. lstrcat(pos, url);
  400. USES_CONVERSION;
  401. result = (HINSTANCE) WinExec(T2A(key),showcmd);
  402. }
  403. }
  404. }
  405. return result;
  406. }