InputDlg.cpp 3.7 KB

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