------------------------------------------------------------------------------
--                                                                          --
--                An Ada2005 binding to the Qt C++ framework                --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2008-2010 Vadim Godunko <vgodunko@gmail.com>                 --
--                                                                          --
-- QtAda is free software;  you can  redistribute it and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
-- sion. QtAda is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License distributed with QtAda;  see file COPYING.  If not, write --
-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
-- Boston, MA 02110-1301, USA.                                              --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable  to  be --
-- covered  by the  GNU  General  Public  License.  This exception does not --
-- however invalidate  any other reasons why  the executable file  might be --
-- covered by the  GNU Public License.                                      --
--                                                                          --
------------------------------------------------------------------------------
--  $Revision: 3769 $ $Date: 2010-10-24 03:16:28 +0400 (Sun, 24 Oct 2010) $
------------------------------------------------------------------------------
with Ada.Containers.Indefinite_Vectors;
with Ada.Finalization;

package body Qt4.Style_Options is

   function type_wrapper (Self : not null access Thin.QStyleOption)
     return Interfaces.C.int;
   pragma Import (C, type_wrapper, "__Qt4Ada_QStyleOption_type_wrapper");

   function version_wrapper (Self : not null access Thin.QStyleOption)
     return Interfaces.C.int;
   pragma Import (C, version_wrapper, "__Qt4Ada_QStyleOption_version_wrapper");

   function new_QStyleOption return not null access Qt4_Entities.Cpp_Class;
   pragma Import (C, new_QStyleOption, "__Qt4Ada_new_QStyleOption");

   procedure delete_QStyleOption
    (Cpp_Object : not null access Qt4_Entities.Cpp_Class);
   pragma Import (C, delete_QStyleOption, "__Qt4Ada_delete_QStyleOption");

   function state_wrapper (Self : not null access Thin.QStyleOption)
     return Qt4.Styles.State;
   pragma Import (C, state_wrapper, "__Qt4Ada_QStyleOption_state_wrapper");

   procedure setRect_wrapper
    (Self : not null access Qt4.Style_Options.Thin.QStyleOption;
     Rect : not null Qt4.Rects.Internals.QRect_Access);
   pragma Import
    (C, setRect_wrapper, "__Qt4Ada_QStyleOption_setRect_wrapper");

   procedure rect_wrapper
    (Self    : not null access Qt4.Style_Options.Thin.QStyleOption;
     Returns : not null Qt4.Rects.Internals.QRect_Access);
   pragma Import
    (C, rect_wrapper, "__Qt4Ada_QStyleOption_rect_wrapper");

   type Style_Option_Info is record
      The_Type    : Internals.Option_Type;
      The_Version : Internals.Style_Option_Version;
      Func        : Internals.Ada_Object_Constructor;
   end record;

   package Registry_Vectors is
     new Ada.Containers.Indefinite_Vectors (Positive, Style_Option_Info);

   Registry : Registry_Vectors.Vector;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Create
   --!    <Purpose>
   --!    <Exceptions>
   ---------------------------------------------------------------------------
   function Create (The_Type    : Internals.Option_Type;
                    The_Version : Internals.Style_Option_Version;
                    Cpp_Object  : not null access Thin.QStyleOption)
     return Q_Style_Option'Class;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Copy_Cpp_Object
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   overriding
   function Copy_Cpp_Object (Self : not null access Q_Style_Option)
     return not null access Qt4_Entities.Cpp_Class
   is
   begin
      return Thin.copy_QStyleOption (Self.Cpp_Object);
   end Copy_Cpp_Object;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Create
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   function Create (The_Type    : Internals.Option_Type;
                    The_Version : Internals.Style_Option_Version;
                    Cpp_Object  : not null access Thin.QStyleOption)
     return Q_Style_Option'Class
   is
      pragma Unreferenced (The_Type);
      pragma Unreferenced (The_Version);

   begin
      return
        Q_Style_Option'
         (Ada.Finalization.Controlled with
            Cpp_Object => Cpp_Object,
            Role       => Qt4_Entities.Wrapper,
            Class      => Qt4_Entities.Static,
            others     => <>);
    end Create;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Create
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   function Create return Q_Style_Option is
      Result : Q_Style_Option;

   begin
      return Result;
   end Create;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Default_Cpp_Object
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   overriding function Default_Cpp_Object (Self : not null access Q_Style_Option)
     return access Qt4_Entities.Cpp_Class
   is
      pragma Unreferenced (Self);
      --  Параметр необходим для диспетчеризации надклассового вызова.

   begin
      return new_QStyleOption;
   end Default_Cpp_Object;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Delete_Cpp_Object
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   overriding
   procedure Delete_Cpp_Object (Self : not null access Q_Style_Option)
   is
   begin
      delete_QStyleOption (Self.Cpp_Object);
   end Delete_Cpp_Object;

   ----------
   -- Rect --
   ----------

   function Rect
    (Self : Q_Style_Option'Class)
       return Qt4.Rects.Q_Rect
   is
   begin
      return Result : Qt4.Rects.Q_Rect do
         rect_wrapper
          (Qt4.Style_Options.Internals.QStyleOption_View (Self),
           Qt4.Rects.Internals.QRect_View (Result));
      end return;
   end Rect;

   --------------
   -- Set_Rect --
   --------------

   procedure Set_Rect
    (Self : Q_Style_Option'Class;
     Rect : Qt4.Rects.Q_Rect)
   is
   begin
      setRect_wrapper
       (Qt4.Style_Options.Internals.QStyleOption_View (Self),
        Qt4.Rects.Internals.QRect_View (Rect));
   end Set_Rect;

   ---------------
   -- Internals --
   ---------------

   package body Internals is

      ------------------------------------------------------------------------
      --! <Subprogram>
      --!    <Unit> Create_Wrapper
      --!    <ImplementationNotes>
      ------------------------------------------------------------------------
      function Create_Wrapper (Cpp_Object : not null access Thin.QStyleOption)
        return Q_Style_Option'Class
      is
         use Registry_Vectors;

         The_Type    : constant Option_Type
           := Option_Type (type_wrapper (Cpp_Object));
         The_Version : constant Style_Option_Version
           := Style_Option_Version (version_wrapper (Cpp_Object));
         C           : Registry_Vectors.Cursor := First (Registry);

      begin
         while C /= Registry_Vectors.No_Element loop
            if Element (C).The_Type = The_Type
                 and Element (C).The_Version = The_Version
            then
               return Element (C).Func (The_Type, The_Version, Cpp_Object);
            end if;

            C := Next (C);
         end loop;

         raise Program_Error
            with "Style_Option type " & The_Type'Img & " not registered";
      end Create_Wrapper;

      ------------------------------------------------------------------------
      --! <Subprogram>
      --!    <Unit> Cpp_Object
      --!    <ImplementationNotes>
      ------------------------------------------------------------------------
      function QStyleOption_View (Self : Q_Style_Option'Class)
        return not null access Thin.QStyleOption
      is
      begin
         return Qt4_Entities.Cpp_Object (Self);
      end QStyleOption_View;

      ------------------------------------------------------------------------
      --! <Subprogram>
      --!    <Unit> Register
      --!    <ImplementationNotes>
      ------------------------------------------------------------------------
      procedure Register (The_Type    : Option_Type;
                          The_Version : Style_Option_Version;
                          Constructor : Ada_Object_Constructor)
      is
         use Registry_Vectors;
         C : Registry_Vectors.Cursor := First (Registry);

      begin
         --  Выполняем поиск конструктора в реестре.
         --  Если добавляемый конструктор уже зарегистрирован,
         --  то возбуждаем исключение.

         while C /= Registry_Vectors.No_Element loop
            if Element (C).The_Type = The_Type
              and Element (C).The_Version = The_Version
            then
               raise Program_Error with "Ada constructor for "
                                          & The_Type'Img
                                          & " already registered";
            end if;
            C := Next (C);
         end loop;

         --  Если конструктор не найден, добавляем его.

         Registry.Append ((The_Type, The_Version, Constructor));

      end Register;
   end Internals;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> State
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   function State (Self : Q_Style_Option) return Qt4.Styles.State is
   begin
      return state_wrapper (Internals.QStyleOption_View (Self));
   end State;

begin
   Internals.Register (Internals.SO_Default, Internals.Version, Create'Access);
end Qt4.Style_Options;
