CustomCommand.cpp 9.0 KB

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