While I am not going into any detail or explanation on this subject right now, I will provide you with a sample application to get you started using a Gtk::TreeView and the Gtk::TreeStore objects ( Before this appears on the FAQ ).

require 'gtk2'
def add_node(tree, model )
select = tree.selection
if iter = select.selected
iter2 = model.append( iter )
iter2.set_value(0, "This is inserted data.")
end
end
def del_node(tree, model )
select = tree.selection
if iter = select.selected
model.remove( iter )
end
end
def get_depth( tree, model )
select = tree.selection
if iter = select.selected
@label.set_text( "Iter Depth: " << model.iter_depth( iter ).to_s )
end
end
def get_data(tree, model)
select = tree.selection
if iter = select.selected
data = model.get_value(iter, 0)
end
if data != nil
@label.set_text( "Data Value: #{data}" )
else
@label.set_text( '' )
end
end
def build_tree( tree, model )
root = model.append( nil )
root.set_value( 0, "Root" )
for i in 1..4
sub = model.append( root )
sub.set_value(0, "Foo#{i}" )
for i in 1..2
sub2 = model.append( sub )
sub2.set_value(0, "Bar#{i}" )
for i in 1..2
sub3 = model.append( sub2 )
sub3.set_value(0, "Spam#{i}" )
for i in 1..2
sub4 = model.append( sub3 )
sub4.set_value(0, "Eggs#{i}" )
end
end
end
end
@window.show_all
end
Gtk.init
@window = Gtk::Window.new( Gtk::Window::TOPLEVEL )
@window.set_size_request( 300, 300 )
@window.signal_connect( "delete_event" ) { Gtk.main_quit }
@window.set_border_width( 5 )
@window.set_title( "TreeStore Example" );
vbox2 = Gtk::VBox.new( false, 0 )
scroller = Gtk::ScrolledWindow.new
scroller.set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC )
@window.add( vbox2 )
vbox2.pack_start( scroller, true, true, 0 )
# Create the TreeStore with two columns
model = Gtk::TreeStore.new( String )
# Create the TreeView adding the TreeStore
tree = Gtk::TreeView.new( model )
# Create the renderer and set properties
render = Gtk::CellRendererText.new
render.set_property( "background", "black" )
render.set_property( "foreground", "green" )
# Create the columns
c1 = Gtk::TreeViewColumn.new( "Headings", render, {:text => 0} )
# append the columns to treeview
tree.append_column( c1 )
# add the treeview to the scroller
scroller.add( tree )
build_tree( tree, model )
## Button Frame
frame = Gtk::Frame.new( "Actions" )
vbox = Gtk::VBox.new( false, 0 )
frame.add( vbox )
vbox2.pack_start( frame, false, false, 0 )
## 1st row
hbox = Gtk::HBox.new( true, 0 )
button = Gtk::Button.new( "Expand all" )
button.signal_connect( "clicked" ) { tree.expand_all }
hbox.pack_start( button, true, true, 0 )
button = Gtk::Button.new( "Collapse all" )
button.signal_connect( "clicked" ) { tree.collapse_all }
hbox.pack_start( button, true, true, 0 )
vbox.pack_start( hbox, false, false, 0 )
## 2nd row
hbox = Gtk::HBox.new( true, 0 )
button = Gtk::Button.new( "Insert a Leaf" )
button.signal_connect( "clicked" ) {add_node(tree, model)}
button.signal_connect( "clicked" ) {}
hbox.pack_start( button, true, true, 0 )
button = Gtk::Button.new( "Remove Node/Leaf" )
button.signal_connect( "clicked" ) {del_node(tree, model)}
hbox.pack_start( button, true, true, 0 )
vbox.pack_start( hbox, false, false, 0 )
## 3rd row
hbox = Gtk::HBox.new( true, 0 )
button = Gtk::Button.new( "Remove All" )
button.signal_connect( "clicked" ) { model.clear }
hbox.pack_start( button, true, true, 0 )
button = Gtk::Button.new( "Add New Tree" )
button.signal_connect( "clicked" ) { build_tree(tree, model)}
hbox.pack_start( button, true, true, 0 )
vbox.pack_start( hbox, false, false, 0 )
## 4th row
hbox = Gtk::HBox.new( true, 0 )
button = Gtk::Button.new( "Get Data" )
button.signal_connect( "clicked" ) {get_data(tree, model)}
hbox.pack_start( button, true, true, 0 )
button = Gtk::Button.new( "Get Iter Depth" )
button.signal_connect( "clicked" ) {get_depth(tree, model)}
hbox.pack_start( button, true, true, 0 )
vbox.pack_start( hbox, false, false, 0 )
## Value Frame
frame = Gtk::Frame.new( "Values" )
@label = Gtk::Label.new( "")
frame.add( @label )
vbox2.pack_start( frame, false, false, 0 )
@window.show_all
Gtk.main
| Prev | Next |