{***********************************************************
                R&A Library
                   RAI2
       Copyright (C) 1998-2001 Andrei Prygounkov

       description : adapter unit - converts RAI2 calls to delphi calls

       author      : Andrei Prygounkov
       e-mail      : a.prygounkov@gmx.de
       www         : http://ralib.hotbox.ru
************************************************************}
{$INCLUDE RA.INC}

unit RAI2_Types;


interface

uses Types, Variants, RAI2;

  function Point2Var(const Point: TPoint): Variant;
  function Var2Point(const Point: Variant): TPoint;
  function Rect2Var(const Rect: TRect): Variant;
  function Var2Rect(const Rect: Variant): TRect;

  procedure RegisterRAI2Adapter(RAI2Adapter: TRAI2Adapter);

implementation

  { TPoint }

function Point2Var(const Point: TPoint): Variant;
var
  Rec: ^TPoint;
begin
  New(Rec);
  Rec^ := Point;
  Result := R2V('TPoint', Rec);
end;

function Var2Point(const Point: Variant): TPoint;
begin
  Result := TPoint(V2R(Point)^);
end;

procedure RAI2_Point(var Value: Variant; Args: TArgs);
begin
  Value := Point2Var(Point(Args.Values[0], Args.Values[1]));
end;

  { TRect }

function Rect2Var(const Rect: TRect): Variant;
var
  Rec: ^TRect;
begin
  New(Rec);
  Rec^ := Rect;
  Result := R2V('TRect', Rec);
end;

function Var2Rect(const Rect: Variant): TRect;
begin
  Result := TRect(V2R(Rect)^);
end;

procedure RAI2_Rect(var Value: Variant; Args: TArgs);
begin
  Value := Rect2Var(Rect(Args.Values[0], Args.Values[1], Args.Values[2], Args.Values[3]));
end;

procedure RAI2_Bounds(var Value: Variant; Args: TArgs);
begin
  Value := Rect2Var(Bounds(Args.Values[0], Args.Values[1], Args.Values[2], Args.Values[3]));
end;

{  Read Field TopLeft: Integer; }
procedure TRect_Read_TopLeft(var Value: Variant; Args: TArgs);
begin
  Value := Point2Var(TRect(P2R(Args.Obj)^).TopLeft);
end;

{  Write Field TopLeft: Integer; }
procedure TRect_Write_TopLeft(const Value: Variant; Args: TArgs);
begin
  TRect(P2R(Args.Obj)^).TopLeft := Var2Point(Value);
end;

{  Read Field BottomRight: Integer; }
procedure TRect_Read_BottomRight(var Value: Variant; Args: TArgs);
begin
  Value := Point2Var(TRect(P2R(Args.Obj)^).BottomRight);
end;

{  Write Field Right: Integer; }
procedure TRect_Write_BottomRight(const Value: Variant; Args: TArgs);
begin
  TRect(P2R(Args.Obj)^).BottomRight := Var2Point(Value);
end;


procedure RegisterRAI2Adapter(RAI2Adapter: TRAI2Adapter);
begin
  with RAI2Adapter do
  begin
    AddExtUnit('Types');
   { TPoint }
    AddRec('Types', 'TPoint', sizeof(TPoint), [RFD('X', 0, varInteger), RFD('Y', 4, varInteger)], nil, nil, nil);
    AddFun('Types', 'Point', RAI2_Point, 2, [varInteger, varInteger], varRecord);
   { TRect }
    AddRec('Types', 'TRect', sizeof(TRect), [RFD('Left', 0, varInteger), RFD('Top', 4, varInteger), RFD('Right', 8, varInteger), RFD('Bottom', 12, varInteger)], nil, nil, nil);
    AddFun('Types', 'Rect', RAI2_Rect, 4, [varInteger, varInteger, varInteger, varInteger], varRecord);
    AddFun('Types', 'Bounds', RAI2_Bounds, 4, [varInteger, varInteger, varInteger, varInteger], varRecord);
    AddRecGet('Types', 'TRect', 'TopLeft', TRect_Read_TopLeft, 0, [0], varRecord);
    AddRecSet('Types', 'TRect', 'TopLeft', TRect_Write_TopLeft, 0, [0]);
    AddRecGet('Types', 'TRect', 'BottomRight', TRect_Read_BottomRight, 0, [0], varRecord);
    AddRecSet('Types', 'TRect', 'BottomRight', TRect_Write_BottomRight, 0, [0]);
  end;    { with }
end;    { RegisterRAI2Adapter }

end.
