[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:16:07 JST


-------------------------
REMOTE_ADDR = 184.145.80.187
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-parts
-------------------------
@@ -321,121 +321,7 @@
 
 
 
-######## correction of what looks like an omission rather than a BUG # -s- ##########
-{{image_right("dialog-warning.png")}}
 
-Following is the pertinent API documentation of Gtk::TreeViewColumn.new and an example found there,
-which I believe is rather misleading though, if one reads it carefully it does not state anything inappropriately.
-
---- Gtk::TreeViewColumn.new(title = nil, cell_renderer = nil, attributes = nil)
-
-    Creates a new Gtk::TreeViewColumn with a number of default values. 
-    This is equivalent to calling Gtk::TreeViewColumn.new, Gtk::TreeViewColumn#title=, Gtk::TreeViewColumn#pack_start, and Gtk::TreeViewColumn#set_attributes on the newly created Gtk::TreeViewColumn.
-    * title: The title to set the header to. 
-    * cell_renderer: The Gtk::CellRenderer
-    * attributes: a hash of attributes.
-    * Returns : A newly created Gtk::TreeViewColumn.
-       
-    Here's a simple example:
- 
-     TEXT_COLUMN = 0
-     COLOR_COLUMN = 1
- 
-     renderer = Gtk::CellRendererText.new
-     column = Gtk::TreeViewColumn.new("Title", renderer, 
-                                      :text => TEXT_COLUMN,          # COLUMN NUMBERS - NOT attribute values !!!
-                                      :foreground => COLOR_COLUMN)   # COLUMN NUMBERS - NOT attribute values !!!
-
-
-#---------------------------
-Let me warn you again not to be misled by the following code and also the Gtk API documentation that explains it:
-
- # in this code segment user data (in :text property) and system data (in :foreground property) are
- # stored together in the model (store).
- column   = Gtk::TreeViewColumn.new("Buy", renderer,  {:text => 3, :foreground => 4})
-
-Particularly misleading in the above code and in the API documentation are the parts (if read inattentively) suggesting that the hash of attributes may be used to set the attributes to their respective values. That may have been an original Gtk GUI developer's goal that never materialized, or perhaps not, since the way it works now is rather useful though undoubtedly very convoluted. Namely, one may incorrectly conclude that in the above code we could set the foreground colour directly when creating the view column. This, however, is not the case, namely all attributes in the hash can only be assigned their respective column numbers in the list or tree store, which, if model and view columns are incorrectly assumed to be mapped to one another will cause inconsistent programming errors - (note, model columns are not the same as view columns, moreover, view columns are never directly coded or specified! (You will understand this better when defining sort column in tree view, w
 here the column number is not the view but rather model column number.)) Unless you need to design tree views where rendering style or colour of rows in the view depend on some user data, or if you need to design tree views that look like file browser, where each row is rendered in alternate colours, you can simply ignore the sophistication of storing miscellaneous system attribute values in a list or tree models along with the user data. (We will, however, look at this again later.)   
-
-
-
-#-------
-Note, that for setting a property to a property value you use a different syntax, namely:
-
- renderer = Gtk::CellRendererText.new
- renderer.foreground = "#ff0000"                   # or, you could also use colour name ("red") instead. 
- renderer.text = "This is what may be displayed"   # setting text attribute MANUALLY
-
-The((*text*))property above also needs special attention, since this attribute is most likely associated with the model, and hence is automatically rendered by Gtk, provided you set the appropriate column number for text via Gtk::TreeViewColumn.new.  You would set((*text*))manually only for virtual columns in tree view, or columns that need some intervention at run time. 
-
-From the above discussion about column numbers it is important to understand that model columns do not map directly to columns in tree view. While columns in view are determined by the order in which   Gtk::TreeView#append_column statements are executed, the mapping to model columns is defined by the programmer by defining the column numbers via Gtk::TreeViewColumn.new in((*:text = column_number*))idiom or in hash of attributes, when it is used instead. A renderer normally is associated only with a single view column, but can have associated more view columns, as is the case with our example exhibiting((*:foreground*))attribute. Each model column is automatically associated with a particular renderer in the order defined by the programmer at the time he or she issues Gtk::TreeView#append_column statements.
-
-However, earlier we have also learned, that cell renderers are packed into tree view columns similar to how you add widgets into horizontal boxes, and that each tree view column can contain one or more cell renderers, which are used to render the data. For example, in a file browser, the image column would be rendered with the Gtk::CellRendererPixbuf and the file name with Gtk::CellRendererText. Hence, we see time and time again, that we have to be careful not to assume one to one mapping of view and model columns.
-
-
-#-------
-#---------------------------
-######## correction of what looks like an omission rather than a BUG # -e- ##########
-
-:Shorter:
-    These two lines are identical to the five below:
-     renderer = Gtk::CellRendererText.new
-     column = Gtk::TreeViewColumn.new("Buy", renderer, :text => BUY_IT)
-:Longer:
-    The following is equivalent to the above:
-     renderer = Gtk::CellRendererText.new
-     column = Gtk::TreeViewColumn.new
-     column.title = "Buy"
-     column.pack_start(renderer, false)
-     column.set_attributes(:text => BUY_IT)
-
-
-Note how renderer is packed into the column. Following is the API for this line:
-
---- pack_start(cell, expand)
-
-    Packs the cell into the beginning of the column. If expand is false, then the cell is allocated no more space than it needs. Any unused space is divided evenly between cells for which expand is true.
-    * cell: The Gtk::CellRenderer.  
-    * expand: true if cell is to be given extra space allocated to box.  
-    * Returns: self
-
-=== Creating the Gtk::ListStore
-
-Lets now return back to our example program (liststore.rb). 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.
-
-First we create the list and at the same time define the data types for each column within a row, and then loop 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 to individual columns with Gtk::TreeIter#[], or alternatively with Gtk::ListStore#set_value:
-
- # Create a new tree model with three columns, as Boolean,
- # integer and string.
- store = Gtk::ListStore.new(TrueClass, Integer, String)
- 
- # Add all of the products to the GtkListStore.
- 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
 
 
 === Sorting Tree View




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