CustomCommand.cpp 9.7 KB

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