ToolTipEx.cpp 12 KB

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