Console.cpp 9.1 KB

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