(************************************************************************
*    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 AHMTColorCombo;
interface
{$I AHM.INC}
uses
  SysUtils, Windows, Messages, Classes, Graphics, Controls,
  StdCtrls,AHMStdClass;

type
  TAHMOnSelectColor = procedure(Sender : TObject;Index : Integer;var Color : TColor;
                                    var ColorName : string) of object;
  TAHMColorCombo = class(TCustomCombobox)
  private
    FPictureWidth  : Integer;
    FSelectedColor : TColor;
    FAboutStr : TAHMAboutSControlStr;
    FOnSelectColor : TAHMOnSelectColor;
    FSystemColors: Boolean;
    function  GetSelectedColorName : string;
    function  GetPictureWidth : Integer;
    procedure SetPictureWidth(PictWidth : Integer);
    procedure SetsystemColors(const Value: Boolean);
    procedure DrawColors;
    function GetColorbyName: String;
    procedure SetColorByName(const Value: String);
    function GetSelectedColor: TColor;
    procedure SetSelectedColor(const Value: TColor);
  protected
    procedure Click; override;
    procedure CreateWnd; override;
    procedure GetColor(Index : Integer; var Color : TColor; var ColorName : string); virtual;
    procedure DrawItem(Index : Integer; Rect : TRect; State : TOwnerDrawState); override;
    property Style;
    property Items;
    property MaxLength;
    property Sorted;
    property Text;
    property OnMeasureItem;
  public
    constructor Create(AOwner : TComponent); override;
    property SelectedColorName : String read GetSelectedColorName;
  published
    property About : TAHMAboutSControlStr read FAboutStr write FAboutStr;
    property ColorByName : String read GetColorbyName write SetColorByName;
    property ColorSelected : TColor read GetSelectedColor write SetSelectedColor default clBlack;
    property ColorDisplayWidth : Integer read GetPictureWidth write SetPictureWidth;
    property OnSelectColor : TAHMOnSelectColor read FOnSelectColor write FOnSelectColor;
    property SystemColors : Boolean read FSystemColors write SetsystemColors Stored True;
    property Color;
    property Ctl3D;
    property ItemIndex;
    {$IFDEF AHMONLY4}
    property Anchors;
    property BiDiMode;
    property Constraints;
    property DragKind;
    property ParentBiDiMode;
    {$ENDIF}
    property DragMode;
    property DragCursor;
    property DropDownCount;
    property Enabled;
    property Font;
    property ItemHeight;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property TabOrder;
    property TabStop;
    property Visible;
    property OnChange;
    property OnClick;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnDrawItem;
    property OnDropDown;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
 end;
 TAHMColorEntry = record
    Value: TColor;
    Name: string;
  end;
const
  Colors: array[-1..40] of TAHMColorEntry = (
    (Value: clNone; Name: 'None'),
    (Value: clBlack; Name: 'Black'),
    (Value: clMaroon; Name: 'Maroon'),
    (Value: clGreen; Name: 'Green'),
    (Value: clOlive; Name: 'Olive'),
    (Value: clNavy; Name: 'Navy'),
    (Value: clPurple; Name: 'Purple'),
    (Value: clTeal; Name: 'Teal'),
    (Value: clGray; Name: 'Gray'),
    (Value: clSilver; Name: 'Silver'),
    (Value: clRed; Name: 'Red'),
    (Value: clLime; Name: 'Lime'),
    (Value: clYellow; Name: 'Yellow'),
    (Value: clBlue; Name: 'Blue'),
    (Value: clFuchsia; Name: 'Fuchsia'),
    (Value: clAqua; Name: 'Aqua'),
    (Value: clWhite; Name: 'White'),
    (Value: clScrollBar; Name: 'ScrollBar'),
    (Value: clBackground; Name: 'Background'),
    (Value: clActiveCaption; Name: 'ActiveCaption'),
    (Value: clInactiveCaption; Name: 'InactiveCaption'),
    (Value: clMenu; Name: 'Menu'),
    (Value: clWindow; Name: 'Window'),
    (Value: clWindowFrame; Name: 'WindowFrame'),
    (Value: clMenuText; Name: 'MenuText'),
    (Value: clWindowText; Name: 'WindowText'),
    (Value: clCaptionText; Name: 'CaptionText'),
    (Value: clActiveBorder; Name: 'ActiveBorder'),
    (Value: clInactiveBorder; Name: 'InactiveBorder'),
    (Value: clAppWorkSpace; Name: 'AppWorkSpace'),
    (Value: clHighlight; Name: 'Highlight'),
    (Value: clHighlightText; Name: 'HighlightText'),
    (Value: clBtnFace; Name: 'BtnFace'),
    (Value: clBtnShadow; Name: 'BtnShadow'),
    (Value: clGrayText; Name: 'GrayText'),
    (Value: clBtnText; Name: 'BtnText'),
    (Value: clInactiveCaptionText; Name: 'InactiveCaptionText'),
    (Value: clBtnHighlight; Name: 'BtnHighlight'),
    (Value: cl3DDkShadow; Name: '3DDkShadow'),
    (Value: cl3DLight; Name: '3DLight'),
    (Value: clInfoText; Name: 'InfoText'),
    (Value: clInfoBk; Name: 'InfoBk'));


implementation

procedure TAHMColorCombo.Click;
var ColorName : string;
begin
  if ItemIndex >= 0 then GetColor(ItemIndex, FSelectedColor, ColorName);
  inherited Click;
end;

constructor TAHMColorCombo.Create(AOwner : TComponent);
begin
  FSystemColors:=True;
  FPictureWidth:= 32;
  inherited Create(AOwner);
  if Owner is TWinControl then Parent := (Owner as TWinControl);
  Sorted:= False;
  Style:= csOwnerDrawFixed;
end;

procedure TAHMColorCombo.CreateWnd;
begin
  inherited CreateWnd;
  DrawColors;
end;

procedure TAHMColorCombo.GetColor(Index : Integer;var Color : TColor;var ColorName : string);
begin
  ColorName := Colors[index].name;
  Color:=Colors[index].value;
  if Assigned(FOnSelectColor) then FOnSelectColor(Self, Index, Color, ColorName);
end;

function TAHMColorCombo.GetSelectedColorName : string;
begin
  result:=ColorToString(Colors[ItemIndex].Value);
end;


function TAHMColorCombo.GetPictureWidth : Integer;
begin
  GetPictureWidth := FPictureWidth;
  if FPictureWidth < 0 then GetPictureWidth := 32;
end;


procedure TAHMColorCombo.SetPictureWidth(PictWidth : Integer);
begin
  if FPictureWidth <> PictWidth then
  begin
    FPictureWidth := PictWidth;
    Refresh;
  end;
end;


procedure TAHMColorCombo.DrawItem(Index : Integer; Rect : TRect; State : TOwnerDrawState);
var orgRect     : TRect;
    pictRect    : TRect;
    oldColor    : TColor;
    lColor       : TColor;
    lColorName   : string;
    FOnDrawItem : TDrawItemEvent;
begin
  OrgRect := Rect;
  GetColor(Index, lColor, lColorName);
  canvas.fillrect(orgrect);
  with Canvas do
  begin
    if ColorDisplayWidth > 0 then
    begin
      pictRect := Classes.Rect(Rect.left+2,Rect.Top+2,ColorDisplayWidth,Rect.Bottom-2);;
      oldColor := Brush.Color;
      pen.width:=1;
      Brush.Color := lColor;
      Brush.Style := bsSolid;
      Pen.Color:=clBlack;
      Rectangle(pictrect.left,pictrect.top,pictrect.right,pictrect.bottom);
      Brush.Color := oldColor;
      pen.color:=oldColor;
      PictRect:=Classes.Rect(Pictrect.Right+2,Rect.Top,Rect.Right,Rect.Bottom)
    end;
    DrawTextEx(Canvas.Handle,PChar(lColorname),-1,PictRect,DT_VCenter or DT_SingleLine,nil);
  end;
  FOnDrawItem := OnDrawItem;
  if Assigned(FOnDrawItem) then FOnDrawItem(Self, Index, orgRect, State);
end;

procedure TAHMColorCombo.DrawColors;
Var I : Integer;
begin
  Items.clear;
  if FSystemColors then
    for I:=0 to 40 do items.add(inttostr(I))
  else
    for I:=0 to 15 do items.add(inttostr(I));
  ItemIndex:=0;
end;

procedure TAHMColorCombo.SetSystemColors(const Value: Boolean);
begin
  if FSystemColors<>Value then
  begin
    FSystemColors := Value;
    DrawColors;
  end;
end;

function TAHMColorCombo.GetColorbyName: String;
begin
  if ItemIndex>-1 then result:=colors[ItemIndex].name else result:='';
end;

procedure TAHMColorCombo.SetColorByName(const Value: String);
var Num,I : integer;
begin
  if FSystemColors then Num:=40 else Num:=15;
  for i:=0 to Num do
  if Value=Colors[I].Name then Itemindex:=I;
end;

function TAHMColorCombo.GetSelectedColor: TColor;
begin
 if ItemIndex>-1 then result:=colors[itemindex].value else result:=clNone;
end;

procedure TAHMColorCombo.SetSelectedColor(const Value: TColor);
var Num,I : integer;
begin
  if FSystemColors then Num:=40 else Num:=15;
  for I:=0 to Num do
    if value=colors[i].value then itemindex:=i;
end;

end.
