CustomCommand.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <Terminal.h>
  6. #include <TextsWin.h>
  7. #include <WinConfiguration.h>
  8. #include <WinInterface.h>
  9. #include <GUITools.h>
  10. #include <CoreMain.h>
  11. #include "CustomCommand.h"
  12. #include "VCLCommon.h"
  13. //---------------------------------------------------------------------------
  14. #pragma package(smart_init)
  15. #pragma link "HistoryComboBox"
  16. #pragma resource "*.dfm"
  17. //---------------------------------------------------------------------------
  18. bool __fastcall DoCustomCommandDialog(TCustomCommandType & Command,
  19. const TCustomCommandList * CustomCommandList,
  20. TCustomCommandsMode Mode, int Options, TCustomCommandValidate OnValidate,
  21. const TShortCuts * ShortCuts)
  22. {
  23. bool Result;
  24. TCustomCommandDialog * Dialog = new TCustomCommandDialog(
  25. GetFormOwner(), CustomCommandList, Mode, Options, OnValidate, ShortCuts);
  26. try
  27. {
  28. Result = Dialog->Execute(Command);
  29. }
  30. __finally
  31. {
  32. delete Dialog;
  33. }
  34. return Result;
  35. }
  36. //---------------------------------------------------------------------------
  37. __fastcall TCustomCommandDialog::TCustomCommandDialog(TComponent* Owner,
  38. const TCustomCommandList * CustomCommandList, TCustomCommandsMode Mode,
  39. int Options, TCustomCommandValidate OnValidate, const TShortCuts * ShortCuts)
  40. : TForm(Owner)
  41. {
  42. SetCorrectFormParent(this);
  43. UseSystemSettings(this);
  44. FCustomCommandList = CustomCommandList;
  45. FMode = Mode;
  46. FOnValidate = OnValidate;
  47. std::unique_ptr<TStrings> HintStrings(TextToStringList(LoadStr(CUSTOM_COMMAND_PATTERNS_HINT5)));
  48. HintStrings->Insert(6, LoadStr(CUSTOM_COMMAND_PATTERNS_HINT6));
  49. HintStrings->Insert(11, LoadStr(PATTERNS_HINT_K));
  50. HintStrings->Insert(18, LoadStr(PATTERNS_HINT_BACKSLASH));
  51. HintLabel(HintText, TrimRight(StringsToText(HintStrings.get())));
  52. int CaptionRes;
  53. switch (FMode)
  54. {
  55. case ccmAdd:
  56. CaptionRes = CUSTOM_COMMAND_ADD;
  57. break;
  58. case ccmEdit:
  59. CaptionRes = CUSTOM_COMMAND_EDIT;
  60. break;
  61. case ccmAdHoc:
  62. default:
  63. CaptionRes = CUSTOM_COMMAND_AD_HOC;
  64. break;
  65. }
  66. Caption = LoadStr(CaptionRes);
  67. if (FMode == ccmAdHoc)
  68. {
  69. int Shift = CommandEdit->Top - DescriptionEdit->Top;
  70. int Shift2 = Group->Height - ShortCutLabel->Top;
  71. DescriptionLabel->Visible = false;
  72. DescriptionEdit->Visible = false;
  73. for (int i = 0; i < Group->ControlCount; i++)
  74. {
  75. TControl * Control = Group->Controls[i];
  76. if (Control->Visible)
  77. {
  78. if (Control->Top > DescriptionLabel->Top)
  79. {
  80. Control->Top = Control->Top - Shift;
  81. }
  82. }
  83. }
  84. ShortCutLabel->Visible = false;
  85. ShortCutCombo->Visible = false;
  86. ClientHeight = ClientHeight - Shift - Shift2;
  87. }
  88. else
  89. {
  90. DebugAssert(ShortCuts != NULL);
  91. InitializeShortCutCombo(ShortCutCombo, *ShortCuts);
  92. }
  93. FOptions = Options;
  94. UpdateControls();
  95. }
  96. //---------------------------------------------------------------------------
  97. void __fastcall TCustomCommandDialog::UpdateControls()
  98. {
  99. EnableControl(RemoteCommandButton, FLAGCLEAR(FOptions, ccoDisableRemote));
  100. UnicodeString Command = CommandEdit->Text;
  101. EnableControl(OkButton, !Command.IsEmpty() && !DescriptionEdit->Text.IsEmpty());
  102. bool RemoteCommand = RemoteCommandButton->Checked;
  103. bool AllowRecursive = true;
  104. bool AllowApplyToDirectories = true;
  105. bool AllowRemoteFiles = false;
  106. try
  107. {
  108. TRemoteCustomCommand RemoteCustomCommand;
  109. TLocalCustomCommand LocalCustomCommand;
  110. TFileCustomCommand * FileCustomCommand =
  111. (RemoteCommand ? &RemoteCustomCommand : &LocalCustomCommand);
  112. TInteractiveCustomCommand InteractiveCustomCommand(FileCustomCommand);
  113. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command, false);
  114. bool FileCommand = FileCustomCommand->IsFileCommand(Cmd);
  115. AllowRemoteFiles = !RemoteCommand && FileCustomCommand->IsRemoteFileCommand(Cmd);
  116. AllowRecursive = FileCommand && !FileCustomCommand->IsFileListCommand(Cmd);
  117. if (AllowRecursive && !RemoteCommand)
  118. {
  119. AllowRecursive = !LocalCustomCommand.HasLocalFileName(Cmd);
  120. }
  121. AllowApplyToDirectories = FileCommand;
  122. }
  123. catch(...)
  124. {
  125. }
  126. EnableControl(RecursiveCheck, AllowRecursive && (!RemoteFilesCheck->Enabled || !RemoteFilesCheck->Checked));
  127. EnableControl(ApplyToDirectoriesCheck, AllowApplyToDirectories);
  128. EnableControl(ShowResultsCheck, RemoteCommand);
  129. EnableControl(RemoteFilesCheck,
  130. FLAGCLEAR(FOptions, ccoDisableRemoteFiles) && AllowRemoteFiles &&
  131. (!RecursiveCheck->Enabled || !RecursiveCheck->Checked));
  132. }
  133. //---------------------------------------------------------------------------
  134. void __fastcall TCustomCommandDialog::SetParams(int value)
  135. {
  136. FParams = value;
  137. ApplyToDirectoriesCheck->Checked = FLAGSET(value, ccApplyToDirectories);
  138. RecursiveCheck->Checked = FLAGSET(value, ccRecursive);
  139. (FLAGSET(value, ccLocal) ? LocalCommandButton : RemoteCommandButton)->Checked = true;
  140. ShowResultsCheck->Checked = FLAGSET(value, ccShowResults);
  141. CopyResultsCheck->Checked = FLAGSET(value, ccCopyResults);
  142. RemoteFilesCheck->Checked = FLAGSET(value, ccRemoteFiles);
  143. }
  144. //---------------------------------------------------------------------------
  145. int __fastcall TCustomCommandDialog::GetParams()
  146. {
  147. return
  148. (FParams & ~(ccApplyToDirectories | ccRecursive | ccLocal |
  149. ccShowResults | ccCopyResults | ccRemoteFiles)) |
  150. FLAGMASK(!RemoteCommandButton->Checked, ccLocal) |
  151. FLAGMASK(ApplyToDirectoriesCheck->Checked, ccApplyToDirectories) |
  152. FLAGMASK(RecursiveCheck->Checked && RecursiveCheck->Enabled, ccRecursive) |
  153. FLAGMASK(ShowResultsCheck->Checked && ShowResultsCheck->Enabled, ccShowResults) |
  154. FLAGMASK(CopyResultsCheck->Checked && CopyResultsCheck->Enabled, ccCopyResults) |
  155. FLAGMASK(RemoteFilesCheck->Checked && RemoteFilesCheck->Enabled, ccRemoteFiles);
  156. }
  157. //---------------------------------------------------------------------------
  158. void __fastcall TCustomCommandDialog::ControlChange(TObject * /*Sender*/)
  159. {
  160. UpdateControls();
  161. }
  162. //---------------------------------------------------------------------------
  163. bool __fastcall TCustomCommandDialog::Execute(TCustomCommandType & Command)
  164. {
  165. CommandEdit->Items = CustomWinConfiguration->History[L"CustomCommand"];
  166. if (CommandEdit->Items->Count == 0)
  167. {
  168. for (int i = 0; i < FCustomCommandList->Count; i++)
  169. {
  170. CommandEdit->Items->Add(FCustomCommandList->Commands[i]->Name);
  171. }
  172. }
  173. DescriptionEdit->Text = Command.Name;
  174. FOrigDescription = Command.Name;
  175. CommandEdit->Text = Command.Command;
  176. SetParams(Command.Params);
  177. if (FMode != ccmAdHoc)
  178. {
  179. SetShortCutCombo(ShortCutCombo, Command.ShortCut);
  180. }
  181. bool Result = (ShowModal() == DefaultResult(this));
  182. if (Result)
  183. {
  184. GetCommand(Command);
  185. CommandEdit->SaveToHistory();
  186. CustomWinConfiguration->History[L"CustomCommand"] = CommandEdit->Items;
  187. }
  188. return Result;
  189. }
  190. //---------------------------------------------------------------------------
  191. void __fastcall TCustomCommandDialog::FormCloseQuery(TObject * /*Sender*/,
  192. bool & /*CanClose*/)
  193. {
  194. if (ModalResult == DefaultResult(this))
  195. {
  196. if ((FMode == ccmAdd) || (FMode == ccmEdit))
  197. {
  198. UnicodeString Desc = DescriptionEdit->Text;
  199. if (Desc.Pos(L"=") > 0)
  200. {
  201. DescriptionEdit->SetFocus();
  202. throw Exception(FMTLOAD(CUSTOM_COMMAND_INVALID, (L"=")));
  203. }
  204. if (((FMode == ccmAdd) || ((FMode == ccmEdit) && (Desc != FOrigDescription))) &&
  205. (FCustomCommandList->Find(Desc) != NULL))
  206. {
  207. DescriptionEdit->SetFocus();
  208. throw Exception(FMTLOAD(CUSTOM_COMMAND_DUPLICATE, (Desc)));
  209. }
  210. }
  211. try
  212. {
  213. bool RemoteCommand = RemoteCommandButton->Checked;
  214. TRemoteCustomCommand RemoteCustomCommand;
  215. TLocalCustomCommand LocalCustomCommand;
  216. TFileCustomCommand * FileCustomCommand =
  217. (RemoteCommand ? &RemoteCustomCommand : &LocalCustomCommand);
  218. TInteractiveCustomCommand InteractiveCustomCommand(FileCustomCommand);
  219. UnicodeString Command = CommandEdit->Text;
  220. InteractiveCustomCommand.Validate(Command);
  221. Command = InteractiveCustomCommand.Complete(Command, false);
  222. FileCustomCommand->Validate(Command);
  223. }
  224. catch(...)
  225. {
  226. CommandEdit->SetFocus();
  227. throw;
  228. }
  229. if (FOnValidate)
  230. {
  231. TCustomCommandType Command;
  232. GetCommand(Command);
  233. FOnValidate(Command);
  234. }
  235. }
  236. }
  237. //---------------------------------------------------------------------------
  238. void __fastcall TCustomCommandDialog::HelpButtonClick(TObject * /*Sender*/)
  239. {
  240. FormHelp(this);
  241. }
  242. //---------------------------------------------------------------------------
  243. void __fastcall TCustomCommandDialog::CommandEditGetData(
  244. THistoryComboBox * /*Sender*/, Pointer & Data)
  245. {
  246. Data = reinterpret_cast<void *>(ccSet | GetParams());
  247. }
  248. //---------------------------------------------------------------------------
  249. void __fastcall TCustomCommandDialog::CommandEditSetData(
  250. THistoryComboBox * /*Sender*/, Pointer Data)
  251. {
  252. int IData = reinterpret_cast<int>(Data);
  253. if (FLAGSET(IData, ccSet))
  254. {
  255. SetParams(IData & ~ccSet);
  256. }
  257. }
  258. //---------------------------------------------------------------------------
  259. void __fastcall TCustomCommandDialog::GetCommand(TCustomCommandType & Command)
  260. {
  261. Command.Name = DescriptionEdit->Text;
  262. Command.Command = CommandEdit->Text;
  263. Command.Params = GetParams();
  264. if (FMode != ccmAdHoc)
  265. {
  266. Command.ShortCut = GetShortCutCombo(ShortCutCombo);
  267. }
  268. }
  269. //---------------------------------------------------------------------------
  270. void __fastcall TCustomCommandDialog::FormShow(TObject * /*Sender*/)
  271. {
  272. InstallPathWordBreakProc(CommandEdit);
  273. }
  274. //---------------------------------------------------------------------------