/src/wrappers/gtk/library/gtk_object.e

http://github.com/tybor/Liberty · Specman e · 170 lines · 60 code · 31 blank · 79 comment · 2 complexity · 8579b7259aa8a3a1dfb2f922f90ca190 MD5 · raw file

  1. indexing
  2. copyright: "[
  3. Copyright (C) 2006 eiffel-libraries team, GTK+ team
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public License
  6. as published by the Free Software Foundation; either version 2.1 of
  7. the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA
  16. ]"
  17. date: "$Date:$"
  18. revision "$REvision:$"
  19. deferred class GTK_OBJECT
  20. -- GtkObject is the base class for all widgets, and for a few
  21. -- non-widget objects such as GtkAdjustment. GtkObject predates
  22. -- GObject; non-widgets that derive from GtkObject rather than
  23. -- GObject do so for backward compatibility reasons.
  24. -- The most interesting difference between GtkObject and GObject is
  25. -- the "floating" reference count. A GObject is created with a
  26. -- reference count of 1, owned by the creator of the GObject. (The
  27. -- owner of a reference is the code section that has the right to
  28. -- call g_object_unref() in order to remove that reference.) A
  29. -- GtkObject is created with a reference count of 1 also, but it
  30. -- isn't owned by anyone; calling g_object_unref() on the
  31. -- newly-created GtkObject is incorrect. Instead, the initial
  32. -- reference count of a GtkObject is "floating". The floating
  33. -- reference can be removed by anyone at any time, by calling
  34. -- gtk_object_sink(). gtk_object_sink() does nothing if an object
  35. -- is already sunk (has no floating reference).
  36. -- When you add a widget to its parent container, the parent
  37. -- container will do this:
  38. -- child_widget.ref; child_widget.sink
  39. -- This means that the container now owns a reference to the child
  40. -- widget (since it called `ref'), and the child widget has no
  41. -- floating reference.
  42. -- The purpose of the floating reference is to keep the child
  43. -- widget alive until you add it to a parent container:
  44. -- create button.make -- button has one floating reference to keep
  45. -- it alive container.add(button) -- button has one non-floating
  46. -- reference owned by the container
  47. -- GtkWindow is a special case, because GTK+ itself will ref/sink
  48. -- it on creation. That is, after calling `make,' the GtkWindow
  49. -- will have one reference which is owned by GTK+, and no floating
  50. -- references.
  51. -- One more factor comes into play: the "destroy" signal, emitted
  52. -- by the `destroy' method. The "destroy" signal asks all code
  53. -- owning a reference to an object to release said reference. So,
  54. -- for example, if you call `destroy' on a GtkWindow, GTK+ will
  55. -- release the reference count that it owns; if you call `destroy'
  56. -- on a GtkButton, then the button will be removed from its parent
  57. -- container and the parent container will release its reference to
  58. -- the button. Because these references are released, calling
  59. -- `destroy' should result in freeing all memory associated with an
  60. -- object, unless some buggy code fails to release its references
  61. -- in response to the "destroy" signal. Freeing memory (referred to
  62. -- as finalization only happens if the reference count reaches
  63. -- zero.
  64. -- Some simple rules for handling GtkObject:
  65. -- * Never call `unref' unless you have previously called `ref'
  66. -- even if you created the GtkObject. (Note: this is not true for
  67. -- GObject; for GObject, the creator of the object owns a
  68. -- reference.)
  69. -- * Call `destroy' to get rid of most objects in most cases. In
  70. -- particular, widgets are almost always destroyed in this way.
  71. -- * Because of the floating reference count, you don't need to
  72. -- worry about reference counting for widgets and toplevel
  73. -- windows, unless you explicitly call `ref' yourself.
  74. inherit
  75. G_OBJECT
  76. -- undefine make
  77. redefine store_eiffel_wrapper
  78. end
  79. insert
  80. GTK -- that provides the gtk singleton.
  81. GTK_OBJECT_EXTERNALS
  82. feature
  83. sink is
  84. -- Removes the floating reference from a GtkObject, if it exists;
  85. -- otherwise does nothing. See the GtkObject overview documentation at
  86. -- the top of the page.
  87. do
  88. gtk_object_sink (handle)
  89. end
  90. destroy is
  91. -- Emits the "destroy" signal notifying all reference holders that they
  92. -- should release the GtkObject. See the overview documentation at the
  93. -- top of the page for more details. The memory for the object itself
  94. -- won't be deleted until its reference count actually drops to 0;
  95. -- destroy merely asks reference holders to release their references,
  96. -- it does not free the object.
  97. do
  98. gtk_object_destroy (handle)
  99. end
  100. store_eiffel_wrapper is
  101. do
  102. Precursor
  103. ref -- This takes care of sinking the object and/or adding a reference
  104. end
  105. feature -- Signals
  106. -- The "destroy" signal
  107. -- void user_function (GtkObject *object, gpointer user_data);
  108. -- Signals that all holders of a reference to the GtkObject should
  109. -- release the reference that they hold. May result in finalization
  110. -- of the object if all references are released.
  111. -- object : the object which received the signal.
  112. -- user_data : user data set when the signal handler was connected.
  113. connect_to_destroy_signal,
  114. connect_agent_to_destroy_signal (a_procedure: PROCEDURE[TUPLE[GTK_OBJECT]]) is
  115. -- Connect `a_procedure' but invokes the fixed
  116. -- `destroy_callback' special feature."
  117. local destroy_callback: DESTROY_CALLBACK
  118. do
  119. create destroy_callback.make
  120. destroy_callback.connect (Current, a_procedure)
  121. -- The above is just a shorter version of
  122. -- create destroy_callback.make
  123. -- destroy_callback.connect (Current, a_procedure)
  124. end
  125. -- TODO: implement a enable_on_destroy and on_destroy
  126. enable_on_destroy is
  127. -- Connects "destroy" signal to `on_destroy' feature.
  128. do
  129. connect (Current, destroy_signal_name, $on_destroy)
  130. end
  131. on_destroy is
  132. -- Called on destroy signals. Redefine it in your heir classes
  133. do
  134. end
  135. feature {} -- Signal names
  136. destroy_signal_name: STRING is "destroy"
  137. invariant
  138. gtk_initialized: gtk.is_initialized
  139. end