{==============================================================================

  Copyright (C) 2000-2001 by Evgeny Kryukov
  All rights reserved

  All contents of this file and all other files included in this archive
  are Copyright (C) 2001 Evgeny Kryukov. Use and/or distribution of
  them requires acceptance of the License Agreement.

  See License.txt for licence information

===============================================================================}

unit EpIIPanels;

{$I EpIIVer.Inc}
{$T-,W-,X+,P+}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  KsUtils, KsBitmap, KsControls, KsPanels, KsButtons, EpIIStyles, EpIIVersion;

type

{ TEpPanel class }

  TEpPanel = class(TKsCustomPanel)
  private
    FBackground: TEpBackground;
    FBorder: TepBorder;
    FCaptionBackground: TEpBackground;
    FCaptionBorder: TepBorder;
    FButtonGlyphColor: TColor;
    procedure DoChange(Sender: TObject);

    function GetVersion: TEpVersion;
    procedure SetVersion(const Value: TEpVersion);
    procedure SetBackground(const Value: TEpBackground);
    procedure SetBorder(const Value: TepBorder);
    procedure SetCaptionBackground(const Value: TEpBackground);
    procedure SetCaptionBorder(const Value: TepBorder);
    procedure SetButtonGlyphColor(const Value: TColor);
  protected
    { Inherited }
    procedure DrawCaption; override;
    procedure DrawFace; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Loaded; override;
  published
    property Align;
    { It's possible to draw the background for control. Based on TEpBackground class.
      See Also:
        TEpBackground
    }
    property Background: TEpBackground read FBackground write SetBackground;
    { It's possible to draw the border for control. Based on TEpBorder class.
      See Also:
        TEpBorder
    }
    property Border: TepBorder read FBorder write SetBorder;
    { Specifies the button glyph color. }
    property ButtonGlyphColor: TColor read FButtonGlyphColor write SetButtonGlyphColor;
    property Blend;
    property BlendValue;
    { It's possible to draw the caption background for control. Based on TEpBackground class.
      See Also:
        TEpBackground
    }
    property CaptionBackground: TEpBackground read FCaptionBackground write SetCaptionBackground;
    { It's possible to draw the caption border for control. Based on TEpBorder class.
      See Also:
        TEpBorder
    }
    property CaptionBorder: TepBorder read FCaptionBorder write SetCaptionBorder;
    property Color;
    property Transparent;
    property Version: TEpVersion read GetVersion write SetVersion
      stored False;
  end;

implementation {===============================================================}

{ TEpPanel }

constructor TEpPanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FBackground := TEpBackground.Create;
  FBackground.OnChange := DoChange;
  FBorder := TEpBorder.Create;
  FBorder.OnChange := DoChange;

  FCaptionBackground := TEpBackground.Create;
  FCaptionBackground.OnChange := DoChange;
  FCaptionBorder := TEpBorder.Create;
  FCaptionBorder.OnChange := DoChange;
end;

destructor TEpPanel.Destroy;
begin
  FCaptionBackground.Free;
  FCaptionBorder.Free;
  FBackground.Free;
  FBorder.Free;
  inherited Destroy;
end;

procedure TEpPanel.Loaded;
begin
  inherited;
  Invalidate;
end;

procedure TEpPanel.DrawCaption;
var
  R: TRect;
  KindFlags, Flags: integer;
  BackColor: TKColor;
  B: TKsBitmap;
  i, j: integer;
  P: TKColorRec;
begin
  if not ShowCaption then Exit;
  if FCaptionBorder = nil then Exit;

  R := GetCaptionRect;
  { Draw Caption }
  FCaptionBackground.Draw(Buffer, R);
  FCaptionBorder.Draw(Buffer, R, 0);

  { Draw Text }
  InflateRect(R, -5, 0);
  Buffer.Font.Assign(Font);
  Buffer.DrawText(Caption, R, DT_LEFT or DT_SINGLELINE or DT_VCENTER);

  { Draw Button }
  if not ShowButton then Exit;

  R := GetButtonRect;

  { Calc glyph flags }

  if ButtonKind = pbkHide then
  begin
    KindFlags := DFC_CAPTION;
    Flags := DFCS_CAPTIONCLOSE
  end
  else
  begin
    KindFlags := DFC_SCROLL;
    if Rolled then
      Flags := DFCS_SCROLLDOWN
    else
      Flags := DFCS_SCROLLUP;
  end;

  { Draw Glyph }

  B := TKsBitmap.Create;
  try
    B.SetSize(RectWidth(R), RectHeight(R));

    if ButtonPressed then
      DrawFrameControl(B.DC, Rect(0, 0, B.Width, B.Height), KindFlags, Flags or DFCS_PUSHED)
    else
      DrawFrameControl(B.DC, Rect(0, 0, B.Width, B.Height), KindFlags, Flags);

    { Draw to Buffer }
    for i := 2 to B.Width - 3 do
      for j := 2 to B.Height - 3 do
      begin
        P := TKColorRec(B.Pixels[i, j]);
        if (P.R = 0) and (P.G = 0) and (P.B = 0) then
          Buffer.Pixels[R.Left + i, R.Top + j] := KColor(FButtonGlyphColor);
      end;
  finally
    B.Free;
  end;
end;

procedure TEpPanel.DrawFace;
var
  R: TRect;
begin
  if FBorder = nil then Exit;

  R := Rect(0, 0, FWidth, FHeight);

  { Fill Back  }
  FBackground.Draw(Buffer, R);

  { Draw Bevel }
  if ShowBevel then
  begin
    FBorder.Draw(Buffer, R, 0); 
  end;
end;

procedure TEpPanel.DoChange(Sender: TObject);
begin
  Invalidate;
end;

{ Properties }

function TEpPanel.GetVersion: TEpVersion;
begin
  Result := EpVersion;
end;

procedure TEpPanel.SetBackground(const Value: TEpBackground);
begin
  FBackground.Assign(Value);
end;

procedure TEpPanel.SetBorder(const Value: TepBorder);
begin
  FBorder.Assign(Value);
end;

procedure TEpPanel.SetVersion(const Value: TEpVersion);
begin
end;

procedure TEpPanel.SetCaptionBackground(const Value: TEpBackground);
begin
  FCaptionBackground.Assign(Value);
end;

procedure TEpPanel.SetCaptionBorder(const Value: TepBorder);
begin
  FCaptionBorder.Assign(Value);
end;

procedure TEpPanel.SetButtonGlyphColor(const Value: TColor);
begin
  if FButtonGlyphColor <> Value then
  begin
    FButtonGlyphColor := Value;
    Invalidate;
  end;
end;

end.
