{------------------------------------------------------------------------------}
{
   Unit Name: TabButtons
   Purpose  : Panel for TkButtons to comminicate with PageControl
   Author   : Vesa Lappalainen
   Date     : 13.10.2004
   Changed  :

   ToDo     :
}
{------------------------------------------------------------------------------}

unit TabButtons;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, ExtCtrls,ComCtrls,kExtraCtl,KButtons,Buttons;

type
  TTabButtons = class(TPanel)
  private
    FButtons : TList;
    FPageControl: TPageControl;
    FAllowAllUp: Boolean;
    FSpacing: Integer;
    FMargin: Integer;
    FBitFileName: string;
    FLayout: TButtonLayout;
    FNumGlyphs: TNumGlyphs;
    FButtonWidth: integer;
    FButtonHeight: integer;
    FButtonAlign: TAlign;
    FShowTabs: boolean;
    procedure SetPageControl(const Value: TPageControl);
    procedure CreateButtons;
    procedure DeleteButtons;
    procedure SetAllowAllUp(const Value: Boolean);
    procedure SetBitFileName(const Value: string);
    procedure SetLayout(const Value: TButtonLayout);
    procedure SetMargin(const Value: Integer);
    procedure SetNumGlyphs(const Value: TNumGlyphs);
    procedure SetSpacing(const Value: Integer);
    procedure SetButtonProperties;
    procedure SetButtonVisible;
    procedure ButtonClick(Sender: TObject);
    procedure SetButtonAlign(const Value: TAlign);
    procedure SetButtonHeight(const Value: integer);
    procedure SetButtonWidth(const Value: integer);
    procedure SetShowTabs(const Value: boolean);
  protected
  public
    constructor Create(AOwner:TComponent); override;
    destructor Destroy; override;
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  published
    property PageControl : TPageControl read FPageControl write SetPageControl;
    property AllowAllUp: Boolean read FAllowAllUp write SetAllowAllUp default False;
    property Layout: TButtonLayout read FLayout write SetLayout default blGlyphLeft;
    property Margin: Integer read FMargin write SetMargin default -1;
    property NumGlyphs: TNumGlyphs read FNumGlyphs write SetNumGlyphs default 4;
    property Spacing: Integer read FSpacing write SetSpacing default 4;
    property BitFileName : string read FBitFileName write SetBitFileName;
    property ButtonWidth : integer read FButtonWidth write SetButtonWidth  default 40;
    property ButtonHeight : integer read FButtonHeight write SetButtonHeight   default 40;
    property ButtonAlign : TAlign read FButtonAlign write SetButtonAlign  default alTop;
    property ShowTabs : boolean read FShowTabs write SetShowTabs  default false ;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Kave2000', [TTabButtons]);
end;

{ TTabButtons }

procedure TTabButtons.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited; // Notification(AComponent, Operation);
  if (Operation = opRemove) and (AComponent = PageControl) then begin
    PageControl := nil;
    DeleteButtons;
  end;
end;

procedure TTabButtons.SetButtonVisible;
var i:integer; b : TkProgButton; s:string;
begin
  if ( FButtons = nil ) then exit;
  if ( PageControl = nil ) then exit;
  Visible := not ShowTabs;
  for i:=0 to FButtons.Count-1 do begin
    b := FButtons[i];
    if ( ShowTabs ) then begin
      PageControl.Pages[i].TabVisible := (PageControl.Pages[i].Tag >= 0);
    end else begin
      b.Visible     := (PageControl.Pages[i].Tag >= 0);
      PageControl.Pages[i].TabVisible := false;
    end;
  end;

end;


procedure TTabButtons.SetButtonProperties;
var i:integer; b : TkProgButton; s:string;
begin
  if ( FButtons = nil ) then exit;
  if ( PageControl = nil ) then exit;
  for i:=0 to FButtons.Count-1 do begin
    b := FButtons[i];
    b.AllowAllUp  := AllowAllUp;
    b.Spacing     := Spacing;
    b.Margin      := Margin;
    b.BitFileName := BitFileName;
    b.Layout      := Layout;
    b.NumGlyphs   := NumGlyphs;
    b.Font        := Font;
    b.Align       := ButtonAlign;
    b.Height      := ButtonHeight;
    b.Width       := ButtonWidth;
    b.Top         := 1000;
    b.Left        := 1000;
    b.Caption     := PageControl.Pages[i].Caption;
    s := b.Caption;
  end;
  SetButtonVisible;
