ConsoleDlg.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "qe3.h"
  23. #include "Radiant.h"
  24. #include "ConsoleDlg.h"
  25. // CConsoleDlg dialog
  26. IMPLEMENT_DYNCREATE(CConsoleDlg, CDialog)
  27. CConsoleDlg::CConsoleDlg(CWnd* pParent /*=NULL*/)
  28. : CDialog(CConsoleDlg::IDD)
  29. {
  30. currentHistoryPosition = -1;
  31. currentCommand = "";
  32. saveCurrentCommand = true;
  33. }
  34. CConsoleDlg::~CConsoleDlg()
  35. {
  36. }
  37. void CConsoleDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. DDX_Control(pDX, IDC_EDIT_CONSOLE, editConsole);
  41. DDX_Control(pDX, IDC_EDIT_INPUT, editInput);
  42. }
  43. void CConsoleDlg::AddText( const char *msg ) {
  44. idStr work;
  45. CString work2;
  46. work = msg;
  47. work.RemoveColors();
  48. work = CEntityDlg::TranslateString( work.c_str() );
  49. editConsole.GetWindowText( work2 );
  50. int len = work2.GetLength();
  51. if ( len + work.Length() > (int)editConsole.GetLimitText() ) {
  52. work2 = work2.Right( editConsole.GetLimitText() * 0.75 );
  53. len = work2.GetLength();
  54. editConsole.SetWindowText(work2);
  55. }
  56. editConsole.SetSel( len, len );
  57. editConsole.ReplaceSel( work );
  58. }
  59. BEGIN_MESSAGE_MAP(CConsoleDlg, CDialog)
  60. ON_WM_SIZE()
  61. ON_WM_SETFOCUS()
  62. ON_WM_ACTIVATE()
  63. END_MESSAGE_MAP()
  64. // CConsoleDlg message handlers
  65. void CConsoleDlg::OnSize(UINT nType, int cx, int cy)
  66. {
  67. CDialog::OnSize(nType, cx, cy);
  68. if (editInput.GetSafeHwnd() == NULL) {
  69. return;
  70. }
  71. CRect rect, crect;
  72. GetWindowRect(rect);
  73. editInput.GetWindowRect(crect);
  74. editInput.SetWindowPos(NULL, 4, rect.Height() - 4 - crect.Height(), rect.Width() - 8, crect.Height(), SWP_SHOWWINDOW);
  75. editConsole.SetWindowPos(NULL, 4, 4, rect.Width() - 8, rect.Height() - crect.Height() - 8, SWP_SHOWWINDOW);
  76. }
  77. BOOL CConsoleDlg::PreTranslateMessage(MSG* pMsg)
  78. {
  79. if (pMsg->hwnd == editInput.GetSafeHwnd()) {
  80. if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE ) {
  81. Select_Deselect();
  82. g_pParentWnd->SetFocus ();
  83. return TRUE;
  84. }
  85. if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN ) {
  86. ExecuteCommand();
  87. return TRUE;
  88. }
  89. if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE ) {
  90. if (pMsg->wParam == VK_ESCAPE) {
  91. g_pParentWnd->GetCamera()->SetFocus();
  92. Select_Deselect();
  93. }
  94. return TRUE;
  95. }
  96. if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_UP ) {
  97. //save off the current in-progress command so we can get back to it
  98. if ( saveCurrentCommand == true ) {
  99. CString str;
  100. editInput.GetWindowText ( str );
  101. currentCommand = str.GetBuffer ( 0 );
  102. saveCurrentCommand = false;
  103. }
  104. if ( consoleHistory.Num () > 0 ) {
  105. editInput.SetWindowText ( consoleHistory[currentHistoryPosition] );
  106. int selLocation = consoleHistory[currentHistoryPosition].Length ();
  107. editInput.SetSel ( selLocation , selLocation + 1);
  108. }
  109. if ( currentHistoryPosition > 0) {
  110. --currentHistoryPosition;
  111. }
  112. return TRUE;
  113. }
  114. if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN ) {
  115. int selLocation = 0;
  116. if ( currentHistoryPosition < consoleHistory.Num () - 1 ) {
  117. ++currentHistoryPosition;
  118. editInput.SetWindowText ( consoleHistory[currentHistoryPosition] );
  119. selLocation = consoleHistory[currentHistoryPosition].Length ();
  120. }
  121. else {
  122. editInput.SetWindowText ( currentCommand );
  123. selLocation = currentCommand.Length ();
  124. currentCommand.Clear ();
  125. saveCurrentCommand = true;
  126. }
  127. editInput.SetSel ( selLocation , selLocation + 1);
  128. return TRUE;
  129. }
  130. if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_TAB ) {
  131. common->Printf ( "Command History\n----------------\n" );
  132. for ( int i = 0 ; i < consoleHistory.Num ();i++ )
  133. {
  134. common->Printf ( "[cmd %d]: %s\n" , i , consoleHistory[i].c_str() );
  135. }
  136. common->Printf ( "----------------\n" );
  137. }
  138. if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_NEXT) {
  139. editConsole.LineScroll ( 10 );
  140. }
  141. if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_PRIOR ) {
  142. editConsole.LineScroll ( -10 );
  143. }
  144. if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_HOME ) {
  145. editConsole.LineScroll ( -editConsole.GetLineCount() );
  146. }
  147. if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_END ) {
  148. editConsole.LineScroll ( editConsole.GetLineCount() );
  149. }
  150. }
  151. return CDialog::PreTranslateMessage(pMsg);
  152. }
  153. void CConsoleDlg::OnSetFocus(CWnd* pOldWnd) {
  154. CDialog::OnSetFocus(pOldWnd);
  155. editInput.SetFocus();
  156. }
  157. void CConsoleDlg::SetConsoleText ( const idStr& text ) {
  158. editInput.Clear ();
  159. editInput.SetWindowText ( text.c_str() );
  160. }
  161. void CConsoleDlg::ExecuteCommand ( const idStr& cmd ) {
  162. CString str;
  163. if ( cmd.Length() > 0 ) {
  164. str = cmd;
  165. }
  166. else {
  167. editInput.GetWindowText(str);
  168. }
  169. if ( str != "" ) {
  170. editInput.SetWindowText("");
  171. common->Printf("%s\n", str.GetBuffer(0));
  172. //avoid adding multiple identical commands in a row
  173. int index = consoleHistory.Num ();
  174. if ( index == 0 || str.GetBuffer(0) != consoleHistory[index-1]) {
  175. //keep the history to 16 commands, removing the oldest command
  176. if ( consoleHistory.Num () > 16 ) {
  177. consoleHistory.RemoveIndex ( 0 );
  178. }
  179. currentHistoryPosition = consoleHistory.Append ( str.GetBuffer (0) );
  180. }
  181. else {
  182. currentHistoryPosition = consoleHistory.Num () - 1;
  183. }
  184. currentCommand.Clear ();
  185. bool propogateCommand = true;
  186. //process some of our own special commands
  187. if ( str.CompareNoCase ( "clear" ) == 0) {
  188. editConsole.SetSel ( 0 , -1 );
  189. editConsole.Clear ();
  190. }
  191. else if ( str.CompareNoCase ( "edit" ) == 0) {
  192. propogateCommand = false;
  193. }
  194. if ( propogateCommand ) {
  195. cmdSystem->BufferCommandText( CMD_EXEC_NOW, str );
  196. }
  197. Sys_UpdateWindows(W_ALL);
  198. }
  199. }
  200. void CConsoleDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
  201. {
  202. CDialog::OnActivate(nState, pWndOther, bMinimized);
  203. if ( nState == WA_ACTIVE || nState == WA_CLICKACTIVE )
  204. {
  205. editInput.SetFocus();
  206. }
  207. }