ruby-****@sourc*****
ruby-****@sourc*****
2012年 8月 12日 (日) 07:04:01 JST
------------------------- REMOTE_ADDR = 184.145.90.35 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-treeview-model-reference ------------------------- @@ -53,3 +53,146 @@ The code above asks the model to fill the iter structure to make it point to the first row in the list store. If there is a first row and the list store is not empty, the iter will be set, and Gtk::TreeModel#iter_first will return TRUE. If there is no first row, it will just return FALSE. If a first row exists, the while loop will be entered and we change some of the first row's data. Then we ask the model to make the given iter point to the next row, until there are no more rows, which is when Gtk::TreeIter#next! returns FALSE. Instead of traversing the list store we could also have used Gtk::TreeModel#each liststore.each { |model,path,iter| iter[0]= "Joe" } + + +((*Example employing some of the items discussed on this page:*)) + + #!/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, String) + + # 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