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 ]
 $text configure -onChanged {
         puts "changed"
        $text tag apply {0 0} end -tags _tag_default
 }


 set _tag_rollOver(baseClr) blue
 set _tag_rollOver(highlight) red

 set j 0

 $text tag create _tag_default \
        -onEvent {
                set t %t
                set w %w
                if { $t == "motionNotify" } {
                        for { set i 1 } { $i <= $j } { incr i } {
                                $w tag configure _tag_rollOver_$i -foreground $_tag_rollOver(baseClr)
                        }
                }
        }

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

 $text insert end "This is " ;#-tags _tag_default
 $text insert end "Gnocl" -tags _tag_rollOver_$j
 $text insert end "! \n" ;#-tags _tag_default


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

 $text insert end "This is " ;#-tags _tag_default
 $text insert end "Gnocl" -tags _tag_rollOver_2
 $text insert end "! \n" ;#-tags _tag_default

 gnocl::window -title "Text TagRollover Effext" -child $text
 gnocl::mainLoop

enter categories here