CustomCommand.cpp 9.6 KB

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