PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/wrappers/gtk/examples/tree/list_demo.e

http://github.com/tybor/Liberty
Specman e | 271 lines | 143 code | 50 blank | 78 comment | 6 complexity | 013577916df61928e08a1b94ab7985d1 MD5 | raw file
  1indexing
  2	description: "List example translated to Eiffel from C"
  3	copyright: "Copyright (c) 2003-2004 Tim-Philipp M??ller <tim at centricular dot net>, Copyright (c) 2005, Paolo Redaelli"
  4	license: "LGPL v2 or later"
  5	date: "$Date:$"
  6	revision: "$Revision:$"
  7	original_version_url: "http://scentric.net/tutorial/treeview-tutorial.html"
  8
  9class LIST_DEMO
 10
 11inherit
 12	GTK
 13	G_TYPES
 14		-- TODO: This class is necessary when creating GTK_LIST_STOREs,
 15		-- since it requires explicit reference to g_type_*; it's ugly,
 16		-- or better it feels mostly unEiffelish to me. Paolo 2005-06-12
 17	
 18	WRAPPER_HANDLER -- required to check for some bug in the implementation and accessing wrappers' handles
 19creation make
 20	
 21feature -- Columns
 22	name_column_n: INTEGER is 0
 23	age_column_n: INTEGER is 1
 24	columns_n: INTEGER is 2
 25
 26feature 
 27	model: GTK_LIST_STORE is -- GTK_TREE_MODEL is
 28			-- tree model with some data set
 29		local
 30			iter: GTK_TREE_ITER
 31		once 
 32			create Result.make (<<g_type_string, g_type_uint>>)
 33			-- TODO: change design to remove explicit reference to g_type_*; it's
 34			-- ugly, or better it feels mostly unEiffelish to me. Paolo 2005-06-01
 35			
 36			-- Append three rows and fill in some data
 37			create iter.make_from_model (Result)
 38			iter.start
 39			Result.append (iter) 
 40			Result.set_string (iter, name_column_n, "Paolo Redaelli")
 41			Result.set_natural (iter, age_column_n, 28)
 42			
 43			Result.append (iter)
 44			Result.set_string (iter, name_column_n, "Richard Stallman")
 45			Result.set_natural (iter, age_column_n, 53)
 46
 47			Result.append (iter)
 48			Result.set_string (iter, name_column_n, "Andreas Leitner")
 49			Result.set_natural (iter, age_column_n, 31)
 50
 51			print ("Model's count: "+iter.toplevel_nodes_count.out+"%N")
 52			check
 53				corrent_number_of_elements: iter.toplevel_nodes_count = 3
 54			end
 55		end
 56
 57	renderer: GTK_CELL_RENDERER
 58
 59	name_column, age_column: GTK_TREE_VIEW_COLUMN
 60
 61	view: GTK_TREE_VIEW is
 62		once
 63			create {GTK_CELL_RENDERER_TEXT} renderer.make
 64			
 65			create name_column.make
 66			name_column.set_title ("Name")
 67			name_column.pack_start (renderer, True)
 68			name_column.add_attribute (renderer, "text", name_column_n)
 69												
 70			create age_column.make
 71			age_column.set_title ("Age")
 72			age_column.pack_start (renderer, True)
 73			age_column.add_attribute (renderer, "text", age_column_n)
 74
 75			create Result.make
 76			Result.insert_column (name_column, name_column_n)
 77			Result.insert_column (age_column, age_column_n)
 78			-- Note: both xxx_column_n was -1. Paolo 2005-06-12
 79			
 80			Result.set_model (model)
 81
 82			-- TODO: Original C example here insert a g_object_unref
 83			-- (model); /* destroy model automatically with view */ I'm
 84			-- not sure, but it shouldn't be necessary. Shall we insert
 85			-- it? Paolo 2005-06-12
 86		end
 87
 88	window: GTK_WINDOW
 89
 90feature {}  -- Creation
 91	
 92	make is
 93			-- Run the demo
 94		local selection: GTK_TREE_SELECTION
 95		do
 96			gtk.initialize_gtk
 97						
 98			-- Create a GTK window toplevel window
 99			create window.make
