Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
6.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <windows.h>
  8. #include "consolewnd.h"
  9. #pragma warning( disable : 4311 ) // warning C4311: 'reinterpret_cast' : pointer truncation from 'CConsoleWnd *const ' to 'LONG'
  10. #pragma warning( disable : 4312 ) // warning C4312: 'type cast' : conversion from 'LONG' to 'CConsoleWnd *' of greater size
  11. #define EDITCONTROL_BORDER_SIZE 5
  12. // ------------------------------------------------------------------------------------------------ //
  13. // Functions to manage the console window.
  14. // ------------------------------------------------------------------------------------------------ //
  15. class CConsoleWnd : public IConsoleWnd
  16. {
  17. public:
  18. CConsoleWnd();
  19. ~CConsoleWnd();
  20. bool Init( void *hInstance, int dialogResourceID, int editControlID, bool bVisible );
  21. void Term();
  22. virtual void Release();
  23. virtual void SetVisible( bool bVisible );
  24. virtual bool IsVisible() const;
  25. virtual void PrintToConsole( const char *pMsg );
  26. virtual void SetTitle( const char *pTitle );
  27. virtual void SetDeleteOnClose( bool bDelete );
  28. private:
  29. int WindowProc(
  30. HWND hwndDlg, // handle to dialog box
  31. UINT uMsg, // message
  32. WPARAM wParam, // first message parameter
  33. LPARAM lParam // second message parameter
  34. );
  35. static int CALLBACK StaticWindowProc(
  36. HWND hwndDlg, // handle to dialog box
  37. UINT uMsg, // message
  38. WPARAM wParam, // first message parameter
  39. LPARAM lParam // second message parameter
  40. );
  41. void RepositionEditControl();
  42. private:
  43. HWND m_hWnd;
  44. HWND m_hEditControl;
  45. bool m_bVisible;
  46. bool m_bDeleteOnClose;
  47. int m_nCurrentChars;
  48. };
  49. CConsoleWnd::CConsoleWnd()
  50. {
  51. m_hWnd = m_hEditControl = NULL;
  52. m_bVisible = false;
  53. m_bDeleteOnClose = false;
  54. m_nCurrentChars = 0;
  55. }
  56. CConsoleWnd::~CConsoleWnd()
  57. {
  58. Term();
  59. }
  60. bool CConsoleWnd::Init( void *hInstance, int dialogResourceID, int editControlID, bool bVisible )
  61. {
  62. // Create the window.
  63. m_hWnd = CreateDialog(
  64. (HINSTANCE)hInstance,
  65. MAKEINTRESOURCE( dialogResourceID ),
  66. NULL,
  67. &CConsoleWnd::StaticWindowProc );
  68. if ( !m_hWnd )
  69. return false;
  70. SetWindowLong( m_hWnd, GWL_USERDATA, reinterpret_cast< LONG >( this ) );
  71. if ( bVisible )
  72. ShowWindow( m_hWnd, SW_SHOW );
  73. // Get a handle to the edit control.
  74. m_hEditControl = GetDlgItem( m_hWnd, editControlID );
  75. if ( !m_hEditControl )
  76. return false;
  77. RepositionEditControl();
  78. m_bVisible = bVisible;
  79. return true;
  80. }
  81. void CConsoleWnd::Term()
  82. {
  83. if ( m_hWnd )
  84. {
  85. DestroyWindow( m_hWnd );
  86. m_hWnd = NULL;
  87. }
  88. }
  89. void CConsoleWnd::Release()
  90. {
  91. delete this;
  92. }
  93. void CConsoleWnd::SetVisible( bool bVisible )
  94. {
  95. ShowWindow( m_hWnd, bVisible ? SW_RESTORE : SW_HIDE );
  96. if ( bVisible )
  97. {
  98. ShowWindow( m_hWnd, SW_SHOW );
  99. SetWindowPos( m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
  100. UpdateWindow( m_hWnd );
  101. int nLen = (int)SendMessage( m_hEditControl, EM_GETLIMITTEXT, 0, 0 );
  102. SendMessage( m_hEditControl, EM_SETSEL, nLen, nLen );
  103. }
  104. else
  105. {
  106. SetWindowPos( m_hWnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_HIDEWINDOW | SWP_NOOWNERZORDER );
  107. }
  108. m_bVisible = bVisible;
  109. }
  110. bool CConsoleWnd::IsVisible() const
  111. {
  112. return m_bVisible;
  113. }
  114. void CConsoleWnd::PrintToConsole( const char *pMsg )
  115. {
  116. if ( m_nCurrentChars >= 16*1024 )
  117. {
  118. // Clear the edit control otherwise it'll stop outputting anything.
  119. m_nCurrentChars = 0;
  120. ClearEditControl( m_hEditControl );
  121. }
  122. FormatAndSendToEditControl( m_hEditControl, pMsg );
  123. m_nCurrentChars += (int)strlen( pMsg );
  124. }
  125. void CConsoleWnd::SetTitle( const char *pTitle )
  126. {
  127. SetWindowText( m_hWnd, pTitle );
  128. }
  129. int CConsoleWnd::WindowProc(
  130. HWND hwndDlg, // handle to dialog box
  131. UINT uMsg, // message
  132. WPARAM wParam, // first message parameter
  133. LPARAM lParam // second message parameter
  134. )
  135. {
  136. lParam = lParam; // avoid compiler warning
  137. if ( hwndDlg != m_hWnd )
  138. return false;
  139. switch ( uMsg )
  140. {
  141. case WM_SYSCOMMAND:
  142. {
  143. if ( wParam == SC_CLOSE )
  144. {
  145. if ( m_bDeleteOnClose )
  146. {
  147. Release();
  148. }
  149. else
  150. {
  151. SetVisible( false );
  152. return true;
  153. }
  154. }
  155. }
  156. break;
  157. case WM_SHOWWINDOW:
  158. {
  159. m_bVisible = (wParam != 0);
  160. }
  161. break;
  162. case WM_SIZE:
  163. case WM_INITDIALOG:
  164. {
  165. RepositionEditControl();
  166. }
  167. break;
  168. }
  169. return false;
  170. }
  171. int CConsoleWnd::StaticWindowProc(
  172. HWND hwndDlg, // handle to dialog box
  173. UINT uMsg, // message
  174. WPARAM wParam, // first message parameter
  175. LPARAM lParam // second message parameter
  176. )
  177. {
  178. CConsoleWnd *pDlg = (CConsoleWnd*)GetWindowLong( hwndDlg, GWL_USERDATA );
  179. if ( pDlg )
  180. return pDlg->WindowProc( hwndDlg, uMsg, wParam, lParam );
  181. else
  182. return false;
  183. }
  184. void CConsoleWnd::RepositionEditControl()
  185. {
  186. RECT rcMain;
  187. GetClientRect( m_hWnd, &rcMain );
  188. RECT rcNew;
  189. rcNew.left = rcMain.left + EDITCONTROL_BORDER_SIZE;
  190. rcNew.right = rcMain.right - EDITCONTROL_BORDER_SIZE;
  191. rcNew.top = rcMain.top + EDITCONTROL_BORDER_SIZE;
  192. rcNew.bottom = rcMain.bottom - EDITCONTROL_BORDER_SIZE;
  193. SetWindowPos(
  194. m_hEditControl,
  195. NULL,
  196. rcNew.left,
  197. rcNew.top,
  198. rcNew.right - rcNew.left,
  199. rcNew.bottom - rcNew.top,
  200. SWP_NOZORDER );
  201. }
  202. void CConsoleWnd::SetDeleteOnClose( bool bDelete )
  203. {
  204. m_bDeleteOnClose = bDelete;
  205. }
  206. // ------------------------------------------------------------------------------------ //
  207. // Module interface.
  208. // ------------------------------------------------------------------------------------ //
  209. void ClearEditControl( void *hWnd )
  210. {
  211. HWND hEditControl = (HWND)hWnd;
  212. int nLen = (int)SendMessage( hEditControl, EM_GETLIMITTEXT, 0, 0 );
  213. SendMessage( hEditControl, EM_SETSEL, 0, nLen );
  214. SendMessage( hEditControl, EM_REPLACESEL, FALSE, (LPARAM)"" );
  215. }
  216. void SendToEditControl( HWND hEditControl, const char *pText )
  217. {
  218. int nLen = (int)SendMessage( hEditControl, EM_GETLIMITTEXT, 0, 0 );
  219. SendMessage( hEditControl, EM_SETSEL, nLen, nLen );
  220. SendMessage( hEditControl, EM_REPLACESEL, FALSE, (LPARAM)pText );
  221. }
  222. void FormatAndSendToEditControl( void *hWnd, const char *pText )
  223. {
  224. HWND hEditControl = (HWND)hWnd;
  225. // Translate \n to \r\n.
  226. char outMsg[1024];
  227. const char *pIn = pText;
  228. char *pOut = outMsg;
  229. while ( *pIn )
  230. {
  231. if ( *pIn == '\n' )
  232. {
  233. *pOut = '\r';
  234. pOut++;
  235. }
  236. *pOut = *pIn;
  237. ++pIn;
  238. ++pOut;
  239. if ( pOut - outMsg >= 1020 )
  240. {
  241. *pOut = 0;
  242. SendToEditControl( hEditControl, outMsg );
  243. pOut = outMsg;
  244. }
  245. }
  246. *pOut = 0;
  247. SendToEditControl( hEditControl, outMsg );
  248. }
  249. IConsoleWnd* CreateConsoleWnd( void *hInstance, int dialogResourceID, int editControlID, bool bVisible )
  250. {
  251. CConsoleWnd *pWnd = new CConsoleWnd;
  252. if ( pWnd->Init( hInstance, dialogResourceID, editControlID, bVisible ) )
  253. {
  254. return pWnd;
  255. }
  256. else
  257. {
  258. pWnd->Release();
  259. return NULL;
  260. }
  261. }