Skip to main content

Posts

Showing posts from 2022

Making Runtime Proceedure Script Modifications.

When using Gnocl widgets only a single callback script is permitted. This differs somewhat to the underlying Gtk bindings api which allows for a chain of callback functions to be defined.   Rather than trying to produce a convoluted means of setting complex callbacks in C, simple changes can be made during application runtime because Tcl, as a dynamic language, allows everything to be reconfigurable. Using a simple procedure which takes three arguments: the name of the procedure to be modified, the script modifications and a flag specifying append or prepend, the following proof of concept script can created.   # !/bin/sh # the next line restarts using tclsh \ exec tclsh "$0" "$@" proc p1 {str} {  puts $str  } proc procModify {proc script {mode -append} } {     set args [info args $proc]     set body [string trim [info body $proc]]    set box      if { $mode == "-prepend" } {          set body $script\n$body      } else {           set body $body\n$scrip

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

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

Switching between IBUS input methods....

Its great having the input method controller embedded into the Gnome taskbar so that input methods can be changed when moving between input windows using a keyboard command sequence. It would be useful too, if its was possible to keep tabs on what language is supposed to be used in a text widget and have the Tcl script switch input methods automatically. Here's how to do it... # !/bin/sh # the next line restarts using tclsh \ exec tclsh "$0" "$@" package require Gnocl set chn [gnocl::text] set skt [gnocl::text] set vb [gnocl::vBox] $vb add $chn -fill 1 -expand 1 $vb add $skt -fill 1 -expand 1 gnocl::window -child $vb -setSize 0.4 $chn configure \ -onFocusIn  { eval exec "ibus engine pinyin"} \ -onFocusOut { eval exec "ibus engine xkb:gb:extd:eng" } $skt configure \ -onFocusIn  { eval exec "ibus engine m17n:sa:IAST" } \ -onFocusOut { eval exec "ibus engine xkb:gb:extd:eng" }

Using Tcl to trace changes to array variables

  The Tcl trace command can be used to respond to changes to variables occur, these changes are array, read, write and unset.  When one of these events occurs, a sub-script is execute and a number of values passed to it. In the case of an array operation, these are the array name, the member, and the operation. Following this it is then possible, with a bit of string splicing, to obtain the value of the value of the changed array member. Example: proc AAA {args} {     lassign $args a b c     puts [set ${a}($b)]         } trace add variable ::conze::editor::block write AAA set ::conze::editor::block(::gnocl::_WID26_,start) 5

Slow startup of Gnocl based applications in Ubuntu 20.4

 The recent changes to the Ubuntu 20.4 deployment sometimes sees slow startup times of between 25-60secs. Thankfully someone has solved the problem. I've downloaded the module and it works for me. https://itsfoss.community/t/solved-apps-opening-too-slow-in-ubuntu-20-04/4578/2 keith.myers 1 Apr '20 With help from a teammate in investigating the common problem with our apps, he discovered a cure by installing the deprecated gtk2 library module. Available in the 20.04 distro. All you need to do is to run this command: sudo apt install appmenu-gtk2-module and then rebooting. You need to reboot to get the new library working. This allows older applications based on the older menu API to function normally. Solved the issue with Boinc Manager and GKrellm config menu. Ref:  https://launchpad.net/ubuntu/bionic/+package/appmenu-gtk2-module  478