1. ----------------------------------------------------------------------- 
  2. --               GtkAda - Ada95 binding for Gtk+/Gnome               -- 
  3. --                                                                   -- 
  4. --                   Copyright (C) 2001-2008, 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 minimal binding to the GObject type in Glib. 
  27. --  See Glib.Properties for information on how to manipulate properties 
  28. -- 
  29. --  </description> 
  30. --  <group>Glib, the general-purpose library</group> 
  31.  
  32. with Gtkada.Types; 
  33. with Glib.GSlist; 
  34. with Glib.Glist; 
  35. pragma Elaborate_All (Glib.GSlist); 
  36. pragma Elaborate_All (Glib.Glist); 
  37.  
  38. package Glib.Object is 
  39.  
  40.    type GObject_Record is tagged private; 
  41.    type GObject is access all GObject_Record'Class; 
  42.    --  The base type for Glib/Gdk/Gtk objects. It basically gives access 
  43.    --  to an underlying C object. This is not a controlled type for 
  44.    --  efficiency reasons and because glib takes care of the memory 
  45.    --  management on its own. 
  46.  
  47.    function Is_Created (Object : GObject_Record'Class) return Boolean; 
  48.    --  Return True if the associated C object has been created, False if 
  49.    --  no C object is associated with Object. 
  50.    --  This is not the same as testing whether an access type (for instance 
  51.    --  any of the widgets) is "null", since this relates to the underlying 
  52.    --  C object. 
  53.  
  54.    function Get_Type (Object : access GObject_Record) return GType; 
  55.    --  Return the type of Object. 
  56.    --  This function is mostly used internally, since in Ada you can simply 
  57.    --  test whether an object belong to a class with a statement like: 
  58.    -- 
  59.    --     if Object in Gtk_Button_Record'Class then ... 
  60.    -- 
  61.    --  which is easier. 
  62.  
  63.    ---------------- 
  64.    -- Life cycle -- 
  65.    ---------------- 
  66.  
  67.    procedure G_New (Object : out GObject); 
  68.    --  Create a new GObject. 
  69.    --  This is only required when you want to create an Ada tagged type to 
  70.    --  which you can attach new signals. Most of the time, you only need to 
  71.    --  directly create the appropriate Gtk Widget by calling the correct 
  72.    --  Gtk_New procedure. 
  73.  
  74.    procedure Initialize (Object : access GObject_Record'Class); 
  75.    --  Internal initialization function. 
  76.    --  See the section "Creating your own widgets" in the documentation. 
  77.  
  78.    procedure Ref (Object : access GObject_Record); 
  79.    --  Increment the reference counter for Object. See Unref below. 
  80.    --  Since an object is not deleted while its reference count is not null, 
  81.    --  this is a way to keep an object in memory, in particular when you 
  82.    --  want to temporarily remove a widget from its parent. 
  83.  
  84.    procedure Unref (Object : access GObject_Record); 
  85.    --  Decrement the reference counter for Object. When this reaches 0, the 
  86.    --  object is effectively destroy, all the callbacks associated with it are 
  87.    --  disconnected. 
  88.  
  89.    type Weak_Notify is access procedure 
  90.      (Data                 : System.Address; 
  91.       Where_The_Object_Was : System.Address); 
  92.    pragma Convention (C, Weak_Notify); 
  93.    --  Called when Where_The_Object_Was is destroyed (although you can still 
  94.    --  use this to reset it). Data is the argument passed to Weak_Ref. 
  95.    --  You should destroy and free the memory occupied by Data 
  96.  
  97.    procedure Weak_Ref 
  98.      (Object : access GObject_Record'Class; 
  99.       Notify : Weak_Notify; 
  100.       Data   : System.Address := System.Null_Address); 
  101.    --  This kind of reference doesn't increment the object's reference 
  102.    --  counting. However, it can and should be used to monitor the object's 
  103.    --  life cycle, in particular to detect is destruction. 
  104.    --  When Object is destroyed, calls Notify 
  105.  
  106.    procedure Weak_Unref 
  107.      (Object : access GObject_Record'Class; 
  108.       Notify : Weak_Notify; 
  109.       Data   : System.Address := System.Null_Address); 
  110.    --  Cancels the settings of Weak_Ref. 
  111.  
  112.    procedure Deallocate (Object : access GObject_Record); 
  113.    --  This operation is used to deallocate Object. 
  114.    --  The default implementation assumes that the value passed in is an 
  115.    --  access value created by an allocator of the default pool, i.e. it 
  116.    --  will assume that an instance of 
  117.    --  Unchecked_Deallocation (GObject_Record'Class, GObject) 
  118.    --  can be used to deallocate the designated object. 
  119.    --  Types derived of GObject_Record can override this operation in order 
  120.    --  to cope with objects allocated on other pools or even objects allocated 
  121.    --  on the stack. 
  122.    --  This design is limited to support only one allocation strategy for each 
  123.    --  class, as the class tag is used to identify the applicable strategy. 
  124.  
  125.    ------------------------ 
  126.    -- Interfacing with C -- 
  127.    ------------------------ 
  128.    --  The following functions are made public so that one can easily create 
  129.    --  new objects outside the Glib or Gtk package hierarchy. 
  130.    --  Only experienced users should make use of these functions. 
  131.  
  132.    function Get_Object 
  133.      (Object : access GObject_Record'Class) return System.Address; 
  134.    --  Access the underlying C pointer. 
  135.  
  136.    procedure Set_Object 
  137.      (Object : access GObject_Record'Class; 
  138.       Value  : System.Address); 
  139.    --  Modify the underlying C pointer. 
  140.  
  141.    function Get_User_Data 
  142.      (Obj  : System.Address; 
  143.       Stub : GObject_Record'Class) return GObject; 
  144.    --  Return the Ada object matching the C object Obj. If Obj was created 
  145.    --  explicitely from GtkAda, this will be the exact same widget. If Obj was 
  146.    --  created implicitely by gtk+ (buttons in complex windows,...), a new Ada 
  147.    --  object of type Stub will be created. 
  148.  
  149.    function Get_User_Data_Fast 
  150.      (Obj  : System.Address; 
  151.       Stub : GObject_Record'Class) return GObject; 
  152.    --  Same as Get_User_Data, but does not try to guess the type of Obj, 
  153.    --  always default to Stub if Obj is unknown to GtkAda. 
  154.  
  155.    function Unchecked_Cast 
  156.      (Obj  : access GObject_Record'Class; 
  157.       Stub : GObject_Record'Class) return GObject; 
  158.    --  Cast Obj in an object of tag Stub'Class. 
  159.    --  Return the resulting object and free the memory pointed by Obj. 
  160.  
  161.    ------------- 
  162.    -- Signals -- 
  163.    ------------- 
  164.    --  Any child of GObject can be associated with any number of signals. The 
  165.    --  mechanism for signals is fully generic, and any number of arguments can 
  166.    --  be associated with signals. 
  167.    --  See the function Initialize_Class_Record for more information on how 
  168.    --  to create new signals for your own new widgets. 
  169.    --  The subprograms below are provided for introspection: they make it 
  170.    --  possible to query the list of signals defined for a specific widget, 
  171.    --  as well as their parameters and return types. 
  172.  
  173.    type Signal_Id_Array is array (Guint range <>) of Glib.Signal_Id; 
  174.  
  175.    type Signal_Query is private; 
  176.  
  177.    function Lookup 
  178.      (Object : Glib.GType; Signal : String) return Glib.Signal_Id; 
  179.    --  Returns the signal Id associated with a specific Object/Signal pair. 
  180.    --  Null_Signal_Id is returned if no such signal exists for Object. 
  181.    --  You can then use the Query procedure to get more information on the 
  182.    --  signal. 
  183.  
  184.    function List_Ids (Typ : Glib.GType) return Signal_Id_Array; 
  185.    --  Return the list of signals defined for Typ. You can get more information 
  186.    --  on each of this signals by using the Query function below. 
  187.    --  See also the function Get_Type above to convert from an object instance 
  188.    --  to its type. Using a GType as the parameter makes it easier to find the 
  189.    --  signals for a widget and its ancestors (using Glib.Parent). 
  190.  
  191.    procedure Query (Id : Glib.Signal_Id; Result : out Signal_Query); 
  192.    --  Return the description associated with the signal Id. You can get the 
  193.    --  various fields from Query with one of the functions below. 
  194.    --  Result is undefined if Id is Invalid_Signal_Id or Null_Signal_Id 
  195.  
  196.    function Id (Q : Signal_Query) return Glib.Signal_Id; 
  197.    --  Return the signal Id. Each Id is specific to a widget/signal name pair. 
  198.    --  These Ids can then be used to temporarily block a signal for instance, 
  199.    --  through the subprograms in Gtk.Handlers. 
  200.  
  201.    function Signal_Name (Q : Signal_Query) return Glib.Signal_Name; 
  202.    --  Return the name of the signal, as should be used in a call to Connect. 
  203.  
  204.    function Return_Type (Q : Signal_Query) return Glib.GType; 
  205.    --  Return the type of object returned by the handlers for this signal. 
  206.  
  207.    function Params (Q : Signal_Query) return GType_Array; 
  208.    --  Return the list of parameters for the handlers for this signal 
  209.  
  210.    -------------------------- 
  211.    -- Creating new widgets -- 
  212.    -------------------------- 
  213.    --  These types and functions are used only when creating new widget types 
  214.    --  directly in Ada. These functions initialize the classes so that they are 
  215.    --  correctly recognized by gtk+ itself 
  216.    --  See the GtkAda user's guide for more information on how to create your 
  217.    --  own widget types in Ada. 
  218.  
  219.    type Interface_Vtable is private; 
  220.    --  The virtual table of an interface (see Glib.Types). This is only useful 
  221.    --  when doing introspection. 
  222.  
  223.    type GObject_Class is new GType_Class; 
  224.    Uninitialized_Class : constant GObject_Class; 
  225.    --  This type encloses all the informations related to a specific type of 
  226.    --  object or widget. All instances of such an object have a pointer to this 
  227.    --  structure, that includes the definition of all the signals that exist 
  228.    --  for a given object, all its properties,... 
  229.  
  230.    type Signal_Parameter_Types is 
  231.      array (Natural range <>, Natural range <>) of GType; 
  232.    --  The description of the parameters for each event. These are the 
  233.    --  parameters that the application must provide when emitting the 
  234.    --  signal. The user can of course add his own parameters when connecting 
  235.    --  the signal in his application, through the use of 
  236.    --  Gtk.Handlers.User_Callback. 
  237.    -- 
  238.    --  Each event defined with Initialize_Class_Record below should have an 
  239.    --  entry in this table. If Gtk_Type_None is found in the table, it is 
  240.    --  ignored. For instance, a Signal_Parameter_Type like: 
  241.    --    (1 => (1 => Gdk_Type_Gdk_Event, 2 => GType_None), 
  242.    --     2 => (1 => GType_Int,          2 => GType_Int)); 
  243.    --  defines two signals, the first with a single Gdk_Event parameter, the 
  244.    --  second with two ints parameters. 
  245.  
  246.    Null_Parameter_Types : constant Signal_Parameter_Types (1 .. 0, 1 .. 0) := 
  247.      (others => (others => GType_None)); 
  248.    --  An empty array, used as a default parameter in Initialize_Class_Record. 
  249.  
  250.    procedure Initialize_Class_Record 
  251.      (Object       : access GObject_Record'Class; 
  252.       Signals      : Gtkada.Types.Chars_Ptr_Array; 
  253.       Class_Record : in out GObject_Class; 
  254.       Type_Name    : String; 
  255.       Parameters   : Signal_Parameter_Types := Null_Parameter_Types); 
  256.    --  Create the class record for a new object type. 
  257.    --  It is associated with Signals'Length new signals. A pointer to the 
  258.    --  newly created structure is also returned in Class_Record. 
  259.    --  If Class_Record /= System.Null_Address, no memory allocation is 
  260.    --  performed, we just reuse it. As a result, each instantiation of an 
  261.    --  object will share the same GObject_Class, exactly as is done for gtk+. 
  262.    -- 
  263.    --  Note: The underlying C object must already have been initialized 
  264.    --  by a call to its parent's Initialize function. 
  265.    --  Parameters'Length should be the same as Signals'Length, or the result 
  266.    --  is undefined. 
  267.    --  As a special case, if Parameters has its default value, all signals are 
  268.    --  created with no argument. This is done for backward compatibility 
  269.    --  mainly, and you should instead give it an explicit value. 
  270.    --  Type_Name should be a unique name identifying the name of the new type. 
  271.    -- 
  272.    --  Only the signals with no parameter can be connected from C code. 
  273.    --  However, any signal can be connected from Ada. This is due to the way 
  274.    --  we define default marshallers for the signals. 
  275.  
  276.    function Type_From_Class (Class_Record : GObject_Class) return GType; 
  277.    --  Return the internal gtk+ type that describes the newly created 
  278.    --  Class_Record. 
  279.    --  See the function Glib.Types.Class_Peek for the opposite function 
  280.    --  converting from a GType to a GObject_Class. 
  281.  
  282.    ------------------------------ 
  283.    -- Properties introspection -- 
  284.    ------------------------------ 
  285.    --  See glib.ads for more information on properties 
  286.  
  287.    function Interface_List_Properties 
  288.      (Vtable : Interface_Vtable) return Glib.Param_Spec_Array; 
  289.    --  Return the list of properties of an interface (see also Glib.Properties) 
  290.    --  from a Vtable from Default_Interface_Peek). 
  291.    --  See also Class_List_Properties for a similar function for objects. 
  292.  
  293.    function Class_List_Properties 
  294.      (Class : GObject_Class) return Glib.Param_Spec_Array; 
  295.    --  Return the list of all properties of the class. 
  296.  
  297.    ------------- 
  298.    -- Signals -- 
  299.    ------------- 
  300.    --  ??? This section is incomplete. 
  301.  
  302.    --  <signals> 
  303.    --  The following new signals are defined for this object: 
  304.    -- 
  305.    --  - "notify" 
  306.    --    procedure Handler 
  307.    --      (Object : access GObject_Record'Class; Name : String); 
  308.    -- 
  309.    --    Emitted when the property Name has been modified 
  310.    --  </signals> 
  311.  
  312.    procedure Notify 
  313.      (Object        : access GObject_Record; 
  314.       Property_Name : String); 
  315.    --  Emits the "notify" signal, to signal every listener that the property 
  316.    --  has been changed. 
  317.  
  318.    --------------- 
  319.    -- User_Data -- 
  320.    --------------- 
  321.    --  This package allow you to associate your own Data to the C widgets. No 
  322.    --  type verification is made to check if you are using the correct 
  323.    --  matching Get function. This is your own responsability. 
  324.    -- 
  325.    --  We recommend using this package only if you want your data to be 
  326.    --  available from your own C code. If you just want to access it from Ada, 
  327.    --  you should consider creating a new tagged type instead, that extends 
  328.    --  either GObject_Record or the specific widget type you need. 
  329.  
  330.    --  <doc_ignore> 
  331.  
  332.    generic 
  333.       type Data_Type (<>) is private; 
  334.    package User_Data is 
  335.       type On_Destroyed_Callback is access procedure (Data : Data_Type); 
  336.       --  On_Destroyed is called when the data is overriden in the object, by 
  337.       --  an other object with the same ID, or when the object itself is 
  338.       --  destroyed 
  339.  
  340.       function Get 
  341.         (Object : access GObject_Record'Class; 
  342.          Id     : String := "user_data") return Data_Type; 
  343.       --  Get the information associated with the key ID. 
  344.       --  Raise Gtkada.Types.Data_Error if there is none. 
  345.  
  346.       function Get 
  347.         (Object  : access GObject_Record'Class; 
  348.          Id      : String := "user_data"; 
  349.          Default : Data_Type) return Data_Type; 
  350.       --  Get the information associated with the key ID. 
  351.       --  Return Default instead of raising an exception if there is no such 
  352.       --  user data 
  353.  
  354.       procedure Set 
  355.         (Object : access GObject_Record'Class; 
  356.          Data   : Data_Type; 
  357.          Id     : String := "user_data"; 
  358.          On_Destroyed : On_Destroyed_Callback := null); 
  359.       --  Associate some new user data with the object. 
  360.       --  The strings starting with "gtkada_" are reserved for GtkAda's 
  361.       --  internal use, please avoid using them. 
  362.  
  363.       procedure Remove 
  364.         (Object : access GObject_Record'Class; Id : String := "user_data"); 
  365.       --  Remove some data from the object 
  366.  
  367.       function Get 
  368.         (Object : access GObject_Record'Class; 
  369.          Id     : Glib.GQuark) return Data_Type; 
  370.       function Get 
  371.         (Object  : access GObject_Record'Class; 
  372.          Id      : Glib.GQuark; 
  373.          Default : Data_Type) return Data_Type; 
  374.       --  Same function as Get above, but uses directly the Quark associated 
  375.       --  with the string, which speeds up the access time significantly. 
  376.  
  377.       procedure Set 
  378.         (Object : access GObject_Record'Class; 
  379.          Data   : Data_Type; 
  380.          Id     : Glib.GQuark; 
  381.          On_Destroyed : On_Destroyed_Callback := null); 
  382.       --  Same function as Set above, but uses directly the Quark associated 
  383.       --  with the string, which speeds up the access time significantly. 
  384.  
  385.       procedure Remove 
  386.         (Object : access GObject_Record'Class; Id : Glib.GQuark); 
  387.       --  Same function as Remove above, but uses directly the Quark associated 
  388.       --  with the string, which speeds up the access time significantly. 
  389.  
  390.    private 
  391.       --  <doc_ignore> 
  392.       procedure Free_Data (Data : System.Address); 
  393.       --  Internal procedure used to free user data in the package body 
  394.       pragma Convention (C, Free_Data); 
  395.       --  </doc_ignore> 
  396.    end User_Data; 
  397.  
  398.    --  </doc_ignore> 
  399.  
  400.    ----------- 
  401.    -- Lists -- 
  402.    ----------- 
  403.  
  404.    function Convert (W : GObject) return System.Address; 
  405.    function Convert (W : System.Address) return GObject; 
  406.  
  407.    package Object_List is new Glib.GSlist.Generic_SList (GObject); 
  408.    package Object_Simple_List is new Glib.Glist.Generic_List (GObject); 
  409.  
  410. private 
  411.  
  412.    type GObject_Record is tagged record 
  413.       Ptr : System.Address := System.Null_Address; 
  414.    end record; 
  415.  
  416.    type Interface_Vtable is new Glib.C_Proxy; 
  417.  
  418.    Uninitialized_Class : constant GObject_Class := 
  419.      GObject_Class (System.Null_Address); 
  420.  
  421.    type Signal_Query is record 
  422.       Signal_Id    : Guint; 
  423.       Signal_Name  : System.Address;  --  const gchar* 
  424.       IType        : GType; 
  425.       Signal_Flags : Gint;            --  enum GSignalFlags 
  426.       Return_Type  : GType; 
  427.       N_Params     : Guint; 
  428.       Param_Types  : System.Address;  --  const gtype* 
  429.    end record; 
  430.    pragma Convention (C, Signal_Query); 
  431.  
  432.    --  <doc_ignore> 
  433.  
  434.    --  Note: the following functions and types should only be used 
  435.    --  for internal usage, not in the user's applications. 
  436.    --  If you use type inheritance for new widgets, you should not need 
  437.    --  these functions. 
  438.  
  439.    GtkAda_String : constant String := "_GtkAda" & ASCII.NUL; 
  440.    GtkAda_String_Quark : Glib.GQuark := Glib.Unknown_Quark; 
  441.    --  The name for the user data that we set in the objects. 
  442.    --  The Quark version is to speed up the string lookup (this is done 
  443.    --  only once). 
  444.  
  445.    --  </doc_ignore> 
  446.  
  447.    pragma Inline (Get_Object); 
  448.    pragma Inline (Set_Object); 
  449.    pragma Import (C, Type_From_Class, "ada_type_from_class"); 
  450.    pragma Import (C, Query, "g_signal_query"); 
  451.    pragma Import (C, Id, "ada_gsignal_query_id"); 
  452.    pragma Import (C, Return_Type, "ada_gsignal_query_return_type"); 
  453. end Glib.Object;