------------------------------------------------------------------------------
--                                                                          --
--                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.Text_Formats;
with Qt4.Text_Frames;
with Qt_Ada.Internals.Object_Registry;
with Qt_Ada.Internals.Objects;

package body Qt4.Text_Cursors is

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

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

   procedure assign_QTextCursor
    (P_1 : not null access          Thin.QTextCursor;
     P_2 : not null access constant Thin.QTextCursor);
   pragma Import (C, assign_QTextCursor, "__Qt4Ada_assign_QTextCursor");

   function copy_QTextCursor
    (P_1 : not null access Qt4_Entities.Cpp_Class)
       return not null access Qt4_Entities.Cpp_Class;
   pragma Import (C, copy_QTextCursor, "__Qt4Ada_copy_QTextCursor");

   function movePosition_wrapper
    (Self      : not null access Thin.QTextCursor;
     Operation : Move_Operation;
     Mode      : Move_Mode := Move_Anchor;
     N         : Interfaces.C.int) return Qt4.bool;
   pragma Import (C, movePosition_wrapper, "__Qt4Ada_movePosition_wrapper");

   procedure insertBlock_wrapper
    (Self      : not null access Thin.QTextCursor);
   pragma Import (C, insertBlock_wrapper, "__Qt4Ada_insertBlock_wrapper");

   function isNull_wrapper (Self : not null access Thin.QTextCursor)
     return Qt4.bool;
   pragma Import (C, isNull_wrapper, "__Qt4Ada_isNull_wrapper");

   procedure beginEditBlock_wrapper (Self : not null access Thin.QTextCursor);
   pragma Import
    (C, beginEditBlock_wrapper, "__Qt4Ada_beginEditBlock_wrapper");

   procedure endEditBlock_wrapper (Self : not null access Thin.QTextCursor);
   pragma Import (C, endEditBlock_wrapper, "__Qt4Ada_endEditBlock_wrapper");

   procedure insertText_0_wrapper
    (Self : not null access Thin.QTextCursor;
     Text : not null access Qt4.Strings.Internals.QString);
   pragma Import (C, insertText_0_wrapper, "__Qt4Ada_insertText_0_wrapper");

   procedure insertText_1_wrapper
    (Self   : not null access Thin.QTextCursor;
     Text   : not null access Qt4.Strings.Internals.QString;
     Format : not null access Qt4.Text_Formats.Internals.QTextFormat);
   pragma Import (C, insertText_1_wrapper, "__Qt4Ada_insertText_1_wrapper");

   procedure setPosition_wrapper
    (Self : not null access Thin.QTextCursor;
     Pos  : Interfaces.C.int;
     Mode : Move_Mode);
   pragma Import (C, setPosition_wrapper, "__Qt4Ada_setPosition_wrapper");

   function currentFrame_wrapper (Self : not null access Thin.QTextCursor)
     return not null access Qt_Ada.Internals.Objects.QObject;
   pragma Import
    (C, currentFrame_wrapper, "__Qt4Ada_QTextCursor_currentFrame_wrapper");

   procedure insertTable_0_wrapper
    (Self   : not null access Thin.QTextCursor;
     Row    : Interfaces.C.int;
     Col    : Interfaces.C.int);
   pragma Import
    (C, insertTable_0_wrapper, "__Qt4Ada_QTextCursor_insertTable_0_wrapper");

   procedure insertTable_1_wrapper
    (Self   : not null access Thin.QTextCursor;
     Row    : Interfaces.C.int;
     Col    : Interfaces.C.int;
     Format : not null access Qt4.Text_Formats.Internals.QTextFormat);
   pragma Import
    (C, insertTable_1_wrapper, "__Qt4Ada_QTextCursor_insertTable_1_wrapper");

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Begin_Edit_Block
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   procedure Begin_Edit_Block (Self : Q_Text_Cursor) is
   begin
      beginEditBlock_wrapper (Internals.QTextCursor_View (Self));
   end Begin_Edit_Block;

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

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

   begin
      return Result;
   end Create;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Current_Frame
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   function Current_Frame (Self : Q_Text_Cursor)
     return not null access Qt4.Text_Frames.Q_Text_Frame'Class
   is
   begin
     return
        Qt4.Text_Frames.Q_Text_Frame'Class
         (Qt_Ada.Internals.Object_Registry.To_Q_Object
           (currentFrame_wrapper
             (Internals.QTextCursor_View (Self))).all)'Access;
   end Current_Frame;

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

   begin
      return new_QTextCursor;
   end Default_Cpp_Object;

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

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> End_Edit_Block
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   procedure End_Edit_Block (Self : Q_Text_Cursor) is
   begin
      endEditBlock_wrapper (Internals.QTextCursor_View (Self));
   end End_Edit_Block;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Insert_Block
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   procedure Insert_Block (Self : Q_Text_Cursor) is
   begin
      insertBlock_wrapper (Internals.QTextCursor_View (Self));
   end Insert_Block;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Insert_Table
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   procedure Insert_Table (Self : Q_Text_Cursor;
                           Row : Q_Integer;
                           Col : Q_Integer)
   is
   begin
      insertTable_0_wrapper (Internals.QTextCursor_View (Self),
                             Interfaces.C.int (Row),
                             Interfaces.C.int (Col));
   end Insert_Table;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Insert_Text
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   procedure Insert_Table
    (Self   : Q_Text_Cursor;
     Row    : Q_Integer;
     Col    : Q_Integer;
     Format : Qt4.Text_Table_Formats.Q_Text_Table_Format)
   is
   begin
      insertTable_1_wrapper
       (Internals.QTextCursor_View (Self),
        Interfaces.C.int (Row),
        Interfaces.C.int (Col),
        Qt4.Text_Formats.Internals.QTextFormat_View (Format));
   end Insert_Table;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit>
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   procedure Insert_Text (Self : Q_Text_Cursor;
                          Text : Qt4.Strings.Q_String)
   is
   begin
      insertText_0_wrapper (Internals.QTextCursor_View (Self),
                            Qt4.Strings.Internals.QString_View (Text));
   end Insert_Text;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Insert_Text
   --!    <Purpose>
   --!    <Exceptions>
   ---------------------------------------------------------------------------
   procedure Insert_Text
    (Self   : Q_Text_Cursor;
     Text   : Qt4.Strings.Q_String;
     Format : Qt4.Text_Char_Formats.Q_Text_Char_Format)
   is
   begin
      insertText_1_wrapper
       (Internals.QTextCursor_View (Self),
        Qt4.Strings.Internals.QString_View (Text),
        Qt4.Text_Formats.Internals.QTextFormat_View (Format));
   end Insert_Text;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Is_Null
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   function Is_Null (Self : Q_Text_Cursor) return Boolean is
   begin
      return Qt4.To_Boolean
                  (isNull_wrapper (Internals.QTextCursor_View (Self)));
   end Is_Null;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Move_Position
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   function Move_Position (Self      : Q_Text_Cursor;
                           Operation : Move_Operation;
                           Mode      : Move_Mode := Move_Anchor;
                           N         : Qt4.Q_Integer := 1)
     return Boolean is
   begin
      return Qt4.To_Boolean
              (movePosition_wrapper (Internals.QTextCursor_View (Self),
                                     Operation,
                                     Mode,
                                     Interfaces.C.int (N)));
   end Move_Position;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Move_Position
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   procedure Move_Position (Self      : Q_Text_Cursor;
                            Operation : Move_Operation;
                            Mode      : Move_Mode := Move_Anchor;
                            N         : Qt4.Q_Integer := 1) is
   begin
      if not Qt4.To_Boolean
              (movePosition_wrapper (Internals.QTextCursor_View (Self),
                                     Operation,
                                     Mode,
                                     Interfaces.C.int (N)))
      then
         raise Program_Error with "Q_Text_Cursor.Move_Position";
      end if;
   end Move_Position;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Set_Position
   --!    <ImplementationNotes>
   ---------------------------------------------------------------------------
   procedure Set_Position (Self : Q_Text_Cursor;
                           Pos  : Q_Integer;
                           Mode : Move_Mode := Move_Anchor)
   is
   begin
      setPosition_wrapper (Internals.QTextCursor_View (Self),
                           Interfaces.C.int (Pos),
                           Mode);
   end Set_Position;

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

   package body Internals is

      ------------------------------------------------------------------------
      --! <Subprogram>
      --!    <Unit> Cpp_Object
      --!    <ImplementationNotes>
      ------------------------------------------------------------------------
      function Cpp_Object (Self : Q_Text_Cursor)
        return not null access Thin.QTextCursor
      is
      begin
         return Qt4_Entities.Cpp_Object (Self);
      end Cpp_Object;

      ------------------------------------------------------------------------
      --! <Subprogram>
      --!    <Unit> To_Q_Text_Cursor
      --!    <ImplementationNotes>
      ------------------------------------------------------------------------
      function To_Q_Text_Cursor
       (Item : not null access constant Thin.QTextCursor)
          return Q_Text_Cursor
      is
      begin
         return Result : Q_Text_Cursor do
            assign_QTextCursor (Internals.QTextCursor_View (Result), Item);
         end return;
      end To_Q_Text_Cursor;

   end Internals;

end Qt4.Text_Cursors;
