Saturday, October 19, 2013

gnocl::text tag rollover effects

Suprisingly, to me at least, tag rollover is not standard feature of the GtkTextTag. I've had Tcl a solution for some time now but always felt that this needed to be moved in to the package code itself. Whilst implementing rollover effects isn't difficult, matters would be made easier if the widget internals offered tag signals which tracked motion in and out of the tag rather than giving the simpler 'event' event binding. Well, its sorted now and the following test script illustrates how it works. There's a little more tweeking that can be done to the source code to allow similar changes to the rollover tag background colour. Finally,  runtime changes to the rollover colour setting (default = red) for the script will affect all instances of text widgets within a script.

# tagRollover.tcl

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

package require Gnocl

set text [gnocl::text ]

$text configure -rollOverColor red

# whenever the pointer moves, clear any rollOver highlight
 $text configure -onMotion { #puts MOTION }


for {set j 0} {$j <= 5 } {incr j} {
 # create new, rollOver tag
 $text tag create _tag_rollOver_$j -rollOver 1 -fontWeight bold -foreground blue -onEvent {puts "HELLO!"}

 # use it
 $text insert end "This is "
 $text insert end "Gnocl tag $j" -tags _tag_rollOver_$j
 $text insert end "! \n"
}

gnocl::window -title "Text TagRollover Effects" -child $text -setSize 0.25
gnocl::mainLoop