1. ----------------------------------------------------------------------- 
  2. --               GtkAda - Ada95 binding for Gtk+/Gnome               -- 
  3. --                                                                   -- 
  4. --                    Copyright (C) 2011, AdaCore                    -- 
  5. --                                                                   -- 
  6. -- This library is free software; you can redistribute it and/or     -- 
  7. -- modify it under the terms of the GNU General Public               -- 
  8. -- License as published by the Free Software Foundation; either      -- 
  9. -- version 2 of the License, or (at your option) any later version.  -- 
  10. --                                                                   -- 
  11. -- This library is distributed in the hope that it will be useful,   -- 
  12. -- but WITHOUT ANY WARRANTY; without even the implied warranty of    -- 
  13. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU -- 
  14. -- General Public License for more details.                          -- 
  15. --                                                                   -- 
  16. -- You should have received a copy of the GNU General Public         -- 
  17. -- License along with this library; if not, write to the             -- 
  18. -- Free Software Foundation, Inc., 59 Temple Place - Suite 330,      -- 
  19. -- Boston, MA 02111-1307, USA.                                       -- 
  20. --                                                                   -- 
  21. -- -- -- -- -- -- -- -- -- -- -- --
  22. ----------------------------------------------------------------------- 
  23.  
  24. --  <description> 
  25. -- 
  26. --  This package provides a high-level API for using Gtk.Builder and 
  27. --  user interface files produced with the GUI builder glade-3. 
  28. -- 
  29. --  Here is how to use this package: 
  30. -- 
  31. --     Step 1: create a Builder and add the XML data, just as you would a 
  32. --             standard Gtk.Builder: 
  33. -- 
  34. --             declare 
  35. --                Builder : Gtkada_Builder; 
  36. --                Error   : GError; 
  37. --             begin 
  38. --                Gtk_New (Builder); 
  39. --                Error := Add_From_File (Builder, Default_Filename); 
  40. -- 
  41. --     Step 2: add calls to "Register_Handler" to associate your handlers 
  42. --             with your callbacks. 
  43. -- 
  44. --                Register_Handler 
  45. --                   (Builder      => Builder, 
  46. --                    Handler_Name => "my_handler_id", 
  47. --                    Handler      => My_Handler'Access); 
  48. -- 
  49. --             Where: 
  50. --              - Builder is your Gtkada_Builder, 
  51. --              - "my_handler_id" is the name of the handler as specified in 
  52. --                  Glade-3, in the "Handler" column of the "Signals" tab for 
  53. --                  your object, 
  54. --              - Handler is your Ada subprogram. 
  55. -- 
  56. --              You will need one call to "Register_Handler" per handler 
  57. --              declared in the Glade-3 interface. If there is one or more 
  58. --              handler declared in Glade-3 which does not have an associated 
  59. --              call to Register_Handler, an ASSERT_FAILURE will be raised by 
  60. --              the Gtk main loop. 
  61. -- 
  62. --              There are multiple way to call Register_Handler, see below. 
  63. -- 
  64. --     Step 3: call Do_Connect. 
  65. -- 
  66. --     Step 4: when the application terminates or all Windows described through 
  67. --             your builder should be closed, call Unref to free memory 
  68. --             associated with the Builder. 
  69. -- 
  70. --  </description> 
  71. --  <group>GUI Builder</group> 
  72.  
  73. pragma Ada_2005; 
  74.  
  75. with Ada.Containers.Hashed_Maps; 
  76. with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; 
  77. with Ada.Strings.Unbounded.Hash; 
  78.  
  79. with Glib.Object; use Glib.Object; 
  80. with Gtk.Builder; 
  81.  
  82. package Gtkada.Builder is 
  83.  
  84.    type Gtkada_Builder_Record is new 
  85.      Gtk.Builder.Gtk_Builder_Record with private; 
  86.    type Gtkada_Builder is access all Gtkada_Builder_Record'Class; 
  87.  
  88.    procedure Gtk_New (Builder : out Gtkada_Builder); 
  89.    procedure Initialize (Builder : access Gtkada_Builder_Record'Class); 
  90.    --  Create a new Gtkada_Builder. 
  91.  
  92.    procedure Do_Connect (Builder : access Gtkada_Builder_Record'Class); 
  93.    --  Activate in the builder callabacks that have been connected using 
  94.    --  calls to Register_Handler functions below. 
  95.  
  96.    -------------------------------------- 
  97.    -- Callbacks working on the Builder -- 
  98.    -------------------------------------- 
  99.  
  100.    --  These callbacks take as parameter the Gtkada_Builder. 
  101.    --  If a "User data" is present in the Glade-3, it will be ignored. 
  102.  
  103.    type Builder_Handler is access procedure 
  104.      (Builder : access Gtkada_Builder_Record'Class); 
  105.  
  106.    type Builder_Return_Handler is access function 
  107.      (User_Data : access Gtkada_Builder_Record'Class) return Boolean; 
  108.  
  109.    procedure Register_Handler 
  110.      (Builder      : access Gtkada_Builder_Record'Class; 
  111.       Handler_Name : String; 
  112.       Handler      : Builder_Handler); 
  113.  
  114.    procedure Register_Handler 
  115.      (Builder      : access Gtkada_Builder_Record'Class; 
  116.       Handler_Name : String; 
  117.       Handler      : Builder_Return_Handler); 
  118.  
  119.    -------------------------------------------------------------- 
  120.    -- Callbacks working on user data specified through Glade-3 -- 
  121.    -------------------------------------------------------------- 
  122.  
  123.    --  Use these registry functions if your signal handler was defined in 
  124.    --  the Glade-3 interface with a "User data". The parameter User_Data 
  125.    --  passed to the handlers corresponds to the object entered in the 
  126.    --  "User data" column in Glade-3. 
  127.  
  128.    type Object_Handler is access procedure 
  129.      (User_Data : access GObject_Record'Class); 
  130.  
  131.    type Object_Return_Handler is access function 
  132.      (User_Data : access GObject_Record'Class) return Boolean; 
  133.  
  134.    procedure Register_Handler 
  135.      (Builder      : access Gtkada_Builder_Record'Class; 
  136.       Handler_Name : String; 
  137.       Handler      : Object_Handler); 
  138.  
  139.    procedure Register_Handler 
  140.      (Builder      : access Gtkada_Builder_Record'Class; 
  141.       Handler_Name : String; 
  142.       Handler      : Object_Return_Handler); 
  143.  
  144. private 
  145.  
  146.    type Handler_Type is (Object, Object_Return, Builder, Builder_Return); 
  147.    type Universal_Marshaller (T : Handler_Type) is record 
  148.       case T is 
  149.          when Object => 
  150.             The_Object_Handler : Object_Handler; 
  151.          when Object_Return => 
  152.             The_Object_Return_Handler : Object_Return_Handler; 
  153.          when Builder => 
  154.             The_Builder_Handler : Builder_Handler; 
  155.          when Builder_Return => 
  156.             The_Builder_Return_Handler : Builder_Return_Handler; 
  157.       end case; 
  158.    end record; 
  159.  
  160.    type Universal_Marshaller_Access is access Universal_Marshaller; 
  161.  
  162.    package Handlers_Map is new Ada.Containers.Hashed_Maps 
  163.      (Key_Type        => Unbounded_String, 
  164.       Element_Type    => Universal_Marshaller_Access, 
  165.       Hash            => Ada.Strings.Unbounded.Hash, 
  166.       Equivalent_Keys => "=", 
  167.       "="             => "="); 
  168.  
  169.    type Gtkada_Builder_Record is new 
  170.      Gtk.Builder.Gtk_Builder_Record 
  171.    with record 
  172.       Handlers : Handlers_Map.Map; 
  173.    end record; 
  174.  
  175. end Gtkada.Builder;