Console.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <TextsWin.h>
  6. #include <Interface.h>
  7. #include <CoreMain.h>
  8. #include <VCLCommon.h>
  9. #include <CustomWinConfiguration.h>
  10. #include "Console.h"
  11. #include <Tools.h>
  12. #include <PasTools.hpp>
  13. //---------------------------------------------------------------------
  14. #pragma link "HistoryComboBox"
  15. #pragma link "PathLabel"
  16. #pragma link "PngImageList"
  17. #ifndef NO_RESOURCES
  18. #pragma resource "*.dfm"
  19. #endif
  20. //---------------------------------------------------------------------
  21. void __fastcall DoConsoleDialog(TTerminal * Terminal, const UnicodeString Command,
  22. const TStrings * Log)
  23. {
  24. TConsoleDialog * Dialog = new TConsoleDialog(Application);
  25. try
  26. {
  27. Dialog->Terminal = Terminal;
  28. Dialog->Execute(Command, Log);
  29. }
  30. __finally
  31. {
  32. delete Dialog;
  33. }
  34. }
  35. //---------------------------------------------------------------------
  36. __fastcall TConsoleDialog::TConsoleDialog(TComponent* AOwner)
  37. : TForm(AOwner)
  38. {
  39. FTerminal = NULL;
  40. FClearExceptionOnFail = false;
  41. FOldChangeDirectory = NULL;
  42. FPrevTerminalClose = NULL;;
  43. FLastTerminal = NULL;
  44. FDirectoryChanged = false;
  45. OutputMemo->Color = clBlack;
  46. OutputMemo->Font->Color = (TColor)0x00BBBBBB;
  47. FixComboBoxResizeBug(CommandEdit);
  48. UseSystemSettings(this);
  49. OutputMemo->Font->Name = CustomWinConfiguration->DefaultFixedWidthFontName;
  50. OutputMemo->Font->Size = CustomWinConfiguration->DefaultFixedWidthFontSize;
  51. }
  52. //---------------------------------------------------------------------
  53. __fastcall TConsoleDialog::~TConsoleDialog()
  54. {
  55. Terminal = NULL;
  56. }
  57. //---------------------------------------------------------------------
  58. void __fastcall TConsoleDialog::SetTerminal(TTerminal * value)
  59. {
  60. if (FTerminal != value)
  61. {
  62. if (FTerminal)
  63. {
  64. if (FClearExceptionOnFail)
  65. {
  66. FTerminal->ExceptionOnFail = false;
  67. FClearExceptionOnFail = false;
  68. }
  69. assert(FTerminal->OnClose == TerminalClose);
  70. FTerminal->OnClose = FPrevTerminalClose;
  71. assert(FTerminal->OnChangeDirectory == DoChangeDirectory);
  72. FTerminal->OnChangeDirectory = FOldChangeDirectory;
  73. FOldChangeDirectory = NULL;
  74. if (FDirectoryChanged)
  75. {
  76. FDirectoryChanged = false;
  77. if (FTerminal->Active)
  78. {
  79. // directory would be read from EndTransaction anyway,
  80. // but with reload only flag set, what prevents
  81. // recording path in history, what we want if the path was
  82. // changed by "cd" command in console
  83. FTerminal->ReadDirectory(false);
  84. }
  85. }
  86. FTerminal->EndTransaction();
  87. }
  88. FTerminal = value;
  89. if (FTerminal)
  90. {
  91. OutputMemo->Clear();
  92. FOldChangeDirectory = FTerminal->OnChangeDirectory;
  93. FTerminal->OnChangeDirectory = DoChangeDirectory;
  94. // avoid reloading directory after each change of current directory from console
  95. FTerminal->BeginTransaction();
  96. FLastTerminal = FTerminal;
  97. FPrevTerminalClose = FTerminal->OnClose;
  98. // used instead of previous TTerminalManager::OnChangeTerminal
  99. FTerminal->OnClose = TerminalClose;
  100. }
  101. UpdateControls();
  102. }
  103. }
  104. //---------------------------------------------------------------------
  105. void __fastcall TConsoleDialog::DoChangeDirectory(TObject * Sender)
  106. {
  107. if (FOldChangeDirectory) FOldChangeDirectory(Sender);
  108. UpdateControls();
  109. }
  110. //---------------------------------------------------------------------
  111. void __fastcall TConsoleDialog::UpdateControls()
  112. {
  113. DirectoryLabel->Caption = (FTerminal ? FTerminal->CurrentDirectory : UnicodeString());
  114. EnableControl(ExecuteButton,
  115. (FTerminal != NULL) ? FTerminal->AllowedAnyCommand(CommandEdit->Text) : false);
  116. }
  117. //---------------------------------------------------------------------
  118. bool __fastcall TConsoleDialog::Execute(const UnicodeString Command,
  119. const TStrings * Log)
  120. {
  121. try
  122. {
  123. CommandEdit->Items = CustomWinConfiguration->History[L"Commands"];
  124. if (Log != NULL)
  125. {
  126. OutputMemo->Lines->BeginUpdate();
  127. try
  128. {
  129. TStrings * ALog = const_cast<TStrings *>(Log);
  130. for (int i = 0; i < ALog->Count; i++)
  131. {
  132. AddLine(ALog->Strings[i], cotOutput);
  133. }
  134. }
  135. __finally
  136. {
  137. OutputMemo->Lines->EndUpdate();
  138. }
  139. }
  140. if (!Command.IsEmpty())
  141. {
  142. CommandEdit->Text = Command;
  143. DoExecuteCommand();
  144. }
  145. UpdateControls();
  146. ShowModal();
  147. TConsoleWinConfiguration ConsoleWin = CustomWinConfiguration->ConsoleWin;
  148. if ((FAutoBounds.Width() != Width) ||
  149. (FAutoBounds.Height() != Height))
  150. {
  151. ConsoleWin.WindowSize = StoreFormSize(this);
  152. }
  153. CustomWinConfiguration->ConsoleWin = ConsoleWin;
  154. }
  155. __finally
  156. {
  157. if (FTerminal)
  158. {
  159. CommandEdit->SaveToHistory();
  160. CustomWinConfiguration->History[L"Commands"] = CommandEdit->Items;
  161. }
  162. }
  163. return true;
  164. }
  165. //---------------------------------------------------------------------------
  166. void __fastcall TConsoleDialog::TerminalClose(TObject * Sender)
  167. {
  168. Close();
  169. Terminal = NULL;
  170. if (FPrevTerminalClose)
  171. {
  172. FPrevTerminalClose(Sender);
  173. }
  174. }
  175. //---------------------------------------------------------------------------
  176. void __fastcall TConsoleDialog::ExecuteButtonClick(TObject * /*Sender*/)
  177. {
  178. // When pressing "Enter" key, focus is not lst and
  179. // the command is not saved (as oppisute to clicking the button by mouse)
  180. CommandEdit->SaveToHistory();
  181. ExecuteCommand();
  182. }
  183. //---------------------------------------------------------------------------
  184. void __fastcall TConsoleDialog::DoExecuteCommand()
  185. {
  186. CommandEdit->SelectAll();
  187. FTerminal->ExceptionOnFail = true;
  188. FClearExceptionOnFail = true;
  189. UnicodeString CurrentDirectory = FTerminal->CurrentDirectory;
  190. try
  191. {
  192. UnicodeString Command = CommandEdit->Text;
  193. OutputMemo->Lines->Add(FORMAT(L"%s$ %s", (CurrentDirectory, Command)));
  194. Configuration->Usage->Inc(L"RemoteCommandExecutions");
  195. FTerminal->AnyCommand(Command, AddLine);
  196. }
  197. __finally
  198. {
  199. if (FTerminal)
  200. {
  201. FTerminal->ExceptionOnFail = false;
  202. assert(FClearExceptionOnFail);
  203. FClearExceptionOnFail = false;
  204. if (FTerminal->Active)
  205. {
  206. FTerminal->ReadCurrentDirectory();
  207. }
  208. }
  209. }
  210. if (CurrentDirectory != FTerminal->CurrentDirectory)
  211. {
  212. FDirectoryChanged = true;
  213. }
  214. }
  215. //---------------------------------------------------------------------------
  216. void __fastcall TConsoleDialog::ExecuteCommand()
  217. {
  218. try
  219. {
  220. DoExecuteCommand();
  221. }
  222. catch(Exception & E)
  223. {
  224. assert(FLastTerminal != NULL);
  225. FLastTerminal->ShowExtendedException(&E);
  226. }
  227. }
  228. //---------------------------------------------------------------------------
  229. void __fastcall TConsoleDialog::CommandEditChange(TObject * /*Sender*/)
  230. {
  231. UpdateControls();
  232. }
  233. //---------------------------------------------------------------------------
  234. void __fastcall TConsoleDialog::AddLine(const UnicodeString & Line, TCaptureOutputType OutputType)
  235. {
  236. if ((OutputType == cotOutput) || (OutputType == cotError))
  237. {
  238. OutputMemo->Lines->Add(Line);
  239. }
  240. }
  241. //---------------------------------------------------------------------------
  242. void __fastcall TConsoleDialog::CreateParams(TCreateParams & Params)
  243. {
  244. TForm::CreateParams(Params);
  245. // we no longer exclude WS_SYSMENU, was there any reason for that, apart from
  246. // hidding the window icon?
  247. }
  248. //---------------------------------------------------------------------------
  249. void __fastcall TConsoleDialog::HelpButtonClick(TObject * /*Sender*/)
  250. {
  251. FormHelp(this);
  252. }
  253. //---------------------------------------------------------------------------
  254. void __fastcall TConsoleDialog::DoAdjustWindow()
  255. {
  256. HGDIOBJ OldFont;
  257. HDC DC;
  258. TTextMetric TM;
  259. TRect Rect;
  260. DC = GetDC(OutputMemo->Handle);
  261. OldFont = SelectObject(DC, OutputMemo->Font->Handle);
  262. try
  263. {
  264. GetTextMetrics(DC, &TM);
  265. OutputMemo->Perform(EM_GETRECT, 0, ((int)&Rect));
  266. }
  267. __finally
  268. {
  269. SelectObject(DC, OldFont);
  270. ReleaseDC(OutputMemo->Handle, DC);
  271. }
  272. int Rows = OutputMemo->Lines->Count;
  273. int Columns = 0;
  274. for (int Index = 0; Index < Rows; Index++)
  275. {
  276. int Len = OutputMemo->Lines->Strings[Index].Length();
  277. if (Columns < Len)
  278. {
  279. Columns = Len;
  280. }
  281. }
  282. // 10 is surplus to cover any borders, etc.
  283. int RequiredWidth = (TM.tmAveCharWidth * Columns) + ScaleByTextHeight(this, 10);
  284. // there is always one line more
  285. int RequiredHeight = (TM.tmHeight + TM.tmExternalLeading) * (Rows + 1) + ScaleByTextHeight(this, 10);
  286. int CurrentWidth = (Rect.Right - Rect.Left);
  287. int CurrentHeight = (Rect.Bottom - Rect.Top);
  288. ResizeForm(this,
  289. Width + (RequiredWidth - CurrentWidth),
  290. Height + (RequiredHeight - CurrentHeight));
  291. FAutoBounds = BoundsRect;
  292. }
  293. //---------------------------------------------------------------------------
  294. void __fastcall TConsoleDialog::ActionListExecute(TBasicAction * Action,
  295. bool & Handled)
  296. {
  297. if (Action == AdjustWindow)
  298. {
  299. DoAdjustWindow();
  300. Handled = true;
  301. }
  302. }
  303. //---------------------------------------------------------------------------
  304. void __fastcall TConsoleDialog::ActionListUpdate(TBasicAction * Action,
  305. bool & Handled)
  306. {
  307. if (Action == AdjustWindow)
  308. {
  309. Handled = true;
  310. }
  311. }
  312. //---------------------------------------------------------------------------
  313. void __fastcall TConsoleDialog::FormShow(TObject * /*Sender*/)
  314. {
  315. UpdateFormPosition(this, poOwnerFormCenter);
  316. RestoreFormSize(CustomWinConfiguration->ConsoleWin.WindowSize, this);
  317. FAutoBounds = BoundsRect;
  318. }
  319. //---------------------------------------------------------------------------
  320. void __fastcall TConsoleDialog::OutputMemoContextPopup(TObject * Sender,
  321. TPoint & MousePos, bool & Handled)
  322. {
  323. MenuPopup(Sender, MousePos, Handled);
  324. }
  325. //---------------------------------------------------------------------------