CustomCommand.cpp 9.2 KB

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