Wednesday, August 22, 2012

gnocl::text new option -variable

All of the standard entry widgets have a -variable option much like those encountered in Tk. This means that setting the value of a widget will cause a named variable to contain a copy of this data. The idea of using the -variable option lies in the ease of setting application wide settings or the setting or obtaining  values from a data entry form. The only exception to this was the text widget.

I've been working on a forms based dictionary and glossary edit library which needed something more than the standard gnocl::entry. After hacking around with Tcl scripts to gather data from a text widget I decided to work on the C-side of things. This required a little bit of tweaking of the C sourcecode which, as it would turn out, was an easier approach than scripting. It should be pointed out, however, that only plain unicode text will be copied. Any tags or invisible text will not be captured.

Here's the test script I used to work through the implementation.

#---------------
# test-textVar.tcl
#---------------
#!/bin/sh
#\
exec tclsh "$0" "$@"

package require Gnocl

set box [gnocl::box -orientation vertical]
 

set but [gnocl::button \
     -text Press \
     -onClicked { 
           puts $var
           set var "How now brown cow." }]

set txt [gnocl::text -variable var -baseFont {Sans 12} -wrapMode word]

$box add $but -fill {0.25 0} -align left
$box add $txt -fill {1 1} -expand 1

gnocl::window -child $box -setSize 0.25
set var "hello world!"

No comments: