Custom.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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. Item = WinConfiguration->ExtensionStringTranslation(FCommand->Id, Item);
  999. Items->Add(Item);
  1000. if (Value == ParamValue)
  1001. {
  1002. ItemIndex = Items->Count - 1;
  1003. }
  1004. Values.push_back(ParamValue);
  1005. ParamI++;
  1006. }
  1007. AddComboBox(ComboBox, CreateLabel(Option.Caption), Items.get(), true);
  1008. ComboBox->ItemIndex = ItemIndex;
  1009. }
  1010. //---------------------------------------------------------------------------
  1011. int __fastcall TCustomCommandOptionsDialog::GetOptionIndex(TControl * Control)
  1012. {
  1013. return (Control->Tag >> 16);
  1014. }
  1015. //---------------------------------------------------------------------------
  1016. int __fastcall TCustomCommandOptionsDialog::GetControlIndex(TControl * Control)
  1017. {
  1018. return (Control->Tag & 0xFFFF);
  1019. }
  1020. //---------------------------------------------------------------------------
  1021. void __fastcall TCustomCommandOptionsDialog::LinkLabelClick(TObject * Sender)
  1022. {
  1023. TStaticText * Label = DebugNotNull(dynamic_cast<TStaticText *>(Sender));
  1024. const TCustomCommandType::TOption & Option = FCommand->GetOption(GetOptionIndex(Label));
  1025. OpenBrowser(SecureUrl(Option.Default));
  1026. }
  1027. //---------------------------------------------------------------------------
  1028. void __fastcall TCustomCommandOptionsDialog::BrowseButtonClick(TObject * Sender)
  1029. {
  1030. TButton * Button = DebugNotNull(dynamic_cast<TButton *>(Sender));
  1031. int OptionIndex = GetOptionIndex(Button);
  1032. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1033. int ControlIndex = GetControlIndex(Button);
  1034. THistoryComboBox * ComboBox = dynamic_cast<THistoryComboBox *>(FControls[ControlIndex]);
  1035. std::unique_ptr<TOpenDialog> OpenDialog(new TOpenDialog(Application));
  1036. UnicodeString Title;
  1037. if (!Option.FileCaption.IsEmpty())
  1038. {
  1039. Title = Option.FileCaption;
  1040. }
  1041. else
  1042. {
  1043. UnicodeString Caption = Option.Caption;
  1044. Caption = StripHotkey(Caption);
  1045. if (!Caption.IsEmpty() && (Caption[Caption.Length()] == L':'))
  1046. {
  1047. Caption.SetLength(Caption.Length() - 1);
  1048. }
  1049. Title = FMTLOAD(EXTENSION_OPTIONS_BROWSE_TITLE, (Caption));
  1050. }
  1051. OpenDialog->Title = Title;
  1052. UnicodeString Value;
  1053. if (ComboBox->Text.IsEmpty())
  1054. {
  1055. Value = Option.FileInitial;
  1056. }
  1057. else
  1058. {
  1059. Value = ComboBox->Text;
  1060. }
  1061. UnicodeString ExpandedValue = ExpandEnvironmentVariables(Value);
  1062. OpenDialog->FileName = ExpandedValue;
  1063. UnicodeString InitialDir = ExtractFilePath(ExpandedValue);
  1064. if (!InitialDir.IsEmpty())
  1065. {
  1066. OpenDialog->InitialDir = InitialDir;
  1067. }
  1068. OpenDialog->Filter = Option.FileFilter;
  1069. OpenDialog->DefaultExt = Option.FileExt;
  1070. if (OpenDialog->Execute())
  1071. {
  1072. if (OpenDialog->FileName != ExpandedValue)
  1073. {
  1074. ComboBox->Text = OpenDialog->FileName;
  1075. }
  1076. // If user just confirms the initial value, persist it
  1077. else if (ComboBox->Text.IsEmpty())
  1078. {
  1079. DebugAssert(Option.FileInitial == Value);
  1080. ComboBox->Text = Value;
  1081. }
  1082. }
  1083. }
  1084. //---------------------------------------------------------------------------
  1085. THistoryComboBox * __fastcall TCustomCommandOptionsDialog::CreateHistoryComboBox(
  1086. const TCustomCommandType::TOption & Option, const UnicodeString & Value)
  1087. {
  1088. THistoryComboBox * ComboBox = new THistoryComboBox(this);
  1089. ComboBox->AutoComplete = false;
  1090. AddComboBox(ComboBox, CreateLabel(Option.Caption));
  1091. ComboBox->Items = CustomWinConfiguration->History[HistoryKey(Option)];
  1092. ComboBox->Text = Value;
  1093. return ComboBox;
  1094. }
  1095. //---------------------------------------------------------------------------
  1096. UnicodeString __fastcall TCustomCommandOptionsDialog::HistoryKey(const TCustomCommandType::TOption & Option)
  1097. {
  1098. UnicodeString Result = FCommand->GetOptionKey(Option, FSite);
  1099. Result = CustomWinConfiguration->GetValidHistoryKey(Result);
  1100. return L"CustomCommandOption_" + Result;
  1101. }
  1102. //---------------------------------------------------------------------------
  1103. bool __fastcall TCustomCommandOptionsDialog::Execute(TShortCut * ShortCut)
  1104. {
  1105. if (ShortCut != NULL)
  1106. {
  1107. SetShortCutCombo(FShortCutCombo, *ShortCut);
  1108. }
  1109. bool Result = TCustomDialog::Execute();
  1110. if (Result)
  1111. {
  1112. int ControlIndex = 0;
  1113. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  1114. {
  1115. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1116. if ((Option.Flags & FFlags) != 0)
  1117. {
  1118. if ((Option.Kind != TCustomCommandType::okUnknown) &&
  1119. Option.IsControl)
  1120. {
  1121. UnicodeString OptionKey = FCommand->GetOptionKey(Option, FSite);
  1122. TControl * Control = FControls[ControlIndex];
  1123. UnicodeString Value;
  1124. if (Option.Kind == TCustomCommandType::okTextBox)
  1125. {
  1126. Value = SaveHistoryComboBoxValue(Control, Option);
  1127. }
  1128. else if (Option.Kind == TCustomCommandType::okFile)
  1129. {
  1130. Value = SaveHistoryComboBoxValue(Control, Option);
  1131. }
  1132. else if (Option.Kind == TCustomCommandType::okDropDownList)
  1133. {
  1134. Value = GetComboBoxValue(Control, Option.Default);
  1135. }
  1136. else if (Option.Kind == TCustomCommandType::okComboBox)
  1137. {
  1138. TComboBox * ComboBox = DebugNotNull(dynamic_cast<TComboBox *>(Control));
  1139. Value = GetComboBoxValue(Control, ComboBox->Text);
  1140. }
  1141. else if (Option.Kind == TCustomCommandType::okCheckBox)
  1142. {
  1143. TCheckBox * CheckBox = DebugNotNull(dynamic_cast<TCheckBox *>(Control));
  1144. int Index = (CheckBox->Checked ? 0 : 1);
  1145. Value = (Index < static_cast<int>(Option.Params.size())) ? Option.Params[Index] : UnicodeString();
  1146. }
  1147. else
  1148. {
  1149. DebugFail();
  1150. }
  1151. // The default value setter deletes the "name" when the value is empty.
  1152. // It would cause us to fall back to the default value, but we want to remember the empty value.
  1153. if (Value.IsEmpty())
  1154. {
  1155. int Index = FCustomCommandOptions->IndexOfName(OptionKey);
  1156. if (Index < 0)
  1157. {
  1158. Index = FCustomCommandOptions->Add(L"");
  1159. }
  1160. UnicodeString Line = OptionKey + FCustomCommandOptions->NameValueSeparator;
  1161. FCustomCommandOptions->Strings[Index] = Line;
  1162. }
  1163. else
  1164. {
  1165. FCustomCommandOptions->Values[OptionKey] = Value;
  1166. }
  1167. }
  1168. ControlIndex++;
  1169. }
  1170. }
  1171. if (ShortCut != NULL)
  1172. {
  1173. *ShortCut = GetShortCutCombo(FShortCutCombo);
  1174. }
  1175. }
  1176. return Result;
  1177. }
  1178. //---------------------------------------------------------------------------
  1179. UnicodeString __fastcall TCustomCommandOptionsDialog::GetComboBoxValue(
  1180. TControl * Control, const UnicodeString & Default)
  1181. {
  1182. TComboBox * ComboBox = DebugNotNull(dynamic_cast<TComboBox *>(Control));
  1183. UnicodeString Result;
  1184. if (ComboBox->ItemIndex < 0)
  1185. {
  1186. Result = Default;
  1187. }
  1188. else
  1189. {
  1190. Result = FValues[GetControlIndex(Control)][ComboBox->ItemIndex];
  1191. }
  1192. return Result;
  1193. }
  1194. //---------------------------------------------------------------------------
  1195. UnicodeString __fastcall TCustomCommandOptionsDialog::SaveHistoryComboBoxValue(
  1196. TControl * Control, const TCustomCommandType::TOption & Option)
  1197. {
  1198. THistoryComboBox * ComboBox = DebugNotNull(dynamic_cast<THistoryComboBox *>(Control));
  1199. ComboBox->SaveToHistory();
  1200. CustomWinConfiguration->History[HistoryKey(Option)] = ComboBox->Items;
  1201. return ComboBox->Text;
  1202. }
  1203. //---------------------------------------------------------------------------
  1204. void __fastcall TCustomCommandOptionsDialog::DoHelp()
  1205. {
  1206. UnicodeString HelpPage;
  1207. if (!FCommand->OptionsPage.IsEmpty())
  1208. {
  1209. HelpPage = FCommand->OptionsPage;
  1210. }
  1211. else
  1212. {
  1213. HelpPage = FCommand->HomePage;
  1214. }
  1215. if (!HelpPage.IsEmpty())
  1216. {
  1217. OpenBrowser(HelpPage);
  1218. }
  1219. else
  1220. {
  1221. TCustomDialog::DoHelp();
  1222. }
  1223. }
  1224. //---------------------------------------------------------------------------
  1225. void __fastcall TCustomCommandOptionsDialog::DoShow()
  1226. {
  1227. TCustomDialog::DoShow();
  1228. int ControlIndex = 0;
  1229. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  1230. {
  1231. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1232. if ((Option.Flags & FFlags) != 0)
  1233. {
  1234. if (Option.Kind == TCustomCommandType::okFile)
  1235. {
  1236. TControl * Control = FControls[ControlIndex];
  1237. InstallPathWordBreakProc(DebugNotNull(dynamic_cast<TWinControl *>(Control)));
  1238. }
  1239. ControlIndex++;
  1240. }
  1241. }
  1242. }
  1243. //---------------------------------------------------------------------------
  1244. bool __fastcall DoCustomCommandOptionsDialog(
  1245. const TCustomCommandType * Command, TStrings * CustomCommandOptions, TShortCut * ShortCut,
  1246. unsigned int Flags, TCustomCommand * CustomCommandForOptions,
  1247. const UnicodeString & Site, const TShortCuts * ShortCuts)
  1248. {
  1249. std::unique_ptr<TCustomCommandOptionsDialog> Dialog(
  1250. new TCustomCommandOptionsDialog(Command, CustomCommandOptions, Flags, CustomCommandForOptions, Site, ShortCuts));
  1251. return Dialog->Execute(ShortCut);
  1252. }
  1253. //---------------------------------------------------------------------------
  1254. //---------------------------------------------------------------------------
  1255. class TUsageStatisticsDialog : public TCustomDialog
  1256. {
  1257. public:
  1258. __fastcall TUsageStatisticsDialog();
  1259. protected:
  1260. virtual void __fastcall DoChange(bool & CanSubmit);
  1261. private:
  1262. TEdit * FilterEdit;
  1263. TMemo * UsageMemo;
  1264. TButton * ClipboardButton;
  1265. void __fastcall ClipboardButtonClick(TObject * Sender);
  1266. };
  1267. //---------------------------------------------------------------------------
  1268. __fastcall TUsageStatisticsDialog::TUsageStatisticsDialog() :
  1269. TCustomDialog(HELP_USAGE)
  1270. {
  1271. Caption = LoadStr(USAGE_CAPTION);
  1272. Width = ScaleByTextHeight(this, 400);
  1273. TLabel * Label = new TLabel(this);
  1274. // UnformatMessage is called, because previously, ** markup was used and translations may still contain that
  1275. Label->Caption = UnformatMessage(LoadStr(USAGE_DATA2));
  1276. AddText(Label);
  1277. FilterEdit = new TEdit(this);
  1278. FilterEdit->Width = ScaleByTextHeight(this, 250);
  1279. TLabel * FilterLabel = new TLabel(this);
  1280. FilterLabel->Caption = LoadStr(USAGE_FILTER);
  1281. AddEdit(FilterEdit, FilterLabel, true);
  1282. UsageMemo = new TMemo(this);
  1283. UsageMemo->Height = ScaleByTextHeight(this, 300);
  1284. UsageMemo->ScrollBars = ssVertical;
  1285. AddEdit(UsageMemo, NULL);
  1286. ReadOnlyControl(UsageMemo);
  1287. ClipboardButton = new TButton(this);
  1288. ClipboardButton->Caption = LoadStr(USAGE_COPY);
  1289. ClipboardButton->Width = ScaleByTextHeight(this, 121);
  1290. ClipboardButton->OnClick = ClipboardButtonClick;
  1291. AddDialogButton(ClipboardButton);
  1292. RemoveCancelButton();
  1293. }
  1294. //---------------------------------------------------------------------------
  1295. void __fastcall TUsageStatisticsDialog::ClipboardButtonClick(TObject * /*Sender*/)
  1296. {
  1297. TInstantOperationVisualizer Visualizer;
  1298. CopyToClipboard(UsageMemo->Lines);
  1299. }
  1300. //---------------------------------------------------------------------------
  1301. void __fastcall TUsageStatisticsDialog::DoChange(bool & CanSubmit)
  1302. {
  1303. TCustomDialog::DoChange(CanSubmit);
  1304. UnicodeString Text = Configuration->Usage->Serialize(L"\n", FilterEdit->Text);
  1305. bool NoUsage = Text.IsEmpty();
  1306. ClipboardButton->Enabled = !NoUsage;
  1307. if (NoUsage)
  1308. {
  1309. Text = LoadStr(USAGE_DATA_NONE2);
  1310. }
  1311. UsageMemo->Lines->Text = Text;
  1312. }
  1313. //---------------------------------------------------------------------------
  1314. void __fastcall DoUsageStatisticsDialog()
  1315. {
  1316. std::unique_ptr<TUsageStatisticsDialog> Dialog(new TUsageStatisticsDialog());
  1317. Dialog->Execute();
  1318. }