Console.cpp 12 KB

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