{------------------------------------------------------------------------------}
{
   Unit Name: kpropfunc
   Purpose  : Functions to handle properties
   Author   : Vesa Lappalainen
   Date     : 06.09.00
   Changed  :

   ToDo     :
}
{------------------------------------------------------------------------------}

unit kPropFunc;
(*

   Functions:
   ==========

     function SetBetween(i,min,max:integer) : integer;
       - palauttaa i:n muutettuna välille [min,max]


     function GetStringProperty(comp:TObject; const propname:string ;
                                const def:string) : string;
      - selvittää valitun komponetin (comp) tietyn ominaisuuden (propname)
        arvon merkkijonona jos sellainen on olemassa.  Muuten palauttaa def

     function SetStringProperty(comp:TObject; const propname:string ;
                                const val:string) : boolean;
      - yrittää asettaa tietyn komponentin merkkijono-ominaisuuden arvon.
        Palauttaa miten onnistui.


*)

{------------------------------------------------------------------------------}

interface
uses classes,
{$ifdef CLX}
  QControls,QStdctrls;
{$else}
  Controls,Stdctrls;
{$endif}

{------------------------------------------------------------------------------}

{------------------------------------------------------------------------------}
  function SetBetween(i,min,max:integer):integer;
  function GetStringProperty(comp:TObject; const propname:string ;
                             const def:string) : string;
  function GetBooleanProperty(comp:TObject; const propname:string ;
                              def:boolean) : boolean;
  function SetStringProperty(comp:TObject; const propname:string ;
                             const val:string) : boolean;
  function SetBooleanProperty(comp:TObject; const propname:string ;
                             val:boolean) : boolean;
  function GetObjProperty(comp:TObject; const propname:string) : TObject;


{------------------------------------------------------------------------------}
implementation
{------------------------------------------------------------------------------}
uses SysUtils,Typinfo;


{------------------------------------------------------------------------------}
function SetBetween(i,min,max:integer) : integer;
begin
  Result := i;
  if ( Result < min ) then Result := min;
  if ( Result > max ) then Result := max;
end;

{------------------------------------------------------------------------------}
function GetStringProperty(comp:TObject; const propname:string ;
                           const def:string) : string;
var
  PropInfo: PPropInfo;
begin
  Result := def;
  if ( comp = NIL  ) then exit;
  PropInfo := GetPropInfo(comp.ClassInfo, propname);
  if ( PropInfo = NIL ) then exit;
  if ( PropInfo^.name <> propname ) then exit;

  Result := GetStrProp(comp,PropInfo);
end;

{------------------------------------------------------------------------------}
function SetStringProperty(comp:TObject; const propname:string ;
                           const val:string) : boolean;
var
  PropInfo: PPropInfo;
begin
  Result := false;
  if ( comp = NIL  ) then exit;
  PropInfo := GetPropInfo(comp.ClassInfo, propname);
  if ( PropInfo = NIL ) then exit;
  if ( PropInfo^.name <> propname ) then exit;

  SetStrProp(comp,PropInfo,val);
  Result := true;
end;

{------------------------------------------------------------------------------}
function GetBooleanProperty(comp:TObject; const propname:string ;
                              def:boolean) : boolean;
var
  PropInfo: PPropInfo;
begin
  Result := def;
  if ( comp = NIL  ) then exit;
  PropInfo := GetPropInfo(comp.ClassInfo, propname);
  if ( PropInfo = NIL ) then exit;
  if ( PropInfo^.name <> propname ) then exit;

  Result := Boolean(GetOrdProp(comp,PropInfo));
end;

{------------------------------------------------------------------------------}
function SetBooleanProperty(comp:TObject; const propname:string ;
                           val:boolean) : boolean;
var
  PropInfo: PPropInfo;
begin
  Result := false;
  if ( comp = NIL  ) then exit;
  PropInfo := GetPropInfo(comp.ClassInfo, propname);
  if ( PropInfo = NIL ) then exit;
  if ( PropInfo^.name <> propname ) then exit;

  SetOrdProp(comp,PropInfo,Ord(val));
  Result := true;
end;

{------------------------------------------------------------------------------}
function GetObjProperty(comp:TObject; const propname:string) : TObject;
var
  PropInfo: PPropInfo;
begin
  Result := nil;
  if ( comp = NIL  ) then exit;
  PropInfo := GetPropInfo(comp.ClassInfo, propname);
  if ( PropInfo = NIL ) then exit;
  if ( PropInfo^.name <> propname ) then exit;
  Result := GetObjectProp(comp,PropInfo);
end;









end.
