Console.cpp 9.7 KB

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