unit comptest;

{$mode objfpc}{$H+}

interface
uses
  Classes, SysUtils, controls, LazarusPackageIntf;

type
  TViewOption = (voTest1, voTest2, voTest3);
  TViewOptions = set of TViewOption;

  TMiscOption = (moTest1, moTest2, moTest3);
  TMiscOptions = set of TMiscOption;

const
  DefaultViewOptions = [voTest2];
  DefaultMiscOptions = [moTest1, moTest3];

type

  { THeaderOptions }

  THeaderOptions = class ( TPersistent )
  private
   fViewOptions : TViewOptions;
   fMiscOptions : TMiscOptions;
  public
   constructor Create;
  published
   property ViewOptions : TViewOptions read fViewOptions write fViewOptions;
   property MiscOptions : TMiscOptions read fMiscOptions write fMiscOptions;
  end;

  THeader = class ( TPersistent )
  private
   fOnHeaderChanged: TNotifyEvent;
   fOptions : THeaderOptions;
   fHeaderName : string;
   fOnSetHeaderName : TNotifyEvent;
   procedure SetHeaderName(const Value : string);
  public
   constructor Create;
   destructor Destroy; override;
  published
   property Options : THeaderOptions read fOptions;
   property HeaderName : string read fHeaderName write SetHeaderName;
   property OnHeaderChanged : TNotifyEvent read fOnHeaderChanged write fOnHeaderChanged;
  end;

  { TMyControl }

  { TMyCustomControl }

  TMyCustomControl = class ( TCustomControl )
  private
    fHeader : THeader;
    fHeaderRect : TRect;
    fDingens : string;
    fOnDingensChanged : TNotifyEvent;
    procedure SetDingens(const Value : string);
  protected
    procedure Paint; override;
    property  Header : THeader read fHeader;
    property  HeaderRect : TRect read fHeaderRect write fHeaderRect;
    property  Dingens : string read fDingens write SetDingens;
    property  OnDingensChanged : TNotifyEvent read fOnDingensChanged write fOnDingensChanged;
  public
    constructor Create(aOwner : TComponent); override;
    destructor  Destroy; override;
  end;

  TMyControl = class ( TMyCustomControl )
  published
     property Header;
     property Dingens;
     property OnDingensChanged;
  end;

  procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('TestDingens', [TMyControl]);
end;

{ TMyControl }

procedure TMyCustomControl.SetDingens(const Value: string);
begin
  if fDingens <> Value then
  begin
    fDingens := Value;
    if Assigned(fOnDingensChanged) then fOnDingensChanged(self);
  end;
end;

procedure TMyCustomControl.Paint;
var h : integer;
begin
  inherited Paint;
  with Canvas do
  begin
    RectAngle(fHeaderRect);
    h := ((ClientHeight - fHeaderRect.Bottom) shr 1) + fHeaderRect.Bottom;
    TextOut((ClientWidth shr 1) - (TextWidth(fDingens) shr 1),
            (h - (TextHeight(fDingens) shr 1)), fDingens);
  end;
end;

constructor TMyCustomControl.Create(aOwner: TComponent);
begin
  inherited Create(aOwner);
  fHeader := THeader.Create;
  with GetControlClassDefaultSize do
    SetInitialBounds(0, 0, CX, CY);
end;

destructor TMyCustomControl.Destroy;
begin
  fHeader.Free;
  fHeader := nil;
  inherited Destroy;
end;

{ THeader }

procedure THeader.SetHeaderName(const Value: string);
begin
  if fHeaderName <> Value then
  begin
    fHeaderName := Value;
    if Assigned(fOnHeaderChanged) then fOnHeaderChanged(self);
  end;
end;

constructor THeader.Create;
begin
  inherited Create;
  fOptions := THeaderOptions.Create;
end;

destructor THeader.Destroy;
begin
  fOptions.Free;
  fOptions := nil;
  inherited Destroy;
end;

{ THeaderOptions }

constructor THeaderOptions.Create;
begin
  fViewOptions := DefaultViewOptions;
  fMiscOptions := DefaultMiscOptions;
end;

end.

