Forum: Ruby-Gnome 2 Catching TAB presses in a TextView

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6ed4a727f8f6c71ca2f759008ec3febe?d=identicon&s=25 Kristoffer Lundén (stoffe)
on 2007-01-24 22:53
(Received via mailing list)
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. :)

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
✉ kristoffer.lunden@gmail.com
✉ kristoffer.lunden@gamemaker.nu
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...
2001ceff7e9dc753cf96fcb2e6f41110?d=identicon&s=25 Peter Johanson (Guest)
on 2007-01-24 23:08
(Received via mailing list)
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):

<snip>
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
</snip>

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...
6ed4a727f8f6c71ca2f759008ec3febe?d=identicon&s=25 Kristoffer Lundén (stoffe)
on 2007-01-24 23:27
(Received via mailing list)
On 1/24/07, Peter Johanson <peter@peterjohanson.com> 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
✉ kristoffer.lunden@gmail.com
✉ kristoffer.lunden@gamemaker.nu
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...
This topic is locked and can not be replied to.