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

interface
{$I AHM.INC}
{$R AHMTLookupMenu.res}

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Mask, AHMTLookupEdit,AHMStdClass,AHMStandardConst;

type
  TAHMLookupMenu = class(TAHMCustomLookupEdit)
  private
    FItems: TStrings;
    FItemIndex: Integer;
    FOnChange: TNotifyEvent;
    FNonSelected : String;
    FItemChecked: Boolean;
    procedure SetNonSelected(Value:String);
    procedure SetItems(Items: TStrings);
    procedure SetItemIndex(Value : Integer);
    procedure WMCommand(var Message: TMessage); message WM_COMMAND;
    procedure WMChar(var Message: TWMChar); message WM_CHAR;
  protected
    procedure OnLookupPress(Sender : TObject);
  public
    constructor Create(AOwner: TComponent); override;
    destructor destroy; override;
  published
    {$IFDEF AHMONLY4}
    property Anchors;
    property BiDiMode;
    property Constraints;
    property DragCursor;
    property DragKind;
    property DragMode;
    property ParentBiDiMode;
    {$ENDIF}
    property NonSelected : String Read FNonSelected Write SetNonSelected;
    property Items: TStrings read FItems write SetItems;
    property ItemChecked: Boolean read FItemChecked write FItemChecked;
    property OnChange: TNotifyEvent read FOnChange write FOnChange;
    property ItemIndex: Integer read FItemIndex write SetItemIndex;
    property EditButton1;
    Property Enabled;
    property Alignment;
    property EditMask;
    property MaxLength;
    property Text;
    property AutoSize;
    property CharCase;
    property PasswordChar;
    property ReadOnly;
    property Font;
    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;

implementation

constructor TAHMLookupMenu.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  EditButton1.Glyph.LoadFromResourceName(HInstance,'TAHMLOOKUPLISTA');
  EditButton1.NumGlyphs:=2;
  FNonSelected:=ahmrcstdnonselected;
  FItems := TStringList.Create;
  ItemIndex := -1;
  OnButton1Click:=OnLookupPress;
  EditButton2.Visible:=False;
end;

destructor TAHMLookupMenu.Destroy;
begin
  FItems.Free;
  Inherited destroy;
end;

procedure TAHMLookupMenu.WMChar(var Message: TWMChar);
begin
  if Message.CharCode = VK_SPACE Then PostMessage(Handle,WM_LBUTTONDOWN,0,0) else inherited;
end;

procedure TAHMLookupMenu.SetItems(Items: TStrings);
begin
  FItems.Assign(Items);
  SetItemIndex(FItemIndex);
end;

procedure TAHMLookupMenu.SetNonSelected(Value : String);
begin
  FNonSelected:=Value;
  SetItemIndex(FItemIndex);
end;

procedure TAHMLookupMenu.WMCommand(var Message: TMessage);
begin
  FItemIndex := Message.wParam;
  Self.text:=FItems[FitemIndex];
  if Assigned(FOnChange) Then FOnChange(Self);
end;

procedure TAHMLookupMenu.SetItemIndex(Value : Integer);
begin
  if (Value>-2) and (Value<FItems.Count-1) then
  begin
    FItemIndex:=Value;
    if Value=-1 then Self.Text:=FNonSelected else Self.text:=FItems[FitemIndex];
    If Assigned(FOnChange) then FOnChange(self);
  end;
end;

procedure TAHMLookupMenu.OnLookupPress(Sender : TObject);
var pc,pd: TPoint;
    hMyMenu: Hmenu;
    i: Integer;
    CCaption: array[0..255] of Char;
begin
  If Fitems.Count >=1 then
  begin
    hMyMenu := CreatePopupMenu;
    For i := 1 to FItems.Count Do
      if FItems[i-1] = '-' then
        appendmenu(HMyMenu,MF_MENUBREAK,i-1,nil)
      else
      begin
        StrPCopy(CCaption,FItems[i-1]);
        if (FItemChecked) and (ItemIndex = i-1) then
          AppendMenu(HMyMenu,MF_STRING or MF_CHECKED,i-1,CCaption)
        else
          AppendMenu(HMyMenu,MF_STRING,i-1,CCaption);
      end;
    pc.x := left;
    pc.y := top + Height;
    With (Owner as TForm) do pd := ClientToScreen(pc);
    TrackPopupMenu(HMyMenu,TPM_LEFTALIGN,pd.x,pd.y,0,Handle,nil);
    DestroyMenu(HMyMenu);
    PostMessage(Handle,WM_LBUTTONUP,0,0);
  end;
end;

end.
