------------------------------------------------------------------------------
--                                                                          --
--                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 Qt4_Entities.Constructors;
with Qt4_Debug;

package body Qt4.Gradients is

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

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

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

   procedure setColorAt_wrapper
    (Self  : not null Internals.QGradient_Access;
     Pos   : Interfaces.C.double;
     Color : not null access Qt4.Colors.Internals.QColor);
   pragma Import
    (C, setColorAt_wrapper, "__Qt4Ada_QGradient_setColorAt_wrapper");

   Registry : array (Internals.Gradient_Type)
     of Internals.Ada_Object_Constructor
       := (others => null);
   --  Реестр конструкторов Ada объектов.

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Create
   --!    <Purpose>
   --!    <Exceptions>
   ---------------------------------------------------------------------------
   function Create (The_Type    : Internals.Gradient_Type)
     return Q_Gradient'Class;

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

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Create
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   function Create (The_Type    : Internals.Gradient_Type)
     return Q_Gradient'Class
   is
      pragma Unreferenced (The_Type);

      Result : Q_Gradient;

   begin
      return Result;
    end Create;

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

   begin
      return new_QGradient;
   end Default_Cpp_Object;

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

   ---------------
   -- Internals --
   ---------------
   package body Internals is

      ------------------------------------------------------------------------
      --! <Subprogram>
      --!    <Unit> Create_Wrapper
      --!    <ImplementationNotes>
      ------------------------------------------------------------------------
      function Create_Wrapper (Cpp_Object : not null access Thin.QGradient)
        return Q_Gradient'Class
      is
         The_Type    : constant Gradient_Type
           := Gradient_Type (type_wrapper (Cpp_Object));

      begin
         if Registry (The_Type) = null then
            Qt4_Debug.Output
             ("Gradient_Type type" & Gradient_Type'Image (The_Type)
                & " not registered");

            raise Program_Error
              with "Gradient type" & Gradient_Type'Image (The_Type)
                      & " not registered";
         end if;

         return Result : aliased Q_Gradient'Class
           := Registry (The_Type) (The_Type)
         do
            Qt4_Entities.Constructors.Initialize_Wrapper
             (Qt4_Entities.Entity'Class (Result)'Access,
              Cpp_Object);
         end return;
      end Create_Wrapper;

      --------------------
      -- QGradient_View --
      --------------------

      function QGradient_View (Self : Q_Gradient'Class)
        return not null QGradient_Access
      is
      begin
         return QGradient_Access (Qt4_Entities.Cpp_Object (Self));
      end QGradient_View;

      ------------------------------------------------------------------------
      --! <Subprogram>
      --!    <Unit> Register
      --!    <ImplementationNotes>
      ------------------------------------------------------------------------
      procedure Register (The_Type    : Gradient_Type;
                          Constructor : Ada_Object_Constructor)
      is
      begin
         if Registry (The_Type) /= null then
         raise Program_Error
           with "Ada constructor for Gradient type " & The_Type'Img
                   & " already registered";
      end if;

      Registry (The_Type) := Constructor;
      end Register;
   end Internals;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Set_Color_At
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   procedure Set_Color_At (Self  : Q_Gradient;
                           Pos   : Q_Real;
                           Color : Qt4.Colors.Q_Color)
   is
   begin
      setColorAt_wrapper (Internals.QGradient_View (Self),
                          Interfaces.C.double (Pos),
                          Qt4.Colors.Internals.QColor_View (Color));
   end Set_Color_At;

begin
   Internals.Register (Internals.No_Gradient, Create'Access);
end Qt4.Gradients;
