(************************************************************************
*    This File is Part of the AHM Triton Tools                          *
*    for Borland Delphi and C++ Builder.                                *
*************************************************************************
* Copyright © 1995-1999 Alexander H. Mehlhorn (AHM) and Tritontools.com *
* All Rights reserved.                                                  *
*                                                                       *
*    For more Information contact us at                                 *
*    E-Mail : support@tritontools.com                                   *
*    URL : http://www.tritontools.com                                   *
*                                                                       *
*    AHM is copyright by Alexander H. Mehlhorn                          *
*    All Parts of this Collection were developed,                       *
*    designed and implemented by AHM                                    *
*                                                                       *
*    Under no Circumstances may you distribute this file                *
*    in this or any other format. This File is supplied                 *
*    as Evaluation of Source only. No Delphi Components may be developed*
*    using extracts or subsets of this code without expressed permission*
*    by AHM.                                                            *
*                                                                       *
*************************************************************************)

unit AHMTPasswordDlg;

interface
{$I AHM.INC}
uses
  SysUtils, Windows, Messages, Classes, Graphics, Controls,
  Forms, AHMDialogClass, ExtCtrls, Buttons, StdCtrls;

type
  TAHMOnRetry = procedure(Sender : TObject;RetriesLeft : Integer) of Object;
  TAHMPasswordDlg = class(TAHMAdvDialogComponent)
  private
    FFont : Tfont;
    FPasswordChar : char;
    FNoOfRetries : byte;
    FGlyphCancel,FGlyphOk : TBitmap;
    FScramble,FPassword : String;
    FOnRightPassword,FOnWrongPassword,FOnOk,FOnCancel : TNotifyEvent;
    FOnRetry : TAHMOnRetry;
    FAfterRetries: TNotifyevent;
    Procedure SetFont(Value : TFont);
    Procedure Setpassw(value : string);
    Procedure Setscramble(value : string);
    Procedure SetGlyphOk(value : Tbitmap);
    Procedure SetGlyphCancel(value : Tbitmap);
    procedure DoBitBtn2Click(Sender: TObject);
    procedure DoBitBtn1Click(Sender: TObject);
    procedure DoFormShow(Sender: TObject);
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    Function Execute : Boolean; override;
  published
    Property Fonts           : TFont read FFont write SetFont;
    Property Passwordchar    : Char read FPasswordChar write FPasswordChar;
    Property GlyphCancel     : TBitmap read FGlyphCancel write SetGlyphCancel;
    Property GlyphOk         : TBitmap read FGlyphOk write SetGlyphOk;
    Property OnRightPassword : TNotifyevent read FOnRightPassword write FOnRightPassword;
    Property OnWrongPassword : TNotifyevent read FOnWrongPassword write FOnWrongPassword;
    Property OnOk            : TNotifyevent read FOnOk write FOnOk;
    Property OnCancel        : TNotifyevent read FOnCancel write FOnCancel;
    Property OnRetry    : TAHMOnRetry read FOnRetry write FOnRetry;
    Property AfterRetries : TNotifyevent read FAfterRetries write FAfterRetries;
    Property Scramble        : String read FScramble write Setscramble;
    Property Password        : String read FPassword write SetPassw;
    Property NoOfRetries     : Byte read FNoOfRetries write FNoOfRetries default 3;
  end;

type
  TPasswordForm = class(TForm)
    PassEdit: TEdit;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    FPasswordLabel: TLabel;
    FUserLabel: TLabel;
    UserEdit: TEdit;
    Image1: TImage;
    Image2: TImage;
    procedure PASSKeyPress(Sender: TObject; var Key: Char);
    procedure PASSChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
  public
    xsret : Byte;
    HasPassword : Boolean;
    procedure SetupForm;
  end;

Var Passform : TPasswordForm;

implementation
uses AHMDialogConst;

procedure TPasswordForm.SetupForm;
begin
  Left := 100;
  Top := 100;
  BorderIcons := [biSystemMenu];
  Color := clBtnFace;
  OnCreate := FormCreate;
  FPasswordLabel:=TLabel.Create(Self);
  with TBevel.Create(Self) do
  begin
    Parent:=Self;
    Top:=10; Left:=10;
    Height:=self.ClientHeight-20; Width:=Self.ClientWidth-20;
    Shape:=bsFrame;
  end;
  with FPasswordLabel do
  begin
    Parent:=self;
    Left := 16;
    Top := 60;
    Width := 55;
    Height := 13;
    Caption := ahmrcdlgpassword;
    FocusControl := PassEdit;
    Transparent := True;
  end;
  FUserLabel:=TLabel.Create(Self);
  with FUserLabel do
  begin
    Parent:=Self;
    Left := 16;
    Top := 20;
    Width := 57;
    Height := 13;
    Caption := ahmrcdlgusername;
    FocusControl := UserEdit;
    Transparent := True;
  end;
  Image1:= TImage.Create(Self);
  with Image1 do
  begin
    Parent:=Self;
    Left := Self.ClientWidth-55;
    Top := 20;
    Width := 32;
    Height := 32;
    Center := True;
  end;
  PassEdit:= TEdit.Create(Self);
  with PassEdit do
  begin
    Parent:=Self;
    Left := 16;
    Top := 76;
    Width := 201;
    Height := 24;
    TabOrder := 0;
    OnChange := PASSChange;
    OnKeyPress := PASSKeyPress;
  end;
  Bitbtn1:=TBitBtn.Create(Self);
  with Bitbtn1 do
  begin
    Parent:=Self;
    Left := 15;
    Top := 104;
    Width := 75;
    Height := 25;
    Caption := ahmrcdlgok;
    Default := True;
    TabOrder := 1;
  end;
  Bitbtn2:=TBitBtn.Create(Self);
  with Bitbtn2 do
  begin
    Parent:=self;
    Left := 94;
    Top := 104;
    Width := 75;
    Height := 25;
    Caption := ahmrcdlgcancel;
    TabOrder := 2;
    Modalresult:=mrCancel;
  end;
  UserEdit:= TEdit.Create(Self);
  with UserEdit do
  begin
    Parent:=self;
    Left := 16;
    Top := 36;
    Width := 201;
    Height := 24;
    TabOrder := 3;
    OnChange := PASSChange;
    OnKeyPress := PASSKeyPress;
  end;
  ActiveControl := PassEdit;
