Console.cpp 9.1 KB

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