InputDlg.cpp 3.5 KB

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