/src/wrappers/gtk/library/gtk_tree_iter.e

http://github.com/tybor/Liberty · Specman e · 276 lines · 190 code · 36 blank · 50 comment · 8 complexity · 327f77406b7dc9f240b58ca15f591c4a MD5 · raw file

  1. indexing
  2. description: "The GtkTreeIter is the primary structure for accessing a structure. Models are expected to put a unique integer in the stamp member, and put model-specific data in the three user_data members."
  3. copyright: "[
  4. Copyright (C) 2006 eiffel-libraries team, GTK+ team
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation; either version 2.1 of
  8. the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA
  17. ]"
  18. date: "$Date:$"
  19. revision: "$Revision:$"
  20. class GTK_TREE_ITER
  21. inherit
  22. C_STRUCT redefine allocate end
  23. insert
  24. GTK
  25. GTK_TREE_ITER_EXTERNALS rename set_stamp as set_stamp_internal end
  26. GTK_TREE_MODEL_EXTERNALS
  27. GLIB_MEMORY_ALLOCATION
  28. creation
  29. make,
  30. make_from_model, from_model,
  31. from_external_pointer,
  32. copy_from_pointer,
  33. as_children_of
  34. feature -- Creation
  35. make is
  36. require
  37. gtk_initialized: gtk.is_initialized
  38. do
  39. allocate
  40. end
  41. make_from_model, from_model (a_model: GTK_TREE_MODEL) is
  42. require
  43. gtk_initialized: gtk.is_initialized
  44. valid_model: a_model/=Void
  45. do
  46. allocate
  47. tree_model := a_model
  48. ensure
  49. handle.is_not_null
  50. attached_to_model
  51. end
  52. copy_from_pointer (a_ptr: POINTER) is
  53. require
  54. a_ptr.is_not_null
  55. do
  56. handle := gtk_tree_iter_copy (a_ptr)
  57. end
  58. as_children_of (a_parent: GTK_TREE_ITER) is
  59. -- Create an iterator pointing to the first child of
  60. -- `a_parent'.
  61. require
  62. parent_not_void: a_parent /= Void
  63. parent_has_children: a_parent.has_children
  64. do
  65. allocate
  66. tree_model := a_parent.tree_model
  67. is_valid := (gtk_tree_model_iter_children
  68. (tree_model.handle, handle,
  69. a_parent.handle).to_boolean)
  70. end
  71. feature
  72. is_valid: BOOLEAN
  73. -- Is last action made on Current successful, making it valid?
  74. start, first is
  75. -- Moves Current to the first iterator in the tree (the one
  76. -- at the path "0"). `is_valid' will be set to False if the
  77. -- tree is empty.
  78. require
  79. attached_to_model
  80. do
  81. is_valid:=(gtk_tree_model_get_iter_first (tree_model.handle, handle)).to_boolean
  82. end
  83. next is
  84. -- Points Current to the node following it at the current
  85. -- level. If there is no next position `is_valid' will be
  86. -- False and Current is set to be invalid.
  87. require
  88. attached_to_model
  89. do
  90. is_valid:=(gtk_tree_model_iter_next (tree_model.handle,handle)).to_boolean
  91. end
  92. to_children_of (a_parent: GTK_TREE_ITER) is
  93. -- Sets Current iterator to point to the first child of
  94. -- `tree_model'. If parent has no children `is_valid' will be
  95. -- False. Current will remain a valid node after this
  96. -- function has been called. If `a_parent' is Void returns
  97. -- the first node, equivalent to `first'
  98. require
  99. attached_to_model
  100. do
  101. if a_parent=Void then
  102. is_valid:=(gtk_tree_model_iter_children (tree_model.handle, handle, default_pointer)).to_boolean
  103. else
  104. is_valid:=(gtk_tree_model_iter_children (tree_model.handle, handle, a_parent.handle)).to_boolean
  105. end
  106. end
  107. has_children, has_child: BOOLEAN is
  108. -- Does Current iterator have children?
  109. require
  110. attached_to_model
  111. do
  112. Result := (gtk_tree_model_iter_has_child (tree_model.handle,
  113. handle)).to_boolean
  114. end
  115. n_children, children_count: INTEGER is
  116. -- Number of children that Current iter has. As a special case, if iter
  117. -- is NULL, then the number of toplevel nodes is returned. (Note:
  118. -- `n_children' feature name comes from C Api. `children_count' is
  119. -- another name of the same feature that follow Eiffel naming style)
  120. require
  121. attached_to_model
  122. do
  123. Result := gtk_tree_model_iter_n_children (tree_model.handle, handle)
  124. end
  125. toplevel_nodes_count: INTEGER is
  126. -- Number of toplevel nodes of `tree_model'
  127. require
  128. attached_to_model
  129. do
  130. Result := gtk_tree_model_iter_n_children (tree_model.handle,default_pointer)
  131. end
  132. to_nth_child_of (a_parent: like Current; an_index: INTEGER) is
  133. -- Sets Current to be the child of `a_parent', using `an_index'. The
  134. -- first index is 0. If n is too big, or parent has no children, iter
  135. -- is set to an invalid iterator and `is_valid' will be
  136. -- False. `a_parent' will remain a valid node after this function has
  137. -- been called. As a special case, if `a_parent' is Void, then the nth
  138. -- root node is set. `is_valid' is True, if `a_parent' has an nth
  139. -- child.
  140. -- Note: Is `to_nth_child' a better name than `to_nth_child_of'?
  141. require
  142. valid_index: an_index >= 0
  143. attached_to_model
  144. local
  145. parent_ptr: POINTER
  146. do
  147. if a_parent/=Void then parent_ptr := a_parent.handle end
  148. is_valid := (gtk_tree_model_iter_nth_child (tree_model.handle,handle,
  149. parent_ptr, an_index)).to_boolean
  150. end
  151. to_parent (a_child: like Current) is
  152. -- Sets Current to be the parent of `a_child'. If child is at the
  153. -- toplevel it doesn't have a parent, then iter is set to an invalid
  154. -- iterator and `is_valid' will be False. `a_child' will remain a valid
  155. -- node after this function has been called.
  156. require
  157. valid_child: a_child /= Void
  158. attached_to_model
  159. do
  160. is_valid := (gtk_tree_model_iter_parent (tree_model.handle,handle,
  161. a_child.handle)).to_boolean
  162. end
  163. to_string: STRING is
  164. -- a representation of the iter. This string is a ':' separated list of
  165. -- numbers. For example, "4:10:0:3" would be an acceptable return value
  166. -- for this string.
  167. require
  168. attached_to_model
  169. do
  170. create Result.from_external (gtk_tree_model_get_string_from_iter (tree_model.handle,handle))
  171. -- Note: gtk_tree_model_get_string_from_iter returns a newly-allocated
  172. -- string that Must be freed with g_free. As far as I know this means
  173. -- that Result shall be created without copying the string and letting
  174. -- the Garbage Collector free it.
  175. end
  176. attached_to_model: BOOLEAN is
  177. do
  178. Result := tree_model /= Void
  179. end
  180. feature {CALLBACK}
  181. attach_to (a_model: like tree_model) is
  182. require
  183. not attached_to_model
  184. a_model /= Void
  185. do
  186. tree_model := a_model
  187. ensure
  188. attached_to_model
  189. tree_model = a_model
  190. end
  191. feature -- struct size
  192. struct_size: INTEGER is
  193. external "C inline use <gtk/gtk.h>"
  194. alias "sizeof(GtkTreeIter)"
  195. end
  196. feature {}
  197. allocate is
  198. -- There is no malloc-like function in GTK to allocate iterators.
  199. -- Therefore, we allocate iterators using gtk_tree_iter_copy()
  200. require
  201. handle.is_null
  202. do
  203. handle := gtk_tree_iter_copy (dummy_iter)
  204. if handle.is_null then
  205. raise_exception (No_more_memory)
  206. end
  207. end
  208. dummy_iter: POINTER is
  209. once
  210. Result := calloc (1, struct_size)
  211. if Result.is_null then
  212. raise_exception (No_more_memory)
  213. end
  214. end
  215. feature
  216. dispose is
  217. do
  218. if handle.is_not_null then gtk_tree_iter_free (handle) end
  219. handle:= default_pointer
  220. end
  221. feature
  222. stamp: INTEGER is
  223. -- A unique stamp to catch invalid iterators
  224. do
  225. Result := get_stamp (handle)
  226. end
  227. set_stamp (a_stamp: INTEGER) is
  228. do
  229. set_stamp_internal (handle, a_stamp)
  230. end
  231. -- TODO: (if ever necessary) user_data[|2|3]
  232. -- gpointer user_data; Model specific data
  233. -- gpointer user_data2; Model specific data
  234. -- gpointer user_data3; Model specific data
  235. tree_model: GTK_TREE_MODEL
  236. -- Reference to the tree model.
  237. -- Note: It could have been just a POINTER but in this case
  238. -- we would not be sure that the model is still alive when
  239. -- the iterator tries to access it.
  240. end