ruby-****@sourc*****
ruby-****@sourc*****
2012年 8月 16日 (木) 04:41:27 JST
------------------------- REMOTE_ADDR = 70.49.49.99 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-parts ------------------------- @@ -222,10 +222,11 @@ In our example program (liststore.rb) above, we are defining three individual single columns, so we have a single name/value pair for each attribute. Finally we have to add (append) our newly defined column to the tree view. +######## correction of what looks like an omission rather than a BUG # -s- ########## {{image_right("dialog-warning.png")}} -Following is the pertinent API documentation and an example found there, -which I believe is either lacking a vital part of setup, is incorrect or there is a bug in current Ruby implementation of Gtk::TreeViewColumn.new. (It looks as if the hash was never meant to define multiple columns.) +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) @@ -243,11 +244,32 @@ 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 !!! + :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: + + column = Gtk::TreeViewColumn.new("Buy", renderer, {:text => 3, :foreground => 4}) + +Particularly misleading in the above code and in the API documentation is the part (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 tree view, which with the exception of :text attribute, is a rather ridiculous thing in most everyday situations. Unless 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 attribute values in a list or tree models along with the user d ata. (We will however look at this more closely in the following pages.) + + +#------- +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 -To fully comprehend the above Gtk::TreeViewColumn.new API fragment just above the questionable code example, it would be beneficial if you understand that the API fragment alludes to how to implement the same functionality as that of the ((*new*)) method, manually - with four simpler methods. So let us rewrite the following code (the shorter fragment) from our example program with a bit longer one, to reflect what was just said: +The ((*text*)) property above 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 model are determined by the order in which Gtk::TreeView#append_column statements are executed, the mapping to view 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. +#------- +#--------------------------- +######## correction of what looks like an omission rather than a BUG # -e- ########## :Shorter: These two lines are identical to the five below: