Custom.cpp 47 KB

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