1
0

Custom.cpp 45 KB

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