2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
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.
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.
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/>.
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.
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.
26 ===========================================================================
29 #include "../../idlib/precompiled.h"
34 #include "ConsoleDlg.h"
39 IMPLEMENT_DYNCREATE(CConsoleDlg, CDialog)
40 CConsoleDlg::CConsoleDlg(CWnd* pParent /*=NULL*/)
41 : CDialog(CConsoleDlg::IDD)
43 currentHistoryPosition = -1;
45 saveCurrentCommand = true;
48 CConsoleDlg::~CConsoleDlg()
52 void CConsoleDlg::DoDataExchange(CDataExchange* pDX)
54 CDialog::DoDataExchange(pDX);
55 DDX_Control(pDX, IDC_EDIT_CONSOLE, editConsole);
56 DDX_Control(pDX, IDC_EDIT_INPUT, editInput);
59 void CConsoleDlg::AddText( const char *msg ) {
65 work = CEntityDlg::TranslateString( work.c_str() );
66 editConsole.GetWindowText( work2 );
67 int len = work2.GetLength();
68 if ( len + work.Length() > (int)editConsole.GetLimitText() ) {
69 work2 = work2.Right( editConsole.GetLimitText() * 0.75 );
70 len = work2.GetLength();
71 editConsole.SetWindowText(work2);
73 editConsole.SetSel( len, len );
74 editConsole.ReplaceSel( work );
78 BEGIN_MESSAGE_MAP(CConsoleDlg, CDialog)
85 // CConsoleDlg message handlers
87 void CConsoleDlg::OnSize(UINT nType, int cx, int cy)
89 CDialog::OnSize(nType, cx, cy);
91 if (editInput.GetSafeHwnd() == NULL) {
97 editInput.GetWindowRect(crect);
99 editInput.SetWindowPos(NULL, 4, rect.Height() - 4 - crect.Height(), rect.Width() - 8, crect.Height(), SWP_SHOWWINDOW);
100 editConsole.SetWindowPos(NULL, 4, 4, rect.Width() - 8, rect.Height() - crect.Height() - 8, SWP_SHOWWINDOW);
103 BOOL CConsoleDlg::PreTranslateMessage(MSG* pMsg)
106 if (pMsg->hwnd == editInput.GetSafeHwnd()) {
107 if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE ) {
109 g_pParentWnd->SetFocus ();
113 if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN ) {
118 if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE ) {
119 if (pMsg->wParam == VK_ESCAPE) {
120 g_pParentWnd->GetCamera()->SetFocus();
127 if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_UP ) {
128 //save off the current in-progress command so we can get back to it
129 if ( saveCurrentCommand == true ) {
131 editInput.GetWindowText ( str );
132 currentCommand = str.GetBuffer ( 0 );
133 saveCurrentCommand = false;
136 if ( consoleHistory.Num () > 0 ) {
137 editInput.SetWindowText ( consoleHistory[currentHistoryPosition] );
139 int selLocation = consoleHistory[currentHistoryPosition].Length ();
140 editInput.SetSel ( selLocation , selLocation + 1);
143 if ( currentHistoryPosition > 0) {
144 --currentHistoryPosition;
150 if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN ) {
152 if ( currentHistoryPosition < consoleHistory.Num () - 1 ) {
153 ++currentHistoryPosition;
154 editInput.SetWindowText ( consoleHistory[currentHistoryPosition] );
155 selLocation = consoleHistory[currentHistoryPosition].Length ();
158 editInput.SetWindowText ( currentCommand );
159 selLocation = currentCommand.Length ();
160 currentCommand.Clear ();
161 saveCurrentCommand = true;
164 editInput.SetSel ( selLocation , selLocation + 1);
168 if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_TAB ) {
169 common->Printf ( "Command History\n----------------\n" );
170 for ( int i = 0 ; i < consoleHistory.Num ();i++ )
172 common->Printf ( "[cmd %d]: %s\n" , i , consoleHistory[i].c_str() );
174 common->Printf ( "----------------\n" );
176 if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_NEXT) {
177 editConsole.LineScroll ( 10 );
180 if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_PRIOR ) {
181 editConsole.LineScroll ( -10 );
184 if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_HOME ) {
185 editConsole.LineScroll ( -editConsole.GetLineCount() );
188 if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_END ) {
189 editConsole.LineScroll ( editConsole.GetLineCount() );
193 return CDialog::PreTranslateMessage(pMsg);
196 void CConsoleDlg::OnSetFocus(CWnd* pOldWnd) {
197 CDialog::OnSetFocus(pOldWnd);
198 editInput.SetFocus();
201 void CConsoleDlg::SetConsoleText ( const idStr& text ) {
203 editInput.SetWindowText ( text.c_str() );
206 void CConsoleDlg::ExecuteCommand ( const idStr& cmd ) {
208 if ( cmd.Length() > 0 ) {
212 editInput.GetWindowText(str);
216 editInput.SetWindowText("");
217 common->Printf("%s\n", str.GetBuffer(0));
219 //avoid adding multiple identical commands in a row
220 int index = consoleHistory.Num ();
222 if ( index == 0 || str.GetBuffer(0) != consoleHistory[index-1]) {
223 //keep the history to 16 commands, removing the oldest command
224 if ( consoleHistory.Num () > 16 ) {
225 consoleHistory.RemoveIndex ( 0 );
227 currentHistoryPosition = consoleHistory.Append ( str.GetBuffer (0) );
230 currentHistoryPosition = consoleHistory.Num () - 1;
233 currentCommand.Clear ();
235 bool propogateCommand = true;
237 //process some of our own special commands
238 if ( str.CompareNoCase ( "clear" ) == 0) {
239 editConsole.SetSel ( 0 , -1 );
240 editConsole.Clear ();
242 else if ( str.CompareNoCase ( "edit" ) == 0) {
243 propogateCommand = false;
245 if ( propogateCommand ) {
246 cmdSystem->BufferCommandText( CMD_EXEC_NOW, str );
249 Sys_UpdateWindows(W_ALL);
253 void CConsoleDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
255 CDialog::OnActivate(nState, pWndOther, bMinimized);
257 if ( nState == WA_ACTIVE || nState == WA_CLICKACTIVE )
259 editInput.SetFocus();