ruby-****@sourc*****
ruby-****@sourc*****
2012年 8月 14日 (火) 04:48:36 JST
------------------------- REMOTE_ADDR = 184.145.90.35 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-treeview-renderer-attributes ------------------------- @@ -33,3 +33,75 @@ Setting column attributes is the most straight-forward way to get your model data to be displayed. This is usually used whenever you want the data in the model to be displayed exactly as it is in the model. Another way to get your model data displayed on the screen is to set up ((<cell data functions|tut-treeview-renderer-celldatafunc>)). + +Following is an example showing how to programatically (manually) handle attributes, in particular ((*text, forground*)) and ((*foreground_set.*)) Again the program utiliyes ((*set_cell_data_func*)) method which we haven't covered yet, and those of you that find tis disturbing can ignore tge rest of this page on your first reading. + +((*Demo showing how to handle attributes:*)) + + #!/usr/bin/env ruby + require 'gtk2' + + 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 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 + + 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 last 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 = 2003 # To save code not relevent to the example + year_born = iter[2] + + if (year_born <= year_now) && (year_born > 0) + 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 + + window = Gtk::Window.new("Demo: Handling Attributes") + window.signal_connect("destroy") { Gtk.main_quit } + window.add(view) + window.show_all + Gtk.main