Selection in TreeView

I have several columns in a row. The following code, which I’ve
borrowed from another post and modified, selects the row correctly, but
I, a Ruby newbie, don’t know how to query which row the user clicked on.
The line
puts "Selection was: "+widget.selection.selected[0]
outputs the value of column 0 in this case, but I don’t want to specify
the column. I want to know which column, as well as which row, that the
user clicked on. Thanks for any help.

#!/usr/bin/ruby
require ‘gtk2’
Gtk.init

SimpleTreeView.new

class SimpleTreeView

=== initialize

def initialize
@main_window = Gtk::Window.new
@main_window.set_size_request 555,255
create_parts
end

def create_parts
@renderer=Gtk::CellRendererText.new
@renderer.set_property( ‘background’, ‘lavender’ )
@renderer.set_property( ‘foreground’, ‘black’ )

@list_store  = Gtk::ListStore.new(String,String)
@tree_view = Gtk::TreeView.new(@list_store)
@tree_view_col1 = Gtk::TreeViewColumn.new('Col 1', @renderer, {:text

=> 0})
@tree_view.append_column(@tree_view_col1)
@tree_view_col2 = Gtk::TreeViewColumn.new(‘Col 2’, @renderer, {:text
=> 1})
@tree_view.append_column(@tree_view_col2)

# Example Data:
iter = @list_store.append
iter.set_value(0, 'a0')
iter.set_value(1, 'a1')
iter = @list_store.append
iter.set_value(0, 'b0')
iter.set_value(1, 'b1')
iter = @list_store.append
iter.set_value(0, 'c0')
iter.set_value(1, 'c1')

@main_window.signal_connect("destroy") { Gtk.main_quit }

@tree_view.signal_connect('button_release_event') do |widget,event|
  puts "Selection was: "+widget.selection.selected[0]
end

@main_window.add(@tree_view)
@main_window.show_all
Gtk.main

end
end
SimpleTreeView.new

Alan L. wrote:

I have several columns in a row. The following code, which I’ve
borrowed from another post and modified, selects the row correctly, but
I, a Ruby newbie, don’t know how to query which row the user clicked on.
snip
I said the opposite of what I meant. As this code shows, I know how to
get the selected row. What I don’t know (and would like to learn) is
how to determine which column within that row the user has clicked on.
Thanks for the help.


This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

Am Donnerstag, den 07.06.2007, 04:17 +0200 schrieb Alan L.:

Alan L. wrote:

I have several columns in a row. The following code, which I’ve
borrowed from another post and modified, selects the row correctly, but
I, a Ruby newbie, don’t know how to query which row the user clicked on.
snip
I said the opposite of what I meant. As this code shows, I know how to
get the selected row. What I don’t know (and would like to learn) is
how to determine which column within that row the user has clicked on.
Thanks for the help.

Hi,

i don’t know about a direct way, but if you replace your button-release
handler with:

@tree_view.signal_connect(‘button_release_event’) do |widget,event|
path, column, cell_x, cell_y = @tree_view.get_path_at_pos(event.x,
event.y)
p @tree_view.columns.index(column)
end

you get what you want. What does it? First it gets the TreeView::Column
at you button-release event. Than an array with all columns of the
treeview is requested and there is the column looked up.

Cheers
detlef


This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs