# Activating multiple rows in a TreeView

**URL:** <https://www.ruby-forum.com/t/activating-multiple-rows-in-a-treeview/70558>\
**Category:** Ruby-Gnome 2\
**Created:** [September 30, 2006, 3:32am UTC](https://www.ruby-forum.com/t/activating-multiple-rows-in-a-treeview/70558 "2006-09-30T03:32:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![subroutine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@subroutine](https://www.ruby-forum.com/u/subroutine)\
**Post date:** [September 30, 2006, 3:32am UTC](https://www.ruby-forum.com/t/activating-multiple-rows-in-a-treeview/70558/1 "2006-09-30T03:32:54Z")

</div>

Hi,

When the ‘row-activated’ signal gets fired in a TreeView, either by  
pressing  
Enter or double-clicking, the selection gets reduced to the currently  
focused  
row, i.e. any other rows that were previously selected get deselected.

Is there some way to avoid this, so I can get the list of selected rows  
in  
the row-activated handler? PyGTK supports this directly by passing a  
list  
of selected rows to the handler, but it looks like in Ruby-GTK this  
isn’t  
possible for some reason.

Cheers,  
Markus

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys – and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)

---

<div class="post-metadata">

**Author:** ![subroutine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@subroutine](https://www.ruby-forum.com/u/subroutine)\
**Post date:** [September 30, 2006, 11:03am UTC](https://www.ruby-forum.com/t/activating-multiple-rows-in-a-treeview/70558/2 "2006-09-30T11:03:59Z")

</div>

Hi,

In [20060929180244.GA5145@fnord](mailto:20060929180244.GA5145@fnord)  
“[ruby-gnome2-devel-en] Activating multiple rows in a TreeView” on  
Fri, 29 Sep 2006 20:02:44 +0200,  
Markus K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there some way to avoid this, so I can get the list of selected rows in  
> the row-activated handler?

What about the following script:

#!/usr/bin/env ruby

require ‘gtk2’

window = Gtk::Window.new

model = Gtk::TreeStore.new(String)

tree\_view = Gtk::TreeView.new  
tree\_view.model = model  
tree\_view.selection.mode = Gtk::SELECTION\_MULTIPLE

prev\_selected = []  
selected = []  
tree\_view.selection.signal\_connect(“changed”) do |selection|  
prev\_selected, selected = selected, selection.selected\_rows  
false  
end  
tree\_view.signal\_connect(“row-activated”) do |widget, path, column|  
p prev\_selected  
false  
end

cell = Gtk::CellRendererText.new  
column = Gtk::TreeViewColumn.new(“column”, cell, “text” =\> 0)  
tree\_view.append\_column(column)

10.times do |i|  
iter = model.append(nil)  
iter.set\_value(0, i.to\_s)  
end

window.add(tree\_view)  
window.show\_all

Gtk.main

> ```
> PyGTK supports this directly by passing a list
> 
> ```
> 
> of selected rows to the handler

Really? Could you show me a example for that?

## Thanks,

kou

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys – and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)

---

<div class="post-metadata">

**Author:** ![subroutine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@subroutine](https://www.ruby-forum.com/u/subroutine)\
**Post date:** [October 1, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/activating-multiple-rows-in-a-treeview/70558/3 "2006-10-01T15:28:45Z")

</div>

Hi (sorry for the broken thread, I wasn’t subscribed to the list yet)

> > PyGTK supports this directly by passing a list of selected rows to the handler  
> > Really? Could you show me a example for that?

You’re script does the trick, though it doesn’t work when  
double-clicking, and it  
doesn’t restore the selection afterwards. But after rewriting it in  
Python I  
realized that PyGTK has the same behaviour.

The reason I assumed PyGTK handles this is that I saw it in the code of  
the  
Python music player Quodlibet, but I just now realized that they use a  
custom  
TreeView which implements that behaviour (see the MultiDragTreeView in  
[http://www.sacredchao.net/quodlibet/browser/trunk/quodlibet/qltk/views.py](http://www.sacredchao.net/quodlibet/browser/trunk/quodlibet/qltk/views.py)).

So, I guess I’ll try to rebuild that class in ruby.  
But thanks for your answer, anyway!

Here’s the python version of your script:

#!/usr/bin/env python

import gtk

window = gtk.Window()

model = gtk.TreeStore(str)

tree\_view = gtk.TreeView(model)  
tree\_view.get\_selection().set\_mode(gtk.SELECTION\_MULTIPLE)

prev\_selected = []  
selected = []  
def changed(selection):  
global prev\_selected, selected  
prev\_selected = selected  
model, selected = selection.get\_selected\_rows()  
tree\_view.get\_selection().connect(“changed”, changed)  
def row\_activated(widget, path, column):  
global prev\_selected  
print prev\_selected  
False  
tree\_view.connect(“row-activated”, row\_activated)

cell = gtk.CellRendererText()  
column = gtk.TreeViewColumn(“column”, cell)  
column.add\_attribute(cell, “text”, 0)  
tree\_view.append\_column(column)

for i in range(0, 10):  
model.append(None, row=[str(i)])

window.add(tree\_view)  
window.show\_all()

gtk.main()

Cheers  
Markus

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys – and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)
