/src/wrappers/gtk/library/gtk_list_store.e

http://github.com/tybor/Liberty · Specman e · 445 lines · 160 code · 72 blank · 213 comment · 2 complexity · 75107fa5b3fc7c1743cca2aff3ff8a89 MD5 · raw file

  1. indexing
  2. description: "GtkListStore -- A list-like data structure that can be used with the GtkTreeView."
  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. class GTK_LIST_STORE
  19. -- The GtkListStore object is a list model for use with a
  20. -- GtkTreeView widget. It implements the GtkTreeModel interface,
  21. -- and consequentialy, can use all of the methods available
  22. -- there. It also implements the GtkTreeSortable interface so it
  23. -- can be sorted by the view. Finally, it also implements the tree
  24. -- drag and drop interfaces.
  25. -- The GtkListStore can accept most GObject types as a column type,
  26. -- though it can't accept all custom types. Internally, it will
  27. -- keep a copy of data passed in (such as a string or a boxed
  28. -- pointer). Columns that accept GObjects are handled a little
  29. -- differently. The GtkListStore will keep a reference to the
  30. -- object instead of copying the value. As a result, if the object
  31. -- is modified, it is up to the application writer to call
  32. -- gtk_tree_model_row_changed to emit the "row_changed"
  33. -- signal. This most commonly affects lists with GdkPixbufs stored.
  34. -- Example 5. Creating a simple list store. TODO: Eiffelize this example
  35. -- enum { COLUMN_STRING, COLUMN_INT, COLUMN_BOOLEAN, N_COLUMNS };
  36. -- {
  37. -- GtkListStore *list_store;
  38. -- GtkTreePath *path;
  39. -- GtkTreeIter iter;
  40. -- gint i;
  41. -- list_store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING,
  42. -- G_TYPE_INT, G_TYPE_BOOLEAN);
  43. -- for (i = 0; i < 10; i++) {
  44. -- gchar *some_data;
  45. -- some_data = get_some_data (i);
  46. -- /* Add a new row to the model */
  47. -- gtk_list_store_append (list_store, &iter);
  48. -- gtk_list_store_set (list_store, &iter,
  49. -- COLUMN_STRING, some_data,
  50. -- COLUMN_INT, i,
  51. -- COLUMN_BOOLEAN, FALSE,
  52. -- -1);
  53. -- /* As the store will keep a copy of the string internally, we
  54. -- * free some_data.
  55. -- */
  56. -- g_free (some_data);
  57. -- }
  58. -- /* Modify a particular row */
  59. -- path = gtk_tree_path_new_from_string ("4");
  60. -- gtk_tree_model_get_iter (GTK_TREE_MODEL (list_store),
  61. -- &iter,
  62. -- path);
  63. -- gtk_tree_path_free (path);
  64. -- gtk_list_store_set (list_store, &iter,
  65. -- COLUMN_BOOLEAN, TRUE,
  66. -- -1);
  67. -- }
  68. -- Performance Considerations: Internally, the GtkListStore was
  69. -- implemented with a linked list with a tail pointer prior to GTK+
  70. -- 2.6. As a result, it was fast at data insertion and deletion,
  71. -- and not fast at random data access. The GtkListStore sets the
  72. -- GTK_TREE_MODEL_ITERS_PERSIST flag, which means that GtkTreeIters
  73. -- can be cached while the row exists. Thus, if access to a
  74. -- particular row is needed often and your code is expected to run
  75. -- on older versions of GTK+, it is worth keeping the iter around.
  76. -- Note: GTK_LIST_STORE has some features that are deliberately similar to COLLECTION's
  77. inherit
  78. GTK_TREE_MODEL
  79. GTK_TREE_SORTABLE
  80. GTK_TREE_DRAG_SOURCE
  81. GTK_TREE_DRAG_DEST
  82. insert
  83. GTK_STORE_SETTERS
  84. GTK_LIST_STORE_EXTERNALS
  85. creation make, from_external_pointer
  86. feature {} -- Creation
  87. make (some_columns: ARRAY[INTEGER]) is
  88. -- Creates a new list store. `some_columns' is a list of integers; each
  89. -- integer is the G_TYPE of an actual column. Note that only types
  90. -- derived from standard GObject fundamental types are supported.
  91. -- As an example, make (<<g_type_int, g_type_string, gdk_type_pixbuf>>)
  92. -- will create a new GtkListStore with three columns, of type int,
  93. -- string and GdkPixbuf respectively.
  94. -- TODO: make this more eiffelish
  95. -- Note: ARRAY seems to be the more general class that fits the
  96. -- task. Feel free to change it. Paolo 2006-02-22
  97. require gtk_initialized: gtk.is_initialized
  98. do
  99. from_external_pointer (gtk_list_store_newv (some_columns.count, some_columns.to_external))
  100. end
  101. feature {} -- Unwrapped code
  102. -- gtk_list_store_set_column_types ()
  103. -- void gtk_list_store_set_column_types (GtkListStore *list_store, gint
  104. -- n_columns, GType *types);
  105. -- This function is meant primarily for GObjects that inherit from
  106. -- GtkListStore, and should only be used when constructing a new
  107. -- GtkListStore. It will not function after a row has been added, or a method
  108. -- on the GtkTreeModel interface is called.
  109. -- list_store : A GtkListStore
  110. -- n_columns : Number of columns for the list store
  111. -- types : An array length n of GTypes
  112. -- gtk_list_store_set ()
  113. -- void gtk_list_store_set (GtkListStore *list_store, GtkTreeIter *iter,
  114. -- ...);
  115. -- Sets the value of one or more cells in the row referenced by iter. The
  116. -- variable argument list should contain integer column numbers, each column
  117. -- number followed by the value to be set. The list is terminated by a
  118. -- -1. For example, to set column 0 with type G_TYPE_STRING to "Foo", you
  119. -- would write gtk_list_store_set (store, iter, 0, "Foo", -1).
  120. -- list_store : a GtkListStore
  121. -- iter : row iterator
  122. -- ... : pairs of column number and value, terminated with -1
  123. -- gtk_list_store_set_valist ()
  124. -- void gtk_list_store_set_valist (GtkListStore *list_store, GtkTreeIter
  125. -- *iter, va_list var_args);
  126. -- See gtk_list_store_set(); this version takes a va_list for use by language
  127. -- bindings.
  128. -- list_store : A GtkListStore
  129. -- iter : A valid GtkTreeIter for the row being modified
  130. -- var_args : va_list of column/value pairs
  131. feature -- Generic setter
  132. set_value (an_iterator: GTK_TREE_ITER; a_column: INTEGER; a_value: G_VALUE) is
  133. -- Sets the data in the cell specified by `an_iterator' and
  134. -- `a_column'. The type of `a_value' must be convertible to the type of
  135. -- the column.
  136. -- `an_iterator': A valid GtkTreeIter for the row being modified
  137. -- `a_column' : column number to modify
  138. -- `a_value' : new value for the cell
  139. require
  140. valid_iterator: an_iterator/=Void
  141. valid_value: a_value /= Void -- and then Eiffelize "The type of
  142. -- `a_value' must be convertible to the type of the column."
  143. do
  144. gtk_list_store_set_value (handle, an_iterator.handle, a_column, a_value.handle)
  145. end
  146. is_last_iterator_valid: BOOLEAN
  147. -- Is the last iterator passed as an argument still valid?
  148. remove (an_iterator: GTK_TREE_ITER) is
  149. -- Removes the given row from the list store. After being removed,
  150. -- `an_iterator' is set to be the next valid row, or invalidated if it
  151. -- pointed to the last row in the list store; in this case
  152. -- `is_last_iterator_valid' will be False
  153. require
  154. valid_iterator: an_iterator/=Void
  155. do
  156. is_last_iterator_valid := gtk_list_store_remove (handle,an_iterator.handle).to_boolean
  157. end
  158. put (an_iterator: GTK_TREE_ITER; a_position: INTEGER) is
  159. -- Creates a new row at position. iter will be changed to
  160. -- point to this new row. If position is larger than the
  161. -- number of rows on the list, then the new row will be
  162. -- appended to the list. The row will be empty after this
  163. -- function is called. To fill in values, you need to call
  164. -- `set' or `set_value'.
  165. require
  166. valid_iterator: an_iterator/=Void
  167. do
  168. gtk_list_store_insert (handle, an_iterator.handle, a_position)
  169. end
  170. put_before, insert_before (an_iterator, a_sibling: GTK_TREE_ITER) is
  171. -- Inserts a new row before `a_sibling'. `an_iterator' iter will
  172. -- be changed to point to this new row. The row will be empty
  173. -- after this function is called. To fill in values, you need
  174. -- to call gtk_list_store_set() or
  175. -- gtk_list_store_set_value().
  176. require
  177. valid_iterator: an_iterator/=Void
  178. valid_sibling: a_sibling/=Void
  179. do
  180. gtk_list_store_insert_before (handle, an_iterator.handle, a_sibling.handle)
  181. end
  182. add_last (an_iterator: GTK_TREE_ITER) is
  183. -- Append the row referred by `an_iterator' to the end of the
  184. -- list. `an_iterator' will be changed to point to this new
  185. -- row. The row will be empty after this function is
  186. -- called. To fill in values, you need to call `set' or
  187. -- `set_value'.
  188. require
  189. valid_iterator: an_iterator/=Void
  190. do
  191. gtk_list_store_insert_before (handle, an_iterator.handle, default_pointer)
  192. end
  193. put_after, insert_after (an_iterator, a_sibling: GTK_TREE_ITER) is
  194. -- Inserts a new row after `a_sibling'. `an_iterator' iter
  195. -- will be changed to point to this new row. The row will be
  196. -- empty after this function is called. To fill in values,
  197. -- you need to call `set' or `set_value'.
  198. require
  199. valid_iterator: an_iterator/=Void
  200. valid_sibling: a_sibling/=Void
  201. do
  202. gtk_list_store_insert_after (handle, an_iterator.handle, a_sibling.handle)
  203. end
  204. add_first (an_iterator: GTK_TREE_ITER) is
  205. -- Prepend the row to the beginning of the list. `an_iterator' iter will
  206. -- be changed to point to this new row. The row will be empty
  207. -- after this function is called. To fill in values, you need
  208. -- to call `set' or `set_value'.
  209. require
  210. valid_iterator: an_iterator/=Void
  211. do
  212. gtk_list_store_insert_after (handle, an_iterator.handle, default_pointer)
  213. end
  214. -- Unwrappable: variadic gtk_list_store_insert_with_values ()
  215. -- void gtk_list_store_insert_with_values
  216. -- (GtkListStore *list_store,
  217. -- GtkTreeIter *iter,
  218. -- gint position,
  219. -- ...);
  220. -- Creates a new row at position. iter will be changed to point to
  221. -- this new row. If position is larger than the number of rows on
  222. -- the list, then the new row will be appended to the list. The row
  223. -- will be filled with the values given to this function.
  224. -- Calling gtk_list_store_insert_with_values(list_store, iter,
  225. -- position...) has the same effect as calling
  226. -- gtk_list_store_insert (list_store, iter, position);
  227. -- gtk_list_store_set (list_store_iter, ...);
  228. -- with the difference that the former will only emit a
  229. -- row_inserted signal, while the latter will emit row_inserted,
  230. -- row_changed and, if the list store is sorted,
  231. -- rows_reordered. Since emitting the rows_reordered signal
  232. -- repeatedly can affect the performance of the program,
  233. -- gtk_list_store_insert_with_values() should generally be
  234. -- preferred when inserting rows in a sorted list store.
  235. -- list_store : A GtkListStore
  236. -- iter : An unset GtkTreeIter to set to the new row
  237. -- position : position to insert the new row
  238. -- ... : pairs of column number and value, terminated with -1
  239. insert_with_values (an_iterator: GTK_TREE_ITER;
  240. a_position: INTEGER;
  241. some_columns: ARRAY[INTEGER];
  242. some_values: ARRAY[G_VALUE]) is
  243. -- Creates a new row at `a_position'. `an_iterator' will be changed to
  244. -- point to this new row. If position is larger than the number of rows
  245. -- on the list, then the new row will be appended to the list. The row
  246. -- will be filled with the values given to this function.
  247. -- `insert_with_values' has the same effect as calling `put' (known as
  248. -- insert in C) and `set_values' with the difference that the former
  249. -- will only emit a row_inserted signal, while the latter will emit
  250. -- row_inserted, row_changed and, if the list store is sorted,
  251. -- rows_reordered. Since emitting the rows_reordered signal repeatedly
  252. -- can affect the performance of the program, insert_with_values should
  253. -- generally be preferred when inserting rows in a sorted list store.
  254. -- `some_columns' contains the column numbers; each column
  255. -- will be set with the corresponding value in `some_values'
  256. require columns_n_equals_values_n: some_columns.count = some_values.count
  257. do
  258. -- Uses a variant of gtk_list_store_insert_with_values() which takes
  259. -- the columns and values as two arrays, instead of varargs. This
  260. -- function is mainly intended for language-bindings. (PS Paolo: No, I
  261. -- couldn't believe it!)
  262. -- list_store : A GtkListStore
  263. -- iter : An unset GtkTreeIter to set to the new row
  264. -- position : position to insert the new row
  265. not_yet_implemented
  266. -- TODO: some_values.to_external is an array of pointers to the Eiffel wrappers!!!
  267. gtk_list_store_insert_with_valuesv (handle, an_iterator.handle,
  268. a_position,
  269. -- gint *columns an array of column numbers
  270. some_columns.to_external,
  271. -- GValue *values an array of
  272. -- GValues
  273. some_values.to_external,
  274. -- gint n_values the length of the
  275. -- columns and values arrays
  276. some_values.count
  277. )
  278. end
  279. prepend (an_iterator: GTK_TREE_ITER) is
  280. -- Prepends a new row. `an_iterator' will be changed to
  281. -- point to this new row. The row will be empty after this
  282. -- function is called. To fill in values, you need to call
  283. -- `set_values'
  284. -- TODO: C documentation says that `an_iterator` shall be unset. Is it true?
  285. require valid_iterator: an_iterator/=Void
  286. do
  287. gtk_list_store_prepend (handle, an_iterator.handle)
  288. end
  289. append (an_iterator: GTK_TREE_ITER) is
  290. -- Append a new row. `an_iterator' will be changed to point to this new
  291. -- row. The row will be empty after this function is called. To fill in
  292. -- values, you need to call `set_values'
  293. -- TODO: C documentation says that `an_iterator` shall be unset. Is it
  294. -- true?
  295. require valid_iterator: an_iterator/=Void
  296. do
  297. gtk_list_store_append (handle, an_iterator.handle)
  298. end
  299. clear is
  300. -- Removes all rows from the list store.
  301. do
  302. gtk_list_store_clear (handle)
  303. end
  304. is_iterator_valid (an_iterator: GTK_TREE_ITER): BOOLEAN is
  305. -- Is `an_iterator' valid for Current GtkListStore?
  306. -- Warning: this query is slow. Only use it for debugging
  307. -- and/or testing purposes.
  308. require valid_iterator: an_iterator/=Void
  309. do
  310. Result:=(gtk_list_store_iter_is_valid(handle,an_iterator.handle)).to_boolean
  311. end
  312. reorder (a_new_order: ARRAY[INTEGER]) is
  313. -- Reorders store to follow the order indicated by
  314. -- `a_new_order'. Note that this function only works with
  315. -- unsorted stores. `a_new_order' is the array of integers
  316. -- mapping the new position of each child to its old position
  317. -- before the re-ordering, i.e. a_new_order.item(newpos) =
  318. -- oldpos.
  319. require
  320. unsorted_store: not is_sorted
  321. valid_order: a_new_order /= Void
  322. do
  323. gtk_list_store_reorder (handle, a_new_order.to_external)
  324. end
  325. swap (an_iterator, another: GTK_TREE_ITER) is
  326. -- Swaps `an_iterator' and `another' in store. Note that this
  327. -- function only works with unsorted stores.
  328. require
  329. unsorted_store: not is_sorted
  330. valid_iterator: an_iterator/=Void
  331. valid_another: another/=Void
  332. do
  333. gtk_list_store_swap (handle, an_iterator.handle, another.handle)
  334. end
  335. move_before (an_iterator, a_position: GTK_TREE_ITER) is
  336. -- Moves iter in store to the position before position. Note
  337. -- that this function only works with unsorted stores.
  338. require
  339. unsorted_store: not is_sorted
  340. valid_iterator: an_iterator/=Void
  341. valid_position: a_position/=Void
  342. do
  343. gtk_list_store_move_before (handle,an_iterator.handle, a_position.handle)
  344. end
  345. move_last (an_iterator: GTK_TREE_ITER) is
  346. -- Moves iter in store to the end of the list. Note that this
  347. -- function only works with unsorted stores.
  348. require
  349. unsorted_store: not is_sorted
  350. valid_iterator: an_iterator/=Void
  351. do
  352. gtk_list_store_move_before (handle,an_iterator.handle, default_pointer)
  353. end
  354. move_after (an_iterator, a_position: GTK_TREE_ITER) is
  355. -- Moves iter in store to the position after position. Note
  356. -- that this function only works with unsorted stores.
  357. require
  358. unsorted_store: not is_sorted
  359. valid_iterator: an_iterator/=Void
  360. valid_position: a_position/=Void
  361. do
  362. gtk_list_store_move_after (handle,an_iterator.handle, a_position.handle)
  363. end
  364. move_first (an_iterator: GTK_TREE_ITER) is
  365. -- Moves item pointed to by `an_iterator' to the start of the list.
  366. -- Note that this function only works with unsorted stores.
  367. require
  368. unsorted_store: not is_sorted
  369. valid_iterator: an_iterator/=Void
  370. do
  371. gtk_list_store_move_after (handle, an_iterator.handle, default_pointer)
  372. end
  373. end