unit ANBInputBox;
{$MODE Delphi}
{$codepage UTF8}
interface
uses LCLIntf, LCLType, SysUtils, Graphics, Controls, Forms, StdCtrls, Classes, Types, MaskEdit, Spin, VCLEx;
function ShowInputBox (const ACaption, APrompt, ADefault: String; var isAccept: Boolean): String; OVERLOAD; STDCALL;
function ShowInputBox (const ACaption, APrompt, ADefault: String; const ABtnCaptions: array of String; var isAccept: Boolean): String; OVERLOAD; STDCALL;
function ShowInputBox (const ACaption, APrompt, ADefault: String; const ABtnCaptions: array of String; const AMaxLength: IntEx; var isAccept: Boolean): String; OVERLOAD; STDCALL;
function ShowPasswordInputBox (const ACaption, APrompt, ADefault, APassword: String; var isAccept, isPasswordConfirmed: Boolean; const APasswordChar: Char = '*'): String; OVERLOAD; STDCALL;
function ShowPasswordInputBox (const ACaption, APrompt, ADefault, APassword: String; var isAccept, isPasswordConfirmed: Boolean; const ABtnCaptions: array of String; const APasswordChar: Char = '*'): String; OVERLOAD; STDCALL;
function ShowComboBox (const ACaption, APrompt: String; var Items: TStringList; const ADefaultIndex: IntEx; var isAccept: Boolean): IntEx; OVERLOAD; STDCALL;
function ShowComboBox (const ACaption, APrompt: String; var Items: TStringList; const ADefaultIndex: IntEx; const ABtnsCaption: array of String; var isAccept: Boolean): IntEx; OVERLOAD; STDCALL;
function ShowMaskedBox (const ACaption, APrompt, AMask, ADefault: String; const isIgnoreMask: Boolean;  var isAccept: Boolean): String; OVERLOAD; STDCALL;
function ShowMaskedBox (const ACaption, APrompt, AMask, ADefault: String; const isIgnoreMask: Boolean; const ABtnCaptions: array of String; const AMaxLength: IntEx; var isAccept: Boolean): String; OVERLOAD; STDCALL;
function ShowSpinedBox (const ACaption, APrompt: String; const ADefault: IntEx; var isAccept: Boolean): IntEx; STDCALL; OVERLOAD;
function ShowSpinedBox (const ACaption, APrompt: String; const ADefault, AMinValue, AMaxValue: IntEx; const ABtnCaptions: array of String; var isAccept: Boolean): IntEx; STDCALL; OVERLOAD;
const OkBtnDefCaption: String = '&Ok';
      CancelBtnDefCaption: String = 'C&ancel';
implementation
function MyGetAveCharSize(Canvas: TCanvas): TPoint;
var I: IntEx;
    Buffer: array[0..51] of Char;
begin
with Result do
 begin
 x:= 0;
 y:= 0;
 end;
for I:= 0 to 25 do
 Buffer[I]:= Chr(I + Ord('A'));
for I:= 0 to 25 do
 Buffer[I + 26]:= Chr(I + Ord('a'));
GetTextExtentPoint(Canvas.Handle, Buffer, 52, TSize(Result));
Result.X:= Result.X div 52;
end;
function MyInputQuery(const ACaption, APrompt: String; var Value: String; var APasswordConfirmed: Boolean; const APassword: Boolean; const ANeedPassword: String; const APasswordChar: Char; const AMaxLength: IntEx; const ABtnsCaption: array of String): Boolean;
var Form: TForm;
    Prompt: TLabel;
    Edit: TEdit;
    DialogUnits: TPoint;
    ButtonTop, ButtonWidth, ButtonHeight: IntEx;
