Console.cpp 9.3 KB

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