Console.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. FixComboBoxResizeBug(CommandEdit);
  46. UseSystemSettings(this);
  47. try
  48. {
  49. OutputMemo->Font->Name = "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 : AnsiString());
  117. EnableControl(ExecuteButton,
  118. (FTerminal != NULL) ? FTerminal->AllowedAnyCommand(CommandEdit->Text) : false);
  119. }
  120. //---------------------------------------------------------------------
  121. bool __fastcall TConsoleDialog::Execute(const AnsiString Command,
  122. const TStrings * Log)
  123. {
  124. try
  125. {
  126. CommandEdit->Items = CustomWinConfiguration->History["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["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. ExecuteCommand();
  182. }
  183. //---------------------------------------------------------------------------
  184. void __fastcall TConsoleDialog::DoExecuteCommand()
  185. {
  186. CommandEdit->SelectAll();
  187. FTerminal->ExceptionOnFail = true;
  188. FClearExceptionOnFail = true;
  189. try
  190. {
  191. AnsiString Command = CommandEdit->Text;
  192. OutputMemo->Lines->Add(FORMAT("%s$ %s", (FTerminal->CurrentDirectory, Command)));
  193. FAnyCommandExecuted = true;
  194. FTerminal->AnyCommand(Command, AddLine);
  195. }
  196. __finally
  197. {
  198. if (FTerminal)
  199. {
  200. FTerminal->ExceptionOnFail = false;
  201. assert(FClearExceptionOnFail);
  202. FClearExceptionOnFail = false;
  203. if (FTerminal->Active)
  204. {
  205. FTerminal->ReadCurrentDirectory();
  206. }
  207. }
  208. }
  209. }
  210. //---------------------------------------------------------------------------
  211. void __fastcall TConsoleDialog::ExecuteCommand()
  212. {
  213. try
  214. {
  215. DoExecuteCommand();
  216. }
  217. catch(Exception & E)
  218. {
  219. assert(FLastTerminal != NULL);
  220. FLastTerminal->ShowExtendedException(&E);
  221. }
  222. }
  223. //---------------------------------------------------------------------------
  224. void __fastcall TConsoleDialog::CommandEditChange(TObject * /*Sender*/)
  225. {
  226. UpdateControls();
  227. }
  228. //---------------------------------------------------------------------------
  229. void __fastcall TConsoleDialog::AddLine(const AnsiString & Line, bool /*StdError*/)
  230. {
  231. OutputMemo->Lines->Add(Line);
  232. }
  233. //---------------------------------------------------------------------------
  234. void __fastcall TConsoleDialog::CreateParams(TCreateParams & Params)
  235. {
  236. TForm::CreateParams(Params);
  237. // we no longer exclude WS_SYSMENU, was there any reason for that, apart from
  238. // hidding the window icon?
  239. }
  240. //---------------------------------------------------------------------------
  241. void __fastcall TConsoleDialog::HelpButtonClick(TObject * /*Sender*/)
  242. {
  243. FormHelp(this);
  244. }
  245. //---------------------------------------------------------------------------
  246. void __fastcall TConsoleDialog::DoAdjustWindow()
  247. {
  248. HFONT OldFont;
  249. void * DC;
  250. TTextMetric TM;
  251. TRect Rect;
  252. DC = GetDC(OutputMemo->Handle);
  253. OldFont = SelectObject(DC, OutputMemo->Font->Handle);
  254. try
  255. {
  256. GetTextMetrics(DC, &TM);
  257. OutputMemo->Perform(EM_GETRECT, 0, ((int)&Rect));
  258. }
  259. __finally
  260. {
  261. SelectObject(DC, OldFont);
  262. ReleaseDC(OutputMemo->Handle, DC);
  263. }
  264. int Rows = OutputMemo->Lines->Count;
  265. int Columns = 0;
  266. for (int Index = 0; Index < Rows; Index++)
  267. {
  268. int Len = OutputMemo->Lines->Strings[Index].Length();
  269. if (Columns < Len)
  270. {
  271. Columns = Len;
  272. }
  273. }
  274. // 10 is surplus to cover any borders, etc.
  275. int RequiredWidth = (TM.tmAveCharWidth * Columns) + 10;
  276. // thre is always one line more
  277. int RequiredHeight = (TM.tmHeight + TM.tmExternalLeading) * (Rows + 1) + 10;
  278. int CurrentWidth = (Rect.Right - Rect.Left);
  279. int CurrentHeight = (Rect.Bottom - Rect.Top);
  280. ResizeForm(this,
  281. Width + (RequiredWidth - CurrentWidth),
  282. Height + (RequiredHeight - CurrentHeight));
  283. FAutoBounds = BoundsRect;
  284. }
  285. //---------------------------------------------------------------------------
  286. void __fastcall TConsoleDialog::ActionListExecute(TBasicAction * Action,
  287. bool & Handled)
  288. {
  289. if (Action == AdjustWindow)
  290. {
  291. DoAdjustWindow();
  292. Handled = true;
  293. }
  294. }
  295. //---------------------------------------------------------------------------
  296. void __fastcall TConsoleDialog::ActionListUpdate(TBasicAction * Action,
  297. bool & Handled)
  298. {
  299. if (Action == AdjustWindow)
  300. {
  301. Handled = true;
  302. }
  303. }
  304. //---------------------------------------------------------------------------
  305. void __fastcall TConsoleDialog::FormShow(TObject * /*Sender*/)
  306. {
  307. UpdateFormPosition(this, poMainFormCenter);
  308. RestoreFormSize(CustomWinConfiguration->ConsoleWin.WindowSize, this);
  309. FAutoBounds = BoundsRect;
  310. }
  311. //---------------------------------------------------------------------------
  312. void __fastcall TConsoleDialog::OutputMemoContextPopup(TObject * Sender,
  313. TPoint & MousePos, bool & Handled)
  314. {
  315. MenuPopup(Sender, MousePos, Handled);
  316. }
  317. //---------------------------------------------------------------------------