doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConsoleView.cpp
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 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.
23 
24 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.
25 
26 ===========================================================================
27 */
28 #include "../../idlib/precompiled.h"
29 #pragma hdrstop
30 
31 #include "ConsoleView.h"
32 
33 #ifdef _DEBUG
34 #define new DEBUG_NEW
35 #endif
36 
37 #define EDIT_HEIGHT 25
38 
39 
41 
42 BEGIN_MESSAGE_MAP(ConsoleView, CFormView)
43  ON_WM_SIZE()
44 END_MESSAGE_MAP()
45 
49 ConsoleView::ConsoleView()
50 : CFormView(ConsoleView::IDD) {
51 }
52 
57 }
58 
65 void ConsoleView::AddText( const char *msg ) {
66 
67  if(!editConsole.GetSafeHwnd())
68  return;
69 
70  idStr work;
71  CString work2;
72 
73  work = msg;
74  work.RemoveColors();
75  work = TranslateString( work.c_str() );
76 
77  editConsole.GetWindowText( work2 );
78  int len = work2.GetLength();
79  if ( len + work.Length() > (int)editConsole.GetLimitText() ) {
80  work2 = work2.Right( editConsole.GetLimitText() * .75f );
81  len = work2.GetLength();
82  editConsole.SetWindowText(work2);
83  }
84  editConsole.SetSel( len, len );
85  editConsole.ReplaceSel( work );
86 
87  //Hack: scrolls down a bit
88  editConsole.LineScroll(100);
89 }
90 
95 void ConsoleView::SetConsoleText ( const idStr& text ) {
96  editInput.Clear ();
97  editInput.SetWindowText ( text.c_str() );
98 }
99 
107 void ConsoleView::ExecuteCommand ( const idStr& cmd ) {
108 
109  CString str;
110  if ( cmd.Length() > 0 ) {
111  str = cmd;
112  }
113  else {
114  editInput.GetWindowText(str);
115  }
116 
117  if ( str != "" ) {
118  editInput.SetWindowText("");
119  common->Printf("%s\n", str.GetBuffer(0));
120 
121  //avoid adding multiple identical commands in a row
122  int index = consoleHistory.Num ();
123 
124  if ( index == 0 || str.GetBuffer(0) != consoleHistory[index-1]) {
125  //keep the history to 16 commands, removing the oldest command
126  if ( consoleHistory.Num () > 16 ) {
128  }
129  currentHistoryPosition = consoleHistory.Append ( str.GetBuffer (0) );
130  }
131  else {
133  }
134 
136 
137  bool propogateCommand = true;
138 
139  //process some of our own special commands
140  if ( str.CompareNoCase ( "clear" ) == 0) {
141  editConsole.SetSel ( 0 , -1 );
142  editConsole.Clear ();
143  }
144  else if ( str.CompareNoCase ( "edit" ) == 0) {
145  propogateCommand = false;
146  }
147  if ( propogateCommand ) {
149  }
150  }
151 }
152 
158 
159  if (pMsg->hwnd == editInput.GetSafeHwnd()) {
160 
161  if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN ) {
162  this->ExecuteCommand();
163  return TRUE;
164  }
165 
166  if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_UP ) {
167  //save off the current in-progress command so we can get back to it
168  if ( saveCurrentCommand == true ) {
169  CString str;
170  editInput.GetWindowText ( str );
171  currentCommand = str.GetBuffer ( 0 );
172  saveCurrentCommand = false;
173  }
174 
175  if ( consoleHistory.Num () > 0 ) {
177 
178  int selLocation = consoleHistory[currentHistoryPosition].Length ();
179  editInput.SetSel ( selLocation , selLocation + 1);
180  }
181 
182  if ( currentHistoryPosition > 0) {
184  }
185 
186  return TRUE;
187  }
188 
189  if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN ) {
190  int selLocation = 0;
191  if ( currentHistoryPosition < consoleHistory.Num () - 1 ) {
194  selLocation = consoleHistory[currentHistoryPosition].Length ();
195  }
196  else {
197  editInput.SetWindowText ( currentCommand );
198  selLocation = currentCommand.Length ();
200  saveCurrentCommand = true;
201  }
202 
203  editInput.SetSel ( selLocation , selLocation + 1);
204 
205  return TRUE;
206  }
207  if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_TAB ) {
208  common->Printf ( "Command History\n----------------\n" );
209  for ( int i = 0 ; i < consoleHistory.Num ();i++ )
210  {
211  common->Printf ( "[cmd %d]: %s\n" , i , consoleHistory[i].c_str() );
212  }
213  common->Printf ( "----------------\n" );
214  }
215  if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_NEXT) {
216  editConsole.LineScroll ( 10 );
217  }
218 
219  if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_PRIOR ) {
220  editConsole.LineScroll ( -10 );
221  }
222 
223  if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_HOME ) {
224  editConsole.LineScroll ( -editConsole.GetLineCount() );
225  }
226 
227  if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_END ) {
228  editConsole.LineScroll ( editConsole.GetLineCount() );
229  }
230  }
231 
232  return CFormView::PreTranslateMessage(pMsg);
233 }
234 
238 void ConsoleView::DoDataExchange(CDataExchange* pDX) {
239  CFormView::DoDataExchange(pDX);
240 
241  DDX_Control(pDX, IDC_CONSOLE_OUTPUT, editConsole);
242  DDX_Control(pDX, IDC_CONSOLE_EDIT, editInput);
243 }
244 
249 
250  CFormView::OnInitialUpdate();
251 
252  CRect rect;
253  GetWindowRect(rect);
254 
255  if(editConsole.m_hWnd)
256  editConsole.MoveWindow(0, 0, rect.Width(), rect.Height()-EDIT_HEIGHT);
257 
258  if(editInput.m_hWnd)
259  editInput.MoveWindow(0, rect.Height()-EDIT_HEIGHT, rect.Width(), EDIT_HEIGHT);
260 }
261 
265 void ConsoleView::OnSize(UINT nType, int cx, int cy) {
266  CFormView::OnSize(nType, cx, cy);
267 
268  //Move the edit windows around
269  if(editConsole.GetSafeHwnd())
270  editConsole.MoveWindow(0, 0, cx, cy-EDIT_HEIGHT);
271 
272  if(editInput.GetSafeHwnd())
273  editInput.MoveWindow(0, cy-EDIT_HEIGHT, cx, EDIT_HEIGHT);
274 }
275 
279 const char *ConsoleView::TranslateString(const char *buf) {
280  static char buf2[32768];
281  int i, l;
282  char *out;
283 
284  l = strlen(buf);
285  out = buf2;
286  for (i = 0; i < l; i++) {
287  if (buf[i] == '\n') {
288  *out++ = '\r';
289  *out++ = '\n';
290  }
291  else {
292  *out++ = buf[i];
293  }
294  }
295 
296  *out++ = 0;
297 
298  return buf2;
299 }
virtual void OnInitialUpdate()
Transfers data to and from the controls in the console.
CONST PIXELFORMATDESCRIPTOR UINT
Definition: win_qgl.cpp:47
#define EDIT_HEIGHT
Definition: ConsoleView.cpp:37
int Length(void) const
Definition: Str.h:702
case const int
Definition: Callbacks.cpp:52
idCmdSystem * cmdSystem
Definition: CmdSystem.cpp:116
GLenum GLsizei len
Definition: glext.h:3472
int i
Definition: process.py:33
#define BOOL
Definition: mprintf.c:71
virtual BOOL PreTranslateMessage(MSG *pMsg)
Handles keyboard input to process the "Enter" key to execute commands and command history...
list l
Definition: prepare.py:17
virtual void BufferCommandText(cmdExecution_t exec, const char *text)=0
#define IDC_CONSOLE_EDIT
virtual void DoDataExchange(CDataExchange *pDX)
Transfers data to and from the controls in the console.
GLuint index
Definition: glext.h:3476
bool saveCurrentCommand
Definition: ConsoleView.h:50
idCommon * common
Definition: Common.cpp:206
afx_msg void OnSize(UINT nType, int cx, int cy)
Windows message called when the window is resized.
void Clear(void)
Definition: Str.h:724
idStr & RemoveColors(void)
Definition: Str.h:849
virtual void Printf(const char *fmt,...) id_attribute((format(printf
idStrList consoleHistory
Definition: ConsoleView.h:47
idStr currentCommand
Definition: ConsoleView.h:48
virtual ~ConsoleView()
Destructor for ConsoleView.
Definition: ConsoleView.cpp:56
int Append(const type &obj)
Definition: List.h:646
IMPLEMENT_DYNCREATE(CCamWnd, CWnd)
void ExecuteCommand(const idStr &cmd="")
Executes the specified console command.
const char * TranslateString(const char *buf)
Replaces \n with \r\n for carriage returns in an edit control.
View in the Material Editor that functions as a Doom III console.
Definition: ConsoleView.h:37
int Num(void) const
Definition: List.h:265
bool RemoveIndex(int index)
Definition: List.h:849
void SetConsoleText(const idStr &text)
Replaces the text in the console window with the specified text.
Definition: ConsoleView.cpp:95
CEdit editConsole
Definition: ConsoleView.h:43
Definition: Str.h:116
const char * c_str(void) const
Definition: Str.h:487
#define TRUE
Definition: mprintf.c:69
int currentHistoryPosition
Definition: ConsoleView.h:49
CEdit editInput
Definition: ConsoleView.h:44
#define IDC_CONSOLE_OUTPUT
void AddText(const char *msg)
Adds text to the end of the console output window.
Definition: ConsoleView.cpp:65