ToolTipEx.cpp 12 KB

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