Skip to main content

Posts

Showing posts from June, 2022

Text chopping and glueing.

      # !/bin/sh # the next line restarts using tclsh \ exec tclsh "$0" "$@" package require Gnocl namespace eval text {} ## split text into enumerated list of paragraphs # @param    txt        block of text to split # @returns    enumerated list of paragraphs. proc text::paras { txt } {     set i 0     foreach line [split $txt \n] {         if { [string is space $line] } {             incr i         } else {             dict append res $i "$line "         }       }     return $res } ## split text into blocks based upon puncutation marks # @param    txt        block of text to split # @param    marks    valid list of punctuation marks # @returns    enumerated list of blocks. proc text::blocks {txt marks} {     set i 0     foreach ch [split $txt ""] {         if { [string first $ch $marks] != -1 } { incr i}         dict append res $i $ch         if { [string first $ch $marks] != -1 } { incr i}     }     return $res } ## concatenate enumerated list

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