HyperLink.cpp 13 KB

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