Custom.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 "Custom.h"
  14. //---------------------------------------------------------------------
  15. #pragma link "PasswordEdit"
  16. #ifndef NO_RESOURCES
  17. #pragma resource "*.dfm"
  18. #endif
  19. //---------------------------------------------------------------------
  20. __fastcall TCustomDialog::TCustomDialog(UnicodeString AHelpKeyword)
  21. : TForm(GetFormOwner())
  22. {
  23. UseSystemSettings(this);
  24. FPos = 8;
  25. HelpKeyword = AHelpKeyword;
  26. TBorderIcons BI = BorderIcons;
  27. if (HelpKeyword.IsEmpty())
  28. {
  29. BI >> biHelp;
  30. OKButton->Left = CancelButton->Left;
  31. CancelButton->Left = HelpButton->Left;
  32. HelpButton->Visible = false;
  33. }
  34. else
  35. {
  36. BI << biHelp;
  37. }
  38. BorderIcons = BI;
  39. }
  40. //---------------------------------------------------------------------
  41. bool __fastcall TCustomDialog::Execute()
  42. {
  43. Changed();
  44. return (ShowModal() == mrOk);
  45. }
  46. //---------------------------------------------------------------------
  47. void __fastcall TCustomDialog::DoChange(bool & /*CanSubmit*/)
  48. {
  49. // noop
  50. }
  51. //---------------------------------------------------------------------
  52. void __fastcall TCustomDialog::Changed()
  53. {
  54. bool CanSubmit = true;
  55. DoChange(CanSubmit);
  56. EnableControl(OKButton, CanSubmit);
  57. }
  58. //---------------------------------------------------------------------
  59. void __fastcall TCustomDialog::Change(TObject * /*Sender*/)
  60. {
  61. Changed();
  62. }
  63. //---------------------------------------------------------------------------
  64. void __fastcall TCustomDialog::HelpButtonClick(TObject * /*Sender*/)
  65. {
  66. FormHelp(this);
  67. }
  68. //---------------------------------------------------------------------------
  69. void __fastcall TCustomDialog::DoShow()
  70. {
  71. OKButton->TabOrder = FCount;
  72. CancelButton->TabOrder = static_cast<short>(FCount + 1);
  73. HelpButton->TabOrder = static_cast<short>(FCount + 2);
  74. Changed();
  75. TForm::DoShow();
  76. }
  77. //---------------------------------------------------------------------------
  78. void __fastcall TCustomDialog::DoValidate()
  79. {
  80. // noop
  81. }
  82. //---------------------------------------------------------------------------
  83. bool __fastcall TCustomDialog::CloseQuery()
  84. {
  85. if (ModalResult == mrOk)
  86. {
  87. DoValidate();
  88. }
  89. return TForm::CloseQuery();
  90. }
  91. //---------------------------------------------------------------------------
  92. void __fastcall TCustomDialog::AddWinControl(TWinControl * Control)
  93. {
  94. Control->TabOrder = FCount;
  95. FCount++;
  96. }
  97. //---------------------------------------------------------------------------
  98. TLabel * __fastcall TCustomDialog::CreateLabel(UnicodeString Label)
  99. {
  100. TLabel * Result = new TLabel(this);
  101. Result->Caption = Label;
  102. return Result;
  103. }
  104. //---------------------------------------------------------------------------
  105. void __fastcall TCustomDialog::AddEditLikeControl(TWinControl * Edit, TLabel * Label)
  106. {
  107. int PrePos = FPos;
  108. Label->Parent = this;
  109. Label->Left = 8;
  110. Label->Top = FPos;
  111. FPos += 16;
  112. Edit->Parent = this;
  113. Edit->Left = 8;
  114. Edit->Top = FPos;
  115. Edit->Width = ClientWidth - (Edit->Left * 2);
  116. // this updates Height property to real value
  117. Edit->HandleNeeded();
  118. FPos += Edit->Height + 8;
  119. if (Label->FocusControl == NULL)
  120. {
  121. Label->FocusControl = Edit;
  122. }
  123. else
  124. {
  125. assert(Label->FocusControl == Edit);
  126. }
  127. ClientHeight = ClientHeight + (FPos - PrePos);
  128. AddWinControl(Edit);
  129. }
  130. //---------------------------------------------------------------------------
  131. void __fastcall TCustomDialog::AddEdit(TCustomEdit * Edit, TLabel * Label)
  132. {
  133. AddEditLikeControl(Edit, Label);
  134. TEdit * PublicEdit = reinterpret_cast<TEdit *>(Edit);
  135. if (PublicEdit->OnChange == NULL)
  136. {
  137. PublicEdit->OnChange = Change;
  138. }
  139. }
  140. //---------------------------------------------------------------------------
  141. void __fastcall TCustomDialog::AddComboBox(TCustomCombo * Combo, TLabel * Label)
  142. {
  143. AddEditLikeControl(Combo, Label);
  144. TComboBox * PublicCombo = reinterpret_cast<TComboBox *>(Combo);
  145. if (PublicCombo->OnChange == NULL)
  146. {
  147. PublicCombo->OnChange = Change;
  148. }
  149. }
  150. //---------------------------------------------------------------------------
  151. void __fastcall TCustomDialog::AddButtonControl(TButtonControl * Control)
  152. {
  153. int PrePos = FPos;
  154. Control->Parent = this;
  155. Control->Left = 14;
  156. Control->Top = FPos;
  157. Control->Width = ClientWidth - Control->Left - 8;
  158. // this updates Height property to real value
  159. Control->HandleNeeded();
  160. FPos += Control->Height + 8;
  161. ClientHeight = ClientHeight + (FPos - PrePos);
  162. AddWinControl(Control);
  163. TCheckBox * PublicControl = reinterpret_cast<TCheckBox *>(Control);
  164. if (PublicControl->OnClick == NULL)
  165. {
  166. PublicControl->OnClick = Change;
  167. }
  168. }
  169. //---------------------------------------------------------------------------
  170. //---------------------------------------------------------------------------
  171. class TSaveSessionDialog : public TCustomDialog
  172. {
  173. public:
  174. __fastcall TSaveSessionDialog(TSessionData * OriginalSession, bool CanSavePassword, bool NotRecommendedSavingPassword);
  175. bool __fastcall Execute(UnicodeString & SessionName, bool & SavePassword);
  176. protected:
  177. DYNAMIC void __fastcall DoShow();
  178. virtual void __fastcall DoValidate();
  179. virtual void __fastcall DoChange(bool & CanSubmit);
  180. private:
  181. TSessionData * FOriginalSession;
  182. TComboBox * SessionNameCombo;
  183. TCheckBox * SavePasswordCheck;
  184. };
  185. //---------------------------------------------------------------------------
  186. __fastcall TSaveSessionDialog::TSaveSessionDialog(
  187. TSessionData * OriginalSession, bool CanSavePassword, bool NotRecommendedSavingPassword) :
  188. TCustomDialog(HELP_SESSION_SAVE),
  189. FOriginalSession(OriginalSession)
  190. {
  191. Caption = LoadStr(SAVE_SESSION_CAPTION);
  192. SessionNameCombo = new TComboBox(this);
  193. SessionNameCombo->AutoComplete = false;
  194. AddComboBox(SessionNameCombo, CreateLabel(LoadStr(SAVE_SESSION_PROMPT)));
  195. // parent has to be set before following
  196. InstallPathWordBreakProc(SessionNameCombo);
  197. SessionNameCombo->Items->BeginUpdate();
  198. try
  199. {
  200. for (int Index = 0; Index < StoredSessions->Count; Index++)
  201. {
  202. TSessionData * Data = StoredSessions->Sessions[Index];
  203. if (!Data->Special)
  204. {
  205. SessionNameCombo->Items->Add(Data->Name);
  206. }
  207. }
  208. }
  209. __finally
  210. {
  211. SessionNameCombo->Items->EndUpdate();
  212. }
  213. SavePasswordCheck = new TCheckBox(this);
  214. SavePasswordCheck->Caption = LoadStr(
  215. NotRecommendedSavingPassword ? SAVE_SESSION_PASSWORD :
  216. (CustomWinConfiguration->UseMasterPassword ? SAVE_SESSION_PASSWORD_MASTER : SAVE_SESSION_PASSWORD_RECOMMENDED));
  217. AddButtonControl(SavePasswordCheck);
  218. EnableControl(SavePasswordCheck, CanSavePassword);
  219. }
  220. //---------------------------------------------------------------------------
  221. bool __fastcall TSaveSessionDialog::Execute(UnicodeString & SessionName, bool & SavePassword)
  222. {
  223. SessionNameCombo->Text = SessionName;
  224. SavePasswordCheck->Checked = SavePassword;
  225. bool Result = TCustomDialog::Execute();
  226. if (Result)
  227. {
  228. SessionName = SessionNameCombo->Text;
  229. SavePassword = SavePasswordCheck->Checked;
  230. }
  231. return Result;
  232. }
  233. //---------------------------------------------------------------------------
  234. void __fastcall TSaveSessionDialog::DoShow()
  235. {
  236. int P = SessionNameCombo->Text.LastDelimiter(L"/");
  237. if (P > 0)
  238. {
  239. SessionNameCombo->SetFocus();
  240. SessionNameCombo->SelStart = P;
  241. SessionNameCombo->SelLength = SessionNameCombo->Text.Length() - P;
  242. }
  243. TCustomDialog::DoShow();
  244. }
  245. //---------------------------------------------------------------------------
  246. void __fastcall TSaveSessionDialog::DoValidate()
  247. {
  248. SessionNameValidate(SessionNameCombo->Text, FOriginalSession);
  249. if (SavePasswordCheck->Enabled && SavePasswordCheck->Checked &&
  250. CustomWinConfiguration->UseMasterPassword)
  251. {
  252. CustomWinConfiguration->AskForMasterPasswordIfNotSet();
  253. }
  254. TCustomDialog::DoValidate();
  255. }
  256. //---------------------------------------------------------------------------
  257. void __fastcall TSaveSessionDialog::DoChange(bool & CanSubmit)
  258. {
  259. CanSubmit = !SessionNameCombo->Text.IsEmpty();
  260. TCustomDialog::DoChange(CanSubmit);
  261. }
  262. //---------------------------------------------------------------------------
  263. bool __fastcall DoSaveSessionDialog(UnicodeString & SessionName,
  264. bool * SavePassword, TSessionData * OriginalSession, bool NotRecommendedSavingPassword)
  265. {
  266. bool Result;
  267. TSaveSessionDialog * Dialog = new TSaveSessionDialog(
  268. OriginalSession, (SavePassword != NULL), NotRecommendedSavingPassword);
  269. try
  270. {
  271. bool Dummy = false;
  272. if (SavePassword == NULL)
  273. {
  274. SavePassword = &Dummy;
  275. }
  276. Result = Dialog->Execute(SessionName, *SavePassword);
  277. }
  278. __finally
  279. {
  280. delete Dialog;
  281. }
  282. return Result;
  283. }
  284. //---------------------------------------------------------------------------
  285. void __fastcall SessionNameValidate(const UnicodeString & Text,
  286. TSessionData * RenamingSession)
  287. {
  288. TSessionData::ValidatePath(Text);
  289. assert(StoredSessions);
  290. TSessionData * Data = (TSessionData *)StoredSessions->FindByName(Text);
  291. if (Data && Data->Special)
  292. {
  293. MessageDialog(FMTLOAD(CANNOT_OVERWRITE_SPECIAL_SESSION, (Text)),
  294. qtError, qaOK, HELP_NONE);
  295. Abort();
  296. }
  297. else if (Data && (Data != RenamingSession) &&
  298. MessageDialog(FMTLOAD(CONFIRM_OVERWRITE_SESSION, (Text)),
  299. qtConfirmation, qaYes | qaNo, HELP_SESSION_SAVE_OVERWRITE) != qaYes)
  300. {
  301. Abort();
  302. }
  303. }
  304. //---------------------------------------------------------------------------
  305. //---------------------------------------------------------------------------
  306. class TShortCutDialog : public TCustomDialog
  307. {
  308. public:
  309. __fastcall TShortCutDialog(const TShortCuts & ShortCuts, UnicodeString HelpKeyword);
  310. bool __fastcall Execute(TShortCut & ShortCut);
  311. private:
  312. TComboBox * ShortCutCombo;
  313. };
  314. //---------------------------------------------------------------------------
  315. __fastcall TShortCutDialog::TShortCutDialog(const TShortCuts & ShortCuts, UnicodeString HelpKeyword) :
  316. TCustomDialog(HelpKeyword)
  317. {
  318. Caption = LoadStr(SHORTCUT_CAPTION);
  319. ShortCutCombo = new TComboBox(this);
  320. AddComboBox(ShortCutCombo, CreateLabel(LoadStr(SHORTCUT_LABEL)));
  321. InitializeShortCutCombo(ShortCutCombo, ShortCuts);
  322. }
  323. //---------------------------------------------------------------------------
  324. bool __fastcall TShortCutDialog::Execute(TShortCut & ShortCut)
  325. {
  326. SetShortCutCombo(ShortCutCombo, ShortCut);
  327. bool Result = TCustomDialog::Execute();
  328. if (Result)
  329. {
  330. ShortCut = GetShortCutCombo(ShortCutCombo);
  331. }
  332. return Result;
  333. }
  334. //---------------------------------------------------------------------------
  335. bool __fastcall DoShortCutDialog(TShortCut & ShortCut,
  336. const TShortCuts & ShortCuts, UnicodeString HelpKeyword)
  337. {
  338. bool Result;
  339. TShortCutDialog * Dialog = new TShortCutDialog(ShortCuts, HelpKeyword);
  340. try
  341. {
  342. Result = Dialog->Execute(ShortCut);
  343. }
  344. __finally
  345. {
  346. delete Dialog;
  347. }
  348. return Result;
  349. }