{ ------------------------------------------------------------- }
{ Purpose : Next Common 6                                       }
{ By      : Bojan Nikolic                                       }
{ For     : BergSoft                                            }
{ ------------------------------------------------------------- }
{ Copyright (C) BergSoft.net 2011. All Rights Reserved.         }
{ ------------------------------------------------------------- }
{ mm/dd/yy comment                                              }
{ -------- ---------------------------------------------------- }
{ 12/06/11 Initial code                                         }
{ 12/06/11 Added: TNxProgressView                               }
{ ------------------------------------------------------------- }
{ Todo :                                                        }
{ ------------------------------------------------------------- }
{$I 'NxSuite6.inc'}
unit NxViews6;

interface

uses
  { Standard }
  Classes, Types, Windows, Graphics, Controls, ImgList,
  {$IFDEF UI_TYPES}UITypes,{$ENDIF}
  { Next Common 6 }
  NxIntf6, NxTypes6, NxClasses6;

const
  sizProgressBorderSize = 1;

type
  { TNxView }

  TNxView = class(TInterfacedObject, INxView)
  private
    FCanvas: TCanvas;
    FClientRect: TRect;
  protected
    function ToObject: TObject;
  public
    function GetCanvas: TCanvas;
    function GetClientRect: TRect;
    procedure Paint; virtual;
    procedure SetCanvas(const Value: TCanvas);
    procedure SetClientRect(const Value: TRect);
    { Properties }
    property Canvas: TCanvas read GetCanvas write SetCanvas;
    property ClientRect: TRect read GetClientRect write SetClientRect;
  end;

  { TNxProgressView }

  TNxProgressView = class(TNxView, INxProgressBar)
  private
    { Property Fields }
    FBorderColor: TColor;
    FColor: TColor;
    FMax: Double;
    FMin: Double;
    FOuterMargin: Integer;
    FProgressColor: TColor;
    FProgressHeight: Integer;
    FProgressMargin: Integer;
    FProgressStyle: TNxProgressBarStyle;
    FRoundCorners: Boolean;
    FStyle: INxControlStyle;
    FTransparent: Boolean;
    FValue: Double;
    { Property Accessors }
    function GetAsFloat: Double;
    function GetAsInteger: Integer;
    function GetBorderColor: TColor;
    function GetMax: Double;
    function GetMin: Double;
    function GetOuterMargin: Integer;
    function GetProgressColor: TColor;
    function GetProgressHeight: Integer;
    function GetProgressMargin: Integer;
    function GetProgressStyle: TNxProgressBarStyle;
    function GetRoundCorners: Boolean;
    function GetTransparent: Boolean;
    procedure SetAsFloat(const Value: Double);
    procedure SetAsInteger(const Value: Integer);
    procedure SetBorderColor(const Value: TColor);
    procedure SetMax(const Value: Double);
    procedure SetMin(const Value: Double);
    procedure SetOuterMargin(const Value: Integer);
    procedure SetProgressColor(const Value: TColor);
    procedure SetProgressHeight(const Value: Integer);
    procedure SetProgressMargin(const Value: Integer);
    procedure SetProgressStyle(const Value: TNxProgressBarStyle);
    procedure SetRoundCorners(const Value: Boolean);
    procedure SetTransparent(const Value: Boolean);
  protected
    procedure DrawProgressBoxes(ProgressRect: TRect; BoxColor: TColor);
  public
    procedure Paint; override;
    { Properties }
    property AsFloat: Double read GetAsFloat write SetAsFloat;
    property AsInteger: Integer read GetAsInteger write SetAsInteger;
    property BorderColor: TColor read GetBorderColor write SetBorderColor;
    property Color: TColor read FColor write FColor;
    property Max: Double read GetMax write SetMax;
    property Min: Double read GetMin write SetMin;
    property OuterMargin: Integer read GetOuterMargin write SetOuterMargin;
    property ProgressColor: TColor read GetProgressColor write SetProgressColor;
    property ProgressHeight: Integer read GetProgressHeight write SetProgressHeight;
    property ProgressMargin: Integer read GetProgressMargin write SetProgressMargin;
    property ProgressStyle: TNxProgressBarStyle read GetProgressStyle write SetProgressStyle;
    property RoundCorners: Boolean read GetRoundCorners write SetRoundCorners;
    property Style: INxControlStyle read FStyle write FStyle;
    property Transparent: Boolean read GetTransparent write SetTransparent;
  end;

  { TNxButtonView }

  TNxButtonView = class(TNxView)
  private
    FImages: TImageList;
    FText: WideString;
    FImageIndex: TImageIndex;
  public
    procedure Paint; override;
    { Properties }
    property ImageIndex: TImageIndex read FImageIndex write FImageIndex;
    property Images: TImageList read FImages write FImages;
    property Text: WideString read FText write FText;
  end;

implementation

uses
  { Standard }
  SysUtils;

{ TNxView }

function TNxView.GetCanvas: TCanvas;
begin
  Result := FCanvas;
end;

function TNxView.GetClientRect: TRect;
begin
  Result := FClientRect;
end;

procedure TNxView.Paint;
begin

end;

procedure TNxView.SetCanvas(const Value: TCanvas);
begin
  FCanvas := Value;
end;

procedure TNxView.SetClientRect(const Value: TRect);
begin
  FClientRect := Value;
end;

function TNxView.ToObject: TObject;
begin
  Result := Self;
end;

{ TNxProgressView }

procedure TNxProgressView.DrawProgressBoxes(ProgressRect: TRect; BoxColor: TColor);
var
  Pos, S: Integer;
