InputDlg.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 AnsiString ACaption,
  31. const AnsiString APrompt, AnsiString & Value, AnsiString 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(Application, 0); // bypass the VCL streaming (for Salamander)
  43. try
  44. {
  45. SetCorrectFormParent(Form);
  46. UseSystemSettingsPre(Form);
  47. if (OnInitialize != NULL)
  48. {
  49. Token.OnInitialize = OnInitialize;
  50. TNotifyEvent OnShow;
  51. ((TMethod *)&OnShow)->Data = &Token;
  52. ((TMethod *)&OnShow)->Code = InputDialogShow;
  53. Form->OnShow = OnShow;
  54. }
  55. Form->Canvas->Font = Form->Font;
  56. DialogUnits = GetAveCharSize(Form->Canvas);
  57. Form->BorderStyle = bsDialog;
  58. Form->Caption = ACaption;
  59. Form->ClientWidth = MulDiv(220, DialogUnits.x, 4);
  60. Form->ClientHeight = MulDiv(63, DialogUnits.y, 8);
  61. Form->Position = poMainFormCenter;
  62. if (!HelpKeyword.IsEmpty())
  63. {
  64. Form->HelpKeyword = HelpKeyword;
  65. Form->BorderIcons = TBorderIcons(Form->BorderIcons) << biHelp;
  66. }
  67. Prompt = new TLabel(Form);
  68. Prompt->Parent = Form;
  69. Prompt->AutoSize = True;
  70. Prompt->Left = MulDiv(8, DialogUnits.x, 4);
  71. Prompt->Top = MulDiv(8, DialogUnits.y, 8);
  72. Prompt->Caption = APrompt;
  73. TWinControl * EditControl;
  74. if (History == NULL)
  75. {
  76. Edit = new TEdit(Form);
  77. Edit->Parent = Form;
  78. Edit->Text = Value;
  79. Edit->SelectAll();
  80. Edit->MaxLength = 255;
  81. Token.Data.Edit = Edit;
  82. EditControl = Edit;
  83. }
  84. else
  85. {
  86. HistoryCombo = new THistoryComboBox(Form);
  87. HistoryCombo->Parent = Form;
  88. HistoryCombo->Text = Value;
  89. HistoryCombo->SelectAll();
  90. HistoryCombo->Items = History;
  91. HistoryCombo->MaxLength = 255;
  92. EditControl = HistoryCombo;
  93. }
  94. EditControl->Left = Prompt->Left;
  95. EditControl->Top = MulDiv(19, DialogUnits.y, 8);
  96. EditControl->Width = MulDiv(204, DialogUnits.x, 4);
  97. if (PathInput)
  98. {
  99. InstallPathWordBreakProc(EditControl);
  100. }
  101. Prompt->FocusControl = EditControl;
  102. ButtonTop = MulDiv(41, DialogUnits.y, 8);
  103. ButtonWidth = MulDiv(50, DialogUnits.x, 4);
  104. ButtonHeight = MulDiv(14, DialogUnits.y, 8);
  105. int ButtonSpace = MulDiv(5, DialogUnits.x, 4);
  106. int ButtonsStart;
  107. if (HelpKeyword.IsEmpty())
  108. {
  109. ButtonsStart = (Form->ClientWidth / 2) - ButtonWidth - (ButtonSpace / 2);
  110. }
  111. else
  112. {
  113. ButtonsStart = (Form->ClientWidth / 2) - (3 * ButtonWidth / 2) - ButtonSpace;
  114. }
  115. TButton * Button;
  116. Button = new TButton(Form);
  117. Button->Parent = Form;
  118. Button->Caption = Consts_SMsgDlgOK;
  119. Button->ModalResult = mrOk;
  120. Button->Default = True;
  121. Button->SetBounds(ButtonsStart, ButtonTop, ButtonWidth, ButtonHeight);
  122. Button = new TButton(Form);
  123. Button->Parent = Form;
  124. Button->Caption = Consts_SMsgDlgCancel;
  125. Button->ModalResult = mrCancel;
  126. Button->Cancel = True;
  127. Button->SetBounds(ButtonsStart + ButtonWidth + ButtonSpace, ButtonTop,
  128. ButtonWidth, ButtonHeight);
  129. if (!HelpKeyword.IsEmpty())
  130. {
  131. Button = new TButton(Form);
  132. Button->Parent = Form;
  133. Button->Caption = Consts_SMsgDlgHelp;
  134. Button->ModalResult = mrNone;
  135. Button->SetBounds(ButtonsStart + 2 * (ButtonWidth + ButtonSpace), ButtonTop,
  136. ButtonWidth, ButtonHeight);
  137. TNotifyEvent OnClick;
  138. ((TMethod*)&OnClick)->Code = InputDialogHelp;
  139. Button->OnClick = OnClick;
  140. }
  141. UseSystemSettingsPost(Form);
  142. if (Form->ShowModal() == mrOk)
  143. {
  144. if (History != NULL)
  145. {
  146. HistoryCombo->SaveToHistory();
  147. History->Assign(HistoryCombo->Items);
  148. Value = HistoryCombo->Text;
  149. }
  150. else
  151. {
  152. Value = Edit->Text;
  153. }
  154. Result = true;
  155. }
  156. }
  157. __finally
  158. {
  159. delete Form;
  160. }
  161. return Result;
  162. }
  163. //---------------------------------------------------------------------------