--------------------------------------------------------------------------------
--                                                                            --
-- Copyright (C) 2004, RISC OS Ada Library (RASCAL) developers.               --
--                                                                            --
-- This library is free software; you can redistribute it and/or              --
-- modify it under the terms of the GNU Lesser General Public                 --
-- License as published by the Free Software Foundation; either               --
-- version 2.1 of the License, or (at your option) any later version.         --
--                                                                            --
-- This library is distributed in the hope that it will be useful,            --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of             --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU           --
-- Lesser General Public License for more details.                            --
--                                                                            --
-- You should have received a copy of the GNU Lesser General Public           --
-- License along with this library; if not, write to the Free Software        --
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA    --
--                                                                            --
--------------------------------------------------------------------------------

-- $Author$
-- $Date$
-- $Revision$

with RASCAL.Memory;     use RASCAL.Memory;

with Interfaces.C;      use Interfaces.C;
with Kernel;            use Kernel;


package body RASCAL.ToolboxToolAction is


   -- SWIs
   ToolAction_ToolAction   : constant := 16#140140#;
   ToolAction_SetIdent     : constant := 16#140140#;
   ToolAction_GetIdent     : constant := 16#140141#;
   ToolAction_SetAction    : constant := 16#140142#;
   ToolAction_GetAction    : constant := 16#140143#;
   ToolAction_SetClickShow : constant := 16#140144#;
   ToolAction_GetClickShow : constant := 16#140145#;
   ToolAction_SetState     : constant := 16#140146#;
   ToolAction_GetState     : constant := 16#140147#;

   Toolbox_ObjectMiscOp    : constant := 16#44EC6#;

   --

   procedure Set_Ident (Window    : in Object_ID;
                        Component : in Component_ID;
                        Ident     : in String;
                        Flags     : in System.Unsigned_Types.Unsigned := 0) is

      Register       : aliased Kernel.swi_regs;
      Error          : oserror_access;
      Ident_0        : String := Ident & ASCII.NUL;
   begin
      Register.R(0) := Int(Unsigned_to_Int(Flags));
      Register.R(1) := Int(Window);
      Register.R(2) := ToolAction_SetIdent;
      Register.R(3) := Int(Component);
      Register.R(4) := Adr_To_Int(Ident_0'Address);
      Error := Kernel.swi(Toolbox_ObjectMiscOp,Register'Access,Register'Access);

      if Error /= null then
         pragma Debug(Report("ToolboxTextArea.Set_Ident: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;
   end Set_Ident;

   --

   function Get_Ident (Window    : in Object_ID;
                       Component : in Component_ID;
                       Flags     : in System.Unsigned_Types.Unsigned := 0) return String is

      Register       : aliased Kernel.swi_regs;
      Error          : oserror_access;
   begin
      Register.R(0) := Int(Unsigned_to_Int(Flags));
      Register.R(1) := Int(Window);
      Register.R(2) := ToolAction_GetIdent;
      Register.R(3) := Int(Component);
      Error := Kernel.swi(Toolbox_ObjectMiscOp,Register'Access,Register'Access);

      if Error /= null then
         pragma Debug(Report("ToolboxTextArea.Get_Ident: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;
      return MemoryToString(Int_To_Adr(Register.R(0)));
   end Get_Ident;

   --

   procedure Set_Action (Window      : in Object_ID;
                        Component    : in Component_ID;
                        SelectAction : in Integer;
                        AdjustAction : in Integer;
                        Flags        : in System.Unsigned_Types.Unsigned := 0) is

      Register       : aliased Kernel.swi_regs;
      Error          : oserror_access;
   begin
      Register.R(0) := Int(Unsigned_to_Int(Flags));
      Register.R(1) := Int(Window);
      Register.R(2) := ToolAction_SetAction;
      Register.R(3) := Int(Component);
      Register.R(4) := Int(SelectAction);
      Register.R(5) := Int(AdjustAction);
      Error := Kernel.swi(Toolbox_ObjectMiscOp,Register'Access,Register'Access);
      if Error /= null then
         pragma Debug(Report("ToolboxTextArea.Set_Action: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;
   end Set_Action;

   --

   procedure Get_Action (Window       : in Object_ID;
                         Component    : in Component_ID;
                         SelectAction : out Integer;
                         AdjustAction : out Integer;
                         Flags        : in System.Unsigned_Types.Unsigned := 0) is

      Register       : aliased Kernel.swi_regs;
      Error          : oserror_access;
   begin
      Register.R(0) := Int(Unsigned_to_Int(Flags));
      Register.R(1) := Int(Window);
      Register.R(2) := ToolAction_GetAction;
      Register.R(3) := Int(Component);
      Error := Kernel.swi(Toolbox_ObjectMiscOp,Register'Access,Register'Access);
      if Error /= null then
         pragma Debug(Report("ToolboxTextArea.Get_Action: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;
      SelectAction := Integer(Register.R(0));
      AdjustAction := Integer(Register.R(1));
   end Get_Action;

   --

   procedure Set_ClickShow(Window     : in Object_ID;
                           Component  : in Component_ID;
                           SelectShow : in String;
                           AdjustShow : in String;
                           Flags      : in System.Unsigned_Types.Unsigned := 0) is

      Register       : aliased Kernel.swi_regs;
      Error          : oserror_access;
   begin
      Register.R(0) := Int(Unsigned_to_Int(Flags));
      Register.R(1) := Int(Window);
      Register.R(2) := ToolAction_SetClickShow;
      Register.R(3) := Int(Component);
      Register.R(4) := Adr_To_Int(SelectShow'Address);
      Register.R(5) := Adr_To_Int(AdjustShow'Address);
      Error := Kernel.swi(Toolbox_ObjectMiscOp,Register'Access,Register'Access);
      if Error /= null then
         pragma Debug(Report("ToolboxTextArea.Set_ClickShow: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;
   end Set_ClickShow;

   --

   procedure Get_ClickShow(Window     : in Object_ID;
                           Component  : in Component_ID;
                           SelectShow : out UString;
                           AdjustShow : out UString;
                           Flags      : in System.Unsigned_Types.Unsigned := 0) is

      Register       : aliased Kernel.swi_regs;
      Error          : oserror_access;
   begin
      Register.R(0) := Int(Unsigned_to_Int(Flags));
      Register.R(1) := Int(Window);
      Register.R(2) := ToolAction_GetClickShow;
      Register.R(3) := Int(Component);
      Error := Kernel.swi(Toolbox_ObjectMiscOp,Register'Access,Register'Access);
      if Error /= null then
         pragma Debug(Report("ToolboxTextArea.Get_ClickShow: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;
      SelectShow := U(MemoryToString(Int_To_Adr(Register.R(0))));
      AdjustShow := U(MemoryToString(Int_To_Adr(Register.R(1))));
   end Get_ClickShow;

   --

   procedure Set_State (Window    : in Object_ID;
                        Component : in Component_ID;
                        State     : in Boolean;
                        Flags     : in System.Unsigned_Types.Unsigned := 0) is

      Register       : aliased Kernel.swi_regs;
      Error          : oserror_access;
   begin
      Register.R(0) := Int(Unsigned_to_Int(Flags));
      Register.R(1) := Int(Window);
      Register.R(2) := ToolAction_SetState;
      Register.R(3) := Int(Component);
      Register.R(4) := Int(Boolean'Pos(State));
      Error := Kernel.swi(Toolbox_ObjectMiscOp,Register'Access,Register'Access);
      if Error /= null then
         pragma Debug(Report("ToolboxTextArea.Set_State: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;
   end Set_State;

   --

   function Get_State (Window    : in Object_ID;
                       Component : in Component_ID;
                       Flags     : in System.Unsigned_Types.Unsigned := 0) return Boolean is

      Register       : aliased Kernel.swi_regs;
      Error          : oserror_access;
   begin
      Register.R(0) := Int(Unsigned_to_Int(Flags));
      Register.R(1) := Int(Window);
      Register.R(2) := ToolAction_GetState;
      Register.R(3) := Int(Component);
      Error := Kernel.swi(Toolbox_ObjectMiscOp,Register'Access,Register'Access);
      if Error /= null then
         pragma Debug(Report("ToolboxTextArea.Get_State: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;

      if Integer(Register.R(0)) = 1 then
         return true;
      else
         return false;
      end if;
   end Get_State;

   --

end RASCAL.ToolboxToolAction;