end;


constructor TAHMPasswordDlg.Create(AOwner: TComponent);
begin
  FGlyphOk  := TBitmap.Create;
  FGlyphCancel := TBitmap.Create;
  FFont    := TFont.create;
  FNoOfRetries := 3;
  inherited Create(AOwner);
  FormWidth:=320;
  FormHeight:=200;
  Position:=poScreenCenter;
  FormCaption := ahmrcdlgenterpassword;
end;

destructor TAHMPasswordDlg.Destroy;
begin
  FGlyphOk.free;
  FGlyphCancel.free;
  FFont.free;
  inherited Destroy;
end;

Function TAHMPasswordDlg.Execute: Boolean;
begin
  inherited Execute;
  PassForm:=TPasswordForm.CreateNew(Application);
  with Passform do
  try
    SetFormProperties(PassForm);
    SetupForm;
    Font:=FFont;
    Image1.Picture.Icon := Application.Icon;
    OnShow:=DOFormShow;
    PassEdit.passwordchar:=FPasswordChar;
    FUserLabel.visible:=false;
    UserEdit.visible:=false;
    Bitbtn1.OnClick:=DoBitbtn1Click;
    Bitbtn2.OnClick:=DoBitbtn2Click;
    Bitbtn1.glyph:=FGlyphOk;
    Bitbtn2.glyph:=FGlyphCancel;
    HasPassword:=False;
    showmodal;
    result:=HasPassword;
  finally
    free;
  end;
end;

procedure TAHMPasswordDlg.SetGlyphOk(value : Tbitmap);
begin
  FGlyphOk.assign(value);
end;

procedure TAHMPasswordDlg.SetFont(value : TFont);
begin
  FFont.assign(value);
end;

procedure TAHMPasswordDlg.SetGlyphCancel(value : Tbitmap);
begin
  FGlyphCancel.assign(value);
end;

procedure TAHMPasswordDlg.Setpassw(value : string);
var i: integer;
    ss : string;
begin
  ss:='';
  for i:=1 to length(value) do ss:=concat(ss,chr(ord(value[i])+75));
  FPassword:=value;
  FScramble:=ss;
end;

procedure TAHMPasswordDlg.Setscramble(value : string);
var i: integer;
    ss : string;
begin
  ss:='';
  for i:=1 to length(value) do ss:=concat(ss,chr(ord(value[i])-75));
  FScramble:=value;
  FPassword:=ss;
end;

procedure TAHMPasswordDlg.DoBitBtn2Click(Sender: TObject);
begin
  if assigned(FOnCancel) then FOnCancel(self);
  PassForm.close;
end;

procedure TPasswordForm.PASSKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then if assigned(bitbtn1.Onclick) then bitbtn1.onclick(self);
end;

procedure TAHMPasswordDlg.DoBitBtn1Click(Sender: TObject);
begin
  if assigned(FOnOk) then FOnOk(self);
  if Passform.passEdit.text=FPassword then
  begin
    Passform.HIDE;
    if assigned(FOnRightPassword) then FOnRightPassword(self);
    Passform.HasPassword:=True;
    Passform.CLOSE;
  end
  else
  begin
    if assigned(FOnWrongPassword) then FOnWrongPassword(self);
    if Passform.xsret>=FNoOfRetries then
    begin
      Passform.Close;
      if assigned(FAfterRetries) then FAfterRetries(Self);
    end else
    begin
      if assigned(FOnRetry) then FOnRetry(Self,FNoOfRetries-Passform.XSret);
      Passform.passEdit.text:='';
      PassForm.PassEdit.Setfocus;
    end;
    Inc(Passform.xsret);
  end;
end;

procedure TPasswordForm.PASSChange(Sender: TObject);
begin
  if PassEdit.text<>'' then Bitbtn1.enabled:=true
  else bitbtn1.enabled:=false;
end;

procedure TAHMPasswordDlg.DoFormShow(Sender: TObject);
begin
  Passform.xsret:=1;
  Passform.PassEdit.text:='';
  Passform.bitbtn1.enabled:=false;
end;

procedure TPasswordForm.FormCreate(Sender: TObject);
begin
  Width:=BitBtn1.Left+BitBtn1.Width+20;
end;

end.
