InputDlg.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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, int AWidth);
  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, int AWidth) :
  35. TCustomDialog(HelpKeyword)
  36. {
  37. Caption = ACaption;
  38. FPathInput = PathInput;
  39. FOnInitialize = OnInitialize;
  40. FHistory = History;
  41. ClientWidth = ScaleByTextHeight(this, AWidth);
  42. TLabel * Label = CreateLabel(Prompt);
  43. int MaxLength = FPathInput ? 0 : 255;
  44. if (History == NULL)
  45. {
  46. if (Echo)
  47. {
  48. Edit = new TEdit(this);
  49. }
  50. else
  51. {
  52. Edit = new TPasswordEdit(this);
  53. }
  54. HistoryCombo = NULL;
  55. AddEditLikeControl(Edit, Label);
  56. reinterpret_cast<TEdit *>(Edit)->MaxLength = MaxLength;
  57. }
  58. else
  59. {
  60. DebugAssert(Echo);
  61. HistoryCombo = new THistoryComboBox(this);
  62. AddEditLikeControl(HistoryCombo, Label);
  63. HistoryCombo->MaxLength = MaxLength;
  64. HistoryCombo->AutoComplete = false;
  65. Edit = NULL;
  66. }
  67. }
  68. //---------------------------------------------------------------------------
  69. void __fastcall TInputDialog::DoShow()
  70. {
  71. TCustomDialog::DoShow();
  72. if (FOnInitialize != NULL)
  73. {
  74. TInputDialogData Data;
  75. Data.Edit = Edit;
  76. FOnInitialize(this, &Data);
  77. }
  78. if (FPathInput)
  79. {
  80. if (FHistory == NULL)
  81. {
  82. InstallPathWordBreakProc(Edit);
  83. }
  84. else
  85. {
  86. InstallPathWordBreakProc(HistoryCombo);
  87. }
  88. }
  89. }
  90. //---------------------------------------------------------------------------
  91. bool __fastcall TInputDialog::Execute(UnicodeString & Value)
  92. {
  93. if (FHistory == NULL)
  94. {
  95. Edit->Text = Value;
  96. Edit->SelectAll();
  97. }
  98. else
  99. {
  100. HistoryCombo->Items = FHistory;
  101. HistoryCombo->Text = Value;
  102. HistoryCombo->SelectAll();
  103. }
  104. bool Result = TCustomDialog::Execute();
  105. if (Result)
  106. {
  107. if (FHistory != NULL)
  108. {
  109. HistoryCombo->SaveToHistory();
  110. FHistory->Assign(HistoryCombo->Items);
  111. Value = HistoryCombo->Text;
  112. }
  113. else
  114. {
  115. Value = Edit->Text;
  116. }
  117. }
  118. return Result;
  119. }
  120. //---------------------------------------------------------------------------
  121. bool __fastcall InputDialog(const UnicodeString ACaption,
  122. const UnicodeString APrompt, UnicodeString & Value, UnicodeString HelpKeyword,
  123. TStrings * History, bool PathInput, TInputDialogInitialize OnInitialize, bool Echo, int Width)
  124. {
  125. std::unique_ptr<TInputDialog> Dialog(new TInputDialog(ACaption, APrompt, HelpKeyword, History, PathInput, OnInitialize, Echo, Width));
  126. return Dialog->Execute(Value);
  127. }
  128. //---------------------------------------------------------------------------