Console.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. Close();
  177. Terminal = NULL;
  178. if (FPrevTerminalClose)
  179. {
  180. FPrevTerminalClose(Sender);
  181. }
  182. }
  183. //---------------------------------------------------------------------------
  184. void __fastcall TConsoleDialog::ExecuteButtonClick(TObject * /*Sender*/)
  185. {
  186. // When pressing "Enter" key, focus is not lst and
  187. // the command is not saved (as oppisute to clicking the button by mouse)
  188. CommandEdit->SaveToHistory();
  189. ExecuteCommand();
  190. }
  191. //---------------------------------------------------------------------------
  192. void __fastcall TConsoleDialog::DoExecuteCommand()
  193. {
  194. CommandEdit->SelectAll();
  195. FTerminal->ExceptionOnFail = true;
  196. FClearExceptionOnFail = true;
  197. UnicodeString CurrentDirectory = FTerminal->CurrentDirectory;
  198. try
  199. {
  200. UnicodeString Command = CommandEdit->Text;
  201. OutputMemo->Lines->Add(FORMAT(L"%s$ %s", (CurrentDirectory, Command)));
  202. Configuration->Usage->Inc(L"RemoteCommandExecutions");
  203. TAutoFlag ExecutingFlag(FExecuting);
  204. UpdateControls();
  205. // give a chance for disabled buttons to redraw
  206. Application->ProcessMessages();
  207. FTerminal->AnyCommand(Command, AddLine);
  208. }
  209. __finally
  210. {
  211. if (FTerminal)
  212. {
  213. FTerminal->ExceptionOnFail = false;
  214. DebugAssert(FClearExceptionOnFail);
  215. FClearExceptionOnFail = false;
  216. if (FTerminal->Active)
  217. {
  218. FTerminal->ReadCurrentDirectory();
  219. }
  220. }
  221. UpdateControls();
  222. }
  223. if (CurrentDirectory != FTerminal->CurrentDirectory)
  224. {
  225. FDirectoryChanged = true;
  226. }
  227. }
  228. //---------------------------------------------------------------------------
  229. void __fastcall TConsoleDialog::ExecuteCommand()
  230. {
  231. try
  232. {
  233. DoExecuteCommand();
  234. }
  235. catch(Exception & E)
  236. {
  237. DebugAssert(FLastTerminal != NULL);
  238. // Should use the command session, if there's one, not to close the main session
  239. FLastTerminal->ShowExtendedException(&E);
  240. }
  241. }
  242. //---------------------------------------------------------------------------
  243. void __fastcall TConsoleDialog::CommandEditChange(TObject * /*Sender*/)
  244. {
  245. UpdateControls();
  246. }
  247. //---------------------------------------------------------------------------
  248. void __fastcall TConsoleDialog::AddLine(const UnicodeString & Line, TCaptureOutputType OutputType)
  249. {
  250. if ((OutputType == cotOutput) || (OutputType == cotError))
  251. {
  252. OutputMemo->Lines->Add(Line);
  253. }
  254. }
  255. //---------------------------------------------------------------------------
  256. void __fastcall TConsoleDialog::CreateParams(TCreateParams & Params)
  257. {
  258. TForm::CreateParams(Params);
  259. // we no longer exclude WS_SYSMENU, was there any reason for that, apart from
  260. // hidding the window icon?
  261. }
  262. //---------------------------------------------------------------------------
  263. void __fastcall TConsoleDialog::HelpButtonClick(TObject * /*Sender*/)
  264. {
  265. FormHelp(this);
  266. }
  267. //---------------------------------------------------------------------------
  268. void __fastcall TConsoleDialog::DoAdjustWindow()
  269. {
  270. HGDIOBJ OldFont;
  271. HDC DC;
  272. TTextMetric TM;
  273. TRect Rect;
  274. DC = GetDC(OutputMemo->Handle);
  275. OldFont = SelectObject(DC, OutputMemo->Font->Handle);
  276. try
  277. {
  278. GetTextMetrics(DC, &TM);
  279. OutputMemo->Perform(EM_GETRECT, 0, ((int)&Rect));
  280. }
  281. __finally
  282. {
  283. SelectObject(DC, OldFont);
  284. ReleaseDC(OutputMemo->Handle, DC);
  285. }
  286. int Rows = OutputMemo->Lines->Count;
  287. int Columns = 0;
  288. for (int Index = 0; Index < Rows; Index++)
  289. {
  290. int Len = OutputMemo->Lines->Strings[Index].Length();
  291. if (Columns < Len)
  292. {
  293. Columns = Len;
  294. }
  295. }
  296. // 10 is surplus to cover any borders, etc.
  297. int RequiredWidth = (TM.tmAveCharWidth * Columns) + ScaleByTextHeight(this, 10);
  298. // there is always one line more
  299. int RequiredHeight = (TM.tmHeight + TM.tmExternalLeading) * (Rows + 1) + ScaleByTextHeight(this, 10);
  300. int CurrentWidth = (Rect.Right - Rect.Left);
  301. int CurrentHeight = (Rect.Bottom - Rect.Top);
  302. ResizeForm(this,
  303. Width + (RequiredWidth - CurrentWidth),
  304. Height + (RequiredHeight - CurrentHeight));
  305. FAutoBounds = BoundsRect;
  306. }
  307. //---------------------------------------------------------------------------
  308. void __fastcall TConsoleDialog::ActionListExecute(TBasicAction * Action,
  309. bool & Handled)
  310. {
  311. if (Action == AdjustWindow)
  312. {
  313. DoAdjustWindow();
  314. Handled = true;
  315. }
  316. }
  317. //---------------------------------------------------------------------------
  318. void __fastcall TConsoleDialog::ActionListUpdate(TBasicAction * Action,
  319. bool & Handled)
  320. {
  321. if (Action == AdjustWindow)
  322. {
  323. Handled = true;
  324. }
  325. }
  326. //---------------------------------------------------------------------------
  327. void __fastcall TConsoleDialog::FormShow(TObject * /*Sender*/)
  328. {
  329. UpdateFormPosition(this, poOwnerFormCenter);
  330. RestoreFormSize(CustomWinConfiguration->ConsoleWin.WindowSize, this);
  331. FAutoBounds = BoundsRect;
  332. }
  333. //---------------------------------------------------------------------------
  334. void __fastcall TConsoleDialog::OutputMemoContextPopup(TObject * Sender,
  335. TPoint & MousePos, bool & Handled)
  336. {
  337. MenuPopup(Sender, MousePos, Handled);
  338. }
  339. //---------------------------------------------------------------------------
  340. void __fastcall TConsoleDialog::FormCloseQuery(TObject * /*Sender*/, bool & CanClose)
  341. {
  342. // Probably not necessary as this is called from top-level dialog loop,
  343. // where we do not get until ExecuteButtonClick exists.
  344. CanClose = !FExecuting;
  345. }
  346. //---------------------------------------------------------------------------
  347. void __fastcall TConsoleDialog::Dispatch(void * Message)
  348. {
  349. TMessage * M = reinterpret_cast<TMessage*>(Message);
  350. if (M->Msg == WM_SYSCOMMAND)
  351. {
  352. if (!HandleMinimizeSysCommand(*M))
  353. {
  354. TForm::Dispatch(Message);
  355. }
  356. }
  357. else
  358. {
  359. TForm::Dispatch(Message);
  360. }
  361. }
  362. //---------------------------------------------------------------------------