ToolTipWnd.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. // ToolTipWnd.cpp : implementation file
  13. //
  14. #include "stdafx.h"
  15. #include "WorldEditor.h"
  16. #include "ToolTipWnd.h"
  17. #ifdef _DEBUG
  18. #undef new
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CToolTipWnd
  25. CStaticArray<PIX> _saPixLineHeights;
  26. CToolTipWnd::CToolTipWnd()
  27. {
  28. }
  29. CToolTipWnd::~CToolTipWnd()
  30. {
  31. _saPixLineHeights.Clear();
  32. }
  33. BEGIN_MESSAGE_MAP(CToolTipWnd, CWnd)
  34. //{{AFX_MSG_MAP(CToolTipWnd)
  35. ON_WM_PAINT()
  36. ON_WM_CREATE()
  37. ON_WM_SETFOCUS()
  38. ON_WM_TIMER()
  39. //}}AFX_MSG_MAP
  40. END_MESSAGE_MAP()
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CToolTipWnd message handlers
  43. CTString CToolTipWnd::GetLine(INDEX iLine)
  44. {
  45. INDEX ctLines=0;
  46. const char *pText = (const char *) m_strText;
  47. while( *pText != 0)
  48. {
  49. if( iLine == ctLines)
  50. {
  51. char achrLine[1024];
  52. INDEX iChar = 0;
  53. while( (*pText != '\n') && (*pText != 0) )
  54. {
  55. achrLine[iChar] = *pText;
  56. iChar++;
  57. pText++;
  58. }
  59. achrLine[iChar] = 0;
  60. return CTString( achrLine);
  61. }
  62. if( *pText == '\n')
  63. {
  64. ctLines++;
  65. }
  66. pText++;
  67. }
  68. ctLines++;
  69. return CTString("Line not found !!!");
  70. }
  71. INDEX CToolTipWnd::GetLinesCount( void)
  72. {
  73. INDEX ctLines=0;
  74. const char *pText = (const char *) m_strText;
  75. while( *pText != 0)
  76. {
  77. if( *pText == '\n')
  78. {
  79. ctLines++;
  80. if( *(pText+1) == 0) return ctLines;
  81. }
  82. pText++;
  83. }
  84. return ctLines+1;
  85. }
  86. void CToolTipWnd::ObtainTextSize(PIX &pixMaxWidth, PIX &pixMaxHeight)
  87. {
  88. CDC *pDC = GetDC();
  89. if( pDC == NULL) return;
  90. pixMaxWidth = 0;
  91. _saPixLineHeights.Clear();
  92. PIX pixStartY = 0;
  93. INDEX ctLines = GetLinesCount();
  94. _saPixLineHeights.New( ctLines);
  95. for(INDEX iLine = 0; iLine<ctLines; iLine++)
  96. {
  97. CTString strLine = GetLine(iLine);
  98. CSize size = pDC->GetOutputTextExtent( CString(strLine));
  99. if( size.cx>pixMaxWidth) pixMaxWidth = size.cx;
  100. _saPixLineHeights[iLine] = pixStartY;
  101. pixStartY += size.cy;
  102. }
  103. pixMaxHeight = pixStartY;
  104. ReleaseDC( pDC);
  105. }
  106. void CToolTipWnd::OnPaint()
  107. {
  108. CPaintDC dc(this);
  109. CRect rectWindow;
  110. GetClientRect(rectWindow);
  111. DWORD colPaper = GetSysColor( COLOR_INFOBK);
  112. DWORD colInk = GetSysColor( COLOR_INFOTEXT);
  113. dc.FillSolidRect( rectWindow, colPaper);
  114. dc.SetTextColor( colInk);
  115. CDC *pDC = GetDC();
  116. if (pDC == NULL) return;
  117. pDC->SelectObject( &theApp.m_FixedFont);
  118. INDEX ctLines = GetLinesCount();
  119. for(INDEX iLine = 0; iLine<ctLines; iLine++)
  120. {
  121. CTString strLine = GetLine(iLine);
  122. dc.TextOut( 0, _saPixLineHeights[iLine], CString(strLine));
  123. }
  124. ReleaseDC( pDC);
  125. }
  126. int CToolTipWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
  127. {
  128. if (CWnd::OnCreate(lpCreateStruct) == -1)
  129. return -1;
  130. SetupWindowSizeAndPosition();
  131. if( !m_bManualControl)
  132. {
  133. SetTimer( 0, 10, NULL);
  134. }
  135. return 0;
  136. }
  137. void CToolTipWnd::SetupWindowSizeAndPosition(void)
  138. {
  139. CDC *pDC = GetDC();
  140. pDC->SelectObject( &theApp.m_FixedFont);
  141. PIX pixWidth, pixHeight;
  142. ObtainTextSize( pixWidth, pixHeight);
  143. pixWidth+=2;
  144. pixHeight+=2;
  145. GetCursorPos( &m_ptMouse);
  146. int iCursorX = 12;
  147. int iCursorY = 18;
  148. CRect rectWindow;
  149. rectWindow.left = m_ptMouse.x+iCursorX;
  150. rectWindow.top = m_ptMouse.y+iCursorY;
  151. if( m_bManualControl)
  152. {
  153. rectWindow.left = m_pixManualX;
  154. rectWindow.top = m_pixManualY;
  155. }
  156. rectWindow.right = rectWindow.left + pixWidth;
  157. rectWindow.bottom = rectWindow.top + pixHeight;
  158. int iEdgeX = ::GetSystemMetrics(SM_CXEDGE); // window edge width
  159. int iEdgeY = ::GetSystemMetrics(SM_CYEDGE);
  160. PIX pixScreenX = ::GetSystemMetrics(SM_CXMAXIMIZED)-4*iEdgeX; // screen size
  161. PIX pixScreenY = ::GetSystemMetrics(SM_CYMAXIMIZED)-4*iEdgeX;
  162. if(rectWindow.bottom > pixScreenY)
  163. {
  164. rectWindow.top -= rectWindow.bottom-pixScreenY;
  165. }
  166. if(rectWindow.right > pixScreenX)
  167. {
  168. rectWindow.left -= rectWindow.right-pixScreenX;
  169. }
  170. MoveWindow( rectWindow);
  171. ReleaseDC( pDC);
  172. }
  173. void CToolTipWnd::OnSetFocus(CWnd* pOldWnd)
  174. {
  175. if ( pOldWnd!=NULL && IsWindow(pOldWnd->m_hWnd))
  176. {
  177. pOldWnd->SetFocus();
  178. }
  179. }
  180. void CToolTipWnd::ManualUpdate(void)
  181. {
  182. SetupWindowSizeAndPosition();
  183. Invalidate( FALSE);
  184. }
  185. void CToolTipWnd::ManualOff(void)
  186. {
  187. CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
  188. if( pMainFrame->m_pwndToolTip != NULL)
  189. {
  190. delete pMainFrame->m_pwndToolTip;
  191. pMainFrame->m_pwndToolTip = NULL;
  192. }
  193. }
  194. void CToolTipWnd::OnTimer(UINT nIDEvent)
  195. {
  196. ASSERT( m_bManualControl == FALSE);
  197. if( nIDEvent == 0)
  198. {
  199. POINT ptMouse;
  200. GetCursorPos( &ptMouse);
  201. if( ((Abs(ptMouse.x-m_ptMouse.x)) > 2) ||
  202. ((Abs(ptMouse.y-m_ptMouse.y)) > 2) )
  203. {
  204. KillTimer( 0);
  205. CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
  206. delete pMainFrame->m_pwndToolTip;
  207. pMainFrame->m_pwndToolTip = NULL;
  208. }
  209. }
  210. }