InputDlg.cpp 5.8 KB

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