begin
Result:= false;
Form:= TForm.Create(Application);
with Form do
 try
  Canvas.Font:= Font;
  DialogUnits:= MyGetAveCharSize(Canvas);
  BorderStyle:= bsDialog;
  Caption:= ACaption;
  ClientWidth:= MulDiv(180, DialogUnits.X, 4);
  Position:= poScreenCenter;
  Color:= clBtnFace;
  Prompt:= TLabel.Create(Form);
  with Prompt do
   begin
   Parent:= Form;
   Caption:= APrompt;
   Left:= MulDiv(8, DialogUnits.X, 4);
   Top:= MulDiv(8, DialogUnits.Y, 8);
   Constraints.MaxWidth:= MulDiv(164, DialogUnits.X, 4);
   WordWrap:= True;
   end;
  Edit:= TEdit.Create(Form);
  with Edit do
   begin
   Parent:= Form;
   Left:= Prompt.Left;
   Top:= Prompt.Top + Prompt.Height + 5;
   Width:= MulDiv(164, DialogUnits.X, 4);
   MaxLength:= AMaxLength;
   if APassword then
    PasswordChar:= APasswordChar;
   Text:= Value;
   SelectAll;
   end;
  ButtonTop:= Edit.Top + Edit.Height + 15;
  ButtonWidth:= MulDiv(50, DialogUnits.X, 4);
  ButtonHeight:= MulDiv(14, DialogUnits.Y, 8);
  with TButton.Create(Form) do
   begin
   Parent:= Form;
   Caption:= ABtnsCaption[0];
   ModalResult:= mrOk;
   Cursor:= crHandPoint;
   Default:= True;
   SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth, ButtonHeight);
   end;
  with TButton.Create(Form) do
   begin
   Parent:= Form;
   Caption:= ABtnsCaption[1];
   ModalResult:= mrCancel;
   Cursor:= crHandPoint;
   Cancel:= True;
   SetBounds(MulDiv(92, DialogUnits.X, 4), Edit.Top + Edit.Height + 15, ButtonWidth, ButtonHeight);
   Form.ClientHeight:= Top + Height + 13;
   end;
  if ShowModal = mrOk then
   begin
   Value:= Edit.Text;
   if APassword then
    if Value = ANeedPassword then
     APasswordConfirmed:= true
     else
     APasswordConfirmed:= false;
   Result:= True;
   end;
 finally
  Form.Free;
  end;
end;
function MyInputComboQuery(const ACaption, APrompt: String; const Values: TStringList; const DefIndex: IntEx; var SelectedIndex: IntEx; const ABtnsCaption: array of String): Boolean;
var Form: TForm;
    Prompt: TLabel;
    Combo: TComboBox;
    DialogUnits: TPoint;
    ButtonTop, ButtonWidth, ButtonHeight: IntEx;
begin
Result:= false;
Form:= TForm.Create(Application);
with Form do
 try
  Canvas.Font:= Font;
  DialogUnits:= MyGetAveCharSize(Canvas);
  BorderStyle:= bsDialog;
  Caption:= ACaption;
  ClientWidth:= MulDiv(180, DialogUnits.X, 4);
  Position:= poScreenCenter;
  Color:= clBtnFace;
  Prompt:= TLabel.Create(Form);
  with Prompt do
   begin
   Parent:= Form;
   Caption:= APrompt;
   Left:= MulDiv(8, DialogUnits.X, 4);
   Top:= MulDiv(8, DialogUnits.Y, 8);
   Constraints.MaxWidth:= MulDiv(164, DialogUnits.X, 4);
   WordWrap:= True;
   end;
  Combo:= TComboBox.Create(Form);
  with Combo do
   begin
   Parent:= Form;
   Left:= Prompt.Left;
   Top:= Prompt.Top + Prompt.Height + 5;
   Width:= MulDiv(164, DialogUnits.X, 4);
   Style:= csDropDownList;
   Items.Assign(Values);
   if DefIndex <= Items.Count - 1 then
    ItemIndex:= DefIndex
    else
    ItemIndex:= -1;
   end;
  ButtonTop:= Combo.Top + Combo.Height + 15;
  ButtonWidth:= MulDiv(50, DialogUnits.X, 4);
  ButtonHeight:= MulDiv(14, DialogUnits.Y, 8);
  with TButton.Create(Form) do
   begin
   Parent:= Form;
   Caption:= ABtnsCaption[0];
   ModalResult:= mrOk;
   Cursor:= crHandPoint;
   Default:= True;
   SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth, ButtonHeight);
   end;
  with TButton.Create(Form) do
   begin
   Parent:= Form;
   Caption:= ABtnsCaption[1];
   ModalResult:= mrCancel;
   Cursor:= crHandPoint;
   Cancel:= True;
   SetBounds(MulDiv(92, DialogUnits.X, 4), Combo.Top + Combo.Height + 15, ButtonWidth, ButtonHeight);
   Form.ClientHeight:= Top + Height + 13;
   end;
  if ShowModal = mrOk then
   begin
   SelectedIndex:= Combo.ItemIndex;
   Result:= True;
   end;
 finally
  Form.Free;
  end;
end;
function MyInputMaskQuery(const ACaption, APrompt, AMask: String; var Value: String; const isIgnoreMask: Boolean; const AMaxLength: IntEx; const ABtnsCaption: array of String): Boolean;
var Form: TForm;
    Prompt: TLabel;
    Edit: TMaskEdit;
    DialogUnits: TPoint;
    ButtonTop, ButtonWidth, ButtonHeight: IntEx;
