1
0

Custom.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Dialogs.hpp>
  5. //---------------------------------------------------------------------
  6. #include <Common.h>
  7. #include <CustomWinConfiguration.h>
  8. #include <WinInterface.h>
  9. #include <VCLCommon.h>
  10. #include <TextsWin.h>
  11. #include <HelpWin.h>
  12. #include <CoreMain.h>
  13. #include <PasTools.hpp>
  14. #include <ProgParams.h>
  15. #include <Tools.h>
  16. #include <GUITools.h>
  17. #include <HistoryComboBox.hpp>
  18. #include <Math.hpp>
  19. #include "Custom.h"
  20. //---------------------------------------------------------------------
  21. #pragma link "PasswordEdit"
  22. #ifndef NO_RESOURCES
  23. #pragma resource "*.dfm"
  24. #endif
  25. //---------------------------------------------------------------------
  26. __fastcall TCustomDialog::TCustomDialog(UnicodeString AHelpKeyword)
  27. : TForm(GetFormOwner())
  28. {
  29. UseSystemSettings(this);
  30. FControlPadding = ScaleByTextHeight(this, 8);
  31. FPos = ScaleByTextHeight(this, 8);
  32. FPrePos = FPos;
  33. FHorizontalMargin = ScaleByTextHeight(this, 8);
  34. FIndent = FHorizontalMargin;
  35. FGroupBox = NULL;
  36. HelpKeyword = AHelpKeyword;
  37. TBorderIcons BI = BorderIcons;
  38. if (HelpKeyword.IsEmpty())
  39. {
  40. BI >> biHelp;
  41. OKButton->Left = CancelButton->Left;
  42. CancelButton->Left = HelpButton->Left;
  43. HelpButton->Visible = false;
  44. }
  45. else
  46. {
  47. BI << biHelp;
  48. }
  49. BorderIcons = BI;
  50. }
  51. //---------------------------------------------------------------------
  52. bool __fastcall TCustomDialog::Execute()
  53. {
  54. Changed();
  55. return (ShowModal() == DefaultResult(this));
  56. }
  57. //---------------------------------------------------------------------
  58. void __fastcall TCustomDialog::DoChange(bool & /*CanSubmit*/)
  59. {
  60. // noop
  61. }
  62. //---------------------------------------------------------------------
  63. void __fastcall TCustomDialog::Changed()
  64. {
  65. bool CanSubmit = true;
  66. DoChange(CanSubmit);
  67. EnableControl(OKButton, CanSubmit);
  68. }
  69. //---------------------------------------------------------------------
  70. void __fastcall TCustomDialog::Change(TObject * /*Sender*/)
  71. {
  72. Changed();
  73. }
  74. //---------------------------------------------------------------------------
  75. void __fastcall TCustomDialog::DoHelp()
  76. {
  77. FormHelp(this);
  78. }
  79. //---------------------------------------------------------------------------
  80. void __fastcall TCustomDialog::HelpButtonClick(TObject * /*Sender*/)
  81. {
  82. DoHelp();
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall TCustomDialog::DoShow()
  86. {
  87. OKButton->TabOrder = FCount;
  88. CancelButton->TabOrder = static_cast<short>(FCount + 1);
  89. HelpButton->TabOrder = static_cast<short>(FCount + 2);
  90. Changed();
  91. TForm::DoShow();
  92. }
  93. //---------------------------------------------------------------------------
  94. void __fastcall TCustomDialog::DoValidate()
  95. {
  96. // noop
  97. }
  98. //---------------------------------------------------------------------------
  99. bool __fastcall TCustomDialog::CloseQuery()
  100. {
  101. if (ModalResult == DefaultResult(this))
  102. {
  103. DoValidate();
  104. }
  105. return TForm::CloseQuery();
  106. }
  107. //---------------------------------------------------------------------------
  108. void __fastcall TCustomDialog::AddImage(const UnicodeString & ImageName)
  109. {
  110. TImage * Image = new TImage(this);
  111. Image->Name = L"Image";
  112. Image->Parent = GetDefaultParent();
  113. LoadDialogImage(Image, ImageName);
  114. Image->SetBounds(FIndent, FPos + ScaleByTextHeight(this, 3), Image->Picture->Width, Image->Picture->Height);
  115. FIndent += Image->Width + ScaleByTextHeight(this, 12);
  116. }
  117. //---------------------------------------------------------------------------
  118. int __fastcall TCustomDialog::GetMaxControlWidth(TControl * Control)
  119. {
  120. return GetDefaultParent()->ClientWidth - Control->Left - FHorizontalMargin;
  121. }
  122. //---------------------------------------------------------------------------
  123. TWinControl * __fastcall TCustomDialog::GetDefaultParent()
  124. {
  125. return (FGroupBox != NULL) ? FGroupBox : static_cast<TWinControl *>(this);
  126. }
  127. //---------------------------------------------------------------------------
  128. void __fastcall TCustomDialog::AdjustHeight(TControl * Control)
  129. {
  130. FPos = Control->Top + Control->Height + FControlPadding;
  131. int Delta = (FPos - FPrePos);
  132. ClientHeight = ClientHeight + Delta;
  133. if (FGroupBox != NULL)
  134. {
  135. FGroupBox->Height = FGroupBox->Height + Delta;
  136. }
  137. FPrePos = FPos;
  138. }
  139. //---------------------------------------------------------------------------
  140. void __fastcall TCustomDialog::AddWinControl(TWinControl * Control)
  141. {
  142. Control->TabOrder = FCount;
  143. FCount++;
  144. }
  145. //---------------------------------------------------------------------------
  146. TCheckBox * __fastcall TCustomDialog::CreateAndAddCheckBox(const UnicodeString & Caption)
  147. {
  148. TCheckBox * CheckBox = new TCheckBox(this);
  149. CheckBox->Caption = Caption;
  150. AddButtonControl(CheckBox);
  151. return CheckBox;
  152. }
  153. //---------------------------------------------------------------------------
  154. TLabel * __fastcall TCustomDialog::CreateLabel(UnicodeString Label)
  155. {
  156. TLabel * Result = new TLabel(this);
  157. Result->Caption = Label;
  158. return Result;
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TCustomDialog::AddEditLikeControl(TWinControl * Edit, TLabel * Label, bool OneLine)
  162. {
  163. Edit->Parent = GetDefaultParent();
  164. // this updates Height property to real value
  165. Edit->HandleNeeded();
  166. Label->Parent = GetDefaultParent();
  167. Label->Left = FIndent;
  168. if (OneLine)
  169. {
  170. DebugAssert(Edit->Height > Label->Height);
  171. Label->Top = FPos + ((Edit->Height - Label->Height) / 2);
  172. }
  173. else
  174. {
  175. Label->Top = FPos;
  176. FPos += Label->Height + ScaleByTextHeight(this, 4);
  177. }
  178. Edit->Top = FPos;
  179. if (OneLine)
  180. {
  181. Edit->Left = GetDefaultParent()->ClientWidth - FHorizontalMargin - Edit->Width;
  182. }
  183. else
  184. {
  185. Edit->Left = FIndent;
  186. Edit->Width = GetMaxControlWidth(Edit);
  187. }
  188. AdjustHeight(Edit);
  189. if (Label->FocusControl == NULL)
  190. {
  191. Label->FocusControl = Edit;
  192. }
  193. else
  194. {
  195. DebugAssert(Label->FocusControl == Edit);
  196. }
  197. AddWinControl(Edit);
  198. }
  199. //---------------------------------------------------------------------------
  200. void __fastcall TCustomDialog::AddEdit(TCustomEdit * Edit, TLabel * Label)
  201. {
  202. AddEditLikeControl(Edit, Label, false);
  203. TEdit * PublicEdit = reinterpret_cast<TEdit *>(Edit);
  204. if (PublicEdit->OnChange == NULL)
  205. {
  206. PublicEdit->OnChange = Change;
  207. }
  208. }
  209. //---------------------------------------------------------------------------
  210. void __fastcall TCustomDialog::AddComboBox(TCustomCombo * Combo, TLabel * Label, TStrings * Items, bool OneLine)
  211. {
  212. AddEditLikeControl(Combo, Label, OneLine);
  213. if (Items != NULL)
  214. {
  215. Combo->Items = Items;
  216. }
  217. if (OneLine)
  218. {
  219. int Width = 0;
  220. for (int Index = 0; Index < Combo->Items->Count; Index++)
  221. {
  222. Width = Max(Width, Combo->Canvas->TextWidth(Combo->Items->Strings[Index]));
  223. }
  224. Width += ScaleByTextHeight(Combo, 4 + 16 + 14);
  225. Width = Max(Width, HelpButton->Width);
  226. Combo->Width = Width;
  227. Combo->Left = GetDefaultParent()->ClientWidth - FHorizontalMargin - Width;
  228. }
  229. TComboBox * PublicCombo = reinterpret_cast<TComboBox *>(Combo);
  230. if (PublicCombo->OnChange == NULL)
  231. {
  232. PublicCombo->OnChange = Change;
  233. }
  234. }
  235. //---------------------------------------------------------------------------
  236. void __fastcall TCustomDialog::ScaleButtonControl(TButtonControl * Control)
  237. {
  238. // this updates Height property to real value
  239. Control->HandleNeeded();
  240. // buttons do not scale with text on their own
  241. Control->Height = ScaleByTextHeight(Control, Control->Height);
  242. }
  243. //---------------------------------------------------------------------------
  244. void __fastcall TCustomDialog::AddButtonControl(TButtonControl * Control)
  245. {
  246. Control->Parent = GetDefaultParent();
  247. Control->Left = FIndent + ScaleByTextHeight(this, 6);
  248. Control->Top = FPos;
  249. Control->Width = GetMaxControlWidth(Control);
  250. ScaleButtonControl(Control);
  251. AdjustHeight(Control);
  252. AddWinControl(Control);
  253. TCheckBox * PublicControl = reinterpret_cast<TCheckBox *>(Control);
  254. if (PublicControl->OnClick == NULL)
  255. {
  256. PublicControl->OnClick = Change;
  257. }
  258. }
  259. //---------------------------------------------------------------------------
  260. void __fastcall TCustomDialog::AddText(TLabel * Label)
  261. {
  262. Label->Parent = GetDefaultParent();
  263. Label->WordWrap = true;
  264. Label->Left = FIndent;
  265. Label->Width = GetMaxControlWidth(Label);
  266. Label->Top = FPos;
  267. Label->ShowAccelChar = false;
  268. TRect TextRect;
  269. SetRect(&TextRect, 0, 0, Label->Width, 0);
  270. DrawText(Label->Canvas->Handle, Label->Caption.c_str(), Label->Caption.Length() + 1, &TextRect,
  271. DT_EXPANDTABS | DT_CALCRECT | DT_WORDBREAK | DT_NOPREFIX |
  272. Label->DrawTextBiDiModeFlagsReadingOnly());
  273. Label->Height = TextRect.Height();
  274. AdjustHeight(Label);
  275. }
  276. //---------------------------------------------------------------------------
  277. void __fastcall TCustomDialog::AddText(TStaticText * Label)
  278. {
  279. Label->Parent = GetDefaultParent();
  280. Label->Left = FIndent;
  281. Label->Width = GetMaxControlWidth(Label);
  282. Label->Top = FPos;
  283. Label->ShowAccelChar = false;
  284. AdjustHeight(Label);
  285. AddWinControl(Label);
  286. }
  287. //---------------------------------------------------------------------------
  288. void __fastcall TCustomDialog::AddSeparator()
  289. {
  290. TBevel * Bevel = new TBevel(this);
  291. Bevel->Parent = GetDefaultParent();
  292. Bevel->Left = FIndent;
  293. Bevel->Top = FPos;
  294. Bevel->Height = 2;
  295. Bevel->Width = GetMaxControlWidth(Bevel);
  296. AdjustHeight(Bevel);
  297. }
  298. //---------------------------------------------------------------------------
  299. void __fastcall TCustomDialog::StartGroup(const UnicodeString & Caption)
  300. {
  301. if (FGroupBox != NULL)
  302. {
  303. FIndent = FGroupBox->Left;
  304. FPos = FGroupBox->Top + FGroupBox->Height + FControlPadding;
  305. FPrePos = FPos;
  306. FGroupBox = NULL;
  307. }
  308. TGroupBox * GroupBox = new TGroupBox(this);
  309. GroupBox->Parent = this;
  310. GroupBox->Caption = Caption;
  311. GroupBox->Left = FIndent;
  312. GroupBox->Top = FPos;
  313. GroupBox->Height = ScaleByTextHeight(GroupBox, 20);
  314. GroupBox->Width = GetMaxControlWidth(GroupBox);
  315. AdjustHeight(GroupBox);
  316. FPos = ScaleByTextHeight(this, 16);
  317. FPrePos = FPos;
  318. FIndent = FHorizontalMargin;
  319. FGroupBox = GroupBox;
  320. }
  321. //---------------------------------------------------------------------------
  322. //---------------------------------------------------------------------------
  323. class TSaveSessionDialog : public TCustomDialog
  324. {
  325. public:
  326. __fastcall TSaveSessionDialog(TComponent* AOwner);
  327. void __fastcall Init(bool CanSavePassword, bool NotRecommendedSavingPassword,
  328. TStrings * AdditionalFolders);
  329. bool __fastcall Execute(UnicodeString & SessionName, bool & SavePassword,
  330. bool & CreateShortcut, const UnicodeString & OriginalSessionName);
  331. protected:
  332. virtual void __fastcall DoValidate();
  333. virtual void __fastcall DoChange(bool & CanSubmit);
  334. private:
  335. UnicodeString FOriginalSessionName;
  336. TEdit * SessionNameEdit;
  337. TComboBox * FolderCombo;
  338. TCheckBox * SavePasswordCheck;
  339. TCheckBox * CreateShortcutCheck;
  340. UnicodeString FRootFolder;
  341. UnicodeString __fastcall GetSessionName();
  342. };
  343. //---------------------------------------------------------------------------
  344. // Need to have an Owner argument for SafeFormCreate
  345. __fastcall TSaveSessionDialog::TSaveSessionDialog(TComponent* /*AOwner*/) :
  346. TCustomDialog(HELP_SESSION_SAVE)
  347. {
  348. }
  349. //---------------------------------------------------------------------------
  350. void __fastcall TSaveSessionDialog::Init(bool CanSavePassword,
  351. bool NotRecommendedSavingPassword, TStrings * AdditionalFolders)
  352. {
  353. Caption = LoadStr(SAVE_SESSION_CAPTION);
  354. SessionNameEdit = new TEdit(this);
  355. AddEdit(SessionNameEdit, CreateLabel(LoadStr(SAVE_SESSION_PROMPT)));
  356. FRootFolder = LoadStr(SAVE_SESSION_ROOT_FOLDER2);
  357. std::unique_ptr<TStringList> Folders(new TStringList());
  358. if (AdditionalFolders != NULL)
  359. {
  360. Folders->AddStrings(AdditionalFolders);
  361. }
  362. for (int Index = 0; Index < StoredSessions->Count; Index++)
  363. {
  364. TSessionData * Data = StoredSessions->Sessions[Index];
  365. if (!Data->Special && !Data->IsWorkspace)
  366. {
  367. UnicodeString Folder = Data->FolderName;
  368. if (!Folder.IsEmpty() && Folders->IndexOf(Folder) < 0)
  369. {
  370. Folders->Add(Folder);
  371. }
  372. }
  373. }
  374. DebugAssert(!Folders->CaseSensitive);
  375. Folders->Sort();
  376. FolderCombo = new TComboBox(this);
  377. AddComboBox(FolderCombo, CreateLabel(LoadStr(SAVE_SESSION_FOLDER)));
  378. FolderCombo->DropDownCount = Max(FolderCombo->DropDownCount, 16);
  379. FolderCombo->Items->Add(FRootFolder);
  380. FolderCombo->Items->AddStrings(Folders.get());
  381. SavePasswordCheck = CreateAndAddCheckBox(
  382. LoadStr(NotRecommendedSavingPassword ? SAVE_SESSION_PASSWORD :
  383. (CustomWinConfiguration->UseMasterPassword ? SAVE_SESSION_PASSWORD_MASTER : SAVE_SESSION_PASSWORD_RECOMMENDED)));
  384. CreateShortcutCheck = CreateAndAddCheckBox(LoadStr(SAVE_SITE_WORKSPACE_SHORTCUT));
  385. EnableControl(SavePasswordCheck, CanSavePassword);
  386. }
  387. //---------------------------------------------------------------------------
  388. bool __fastcall TSaveSessionDialog::Execute(
  389. UnicodeString & SessionName, bool & SavePassword, bool & CreateShortcut,
  390. const UnicodeString & OriginalSessionName)
  391. {
  392. FOriginalSessionName = OriginalSessionName;
  393. SessionNameEdit->Text = TSessionData::ExtractLocalName(SessionName);
  394. UnicodeString Folder = TSessionData::ExtractFolderName(SessionName);
  395. if (Folder.IsEmpty())
  396. {
  397. FolderCombo->Text = FRootFolder;
  398. }
  399. else
  400. {
  401. FolderCombo->Text = Folder;
  402. }
  403. SavePasswordCheck->Checked = SavePassword;
  404. CreateShortcutCheck->Checked = CreateShortcut;
  405. bool Result = TCustomDialog::Execute();
  406. if (Result)
  407. {
  408. SessionName = GetSessionName();
  409. SavePassword = SavePasswordCheck->Checked;
  410. CreateShortcut = CreateShortcutCheck->Checked;
  411. }
  412. return Result;
  413. }
  414. //---------------------------------------------------------------------------
  415. UnicodeString __fastcall TSaveSessionDialog::GetSessionName()
  416. {
  417. UnicodeString Folder;
  418. if (FolderCombo->Text != FRootFolder)
  419. {
  420. Folder = FolderCombo->Text;
  421. }
  422. return TSessionData::ComposePath(Folder, SessionNameEdit->Text);
  423. }
  424. //---------------------------------------------------------------------------
  425. void __fastcall TSaveSessionDialog::DoValidate()
  426. {
  427. TSessionData::ValidateName(SessionNameEdit->Text);
  428. SessionNameValidate(GetSessionName(), FOriginalSessionName);
  429. UnicodeString Folder = TSessionData::ExtractFolderName(GetSessionName());
  430. if (!Folder.IsEmpty() && StoredSessions->IsWorkspace(Folder))
  431. {
  432. throw Exception(FMTLOAD(WORKSPACE_NOT_FOLDER, (Folder)));
  433. }
  434. if (SavePasswordCheck->Enabled && SavePasswordCheck->Checked &&
  435. CustomWinConfiguration->UseMasterPassword)
  436. {
  437. CustomWinConfiguration->AskForMasterPasswordIfNotSet();
  438. }
  439. TCustomDialog::DoValidate();
  440. }
  441. //---------------------------------------------------------------------------
  442. void __fastcall TSaveSessionDialog::DoChange(bool & CanSubmit)
  443. {
  444. CanSubmit = !SessionNameEdit->Text.IsEmpty();
  445. TCustomDialog::DoChange(CanSubmit);
  446. }
  447. //---------------------------------------------------------------------------
  448. TSessionData * __fastcall DoSaveSession(TSessionData * SessionData,
  449. TSessionData * OriginalSession, bool ForceDialog,
  450. TStrings * AdditionalFolders)
  451. {
  452. bool SavePassword = false;
  453. bool * PSavePassword;
  454. bool NotRecommendedSavingPassword =
  455. !CustomWinConfiguration->UseMasterPassword &&
  456. !SameText(SessionData->UserName, AnonymousUserName);
  457. if (Configuration->DisablePasswordStoring ||
  458. !SessionData->HasAnySessionPassword())
  459. {
  460. PSavePassword = NULL;
  461. }
  462. else
  463. {
  464. PSavePassword = &SavePassword;
  465. SavePassword =
  466. ((OriginalSession != NULL) && OriginalSession->HasAnySessionPassword()) ||
  467. !NotRecommendedSavingPassword;
  468. }
  469. UnicodeString SessionName = SessionData->SessionName;
  470. bool Result;
  471. bool CreateShortcut = false;
  472. if (!ForceDialog && ((PSavePassword == NULL) || SavePassword))
  473. {
  474. CustomWinConfiguration->AskForMasterPasswordIfNotSetAndNeededToPersistSessionData(SessionData);
  475. Result = true;
  476. }
  477. else
  478. {
  479. // This can be a standalone dialog when used with save URL (from GetLoginData)
  480. TSaveSessionDialog * Dialog = SafeFormCreate<TSaveSessionDialog>();
  481. try
  482. {
  483. Dialog->Init((PSavePassword != NULL), NotRecommendedSavingPassword, AdditionalFolders);
  484. Result = Dialog->Execute(SessionName, SavePassword, CreateShortcut, SessionData->Name);
  485. }
  486. __finally
  487. {
  488. delete Dialog;
  489. }
  490. }
  491. TSessionData * NewSession = NULL;
  492. if (Result)
  493. {
  494. if ((PSavePassword != NULL) && !SavePassword)
  495. {
  496. SessionData->ClearSessionPasswords();
  497. }
  498. NewSession =
  499. StoredSessions->NewSession(SessionName, SessionData);
  500. // modified only, explicit
  501. StoredSessions->Save(false, true);
  502. if (!SessionData->HostKey.IsEmpty())
  503. {
  504. SessionData->CacheHostKeyIfNotCached();
  505. }
  506. if (CreateShortcut)
  507. {
  508. TOperationVisualizer Visualizer;
  509. UnicodeString AdditionalParams =
  510. TProgramParams::FormatSwitch(DESKTOP_SWITCH) + L" " +
  511. TProgramParams::FormatSwitch(UPLOAD_IF_ANY_SWITCH);
  512. CreateDesktopSessionShortCut(SessionName, L"", AdditionalParams, -1, SITE_ICON);
  513. }
  514. }
  515. return NewSession;
  516. }
  517. //---------------------------------------------------------------------------
  518. void __fastcall SessionNameValidate(const UnicodeString & Text,
  519. const UnicodeString & OriginalName)
  520. {
  521. TSessionData::ValidatePath(Text);
  522. DebugAssert(StoredSessions);
  523. TSessionData * Data = (TSessionData *)StoredSessions->FindByName(Text);
  524. if (Data && Data->Special)
  525. {
  526. MessageDialog(FMTLOAD(CANNOT_OVERWRITE_SPECIAL_SESSION, (Text)),
  527. qtError, qaOK, HELP_NONE);
  528. Abort();
  529. }
  530. else if ((Data != NULL) && !Data->IsSameName(OriginalName) &&
  531. MessageDialog(MainInstructions(FMTLOAD(CONFIRM_OVERWRITE_SESSION, (Text))),
  532. qtConfirmation, qaYes | qaNo, HELP_SESSION_SAVE_OVERWRITE) != qaYes)
  533. {
  534. Abort();
  535. }
  536. }
  537. //---------------------------------------------------------------------------
  538. //---------------------------------------------------------------------------
  539. class TSaveWorkspaceDialog : public TCustomDialog
  540. {
  541. public:
  542. __fastcall TSaveWorkspaceDialog(bool CanSavePasswords,
  543. bool NotRecommendedSavingPasswords);
  544. bool __fastcall Execute(
  545. UnicodeString & WorkspaceName, bool & SavePasswords, bool & CreateShortcut,
  546. bool & EnableAutoSave);
  547. protected:
  548. virtual void __fastcall DoValidate();
  549. virtual void __fastcall DoChange(bool & CanSubmit);
  550. private:
  551. TComboBox * WorkspaceNameCombo;
  552. TCheckBox * SavePasswordsCheck;
  553. TCheckBox * CreateShortcutCheck;
  554. TCheckBox * EnableAutoSaveCheck;
  555. };
  556. //---------------------------------------------------------------------------
  557. __fastcall TSaveWorkspaceDialog::TSaveWorkspaceDialog(
  558. bool CanSavePasswords, bool NotRecommendedSavingPasswords) :
  559. TCustomDialog(HELP_WORKSPACE_SAVE)
  560. {
  561. Caption = LoadStr(SAVE_WORKSPACE_CAPTION);
  562. WorkspaceNameCombo = new TComboBox(this);
  563. WorkspaceNameCombo->AutoComplete = false;
  564. AddComboBox(WorkspaceNameCombo, CreateLabel(LoadStr(SAVE_WORKSPACE_PROMPT)));
  565. WorkspaceNameCombo->DropDownCount = Max(WorkspaceNameCombo->DropDownCount, 16);
  566. std::unique_ptr<TStrings> Workspaces(StoredSessions->GetWorkspaces());
  567. WorkspaceNameCombo->Items->AddStrings(Workspaces.get());
  568. SavePasswordsCheck = CreateAndAddCheckBox(
  569. LoadStr(NotRecommendedSavingPasswords ? SAVE_WORKSPACE_PASSWORDS :
  570. (CustomWinConfiguration->UseMasterPassword ?
  571. SAVE_WORKSPACE_PASSWORDS_MASTER : SAVE_WORKSPACE_PASSWORDS_RECOMMENDED)));
  572. EnableControl(SavePasswordsCheck, CanSavePasswords);
  573. CreateShortcutCheck = CreateAndAddCheckBox(LoadStr(SAVE_SITE_WORKSPACE_SHORTCUT));
  574. EnableAutoSaveCheck = CreateAndAddCheckBox(LoadStr(SAVE_WORKSPACE_AUTO));
  575. }
  576. //---------------------------------------------------------------------------
  577. bool __fastcall TSaveWorkspaceDialog::Execute(
  578. UnicodeString & WorkspaceName, bool & SavePasswords, bool & CreateShortcut,
  579. bool & EnableAutoSave)
  580. {
  581. WorkspaceNameCombo->Text = WorkspaceName;
  582. SavePasswordsCheck->Checked = SavePasswords;
  583. CreateShortcutCheck->Checked = CreateShortcut;
  584. EnableAutoSaveCheck->Checked = EnableAutoSave;
  585. bool Result = TCustomDialog::Execute();
  586. if (Result)
  587. {
  588. WorkspaceName = WorkspaceNameCombo->Text;
  589. SavePasswords = SavePasswordsCheck->Checked;
  590. CreateShortcut = CreateShortcutCheck->Checked;
  591. EnableAutoSave = EnableAutoSaveCheck->Checked;
  592. }
  593. return Result;
  594. }
  595. //---------------------------------------------------------------------------
  596. void __fastcall TSaveWorkspaceDialog::DoValidate()
  597. {
  598. TSessionData::ValidateName(WorkspaceNameCombo->Text);
  599. if (StoredSessions->IsFolder(WorkspaceNameCombo->Text))
  600. {
  601. throw Exception(FMTLOAD(FOLDER_NOT_WORKSPACE, (WorkspaceNameCombo->Text)));
  602. }
  603. if (SavePasswordsCheck->Enabled && SavePasswordsCheck->Checked &&
  604. CustomWinConfiguration->UseMasterPassword)
  605. {
  606. CustomWinConfiguration->AskForMasterPasswordIfNotSet();
  607. }
  608. TCustomDialog::DoValidate();
  609. }
  610. //---------------------------------------------------------------------------
  611. void __fastcall TSaveWorkspaceDialog::DoChange(bool & CanSubmit)
  612. {
  613. CanSubmit = !WorkspaceNameCombo->Text.IsEmpty();
  614. TCustomDialog::DoChange(CanSubmit);
  615. }
  616. //---------------------------------------------------------------------------
  617. bool __fastcall DoSaveWorkspaceDialog(UnicodeString & WorkspaceName,
  618. bool * SavePasswords, bool NotRecommendedSavingPasswords,
  619. bool & CreateShortcut, bool & EnableAutoSave)
  620. {
  621. std::unique_ptr<TSaveWorkspaceDialog> Dialog(
  622. new TSaveWorkspaceDialog((SavePasswords != NULL), NotRecommendedSavingPasswords));
  623. bool Dummy = false;
  624. if (SavePasswords == NULL)
  625. {
  626. SavePasswords = &Dummy;
  627. }
  628. return
  629. Dialog->Execute(
  630. WorkspaceName, *SavePasswords, CreateShortcut, EnableAutoSave);
  631. }
  632. //---------------------------------------------------------------------------
  633. //---------------------------------------------------------------------------
  634. class TShortCutDialog : public TCustomDialog
  635. {
  636. public:
  637. __fastcall TShortCutDialog(const TShortCuts & ShortCuts, UnicodeString HelpKeyword);
  638. bool __fastcall Execute(TShortCut & ShortCut);
  639. private:
  640. TComboBox * ShortCutCombo;
  641. };
  642. //---------------------------------------------------------------------------
  643. __fastcall TShortCutDialog::TShortCutDialog(const TShortCuts & ShortCuts, UnicodeString HelpKeyword) :
  644. TCustomDialog(HelpKeyword)
  645. {
  646. Caption = LoadStr(SHORTCUT_CAPTION);
  647. ShortCutCombo = new TComboBox(this);
  648. AddComboBox(ShortCutCombo, CreateLabel(LoadStr(SHORTCUT_LABEL)));
  649. InitializeShortCutCombo(ShortCutCombo, ShortCuts);
  650. }
  651. //---------------------------------------------------------------------------
  652. bool __fastcall TShortCutDialog::Execute(TShortCut & ShortCut)
  653. {
  654. SetShortCutCombo(ShortCutCombo, ShortCut);
  655. bool Result = TCustomDialog::Execute();
  656. if (Result)
  657. {
  658. ShortCut = GetShortCutCombo(ShortCutCombo);
  659. }
  660. return Result;
  661. }
  662. //---------------------------------------------------------------------------
  663. bool __fastcall DoShortCutDialog(TShortCut & ShortCut,
  664. const TShortCuts & ShortCuts, UnicodeString HelpKeyword)
  665. {
  666. bool Result;
  667. TShortCutDialog * Dialog = new TShortCutDialog(ShortCuts, HelpKeyword);
  668. try
  669. {
  670. Result = Dialog->Execute(ShortCut);
  671. }
  672. __finally
  673. {
  674. delete Dialog;
  675. }
  676. return Result;
  677. }
  678. //---------------------------------------------------------------------------
  679. //---------------------------------------------------------------------------
  680. class TRemoteMoveDialog : public TCustomDialog
  681. {
  682. public:
  683. __fastcall TRemoteMoveDialog(bool Multi);
  684. bool __fastcall Execute(UnicodeString & Target, UnicodeString & FileMask);
  685. protected:
  686. DYNAMIC void __fastcall DoShow();
  687. virtual void __fastcall DoValidate();
  688. UnicodeString __fastcall GetFileMask();
  689. private:
  690. THistoryComboBox * Combo;
  691. bool FMulti;
  692. };
  693. //---------------------------------------------------------------------------
  694. __fastcall TRemoteMoveDialog::TRemoteMoveDialog(bool Multi) :
  695. TCustomDialog(HELP_REMOTE_MOVE)
  696. {
  697. Caption = LoadStr(REMOTE_MOVE_TITLE);
  698. // The same as TRemoteTransferDialog
  699. ClientWidth = ScaleByTextHeight(this, 420);
  700. FMulti = Multi;
  701. AddImage(L"Move To");
  702. Combo = new THistoryComboBox(this);
  703. Combo->AutoComplete = false;
  704. AddComboBox(Combo, CreateLabel(LoadStr(REMOTE_TRANSFER_PROMPT2)));
  705. }
  706. //---------------------------------------------------------------------------
  707. bool __fastcall TRemoteMoveDialog::Execute(UnicodeString & Target, UnicodeString & FileMask)
  708. {
  709. Combo->Items = CustomWinConfiguration->History[L"RemoteTarget"];
  710. Combo->Text = UnixIncludeTrailingBackslash(Target) + FileMask;
  711. bool Result = TCustomDialog::Execute();
  712. if (Result)
  713. {
  714. Target = UnixExtractFilePath(Combo->Text);
  715. FileMask = GetFileMask();
  716. Combo->SaveToHistory();
  717. CustomWinConfiguration->History[L"RemoteTarget"] = Combo->Items;
  718. }
  719. return Result;
  720. }
  721. //---------------------------------------------------------------------------
  722. UnicodeString __fastcall TRemoteMoveDialog::GetFileMask()
  723. {
  724. return UnixExtractFileName(Combo->Text);
  725. }
  726. //---------------------------------------------------------------------------
  727. void __fastcall TRemoteMoveDialog::DoShow()
  728. {
  729. TCustomDialog::DoShow();
  730. InstallPathWordBreakProc(Combo);
  731. }
  732. //---------------------------------------------------------------------------
  733. void __fastcall TRemoteMoveDialog::DoValidate()
  734. {
  735. if (!IsFileNameMask(GetFileMask()) && FMulti)
  736. {
  737. UnicodeString Message =
  738. FormatMultiFilesToOneConfirmation(Combo->Text, true);
  739. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_NONE) == qaCancel)
  740. {
  741. Abort();
  742. }
  743. }
  744. TCustomDialog::DoValidate();
  745. }
  746. //---------------------------------------------------------------------------
  747. bool __fastcall DoRemoteMoveDialog(bool Multi, UnicodeString & Target, UnicodeString & FileMask)
  748. {
  749. std::unique_ptr<TRemoteMoveDialog> Dialog(new TRemoteMoveDialog(Multi));
  750. return Dialog->Execute(Target, FileMask);
  751. }
  752. //---------------------------------------------------------------------------
  753. //---------------------------------------------------------------------------
  754. class TCustomCommandOptionsDialog : public TCustomDialog
  755. {
  756. public:
  757. __fastcall TCustomCommandOptionsDialog(
  758. const TCustomCommandType * Command, TStrings * CustomCommandOptions, unsigned int Flags,
  759. TCustomCommand * CustomCommandForOptions);
  760. bool __fastcall Execute();
  761. protected:
  762. virtual void __fastcall DoHelp();
  763. DYNAMIC void __fastcall DoShow();
  764. private:
  765. const TCustomCommandType * FCommand;
  766. TStrings * FCustomCommandOptions;
  767. std::vector<TControl *> FControls;
  768. std::vector<std::vector<UnicodeString> > FValues;
  769. unsigned int FFlags;
  770. UnicodeString __fastcall HistoryKey(const TCustomCommandType::TOption & Option);
  771. THistoryComboBox * __fastcall CreateHistoryComboBox(const TCustomCommandType::TOption & Option, const UnicodeString & Value);
  772. void __fastcall BrowseButtonClick(TObject * Sender);
  773. void __fastcall LinkLabelClick(TObject * Sender);
  774. UnicodeString __fastcall SaveHistoryComboBoxValue(TControl * Control, const TCustomCommandType::TOption & Option);
  775. void __fastcall AddOptionComboBox(
  776. TComboBox * ComboBox, const UnicodeString & Value, const TCustomCommandType::TOption & Option,
  777. std::vector<UnicodeString> & Values);
  778. UnicodeString __fastcall GetComboBoxValue(TControl * Control, const UnicodeString & Default);
  779. int __fastcall GetOptionIndex(TControl * Control);
  780. int __fastcall GetControlIndex(TControl * Control);
  781. };
  782. //---------------------------------------------------------------------------
  783. __fastcall TCustomCommandOptionsDialog::TCustomCommandOptionsDialog(
  784. const TCustomCommandType * Command, TStrings * CustomCommandOptions,
  785. unsigned int Flags, TCustomCommand * CustomCommandForOptions) :
  786. TCustomDialog(HELP_EXTENSION_OPTIONS)
  787. {
  788. FCommand = Command;
  789. FFlags = Flags;
  790. FCustomCommandOptions = CustomCommandOptions;
  791. Caption = FMTLOAD(EXTENSION_OPTIONS_CAPTION, (StripEllipsis(StripHotkey(FCommand->Name))));
  792. Width = ScaleByTextHeight(this, 400);
  793. int ControlIndex = 0;
  794. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  795. {
  796. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  797. if ((Option.Flags & FFlags) != 0)
  798. {
  799. UnicodeString OptionKey = FCommand->GetOptionKey(Option);
  800. UnicodeString Value;
  801. if ((CustomCommandForOptions != NULL) &&
  802. Option.HasPatterns(CustomCommandForOptions))
  803. {
  804. Value = CustomCommandForOptions->Complete(Option.Default, true);
  805. }
  806. else
  807. {
  808. if (FCustomCommandOptions->IndexOfName(OptionKey) >= 0)
  809. {
  810. Value = FCustomCommandOptions->Values[OptionKey];
  811. }
  812. else
  813. {
  814. Value = Option.Default;
  815. }
  816. }
  817. int Tag = (OptionIndex << 16) + ControlIndex;
  818. TControl * Control = NULL;
  819. std::vector<UnicodeString> Values;
  820. if (Option.Kind == TCustomCommandType::okUnknown)
  821. {
  822. Control = NULL;
  823. }
  824. else if (Option.Kind == TCustomCommandType::okLabel)
  825. {
  826. TLabel * Label = CreateLabel(Option.Caption);
  827. AddText(Label);
  828. Control = Label;
  829. }
  830. else if (Option.Kind == TCustomCommandType::okLink)
  831. {
  832. TStaticText * Label = new TStaticText(this);
  833. Label->Caption = Option.Caption;
  834. if (IsHttpOrHttpsUrl(Label->Caption))
  835. {
  836. Label->Caption = SecureUrl(Label->Caption);
  837. LinkLabel(Label);
  838. Label->TabStop = true;
  839. }
  840. else if (!Option.Default.IsEmpty() && IsHttpOrHttpsUrl(Option.Default))
  841. {
  842. Label->OnClick = LinkLabelClick;
  843. LinkLabel(Label);
  844. Label->TabStop = true;
  845. }
  846. else
  847. {
  848. // keep it plain text, as we have no URL
  849. }
  850. AddText(Label);
  851. Control = Label;
  852. }
  853. else if (Option.Kind == TCustomCommandType::okGroup)
  854. {
  855. StartGroup(Option.Caption);
  856. }
  857. else if (Option.Kind == TCustomCommandType::okSeparator)
  858. {
  859. AddSeparator();
  860. }
  861. else if (Option.Kind == TCustomCommandType::okTextBox)
  862. {
  863. Control = CreateHistoryComboBox(Option, Value);
  864. }
  865. else if (Option.Kind == TCustomCommandType::okFile)
  866. {
  867. THistoryComboBox * ComboBox = CreateHistoryComboBox(Option, Value);
  868. TButton * Button = new TButton(this);
  869. Button->Parent = GetDefaultParent();
  870. Button->Width = HelpButton->Width;
  871. Button->Left = GetDefaultParent()->ClientWidth - Button->Width - HorizontalMargin;
  872. ComboBox->Width = Button->Left - ComboBox->Left - ScaleByTextHeight(this, 6);
  873. Button->Top = ComboBox->Top - ScaleByTextHeight(this, 2);
  874. Button->Tag = Tag;
  875. Button->Caption = LoadStr(EXTENSION_OPTIONS_BROWSE);
  876. Button->OnClick = BrowseButtonClick;
  877. ScaleButtonControl(Button);
  878. AddWinControl(Button);
  879. Control = ComboBox;
  880. }
  881. else if (Option.Kind == TCustomCommandType::okDropDownList)
  882. {
  883. TComboBox * ComboBox = new TComboBox(this);
  884. ComboBox->Style = csDropDownList;
  885. AddOptionComboBox(ComboBox, Value, Option, Values);
  886. Control = ComboBox;
  887. }
  888. else if (Option.Kind == TCustomCommandType::okComboBox)
  889. {
  890. TComboBox * ComboBox = new TComboBox(this);
  891. ComboBox->Style = csDropDown;
  892. AddOptionComboBox(ComboBox, Value, Option, Values);
  893. if (ComboBox->ItemIndex < 0)
  894. {
  895. ComboBox->Text = Value;
  896. }
  897. Control = ComboBox;
  898. }
  899. else if (Option.Kind == TCustomCommandType::okCheckBox)
  900. {
  901. TCheckBox * CheckBox = CreateAndAddCheckBox(Option.Caption);
  902. CheckBox->Checked =
  903. (Option.Params.size() >= 1) &&
  904. (Value == Option.Params[0]);
  905. Control = CheckBox;
  906. }
  907. else
  908. {
  909. DebugFail();
  910. }
  911. if (Control != NULL)
  912. {
  913. Control->Tag = Tag;
  914. }
  915. FControls.push_back(Control);
  916. FValues.push_back(Values);
  917. ControlIndex++;
  918. DebugAssert(static_cast<int>(FControls.size()) == ControlIndex);
  919. }
  920. }
  921. }
  922. //---------------------------------------------------------------------------
  923. void __fastcall TCustomCommandOptionsDialog::AddOptionComboBox(
  924. TComboBox * ComboBox, const UnicodeString & Value, const TCustomCommandType::TOption & Option, std::vector<UnicodeString> & Values)
  925. {
  926. std::unique_ptr<TStringList> Items(new TStringList());
  927. int ItemIndex = -1;
  928. TCustomCommandType::TOption::TParams::const_iterator ParamI = Option.Params.begin();
  929. while (ParamI != Option.Params.end())
  930. {
  931. UnicodeString Item = (*ParamI);
  932. int P = Item.Pos(L"=");
  933. UnicodeString ParamValue;
  934. if (P > 0)
  935. {
  936. ParamValue = Item.SubString(1, P - 1);
  937. Item.Delete(1, P);
  938. }
  939. else
  940. {
  941. ParamValue = Item;
  942. }
  943. Items->Add(Item);
  944. if (Value == ParamValue)
  945. {
  946. ItemIndex = Items->Count - 1;
  947. }
  948. Values.push_back(ParamValue);
  949. ParamI++;
  950. }
  951. AddComboBox(ComboBox, CreateLabel(Option.Caption), Items.get(), true);
  952. ComboBox->ItemIndex = ItemIndex;
  953. }
  954. //---------------------------------------------------------------------------
  955. int __fastcall TCustomCommandOptionsDialog::GetOptionIndex(TControl * Control)
  956. {
  957. return (Control->Tag >> 16);
  958. }
  959. //---------------------------------------------------------------------------
  960. int __fastcall TCustomCommandOptionsDialog::GetControlIndex(TControl * Control)
  961. {
  962. return (Control->Tag & 0xFFFF);
  963. }
  964. //---------------------------------------------------------------------------
  965. void __fastcall TCustomCommandOptionsDialog::LinkLabelClick(TObject * Sender)
  966. {
  967. TStaticText * Label = DebugNotNull(dynamic_cast<TStaticText *>(Sender));
  968. const TCustomCommandType::TOption & Option = FCommand->GetOption(GetOptionIndex(Label));
  969. OpenBrowser(SecureUrl(Option.Default));
  970. }
  971. //---------------------------------------------------------------------------
  972. void __fastcall TCustomCommandOptionsDialog::BrowseButtonClick(TObject * Sender)
  973. {
  974. TButton * Button = DebugNotNull(dynamic_cast<TButton *>(Sender));
  975. int OptionIndex = GetOptionIndex(Button);
  976. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  977. int ControlIndex = GetControlIndex(Button);
  978. THistoryComboBox * ComboBox = dynamic_cast<THistoryComboBox *>(FControls[ControlIndex]);
  979. std::unique_ptr<TOpenDialog> OpenDialog(new TOpenDialog(Application));
  980. UnicodeString Title;
  981. if (!Option.FileCaption.IsEmpty())
  982. {
  983. Title = Option.FileCaption;
  984. }
  985. else
  986. {
  987. UnicodeString Caption = Option.Caption;
  988. Caption = StripHotkey(Caption);
  989. if (!Caption.IsEmpty() && (Caption[Caption.Length()] == L':'))
  990. {
  991. Caption.SetLength(Caption.Length() - 1);
  992. }
  993. Title = FMTLOAD(EXTENSION_OPTIONS_BROWSE_TITLE, (Caption));
  994. }
  995. OpenDialog->Title = Title;
  996. UnicodeString Value;
  997. if (ComboBox->Text.IsEmpty())
  998. {
  999. Value = Option.FileInitial;
  1000. }
  1001. else
  1002. {
  1003. Value = ComboBox->Text;
  1004. }
  1005. UnicodeString ExpandedValue = ExpandEnvironmentVariables(Value);
  1006. OpenDialog->FileName = ExpandedValue;
  1007. UnicodeString InitialDir = ExtractFilePath(ExpandedValue);
  1008. if (!InitialDir.IsEmpty())
  1009. {
  1010. OpenDialog->InitialDir = InitialDir;
  1011. }
  1012. OpenDialog->Filter = Option.FileFilter;
  1013. OpenDialog->DefaultExt = Option.FileExt;
  1014. if (OpenDialog->Execute())
  1015. {
  1016. if (OpenDialog->FileName != ExpandedValue)
  1017. {
  1018. ComboBox->Text = OpenDialog->FileName;
  1019. }
  1020. // If user just confirms the initial value, persist it
  1021. else if (ComboBox->Text.IsEmpty())
  1022. {
  1023. DebugAssert(Option.FileInitial == Value);
  1024. ComboBox->Text = Value;
  1025. }
  1026. }
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. THistoryComboBox * __fastcall TCustomCommandOptionsDialog::CreateHistoryComboBox(
  1030. const TCustomCommandType::TOption & Option, const UnicodeString & Value)
  1031. {
  1032. THistoryComboBox * ComboBox = new THistoryComboBox(this);
  1033. ComboBox->AutoComplete = false;
  1034. AddComboBox(ComboBox, CreateLabel(Option.Caption));
  1035. ComboBox->Items = CustomWinConfiguration->History[HistoryKey(Option)];
  1036. ComboBox->Text = Value;
  1037. return ComboBox;
  1038. }
  1039. //---------------------------------------------------------------------------
  1040. UnicodeString __fastcall TCustomCommandOptionsDialog::HistoryKey(const TCustomCommandType::TOption & Option)
  1041. {
  1042. UnicodeString Result = FCommand->GetOptionKey(Option);
  1043. Result = CustomWinConfiguration->GetValidHistoryKey(Result);
  1044. return L"CustomCommandOption_" + Result;
  1045. }
  1046. //---------------------------------------------------------------------------
  1047. bool __fastcall TCustomCommandOptionsDialog::Execute()
  1048. {
  1049. bool Result = TCustomDialog::Execute();
  1050. if (Result)
  1051. {
  1052. int ControlIndex = 0;
  1053. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  1054. {
  1055. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1056. if ((Option.Flags & FFlags) != 0)
  1057. {
  1058. if ((Option.Kind != TCustomCommandType::okUnknown) &&
  1059. Option.IsControl)
  1060. {
  1061. UnicodeString OptionKey = FCommand->GetOptionKey(Option);
  1062. TControl * Control = FControls[ControlIndex];
  1063. UnicodeString Value;
  1064. if (Option.Kind == TCustomCommandType::okTextBox)
  1065. {
  1066. Value = SaveHistoryComboBoxValue(Control, Option);
  1067. }
  1068. else if (Option.Kind == TCustomCommandType::okFile)
  1069. {
  1070. Value = SaveHistoryComboBoxValue(Control, Option);
  1071. }
  1072. else if (Option.Kind == TCustomCommandType::okDropDownList)
  1073. {
  1074. Value = GetComboBoxValue(Control, Option.Default);
  1075. }
  1076. else if (Option.Kind == TCustomCommandType::okComboBox)
  1077. {
  1078. TComboBox * ComboBox = DebugNotNull(dynamic_cast<TComboBox *>(Control));
  1079. Value = GetComboBoxValue(Control, ComboBox->Text);
  1080. }
  1081. else if (Option.Kind == TCustomCommandType::okCheckBox)
  1082. {
  1083. TCheckBox * CheckBox = DebugNotNull(dynamic_cast<TCheckBox *>(Control));
  1084. int Index = (CheckBox->Checked ? 0 : 1);
  1085. Value = (Index < static_cast<int>(Option.Params.size())) ? Option.Params[Index] : UnicodeString();
  1086. }
  1087. else
  1088. {
  1089. DebugFail();
  1090. }
  1091. // The default value setter deletes the "name" when the value is empty.
  1092. // It would cause us to fall back to the default value, but we want to remember the empty value.
  1093. if (Value.IsEmpty())
  1094. {
  1095. int Index = FCustomCommandOptions->IndexOfName(OptionKey);
  1096. if (Index < 0)
  1097. {
  1098. Index = FCustomCommandOptions->Add(L"");
  1099. }
  1100. UnicodeString Line = OptionKey + FCustomCommandOptions->NameValueSeparator;
  1101. FCustomCommandOptions->Strings[Index] = Line;
  1102. }
  1103. else
  1104. {
  1105. FCustomCommandOptions->Values[OptionKey] = Value;
  1106. }
  1107. }
  1108. ControlIndex++;
  1109. }
  1110. }
  1111. }
  1112. return Result;
  1113. }
  1114. //---------------------------------------------------------------------------
  1115. UnicodeString __fastcall TCustomCommandOptionsDialog::GetComboBoxValue(
  1116. TControl * Control, const UnicodeString & Default)
  1117. {
  1118. TComboBox * ComboBox = DebugNotNull(dynamic_cast<TComboBox *>(Control));
  1119. UnicodeString Result;
  1120. if (ComboBox->ItemIndex < 0)
  1121. {
  1122. Result = Default;
  1123. }
  1124. else
  1125. {
  1126. Result = FValues[GetControlIndex(Control)][ComboBox->ItemIndex];
  1127. }
  1128. return Result;
  1129. }
  1130. //---------------------------------------------------------------------------
  1131. UnicodeString __fastcall TCustomCommandOptionsDialog::SaveHistoryComboBoxValue(
  1132. TControl * Control, const TCustomCommandType::TOption & Option)
  1133. {
  1134. THistoryComboBox * ComboBox = DebugNotNull(dynamic_cast<THistoryComboBox *>(Control));
  1135. ComboBox->SaveToHistory();
  1136. CustomWinConfiguration->History[HistoryKey(Option)] = ComboBox->Items;
  1137. return ComboBox->Text;
  1138. }
  1139. //---------------------------------------------------------------------------
  1140. void __fastcall TCustomCommandOptionsDialog::DoHelp()
  1141. {
  1142. UnicodeString HelpPage;
  1143. if (!FCommand->OptionsPage.IsEmpty())
  1144. {
  1145. HelpPage = FCommand->OptionsPage;
  1146. }
  1147. else
  1148. {
  1149. HelpPage = FCommand->HomePage;
  1150. }
  1151. if (!HelpPage.IsEmpty())
  1152. {
  1153. OpenBrowser(HelpPage);
  1154. }
  1155. else
  1156. {
  1157. TCustomDialog::DoHelp();
  1158. }
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. void __fastcall TCustomCommandOptionsDialog::DoShow()
  1162. {
  1163. TCustomDialog::DoShow();
  1164. int ControlIndex = 0;
  1165. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  1166. {
  1167. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1168. if ((Option.Flags & FFlags) != 0)
  1169. {
  1170. if (Option.Kind == TCustomCommandType::okFile)
  1171. {
  1172. TControl * Control = FControls[ControlIndex];
  1173. InstallPathWordBreakProc(DebugNotNull(dynamic_cast<TWinControl *>(Control)));
  1174. }
  1175. ControlIndex++;
  1176. }
  1177. }
  1178. }
  1179. //---------------------------------------------------------------------------
  1180. bool __fastcall DoCustomCommandOptionsDialog(
  1181. const TCustomCommandType * Command, TStrings * CustomCommandOptions,
  1182. unsigned int Flags, TCustomCommand * CustomCommandForOptions)
  1183. {
  1184. std::unique_ptr<TCustomCommandOptionsDialog> Dialog(
  1185. new TCustomCommandOptionsDialog(Command, CustomCommandOptions, Flags, CustomCommandForOptions));
  1186. return Dialog->Execute();
  1187. }