HyperLink.cpp 12 KB

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