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

Zurück zum Archiv-Index

ruby-****@sourc***** ruby-****@sourc*****
2012年 8月 25日 (土) 01:55:27 JST


-------------------------
REMOTE_ADDR = 70.49.49.99
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-rr
-------------------------
@@ -140,3 +140,150 @@
  end
  
 If you want to remove all rows you can use Gtk::ListStore#clear and Gtk::TreeStore#clear.
+
+The following summary example demonstrates some of the points learned here:
+
+((*Example employing some of the items discussed on this page:*))
+
+
+{{image_right "TreeStore-n-Refs.png"}}
+
+ #!/usr/bin/env ruby
+ require 'gtk2'
+ 
+ =begin
+   1. create 3 levels deep TreeStore
+ 
+   2. Implement {{ set_cell_data_func }}
+   
+   3. Demonstrate how a renderer property (foreground) is handled
+      via {{r.foreground="red"}}, and {{fu_r.foreground_set=false}}
+      inside (as well as outside) the {{ set_cell_data_func }}
+ 
+   4. Use a virtual view column to dynamically display path and depth
+   
+   5. Test the feature showing how to obtain an iter from a tree reference
+ =end
+ 
+ treestore = Gtk::TreeStore.new(String, String, Integer)
+ 
+ # Append a toplevel row and fill in some data
+ parent = treestore.append(nil)
+ parent[0] = "Maria"
+ parent[1] = "Incognito"
+ # Append another toplevel row and fill in some data
+ parent = treestore.append(nil)
+ parent[0] = "Tony"
+ parent[1] = "Zus"
+ parent[2] = 1949
+ # Append a second toplevel row and fill in some data
+ parent = treestore.append(nil)
+ parent[0] = "Jane"
+ parent[1] = "Average"
+ parent[2] = 1962
+ # Append a child to the second toplevel row and fill in some data
+ child = treestore.append(parent)
+ child[0] = "Janinita"
+ child[1] = "Average"
+ child[2] = 1985
+ # Append a grandchild to the third level row and fill in some data
+ grandchild = treestore.append(child)
+ grandchild[0] = "Janinitica"
+ grandchild[1] = "Average"
+ grandchild[2] = 2002
+ # Append the 2nd grandchild to the third level row and fill in some data
+ grandchild = treestore.append(child)
+ grandchild[0] = "Janinitica2"
+ grandchild[1] = "Average"
+ grandchild[2] = 2003
+ # Append again a second toplevel row and fill in some data
+ parent = treestore.append(nil)
+ parent[0] = "Gustav"
+ parent[1] = "Crocky"
+ parent[2] = 1951
+ # Append a child to the second toplevel row and fill in some data
+ child = treestore.append(parent)
+ child[0] = "Gustlek"
+ child[1] = "Crocky"
+ child[2] = 1970
+ # Append a child to the second toplevel row and fill in some data
+ child = treestore.append(parent)
+ child[0] = "Zyla"
+ child[1] = "Crocky"
+ child[2] = 1973
+ 
+ 
+ view = Gtk::TreeView.new(treestore)
+ view.selection.mode = Gtk::SELECTION_NONE
+ 
+ # Create a renderer
+ renderer = Gtk::CellRendererText.new
+ # Add column using our renderer
+ col = Gtk::TreeViewColumn.new("First Name", renderer, :text => 0)
+ view.append_column(col)
+ 
+ # Create another renderer and set the weight property
+ renderer = Gtk::CellRendererText.new
+ renderer.weight = Pango::FontDescription::WEIGHT_BOLD
+ # Add column using the second renderer
+ col = Gtk::TreeViewColumn.new("Last Name", renderer, :text => 1)
+ view.append_column(col)
+ 
+ # Create one more renderer and set the foreground color to red
+ renderer = Gtk::CellRendererText.new
+ renderer.foreground = "red"
+ # Add column using the third renderer
+ col = Gtk::TreeViewColumn.new("Age", renderer)
+ view.append_column(col)
+ 
+ # Create a cell data function to calculate age
+ col.set_cell_data_func(renderer) do |col, renderer, model, iter|
+   year_now = 2012 # To save code not relevent to the example
+   year_born = iter[2]
+ 
+   if (year_born <= year_now) && (year_born > 0)
+ 
+     ### - NOTE: the following doesn't work {{ iter[2] === Integer }} !!!
+     # iter[2] = sprintf("%i years old", year_now - year_born)
+     ### - You must employ:  renderer.text = ...
+     renderer.text = sprintf("%i years old", year_now - year_born)
+ 
+ 
+     # render in default foreground color if we know the age
+     renderer.foreground_set = false
+   else
+     renderer.text = "age unknown"
+     # render with foreground color we set earlier if we don't know the age
+     renderer.foreground_set = true
+   end
+ end
+ 
+ # - add another (virtual) VIEW column to TreeView
+ # Create one last renderer and set its display value to row's path
+ renderer = Gtk::CellRendererText.new
+ # Add column using the third renderer
+ col = Gtk::TreeViewColumn.new("Path & Depth", renderer)
+ view.append_column(col)
+ 
+ # Create a cell data function to display row path and depth.
+ col.set_cell_data_func(renderer) do |col, renderer, model, iter|
+  renderer.text = "P:#{iter.path.to_str} \tDepth:#{iter.path.depth}"
+ end
+ 
+ vbox = Gtk::VBox.new(homogeneous=false, spacing=nil) 
+ button = Gtk::Button.new("Click to Test TreeReference")
+ button.signal_connect("clicked") do
+   # pretend we have an arbitrary TreeReference
+   row_ref = Gtk::TreeRowReference.new(treestore, Gtk::TreePath.new("2:0:1"))
+   iter = row_ref.model.get_iter(row_ref.path)
+   puts "Iter from Ref: First Name: #{iter[0]}"
+ end
+ 
+ vbox.pack_start_defaults(view)
+ vbox.pack_start_defaults(button)
+ 
+ window = Gtk::Window.new("Test Obtaining Iters from TreeReference")
+ window.signal_connect("destroy") { Gtk.main_quit }
+ window.add(vbox)
+ window.show_all
+ Gtk.main




ruby-gnome2-cvs メーリングリストの案内
Zurück zum Archiv-Index