end;



procedure TTabButtons.DeleteButtons;
var i:integer; b : TkProgButton;
begin
  if ( FButtons = nil ) then exit;
  for i:=0 to FButtons.Count-1 do begin
    b := FButtons[i];
    b.free;
  end;
  FButtons.Clear;
end;

procedure TTabButtons.CreateButtons;
var i:integer; b : TkProgButton;
begin
  DeleteButtons;
  if ( PageControl = nil ) then exit;
  for i:=0 to PageControl.PageCount-1 do begin
    b := TkProgButton.Create(self);
    b.IniName := '-';
    b.Parent := self;
    b.Tag := i;
    b.OnClick := ButtonClick;
    b.GroupIndex := 1;
    b.AllowAllUp := false;
    FButtons.Add(b);
  end;
  SetButtonProperties;
end;

procedure TTabButtons.SetPageControl(const Value: TPageControl);
begin
  FPageControl := Value;
  if Value <> nil then Value.FreeNotification(Self);
  CreateButtons;
end;

constructor TTabButtons.Create(AOwner: TComponent);
begin
  inherited;
  FSpacing := 4;
  FMargin := -1;
  FButtonWidth := 40;
  FButtonHeight := 40;
  FButtonAlign := alTop;
  FLayout := blGlyphLeft;
  FNumGlyphs := 4;
  FButtons := TList.Create();
end;

destructor TTabButtons.Destroy;
begin
  FButtons.Free;
  inherited;
end;


procedure TTabButtons.SetAllowAllUp(const Value: Boolean);
begin
  if ( FAllowAllUp = Value ) then exit;
  FAllowAllUp := Value;
  SetButtonProperties;
end;

procedure TTabButtons.SetBitFileName(const Value: string);
begin
  if ( FBitFileName = Value ) then exit;
  FBitFileName := Value;
  SetButtonProperties;
end;

procedure TTabButtons.SetLayout(const Value: TButtonLayout);
begin
  if ( FLayout = Value ) then exit;
  FLayout := Value;
  SetButtonProperties;
end;

procedure TTabButtons.SetMargin(const Value: Integer);
begin
  if ( FMargin = Value ) then exit;
  FMargin := Value;
  SetButtonProperties;
end;

procedure TTabButtons.SetNumGlyphs(const Value: TNumGlyphs);
begin
  if ( FNumGlyphs = Value ) then exit;
  FNumGlyphs := Value;
  SetButtonProperties;
end;

procedure TTabButtons.SetSpacing(const Value: Integer);
begin
  if ( FSpacing = Value ) then exit;
  FSpacing := Value;
  SetButtonProperties;
end;

procedure TTabButtons.ButtonClick(Sender: TObject);
begin
  if ( PageControl = nil ) then exit;
  PageControl.ActivePageIndex := TkProgButton(Sender).Tag;
end;


procedure TTabButtons.SetButtonAlign(const Value: TAlign);
begin
  if ( FButtonAlign = value ) then exit;
  FButtonAlign := Value;
  SetButtonProperties;
end;

procedure TTabButtons.SetButtonHeight(const Value: integer);
begin
  if ( FButtonHeight = value ) then exit;
  FButtonHeight := Value;
  SetButtonProperties;
end;

procedure TTabButtons.SetButtonWidth(const Value: integer);
begin
  if ( FButtonWidth = value ) then exit;
  FButtonWidth := Value;
  SetButtonProperties;
end;

procedure TTabButtons.SetShowTabs(const Value: boolean);
begin
  if ( FShowTabs = value ) then exit;
  FShowTabs := Value;
  SetButtonVisible;
end;


end.
