public
Created

Include multiple CellRenderers in a single TreeView column

  • Download Gist
pack-cell-renderers.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#!/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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.