Console.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. // If ever these events are used elsewhere, use TMulticastEvent,
  95. // to avoid problems when the connnection is lost while the dialog is opened.
  96. DebugAssert(FTerminal->OnChangeDirectory == NULL);
  97. DebugAssert(FTerminal->OnClose == NULL);
  98. FOldChangeDirectory = FTerminal->OnChangeDirectory;
  99. FTerminal->OnChangeDirectory = DoChangeDirectory;
  100. // avoid reloading directory after each change of current directory from console
  101. FTerminal->BeginTransaction();
  102. FLastTerminal = FTerminal;
  103. FPrevTerminalClose = FTerminal->OnClose;
  104. // used instead of previous TTerminalManager::OnChangeTerminal
  105. FTerminal->OnClose = TerminalClose;
  106. }
  107. UpdateControls();
  108. }
  109. }
  110. //---------------------------------------------------------------------
  111. void __fastcall TConsoleDialog::DoChangeDirectory(TObject * Sender)
  112. {
  113. if (FOldChangeDirectory) FOldChangeDirectory(Sender);
  114. UpdateControls();
  115. }
  116. //---------------------------------------------------------------------
  117. void __fastcall TConsoleDialog::UpdateControls()
  118. {
  119. DirectoryLabel->Caption = (FTerminal ? FTerminal->CurrentDirectory : UnicodeString());
  120. // Disabling buttons while executing to prevent recursive execution,
  121. // now that message queue is processed while command is executing.
  122. EnableControl(ExecuteButton,
  123. ((FTerminal != NULL) ? FTerminal->AllowedAnyCommand(CommandEdit->Text) : false) &&
  124. !FExecuting);
  125. EnableControl(CancelBtn, !FExecuting);
  126. }
  127. //---------------------------------------------------------------------
  128. bool __fastcall TConsoleDialog::Execute(const UnicodeString Command,
  129. const TStrings * Log)
  130. {
  131. try
  132. {
  133. CommandEdit->Items = CustomWinConfiguration->History[L"Commands"];
  134. if (Log != NULL)
  135. {
  136. OutputMemo->Lines->BeginUpdate();
  137. try
  138. {
  139. TStrings * ALog = const_cast<TStrings *>(Log);
  140. for (int i = 0; i < ALog->Count; i++)
  141. {
  142. AddLine(ALog->Strings[i], cotOutput);
  143. }
  144. }
  145. __finally
  146. {
  147. OutputMemo->Lines->EndUpdate();
  148. }
  149. }
  150. if (!Command.IsEmpty())
  151. {
  152. CommandEdit->Text = Command;
  153. DoExecuteCommand();
  154. }
  155. UpdateControls();
  156. ShowModal();
  157. TConsoleWinConfiguration ConsoleWin = CustomWinConfiguration->ConsoleWin;
  158. if ((FAutoBounds.Width() != Width) ||
  159. (FAutoBounds.Height() != Height))
  160. {
  161. ConsoleWin.WindowSize = StoreFormSize(this);
  162. }
  163. CustomWinConfiguration->ConsoleWin = ConsoleWin;
  164. }
  165. __finally
  166. {
  167. if (FTerminal)
  168. {
  169. CommandEdit->SaveToHistory();
  170. CustomWinConfiguration->History[L"Commands"] = CommandEdit->Items;
  171. }
  172. }
  173. return true;
  174. }
  175. //---------------------------------------------------------------------------
  176. void __fastcall TConsoleDialog::TerminalClose(TObject * Sender)
  177. {
  178. // Not deassociating terminal here, leaving it to the destructor.
  179. // Because at this point, we could be in the midddle of a reconnect and the event handlers can be set to TTerminalThread.
  180. Close();
  181. if (FPrevTerminalClose)
  182. {
  183. FPrevTerminalClose(Sender);
  184. }
  185. }
  186. //---------------------------------------------------------------------------
  187. void __fastcall TConsoleDialog::ExecuteButtonClick(TObject * /*Sender*/)
  188. {
  189. // When pressing "Enter" key, focus is not lst and
  190. // the command is not saved (as oppisute to clicking the button by mouse)
  191. CommandEdit->SaveToHistory();
  192. ExecuteCommand();
  193. }
  194. //---------------------------------------------------------------------------
  195. void __fastcall TConsoleDialog::DoExecuteCommand()
  196. {
  197. CommandEdit->SelectAll();
  198. FTerminal->ExceptionOnFail = true;
  199. FClearExceptionOnFail = true;
  200. UnicodeString CurrentDirectory = FTerminal->CurrentDirectory;
  201. try
  202. {
  203. UnicodeString Command = CommandEdit->Text;
  204. OutputMemo->Lines->Add(FORMAT(L"%s$ %s", (CurrentDirectory, Command)));
  205. Configuration->Usage->Inc(L"RemoteCommandExecutions");
  206. TAutoFlag ExecutingFlag(FExecuting);
  207. UpdateControls();
  208. // give a chance for disabled buttons to redraw
  209. Application->ProcessMessages();
  210. FTerminal->AnyCommand(Command, AddLine);
  211. }
  212. __finally
  213. {
  214. if (FTerminal)
  215. {
  216. FTerminal->ExceptionOnFail = false;
  217. DebugAssert(FClearExceptionOnFail);
  218. FClearExceptionOnFail = false;
  219. if (FTerminal->Active)
  220. {
  221. FTerminal->ReadCurrentDirectory();
  222. }
  223. }
  224. UpdateControls();
  225. }
  226. if (CurrentDirectory != FTerminal->CurrentDirectory)
  227. {
  228. FDirectoryChanged = true;
  229. }
  230. }
  231. //---------------------------------------------------------------------------
  232. void __fastcall TConsoleDialog::ExecuteCommand()
  233. {
  234. try
  235. {
  236. DoExecuteCommand();
  237. }
  238. catch(Exception & E)
  239. {
  240. DebugAssert(FLastTerminal != NULL);
  241. // Should use the command session, if there's one, not to close the main session
  242. FLastTerminal->ShowExtendedException(&E);
  243. }
  244. }
  245. //---------------------------------------------------------------------------
  246. void __fastcall TConsoleDialog::CommandEditChange(TObject * /*Sender*/)
  247. {
  248. UpdateControls();
  249. }
  250. //---------------------------------------------------------------------------
  251. void __fastcall TConsoleDialog::AddLine(const UnicodeString & Line, TCaptureOutputType OutputType)
  252. {
  253. if ((OutputType == cotOutput) || (OutputType == cotError))
  254. {
  255. OutputMemo->Lines->Add(Line);
  256. }
  257. }
  258. //---------------------------------------------------------------------------
  259. void __fastcall TConsoleDialog::CreateParams(TCreateParams & Params)
  260. {
  261. TForm::CreateParams(Params);
  262. // we no longer exclude WS_SYSMENU, was there any reason for that, apart from
  263. // hidding the window icon?
  264. }
  265. //---------------------------------------------------------------------------
  266. void __fastcall TConsoleDialog::HelpButtonClick(TObject * /*Sender*/)
  267. {
  268. FormHelp(this);
  269. }
  270. //---------------------------------------------------------------------------
  271. void __fastcall TConsoleDialog::DoAdjustWindow()
  272. {
  273. HGDIOBJ OldFont;
  274. HDC DC;
  275. TTextMetric TM;
  276. TRect Rect;
  277. DC = GetDC(OutputMemo->Handle);
  278. OldFont = SelectObject(DC, OutputMemo->Font->Handle);
  279. try
  280. {
  281. GetTextMetrics(DC, &TM);
  282. OutputMemo->Perform(EM_GETRECT, 0, ((int)&Rect));
  283. }
  284. __finally
  285. {
  286. SelectObject(DC, OldFont);
  287. ReleaseDC(OutputMemo->Handle, DC);
  288. }
  289. int Rows = OutputMemo->Lines->Count;
  290. int Columns = 0;
  291. for (int Index = 0; Index < Rows; Index++)
  292. {
  293. int Len = OutputMemo->Lines->Strings[Index].Length();
  294. if (Columns < Len)
  295. {
  296. Columns = Len;
  297. }
  298. }
  299. // 10 is surplus to cover any borders, etc.
  300. int RequiredWidth = (TM.tmAveCharWidth * Columns) + ScaleByTextHeight(this, 10);
  301. // there is always one line more
  302. int RequiredHeight = (TM.tmHeight + TM.tmExternalLeading) * (Rows + 1) + ScaleByTextHeight(this, 10);
  303. int CurrentWidth = (Rect.Right - Rect.Left);
  304. int CurrentHeight = (Rect.Bottom - Rect.Top);
  305. ResizeForm(this,
  306. Width + (RequiredWidth - CurrentWidth),
  307. Height + (RequiredHeight - CurrentHeight));
  308. FAutoBounds = BoundsRect;
  309. }
  310. //---------------------------------------------------------------------------
  311. void __fastcall TConsoleDialog::ActionListExecute(TBasicAction * Action,
  312. bool & Handled)
  313. {
  314. if (Action == AdjustWindow)
  315. {
  316. DoAdjustWindow();
  317. Handled = true;
  318. }
  319. }
  320. //---------------------------------------------------------------------------
  321. void __fastcall TConsoleDialog::ActionListUpdate(TBasicAction * Action,
  322. bool & Handled)
  323. {
  324. if (Action == AdjustWindow)
  325. {
  326. Handled = true;
  327. }
  328. }
  329. //---------------------------------------------------------------------------
  330. void __fastcall TConsoleDialog::FormShow(TObject * /*Sender*/)
  331. {
  332. UpdateFormPosition(this, poOwnerFormCenter);
  333. RestoreFormSize(CustomWinConfiguration->ConsoleWin.WindowSize, this);
  334. FAutoBounds = BoundsRect;
  335. }
  336. //---------------------------------------------------------------------------
  337. void __fastcall TConsoleDialog::OutputMemoContextPopup(TObject * Sender,
  338. TPoint & MousePos, bool & Handled)
  339. {
  340. MenuPopup(Sender, MousePos, Handled);
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TConsoleDialog::FormCloseQuery(TObject * /*Sender*/, bool & CanClose)
  344. {
  345. // Probably not necessary as this is called from top-level dialog loop,
  346. // where we do not get until ExecuteButtonClick exists.
  347. CanClose = !FExecuting;
  348. }
  349. //---------------------------------------------------------------------------
  350. void __fastcall TConsoleDialog::Dispatch(void * Message)
  351. {
  352. TMessage * M = reinterpret_cast<TMessage*>(Message);
  353. if (M->Msg == WM_SYSCOMMAND)
  354. {
  355. if (!HandleMinimizeSysCommand(*M))
  356. {
  357. TForm::Dispatch(Message);
  358. }
  359. }
  360. else
  361. {
  362. TForm::Dispatch(Message);
  363. }
  364. }
  365. //---------------------------------------------------------------------------