Catching TAB presses in a TextView

Hello,

first, hope it’s ok to ask questions on this list. I couldn’t any
other appropriate place to ask, but if it isn’t ok just say so and I’m
gone. :slight_smile:

I would like to catch TAB presses when editing text in a Gtk::TextView
under certain circumstances, but I’m not sure how to go about it. What
I want to do is implement a rudimentary tab completion, nothing more
than inserting a whole word from a lookup table if there is some
matching text to the left of the cursor. I’ve experimented with some
different signals in the simple-editor sample, and I can detect TABs
but not stop them.

I suppose it might be possible to detect the TAB press, delete it from
the buffer and then insert the new stuff, but that seems a bit
backwards and may look strange(?)

So, just wondering, is there any clean way to catch the TAB being
pressed before the buffer is updated and cancel the insertion?

Many thanks,

– Kristoffer


Kristoffer Lundén
✉ [email protected]
✉ [email protected]
http://www.gamemaker.nu/
☎ 0704 48 98 77

Take Surveys. Earn Cash. Influence the Future of IT
Join 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

On Wed, Jan 24, 2007 at 10:53:04PM +0100, Kristoffer Lund??n wrote:

I would like to catch TAB presses when editing text in a Gtk::TextView
under certain circumstances, but I’m not sure how to go about it. What
I want to do is implement a rudimentary tab completion, nothing more
than inserting a whole word from a lookup table if there is some
matching text to the left of the cursor. I’ve experimented with some
different signals in the simple-editor sample, and I can detect TABs
but not stop them.

The following shows how to stop tabs, by connecting to the
key-press-event, and returning “TRUE”, which stops the event from
propogating further (meaning getting to Gtk::TextView at all):

require 'gtk2'

tv = Gtk::TextView.new

accept_tab = false
tv.signal_connect(‘key-press-event’) {
accept_tab = !accept_tab
!accept_tab
}

sw = Gtk::ScrolledWindow.new
sw.add(tv)

w = Gtk::Window.new(‘test’)
w.add(sw)
w.show_all
w.signal_connect(‘destroy’) { Gtk.main_quit }

Gtk.main

In particular, the ‘!accept_tab’ at the end of the one closure makes
only every other tab propogate into Gtk::TextView land.

HTH,

-pete


Take Surveys. Earn Cash. Influence the Future of IT
Join 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

On 1/24/07, Peter J. [email protected] wrote:

The following shows how to stop tabs, by connecting to the
key-press-event, and returning “TRUE”, which stops the event from
propogating further (meaning getting to Gtk::TextView at all):

Thanks a lot, that’s a really simple and nice solution!

In particular, the ‘!accept_tab’ at the end of the one closure makes
only every other tab propogate into Gtk::TextView land.

Actually, it prevents every other keystroke, but comparing the events
keyval to Gdk::Keyval::GDK_Tab works just right.

Again, thanks!

– Kristoffer


Kristoffer Lundén
✉ [email protected]
✉ [email protected]
http://www.gamemaker.nu/
☎ 0704 48 98 77

Take Surveys. Earn Cash. Influence the Future of IT
Join 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