1
0

Console.cpp 11 KB

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