CustomCommand.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. InstallPathWordBreakProc(CommandEdit);
  50. HintLabel(HintText, LoadStr(CUSTOM_COMMAND_PATTERNS_HINT2));
  51. int CaptionRes;
  52. switch (FMode)
  53. {
  54. case ccmAdd:
  55. CaptionRes = CUSTOM_COMMAND_ADD;
  56. break;
  57. case ccmEdit:
  58. CaptionRes = CUSTOM_COMMAND_EDIT;
  59. break;
  60. case ccmAdHoc:
  61. default:
  62. CaptionRes = CUSTOM_COMMAND_AD_HOC;
  63. break;
  64. }
  65. Caption = LoadStr(CaptionRes);
  66. if (FMode == ccmAdHoc)
  67. {
  68. int Shift = CommandEdit->Top - DescriptionEdit->Top;
  69. int Shift2 = Group->Height - ShortCutLabel->Top;
  70. DescriptionLabel->Visible = false;
  71. DescriptionEdit->Visible = false;
  72. for (int i = 0; i < Group->ControlCount; i++)
  73. {
  74. TControl * Control = Group->Controls[i];
  75. if (Control->Visible)
  76. {
  77. if (Control->Top > DescriptionLabel->Top)
  78. {
  79. Control->Top = Control->Top - Shift;
  80. }
  81. }
  82. }
  83. ShortCutLabel->Visible = false;
  84. ShortCutCombo->Visible = false;
  85. ClientHeight = ClientHeight - Shift - Shift2;
  86. }
  87. else
  88. {
  89. assert(ShortCuts != NULL);
  90. InitializeShortCutCombo(ShortCutCombo, *ShortCuts);
  91. }
  92. EnableControl(RemoteCommandButton, FLAGCLEAR(Options, ccoDisableRemote));
  93. UpdateControls();
  94. }
  95. //---------------------------------------------------------------------------
  96. void __fastcall TCustomCommandDialog::UpdateControls()
  97. {
  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. 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. AllowRecursive = FileCommand && !FileCustomCommand->IsFileListCommand(Cmd);
  113. if (AllowRecursive && !RemoteCommand)
  114. {
  115. AllowRecursive = !LocalCustomCommand.HasLocalFileName(Cmd);
  116. }
  117. AllowApplyToDirectories = FileCommand;
  118. }
  119. catch(...)
  120. {
  121. }
  122. EnableControl(RecursiveCheck, AllowRecursive);
  123. EnableControl(ApplyToDirectoriesCheck, AllowApplyToDirectories);
  124. EnableControl(ShowResultsCheck, RemoteCommand);
  125. EnableControl(CopyResultsCheck, RemoteCommand);
  126. }
  127. //---------------------------------------------------------------------------
  128. void __fastcall TCustomCommandDialog::SetParams(int value)
  129. {
  130. FParams = value;
  131. ApplyToDirectoriesCheck->Checked = FLAGSET(value, ccApplyToDirectories);
  132. RecursiveCheck->Checked = FLAGSET(value, ccRecursive);
  133. (FLAGSET(value, ccLocal) ? LocalCommandButton : RemoteCommandButton)->Checked = true;
  134. ShowResultsCheck->Checked = FLAGSET(value, ccShowResults);
  135. CopyResultsCheck->Checked = FLAGSET(value, ccCopyResults);
  136. }
  137. //---------------------------------------------------------------------------
  138. int __fastcall TCustomCommandDialog::GetParams()
  139. {
  140. return
  141. (FParams & ~(ccApplyToDirectories | ccRecursive | ccLocal |
  142. ccShowResults | ccCopyResults)) |
  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. }
  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() == mrOk);
  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 != mrCancel)
  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) != 0))
  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. //---------------------------------------------------------------------------