unit ListBoxEx;

interface

uses
  Classes, SysUtils, LCLType,
  {$IFDEF DPI}Dpi.Graphics, Dpi.Controls, Dpi.StdCtrls, Dpi.Forms, Dpi.Common, System.UITypes{$ELSE}
  Graphics, Controls, StdCtrls, Forms{$ENDIF};

type

  { TListBoxEx }

  TListBoxEx = class(TListBox)
  protected
    procedure DrawItem(Control: TWinControl; Index: Integer; ARect: TRect;
      State: TOwnerDrawState); virtual;
  public
    constructor Create(TheOwner: TComponent); override;
  end;

procedure Register;


implementation

procedure Register;
begin
  RegisterComponents('C-evo', [TListBoxEx]);
end;

{ TListBoxEx }

procedure TListBoxEx.DrawItem(Control: TWinControl; Index: Integer; ARect: TRect;
  State: TOwnerDrawState);
var
  BackgroundColor: TColor;
  TextColor: TColor;
  NewRect: TRect;
const
  Orange = $5F77CB;
  Blue = $d77800;
begin
  {$IFDEF UNIX}
  // Clear blank area under the items
  if Index = 0 then begin
    Canvas.Brush.Color := Color;
    Canvas.Brush.Style := TBrushStyle.bsSolid;
    Canvas.FillRect(0, 0, Width, Height);
  end;
  {$ENDIF}

  if odSelected in State then begin
    BackgroundColor := Blue;
    TextColor := clWhite;
  end else begin
    BackgroundColor := clBlack;
    TextColor := Orange;
  end;

  NewRect := Rect(ARect.Left, ARect.Top, ARect.Right + 1, ARect.Bottom + 1);

  Canvas.Brush.Color := Color;
  Canvas.Brush.Style := TBrushStyle.bsSolid;
  Canvas.FillRect(NewRect);

  Canvas.Font.Assign(Font);
  Canvas.Font.Color := TextColor;

  NewRect := Rect(NewRect.Left + 2, NewRect.Top + 1, NewRect.Right - 2, NewRect.Bottom - 2);

  Canvas.Brush.Color := BackgroundColor;
  Canvas.Brush.Style := TBrushStyle.bsSolid;
  Canvas.FillRect(NewRect);

  Canvas.TextRect(NewRect, NewRect.Left + 2, NewRect.Top +
    (NewRect.Height - Canvas.TextHeight(Items[Index])) div 2,
    Items[Index], Canvas.TextStyle);
  if odSelected in State then begin
    Canvas.Pen.Color := Orange;
    Canvas.Pen.Style := TPenStyle.psDot;
    Canvas.Brush.Style := TBrushStyle.bsClear;
    Canvas.Rectangle(NewRect);
  end;
end;

constructor TListBoxEx.Create(TheOwner: TComponent);
begin
  inherited;
  OnDrawItem := DrawItem;
  Style := TListBoxStyle.lbOwnerDrawVariable;
end;

end.

