
{*******************************************************}
{                                                       }
{       ScaleRichView                                   }
{       The given component represents TPanel for work  }
{       with ScaleRichView.                             }
{                                                       }
{       Copyright (c) Ilya Zelenskiy                    }
{       Ilya.Zelenskiy@gmail.com                        }
{       http://www.trichview.com                        }
{                                                       }
{*******************************************************}

unit SRVPanel;

interface
{$I RV_Defs.inc}
uses
  Windows, Messages, Forms, Graphics, Controls, Classes, SRVControl;

type
  TSRVPanel = class(TSRVCustomControl)
  private
    Buffer : TBitmap;
    FBorderColor: TColor;
    FBorderWidth: Integer;
    FCaptionOffset: Integer;
    FCornersOffset: Integer;
    FDoubleBuffered : Boolean;
    procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR;
    procedure SetBorderColor(NewBorderColor: TColor);
    procedure SetBorderWidth(NewBorderWidth: Integer);
    procedure SetCaptionOffset(Value: Integer);
    procedure SetCornersOffset(Value: Integer);
    procedure SetDoubleBuffered(Value: Boolean);
  protected
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Resize; override;
  published
    property SkinManager;
    property SkinSchemeIndex;
    property Anchors;
    property Align;
    property Alignment default taCenter;
    property BorderColor: TColor read FBorderColor write SetBorderColor default SRV_BORDER_COLOR;
    property BorderWidth: Integer read FBorderWidth write SetBorderWidth default SRV_BORDER_WIDTH;
    property Caption;
    property CaptionOffset: Integer read FCaptionOffset write SetCaptionOffset default SRV_CAPTION_OFFSET;
    property Color;
    property CornersOffset: Integer read FCornersOffset write SetCornersOffset default SRV_CORNERS_OFFSET;
    property DrawOnPrint default True;
    property Font;
    property Hint;
    property Height default 41;
    property Enabled;
    property DoubleBuffered: Boolean read FDoubleBuffered write SetDoubleBuffered default False;
    property PopupMenu;
    property ShowHint;
    property Visible;
    property OnClick;
    {$IFDEF RICHVIEWDEF5}
    property OnContextPopup;
    {$ENDIF}
    property OnDblClick;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnEnter;
    property OnExit;
    property Width default 185;
  end;

implementation

constructor TSRVPanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
    csSetCaption, csDoubleClicks, csReplicatable];
  Height := 41;
  Width := 185;
  inherited DoubleBuffered := False;
  FDoubleBuffered := False;
  FBorderWidth := SRV_BORDER_WIDTH;
  FBorderColor := SRV_BORDER_COLOR;
  FCornersOffset := SRV_CORNERS_OFFSET;
  FAlignment := taCenter;
  FCaptionOffset := SRV_CAPTION_OFFSET;
  Font.Color := SRV_CAPTION_COLOR;
end;

destructor TSRVPanel.Destroy;
begin
  If Assigned(Buffer) then
    Buffer.Free;
  inherited;
end;

procedure TSRVPanel.CMDialogChar(var Message: TCMDialogChar);
begin
with Message do
  if IsAccel(CharCode, Caption) then
    Result := 1
else
  inherited;
end;

procedure TSRVPanel.SetBorderColor(NewBorderColor: TColor);
begin
  FBorderColor := NewBorderColor;
  Invalidate;
end;

procedure TSRVPanel.SetBorderWidth(NewBorderWidth: Integer);
begin
  if NewBorderWidth > SRV_MAX_BORDER_WIDTH then
    begin
      FBorderWidth := SRV_MAX_BORDER_WIDTH;
      Invalidate;
      Exit;
    end;
  if NewBorderWidth < SRV_MIN_BORDER_WIDTH then
    begin
      FBorderWidth := SRV_MIN_BORDER_WIDTH;
      Invalidate;
      Exit;
    end;
  FBorderWidth := NewBorderWidth;
  Invalidate;
