InputDlg.cpp 5.4 KB

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