Created
September 21, 2011 08:06
-
-
Save geoffyoungs/1231530 to your computer and use it in GitHub Desktop.
Include multiple CellRenderers in a single TreeView column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# See http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3ATreeViewColumn | |
# for more details on Gtk::TreeViewColumn | |
require 'gtk2' | |
require 'base64' | |
win = Gtk::Window.new("Multiple cells in a column") | |
win.signal_connect('delete-event') { Gtk.main_quit } | |
loader = Gdk::PixbufLoader.new | |
loader.last_write(Base64.decode64(DATA.read)) | |
# Create model with data for our single column | |
model = Gtk::ListStore.new(String, Gdk::Pixbuf) | |
5.times do |x| | |
iter = model.append | |
iter[0] = "Text #{x}" | |
iter[1] = loader.pixbuf | |
end | |
view = Gtk::TreeView.new(model) | |
win.add(view) | |
# Using shorter constructor for clarity | |
column = Gtk::TreeViewColumn.new("One column") | |
pixbuf_renderer = Gtk::CellRendererPixbuf.new | |
# Gtk::TreeViewColumn#pack_start allows us to specify that extra space in the cell | |
# shouldn't be allocated to the pixbuf | |
column.pack_start(pixbuf_renderer, false) | |
# Set attributes so renderer knows where to find data | |
column.set_attributes(pixbuf_renderer, { :pixbuf => 1 }) | |
# Add second renderer to column & set attributes so it knows where to find data | |
text_renderer = Gtk::CellRendererText.new | |
column.pack_start(text_renderer, true) | |
column.set_attributes(text_renderer, {:text => 0}) | |
# Add column to Gtk::TreeView | |
view.append_column(column) | |
win.show_all | |
Gtk.main | |
__END__ | |
iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAnElEQVR42nWO | |
PQvCMBCGz4qdXF38GvRnKG7+/yVLFVfToFHbqGiTS3xPKNTBgwfecA/vhdqx | |
KttYRQFcwbqzoAt4gcjeJkEyeMtOBMfeBG6O6QdfSlslQokH/xEKuhWjlVU9 | |
z153BN2e2FK1Gy8gPGIMCVL8tvlTI/VgihP5EOEc2dUignvkp3zcgBwCZWAA | |
TL2fz9xhOUHWoE+YD9DvsTPOuAjZAAAAAElFTkSuQmCC | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment