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

アーカイブの一覧に戻る

ruby-****@sourc***** ruby-****@sourc*****
2012年 10月 11日 (木) 03:49:55 JST


-------------------------
REMOTE_ADDR = 184.145.82.7
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-cbbr
-------------------------
@@ -6,15 +6,145 @@
 
 
 
+{{image_right("treev-combo-00.png")}}
+
+
+
+{{br}}
+
+
+((*combo-rndr-1.rb*))
+
+ #!/usr/bin/env ruby
+ require 'gtk2'
+ 
+ # Add two columns to the tree view. The first column will be
+ # an editable combo-box and the second (last) column will be the
+ # name of the product category or the product to buy.
+ def setup_tree_view(treeview)
+   # Create a Gtk::ListStore that will be used for the
+   # combo box renderer.
+   model = Gtk::ListStore.new(String)
+   iter = model.append
+   iter[0] = "None"
+   iter = model.append
+   iter[0] = "One"
+   iter = model.append
+   iter[0] = "Half a Dozen"
+   iter = model.append
+   iter[0] = "Dozen"
+   iter = model.append
+   iter[0] = "Two Dozen"
+   # Create the GtkCellRendererCombo and add the tree model.
+   # Then add the renderer to a new column and add the column
+   # to the GtkTreeView
+   renderer = Gtk::CellRendererCombo.new
+   column = Gtk::TreeViewColumn.new("Count", renderer, "text" => COUNT_COLUMN)
+   column.set_cell_data_func(renderer) do |tvc, cell, model, iter|
+     cell.editable = iter.has_child? ? false : true
+   end
+   renderer.editable = true
+   renderer.text_column = 0
+   renderer.model = model 
+   renderer.signal_connect('edited') do |w, path, new_text|
+     puts "DEBUG: widget=#{w.class} path=#{path} new_text=#{new_text}"
+     if (iter = treeview.model.get_iter(path))
+       iter[COUNT_COLUMN] = new_text
+     end
+   end
+   treeview.append_column(column)
+
+   renderer = Gtk::CellRendererText.new
+   column = Gtk::TreeViewColumn.new("Product", renderer, "text" => PRODUCT_COLUMN)
+   treeview.append_column(column)
+ end
+ 
+ class GroceryItem
+   attr_accessor :product_type, :quantity, :product
+   def initialize(t,q,p)
+     @product_type, @quantity, @product = t, q, p
+   end
+ end
+ COUNT_COLUMN = 0; PRODUCT_COLUMN = 1
+ P_CATG = 0; P_CHLD = 1
+ 
+ list = [
+   GroceryItem.new(P_CATG, " - n/a -",     "Cleaning Supplies"),
+   GroceryItem.new(P_CHLD, "none", "Paper Towels"),
+   GroceryItem.new(P_CHLD, "none", "Toilet Paper"),
+   GroceryItem.new(P_CATG, " - n/a -",     "Food"),
+   GroceryItem.new(P_CHLD, "none", "Bread"),
+   GroceryItem.new(P_CHLD, "A lot, and more", "Butter"),
+   GroceryItem.new(P_CHLD, "none", "Milk"),
+   GroceryItem.new(P_CHLD, "none", "Chips"),
+   GroceryItem.new(P_CHLD, "none", "Soda")
+ ]
+ treeview = Gtk::TreeView.new
+ setup_tree_view(treeview)
+ 
+ # Create a new tree model with three columns, as Boolean, 
+ # integer and string.
+ store = Gtk::TreeStore.new(String, String)
+ 
+ # Avoid creation of iterators on every iterration, since they
+ # need to provide state information for all iterations. Hence:
+ # establish closure variables for iterators parent and child.
+ parent = child = nil
+ 
+ # Add all of the products to the GtkListStore.
+ list.each_with_index do |e, i|
+   # If the product type is a category, count the quantity
+   # of all of the products in the category that are going
+   # to be bought.
+   if (e.product_type == P_CATG)
+     # Add the category as a new root (parent) row (element).
+     parent = store.append(nil)
+     parent[COUNT_COLUMN]  = list[i].quantity
+     parent[PRODUCT_COLUMN] = list[i].product
+ 
+   # Otherwise, add the product as a child row of the category.
+   else
+     child = store.append(parent)
+     child[COUNT_COLUMN]  = list[i].quantity
+     child[PRODUCT_COLUMN] = list[i].product
+   end
+ end
+ 
+ # Add the tree model to the tree view
+ treeview.model = store
+ treeview.expand_all
+ scrolled_win = Gtk::ScrolledWindow.new
+ scrolled_win.add(treeview)
+ scrolled_win.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
+ 
+ window = Gtk::Window.new("Grocery List (w/combo #1)")
+ window.resizable = true
+ window.border_width = 10
+ window.signal_connect('destroy') { Gtk.main_quit }
+ window.set_size_request(275, 200)
+ 
+ window.add(scrolled_win)
+ window.show_all
+ Gtk.main
+
+
+
+
+{{br}}
+
+
 {{image_right("treev-combo-01.png")}}
 
+The following is our old 'Grocery List' example with all three columns. It is included mostly because it gives us the opportunity to implement the optimization we first mentioned under the paragraph (Data Dependencies Between Cells In Different Columns And Rows In Tree View) in the section [8.7.1.2] ((<Important Note|tut-gtk2-treev-crs#Important Note>)). Because the 'Buy' column here is not editable, dropping off one of the two cell data function methods, used in 'toggle-rndr-2.rb' example in section [8.7.1.2], here in the following 'combo-rndr-2.rb' example does not have any adverse effect, namely there is no delay, and no auto-corrective action here. Let's look at the listing: 
 
 
 
+
 {{br}}
 
-((*combo-rndr.rb*))
 
+((*combo-rndr-2.rb*))
+
  #!/usr/bin/env ruby
  require 'gtk2'
  
@@ -23,6 +153,137 @@
  # column will be an editable combo-box and the last column
  # will be the name of the product category or the product to buy.
  def setup_tree_view(treeview)
+ 
    # Create a Gtk::ListStore that will be used for the
    # combo box renderer.
    model = Gtk::ListStore.new(String)
@@ -39,11 +170,144 @@
  
    renderer = Gtk::CellRendererText.new
    column = Gtk::TreeViewColumn.new("Buy", renderer, "text" => BUY_COLUMN)
+  
    treeview.append_column(column)
  
    # Create the GtkCellRendererCombo and add the tree model.
    # Then add the renderer to a new column and add the column
    # to the GtkTreeView
+ 
    renderer = Gtk::CellRendererCombo.new
    column = Gtk::TreeViewColumn.new("Count", renderer, "text" => COUNT_COLUMN)
  ##  renderer.width = 20		# doesn't work
@@ -164,7 +297,139 @@
  window.add(scrolled_win)
  window.show_all
  Gtk.main
-
 
 
 




ruby-gnome2-cvs メーリングリストの案内
アーカイブの一覧に戻る