WJG (18/10/08) The Gtk way of event handling within text widgets is slightly different than in Tk. This, however, doesn't mean that we can can't get the same effects. One such feature commonly encountered in Tk applications is rollover effects on tagged text. Gtk has excellent support for signals but not all of these are inhereted completely. In some of my applications I like differentiate tagged text as the mouse pointer skims across a window. Ok, its not absolutely necessary but, I 'grew-up' with Tk and like its features. So, here's the way to achieve the same effect responding to the Gtk motionNotify signal. It is not an perfect solution, there is no means of determining whether a tagged are is 'entered' or 'left', simply if the tag has changed. So, its necessary to tag all text as it is inputted.

Here's the code:


 # ---------------
 # tagRollover.tcl
 # ---------------
 #!/bin/sh
 # the next line restarts using tclsh \
 exec tclsh "$0" "$@"

 package require Gnocl

 set text [gnocl::text ]

 # create the required tag colour constants
 set _tag_rollOver(baseClr) blue
 set _tag_rollOver(highlight) red

 # create the tags
 $text tag create _tag_default \
        -onEvent {
                set t %t
                set w %w
                if { $t == "motionNotify" } {
                        $w tag configure _tag_rollOver -foreground $_tag_rollOver(baseClr)
                }
        }

 $text tag create _tag_rollOver \
        -fontWeight bold \
        -foreground $_tag_rollOver(baseClr) \
        -onEvent {
                set t %t
                set w %w
                if { $t == "motionNotify" } {
                        $w tag configure _tag_rollOver -foreground $_tag_rollOver(highlight)
                }
        }

 # ensure that '''all''' new text is tagged properly.
 $text configure -onChanged {
        $text tag apply {0 0} end -tags _tag_default
 }

 # some sample text
 $text insert end "This is " -tags _tag_default
 $text insert end "Gnocl" -tags _tag_rollOver
 $text insert end "!\n" -tags _tag_default

 # display the widget
 gnocl::window -title "Text TagRollover Effext" -child $text
 gnocl::mainLoop

There are limitations with this of course. If more that one section is tagged with the _tag_rollOver tag, then all of these will be simultaneously highlighted.


enter categories here