100			window.set_title (window_title)
101
102			-- It is a good idea to do this for all windows
103			window.connect_agent_to_destroy_signal (agent on_destroy)
104
105			view.show
106			window.add (view)
107			window.show
108
109			selection := view.selection
110			selection.set_select_function (agent on_select)
111			gtk.run_main_loop
112		end
113
114feature
115	traverse_model is
116		require valid_model: model /= Void
117		local iter:  GTK_TREE_ITER
118		do
119			print ("Iterating over names%N")
120			create iter.make_from_model (model)	  
121			from iter.start
122			until not iter.is_valid
123			loop -- print each name ...
124				print (model.value (iter, name_column_n).string)
125				-- Note: Is "model.value" clear enough or "model.value_at" would be clearer?
126				iter.next -- Move to next row
127				if iter.is_valid then print (", ")
128				else print ("%N")
129				end
130			end
131		end
132	
133	paths_demo is
134		local path: GTK_TREE_PATH
135		do
136			print ("Tree paths demo:%N")
137			create path.make_first
138			print ("First: ") print (path.to_string) print ("%N")
139			create path.from_string ("1:3:2")
140			print ("From string: ") print (path.to_string) print ("%N")
141		end
142
143feature -- Agents
144	on_destroy (a_gtk_object: GTK_OBJECT) is
145		do
146			print ("Tree demo is quitting.%N")
147			paths_demo
148			print ("Traversing model:%N")
149			traverse_model
150			print ("Using GTK_TREE_MODEL.for_each to print its content:%N")
151			print ("TODO: in tree_demo on_destroy model.for_each (agent print_person)%N")
152			print ("If GTK_TREE_MODEL.for_each is implemented correctly you should see above each person's name and age.%N")
153			gtk.quit
154		end
155
156	print_person (a_model: GTK_TREE_MODEL; a_path: GTK_TREE_PATH; an_iter: GTK_TREE_ITER): BOOLEAN is
157		do
158				check
159					right_model: a_model = model
160				end
161			print ("Person (code: ")
162			print (a_path.to_string) print (") name '")
163			print (a_model.value_at (an_iter, name_column_n).string)
164			print ("' age ")
165			print (a_model.value_at (an_iter, age_column_n).natural.out)
166			print ("%N")
167			Result := False -- i.e. go on
168		end
169			
170	on_select (a_selection: GTK_TREE_SELECTION; a_model: GTK_TREE_MODEL; a_path: GTK_TREE_PATH; path_selected: BOOLEAN): BOOLEAN is
171		do
172			print ("Path '") print (a_path.to_string)
173			if path_selected
174			 then print ("' is selected")
175			else  print ("' is de-selected")
176			end
177			debug
178				if a_model = Void 
179				 then print ("; no model passed but we're sure that out GTK_LIST_STORE at"+model.to_pointer.out+" has handle="+model.handle.out)
180				else print (".")
181				end
182			end
183			print ("%N")
184			Result := True
185		end
186								
187feature -- Constants
188	window_title: STRING is "GTK Trees!"
189			-- Text top level window title
190end
191
192-- Examples from C docs.
193
194-- To help show some common operation of a model, some examples are provided. The first example shows three ways of getting the iter at the location &#x2019;??3:2:5&#x2019;?É. While the first method shown is easier, the second is much more common, as you often get paths from callbacks.
195
196-- Example 1. Acquiring a GtkTreeIter
197
198-- /* Three ways of getting the iter pointing to the location
199--  */
200-- {
201--   GtkTreePath *path;
202--   GtkTreeIter iter;
203--   GtkTree?Iter parent_iter;
204
205--   /* get the iterator from a string */
206--   gtk_tree_model_get_iter_from_string (model, &iter, "3:2:5");
207
208--   /* get the iterator from a path */
209--   path = gtk_tree_path_new_from_string ("3:2:5");
210--   gtk_tree_model_get_iter (model, &iter, path);
211--   gtk_tree_path_free (path);
212
213
214--   /* walk the tree to find the iterator */
215--   gtk_tree_model_iter_nth_child (model, &iter, NULL, 3);
216--   parent_iter = iter;
217--   gtk_tree_model_iter_nth_child (model, &iter, &parent_iter, 2);
218--   parent_iter = iter;
219--   gtk_tree_model_iter_nth_child (model, &iter, &parent_iter, 5);
220-- }
221
222-- This second example shows a quick way of iterating through a list and getting a string and an integer from each row. The populate_model function used below is not shown, as it is specific to the GtkListStore. For information on how to write such a function, see the GtkListStore documentation.
223
224-- Example 2. Reading data from a GtkTreeModel
225
226-- enum
227-- {
228--   STRING_COLUMN,
229--   INT_COLUMN,
230--   N_COLUMNS
231-- };
232
233-- {
234--   GtkTreeModel *list_store;
235--   GtkTreeIter iter;
236--   gboolean valid;
237--   gint row_count = 0;
238
239--   /* make a new list_store */
240--   list_store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_INT);
241
242--   /* Fill the list store with data */
243--   populate_model (list_store);
244
245--   /* Get the first iter in the list */
246--   valid = gtk_tree_model_get_iter_first (list_store, &iter);
247
248--   while (valid)
249--     {
250--       /* Walk through the list, reading each row */
251--       gchar *str_data;
252--       gint   int_data;
253
254--       /* Make sure you terminate calls to gtk_tree_model_get()
255--        * with a '-1' value
256--        */
257--       gtk_tree_model_get (list_store, &iter, 
258--                           STRING_COLUMN, &str_data,
259--                           INT_COLUMN, &int_data,
260--                           -1);
261
262--       /* Do something with the data */
263--       g_print ("Row %d: (%s,%d)\n", row_count, str_data, int_data);
264--       g_free (str_data);
265
266--       row_count ++;
267--       valid = gtk_tree_model_iter_next (list_store, &iter);
268--     }
269-- }
270
271-- Details