Console.cpp 11 KB

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