CustomCommand.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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(RemoteFilesCheck,
  128. FLAGCLEAR(FOptions, ccoDisableRemoteFiles) && AllowRemoteFiles &&
  129. (!RecursiveCheck->Enabled || !RecursiveCheck->Checked));
  130. }
  131. //---------------------------------------------------------------------------
  132. void __fastcall TCustomCommandDialog::SetParams(int value)
  133. {
  134. FParams = value;
  135. ApplyToDirectoriesCheck->Checked = FLAGSET(value, ccApplyToDirectories);
  136. RecursiveCheck->Checked = FLAGSET(value, ccRecursive);
  137. (FLAGSET(value, ccLocal) ? LocalCommandButton : RemoteCommandButton)->Checked = true;
  138. ShowResultsCheck->Checked = FLAGSET(value, ccShowResults);
  139. CopyResultsCheck->Checked = FLAGSET(value, ccCopyResults);
  140. RemoteFilesCheck->Checked = FLAGSET(value, ccRemoteFiles);
  141. }
  142. //---------------------------------------------------------------------------
  143. int __fastcall TCustomCommandDialog::GetParams()
  144. {
  145. return
  146. (FParams & ~(ccApplyToDirectories | ccRecursive | ccLocal |
  147. ccShowResults | ccCopyResults | ccRemoteFiles)) |
  148. FLAGMASK(!RemoteCommandButton->Checked, ccLocal) |
  149. FLAGMASK(ApplyToDirectoriesCheck->Checked, ccApplyToDirectories) |
  150. FLAGMASK(RecursiveCheck->Checked && RecursiveCheck->Enabled, ccRecursive) |
  151. FLAGMASK(ShowResultsCheck->Checked && ShowResultsCheck->Enabled, ccShowResults) |
  152. FLAGMASK(CopyResultsCheck->Checked && CopyResultsCheck->Enabled, ccCopyResults) |
  153. FLAGMASK(RemoteFilesCheck->Checked && RemoteFilesCheck->Enabled, ccRemoteFiles);
  154. }
  155. //---------------------------------------------------------------------------
  156. void __fastcall TCustomCommandDialog::ControlChange(TObject * /*Sender*/)
  157. {
  158. UpdateControls();
  159. }
  160. //---------------------------------------------------------------------------
  161. bool __fastcall TCustomCommandDialog::Execute(TCustomCommandType & Command)
  162. {
  163. CommandEdit->Items = CustomWinConfiguration->History[L"CustomCommand"];
  164. if (CommandEdit->Items->Count == 0)
  165. {
  166. for (int i = 0; i < FCustomCommandList->Count; i++)
  167. {
  168. CommandEdit->Items->Add(FCustomCommandList->Commands[i]->Name);
  169. }
  170. }
  171. DescriptionEdit->Text = Command.Name;
  172. FOrigDescription = Command.Name;
  173. CommandEdit->Text = Command.Command;
  174. SetParams(Command.Params);
  175. if (FMode != ccmAdHoc)
  176. {
  177. SetShortCutCombo(ShortCutCombo, Command.ShortCut);
  178. }
  179. bool Result = (ShowModal() == DefaultResult(this));
  180. if (Result)
  181. {
  182. GetCommand(Command);
  183. CommandEdit->SaveToHistory();
  184. CustomWinConfiguration->History[L"CustomCommand"] = CommandEdit->Items;
  185. }
  186. return Result;
  187. }
  188. //---------------------------------------------------------------------------
  189. void __fastcall TCustomCommandDialog::FormCloseQuery(TObject * /*Sender*/,
  190. bool & /*CanClose*/)
  191. {
  192. if (ModalResult == DefaultResult(this))
  193. {
  194. if ((FMode == ccmAdd) || (FMode == ccmEdit))
  195. {
  196. UnicodeString Desc = DescriptionEdit->Text;
  197. if (Desc.Pos(L"=") > 0)
  198. {
  199. DescriptionEdit->SetFocus();
  200. throw Exception(FMTLOAD(CUSTOM_COMMAND_INVALID, (L"=")));
  201. }
  202. if (((FMode == ccmAdd) || ((FMode == ccmEdit) && (Desc != FOrigDescription))) &&
  203. (FCustomCommandList->Find(Desc) != NULL))
  204. {
  205. DescriptionEdit->SetFocus();
  206. throw Exception(FMTLOAD(CUSTOM_COMMAND_DUPLICATE, (Desc)));
  207. }
  208. }
  209. try
  210. {
  211. bool RemoteCommand = RemoteCommandButton->Checked;
  212. TRemoteCustomCommand RemoteCustomCommand;
  213. TLocalCustomCommand LocalCustomCommand;
  214. TFileCustomCommand * FileCustomCommand =
  215. (RemoteCommand ? &RemoteCustomCommand : &LocalCustomCommand);
  216. TInteractiveCustomCommand InteractiveCustomCommand(FileCustomCommand);
  217. UnicodeString Command = CommandEdit->Text;
  218. InteractiveCustomCommand.Validate(Command);
  219. Command = InteractiveCustomCommand.Complete(Command, false);
  220. FileCustomCommand->Validate(Command);
  221. }
  222. catch(...)
  223. {
  224. CommandEdit->SetFocus();
  225. throw;
  226. }
  227. if (FOnValidate)
  228. {
  229. TCustomCommandType Command;
  230. GetCommand(Command);
  231. FOnValidate(Command);
  232. }
  233. }
  234. }
  235. //---------------------------------------------------------------------------
  236. void __fastcall TCustomCommandDialog::HelpButtonClick(TObject * /*Sender*/)
  237. {
  238. FormHelp(this);
  239. }
  240. //---------------------------------------------------------------------------
  241. void __fastcall TCustomCommandDialog::CommandEditGetData(
  242. THistoryComboBox * /*Sender*/, Pointer & Data)
  243. {
  244. Data = reinterpret_cast<void *>(ccSet | GetParams());
  245. }
  246. //---------------------------------------------------------------------------
  247. void __fastcall TCustomCommandDialog::CommandEditSetData(
  248. THistoryComboBox * /*Sender*/, Pointer Data)
  249. {
  250. int IData = reinterpret_cast<int>(Data);
  251. if (FLAGSET(IData, ccSet))
  252. {
  253. SetParams(IData & ~ccSet);
  254. }
  255. }
  256. //---------------------------------------------------------------------------
  257. void __fastcall TCustomCommandDialog::GetCommand(TCustomCommandType & Command)
  258. {
  259. Command.Name = DescriptionEdit->Text;
  260. Command.Command = CommandEdit->Text;
  261. Command.Params = GetParams();
  262. if (FMode != ccmAdHoc)
  263. {
  264. Command.ShortCut = GetShortCutCombo(ShortCutCombo);
  265. }
  266. }
  267. //---------------------------------------------------------------------------
  268. void __fastcall TCustomCommandDialog::FormShow(TObject * /*Sender*/)
  269. {
  270. InstallPathWordBreakProc(CommandEdit);
  271. }
  272. //---------------------------------------------------------------------------