Friday, September 17, 2021

Creating icons from UTF-8 Characters.

Linux distros have heaps of pre-installed icons ready for use. I recently needed to create a toolbar menu which needed to access a set of unique icons which contained single characters. It was, in fact, a pull down menu for the insertion of 'special characters'. The Gtk+ api has complete functionality for creating icons from pixbufs and Gnocl providing convenient access. 

Here's a screenshot and the script.

 


 


# !/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"
package require Gnocl
if { [namespace exists jmls] == 0} {
    namespace eval jmls {}
}
set ::app(specialCharacters)  [list Section ¶ Paragraph § Separator • Left-Arrow ← Up-Arrow ↑ Right-Arrow → Down-Arrow ↓ Root √]
proc jmls::charIcon {name ch} {
    
    set pb1 [gnocl::pixBuf new -width 40 -height 40]
    $pb1 text \
        -position [list 15 30] \
        -font [list Sans normal 30] \
        -text $ch \
        -align centre \
        -color white
    gnocl::stockItem add \
        -label $name \
        -icon "%?$pb1"
    $pb1 delete
}
proc jmls::tbarSpecialCharacterMenu {tbar {script ""} } {
    if { $script == "" } {set script {puts $::app(spChar)} }
    set menu [gnocl::menu]
    set mb [$tbar add menuButton \
        -icon %#New -text "Char" \
        -menu $menu \
        -tooltip "Insert special character into active text" \
        -arrowTooltip "Select Character" \
        -onClicked $script ]
    foreach {a b} $::app(specialCharacters) {
        jmls::charIcon $a $b
        $menu add [gnocl::menuRadioItem \
            -text "<big>$b</big>\t<small>($a)</small>" \
            -onValue $b \
            -variable ::app(spChar) \
            -data [list $mb configure -icon %#$a ] \
            -onToggled {eval %d}]
    }
    $mb configure -icon %#[lindex $::app(specialCharacters) 0]
}
set tbar [gnocl::toolBar ]
jmls::tbarSpecialCharacterMenu $tbar
set box [gnocl::box -orientation vertical -borderWidth 0 -spacing 0]
$box add $tbar
gnocl::window  -child $box -setSize 0.2
gnocl::mainLoop


September updates

August saw a few modifications to the core, largely in the form of resolving some minor issues adding some utility functionality.

 

2021-08:
    gnocl::menu
        o bug in configure -tearoff now fixed.
    gnocl::notebook
        o new tab option -tooltip
    gnocl::text
        o fixed problems with -onButtonPress/Release signal handling.
          See gnoclConnectOptCmd.
    gnocl::pointer
        o new subcommand, monitor.
    gnocl::timeOut
        o now works properly.
          Use as an alternative to Tcl after which does not work within a Gtk.
    %c (child) substitution string added to -onKeyPress/Release callbacks
          for GtkBin objects (e.g., toplevel windows).
    gnocl::text
        o functionality of getPos subcommand now enhanced.
          $text-wid getPos <position> <offset>
              position, row column pair in list,
              offset, any relative keyword, e.g. wordStart, wordEnd
          Omitting a position and/or keyword option will result in
          returning a buffer position based upon the current mouse pointer position in the widget