CustomCommand.cpp 9.7 KB

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