CustomCommand.cpp 8.4 KB

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