--------------------------------------------------------------------------------
--                                                                            --
-- 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 RASCAL.OS;         use RASCAL.OS;

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


package body RASCAL.ToolboxFontMenu is

   -- SWIs
   Toolbox_ObjectMiscOp : constant := 16#44EC6#;

   FontMenu_ClassSWI    : constant := 16#82A40#;
   FontMenu_PostFilter  : constant := 16#82A41#;
   FontMenu_PreFilter   : constant := 16#82A42#;

   -- Reason codes
   FontMenu_SetFont     : constant := 16#0#;
   FontMenu_GetFont     : constant := 16#1#;

   --

   function Get_Font (FontMenu : in Object_ID;
                      Flags    : in System.Unsigned_Types.Unsigned := 0) return string is

      Register             : aliased Kernel.swi_regs;
      Error                : oserror_access;
      Buffer_Size          : integer;
   begin
      Register.R(0) := int(Unsigned_to_Int(Flags));
      Register.R(1) := int(FontMenu);
      Register.R(2) := FontMenu_GetFont;
      Register.R(3) := 0;
      Register.R(4) := 0;
      Error := Kernel.swi(Toolbox_ObjectMiscOp,Register'Access,Register'Access);

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

      Buffer_Size := integer(Register.R(4));

      declare
         Buffer : string(1..Buffer_Size);
      begin
         Register.R(0) := int(Unsigned_to_Int(Flags));
         Register.R(1) := int(FontMenu);
         Register.R(2) := FontMenu_GetFont;
         Register.R(3) := Adr_To_Int(Buffer'Address);
         Register.R(4) := int(Buffer_Size);
         Error := Kernel.swi(Toolbox_ObjectMiscOp,Register'Access,Register'Access);

         if Error /= null then
            pragma Debug(Report("ToolboxFontMenu.Get_Font(II): " & To_Ada(Error.ErrMess)));
            OS.Raise_Error(Error);
         end if;
         return MemoryToString(Buffer'Address);
      end;
   end Get_Font;

   --

   procedure Set_Font (FontMenu  : in Object_ID;
                       Font      : in String;
                       Flags     : in System.Unsigned_Types.Unsigned := 0) is

      Register   : aliased Kernel.swi_regs;
      Error      : oserror_access;
      Font_0     : String := Font & ASCII.NUL;
   begin
      Register.R(0) := int(Unsigned_to_Int(Flags));
      Register.R(1) := int(FontMenu);
      Register.R(2) := FontMenu_SetFont;
      Register.R(3) := Adr_To_Int(Font_0'Address);
      Error := Kernel.swi(Toolbox_ObjectMiscOp,Register'Access,Register'Access);

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

   end Set_Font;

   --   
   
end RASCAL.ToolboxFontMenu;
