ToolTipEx.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. END_MESSAGE_MAP()
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CToolTipEx message handlers
  41. BOOL CToolTipEx::Create(CWnd* pParentWnd)
  42. {
  43. // Get the class name and create the window
  44. CString szClassName = AfxRegisterWndClass(CS_CLASSDC|CS_SAVEBITS,
  45. LoadCursor(NULL, IDC_ARROW));
  46. // Create the window - just don't show it yet.
  47. if (!CWnd::CreateEx(WS_EX_TOPMOST, szClassName, _T(""),
  48. 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_RichEdit.Create(WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL, CRect(10,10,100,200), this, 1);
  55. m_RichEdit.SetReadOnly();
  56. m_RichEdit.SetBackgroundColor(FALSE, GetSysColor(COLOR_INFOBK));
  57. SetLogFont(GetSystemToolTipFont(), FALSE);
  58. return TRUE;
  59. }
  60. BOOL CToolTipEx::Show(CPoint point)
  61. {
  62. if(m_pBitmap)
  63. {
  64. m_RichEdit.ShowWindow(SW_HIDE);
  65. }
  66. else
  67. {
  68. m_RichEdit.ShowWindow(SW_SHOW);
  69. }
  70. CRect rect = GetBoundsRect();
  71. //account for the scroll bars
  72. rect.right += 20;
  73. rect.bottom += 20;
  74. //if showing rtf then increase the size because
  75. //rtf will probably draw bigger
  76. if(m_csRTF != "")
  77. {
  78. long lNewWidth = rect.Width() + (rect.Width() * .3);
  79. rect.right = rect.left + lNewWidth;
  80. long lNewHeight = rect.Height() + (rect.Height() * 1);
  81. rect.bottom = rect.top + lNewHeight;
  82. }
  83. CRect rcScreen;
  84. ClientToScreen(rect);
  85. int nMonitor = GetMonitorFromRect(&rect);
  86. GetMonitorRect(nMonitor, &rcScreen);
  87. //ensure that we don't go outside the screen
  88. if(point.x < 0)
  89. point.x = 5;
  90. if(point.y < 0)
  91. point.y = 5;
  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. rect.right = rcScreen.right;
  101. if(rect.bottom > rcScreen.bottom)
  102. rect.bottom = rcScreen.bottom;
  103. SetWindowPos(NULL,
  104. point.x, point.y,
  105. rect.Width(), rect.Height(),
  106. SWP_SHOWWINDOW|SWP_NOCOPYBITS|SWP_NOACTIVATE|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. Hide();
  184. break;
  185. }
  186. case WM_KEYDOWN:
  187. {
  188. WPARAM vk = pMsg->wParam;
  189. if(vk == VK_ESCAPE)
  190. {
  191. Hide();
  192. return TRUE;
  193. }
  194. else if(vk == VK_TAB)
  195. {
  196. m_RichEdit.SetFocus();
  197. return TRUE;
  198. }
  199. Hide();
  200. break;
  201. }
  202. case WM_LBUTTONDBLCLK:
  203. case WM_RBUTTONDOWN :
  204. case WM_RBUTTONDBLCLK:
  205. case WM_MBUTTONDOWN:
  206. case WM_MBUTTONDBLCLK:
  207. case WM_NCLBUTTONDOWN:
  208. case WM_NCLBUTTONDBLCLK:
  209. case WM_NCRBUTTONDOWN:
  210. case WM_NCRBUTTONDBLCLK:
  211. case WM_NCMBUTTONDOWN:
  212. case WM_NCMBUTTONDBLCLK:
  213. {
  214. Hide();
  215. break;
  216. }
  217. }
  218. return FALSE;
  219. }
  220. CRect CToolTipEx::GetBoundsRect()
  221. {
  222. CWindowDC dc(NULL);
  223. CFont *pOldFont = (CFont*) dc.SelectObject((CFont*)&m_Font);
  224. int nLineWidth = 0;
  225. if (nLineWidth == 0)
  226. {
  227. // Count the number of lines of text
  228. int nStart = 0, nNumLines = 0;
  229. CString strTextCopy = m_csText;
  230. do
  231. {
  232. nStart = strTextCopy.Find(_T("\n"));
  233. // skip found character
  234. if (nStart >= 0)
  235. strTextCopy = strTextCopy.Mid(nStart+1);
  236. nNumLines++;
  237. } while (nStart >= 0);
  238. // Find the widest line
  239. for (int i = 0; i < nNumLines; i++)
  240. {
  241. CString strLine = GetFieldFromString(m_csText, i, _T('\n')) + _T(" ");
  242. nLineWidth = max(nLineWidth, dc.GetTextExtent(strLine).cx);
  243. }
  244. }
  245. CRect rect(0, 0, max(0,nLineWidth), 0);
  246. dc.DrawText(m_csText, rect, DT_CALCRECT | m_dwTextStyle);
  247. dc.SelectObject(pOldFont);
  248. rect.bottom += m_rectMargin.top + m_rectMargin.bottom;
  249. rect.right += m_rectMargin.left + m_rectMargin.right + 2;
  250. if(m_pBitmap)
  251. {
  252. int nWidth = CBitmapHelper::GetCBitmapWidth(*m_pBitmap);
  253. int nHeight = CBitmapHelper::GetCBitmapHeight(*m_pBitmap);
  254. rect.bottom += nHeight;
  255. if((rect.left + nWidth) > rect.right)
  256. rect.right = rect.left + nWidth;
  257. }
  258. return rect;
  259. }
  260. CString CToolTipEx::GetFieldFromString(CString ref, int nIndex, TCHAR ch)
  261. {
  262. CString strReturn;
  263. LPCTSTR pstrStart = ref.LockBuffer();
  264. LPCTSTR pstrBuffer = pstrStart;
  265. int nCurrent = 0;
  266. int nStart = 0;
  267. int nEnd = 0;
  268. int nOldStart = 0;
  269. while (nCurrent <= nIndex && *pstrBuffer != _T('\0'))
  270. {
  271. if (*pstrBuffer == ch)
  272. {
  273. nOldStart = nStart;
  274. nStart = nEnd+1;
  275. nCurrent++;
  276. }
  277. nEnd++;
  278. pstrBuffer++;
  279. }
  280. // May have reached the end of the string
  281. if (*pstrBuffer == _T('\0'))
  282. {
  283. nOldStart = nStart;
  284. nEnd++;
  285. }
  286. ref.UnlockBuffer();
  287. if (nCurrent < nIndex)
  288. {
  289. //TRACE1("Warning: GetStringField - Couldn't find field %d.\n", nIndex);
  290. return strReturn;
  291. }
  292. return ref.Mid(nOldStart, nEnd-nOldStart-1);
  293. }
  294. LPLOGFONT CToolTipEx::GetSystemToolTipFont()
  295. {
  296. static LOGFONT LogFont;
  297. NONCLIENTMETRICS ncm;
  298. ncm.cbSize = sizeof(NONCLIENTMETRICS);
  299. if (!SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0))
  300. return FALSE;
  301. memcpy(&LogFont, &(ncm.lfStatusFont), sizeof(LOGFONT));
  302. return &LogFont;
  303. }
  304. BOOL CToolTipEx::SetLogFont(LPLOGFONT lpLogFont, BOOL bRedraw /*=TRUE*/)
  305. {
  306. ASSERT(lpLogFont);
  307. if (!lpLogFont)
  308. return FALSE;
  309. LOGFONT LogFont;
  310. // Store font as the global default
  311. memcpy(&LogFont, lpLogFont, sizeof(LOGFONT));
  312. // Create the actual font object
  313. m_Font.DeleteObject();
  314. m_Font.CreateFontIndirect(&LogFont);
  315. if (bRedraw && ::IsWindow(GetSafeHwnd()))
  316. Invalidate();
  317. return TRUE;
  318. }
  319. void CToolTipEx::SetBitmap(CBitmap *pBitmap)
  320. {
  321. DELETE_BITMAP
  322. m_pBitmap = pBitmap;
  323. }
  324. void CToolTipEx::OnSize(UINT nType, int cx, int cy)
  325. {
  326. CWnd::OnSize(nType, cx, cy);
  327. if(::IsWindow(m_RichEdit.GetSafeHwnd()) == FALSE)
  328. return;
  329. CRect cr;
  330. GetClientRect(cr);
  331. // cr.DeflateRect(0, 0, 15, 0);
  332. m_RichEdit.MoveWindow(cr);
  333. }
  334. BOOL CToolTipEx::IsCursorInToolTip()
  335. {
  336. CRect cr;
  337. GetWindowRect(cr);
  338. CPoint cursorPos;
  339. GetCursorPos(&cursorPos);
  340. return cr.PtInRect(cursorPos);
  341. }
  342. void CToolTipEx::SetRTFText(const CString &csRTF)
  343. {
  344. m_RichEdit.SetRTF(csRTF);
  345. m_csRTF = csRTF;
  346. }
  347. void CToolTipEx::SetToolTipText(const CString &csText)
  348. {
  349. m_RichEdit.SetText(csText);
  350. m_csText = csText;
  351. m_RichEdit.SetFont(&m_Font);
  352. }
  353. UINT CToolTipEx::OnNcHitTest(CPoint point)
  354. {
  355. CRect crWindow;
  356. GetWindowRect(crWindow);
  357. const static int nBorder = 10;
  358. if((point.y < crWindow.top + nBorder) &&
  359. (point.x < crWindow.left + nBorder))
  360. return HTTOPLEFT;
  361. else if((point.y < crWindow.top + nBorder) &&
  362. (point.x > crWindow.right - nBorder))
  363. return HTTOPRIGHT;
  364. else if((point.y > crWindow.bottom - nBorder) &&
  365. (point.x > crWindow.right - nBorder))
  366. return HTBOTTOMRIGHT;
  367. else if((point.y > crWindow.bottom - nBorder) &&
  368. (point.x < crWindow.left + nBorder))
  369. return HTBOTTOMLEFT;
  370. if(point.y < crWindow.top + nBorder)
  371. return HTTOP;
  372. else if(point.y > crWindow.bottom - nBorder)
  373. return HTBOTTOM;
  374. else if(point.x > crWindow.right - nBorder)
  375. return HTRIGHT;
  376. else if(point.x < crWindow.left + nBorder)
  377. return HTLEFT;
  378. // if(point.x > crWindow.right - 15)
  379. // return HTCAPTION;
  380. return CWnd::OnNcHitTest(point);
  381. }
  382. void CToolTipEx::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
  383. {
  384. CWnd::OnActivate(nState, pWndOther, bMinimized);
  385. if (nState == WA_INACTIVE)
  386. {
  387. Hide();
  388. if(m_pNotifyWnd)
  389. {
  390. m_pNotifyWnd->PostMessage(NM_INACTIVE_TOOLTIPWND, 0, 0);
  391. }
  392. }
  393. }