InputDlg.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "WinInterface.h"
  5. #include <VCLCommon.h>
  6. #include <Windows.hpp>
  7. #include <Consts.hpp>
  8. #include <HistoryComboBox.hpp>
  9. #include <PasTools.hpp>
  10. #include <PasswordEdit.hpp>
  11. #include <Custom.h>
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. //---------------------------------------------------------------------------
  15. class TInputDialog : public TCustomDialog
  16. {
  17. public:
  18. __fastcall TInputDialog(
  19. const UnicodeString & ACaption, const UnicodeString & Prompt, const UnicodeString & HelpKeyword,
  20. TStrings * History, bool PathInput, TInputDialogInitialize OnInitialize, bool Echo);
  21. bool __fastcall Execute(UnicodeString & Value);
  22. protected:
  23. DYNAMIC void __fastcall DoShow();
  24. private:
  25. bool FPathInput;
  26. TInputDialogInitialize FOnInitialize;
  27. TStrings * FHistory;
  28. TCustomEdit * Edit;
  29. THistoryComboBox * HistoryCombo;
  30. };
  31. //---------------------------------------------------------------------------
  32. __fastcall TInputDialog::TInputDialog(
  33. const UnicodeString & ACaption, const UnicodeString & Prompt, const UnicodeString & HelpKeyword,
  34. TStrings * History, bool PathInput, TInputDialogInitialize OnInitialize, bool Echo) :
  35. TCustomDialog(HelpKeyword)
  36. {
  37. Caption = ACaption;
  38. FPathInput = PathInput;
  39. FOnInitialize = OnInitialize;
  40. FHistory = History;
  41. ClientWidth = ScaleByTextHeight(this, 275);
  42. TLabel * Label = CreateLabel(Prompt);
  43. if (History == NULL)
  44. {
  45. if (Echo)
  46. {
  47. Edit = new TEdit(this);
  48. }
  49. else
  50. {
  51. Edit = new TPasswordEdit(this);
  52. }
  53. HistoryCombo = NULL;
  54. AddEditLikeControl(Edit, Label);
  55. reinterpret_cast<TEdit *>(Edit)->MaxLength = 255;
  56. }
  57. else
  58. {
  59. DebugAssert(Echo);
  60. HistoryCombo = new THistoryComboBox(this);
  61. AddEditLikeControl(HistoryCombo, Label);
  62. HistoryCombo->MaxLength = 255;
  63. HistoryCombo->AutoComplete = false;
  64. Edit = NULL;
  65. }
  66. }
  67. //---------------------------------------------------------------------------
  68. void __fastcall TInputDialog::DoShow()
  69. {
  70. TCustomDialog::DoShow();
  71. if (FOnInitialize != NULL)
  72. {
  73. TInputDialogData Data;
  74. Data.Edit = Edit;
  75. FOnInitialize(this, &Data);
  76. }
  77. if (FPathInput)
  78. {
  79. if (FHistory == NULL)
  80. {
  81. InstallPathWordBreakProc(Edit);
  82. }
  83. else
  84. {
  85. InstallPathWordBreakProc(HistoryCombo);
  86. }
  87. }
  88. }
  89. //---------------------------------------------------------------------------
  90. bool __fastcall TInputDialog::Execute(UnicodeString & Value)
  91. {
  92. if (FHistory == NULL)
  93. {
  94. Edit->Text = Value;
  95. Edit->SelectAll();
  96. }
  97. else
  98. {
  99. HistoryCombo->Items = FHistory;
  100. HistoryCombo->Text = Value;
  101. HistoryCombo->SelectAll();
  102. }
  103. bool Result = TCustomDialog::Execute();
  104. if (Result)
  105. {
  106. if (FHistory != NULL)
  107. {
  108. HistoryCombo->SaveToHistory();
  109. FHistory->Assign(HistoryCombo->Items);
  110. Value = HistoryCombo->Text;
  111. }
  112. else
  113. {
  114. Value = Edit->Text;
  115. }
  116. }
  117. return Result;
  118. }
  119. //---------------------------------------------------------------------------
  120. bool __fastcall InputDialog(const UnicodeString ACaption,
  121. const UnicodeString APrompt, UnicodeString & Value, UnicodeString HelpKeyword,
  122. TStrings * History, bool PathInput, TInputDialogInitialize OnInitialize, bool Echo)
  123. {
  124. std::unique_ptr<TInputDialog> Dialog(new TInputDialog(ACaption, APrompt, HelpKeyword, History, PathInput, OnInitialize, Echo));
  125. return Dialog->Execute(Value);
  126. }
  127. //---------------------------------------------------------------------------