ToolTipEx.cpp 9.7 KB

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