EditConsole.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // EditConsole.cpp : implementation file
  13. //
  14. #include "stdafx.h"
  15. #ifdef _DEBUG
  16. #undef new
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CEditConsole
  23. CEditConsole::CEditConsole()
  24. {
  25. }
  26. CEditConsole::~CEditConsole()
  27. {
  28. }
  29. BEGIN_MESSAGE_MAP(CEditConsole, CEdit)
  30. //{{AFX_MSG_MAP(CEditConsole)
  31. //}}AFX_MSG_MAP
  32. END_MESSAGE_MAP()
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CEditConsole message handlers
  35. void CEditConsole::SetTextFromConsole(void)
  36. {
  37. INDEX ctLines = _pConsole->con_ctLines;
  38. INDEX ctChars = _pConsole->con_ctLines;
  39. // allocate new string with double line ends
  40. char *strNew = (char*)AllocMemory(ctLines*(ctChars+2)+1);
  41. const char *strString = _pConsole->GetBuffer();
  42. char *pch = strNew;
  43. // convert '\n' to '\r''\n'
  44. while(*strString!=0) {
  45. if (*strString=='\n') {
  46. *pch++='\r';
  47. *pch++='\n';
  48. strString++;
  49. }
  50. *pch++ = *strString++;
  51. }
  52. *pch = 0;
  53. CDlgConsole *pdlgParent = (CDlgConsole *) GetParent();
  54. CEdit *pwndOutput = (CEdit *) pdlgParent->GetDlgItem( IDC_CONSOLE_OUTPUT);
  55. pwndOutput->SetWindowText( CString(strNew));
  56. pwndOutput->LineScroll(ctLines-1);
  57. FreeMemory(strNew);
  58. }
  59. BOOL CEditConsole::PreTranslateMessage(MSG* pMsg)
  60. {
  61. BOOL bCtrl = (GetKeyState( VK_CONTROL) & 128) != 0;
  62. if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN)
  63. {
  64. // obtain current line index
  65. INDEX iCurrentLine = LineFromChar(-1);
  66. INDEX ctLinesEdited = GetLineCount();
  67. // obtain char offset of current line in whole edit string
  68. INDEX iCharOffset = LineFromChar(iCurrentLine);
  69. if( !bCtrl && (iCharOffset != -1) )
  70. {
  71. // extract string to execute
  72. wchar_t achrToExecute[ 1024];
  73. INDEX ctLetters = GetLine( iCurrentLine, achrToExecute, 1023);
  74. // set EOF delimiter
  75. achrToExecute[ ctLetters] = 0;
  76. CTString strToExecute = CStringA(achrToExecute);
  77. CPrintF( ">%s\n", strToExecute);
  78. if( ((const char*)strToExecute)[strlen(strToExecute)-1] != ';')
  79. {
  80. strToExecute += ";";
  81. }
  82. _pShell->Execute(strToExecute);
  83. // set new text for output window
  84. SetTextFromConsole();
  85. // remember input text into console input buffer
  86. CString sHistory;
  87. GetWindowText(sHistory);
  88. _pGame->gam_strConsoleInputBuffer = CStringA(sHistory);
  89. }
  90. // if Ctrl is not pressed and current line is not last line, "swallow return"
  91. if( !bCtrl && (ctLinesEdited-1 != iCurrentLine) )
  92. {
  93. return TRUE;
  94. }
  95. }
  96. return CEdit::PreTranslateMessage(pMsg);
  97. }