Re: text tag binding events
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: text tag binding events

From: Kaitzschu <kaitzschu@kaitzschu.cjb.net.nospam.plz.invalid>
Date: Sat Oct 29 2005 - 14:27:25 CEST

On Sat, 29 Oct 2005, William J Giddings wrote:

> If I add a tag the content of a text widget and then add some bindings,
> the mouse events are easily picked up but not the key events. What needs
> to be done to the code below to ensure that the Key binding occurs as
> expected.

It is practically un-doable (at least before someone cranks widget a big
time), [text] tag events occur only at mouse pointer, so you'd have to do
something like
    event generate . <Motion> \
          -warp 1 \
          -x [lindex [.txt bbox 3.1] 0] \
          -y [lindex [.txt bbox 3.1] 1]; focus -force .txt
but that is not, I repeat, not a good thing to do (1: users don't like
their mouse cursor being warped; 2: users don't like windows taking focus
without a reason; 3: Windows won't probably even let you focus like that;
4 (this is the worst): finding that index where to warp is major trouble).

So don't bind key-events to tags, they'll just let you down. Instead, bind
key-events to [text] itself and every time event fires, you check whether
[.txt tag names insert] includes your "bound" tag. Something like

pack [text .txt] -fill both
.txt insert end "aaaaa\nbbbbb\nccccc\nddddd\eeeeee"
.txt tag add cc 3.0 "3.0 lineend"
.txt tag configure cc -background pink
bind .txt <Key> { helper_proc %W }
.txt tag bind cc <Button> { puts BUTTON-EVENT }

proc helper_proc {wid} {
  if {{cc} ni [$wid tag names insert]} {return -code continue}
  puts KEY-EVENT
}

This one has a slight problem, that when insertion cursor is on the right
side of last tagged character, event doesn't fire. Making that happen (if
it is needed) is left as exercise, just as converting this to work with
8.4 series (hint: knights say so).

-- 
-Kaitzschu
s="TCL ";while true;do echo -en "\r$s";s=${s:1:${#s}}${s:0:1};sleep .1;done
Received on Mon Nov 21 00:36:59 2005