[ruby-gnome2-doc-cvs] [Ruby-GNOME2 Project Website] update - tut-gtk2-treev-parts

Back to archive index

ruby-****@sourc***** ruby-****@sourc*****
2012年 9月 21日 (金) 04:49:05 JST


-------------------------
REMOTE_ADDR = 184.145.80.187
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-parts
-------------------------
@@ -316,13 +316,63 @@
 
 However, unlike the previous code example where we set the "text" and "foreground" attributes directly on the renderer, this code here is using  Gtk::TreeIter's rather than direct access to renders's properties (attributes). More importantly the code using iters does two things - it sets both: (1) the columns in the model and also (2) the columns in the view.  
 
-As you see there are many issues to consider when dealing with tree model and tree view. It is not easy to learn all the aspects of this mechanism up front before you start to experiment with the programs. The best place to get your feet wet in practice and begin investigating and learning the ropes of the tree view and tree model MVC pattern in GTK+ is a program like our "liststore.rb" at the place where the tree view is initialized. In our example program this is the ((*setup_tree_view*)) method. In it, we are defining three individual single columns, where we have a single name/value pair for each attribute. Finally we have to add (append) our newly defined column to the tree view. Copy the program to your system and start playing with it. The above discussions should provide you with plenty of material to try different alternatives.
 
+{{br}}
+We are not quite yet done with our example program "liststore.rb". There are a few more marginal issues to discuss. The one that calls for attention is the way the model is initialized, or as we would say in old computer parlance "the way we load data into the model (list in this case)". So lets now return back to.
 
+
+
+=== Creating the Gtk::ListStore
+
+After the tree view columns are set up with the desired cell renderers, it is time to create our list model that will interface between the renderers and the tree view. As you already know, we used the Gtk::ListStore so that the items would be shown as a list of elements. The fact that our program constants BUY_IT, QUANTITY, and PRODUCT refer to model column numbers, and that the respective data types in the model are Boolean, Integer and String is formalized by the list store (model) constructor:
+
+ # Create a new tree model with three columns, as Boolean,
+ # integer and string.
+ store = Gtk::ListStore.new(TrueClass, Integer, String)
+
+In our program, we invoke this constructor to create the list and at the same time define the data types for each column within a row, just before we enter the loop, by iterating through our array of values to append them to the list. Indeed the important feature in this process is the iterator, i.e. the Gtk::TreeIter object, which is manipulated as an index in our list, where the Gtk::ListStore#append does all the work for us and returns the iterator for each subsequently created empty row as it creates a new empty list entry (record or row) and points the iterator (index) to it. We then proceed to assign values fro our array of GroceryItem objects to individual columns with Gtk::TreeIter#[], or alternatively with Gtk::ListStore#set_value:
+
+ store = Gtk::ListStore.new(TrueClass, Integer, String)
+ 
+ # Add all of the products to the Gtk::ListStore.
+ list.each_with_index do |e, i|
+     iter = store.append
+
+     iter[BUY_IT]   = list[i].buy       # same as: >>> # store.set_value(iter, BUY_IT,   list[i].buy)
+     iter[QUANTITY] = list[i].quantity  # same as: >>> # store.set_value(iter, QUANTITY, list[i].quantity)
+     iter[PRODUCT]  = list[i].product   # same as: >>> # store.set_value(iter, PRODUCT,  list[i].product)
+ end
+
+Following is the pertinent API for the methods we used when dealing with the Gtk::ListStore object in the example above:
+
+--- Gtk::ListStore.new(type1, type2, type3, ...)
+
+    Creates a new tree store as with columns each of the types passed in. As an example, Gtk::ListStore.new(Integer, String, Gdk::Pixbuf) will create a new Gtk::ListStore with three columns, of type Integer, String and Gdk::Pixbuf respectively. 
+    * type1, type2, type3, ... : Object.class value
+    * Returns: A new Gtk::ListStore
+
+--- append
+
+    Appends a new row to list_store. iter will be changed to point to this new row. The new row will be empty after this method is called. To fill in values, you need to call Gtk::TreeIter#set_value or Gtk::ListStore#set_value. 
+    * Returns: A new row(Gtk::TreeIter)
+
+--- set_value(iter, column, value)
+
+    Sets the data in the cell specified by iter and column. The type of value must be convertible to the type of the column. Use Gtk::TreeIter#set_value instead. 
+    * iter : A valid Gtk::TreeIter for the row being modified 
+    * column : A column number to modify 
+    * value : A new value for the cell 
+    * Returns: self
+
+{{br}}
+
+As you see there are many issues to consider when dealing with tree model and tree view. It is not easy to learn all the aspects of this mechanism up front before you start to experiment with the programs. The best place to get your feet wet in practice and begin investigating and learning the ropes of the tree view and tree model MVC pattern in GTK+ is a program like our "liststore.rb" at the place where the tree view is initialized. In our example program this is the ((*setup_tree_view*)) method. In it, we are defining three individual single columns, where we have a single name/value pair for each attribute. Finally we have to add (append) our newly defined column to the tree view. Copy the program to your system and start playing with it. The above discussions should provide you with plenty of material to try different alternatives.
 
 
 
 
+{{br}}
+{{br}}
 
 === Sorting Tree View
 




ruby-gnome2-cvs メーリングリストの案内
Back to archive index