ToolTipEx.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // ToolTipEx.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "cp_main.h"
  5. #include "ToolTipEx.h"
  6. #include "BitmapHelper.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. #define DELETE_BITMAP if(m_pBitmap) \
  13. { \
  14. m_pBitmap->DeleteObject(); \
  15. DELETE_PTR(m_pBitmap); \
  16. }
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CToolTipEx
  19. CToolTipEx::CToolTipEx() :
  20. m_dwTextStyle(DT_EXPANDTABS|DT_EXTERNALLEADING|DT_NOPREFIX|DT_WORDBREAK),
  21. m_rectMargin(2, 2, 3, 3),
  22. m_pBitmap(NULL),
  23. m_pNotifyWnd(NULL)
  24. {
  25. }
  26. CToolTipEx::~CToolTipEx()
  27. {
  28. DELETE_BITMAP
  29. m_Font.DeleteObject();
  30. }
  31. BEGIN_MESSAGE_MAP(CToolTipEx, CWnd)
  32. //{{AFX_MSG_MAP(CToolTipEx)
  33. ON_WM_PAINT()
  34. ON_WM_SIZE()
  35. ON_WM_NCHITTEST()
  36. ON_WM_ACTIVATE()
  37. //}}AFX_MSG_MAP
  38. ON_WM_TIMER()
  39. END_MESSAGE_MAP()
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CToolTipEx message handlers
  42. BOOL CToolTipEx::Create(CWnd* pParentWnd)
  43. {
  44. // Get the class name and create the window
  45. CString szClassName = AfxRegisterWndClass(CS_CLASSDC|CS_SAVEBITS,
  46. LoadCursor(NULL, IDC_ARROW));
  47. // Create the window - just don't show it yet.
  48. if (!CWnd::CreateEx(WS_EX_TOPMOST, szClassName, _T(""),
  49. WS_POPUP|WS_BORDER,
  50. 0, 0, 10, 10, // size & position updated when needed
  51. pParentWnd->GetSafeHwnd(), 0, NULL))
  52. {
  53. return FALSE;
  54. }
  55. m_RichEdit.Create(_T(""), _T(""), WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL, CRect(10,10,100,200), this, 1);
  56. m_RichEdit.SetReadOnly();
  57. m_RichEdit.SetBackgroundColor(FALSE, GetSysColor(COLOR_INFOBK));
  58. SetLogFont(GetSystemToolTipFont(), FALSE);
  59. return TRUE;
  60. }
  61. BOOL CToolTipEx::Show(CPoint point)
  62. {
  63. if(m_pBitmap)
  64. {
  65. m_RichEdit.ShowWindow(SW_HIDE);
  66. }
  67. else
  68. {
  69. m_RichEdit.ShowWindow(SW_SHOW);
  70. }
  71. CRect rect = GetBoundsRect();
  72. //account for the scroll bars
  73. rect.right += 20;
  74. rect.bottom += 20;
  75. //if showing rtf then increase the size because
  76. //rtf will probably draw bigger
  77. if(m_csRTF != "")
  78. {
  79. long lNewWidth = (long)rect.Width() + (long)(rect.Width() * .3);
  80. rect.right = rect.left + lNewWidth;
  81. long lNewHeight = rect.Height() + (rect.Height() * 1);
  82. rect.bottom = rect.top + lNewHeight;
  83. }
  84. CRect rcScreen;
  85. ClientToScreen(rect);
  86. CRect cr(point, point);
  87. int nMonitor = GetMonitorFromRect(&cr);
  88. GetMonitorRect(nMonitor, &rcScreen);
  89. //ensure that we don't go outside the screen
  90. if(point.x < 0)
  91. point.x = 5;
  92. if(point.y < 0)
  93. point.y = 5;
  94. rcScreen.DeflateRect(0, 0, 5, 5);
  95. long lWidth = rect.Width();
  96. long lHeight = rect.Height();
  97. rect.left = point.x;
  98. rect.top = point.y;
  99. rect.right = rect.left + lWidth;
  100. rect.bottom = rect.top + lHeight;
  101. if(rect.right > rcScreen.right)
  102. rect.right = rcScreen.right;
  103. if(rect.bottom > rcScreen.bottom)
  104. rect.bottom = rcScreen.bottom;
  105. SetWindowPos(&CWnd::wndTopMost,
  106. point.x, point.y,
  107. rect.Width(), rect.Height(),
  108. SWP_SHOWWINDOW|SWP_NOCOPYBITS|SWP_NOACTIVATE|SWP_NOZORDER);
  109. return TRUE;
  110. }
  111. BOOL CToolTipEx::Hide()
  112. {
  113. DELETE_BITMAP
  114. ShowWindow(SW_HIDE);
  115. m_csRTF = "";
  116. m_csText = "";
  117. return TRUE;
  118. }
  119. void CToolTipEx::OnPaint()
  120. {
  121. CPaintDC dc(this); // device context for painting
  122. CRect rect;
  123. GetClientRect(rect);
  124. // CBrush Brush, *pOldBrush;
  125. // Brush.CreateSolidBrush(GetSysColor(COLOR_INFOBK));
  126. // pOldBrush = dc.SelectObject(&Brush);
  127. // CFont *pOldFont = dc.SelectObject(&m_Font);
  128. // dc.FillRect(&rect, &Brush);
  129. // Draw Text
  130. // dc.SetBkMode(TRANSPARENT);
  131. // rect.DeflateRect(m_rectMargin);
  132. if(m_pBitmap)
  133. {
  134. CDC MemDc;
  135. MemDc.CreateCompatibleDC(&dc);
  136. CBitmap* oldBitmap = MemDc.SelectObject(m_pBitmap);
  137. int nWidth = CBitmapHelper::GetCBitmapWidth(*m_pBitmap);
  138. int nHeight = CBitmapHelper::GetCBitmapHeight(*m_pBitmap);
  139. dc.BitBlt(rect.left, rect.top, nWidth, nHeight, &MemDc, 0, 0, SRCCOPY);
  140. MemDc.SelectObject(oldBitmap);
  141. rect.top += nHeight;
  142. }
  143. //dc.DrawText(m_csText, rect, m_dwTextStyle);
  144. // Cleanup
  145. // dc.SelectObject(pOldBrush);
  146. // dc.SelectObject(pOldFont);
  147. }
  148. void CToolTipEx::PostNcDestroy()
  149. {
  150. CWnd::PostNcDestroy();
  151. delete this;
  152. }
  153. BOOL CToolTipEx::PreTranslateMessage(MSG* pMsg)
  154. {
  155. switch(pMsg->message)
  156. {
  157. case WM_KEYDOWN:
  158. switch( pMsg->wParam )
  159. {
  160. case VK_ESCAPE:
  161. Hide();
  162. return TRUE;
  163. case 'C':
  164. if(GetKeyState(VK_CONTROL) & 0x8000)
  165. {
  166. m_RichEdit.Copy();
  167. }
  168. break;
  169. }
  170. }
  171. return CWnd::PreTranslateMessage(pMsg);
  172. }
  173. BOOL CToolTipEx::OnMsg(MSG* pMsg)
  174. {
  175. if(FALSE == IsWindowVisible())
  176. {
  177. return FALSE;
  178. }
  179. switch(pMsg->message)
  180. {
  181. case WM_WINDOWPOSCHANGING:
  182. case WM_LBUTTONDOWN:
  183. {
  184. if (!IsCursorInToolTip())
  185. Hide();
  186. break;
  187. }
  188. case WM_KEYDOWN:
  189. {
  190. WPARAM vk = pMsg->wParam;
  191. if(vk == VK_ESCAPE)
  192. {
  193. Hide();
  194. return TRUE;
  195. }
  196. else if(vk == VK_TAB)
  197. {
  198. m_RichEdit.SetFocus();
  199. return TRUE;
  200. }
  201. Hide();
  202. break;
  203. }
  204. case WM_LBUTTONDBLCLK:
  205. case WM_RBUTTONDOWN :
  206. case WM_RBUTTONDBLCLK:
  207. case WM_MBUTTONDOWN:
  208. case WM_MBUTTONDBLCLK:
  209. case WM_NCLBUTTONDOWN:
  210. case WM_NCLBUTTONDBLCLK:
  211. case WM_NCRBUTTONDOWN:
  212. case WM_NCRBUTTONDBLCLK:
  213. case WM_NCMBUTTONDOWN:
  214. case WM_NCMBUTTONDBLCLK:
  215. {
  216. Hide();
  217. break;
  218. }
  219. }
  220. return FALSE;
  221. }
  222. CRect CToolTipEx::GetBoundsRect()
  223. {
  224. CWindowDC dc(NULL);
  225. CFont *pOldFont = (CFont*) dc.SelectObject((CFont*)&m_Font);
  226. int nLineWidth = 0;
  227. if (nLineWidth == 0)
  228. {
  229. // Count the number of lines of text
  230. int nStart = 0, nNumLines = 0;
  231. CString strTextCopy = m_csText;
  232. do
  233. {
  234. nStart = strTextCopy.Find(_T("\n"));
  235. // skip found character
  236. if (nStart >= 0)
  237. strTextCopy = strTextCopy.Mid(nStart+1);
  238. nNumLines++;
  239. } while (nStart >= 0);
  240. // Find the widest line
  241. for (int i = 0; i < nNumLines; i++)
  242. {
  243. CString strLine = GetFieldFromString(m_csText, i, _T('\n')) + _T(" ");
  244. nLineWidth = max(nLineWidth, dc.GetTextExtent(strLine).cx);
  245. }
  246. }
  247. CRect rect(0, 0, max(0,nLineWidth), 0);
  248. dc.DrawText(m_csText, rect, DT_CALCRECT | m_dwTextStyle);
  249. dc.SelectObject(pOldFont);
  250. rect.bottom += m_rectMargin.top + m_rectMargin.bottom;
  251. rect.right += m_rectMargin.left + m_rectMargin.right + 2;
  252. if(m_pBitmap)
  253. {
  254. int nWidth = CBitmapHelper::GetCBitmapWidth(*m_pBitmap);
  255. int nHeight = CBitmapHelper::GetCBitmapHeight(*m_pBitmap);
  256. rect.bottom += nHeight;
  257. if((rect.left + nWidth) > rect.right)
  258. rect.right = rect.left + nWidth;
  259. }
  260. return rect;
  261. }
  262. CString CToolTipEx::GetFieldFromString(CString ref, int nIndex, TCHAR ch)
  263. {
  264. CString strReturn;
  265. LPCTSTR pstrStart = ref.LockBuffer();
  266. LPCTSTR pstrBuffer = pstrStart;
  267. int nCurrent = 0;
  268. int nStart = 0;
  269. int nEnd = 0;
  270. int nOldStart = 0;
  271. while (nCurrent <= nIndex && *pstrBuffer != _T('\0'))
  272. {
  273. if (*pstrBuffer == ch)
  274. {
  275. nOldStart = nStart;
  276. nStart = nEnd+1;
  277. nCurrent++;
  278. }
  279. nEnd++;
  280. pstrBuffer++;
  281. }
  282. // May have reached the end of the string
  283. if (*pstrBuffer == _T('\0'))
  284. {
  285. nOldStart = nStart;
  286. nEnd++;
  287. }
  288. ref.UnlockBuffer();
  289. if (nCurrent < nIndex)
  290. {
  291. //TRACE1("Warning: GetStringField - Couldn't find field %d.\n", nIndex);
  292. return strReturn;
  293. }
  294. return ref.Mid(nOldStart, nEnd-nOldStart-1);
  295. }
  296. LPLOGFONT CToolTipEx::GetSystemToolTipFont()
  297. {
  298. static LOGFONT LogFont;
  299. NONCLIENTMETRICS ncm;
  300. ncm.cbSize = sizeof(NONCLIENTMETRICS);
  301. if(!SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0))
  302. return FALSE;
  303. memcpy(&LogFont, &(ncm.lfStatusFont), sizeof(LOGFONT));
  304. return &LogFont;
  305. }
  306. BOOL CToolTipEx::SetLogFont(LPLOGFONT lpLogFont, BOOL bRedraw /*=TRUE*/)
  307. {
  308. ASSERT(lpLogFont);
  309. if (!lpLogFont)
  310. return FALSE;
  311. LOGFONT LogFont;
  312. // Store font as the global default
  313. memcpy(&LogFont, lpLogFont, sizeof(LOGFONT));
  314. // Create the actual font object
  315. m_Font.DeleteObject();
  316. m_Font.CreateFontIndirect(&LogFont);
  317. if (bRedraw && ::IsWindow(GetSafeHwnd()))
  318. Invalidate();
  319. return TRUE;
  320. }
  321. void CToolTipEx::SetBitmap(CBitmap *pBitmap)
  322. {
  323. DELETE_BITMAP
  324. m_pBitmap = pBitmap;
  325. }
  326. void CToolTipEx::OnSize(UINT nType, int cx, int cy)
  327. {
  328. CWnd::OnSize(nType, cx, cy);
  329. if(::IsWindow(m_RichEdit.GetSafeHwnd()) == FALSE)
  330. return;
  331. CRect cr;
  332. GetClientRect(cr);
  333. // cr.DeflateRect(0, 0, 15, 0);
  334. m_RichEdit.MoveWindow(cr);
  335. }
  336. BOOL CToolTipEx::IsCursorInToolTip()
  337. {
  338. CRect cr;
  339. GetWindowRect(cr);
  340. CPoint cursorPos;
  341. GetCursorPos(&cursorPos);
  342. return cr.PtInRect(cursorPos);
  343. }
  344. void CToolTipEx::SetRTFText(const char *pRTF)
  345. {
  346. m_RichEdit.SetRTF(pRTF);
  347. m_csRTF = pRTF;
  348. }
  349. //void CToolTipEx::SetRTFText(const CString &csRTF)
  350. //{
  351. // m_RichEdit.SetRTF(csRTF);
  352. // m_csRTF = csRTF;
  353. //}
  354. void CToolTipEx::SetToolTipText(const CString &csText)
  355. {
  356. m_csText = csText;
  357. m_RichEdit.SetFont(&m_Font);
  358. m_RichEdit.SetText(csText);
  359. }
  360. UINT CToolTipEx::OnNcHitTest(CPoint point)
  361. {
  362. CRect crWindow;
  363. GetWindowRect(crWindow);
  364. const static int nBorder = 10;
  365. if((point.y < crWindow.top + nBorder) &&
  366. (point.x < crWindow.left + nBorder))
  367. return HTTOPLEFT;
  368. else if((point.y < crWindow.top + nBorder) &&
  369. (point.x > crWindow.right - nBorder))
  370. return HTTOPRIGHT;
  371. else if((point.y > crWindow.bottom - nBorder) &&
  372. (point.x > crWindow.right - nBorder))
  373. return HTBOTTOMRIGHT;
  374. else if((point.y > crWindow.bottom - nBorder) &&
  375. (point.x < crWindow.left + nBorder))
  376. return HTBOTTOMLEFT;
  377. if(point.y < crWindow.top + nBorder)
  378. return HTTOP;
  379. else if(point.y > crWindow.bottom - nBorder)
  380. return HTBOTTOM;
  381. else if(point.x > crWindow.right - nBorder)
  382. return HTRIGHT;
  383. else if(point.x < crWindow.left + nBorder)
  384. return HTLEFT;
  385. // if(point.x > crWindow.right - 15)
  386. // return HTCAPTION;
  387. return CWnd::OnNcHitTest(point);
  388. }
  389. void CToolTipEx::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
  390. {
  391. CWnd::OnActivate(nState, pWndOther, bMinimized);
  392. if (nState == WA_INACTIVE)
  393. {
  394. Hide();
  395. if(m_pNotifyWnd)
  396. {
  397. m_pNotifyWnd->PostMessage(NM_INACTIVE_TOOLTIPWND, 0, 0);
  398. }
  399. }
  400. }
  401. void CToolTipEx::OnTimer(UINT_PTR nIDEvent)
  402. {
  403. switch(nIDEvent)
  404. {
  405. case HIDE_WINDOW_TIMER:
  406. Hide();
  407. PostMessage(WM_DESTROY, 0, 0);
  408. break;
  409. }
  410. CWnd::OnTimer(nIDEvent);
  411. }