Home > Delphi > Handle your tabs

Handle your tabs

September 3, 2013 Leave a comment Go to comments

Here is a small class that makes handling TAB pages (TTabSheets) easier. Especially if you make IDE applications (or applications that present information in separate tabs). It allows you to connect a tabsheet to an object – which may or may not be a sourcecode object. The addTabFile and addTabSource are just examples of how you can extend the basic functionality to keep track of source code editors and link them back to a project element, for instance.

Tabs galore, easy to handle

Tabs galore, easy to handle

Useful and handy.


  TTabSourceType = (stCustom,stSourceFile, stSource);

  TTabElement = Class(TObject)
  private
    FKind:      TTabSourceType;
    FSheet:     TTabSheet;
    FFilename:  String;
    FSource:    TAdvMemoSource;
    FRef:       TObject;
  public
    Property    Source:TAdvMemoSource read FSource write FSource;
    Property    Filename:String read FFilename write FFilename;
    Property    Sheet:TTabSheet read FSheet write FSheet;
    Property    Kind:TTabSourceType read FKind write FKind;
    Property    RefObj:TObject read FRef write FRef;
  End;

  TTabController = Class(TObject)
  private
    FPage:      TPageControl;
  public
    Property  PageControl:TPageControl read FPage;

    function  AddTab(const aType:TTabSourceType;
              const aRef:TObject;
              out aObj: TTabElement):Boolean;

    function  AddTabFile(aFilename:String;
              const aRef:TObject;
              out aObj:TTabElement):Boolean;

    function  AddTabSource(const aRef:TObject;
              out aObj:TTabElement):Boolean;

    function  getTabByRef(const aRef:TObject;
              out aTab:TTabElement):boolean;

    Constructor Create(aPageControl:TPageControl);virtual;
  End;

//############################################################################
// TTabController
//############################################################################

Constructor TTabController.Create(aPageControl:TPageControl);
Begin
  inherited Create;
  FPage:=aPageControl;
end;

function TTabController.AddTabFile(aFilename:String;
         const aRef:TObject;
         out aObj:TTabElement):Boolean;
var
  mObj:     TTabElement;
Begin
  result:=False;
  aObj:=NIL;
  if FileExists(aFilename,true) then
  Begin
    if AddTabSource(aRef,mObj) then
    Begin
      mObj.Kind:=stSourceFile;
      mObj.Sheet.Caption:=ExtractFileName(aFilename);
      mObj.Filename:=aFilename;
      mObj.Source.Lines.LoadFromFile(aFilename);
      aObj:=mObj;
      result:=true;
    end;
  end;
end;

function TTabController.AddTabSource(Const aRef:TObject;
         out aObj:TTabElement):Boolean;
var
  mObj:     TTabElement;
  mMemo:    TAdvMemo;
  mSource:  TAdvMemoSource;
  mStyler:  TAdvPascalMemoStyler;
Begin
  result:=False;
  aObj:=NIL;
  if AddTab(stSource,aRef,mObj) then
  Begin
    (* Create memo *)
    mMemo:=TAdvMemo.Create(mObj.Sheet);
    mMemo.Parent:=mObj.Sheet;
    mMemo.Align:=alClient;

    (* create styler control *)
    mStyler:=TAdvPascalMemoStyler.Create(mObj.Sheet);

    (* create source control *)
    mSource:=TAdvMemoSource.Create(mObj.Sheet);
    mSource.SyntaxStyler:=mStyler;

    (* Link source to memo *)
    mMemo.MemoSource:=mSource;

    (* glue together and success *)
    mObj.Source:=mSource;
    aObj:=mObj;
    result:=true;
  end;
end;

function TTabController.AddTab(const aType:TTabSourceType;
              const aRef:TObject;
              out aObj: TTabElement):Boolean;
var
  mTab: TTabSheet;
begin
  result:=False;
  aObj:=nil;

  try
    mTab:=TTabSheet.Create(FPage);
    mTab.PageControl:=FPage;
    mTab.Visible:=True;
    mTab.Show;
  except
    on exception do
    exit;
  end;

  aObj:=TTabElement.Create;
  aObj.Sheet:=mTab;
  aObj.Kind:=aType;
  aObj.RefObj:=aRef;
  mTab.Tag:=NativeInt(aObj);
  Result:=True;
end;

function TTabController.getTabByRef(const aRef:TObject;
              out aTab:TTabElement):boolean;
var
  x:      Integer;
  mPage:  TTabSheet;
  mObj:   TTabElement;
begin
  result:=False;
  aTab:=NIL;
  if (FPage<>NIL)
  and not (csDestroying in FPage.ComponentState) then
  Begin
    for x:=0 to FPage.PageCount-1 do
    Begin
      mPage:=FPage.Pages[x];
      if mPage.Tag<>0 then
      Begin
        mObj:=TTabElement(mPage.Tag);
        if mObj.RefObj=aRef then
        Begin
          result:=True;
          aTab:=mObj;
          Break;
        end;
      end;
    end;
  end;
end;

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: