ToolTipEx.cpp 13 KB

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