# Bug in Gtk::TreeView#set\_cursor?

**URL:** <https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042>\
**Category:** Ruby-Gnome 2\
**Created:** [November 2, 2007, 2:44pm UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042 "2007-11-02T14:44:48Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![joachimgross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@joachimgross](https://www.ruby-forum.com/u/joachimgross)\
**Post date:** [November 2, 2007, 2:44pm UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042/1 "2007-11-02T14:44:48Z")

</div>

Below an example program in which I try to make something like an  
editable grid.

If you edit a field and press Enter or Tab, it should start editing the  
next cell to the right.  
From the documentation:  
[http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3ATreeView#set\_cursor](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3ATreeView#set_cursor)

"Additionally, if column is specified, and start\_editing is true, then  
editing should be started in the specified cell. "

I tried this in method connect\_edit. I supplied both, the TreePath and  
the “next” TreeViewColumn. Regardless how if the third option is true or  
false, the next cell will only start editing after pressing spacebar.

Anyone has an idea what’s wrong?

* * *

#!/usr/bin/ruby

require ‘gtk2’

def connect\_edit(renderer,i,view)  
renderer.signal\_connect(“edited”) do |rendr, row, new\_text, model|  
iter = view.model.get\_iter(row)  
iter[i] = new\_text  
view.set\_cursor(Gtk::TreePath.new(row.to\_s),view.get\_column(i+1),true)  
# if 3rd option ‘start\_editing’ set to false it has the same effect  
view.grab\_focus  
end  
end

liststore = Gtk::ListStore.new(String, String, String, String, String,  
String)  
5.times do  
iter = liststore.append  
i = 0  
6.times do  
iter[i] = “bla”  
i = i + 1  
end  
end

view = Gtk::TreeView.new(liststore)

i = 0  
[“foo”,“bar”,“blah”,“more”,“spaceholder”,“stuff”].each do |c|

renderer = Gtk::CellRendererText.new  
renderer.editable = true  
connect\_edit(renderer,i,view)

col = Gtk::TreeViewColumn.new(c, renderer, :text =\> i)  
view.append\_column col  
i = i + 1  
end

w = Gtk::Window.new  
w \<\< view  
w.show\_all  
Gtk.main

---

<div class="post-metadata">

**Author:** ![joachimgross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@joachimgross](https://www.ruby-forum.com/u/joachimgross)\
**Post date:** [November 2, 2007, 3:27pm UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042/2 "2007-11-02T15:27:15Z")

</div>

hi, i looked at it, we share common interests 🙂  
the attached one works. i disabled the grab.

---

<div class="post-metadata">

**Author:** ![joachimgross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@joachimgross](https://www.ruby-forum.com/u/joachimgross)\
**Post date:** [November 2, 2007, 3:33pm UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042/3 "2007-11-02T15:33:25Z")

</div>

Dobai-Pataky BÃ¡lint wrote:

> hi, i looked at it, we share common interests 🙂  
> the attached one works. i disabled the grab.

Interestingly it works if you use Enter-Key. There is some wired  
behaviour when using the tab key for changing columns. Try it out.

---

<div class="post-metadata">

**Author:** ![joachimgross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@joachimgross](https://www.ruby-forum.com/u/joachimgross)\
**Post date:** [November 2, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042/4 "2007-11-02T19:50:55Z")

</div>

Dobai-Pataky BÃ¡lint wrote:

> hmm, yeah.  
> but at least we know now: treeview.grab stops the editing, which was  
> started by set\_cursor

The question is why. The manual says  
“This method is often followed by Gtk::Widget#grab\_focus in order to  
give keyboard focus to the widget.”

So, what, instead of the treeview, should I focus to get the next cell  
focused (must be not stop editing)?

---

<div class="post-metadata">

**Author:** ![joachimgross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@joachimgross](https://www.ruby-forum.com/u/joachimgross)\
**Post date:** [November 2, 2007, 5:50pm UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042/5 "2007-11-02T17:50:40Z")

</div>

hmm, yeah.  
but at least we know now: treeview.grab stops the editing, which was  
started by set\_cursor

---

<div class="post-metadata">

**Author:** ![joachimgross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@joachimgross](https://www.ruby-forum.com/u/joachimgross)\
**Post date:** [November 2, 2007, 10:29pm UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042/6 "2007-11-02T22:29:59Z")

</div>

i have edited your script to the point where i found out what is  
happening, but i lost it :(.  
basically i disabled the treeview’s focusing ability, thus it did not do  
the tab trick(but did not focus on cells).  
i changed your TreeView.grab\_focus to  
nextCellRenderer\_to\_edit.grab\_focus (this solved the problem of  
grab\_focus forces the CellRenderer to exit editing mode)  
and i hooked the editing-canceled and editing-started signals to see  
what is happening, and i found out that when that strange behavior  
occurs it’s because of a premature editing-canceled event is called on  
the second CellRenderer.

and i concluded that if you want to imitate an excel-like grid object  
than you’ll end up writing your own CellRenderer object, better your own  
TreeView, which will not be a TreeView, but probably a Gtk::Layout or a  
Table with Labels/Entries(in editing mode) on it.  
for one reason, TreeView has a row selection mechanism, and it’ll not  
emit the editing signal to the CellRenderer if the row is not selected  
first, which means at least two mouse clicks to enter a Renderer’s  
editing mode which was not in the selected row.

---

<div class="post-metadata">

**Author:** ![joachimgross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@joachimgross](https://www.ruby-forum.com/u/joachimgross)\
**Post date:** [November 2, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042/7 "2007-11-02T23:12:28Z")

</div>

Dobai-Pataky BÃ¡lint wrote:

> i changed your TreeView.grab\_focus to  
> nextCellRenderer\_to\_edit.grab\_focus (this solved the problem of  
> grab\_focus forces the CellRenderer to exit editing mode)  
> Huh? How did you do that, since the CellRenderer is not a Gtk::Widget  
> …?

> and i concluded that if you want to imitate an excel-like grid object  
> than you’ll end up writing your own CellRenderer object, better your own  
> TreeView, which will not be a TreeView, but probably a Gtk::Layout or a  
> Table with Labels/Entries(in editing mode) on it.  
> I suppose that won’t be a trivial task using a Gtk::Layout and create a  
> Treeview “from the scratch”.

---

<div class="post-metadata">

**Author:** ![joachimgross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@joachimgross](https://www.ruby-forum.com/u/joachimgross)\
**Post date:** [November 3, 2007, 12:55am UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042/8 "2007-11-03T00:55:38Z")

</div>

Joachim G. wrote:

> Dobai-Pataky BÃ¡lint wrote:
> 
> > i changed your TreeView.grab\_focus to  
> > nextCellRenderer\_to\_edit.grab\_focus (this solved the problem of  
> > grab\_focus forces the CellRenderer to exit editing mode)
> 
> Huh? How did you do that, since the CellRenderer is not a Gtk::Widget  
> …?

sorry, i wasn’t precise.  
[http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3ACellRenderer#editing-started%3A+self%2C+editable%2C+path](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3ACellRenderer#editing-started%3A+self%2C+editable%2C+path)  
i figured the editable is the widget which appears when a CellRenderer  
enters editing mode.  
that’s a widget, and can grab\_focus.

---

<div class="post-metadata">

**Author:** ![joachimgross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@joachimgross](https://www.ruby-forum.com/u/joachimgross)\
**Post date:** [November 3, 2007, 11:22am UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042/9 "2007-11-03T11:22:19Z")

</div>

Ok, I think I got the state you were describing now, see attachment.

When pressing tab without a change, editing-canceled is emitted shortly  
after editing-started is.

When changing the text, the behaviour gets more strange and I get  
outputs like  
GLib-GObject-WARNING  
\*\*:/build/buildd/glib2.0-2.14.1/gobject/gsignal.c:1741: instance  
`0x8405460' has no handler with id`632’

When uncommenting the line  
view.grab\_focus = false

Tab doesn’t work anymore.

The editable widget which is created by Gtk::CellRendererText is a  
Gtk::Entry, so since the Entry is a Gtk::Widget I could catch a  
key-press-event and check for the Tab key… what do you think?

I wonder if I can change the widget which is spawned by the  
CellRendererText or if I need to write my own CellRenderer?

---

<div class="post-metadata">

**Author:** ![joachimgross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@joachimgross](https://www.ruby-forum.com/u/joachimgross)\
**Post date:** [November 3, 2007, 2:17pm UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042/10 "2007-11-03T14:17:32Z")

</div>

Joachim G. wrote:

> Ok, I think I got the state you were describing now, see attachment.

there is none.

> When pressing tab without a change, editing-canceled is emitted shortly  
> after editing-started is.

yup

> When changing the text, the behaviour gets more strange and I get  
> outputs like  
> GLib-GObject-WARNING  
> \*\*:/build/buildd/glib2.0-2.14.1/gobject/gsignal.c:1741: instance  
> `0x8405460' has no handler with id`632’

yup

> When uncommenting the line  
> view.grab\_focus = false
> 
> Tab doesn’t work anymore.

yup, becouse TreeView is not changing it’s childs’ focus.

> The editable widget which is created by Gtk::CellRendererText is a  
> Gtk::Entry, so since the Entry is a Gtk::Widget I could catch a  
> key-press-event and check for the Tab key… what do you think?

i agree, i thought of that. something like this(i haven’t tested it):

renderer.signal\_connect(“editing-started”){|me,ewidget,pa|  
ewidged.signal\_connect(“key-release-event”){|me,event|  
if event.keyval == Gdk::Keyval::Gdk\_Tab  
}

> I wonder if I can change the widget which is spawned by the  
> CellRendererText or if I need to write my own CellRenderer?

i don’t know anything about writing a CellRenderer, but i’m interested  
in it (i’d need a CellRendererButton).

---

<div class="post-metadata">

**Author:** ![joachimgross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@joachimgross](https://www.ruby-forum.com/u/joachimgross)\
**Post date:** [November 3, 2007, 4:08pm UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042/11 "2007-11-03T16:08:39Z")

</div>

i think i solved the problem.  
you should jump to the next column, only when Enter was pressed on the  
editable\_widget.  
that will not change the Treeview’s behavior, will not jump forward on  
editing-cancelled ny any mode(ESC or TAB)

---

<div class="post-metadata">

**Author:** ![joachimgross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@joachimgross](https://www.ruby-forum.com/u/joachimgross)\
**Post date:** [November 3, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042/12 "2007-11-03T21:03:37Z")

</div>

Joachim G. wrote:

> Dobai-Pataky BÃ¡lint wrote:
> 
> > i think i solved the problem.  
> > you should jump to the next column, only when Enter was pressed on the  
> > editable\_widget.
> 
> So as I understand you correctly you say that I should fix the problem  
> by not pressing Tab to get to the next column? But I want to…

try out my suggestion.  
here is a simplified version of the CellRenderer i’m talking about. -  
simplified on the fly, not tested, but you should get the idea.  
currently does not jump on tab, but should be not too hard to modify.  
please let me know, if/how you did it.

class MyCellRendererText \< Gtk::CellRendererText  
def initialize(view,model\_col\_num)  
@view=view  
@model\_col\_num=model\_col\_num  
@jump\_to\_next=false  
super()  
set\_editable\_set(true)  
set\_editable(true)  
signal\_connect(“edited”) do |me,path,new\_value|  
data\_iter=@view.model.get\_iter(path)  
if @jump\_to\_next  
if next\_col=view.get\_column(model\_col\_num+1)  
view.set\_cursor(Gtk::TreePath.new(path),next\_col,true)  
end  
@jump\_to\_next=false  
end  
true  
end  
signal\_connect(“editing-started”){|cell, editable\_widget, path|  
editable\_widget.signal\_connect(“key-press-event”){|me,event|  
@jump\_to\_next= Gdk::Keyval.to\_name(event.keyval) ==  
“Return”  
false  
}  
}  
end  
attr\_accessor :model\_col\_num  
attr\_accessor :view

end

---

<div class="post-metadata">

**Author:** ![joachimgross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@joachimgross](https://www.ruby-forum.com/u/joachimgross)\
**Post date:** [November 3, 2007, 6:34pm UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042/13 "2007-11-03T18:34:42Z")

</div>

Dobai-Pataky BÃ¡lint wrote:

> i think i solved the problem.  
> you should jump to the next column, only when Enter was pressed on the  
> editable\_widget.  
> So as I understand you correctly you say that I should fix the problem  
> by not pressing Tab to get to the next column? But I want to…

---

<div class="post-metadata">

**Author:** ![joachimgross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@joachimgross](https://www.ruby-forum.com/u/joachimgross)\
**Post date:** [November 4, 2007, 11:24pm UTC](https://www.ruby-forum.com/t/bug-in-gtk-treeview-set-cursor/119042/14 "2007-11-04T23:24:31Z")

</div>

> simplified on the fly, not tested, but you should get the idea.  
> I made it, thanks to your ideas. See attachment for code.

My renderer now looks like this  
class MyCellRendererText \< Gtk::CellRendererText  
def initialize  
super()  
set\_editable\_set(true)  
set\_editable(true)  
signal\_connect(“editing-started”){|cell, editable\_widget, path|  
editable\_widget.signal\_connect(“key-press-event”){|me,event|  
key = Gdk::Keyval.to\_name(event.keyval)  
if key == “Tab”  
me.editing\_done  
me.remove\_widget  
end  
false  
}  
}  
end  
end

The only thing I’ve not yet implemented is to jump to the next row/first  
column when finished editing of the last column. But that’s a trivial  
thing to do.

Maybe one could re-pack the edited signal to the renderer.