begin
  if ProgressRect.Bottom - ProgressRect.Top <= 0 then Exit;
  Pos := ProgressRect.Left;
  S := Round((ProgressRect.Bottom - ProgressRect.Top) / 1.5);
  while Pos + S < ProgressRect.Right do
  begin
    with Canvas do
    begin
      Brush.Color := BoxColor;
      FillRect(Rect(Pos, ProgressRect.Top, Pos + S, ProgressRect.Bottom));
    end;
    Inc(Pos, S + 2);
  end;
  if Pos < ProgressRect.Right then
    with Canvas do
    begin
      Brush.Color := BoxColor;
      FillRect(Rect(Pos, ProgressRect.Top, ProgressRect.Right, ProgressRect.Bottom));
    end;
end;

function TNxProgressView.GetAsFloat: Double;
begin
  Result := FValue;
end;

function TNxProgressView.GetAsInteger: Integer;
begin
  Result := Round(FValue);
end;

function TNxProgressView.GetBorderColor: TColor;
begin
  Result := FBorderColor;
end;

function TNxProgressView.GetMax: Double;
begin
  Result := FMax;
end;

function TNxProgressView.GetMin: Double;
begin
  Result := FMin;
end;

function TNxProgressView.GetOuterMargin: Integer;
begin
  Result := FOuterMargin;
end;

function TNxProgressView.GetProgressColor: TColor;
begin
  Result := FProgressColor;
end;

function TNxProgressView.GetProgressHeight: Integer;
begin
  Result := FProgressHeight;
end;

function TNxProgressView.GetProgressMargin: Integer;
begin
  Result := FProgressMargin;
end;

function TNxProgressView.GetProgressStyle: TNxProgressBarStyle;
begin
  Result := FProgressStyle;
end;

function TNxProgressView.GetRoundCorners: Boolean;
begin
  Result := FRoundCorners;
end;

function TNxProgressView.GetTransparent: Boolean;
begin
  Result := FTransparent;
end;

procedure TNxProgressView.Paint;
var
  BorderRect, BoxRect, ProgressRect: TRect;
  Distance: Integer;
begin
  if Max = Min then Exit;

  BorderRect := ClientRect;
  InflateRect(BorderRect, -OuterMargin, -OuterMargin);

  if not IsRectEmpty(BorderRect) then
  begin
    if ProgressHeight > 0 then
    begin
      BorderRect.Top := BorderRect.Top + (BorderRect.Bottom - BorderRect.Top) div 2 - ProgressHeight div 2;
      BorderRect.Bottom := BorderRect.Top + ProgressHeight;
    end;

    with Canvas do
    begin
      if Transparent then Brush.Style := bsClear;
      Brush.Color := Color;
      Pen.Color := BorderColor;

      case ProgressStyle of
        pbSolid, pbBoxes:
          if RoundCorners then RoundRect(BorderRect.Left, BorderRect.Top, BorderRect.Right, BorderRect.Bottom, 4, 4)
            else Rectangle(BorderRect);
        pbStyled: Style.PaintProgressBar(BorderRect);
      end;

      Brush.Style := bsSolid;

      BoxRect := BorderRect;

      { Border }
      InflateRect(BoxRect, -sizProgressBorderSize, -sizProgressBorderSize);

      InflateRect(BoxRect, -ProgressMargin, -ProgressMargin);

      Distance := Round((BoxRect.Right - BoxRect.Left) * (AsFloat - Min) / (Max - Min));

      with ProgressRect do
      begin
        Left := BoxRect.Left;
        Right := BoxRect.Left + Distance;
        if Right > BoxRect.Right then Right := BoxRect.Right;
        Top := BoxRect.Top;
        Bottom := BoxRect.Bottom;
      end;

      if not IsRectEmpty(ProgressRect) then
      begin
        Brush.Color := ProgressColor;
        case ProgressStyle of
          pbSolid: FillRect(ProgressRect);
          pbBoxes: DrawProgressBoxes(ProgressRect, ProgressColor);
          pbStyled:
          begin
            FStyle.PaintProgressChunk(ProgressRect);
          end;
        end;
      end;

    end; { Canvas }

  end; { IsRectEmpty }

end;

procedure TNxProgressView.SetAsFloat(const Value: Double);
begin
  FValue := Value;
end;

procedure TNxProgressView.SetAsInteger(const Value: Integer);
begin
  FValue := Value;
end;

procedure TNxProgressView.SetBorderColor(const Value: TColor);
begin
  FBorderColor := Value;
end;

procedure TNxProgressView.SetMax(const Value: Double);
begin
  FMax := Value;
end;

procedure TNxProgressView.SetMin(const Value: Double);
begin
  FMin := Value;
end;

procedure TNxProgressView.SetOuterMargin(const Value: Integer);
begin
  FOuterMargin := Value;
end;

procedure TNxProgressView.SetProgressColor(const Value: TColor);
begin
  FProgressColor := Value;
end;

procedure TNxProgressView.SetProgressHeight(const Value: Integer);
begin
  FProgressHeight := Value;
end;

procedure TNxProgressView.SetProgressMargin(const Value: Integer);
begin
  FProgressMargin := Value;
end;

procedure TNxProgressView.SetProgressStyle(const Value: TNxProgressBarStyle);
begin
  FProgressStyle := Value;
end;

procedure TNxProgressView.SetRoundCorners(const Value: Boolean);
begin
  FRoundCorners := Value;
end;

procedure TNxProgressView.SetTransparent(const Value: Boolean);
begin
  FTransparent := Value;
end;

{ TNxButtonView }

procedure TNxButtonView.Paint;
begin
  inherited;

end;

end.
