(************************************************************************
*    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 AHMTLookupEdit;

interface
{$I AHM.INC}
uses
  Windows, Messages, SysUtils, Controls,Classes,
  Mask,Menus,AHMStdClass,Graphics,Buttons;

{$R AHMTLookupEdit.res}
type
  TAHMButtonClass = Class;
  TAHMCustomLookupEdit = class(TCustomMaskEdit)
  private
    FEditButton2,FEditButton1: TSpeedButton;
    FEditBClass2,FEditBClass1: TAHMButtonClass;
    FAboutStr : TAHMAboutSControlStr;
    FOnButton2Click,FOnButton1Click: TNotifyEvent;
    FAlignment: TAlignment;
    function GetMinHeight: Integer;
    procedure EditButton2Click(Sender: TObject);
    procedure EditButton1Click(Sender: TObject);
    procedure WMSize(var Message: TWMSize); message WM_SIZE;
    procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
    procedure CMEnter(var Message: TMessage); message CM_ENTER;
    procedure SetAlignment(Value: TAlignment);
    Procedure SetEditBClass2(value : TAHMButtonClass);
    Procedure SetEditBClass1(value : TAHMButtonClass);
  protected
    procedure CreateWnd; override;
    procedure SetButtons;
  public
    procedure SetEditRect;
    procedure CreateParams(var Params: TCreateParams); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Loaded; override;
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;
    procedure KeyPress(var Key: Char); override;
    property EditButton2 : TAHMButtonClass read FEditBClass2 write SetEditBClass2 stored true;
    property EditButton1 : TAHMButtonClass read FEditBClass1 write SetEditBClass1 stored true;
    property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
    property OnButton2Click: TNotifyEvent read FOnButton2Click write FOnButton2Click;
    property OnButton1Click: TNotifyEvent read FOnButton1Click write FOnButton1Click;
  published
    property About : TAHMAboutSControlStr read faboutStr write faboutStr;
  end;

  TAHMLookupEdit = class(TAHMCustomLookupEdit)
  private
  protected
  public
    constructor Create(AOwner: TComponent); override;
  published
    property Alignment;
    {$IFDEF AHMONLY4}
    property Anchors;
    property BiDiMode;
    property Constraints;
    property DragCursor;
    property DragKind;
    property DragMode;
    property ParentBiDiMode;
    {$ENDIF}
    property EditButton1;
    property OnButton1Click;
    property EditButton2;
    property OnButton2Click;
    property EditMask;
    property MaxLength;
    property Text;
    property AutoSize;
    property CharCase;
    property PasswordChar;
    property ReadOnly;
    property Font;
    property Enabled;
    property OnChange;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnClick;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnEndDrag;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnStartDrag;
  end;
  TAHMButtonClass = Class(TPersistent)
  private
    FButton : TSpeedButton;
    FShortCutKey : TShortCut;
    { Procedures }
    Procedure SetCaption(value : String);
    Procedure SetVisible(value : Boolean);
    Procedure SetEnabled(value : Boolean);
    Procedure SetHint(value : String);
    Procedure SetShowHint(value : Boolean);
    Procedure SetNumGlyphs(value : Integer);
    Procedure SetGlyph(value : TBitmap);
    Procedure SetShortCutKey(value : TShortCut);
    { Functions }
    Function GetCaption : String;
    Function GetVisible : Boolean;
    Function GetEnabled : Boolean;
    Function GetHint : String;
    Function GetShowHint : Boolean;
    Function GetNumGlyphs : Integer;
    Function GetGlyph : TBitmap;
  protected
  public
    Constructor create(AOwner : TComponent);
  published
    { Published Properties }
    Property Caption : String read GetCaption write SetCaption stored True;
    Property Visible : Boolean read GetVisible write SetVisible stored True;
    Property Enabled : Boolean read GetEnabled write SetEnabled stored True;
    Property Hint : String read GetHint write SetHint stored True;
    Property ShowHint : Boolean read GetShowHint write SetShowHint stored True;
    Property NumGlyphs : Integer read GetNumGlyphs write SetNumGlyphs stored True;
    Property Glyph : TBitmap read GetGlyph write SetGlyph stored True;
    Property ShortCutKey: TShortCut read FShortCutKey write SetShortCutKey stored True;
  end;


implementation
uses Forms;

{ Class TAHMCustomLookupEdit of the AHM Add-Ons for Delphi, C++Builder written 1998 by AHM}

constructor TAHMCustomLookupEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FEditButton2 := TSpeedButton.Create(self);
  FEditButton1 := TSpeedButton.Create(self);
  FEditBClass2 := TAHMButtonClass.Create(FEditButton2);
  FEditBClass1 := TAHMButtonClass.Create(FEditButton1);
  ControlStyle := ControlStyle + [csReplicatable];
  AutoSize := False;
  FEditButton2.Parent:=self;
  FEditButton1.Parent:=self;
  FEditButton2.Cursor:=crArrow;
  FEditButton1.Cursor:=crArrow;
  FEditButton2.OnClick := EditButton2Click;
  FEditButton1.OnClick := EditButton1Click;
  SetButtons;
end;

procedure TAHMCustomLookupEdit.Loaded;
begin
  inherited Loaded;
end;

destructor TAHMCustomLookupEdit.Destroy;
begin
  FEditButton1.OnClick := nil;
  FEditButton2.OnClick := nil;
  FEditBClass1.Free;
  FEditBClass2.Free;
  FEditButton1.free;
  FEditButton2.free;
  inherited Destroy;
end;


procedure TAHMCustomLookupEdit.CreateParams(var Params: TCreateParams);
const
  Alignments: array[TAlignment] of DWord = (ES_LEFT, ES_RIGHT, ES_CENTER);
begin
  inherited CreateParams(Params);
  with Params do
    Style := Style or ES_MULTILINE or WS_CLIPCHILDREN or Alignments[FAlignment];
end;

procedure TAHMCustomLookupEdit.CreateWnd;
begin
  inherited CreateWnd;
  SetEditRect;
end;

procedure TAHMCustomLookupEdit.KeyDown(var Key: Word; Shift: TShiftState);
begin
  inherited KeyDown(Key, Shift);
  if FEditBClass2.ShortCutKey<>TextToShortCut('None') then
  if FEditBClass2.ShortCutKey = ShortCut(Key, Shift) then
  begin
    EditButton2Click(Self);
    Key := 0;
  end;
  if FEditBClass1.ShortCutKey<>TextToShortCut('None') then
  if FEditBClass1.ShortCutKey = ShortCut(Key, Shift) then
  begin
    EditButton1Click(Self);
    Key := 0;
  end;
end;

procedure TAHMCustomLookupEdit.KeyPress(var Key: Char);
begin
  inherited KeyPress(Key);
  if (Key = Char(VK_RETURN)) or (Key = Char(VK_ESCAPE)) then
  begin
    GetParentForm(Self).Perform(CM_DIALOGKEY, Byte(Key), 0);
    if Key = Char(VK_RETURN) then Key := #0;
  end;
end;

procedure TAHMCustomLookupEdit.SetEditBClass2(value: TAHMButtonClass);
begin
  FEditBClass2 := Value;
  ReCreatewnd;
end;

procedure TAHMCustomLookupEdit.SetEditBClass1(value: TAHMButtonClass);
begin
  FEditBClass1 := Value;
  ReCreatewnd;
end;

procedure TAHMCustomLookupEdit.SetEditRect;
var Loc: TRect;
    VLength : Integer;
begin
  VLength:=0;
  if FEditBClass2.Visible then inc(VLength,FEditButton2.width);
  if FEditBClass1.Visible then inc(VLength,FEditButton1.width);
  SetRect(Loc,0,0,ClientWidth-VLength,ClientHeight + 1);
  SendMessage(Handle, EM_SETRECTNP, 0, LongInt(@Loc));
end;

procedure TAHMCustomLookupEdit.SetButtons;
var myrect : TRect;
    Subtr : Integer;
begin
  SubTr:=0;
  myrect:=rect(Width-Height, 0, Height-4, Height - 4);
  if FEditButton2.visible then
    FEditButton2.SetBounds(myrect.left,myrect.top,myrect.right,
                           myrect.bottom)
  else FEditButton2.SetBounds(Width+1, 0,0,0);
  if FEditButton2.visible then Subtr:=FEditButton2.width;
  if FEditButton1.visible then
    FEditButton1.SetBounds(myrect.left-subtr,myrect.top,myrect.right,
                           myrect.bottom)
  else FEditButton1.SetBounds(Width+1, 0,0,0);
end;

procedure TAHMCustomLookupEdit.WMSize(var Message: TWMSize);
var MinHeight: Integer;
begin
  inherited;
  if not (csLoading in ComponentState) then
  begin
    MinHeight := GetMinHeight;
    if Height < MinHeight then
    begin
      Height := MinHeight;
      Exit;
    end;
  end;
  SetEditRect;
  SetButtons;
end;

function TAHMCustomLookupEdit.GetMinHeight: Integer;
var
  DC: HDC;
  SaveFont: HFont;
  I: Integer;
  SysMetrics, Metrics: TTextMetric;
begin
  DC := GetDC(0);
  try
    GetTextMetrics(DC, SysMetrics);
    SaveFont := SelectObject(DC, Font.Handle);
    GetTextMetrics(DC, Metrics);
    SelectObject(DC, SaveFont);
  finally
    ReleaseDC(0, DC);
  end;
  I := SysMetrics.tmHeight;
  if I > Metrics.tmHeight then I := Metrics.tmHeight;
  Result := Metrics.tmHeight + (I div 4) + 1 + GetSystemMetrics(SM_CYBORDER) * 4;
end;

procedure TAHMCustomLookupEdit.CMEnabledChanged(var Message: TMessage);
begin
  inherited;
  FEditButton2.Enabled := Enabled;
  FEditButton1.Enabled := Enabled;
end;

procedure TAHMCustomLookupEdit.CMEnter(var Message: TMessage);
begin
  if AutoSelect and not (csLButtonDown in ControlState) then SelectAll;
  inherited;
end;

procedure TAHMCustomLookupEdit.EditButton2Click(Sender: TObject);
begin
  if Assigned(FOnButton2Click) then FOnButton2Click(Self);
  Windows.SetFocus(Handle);
end;

procedure TAHMCustomLookupEdit.EditButton1Click(Sender: TObject);
begin
  if Assigned(FOnButton1Click) then FOnButton1Click(Self);
  Windows.SetFocus(Handle);
end;

procedure TAHMCustomLookupEdit.SetAlignment(Value: TAlignment);
begin
  if FAlignment <> Value then
  begin
    FAlignment := Value;
    RecreateWnd;
  end;
end;

{ Class TAHMButtonClass of the AHM Add-Ons for Delphi, C++Builder written 1998 by AHM}

Constructor TAHMButtonClass.Create(AOwner : TComponent);
begin
  inherited Create;
  FButton:=AOwner as TSpeedButton;
end;

Procedure TAHMButtonClass.SetCaption(value : String);
begin
  FButton.Caption:=Value;
end;

Procedure TAHMButtonClass.SetVisible(value : Boolean);
begin
  FButton.Visible:=Value;
  if (FButton.Parent is TAHMLookupEdit) then (FButton.Parent as TAHMLookupEdit).Recreatewnd;
end;

Procedure TAHMButtonClass.SetEnabled(value : Boolean);
begin
  FButton.Enabled:=Value;
end;

Procedure TAHMButtonClass.SetHint(value : String);
begin
  FButton.Hint:=Value;
end;

Procedure TAHMButtonClass.SetShowHint(value : Boolean);
begin
  FButton.ShowHint:=Value;
end;

Procedure TAHMButtonClass.SetNumGlyphs(value : Integer);
begin
  FButton.NumGlyphs:=Value;
end;

Procedure TAHMButtonClass.SetGlyph(value : TBitmap);
begin
  FButton.Glyph:=Value;
end;

Procedure TAHMButtonClass.SetShortCutKey(value : TShortCut);
begin
  FShortCutKey:=Value;
end;

Function TAHMButtonClass.GetCaption : String;
begin
  Result:=FButton.Caption;
end;

Function TAHMButtonClass.GetVisible : Boolean;
begin
  Result:=FButton.Visible;
end;

Function TAHMButtonClass.GetEnabled : Boolean;
begin
  Result:=FButton.Enabled;
end;

Function TAHMButtonClass.GetHint : String;
begin
  Result:=FButton.Hint;
end;

Function TAHMButtonClass.GetShowHint : Boolean;
begin
  Result:=FButton.ShowHint;
end;

Function TAHMButtonClass.GetNumGlyphs : Integer;
begin
  Result:=FButton.NumGlyphs;
end;

Function TAHMButtonClass.GetGlyph : TBitmap;
begin
  Result:=FButton.Glyph;
end;

{ Class TAHMLookupEdit of the AHM Add-Ons for Delphi, C++Builder written 1998 by AHM}

constructor TAHMLookupEdit.Create(AOwner: TComponent);
begin
  inherited create(AOwner);
  EditButton1.Glyph.LoadFromResourceName(HInstance,'AHMLOOKUPEDITB1');
  EditButton1.NumGlyphs:=2;
  EditButton2.Glyph.LoadFromResourceName(HInstance,'AHMLOOKUPEDITB2');
  EditButton2.NumGlyphs:=2;
end;



end.
