1. ------------------------------------------------------------------------------ 
  2. --                                                                          -- 
  3. --      Copyright (C) 1998-2000 E. Briot, J. Brobecker and A. Charlet       -- 
  4. --                     Copyright (C) 2000-2014, AdaCore                     -- 
  5. --                                                                          -- 
  6. -- This library is free software;  you can redistribute it and/or modify it -- 
  7. -- under terms of the  GNU General Public License  as published by the Free -- 
  8. -- Software  Foundation;  either version 3,  or (at your  option) any later -- 
  9. -- version. This library is distributed in the hope that it will be useful, -- 
  10. -- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- -- 
  11. -- TABILITY or FITNESS FOR A PARTICULAR PURPOSE.                            -- 
  12. --                                                                          -- 
  13. -- As a special exception under Section 7 of GPL version 3, you are granted -- 
  14. -- additional permissions described in the GCC Runtime Library Exception,   -- 
  15. -- version 3.1, as published by the Free Software Foundation.               -- 
  16. --                                                                          -- 
  17. -- You should have received a copy of the GNU General Public License and    -- 
  18. -- a copy of the GCC Runtime Library Exception along with this program;     -- 
  19. -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    -- 
  20. -- <http://www.gnu.org/licenses/>.                                          -- 
  21. --                                                                          -- 
  22. ------------------------------------------------------------------------------ 
  23.  
  24. --  <description> 
  25. --  Activatable widgets can be connected to a Gtk.Action.Gtk_Action and 
  26. --  reflects the state of its action. A Gtk.Activatable.Gtk_Activatable can 
  27. --  also provide feedback through its action, as they are responsible for 
  28. --  activating their related actions. 
  29. -- 
  30. --  == Implementing GtkActivatable == 
  31. -- 
  32. --  When extending a class that is already Gtk.Activatable.Gtk_Activatable; it 
  33. --  is only necessary to implement the 
  34. --  Gtk.Activatable.Gtk_Activatable->sync_action_properties and 
  35. --  Gtk.Activatable.Gtk_Activatable->update methods and chain up to the parent 
  36. --  implementation, however when introducing a new 
  37. --  Gtk.Activatable.Gtk_Activatable class; the 
  38. --  Gtk.Activatable.Gtk_Activatable:related-action and 
  39. --  Gtk.Activatable.Gtk_Activatable:use-action-appearance properties need to be 
  40. --  handled by the implementor. Handling these properties is mostly a matter of 
  41. --  installing the action pointer and boolean flag on your instance, and 
  42. --  calling Gtk.Activatable.Do_Set_Related_Action and 
  43. --  Gtk.Activatable.Sync_Action_Properties at the appropriate times. 
  44. -- 
  45. --  == A class fragment implementing Gtk.Activatable.Gtk_Activatable == 
  46. -- 
  47. --    enum { 
  48. --       ... 
  49. --       PROP_ACTIVATABLE_RELATED_ACTION, 
  50. --       PROP_ACTIVATABLE_USE_ACTION_APPEARANCE 
  51. --    } 
  52. --    struct _FooBarPrivate 
  53. --    { 
  54. --       ... 
  55. --       GtkAction      *action; 
  56. --       gboolean        use_action_appearance; 
  57. --    }; 
  58. --    ... 
  59. --    static void foo_bar_activatable_interface_init         (GtkActivatableIface  *iface); 
  60. --    static void foo_bar_activatable_update                 (GtkActivatable       *activatable, 
  61. --       GtkAction            *action, 
  62. --       const gchar          *property_name); 
  63. --    static void foo_bar_activatable_sync_action_properties (GtkActivatable       *activatable, 
  64. --       GtkAction            *action); 
  65. --    ... 
  66. --    static void 
  67. --    foo_bar_class_init (FooBarClass *klass) 
  68. --    { 
  69. --       ... 
  70. --       g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action"); 
  71. --       g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance"); 
  72. --       ... 
  73. --    } 
  74. --    static void 
  75. --    foo_bar_activatable_interface_init (GtkActivatableIface  *iface) 
  76. --    { 
  77. --       iface->update = foo_bar_activatable_update; 
  78. --       iface->sync_action_properties = foo_bar_activatable_sync_action_properties; 
  79. --    } 
  80. --    ... Break the reference using Gtk.Activatable.Do_Set_Related_Action... 
  81. --    static void 
  82. --    foo_bar_dispose (GObject *object) 
  83. --    { 
  84. --       FooBar *bar = FOO_BAR (object); 
  85. --       FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar); 
  86. --       ... 
  87. --       if (priv->action) 
  88. --       { 
  89. --          gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (bar), NULL); 
  90. --          priv->action = NULL; 
  91. --       } 
  92. --       G_OBJECT_CLASS (foo_bar_parent_class)->dispose (object); 
  93. --    } 
  94. --    ... Handle the "related-action" and "use-action-appearance" properties ... 
  95. --    static void 
  96. --    foo_bar_set_property (GObject         *object, 
  97. --       guint            prop_id, 
  98. --       const GValue    *value, 
  99. --       GParamSpec      *pspec) 
  100. --    { 
  101. --       FooBar *bar = FOO_BAR (object); 
  102. --       FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar); 
  103. --       switch (prop_id) 
  104. --       { 
  105. --          ... 
  106. --          case PROP_ACTIVATABLE_RELATED_ACTION: 
  107. --          foo_bar_set_related_action (bar, g_value_get_object (value)); 
  108. --          break; 
  109. --          case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE: 
  110. --          foo_bar_set_use_action_appearance (bar, g_value_get_boolean (value)); 
  111. --          break; 
  112. --          default: 
  113. --          G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); 
  114. --          break; 
  115. --       } 
  116. --    } 
  117. --    static void 
  118. --    foo_bar_get_property (GObject         *object, 
  119. --       guint            prop_id, 
  120. --       GValue          *value, 
  121. --       GParamSpec      *pspec) 
  122. --    { 
  123. --       FooBar *bar = FOO_BAR (object); 
  124. --       FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar); 
  125. --       switch (prop_id) 
  126. --       { 
  127. --          ... 
  128. --          case PROP_ACTIVATABLE_RELATED_ACTION: 
  129. --          g_value_set_object (value, priv->action); 
  130. --          break; 
  131. --          case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE: 
  132. --          g_value_set_boolean (value, priv->use_action_appearance); 
  133. --          break; 
  134. --          default: 
  135. --          G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); 
  136. --          break; 
  137. --       } 
  138. --    } 
  139. --    static void 
  140. --    foo_bar_set_use_action_appearance (FooBar   *bar, 
  141. --       gboolean  use_appearance) 
  142. --    { 
  143. --       FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar); 
  144. --       if (priv->use_action_appearance != use_appearance) 
  145. --       { 
  146. --          priv->use_action_appearance = use_appearance; 
  147. --          gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (bar), priv->action); 
  148. --       } 
  149. --    } 
  150. --    ... call Gtk.Activatable.Do_Set_Related_Action and then assign the action pointer, 
  151. --    no need to reference the action here since Gtk.Activatable.Do_Set_Related_Action already 
  152. --    holds a reference here for you... 
  153. --    static void 
  154. --    foo_bar_set_related_action (FooBar    *bar, 
  155. --       GtkAction *action) 
  156. --    { 
  157. --       FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar); 
  158. --       if (priv->action == action) 
  159. --       return; 
  160. --       gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (bar), action); 
  161. --       priv->action = action; 
  162. --    } 
  163. --    ... Selectively reset and update activatable depending on the use-action-appearance property ... 
  164. --    static void 
  165. --    gtk_button_activatable_sync_action_properties (GtkActivatable       *activatable, 
  166. --       GtkAction            *action) 
  167. --    { 
  168. --       GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable); 
  169. --       if (!action) 
  170. --       return; 
  171. --       if (gtk_action_is_visible (action)) 
  172. --       gtk_widget_show (GTK_WIDGET (activatable)); 
  173. --    else 
  174. --       gtk_widget_hide (GTK_WIDGET (activatable)); 
  175. --       gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action)); 
  176. --       ... 
  177. --       if (priv->use_action_appearance) 
  178. --       { 
  179. --          if (gtk_action_get_stock_id (action)) 
  180. --          foo_bar_set_stock (button, gtk_action_get_stock_id (action)); 
  181. --       else if (gtk_action_get_label (action)) 
  182. --       foo_bar_set_label (button, gtk_action_get_label (action)); 
  183. --       ... 
  184. --    } 
  185. -- } 
  186. -- static void 
  187. -- foo_bar_activatable_update (GtkActivatable       *activatable, 
  188. --    GtkAction            *action, 
  189. --    const gchar          *property_name) 
  190. -- { 
  191. --    FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (activatable); 
  192. --    if (strcmp (property_name, "visible") == 0) 
  193. --    { 
  194. --       if (gtk_action_is_visible (action)) 
  195. --       gtk_widget_show (GTK_WIDGET (activatable)); 
  196. --    else 
  197. --       gtk_widget_hide (GTK_WIDGET (activatable)); 
  198. --    } 
  199. -- else if (strcmp (property_name, "sensitive") == 0) 
  200. -- gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action)); 
  201. -- ... 
  202. -- if (!priv->use_action_appearance) 
  203. -- return; 
  204. -- if (strcmp (property_name, "stock-id") == 0) 
  205. -- foo_bar_set_stock (button, gtk_action_get_stock_id (action)); 
  206. -- else if (strcmp (property_name, "label") == 0) 
  207. -- foo_bar_set_label (button, gtk_action_get_label (action)); 
  208. -- ... 
  209. -- } 
  210. -- 
  211. -- 
  212. --  </description> 
  213. pragma Ada_2005; 
  214.  
  215. pragma Warnings (Off, "*is already use-visible*"); 
  216. with Glib;            use Glib; 
  217. with Glib.Properties; use Glib.Properties; 
  218. with Glib.Types;      use Glib.Types; 
  219. with Gtk.Action;      use Gtk.Action; 
  220.  
  221. package Gtk.Activatable is 
  222.  
  223.    type Gtk_Activatable is new Glib.Types.GType_Interface; 
  224.    Null_Gtk_Activatable : constant Gtk_Activatable; 
  225.  
  226.    ------------------ 
  227.    -- Constructors -- 
  228.    ------------------ 
  229.  
  230.    function Get_Type return Glib.GType; 
  231.    pragma Import (C, Get_Type, "gtk_activatable_get_type"); 
  232.  
  233.    ------------- 
  234.    -- Methods -- 
  235.    ------------- 
  236.  
  237.    procedure Do_Set_Related_Action 
  238.       (Self   : Gtk_Activatable; 
  239.        Action : not null access Gtk.Action.Gtk_Action_Record'Class); 
  240.    --  This is a utility function for Gtk.Activatable.Gtk_Activatable 
  241.    --  implementors. 
  242.    --  When implementing Gtk.Activatable.Gtk_Activatable you must call this 
  243.    --  when handling changes of the 
  244.    --  Gtk.Activatable.Gtk_Activatable:related-action, and you must also use 
  245.    --  this to break references in Glib.Object.GObject->dispose. 
  246.    --  This function adds a reference to the currently set related action for 
  247.    --  you, it also makes sure the Gtk.Activatable.Gtk_Activatable->update 
  248.    --  method is called when the related Gtk.Action.Gtk_Action properties 
  249.    --  change and registers to the action's proxy list. 
  250.    --  Note: 
  251.    --  Be careful to call this before setting the local copy of the 
  252.    --  Gtk.Action.Gtk_Action property, since this function uses 
  253.    --  Gtk.Activatable.Get_Related_Action to retrieve the previous action 
  254.    --  Since: gtk+ 2.16 
  255.    --  "action": the Gtk.Action.Gtk_Action to set 
  256.  
  257.    function Get_Related_Action 
  258.       (Self : Gtk_Activatable) return Gtk.Action.Gtk_Action; 
  259.    --  Gets the related Gtk.Action.Gtk_Action for Activatable. 
  260.    --  Since: gtk+ 2.16 
  261.  
  262.    procedure Set_Related_Action 
  263.       (Self   : Gtk_Activatable; 
  264.        Action : not null access Gtk.Action.Gtk_Action_Record'Class); 
  265.    --  Sets the related action on the Activatable object. 
  266.    --  Note: 
  267.    --  Gtk.Activatable.Gtk_Activatable implementors need to handle the 
  268.    --  Gtk.Activatable.Gtk_Activatable:related-action property and call 
  269.    --  Gtk.Activatable.Do_Set_Related_Action when it changes. 
  270.    --  Since: gtk+ 2.16 
  271.    --  "action": the Gtk.Action.Gtk_Action to set 
  272.  
  273.    function Get_Use_Action_Appearance 
  274.       (Self : Gtk_Activatable) return Boolean; 
  275.    --  Gets whether this activatable should reset its layout and appearance 
  276.    --  when setting the related action or when the action changes appearance. 
  277.    --  Since: gtk+ 2.16 
  278.  
  279.    procedure Set_Use_Action_Appearance 
  280.       (Self           : Gtk_Activatable; 
  281.        Use_Appearance : Boolean); 
  282.    --  Sets whether this activatable should reset its layout and appearance 
  283.    --  when setting the related action or when the action changes appearance 
  284.    --  Note: 
  285.    --  Gtk.Activatable.Gtk_Activatable implementors need to handle the 
  286.    --  Gtk.Activatable.Gtk_Activatable:use-action-appearance property and call 
  287.    --  Gtk.Activatable.Sync_Action_Properties to update Activatable if needed. 
  288.    --  Since: gtk+ 2.16 
  289.    --  "use_appearance": whether to use the actions appearance 
  290.  
  291.    procedure Sync_Action_Properties 
  292.       (Self   : Gtk_Activatable; 
  293.        Action : access Gtk.Action.Gtk_Action_Record'Class); 
  294.    --  This is called to update the activatable completely, this is called 
  295.    --  internally when the Gtk.Activatable.Gtk_Activatable:related-action 
  296.    --  property is set or unset and by the implementing class when 
  297.    --  Gtk.Activatable.Gtk_Activatable:use-action-appearance changes. 
  298.    --  Since: gtk+ 2.16 
  299.    --  "action": the related Gtk.Action.Gtk_Action or null 
  300.  
  301.    ---------------- 
  302.    -- Properties -- 
  303.    ---------------- 
  304.    --  The following properties are defined for this widget. See 
  305.    --  Glib.Properties for more information on properties) 
  306.  
  307.    Related_Action_Property : constant Glib.Properties.Property_Object; 
  308.    --  Type: Gtk.Action.Gtk_Action 
  309.    --  The action that this activatable will activate and receive updates from 
  310.    --  for various states and possibly appearance. 
  311.    -- 
  312.    --  Note: 
  313.    -- 
  314.    --  Gtk.Activatable.Gtk_Activatable implementors need to handle the this 
  315.    --  property and call Gtk.Activatable.Do_Set_Related_Action when it changes. 
  316.  
  317.    Use_Action_Appearance_Property : constant Glib.Properties.Property_Boolean; 
  318.    --  Whether this activatable should reset its layout and appearance when 
  319.    --  setting the related action or when the action changes appearance. 
  320.    -- 
  321.    --  See the Gtk.Action.Gtk_Action documentation directly to find which 
  322.    --  properties should be ignored by the Gtk.Activatable.Gtk_Activatable when 
  323.    --  this property is False. 
  324.    -- 
  325.    --  Note: 
  326.    -- 
  327.    --  Gtk.Activatable.Gtk_Activatable implementors need to handle this 
  328.    --  property and call Gtk.Activatable.Sync_Action_Properties on the 
  329.    --  activatable widget when it changes. 
  330.  
  331.    ---------------- 
  332.    -- Interfaces -- 
  333.    ---------------- 
  334.    --  This class implements several interfaces. See Glib.Types 
  335.    -- 
  336.    --  - "Gtk_Activatable" 
  337.  
  338.    function "+" (W : Gtk_Activatable) return Gtk_Activatable; 
  339.    pragma Inline ("+"); 
  340.  
  341. private 
  342.    Use_Action_Appearance_Property : constant Glib.Properties.Property_Boolean := 
  343.      Glib.Properties.Build ("use-action-appearance"); 
  344.    Related_Action_Property : constant Glib.Properties.Property_Object := 
  345.      Glib.Properties.Build ("related-action"); 
  346.  
  347. Null_Gtk_Activatable : constant Gtk_Activatable := 
  348.    Gtk_Activatable (Glib.Types.Null_Interface); 
  349. end Gtk.Activatable;