begin
Result:= false;
Form:= TForm.Create(Application);
with Form do
 try
  Canvas.Font:= Font;
  DialogUnits:= MyGetAveCharSize(Canvas);
  BorderStyle:= bsDialog;
  Caption:= ACaption;
  ClientWidth:= MulDiv(180, DialogUnits.X, 4);
  Position:= poScreenCenter;
  Color:= clBtnFace;
  Prompt:= TLabel.Create(Form);
  with Prompt do
   begin
   Parent:= Form;
   Caption:= APrompt;
   Left:= MulDiv(8, DialogUnits.X, 4);
   Top:= MulDiv(8, DialogUnits.Y, 8);
   Constraints.MaxWidth:= MulDiv(164, DialogUnits.X, 4);
   WordWrap:= True;
   end;
  Edit:= TMaskEdit.Create(Form);
  with Edit do
   begin
   Parent:= Form;
   Left:= Prompt.Left;
   Top:= Prompt.Top + Prompt.Height + 5;
   Width:= MulDiv(164, DialogUnits.X, 4);
   MaxLength:= AMaxLength;
   EditMask:= AMask;
   if isIgnoreMask then
    Text:= Value
    else
    EditText:= Value;
   ValidateEdit;
   end;
  ButtonTop:= Edit.Top + Edit.Height + 15;
  ButtonWidth:= MulDiv(50, DialogUnits.X, 4);
  ButtonHeight:= MulDiv(14, DialogUnits.Y, 8);
  with TButton.Create(Form) do
   begin
   Parent:= Form;
   Caption:= ABtnsCaption[0];
   ModalResult:= mrOk;
   Cursor:= crHandPoint;
   Default:= True;
   SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth, ButtonHeight);
   end;
  with TButton.Create(Form) do
   begin
   Parent:= Form;
   Caption:= ABtnsCaption[1];
   ModalResult:= mrCancel;
   Cursor:= crHandPoint;
   Cancel:= True;
   SetBounds(MulDiv(92, DialogUnits.X, 4), Edit.Top + Edit.Height + 15, ButtonWidth, ButtonHeight);
   Form.ClientHeight:= Top + Height + 13;
   end;
  if ShowModal = mrOk then
   begin
   if isIgnoreMask then
    Value:= Edit.Text
    else
    Value:= Edit.EditText;
   Result:= True;
   end;
 finally
  Form.Free;
  end;
end;
function MyInputSpinQuery(const ACaption, APrompt: String; var _Value: IntEx; const AMinValue, AMaxValue: IntEx; const ABtnsCaption: array of String): Boolean;
var Form: TForm;
    Prompt: TLabel;
    Edit: TSpinEdit;
    DialogUnits: TPoint;
    ButtonTop, ButtonWidth, ButtonHeight: IntEx;
begin
Result:= False;
Form:= TForm.Create(Application);
with Form do
 try
  Canvas.Font:= Font;
  DialogUnits:= MyGetAveCharSize(Canvas);
  BorderStyle:= bsDialog;
  Caption:= ACaption;
  ClientWidth:= MulDiv(180, DialogUnits.X, 4);
  Position:= poScreenCenter;
  Color:= clBtnFace;
  Prompt:= TLabel.Create(Form);
  with Prompt do
   begin
   Parent:= Form;
   Caption:= APrompt;
   Left:= MulDiv(8, DialogUnits.X, 4);
   Top:= MulDiv(8, DialogUnits.Y, 8);
   Constraints.MaxWidth:= MulDiv(164, DialogUnits.X, 4);
   WordWrap:= True;
   end;
  Edit:= TSpinEdit.Create(Form);
  with Edit do
   begin
   Parent:= Form;
   Left:= Prompt.Left;
   Top:= Prompt.Top + Prompt.Height + 5;
   Width:= MulDiv(164, DialogUnits.X, 4);
   MinValue:= Integer(AMinValue);
   MaxValue:= Integer(AMaxValue);
   if _Value < AMinValue then
    Value:= AMinValue
    else
    if _Value > AMaxValue then
     Value:= AMaxValue
     else
     Value:= _Value;
   SelectAll;
   end;
  ButtonTop:= Edit.Top + Edit.Height + 15;
  ButtonWidth:= MulDiv(50, DialogUnits.X, 4);
  ButtonHeight:= MulDiv(14, DialogUnits.Y, 8);
  with TButton.Create(Form) do
   begin
   Parent:= Form;
   Caption:= ABtnsCaption[0];
   ModalResult:= mrOk;
   Cursor:= crHandPoint;
   Default:= True;
   SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth, ButtonHeight);
   end;
  with TButton.Create(Form) do
   begin
   Parent:= Form;
   Caption:= ABtnsCaption[1];
   ModalResult:= mrCancel;
   Cursor:= crHandPoint;
   Cancel:= True;
   SetBounds(MulDiv(92, DialogUnits.X, 4), Edit.Top + Edit.Height + 15, ButtonWidth, ButtonHeight);
   Form.ClientHeight:= Top + Height + 13;
   end;
  if ShowModal = mrOk then
   begin
   _Value:= Edit.Value;
   Result:= True;
   end;
 finally
  Form.Free;
  end;
