CustomCommand.cpp 9.7 KB

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