InputDlg.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. //---------------------------------------------------------------------------
  12. #pragma package(smart_init)
  13. //---------------------------------------------------------------------------
  14. struct TInputDialogToken
  15. {
  16. TInputDialogInitialize OnInitialize;
  17. TInputDialogData Data;
  18. TWinControl * EditControl;
  19. bool PathInput;
  20. };
  21. //---------------------------------------------------------------------------
  22. void __fastcall InputDialogHelp(void * /*Data*/, TObject * Sender)
  23. {
  24. TControl * Control = dynamic_cast<TControl *>(Sender);
  25. Application->HelpKeyword(Control->Parent->HelpKeyword);
  26. }
  27. //---------------------------------------------------------------------------
  28. void __fastcall InputDialogShow(void * Data, TObject * Sender)
  29. {
  30. TInputDialogToken & Token = *static_cast<TInputDialogToken *>(Data);
  31. if (Token.OnInitialize != NULL)
  32. {
  33. Token.OnInitialize(Sender, &Token.Data);
  34. }
  35. if (Token.PathInput)
  36. {
  37. InstallPathWordBreakProc(Token.EditControl);
  38. }
  39. }
  40. //---------------------------------------------------------------------------
  41. bool __fastcall InputDialog(const UnicodeString ACaption,
  42. const UnicodeString APrompt, UnicodeString & Value, UnicodeString HelpKeyword,
  43. TStrings * History, bool PathInput, TInputDialogInitialize OnInitialize, bool Echo)
  44. {
  45. bool Result = False;
  46. TInputDialogToken Token;
  47. TForm * Form = new TForm(GetFormOwner(), 0); // bypass the VCL streaming (for Salamander)
  48. try
  49. {
  50. // salam needs to override this in UseSystemSettings
  51. Form->Position = poOwnerFormCenter;
  52. SetCorrectFormParent(Form);
  53. UseSystemSettingsPre(Form);
  54. // this is what TCustomForm.Loaded does
  55. // Note that this is not needed as due to use of an alternative constructor above
  56. // we are already set to default font
  57. // See TMessageForm::Create for contrary
  58. Form->Font->Assign(Application->DefaultFont);
  59. Form->ParentFont = true;
  60. Token.OnInitialize = OnInitialize;
  61. Token.PathInput = PathInput;
  62. TNotifyEvent OnShow;
  63. ((TMethod *)&OnShow)->Data = &Token;
  64. ((TMethod *)&OnShow)->Code = InputDialogShow;
  65. Form->OnShow = OnShow;
  66. Form->Canvas->Font = Form->Font;
  67. Form->BorderStyle = bsDialog;
  68. Form->Caption = ACaption;
  69. Form->ClientWidth = ScaleByTextHeightRunTime(Form, 275);
  70. Form->ClientHeight = ScaleByTextHeightRunTime(Form, 102);
  71. if (!HelpKeyword.IsEmpty())
  72. {
  73. Form->HelpKeyword = HelpKeyword;
  74. Form->BorderIcons = TBorderIcons(Form->BorderIcons) << biHelp;
  75. }
  76. TLabel * Prompt = new TLabel(Form);
  77. Prompt->Parent = Form;
  78. Prompt->AutoSize = True;
  79. Prompt->Left = ScaleByTextHeightRunTime(Form, 10);
  80. Prompt->Top = ScaleByTextHeightRunTime(Form, 13);
  81. Prompt->Caption = APrompt;
  82. TCustomEdit * Edit;
  83. THistoryComboBox * HistoryCombo;
  84. if (History == NULL)
  85. {
  86. if (Echo)
  87. {
  88. Edit = new TEdit(Form);
  89. }
  90. else
  91. {
  92. Edit = new TPasswordEdit(Form);
  93. }
  94. Edit->Parent = Form;
  95. Edit->Text = Value;
  96. Edit->SelectAll();
  97. reinterpret_cast<TEdit *>(Edit)->MaxLength = 255;
  98. Token.Data.Edit = Edit;
  99. Token.EditControl = Edit;
  100. }
  101. else
  102. {
  103. DebugAssert(Echo);
  104. HistoryCombo = new THistoryComboBox(Form);
  105. HistoryCombo->Parent = Form;
  106. HistoryCombo->Text = Value;
  107. HistoryCombo->SelectAll();
  108. HistoryCombo->Items = History;
  109. HistoryCombo->MaxLength = 255;
  110. HistoryCombo->AutoComplete = false;
  111. Token.EditControl = HistoryCombo;
  112. }
  113. Token.EditControl->Left = Prompt->Left;
  114. Token.EditControl->Top = ScaleByTextHeightRunTime(Form, 30);
  115. Token.EditControl->Width = ScaleByTextHeightRunTime(Form, 255);
  116. Prompt->FocusControl = Token.EditControl;
  117. int ButtonTop = ScaleByTextHeightRunTime(Form, 66);
  118. int ButtonSpace = ScaleByTextHeightRunTime(Form, 6);
  119. TButton * Button;
  120. Button = new TButton(Form);
  121. Button->Parent = Form;
  122. Button->Caption = Vcl_Consts_SMsgDlgOK;
  123. Button->ModalResult = mrOk;
  124. Button->Default = True;
  125. int ButtonHeight = ScaleByTextHeightRunTime(Button, Button->Height);
  126. int ButtonWidth = ScaleByTextHeightRunTime(Button, Button->Width);
  127. int ButtonsStart;
  128. if (HelpKeyword.IsEmpty())
  129. {
  130. ButtonsStart = (Form->ClientWidth / 2) - ButtonWidth - (ButtonSpace / 2);
  131. }
  132. else
  133. {
  134. ButtonsStart = (Form->ClientWidth / 2) - (3 * ButtonWidth / 2) - ButtonSpace;
  135. }
  136. Button->SetBounds(ButtonsStart, ButtonTop, ButtonWidth, ButtonHeight);
  137. Button = new TButton(Form);
  138. Button->Parent = Form;
  139. Button->Caption = Vcl_Consts_SMsgDlgCancel;
  140. Button->ModalResult = mrCancel;
  141. Button->Cancel = True;
  142. Button->SetBounds(ButtonsStart + ButtonWidth + ButtonSpace, ButtonTop,
  143. ButtonWidth, ButtonHeight);
  144. if (!HelpKeyword.IsEmpty())
  145. {
  146. Button = new TButton(Form);
  147. Button->Parent = Form;
  148. Button->Caption = Vcl_Consts_SMsgDlgHelp;
  149. Button->ModalResult = mrNone;
  150. Button->SetBounds(ButtonsStart + 2 * (ButtonWidth + ButtonSpace), ButtonTop,
  151. ButtonWidth, ButtonHeight);
  152. TNotifyEvent OnClick;
  153. ((TMethod*)&OnClick)->Code = InputDialogHelp;
  154. Button->OnClick = OnClick;
  155. }
  156. UseSystemSettingsPost(Form);
  157. if (Form->ShowModal() == DefaultResult(Form))
  158. {
  159. if (History != NULL)
  160. {
  161. HistoryCombo->SaveToHistory();
  162. History->Assign(HistoryCombo->Items);
  163. Value = HistoryCombo->Text;
  164. }
  165. else
  166. {
  167. Value = Edit->Text;
  168. }
  169. Result = true;
  170. }
  171. }
  172. __finally
  173. {
  174. delete Form;
  175. }
  176. return Result;
  177. }
  178. //---------------------------------------------------------------------------