Console.cpp 11 KB

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