From e756eaa248c07ea390980d445471c88bdc42990c Mon Sep 17 00:00:00 2001 From: graemeg Date: Sun, 21 Oct 2007 21:16:24 +0000 Subject: * Added name and version constants. * Added a SetDefaults method which we could use in the future to autoset the default property values for us. * Minor documentation update --- src/corelib/fpgfx.pas | 4 ++++ src/corelib/gfxbase.pas | 45 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) (limited to 'src/corelib') diff --git a/src/corelib/fpgfx.pas b/src/corelib/fpgfx.pas index be4254d9..c2c64cfe 100644 --- a/src/corelib/fpgfx.pas +++ b/src/corelib/fpgfx.pas @@ -37,6 +37,10 @@ const // Used for the internal message queue cMessageQueueSize = 512; + + // version and name constants + fpGUIVersion = '0.5.1'; + fpGUIName = 'fpGUI Library'; type diff --git a/src/corelib/gfxbase.pas b/src/corelib/gfxbase.pas index 9ae7f503..173b8017 100644 --- a/src/corelib/gfxbase.pas +++ b/src/corelib/gfxbase.pas @@ -342,6 +342,7 @@ type public // The standard constructor. constructor Create(AOwner: TComponent); override; + procedure AfterConstruction; override; // Make some setup before the window shows. Forms modify the window creation parameters. procedure AdjustWindowStyle; virtual; // Make some setup before the window shows. Invoked after the window is created. @@ -408,7 +409,13 @@ procedure SortRect(var left, top, right, bottom: integer); implementation uses - fpgfx; // needed for fpgApplication + fpgfx, // needed for fpgApplication + typinfo; + + +const + NoDefault = $80000000; + tkPropsWithDefault = [tkInteger, tkChar, tkSet, tkEnumeration]; function KeycodeToText(AKey: Word; AShiftState: TShiftState): string; @@ -660,6 +667,32 @@ begin SortRect(left, top, right, bottom); end; +// This function uses RTTI to automatically set the default values of properties. +// That means we don't have to do it in the constructor anymore! :-) +procedure SetDefaults(Obj: TObject); +var + PropInfos: PPropList; + Count, Loop: Integer; +begin + PropInfos := nil; + { Find out how many properties we'll be considering } + Count := GetPropList(Obj.ClassInfo, tkPropsWithDefault, nil); + { Allocate memory to hold their RTTI data } + GetMem(PropInfos, Count * SizeOf(PPropInfo)); + try + { Get hold of the property list in our new buffer } + GetPropList(Obj.ClassInfo, tkPropsWithDefault, PropInfos); + { Loop through all the selected properties } + for Loop := 0 to Count - 1 do + with PropInfos^[Loop]^ do + { If there is supposed to be a default value... } + if Default <> NoDefault then + { ...then jolly well set it } + SetOrdProp(Obj, PropInfos^[Loop], Default) + finally + FreeMem(PropInfos, Count * SizeOf(PPropInfo)); + end; +end; { TfpgRect } @@ -755,6 +788,16 @@ begin FMouseCursor := mcDefault; end; +procedure TfpgWindowBase.AfterConstruction; +begin + inherited AfterConstruction; + { Here is a neater way by using RTTI to set default property values all + automatically. No need to duplicate the efforts and manually set the + property default values in the constructor. This code is no the same for + each TfpgWindowBase descendant (which includes GUI widgets) } +// SetDefaults(self); +end; + procedure TfpgWindowBase.AdjustWindowStyle; begin // does nothing here -- cgit v1.2.3-70-g09d2