Handle your tabs
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.
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;
Comments (0)
Trackbacks (0)
Leave a comment
Trackback
You must be logged in to post a comment.