ToolTipEx.cpp 12 KB

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