end;
function ShowInputBox (const ACaption, APrompt, ADefault: String; var isAccept: Boolean): String;
var Pasw: boolean;
begin
Result:= ADefault;
Pasw:= False;
isAccept:= MyInputQuery(ACaption, APrompt, Result, Pasw, False, '', '*', 0, [OkBtnDefCaption, CancelBtnDefCaption]);
end;
function ShowInputBox (const ACaption, APrompt, ADefault: String; const ABtnCaptions: array of String; var isAccept: Boolean): String;
var Pasw: boolean;
begin
Result:= ADefault;
Pasw:= False;
isAccept:= MyInputQuery(ACaption, APrompt, Result, Pasw, False, '', '*', 0, ABtnCaptions);
end;
function ShowInputBox (const ACaption, APrompt, ADefault: String; const ABtnCaptions: array of String; const AMaxLength: IntEx; var isAccept: Boolean): String;
var Pasw: boolean;
begin
Result:= ADefault;
Pasw:= False;
isAccept:= MyInputQuery(ACaption, APrompt, Result, Pasw, false, '', '*', AMaxLength, ABtnCaptions);
end;
function ShowPasswordInputBox (const ACaption, APrompt, ADefault, APassword: String; var isAccept, isPasswordConfirmed: Boolean; const APasswordChar: Char = '*'): String;
begin
Result:= ADefault;
isAccept:= MyInputQuery(ACaption, APrompt, Result, isPasswordConfirmed, True, APassword, APasswordChar, 0, [OkBtnDefCaption, CancelBtnDefCaption]);
end;
function ShowPasswordInputBox (const ACaption, APrompt, ADefault, APassword: String; var isAccept, isPasswordConfirmed: Boolean; const ABtnCaptions: array of String; const APasswordChar: Char = '*'): String;
begin
Result:= ADefault;
isAccept:= MyInputQuery(ACaption, APrompt, Result, isPasswordConfirmed, True, APassword, APasswordChar, 0, ABtnCaptions);
end;
function ShowComboBox (const ACaption, APrompt: string; var Items: TStringList; const ADefaultIndex: IntEx; var isAccept: boolean): IntEx;
begin
Result:= ADefaultIndex;
isAccept:= MyInputComboQuery(ACaption, APrompt, Items, Result, Result, [OkBtnDefCaption, CancelBtnDefCaption]);
end;
function ShowComboBox (const ACaption, APrompt: String; var Items: TStringList; const ADefaultIndex: IntEx; const ABtnsCaption: array of String; var isAccept: Boolean): IntEx;
begin
Result:= ADefaultIndex;
isAccept:= MyInputComboQuery(ACaption, APrompt, Items, Result, Result, ABtnsCaption);
end;
function ShowMaskedBox (const ACaption, APrompt, AMask, ADefault: String; const isIgnoreMask: Boolean; var isAccept: Boolean): String;
begin
Result:= ADefault;
isAccept:= MyInputMaskQuery(ACaption, APrompt, AMask, Result, isIgnoreMask, 0, [OkBtnDefCaption, CancelBtnDefCaption]);
end;
function ShowMaskedBox (const ACaption, APrompt, AMask, ADefault: String; const isIgnoreMask: Boolean; const ABtnCaptions: array of String; const AMaxLength: IntEx; var isAccept: Boolean): String;
begin
Result:= ADefault;
isAccept:= MyInputMaskQuery(ACaption, APrompt, AMask, Result, isIgnoreMask,  AMaxLength, ABtnCaptions);
end;
function ShowSpinedBox (const ACaption, APrompt: String; const ADefault: IntEx; var isAccept: Boolean): IntEx;
begin
Result:= ADefault;
isAccept:= MyInputSpinQuery(ACaption, APrompt, Result, 0, 100, [OkBtnDefCaption, CancelBtnDefCaption]);
end;
function ShowSpinedBox (const ACaption, APrompt: String; const ADefault, AMinValue, AMaxValue: IntEx; const ABtnCaptions: array of String; var isAccept: Boolean): IntEx;
begin
Result:= ADefault;
isAccept:= MyInputSpinQuery(ACaption, APrompt, Result, AMinValue,  AMaxValue, ABtnCaptions);
end;
end.