end;

procedure TSRVPanel.SetCaptionOffset(Value: Integer);
begin
  if Value > SRV_MAX_CAPTION_OFFSET then
    begin
      FCaptionOffset := SRV_MAX_CAPTION_OFFSET;
      Invalidate;
      Exit;
    end;
  if Value < SRV_MIN_CAPTION_OFFSET then
    begin
      FCaptionOffset := SRV_MIN_CAPTION_OFFSET;
      Invalidate;
      Exit;
    end;
  FCaptionOffset := Value;
  Invalidate;
end;

procedure TSRVPanel.SetCornersOffset(Value: Integer);
begin
  if Value > SRV_MAX_CORNERS_OFFSET then
    begin
      FCornersOffset := SRV_MAX_CORNERS_OFFSET;
      Invalidate;
      Exit;
    end;
  if Value < SRV_MIN_CORNERS_OFFSET then
    begin
      FCornersOffset := SRV_MIN_CORNERS_OFFSET;
      Invalidate;
      Exit;
    end;
  FCornersOffset := Value;
  Invalidate;
end;

procedure TSRVPanel.Paint;
var
     Rector: TRect;
     Flag: LongInt;
     DoSkin : Boolean;
     Canvas : TCanvas;
begin
  Rector := Rect(BorderWidth+CaptionOffset, BorderWidth+CaptionOffset,
    Width - BorderWidth - CaptionOffset, Height - BorderWidth - CaptionOffset);

  If not (csDesigning in ComponentState) and FDoubleBuffered then begin
    Canvas := Buffer.Canvas;
    Canvas.CopyRect(Canvas.ClipRect, Self.Canvas, Bounds(0, 0, Width, Height));
  end else Canvas := Self.Canvas;

  If (csDesigning in ComponentState) and (Color = clNone) then begin
    Canvas.Brush.Color := clWhite;
    Canvas.FillRect(ClientRect);
  end;

  DoSkin := DrawBoxBG(Canvas, ClientRect, FSkinManager, SkinSchemeIndex, True,
    MouseIn, False, Enabled, Focused, 1, Color);

  Canvas.Pen.Color := BorderColor;
  Canvas.Pen.Width := BorderWidth;
  Canvas.Brush.Color := Color;
  If Color = clNone then Canvas.Brush.Style := bsClear
  else Canvas.Brush.Style := bsSolid;
  If not DoSkin then
    Canvas.RoundRect(0, 0, Width, Height, -CornersOffset+BorderWidth-1, -CornersOffset+BorderWidth-1);
  If Caption <> '' then begin
    Canvas.Font := Font;
    Flag := Alignments[Alignment];
    Rector.Top := ((Rector.Bottom + Rector.Top) - Canvas.TextHeight('W')) div 2;
    Rector.Bottom := Rector.Top + Canvas.TextHeight('W');
    DrawText(Canvas.Handle, PChar(Caption), Length(Caption), Rector, Flag);
  end;

  DrawBoxBorder(Canvas, GetClientRect, FSkinManager, SkinSchemeIndex, True,
    MouseIn, False, Enabled, Focused, 1);

  If not (csDesigning in ComponentState) and FDoubleBuffered then
    Self.Canvas.Draw(0, 0, Buffer);
end;

procedure TSRVPanel.SetDoubleBuffered(Value: Boolean);
begin
  FDoubleBuffered := Value;
  If Value then begin
    Buffer := TBitmap.Create;
    Buffer.Width := Width;
    Buffer.Height := Height;
  end else If Assigned(Buffer) then begin
    Buffer.Destroy;
    Buffer := nil;
  end;

  Repaint;
end;

procedure TSRVPanel.Resize;
begin
  If Assigned(Buffer) then begin
    Buffer.Width := Width;
    Buffer.Height := Height;
  end;
  inherited;
end;

end.
