Skip to main content

Posts

Text chopping and glueing.

Recent posts

ToggleButton Background Colour Changes

Earlier versions of the Gtk2 libraries allowed for the runtime setting/overriding of default widget colour properties. Legacy Tcl/Gnocl scripts can't rely on such settings to work with later release perhaps because of the shift towards the use of CSS to define widget style properties.

Converting Between Colour Formats: hex, 8-bit, 16-bit and float.

The gnocl package has its own C-based conversion commands but here are the pure Tcl equivalents.     # !/bin/sh # the next line restarts using tclsh \ exec tclsh "$0" "$@" package require Gnocl # colour in hexadecimal set clr #8B6914 puts $clr proc hex2rgb { hex {opt -8bit} } {     if { [lsearch "-8bit -16bit -float" $opt] == -1 } {         set msg "Error. Wrong option $opt.\nValid options are: -8bit (default) -16bit, or -float."         return -code error $msg     }     # convert to 8bit values     scan $hex "#%02x%02x%02x" r g b     switch $opt {         -16bit {             # convert to 0-65535             if {$r eq 0} {set r 0} {set r [expr int ( ($r.0/255)*65535 )]}             if {$g eq 0} {set g 0} {set g [expr int ( ($g.0/255)*65535 )]}             if {$b eq 0} {set b 0} {set b [expr int ( ($b.0/255)*65535 )]}             }         -float {             # convert 0-1.0             if {$r eq 0} {set r 0.0} {set r [expr $r.0/255]}

Converting a Pango Markup String into an Enumerated List

Needed something to breakup a string into  a simple list.  My first thought was to use arrays but then thought about using the dict command. This will produce an enumerated list maintaining the creation order of the extracted text markup substrings. set str "abc deg ghi <b>Bold</b> italic <i>italic</i>" ## parse markup string string into an enumerated list of text and tags # @param   str # @returns enumerated list proc parseMarkupStr {str} {     set idx 0     set res ""     set t 0     foreach ch [split $str {}] {         # detect markup start and end         if { $ch == "&lt;" } {             set t 1             incr idx }         if { $ch == "&gt;" } {             set res [dict append res $idx $ch]             set t 0             incr idx             continue }         set res [dict append res $idx $ch]     }     return $res } puts [parseMarkup $str] 0 {abc deg ghi } 1 <b> 2 Bold 3 </b> 4 { italic }

Retrieving Invisible Text

 One of the many options of the text widget tag is the boolean -invisible property. When used, it must be born in mind that it doesn't merely affect the on-screen visibility of the contents of the text buffer to which the tag set with this option applies, but to the retrieval of the text too. The text widget get comment, returns the visible contents of the text view, if the actual contents of the displayed text buffer are needed, then the dump command should be used.  So, to return the visible contents of the text view use: $wid get But to return the entire text content of the text buffer, use: $wid dump text start end

Tabulate

 Simple way of inserting beautified tables into the gnocl::text widget. # !/bin/sh # the next line restarts using tclsh \ exec tclsh "$0" "$@" package require Gnocl ## insert the list contents as in text widget as a formatted table # # @param    wid        text widget-id # @param    lst        tcl list # @param    id        table id # @param    args    additional tag parameters # proc tabulate {wid lst {id 1} args} {     if {$args == ""} { set args "-tabs 100" }     foreach row $lst { append res [join $row \t]\n }     $wid tag create _tab_table_$id {*}$args     $wid insert end $res -tags _tab_table_$id } set txt [gnocl::text] gnocl::window -child $txt -setSize 0.4 set lst(1) {{red orange yellow green blue indigo violet} {magenta cyan yellow brown grey black white}} set lst(2) {{how now brown cow} {she sells sea shells by the sea shore}} set lst(3) {{peter piper picked a peck of pickled peppers} {the peck of peppers peter piper picked}} t

Home directory for a script...

If an application has a series of Tcl script packages loaded with the source command, then any subsequent calls made by those scripts will potentially loose track of the path to find associated data files. This may not be a problem if the application will always use absolute addressing, ie. be run from its home directory, but if the application is located in the execution path, downstream proceedure calls and scripts may not know where to find obtain resources. The way around this is to keep a note of the path where the main application script is located as soon as the script begins. # !/bin/sh # the next line restarts using tclsh \ exec tclsh "$0" "$@" set ::app(HOME) [file dirname [file normalize [info script]]] set ::app(demo) 0 set ::app(ibus,defaultIM) [exec ibus engine] source $::app(HOME)/editor_widget.tcl source $::app(HOME)/dictionary/dictionary.tcl source $::app(HOME)/translationEngine/translationEngine.tcl source $::app(HOME)/translationEngine/teGUI.tcl s