1
0

Custom.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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. // This is probably here to ask before session is started saving.
  512. // Otherwise we would ask implicitly, when saving passwords, but at that moment,
  513. // part of the site is already saved and when the user cancel the prompt it's too late.
  514. CustomWinConfiguration->AskForMasterPasswordIfNotSetAndNeededToPersistSessionData(SessionData);
  515. Result = true;
  516. }
  517. else
  518. {
  519. // This can be a standalone dialog when used with save URL (from GetLoginData)
  520. TSaveSessionDialog * Dialog = SafeFormCreate<TSaveSessionDialog>();
  521. try
  522. {
  523. Dialog->Init((PSavePassword != NULL), NotRecommendedSavingPassword, AdditionalFolders);
  524. Result = Dialog->Execute(SessionName, SavePassword, CreateShortcut, SessionData->Name);
  525. }
  526. __finally
  527. {
  528. delete Dialog;
  529. }
  530. }
  531. TSessionData * NewSession = NULL;
  532. if (Result)
  533. {
  534. if ((PSavePassword != NULL) && !SavePassword)
  535. {
  536. SessionData->ClearSessionPasswords();
  537. }
  538. NewSession =
  539. StoredSessions->NewSession(SessionName, SessionData);
  540. // modified only, explicit
  541. StoredSessions->Save(false, true);
  542. if (!SessionData->HostKey.IsEmpty())
  543. {
  544. SessionData->CacheHostKeyIfNotCached();
  545. }
  546. if (CreateShortcut)
  547. {
  548. TOperationVisualizer Visualizer;
  549. UnicodeString AdditionalParams =
  550. TProgramParams::FormatSwitch(DESKTOP_SWITCH) + L" " +
  551. TProgramParams::FormatSwitch(UPLOAD_IF_ANY_SWITCH);
  552. CreateDesktopSessionShortCut(SessionName, L"", AdditionalParams, -1, SITE_ICON);
  553. }
  554. }
  555. return NewSession;
  556. }
  557. //---------------------------------------------------------------------------
  558. void __fastcall SessionNameValidate(const UnicodeString & Text,
  559. const UnicodeString & OriginalName)
  560. {
  561. TSessionData::ValidatePath(Text);
  562. DebugAssert(StoredSessions);
  563. TSessionData * Data = (TSessionData *)StoredSessions->FindByName(Text);
  564. if (Data && Data->Special)
  565. {
  566. MessageDialog(FMTLOAD(CANNOT_OVERWRITE_SPECIAL_SESSION, (Text)),
  567. qtError, qaOK, HELP_NONE);
  568. Abort();
  569. }
  570. else if ((Data != NULL) && !Data->IsSameName(OriginalName) &&
  571. MessageDialog(MainInstructions(FMTLOAD(CONFIRM_OVERWRITE_SESSION, (Text))),
  572. qtConfirmation, qaYes | qaNo, HELP_SESSION_SAVE_OVERWRITE) != qaYes)
  573. {
  574. Abort();
  575. }
  576. }
  577. //---------------------------------------------------------------------------
  578. //---------------------------------------------------------------------------
  579. class TSaveWorkspaceDialog : public TCustomDialog
  580. {
  581. public:
  582. __fastcall TSaveWorkspaceDialog(bool CanSavePasswords,
  583. bool NotRecommendedSavingPasswords);
  584. bool __fastcall Execute(
  585. UnicodeString & WorkspaceName, bool & SavePasswords, bool & CreateShortcut,
  586. bool & EnableAutoSave);
  587. protected:
  588. virtual void __fastcall DoValidate();
  589. virtual void __fastcall DoChange(bool & CanSubmit);
  590. private:
  591. TComboBox * WorkspaceNameCombo;
  592. TCheckBox * SavePasswordsCheck;
  593. TCheckBox * CreateShortcutCheck;
  594. TCheckBox * EnableAutoSaveCheck;
  595. };
  596. //---------------------------------------------------------------------------
  597. __fastcall TSaveWorkspaceDialog::TSaveWorkspaceDialog(
  598. bool CanSavePasswords, bool NotRecommendedSavingPasswords) :
  599. TCustomDialog(HELP_WORKSPACE_SAVE)
  600. {
  601. Caption = LoadStr(SAVE_WORKSPACE_CAPTION);
  602. WorkspaceNameCombo = new TComboBox(this);
  603. WorkspaceNameCombo->AutoComplete = false;
  604. AddComboBox(WorkspaceNameCombo, CreateLabel(LoadStr(SAVE_WORKSPACE_PROMPT)));
  605. WorkspaceNameCombo->DropDownCount = Max(WorkspaceNameCombo->DropDownCount, 16);
  606. std::unique_ptr<TStrings> Workspaces(StoredSessions->GetWorkspaces());
  607. WorkspaceNameCombo->Items->AddStrings(Workspaces.get());
  608. SavePasswordsCheck = CreateAndAddCheckBox(
  609. LoadStr(NotRecommendedSavingPasswords ? SAVE_WORKSPACE_PASSWORDS :
  610. (CustomWinConfiguration->UseMasterPassword ?
  611. SAVE_WORKSPACE_PASSWORDS_MASTER : SAVE_WORKSPACE_PASSWORDS_RECOMMENDED)));
  612. EnableControl(SavePasswordsCheck, CanSavePasswords);
  613. CreateShortcutCheck = CreateAndAddCheckBox(LoadStr(SAVE_SITE_WORKSPACE_SHORTCUT));
  614. EnableAutoSaveCheck = CreateAndAddCheckBox(LoadStr(SAVE_WORKSPACE_AUTO));
  615. }
  616. //---------------------------------------------------------------------------
  617. bool __fastcall TSaveWorkspaceDialog::Execute(
  618. UnicodeString & WorkspaceName, bool & SavePasswords, bool & CreateShortcut,
  619. bool & EnableAutoSave)
  620. {
  621. WorkspaceNameCombo->Text = WorkspaceName;
  622. SavePasswordsCheck->Checked = SavePasswords;
  623. CreateShortcutCheck->Checked = CreateShortcut;
  624. EnableAutoSaveCheck->Checked = EnableAutoSave;
  625. bool Result = TCustomDialog::Execute();
  626. if (Result)
  627. {
  628. WorkspaceName = WorkspaceNameCombo->Text;
  629. SavePasswords = SavePasswordsCheck->Checked;
  630. CreateShortcut = CreateShortcutCheck->Checked;
  631. EnableAutoSave = EnableAutoSaveCheck->Checked;
  632. }
  633. return Result;
  634. }
  635. //---------------------------------------------------------------------------
  636. void __fastcall TSaveWorkspaceDialog::DoValidate()
  637. {
  638. TSessionData::ValidateName(WorkspaceNameCombo->Text);
  639. if (StoredSessions->IsFolder(WorkspaceNameCombo->Text))
  640. {
  641. throw Exception(FMTLOAD(FOLDER_NOT_WORKSPACE, (WorkspaceNameCombo->Text)));
  642. }
  643. if (SavePasswordsCheck->Enabled && SavePasswordsCheck->Checked &&
  644. CustomWinConfiguration->UseMasterPassword)
  645. {
  646. CustomWinConfiguration->AskForMasterPasswordIfNotSet();
  647. }
  648. TCustomDialog::DoValidate();
  649. }
  650. //---------------------------------------------------------------------------
  651. void __fastcall TSaveWorkspaceDialog::DoChange(bool & CanSubmit)
  652. {
  653. CanSubmit = !WorkspaceNameCombo->Text.IsEmpty();
  654. TCustomDialog::DoChange(CanSubmit);
  655. }
  656. //---------------------------------------------------------------------------
  657. bool __fastcall DoSaveWorkspaceDialog(UnicodeString & WorkspaceName,
  658. bool * SavePasswords, bool NotRecommendedSavingPasswords,
  659. bool & CreateShortcut, bool & EnableAutoSave)
  660. {
  661. std::unique_ptr<TSaveWorkspaceDialog> Dialog(
  662. new TSaveWorkspaceDialog((SavePasswords != NULL), NotRecommendedSavingPasswords));
  663. bool Dummy = false;
  664. if (SavePasswords == NULL)
  665. {
  666. SavePasswords = &Dummy;
  667. }
  668. return
  669. Dialog->Execute(
  670. WorkspaceName, *SavePasswords, CreateShortcut, EnableAutoSave);
  671. }
  672. //---------------------------------------------------------------------------
  673. //---------------------------------------------------------------------------
  674. class TShortCutDialog : public TCustomDialog
  675. {
  676. public:
  677. __fastcall TShortCutDialog(const TShortCuts & ShortCuts, UnicodeString HelpKeyword);
  678. bool __fastcall Execute(TShortCut & ShortCut);
  679. private:
  680. TComboBox * ShortCutCombo;
  681. };
  682. //---------------------------------------------------------------------------
  683. __fastcall TShortCutDialog::TShortCutDialog(const TShortCuts & ShortCuts, UnicodeString HelpKeyword) :
  684. TCustomDialog(HelpKeyword)
  685. {
  686. Caption = LoadStr(SHORTCUT_CAPTION);
  687. ShortCutCombo = new TComboBox(this);
  688. AddShortCutComboBox(ShortCutCombo, CreateLabel(LoadStr(SHORTCUT_LABEL)), ShortCuts);
  689. }
  690. //---------------------------------------------------------------------------
  691. bool __fastcall TShortCutDialog::Execute(TShortCut & ShortCut)
  692. {
  693. SetShortCutCombo(ShortCutCombo, ShortCut);
  694. bool Result = TCustomDialog::Execute();
  695. if (Result)
  696. {
  697. ShortCut = GetShortCutCombo(ShortCutCombo);
  698. }
  699. return Result;
  700. }
  701. //---------------------------------------------------------------------------
  702. bool __fastcall DoShortCutDialog(TShortCut & ShortCut,
  703. const TShortCuts & ShortCuts, UnicodeString HelpKeyword)
  704. {
  705. bool Result;
  706. TShortCutDialog * Dialog = new TShortCutDialog(ShortCuts, HelpKeyword);
  707. try
  708. {
  709. Result = Dialog->Execute(ShortCut);
  710. }
  711. __finally
  712. {
  713. delete Dialog;
  714. }
  715. return Result;
  716. }
  717. //---------------------------------------------------------------------------
  718. //---------------------------------------------------------------------------
  719. class TRemoteMoveDialog : public TCustomDialog
  720. {
  721. public:
  722. __fastcall TRemoteMoveDialog(bool Multi);
  723. bool __fastcall Execute(UnicodeString & Target, UnicodeString & FileMask);
  724. protected:
  725. DYNAMIC void __fastcall DoShow();
  726. virtual void __fastcall DoValidate();
  727. UnicodeString __fastcall GetFileMask();
  728. private:
  729. THistoryComboBox * Combo;
  730. bool FMulti;
  731. };
  732. //---------------------------------------------------------------------------
  733. __fastcall TRemoteMoveDialog::TRemoteMoveDialog(bool Multi) :
  734. TCustomDialog(HELP_REMOTE_MOVE)
  735. {
  736. Caption = LoadStr(REMOTE_MOVE_TITLE);
  737. // The same as TRemoteTransferDialog
  738. ClientWidth = ScaleByTextHeight(this, 420);
  739. FMulti = Multi;
  740. AddImage(L"Move To");
  741. Combo = new THistoryComboBox(this);
  742. Combo->AutoComplete = false;
  743. AddComboBox(Combo, CreateLabel(LoadStr(REMOTE_TRANSFER_PROMPT2)));
  744. }
  745. //---------------------------------------------------------------------------
  746. bool __fastcall TRemoteMoveDialog::Execute(UnicodeString & Target, UnicodeString & FileMask)
  747. {
  748. Combo->Items = CustomWinConfiguration->History[L"RemoteTarget"];
  749. Combo->Text = UnixIncludeTrailingBackslash(Target) + FileMask;
  750. bool Result = TCustomDialog::Execute();
  751. if (Result)
  752. {
  753. Target = UnixExtractFilePath(Combo->Text);
  754. FileMask = GetFileMask();
  755. Combo->SaveToHistory();
  756. CustomWinConfiguration->History[L"RemoteTarget"] = Combo->Items;
  757. }
  758. return Result;
  759. }
  760. //---------------------------------------------------------------------------
  761. UnicodeString __fastcall TRemoteMoveDialog::GetFileMask()
  762. {
  763. return UnixExtractFileName(Combo->Text);
  764. }
  765. //---------------------------------------------------------------------------
  766. void __fastcall TRemoteMoveDialog::DoShow()
  767. {
  768. TCustomDialog::DoShow();
  769. InstallPathWordBreakProc(Combo);
  770. }
  771. //---------------------------------------------------------------------------
  772. void __fastcall TRemoteMoveDialog::DoValidate()
  773. {
  774. if (!IsFileNameMask(GetFileMask()) && FMulti)
  775. {
  776. UnicodeString Message =
  777. FormatMultiFilesToOneConfirmation(Combo->Text, true);
  778. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_NONE) == qaCancel)
  779. {
  780. Abort();
  781. }
  782. }
  783. TCustomDialog::DoValidate();
  784. }
  785. //---------------------------------------------------------------------------
  786. bool __fastcall DoRemoteMoveDialog(bool Multi, UnicodeString & Target, UnicodeString & FileMask)
  787. {
  788. std::unique_ptr<TRemoteMoveDialog> Dialog(new TRemoteMoveDialog(Multi));
  789. return Dialog->Execute(Target, FileMask);
  790. }
  791. //---------------------------------------------------------------------------
  792. //---------------------------------------------------------------------------
  793. class TCustomCommandOptionsDialog : public TCustomDialog
  794. {
  795. public:
  796. __fastcall TCustomCommandOptionsDialog(
  797. const TCustomCommandType * Command, TStrings * CustomCommandOptions, unsigned int Flags,
  798. TCustomCommand * CustomCommandForOptions, const UnicodeString & Site, const TShortCuts * ShortCuts);
  799. bool __fastcall Execute(TShortCut * ShortCut);
  800. protected:
  801. virtual void __fastcall DoHelp();
  802. DYNAMIC void __fastcall DoShow();
  803. private:
  804. const TCustomCommandType * FCommand;
  805. TStrings * FCustomCommandOptions;
  806. std::vector<TControl *> FControls;
  807. std::vector<std::vector<UnicodeString> > FValues;
  808. unsigned int FFlags;
  809. UnicodeString FSite;
  810. TComboBox * FShortCutCombo;
  811. UnicodeString __fastcall HistoryKey(const TCustomCommandType::TOption & Option);
  812. THistoryComboBox * __fastcall CreateHistoryComboBox(const TCustomCommandType::TOption & Option, const UnicodeString & Value);
  813. void __fastcall BrowseButtonClick(TObject * Sender);
  814. void __fastcall LinkLabelClick(TObject * Sender);
  815. UnicodeString __fastcall SaveHistoryComboBoxValue(TControl * Control, const TCustomCommandType::TOption & Option);
  816. void __fastcall AddOptionComboBox(
  817. TComboBox * ComboBox, const UnicodeString & Value, const TCustomCommandType::TOption & Option,
  818. std::vector<UnicodeString> & Values);
  819. UnicodeString __fastcall GetComboBoxValue(TControl * Control, const UnicodeString & Default);
  820. int __fastcall GetOptionIndex(TControl * Control);
  821. int __fastcall GetControlIndex(TControl * Control);
  822. };
  823. //---------------------------------------------------------------------------
  824. __fastcall TCustomCommandOptionsDialog::TCustomCommandOptionsDialog(
  825. const TCustomCommandType * Command, TStrings * CustomCommandOptions,
  826. unsigned int Flags, TCustomCommand * CustomCommandForOptions,
  827. const UnicodeString & Site, const TShortCuts * ShortCuts) :
  828. TCustomDialog(HELP_EXTENSION_OPTIONS)
  829. {
  830. FCommand = Command;
  831. FFlags = Flags;
  832. FCustomCommandOptions = CustomCommandOptions;
  833. FSite = Site;
  834. Caption = FMTLOAD(EXTENSION_OPTIONS_CAPTION, (StripEllipsis(StripHotkey(FCommand->Name))));
  835. Width = ScaleByTextHeight(this, 400);
  836. bool HasGroups = false;
  837. int ControlIndex = 0;
  838. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  839. {
  840. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  841. if ((Option.Flags & FFlags) != 0)
  842. {
  843. UnicodeString OptionKey = FCommand->GetOptionKey(Option, FSite);
  844. UnicodeString Value;
  845. if ((CustomCommandForOptions != NULL) &&
  846. Option.HasPatterns(CustomCommandForOptions))
  847. {
  848. Value = CustomCommandForOptions->Complete(Option.Default, true);
  849. }
  850. else
  851. {
  852. if (FCustomCommandOptions->IndexOfName(OptionKey) >= 0)
  853. {
  854. Value = FCustomCommandOptions->Values[OptionKey];
  855. }
  856. else
  857. {
  858. Value = Option.Default;
  859. }
  860. }
  861. int Tag = (OptionIndex << 16) + ControlIndex;
  862. TControl * Control = NULL;
  863. std::vector<UnicodeString> Values;
  864. if (Option.Kind == TCustomCommandType::okUnknown)
  865. {
  866. Control = NULL;
  867. }
  868. else if (Option.Kind == TCustomCommandType::okLabel)
  869. {
  870. TLabel * Label = CreateLabel(Option.Caption);
  871. AddText(Label);
  872. Control = Label;
  873. }
  874. else if (Option.Kind == TCustomCommandType::okLink)
  875. {
  876. TStaticText * Label = new TStaticText(this);
  877. Label->Caption = Option.Caption;
  878. if (IsHttpOrHttpsUrl(Label->Caption))
  879. {
  880. Label->Caption = SecureUrl(Label->Caption);
  881. LinkLabel(Label);
  882. Label->TabStop = true;
  883. }
  884. else if (!Option.Default.IsEmpty() && IsHttpOrHttpsUrl(Option.Default))
  885. {
  886. Label->OnClick = LinkLabelClick;
  887. LinkLabel(Label);
  888. Label->TabStop = true;
  889. }
  890. else
  891. {
  892. // keep it plain text, as we have no URL
  893. }
  894. AddText(Label);
  895. Control = Label;
  896. }
  897. else if (Option.Kind == TCustomCommandType::okGroup)
  898. {
  899. StartGroup(Option.Caption);
  900. HasGroups = true;
  901. }
  902. else if (Option.Kind == TCustomCommandType::okSeparator)
  903. {
  904. AddSeparator();
  905. }
  906. else if (Option.Kind == TCustomCommandType::okTextBox)
  907. {
  908. Control = CreateHistoryComboBox(Option, Value);
  909. }
  910. else if (Option.Kind == TCustomCommandType::okFile)
  911. {
  912. THistoryComboBox * ComboBox = CreateHistoryComboBox(Option, Value);
  913. TButton * Button = new TButton(this);
  914. Button->Parent = GetDefaultParent();
  915. Button->Width = HelpButton->Width;
  916. Button->Left = GetDefaultParent()->ClientWidth - Button->Width - HorizontalMargin;
  917. ComboBox->Width = Button->Left - ComboBox->Left - ScaleByTextHeight(this, 6);
  918. Button->Top = ComboBox->Top - ScaleByTextHeight(this, 2);
  919. Button->Tag = Tag;
  920. Button->Caption = LoadStr(EXTENSION_OPTIONS_BROWSE);
  921. Button->OnClick = BrowseButtonClick;
  922. ScaleButtonControl(Button);
  923. AddWinControl(Button);
  924. Control = ComboBox;
  925. }
  926. else if (Option.Kind == TCustomCommandType::okDropDownList)
  927. {
  928. TComboBox * ComboBox = new TComboBox(this);
  929. ComboBox->Style = csDropDownList;
  930. AddOptionComboBox(ComboBox, Value, Option, Values);
  931. Control = ComboBox;
  932. }
  933. else if (Option.Kind == TCustomCommandType::okComboBox)
  934. {
  935. TComboBox * ComboBox = new TComboBox(this);
  936. ComboBox->Style = csDropDown;
  937. AddOptionComboBox(ComboBox, Value, Option, Values);
  938. if (ComboBox->ItemIndex < 0)
  939. {
  940. ComboBox->Text = Value;
  941. }
  942. Control = ComboBox;
  943. }
  944. else if (Option.Kind == TCustomCommandType::okCheckBox)
  945. {
  946. TCheckBox * CheckBox = CreateAndAddCheckBox(Option.Caption);
  947. CheckBox->Checked =
  948. (Option.Params.size() >= 1) &&
  949. (Value == Option.Params[0]);
  950. Control = CheckBox;
  951. }
  952. else
  953. {
  954. DebugFail();
  955. }
  956. if (Control != NULL)
  957. {
  958. Control->Tag = Tag;
  959. }
  960. FControls.push_back(Control);
  961. FValues.push_back(Values);
  962. ControlIndex++;
  963. DebugAssert(static_cast<int>(FControls.size()) == ControlIndex);
  964. }
  965. }
  966. if (ShortCuts != NULL)
  967. {
  968. if (HasGroups)
  969. {
  970. StartGroup(LoadStr(EXTENSION_GENERAL_GROUP));
  971. }
  972. else if (ControlIndex > 0)
  973. {
  974. AddSeparator();
  975. }
  976. FShortCutCombo = new TComboBox(this);
  977. AddShortCutComboBox(FShortCutCombo, CreateLabel(LoadStr(EXTENSION_SHORTCUT)), *ShortCuts);
  978. }
  979. }
  980. //---------------------------------------------------------------------------
  981. void __fastcall TCustomCommandOptionsDialog::AddOptionComboBox(
  982. TComboBox * ComboBox, const UnicodeString & Value, const TCustomCommandType::TOption & Option, std::vector<UnicodeString> & Values)
  983. {
  984. std::unique_ptr<TStringList> Items(new TStringList());
  985. int ItemIndex = -1;
  986. TCustomCommandType::TOption::TParams::const_iterator ParamI = Option.Params.begin();
  987. while (ParamI != Option.Params.end())
  988. {
  989. UnicodeString Item = (*ParamI);
  990. int P = Item.Pos(L"=");
  991. UnicodeString ParamValue;
  992. if (P > 0)
  993. {
  994. ParamValue = Item.SubString(1, P - 1);
  995. Item.Delete(1, P);
  996. }
  997. else
  998. {
  999. ParamValue = Item;
  1000. }
  1001. Item = WinConfiguration->ExtensionStringTranslation(FCommand->Id, Item);
  1002. Items->Add(Item);
  1003. if (Value == ParamValue)
  1004. {
  1005. ItemIndex = Items->Count - 1;
  1006. }
  1007. Values.push_back(ParamValue);
  1008. ParamI++;
  1009. }
  1010. AddComboBox(ComboBox, CreateLabel(Option.Caption), Items.get(), true);
  1011. ComboBox->ItemIndex = ItemIndex;
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. int __fastcall TCustomCommandOptionsDialog::GetOptionIndex(TControl * Control)
  1015. {
  1016. return (Control->Tag >> 16);
  1017. }
  1018. //---------------------------------------------------------------------------
  1019. int __fastcall TCustomCommandOptionsDialog::GetControlIndex(TControl * Control)
  1020. {
  1021. return (Control->Tag & 0xFFFF);
  1022. }
  1023. //---------------------------------------------------------------------------
  1024. void __fastcall TCustomCommandOptionsDialog::LinkLabelClick(TObject * Sender)
  1025. {
  1026. TStaticText * Label = DebugNotNull(dynamic_cast<TStaticText *>(Sender));
  1027. const TCustomCommandType::TOption & Option = FCommand->GetOption(GetOptionIndex(Label));
  1028. OpenBrowser(SecureUrl(Option.Default));
  1029. }
  1030. //---------------------------------------------------------------------------
  1031. void __fastcall TCustomCommandOptionsDialog::BrowseButtonClick(TObject * Sender)
  1032. {
  1033. TButton * Button = DebugNotNull(dynamic_cast<TButton *>(Sender));
  1034. int OptionIndex = GetOptionIndex(Button);
  1035. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1036. int ControlIndex = GetControlIndex(Button);
  1037. THistoryComboBox * ComboBox = dynamic_cast<THistoryComboBox *>(FControls[ControlIndex]);
  1038. std::unique_ptr<TOpenDialog> OpenDialog(new TOpenDialog(Application));
  1039. UnicodeString Title;
  1040. if (!Option.FileCaption.IsEmpty())
  1041. {
  1042. Title = Option.FileCaption;
  1043. }
  1044. else
  1045. {
  1046. UnicodeString Caption = Option.Caption;
  1047. Caption = StripHotkey(Caption);
  1048. if (!Caption.IsEmpty() && (Caption[Caption.Length()] == L':'))
  1049. {
  1050. Caption.SetLength(Caption.Length() - 1);
  1051. }
  1052. Title = FMTLOAD(EXTENSION_OPTIONS_BROWSE_TITLE, (Caption));
  1053. }
  1054. OpenDialog->Title = Title;
  1055. UnicodeString Value;
  1056. if (ComboBox->Text.IsEmpty())
  1057. {
  1058. Value = Option.FileInitial;
  1059. }
  1060. else
  1061. {
  1062. Value = ComboBox->Text;
  1063. }
  1064. UnicodeString ExpandedValue = ExpandEnvironmentVariables(Value);
  1065. OpenDialog->FileName = ExpandedValue;
  1066. UnicodeString InitialDir = ExtractFilePath(ExpandedValue);
  1067. if (!InitialDir.IsEmpty())
  1068. {
  1069. OpenDialog->InitialDir = InitialDir;
  1070. }
  1071. OpenDialog->Filter = Option.FileFilter;
  1072. OpenDialog->DefaultExt = Option.FileExt;
  1073. if (OpenDialog->Execute())
  1074. {
  1075. if (OpenDialog->FileName != ExpandedValue)
  1076. {
  1077. ComboBox->Text = OpenDialog->FileName;
  1078. }
  1079. // If user just confirms the initial value, persist it
  1080. else if (ComboBox->Text.IsEmpty())
  1081. {
  1082. DebugAssert(Option.FileInitial == Value);
  1083. ComboBox->Text = Value;
  1084. }
  1085. }
  1086. }
  1087. //---------------------------------------------------------------------------
  1088. THistoryComboBox * __fastcall TCustomCommandOptionsDialog::CreateHistoryComboBox(
  1089. const TCustomCommandType::TOption & Option, const UnicodeString & Value)
  1090. {
  1091. THistoryComboBox * ComboBox = new THistoryComboBox(this);
  1092. ComboBox->AutoComplete = false;
  1093. AddComboBox(ComboBox, CreateLabel(Option.Caption));
  1094. ComboBox->Items = CustomWinConfiguration->History[HistoryKey(Option)];
  1095. ComboBox->Text = Value;
  1096. return ComboBox;
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. UnicodeString __fastcall TCustomCommandOptionsDialog::HistoryKey(const TCustomCommandType::TOption & Option)
  1100. {
  1101. UnicodeString Result = FCommand->GetOptionKey(Option, FSite);
  1102. Result = CustomWinConfiguration->GetValidHistoryKey(Result);
  1103. return L"CustomCommandOption_" + Result;
  1104. }
  1105. //---------------------------------------------------------------------------
  1106. bool __fastcall TCustomCommandOptionsDialog::Execute(TShortCut * ShortCut)
  1107. {
  1108. if (ShortCut != NULL)
  1109. {
  1110. SetShortCutCombo(FShortCutCombo, *ShortCut);
  1111. }
  1112. bool Result = TCustomDialog::Execute();
  1113. if (Result)
  1114. {
  1115. int ControlIndex = 0;
  1116. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  1117. {
  1118. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1119. if ((Option.Flags & FFlags) != 0)
  1120. {
  1121. if ((Option.Kind != TCustomCommandType::okUnknown) &&
  1122. Option.IsControl)
  1123. {
  1124. UnicodeString OptionKey = FCommand->GetOptionKey(Option, FSite);
  1125. TControl * Control = FControls[ControlIndex];
  1126. UnicodeString Value;
  1127. if (Option.Kind == TCustomCommandType::okTextBox)
  1128. {
  1129. Value = SaveHistoryComboBoxValue(Control, Option);
  1130. }
  1131. else if (Option.Kind == TCustomCommandType::okFile)
  1132. {
  1133. Value = SaveHistoryComboBoxValue(Control, Option);
  1134. }
  1135. else if (Option.Kind == TCustomCommandType::okDropDownList)
  1136. {
  1137. Value = GetComboBoxValue(Control, Option.Default);
  1138. }
  1139. else if (Option.Kind == TCustomCommandType::okComboBox)
  1140. {
  1141. TComboBox * ComboBox = DebugNotNull(dynamic_cast<TComboBox *>(Control));
  1142. Value = GetComboBoxValue(Control, ComboBox->Text);
  1143. }
  1144. else if (Option.Kind == TCustomCommandType::okCheckBox)
  1145. {
  1146. TCheckBox * CheckBox = DebugNotNull(dynamic_cast<TCheckBox *>(Control));
  1147. int Index = (CheckBox->Checked ? 0 : 1);
  1148. Value = (Index < static_cast<int>(Option.Params.size())) ? Option.Params[Index] : UnicodeString();
  1149. }
  1150. else
  1151. {
  1152. DebugFail();
  1153. }
  1154. // The default value setter deletes the "name" when the value is empty.
  1155. // It would cause us to fall back to the default value, but we want to remember the empty value.
  1156. if (Value.IsEmpty())
  1157. {
  1158. int Index = FCustomCommandOptions->IndexOfName(OptionKey);
  1159. if (Index < 0)
  1160. {
  1161. Index = FCustomCommandOptions->Add(L"");
  1162. }
  1163. UnicodeString Line = OptionKey + FCustomCommandOptions->NameValueSeparator;
  1164. FCustomCommandOptions->Strings[Index] = Line;
  1165. }
  1166. else
  1167. {
  1168. FCustomCommandOptions->Values[OptionKey] = Value;
  1169. }
  1170. }
  1171. ControlIndex++;
  1172. }
  1173. }
  1174. if (ShortCut != NULL)
  1175. {
  1176. *ShortCut = GetShortCutCombo(FShortCutCombo);
  1177. }
  1178. }
  1179. return Result;
  1180. }
  1181. //---------------------------------------------------------------------------
  1182. UnicodeString __fastcall TCustomCommandOptionsDialog::GetComboBoxValue(
  1183. TControl * Control, const UnicodeString & Default)
  1184. {
  1185. TComboBox * ComboBox = DebugNotNull(dynamic_cast<TComboBox *>(Control));
  1186. UnicodeString Result;
  1187. if (ComboBox->ItemIndex < 0)
  1188. {
  1189. Result = Default;
  1190. }
  1191. else
  1192. {
  1193. Result = FValues[GetControlIndex(Control)][ComboBox->ItemIndex];
  1194. }
  1195. return Result;
  1196. }
  1197. //---------------------------------------------------------------------------
  1198. UnicodeString __fastcall TCustomCommandOptionsDialog::SaveHistoryComboBoxValue(
  1199. TControl * Control, const TCustomCommandType::TOption & Option)
  1200. {
  1201. THistoryComboBox * ComboBox = DebugNotNull(dynamic_cast<THistoryComboBox *>(Control));
  1202. ComboBox->SaveToHistory();
  1203. CustomWinConfiguration->History[HistoryKey(Option)] = ComboBox->Items;
  1204. return ComboBox->Text;
  1205. }
  1206. //---------------------------------------------------------------------------
  1207. void __fastcall TCustomCommandOptionsDialog::DoHelp()
  1208. {
  1209. UnicodeString HelpPage;
  1210. if (!FCommand->OptionsPage.IsEmpty())
  1211. {
  1212. HelpPage = FCommand->OptionsPage;
  1213. }
  1214. else
  1215. {
  1216. HelpPage = FCommand->HomePage;
  1217. }
  1218. if (!HelpPage.IsEmpty())
  1219. {
  1220. OpenBrowser(HelpPage);
  1221. }
  1222. else
  1223. {
  1224. TCustomDialog::DoHelp();
  1225. }
  1226. }
  1227. //---------------------------------------------------------------------------
  1228. void __fastcall TCustomCommandOptionsDialog::DoShow()
  1229. {
  1230. TCustomDialog::DoShow();
  1231. int ControlIndex = 0;
  1232. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  1233. {
  1234. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1235. if ((Option.Flags & FFlags) != 0)
  1236. {
  1237. if (Option.Kind == TCustomCommandType::okFile)
  1238. {
  1239. TControl * Control = FControls[ControlIndex];
  1240. InstallPathWordBreakProc(DebugNotNull(dynamic_cast<TWinControl *>(Control)));
  1241. }
  1242. ControlIndex++;
  1243. }
  1244. }
  1245. }
  1246. //---------------------------------------------------------------------------
  1247. bool __fastcall DoCustomCommandOptionsDialog(
  1248. const TCustomCommandType * Command, TStrings * CustomCommandOptions, TShortCut * ShortCut,
  1249. unsigned int Flags, TCustomCommand * CustomCommandForOptions,
  1250. const UnicodeString & Site, const TShortCuts * ShortCuts)
  1251. {
  1252. std::unique_ptr<TCustomCommandOptionsDialog> Dialog(
  1253. new TCustomCommandOptionsDialog(Command, CustomCommandOptions, Flags, CustomCommandForOptions, Site, ShortCuts));
  1254. return Dialog->Execute(ShortCut);
  1255. }
  1256. //---------------------------------------------------------------------------
  1257. //---------------------------------------------------------------------------
  1258. class TUsageStatisticsDialog : public TCustomDialog
  1259. {
  1260. public:
  1261. __fastcall TUsageStatisticsDialog();
  1262. protected:
  1263. virtual void __fastcall DoChange(bool & CanSubmit);
  1264. private:
  1265. TEdit * FilterEdit;
  1266. TMemo * UsageMemo;
  1267. TButton * ClipboardButton;
  1268. void __fastcall ClipboardButtonClick(TObject * Sender);
  1269. };
  1270. //---------------------------------------------------------------------------
  1271. __fastcall TUsageStatisticsDialog::TUsageStatisticsDialog() :
  1272. TCustomDialog(HELP_USAGE)
  1273. {
  1274. Caption = LoadStr(USAGE_CAPTION);
  1275. Width = ScaleByTextHeight(this, 400);
  1276. TLabel * Label = new TLabel(this);
  1277. // UnformatMessage is called, because previously, ** markup was used and translations may still contain that
  1278. Label->Caption = UnformatMessage(LoadStr(USAGE_DATA2));
  1279. AddText(Label);
  1280. FilterEdit = new TEdit(this);
  1281. FilterEdit->Width = ScaleByTextHeight(this, 250);
  1282. TLabel * FilterLabel = new TLabel(this);
  1283. FilterLabel->Caption = LoadStr(USAGE_FILTER);
  1284. AddEdit(FilterEdit, FilterLabel, true);
  1285. UsageMemo = new TMemo(this);
  1286. UsageMemo->Height = ScaleByTextHeight(this, 300);
  1287. UsageMemo->ScrollBars = ssVertical;
  1288. AddEdit(UsageMemo, NULL);
  1289. ReadOnlyControl(UsageMemo);
  1290. ClipboardButton = new TButton(this);
  1291. ClipboardButton->Caption = LoadStr(USAGE_COPY);
  1292. ClipboardButton->Width = ScaleByTextHeight(this, 121);
  1293. ClipboardButton->OnClick = ClipboardButtonClick;
  1294. AddDialogButton(ClipboardButton);
  1295. RemoveCancelButton();
  1296. }
  1297. //---------------------------------------------------------------------------
  1298. void __fastcall TUsageStatisticsDialog::ClipboardButtonClick(TObject * /*Sender*/)
  1299. {
  1300. TInstantOperationVisualizer Visualizer;
  1301. CopyToClipboard(UsageMemo->Lines);
  1302. }
  1303. //---------------------------------------------------------------------------
  1304. void __fastcall TUsageStatisticsDialog::DoChange(bool & CanSubmit)
  1305. {
  1306. TCustomDialog::DoChange(CanSubmit);
  1307. UnicodeString Text = Configuration->Usage->Serialize(L"\n", FilterEdit->Text);
  1308. bool NoUsage = Text.IsEmpty();
  1309. ClipboardButton->Enabled = !NoUsage;
  1310. if (NoUsage)
  1311. {
  1312. Text = LoadStr(USAGE_DATA_NONE2);
  1313. }
  1314. UsageMemo->Lines->Text = Text;
  1315. }
  1316. //---------------------------------------------------------------------------
  1317. void __fastcall DoUsageStatisticsDialog()
  1318. {
  1319. std::unique_ptr<TUsageStatisticsDialog> Dialog(new TUsageStatisticsDialog());
  1320. Dialog->Execute();
  1321. }