Console.cpp 8.9 KB

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