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

Back to archive index

ruby-****@sourc***** ruby-****@sourc*****
2012年 9月 19日 (水) 04:56:38 JST


-------------------------
REMOTE_ADDR = 184.145.80.187
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-trees
-------------------------
@@ -640,3 +640,100 @@
 
 {{br}}
 ((*liststore-col-w-double-renderers-pixbufs.rb*))
+
+
+ #!/usr/bin/env ruby
+ require 'gtk2'
+ 
+ def setup_tree_view(treeview)
+   renderer = Gtk::CellRendererText.new
+   renderer.foreground = "#ff0000"
+   column   = Gtk::TreeViewColumn.new("Buy", renderer,  :text => BUY_IT)
+   treeview.append_column(column)
+   renderer = Gtk::CellRendererText.new
+   column   = Gtk::TreeViewColumn.new("Count", renderer, :text => QUANTITY)
+   treeview.append_column(column)
+ 
+ # column   = Gtk::TreeViewColumn.new("Product", renderer, 'text' => PRODUCT)
+   column   = Gtk::TreeViewColumn.new
+   column.title = "Product /w icons"
+   pixb_renderer = Gtk::CellRendererPixbuf.new
+   renderer = Gtk::CellRendererText.new
+   column.pack_start(pixb_renderer, false)
+   column.pack_start(renderer, false)
+   column.set_cell_data_func(pixb_renderer) do |col, renderer, model, iter|
+     renderer.stock_id = Gtk::Stock::DIALOG_ERROR if iter[ICON] == nil
+   end
+ 
+   # PECULIARITY: You can only add/set attributes after they were packed
+ # column.add_attribute(pixb_renderer, :pixbuf, ICON)
+   column.set_attributes(pixb_renderer, :pixbuf => ICON)
+ # column.add_attribute(renderer, :text, PRODUCT)
+   column.set_attributes(renderer, :text => PRODUCT)
+   treeview.append_column(column)
+ end
+ def get_pixbuf_if_file_exists(file)
+   begin
+     pixbuf = Gdk::Pixbuf.new(file)
+     rescue GLib::FileError => err
+       print "I/O ERROR (%s): %s\n" % [err.class, err]
+       pixbuf = nil
+   end
+   pixbuf
+ end
+ window = Gtk::Window.new("Test Multi-Item Column ")
+ window.resizable = true
+ window.border_width = 10
+ window.signal_connect('destroy') { Gtk.main_quit }
+ window.set_size_request(270, 185)
+ 
+ class GroceryItem
+   attr_accessor :buy, :quantity, :product, :icon
+   def initialize(b, q, p, i); @buy, @quantity, @product, @icon = b, q, p, i; end
+ end
+ BUY_IT = 0; QUANTITY = 1; PRODUCT  = 2; ICON=3
+ list = Array.new
+ list[0] = GroceryItem.new(true,  1, "Paper Towels", "redo.png") 
+ list[1] = GroceryItem.new(true,  1, "Dog House",    "go-home.png")
+ list[2] = GroceryItem.new(false, 1, "Butter",       nil)
+ list[3] = GroceryItem.new(true,  1, "Milk",         "redo.png")
+ list[4] = GroceryItem.new(false, 3, "Chips",        nil)
+ list[5] = GroceryItem.new(true,  4, "Soda",         nil) 
+ store = Gtk::ListStore.new(TrueClass, Integer, String, Gdk::Pixbuf)
+ treeview = Gtk::TreeView.new(store)
+ setup_tree_view(treeview)
+ 
+ list.each_with_index do |e, i|
+   iter = store.append
+   iter[BUY_IT]   = list[i].buy
+   iter[QUANTITY] = list[i].quantity
+   iter[PRODUCT]  = list[i].product
+   iter[ICON] = get_pixbuf_if_file_exists(list[i].icon) if list[i].icon
+ end
+ scrolled_win = Gtk::ScrolledWindow.new
+ scrolled_win.add(treeview)
+ scrolled_win.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
+ window.add(scrolled_win)
+ window.show_all
+ Gtk.main
+
+There are a few places in the "liststore.rb" listing, that changed to produce new "Multi-item Column" version. We can start with the expansion of the model. Namely, we had to add the new Gdk::Pixbuf model column:
+
+ store = Gtk::ListStore.new(TrueClass, Integer, String, Gdk::Pixbuf)
+
+After the model structure was augmented we also need to provide and load the data into the newly created column. Accordingly a new column constant ((*ICON*)) is added and the column of image file names is added to the ((*list*)) array. These changes are also reflected in the data loading loop, where our model in ((*store*)) variable is initialized 
+with a Gtk::Pixbuf object created from the image file. For this to work we need a helper method called ((*get_pixbuf_if_file_exists*)), which has to make sure in event image files do not exist errors are handled gracefully.
+
+But the most important changes for this "Multi-item Column" version program are tucked away in the ((*setup_tree_view*)) method, where we needed to replace the original column instantiation method which handled a single "Product" item with a number of different methods that now manage both text and image items in the same column. This also requires reordering of statements dictated first by the API, and second by the way we would like to arrange the order of items (ie text and images) in the Product column. For the API related reordering of code please check the listing and search for the comment (# PECULIARITY: ...). The ordering of image and text in the column deserves some more attention. If you didn't care weather image appeared before the string (text), you wouldn't have to replace the original Product column instantiation statement:
+
+ column   = Gtk::TreeViewColumn.new("Product", renderer, 'text' => PRODUCT)
+
+You could simply pack the "pixb_renderer" into it. We chose the other option, namely, to position the image before the text. This is why we have to manually build both renderers (one for the image and the other for text) and then pack them into a column in order we like!
+
+Yet another wrinkle in our column building process is handling the situations when image is not available. We chose to complain with an annoying ERROR image. This is done with the stock item as follows: 
+
+ column.set_cell_data_func(pixb_renderer) do |col, renderer, model, iter|
+   renderer.stock_id = Gtk::Stock::DIALOG_ERROR if iter[ICON] == nil
+ end
+
+Indeed, to understand this latest item you need to know about ((*Cell Data Functions*)) explained above.




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