1
0

InputDlg.cpp 3.4 KB

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