Wednesday, December 19, 2012

gnocl::cairo instropection

The gnocl::cairo command is not intended to be some form of canvas object, but a means of drawing onto images for display on screen used perhaps in the creation of widgets or images. Bearing in mind the relatively small number of actions required for this task, it's reasonable to assume that the overhead of making multiple calls to the gnocl::cairo command is a worthwhile compromise.

In response to the problem of how to get the extents of rendered text back from Cairo I came up with the solution highlighted in the following code:


set pb(1) [gnocl::pixBuf new -width 256 -height 256]
set myString "Peter Piper picked a peck of pickled peppers."
set actions "
    select_font_face {Sans normal bold}
    set_font_size 15.0
    text_extents $myString"  
array set extents [gnocl::cairo $pb(1) -actions $actions]


In the example above, the following values were obtained.

extents(height)    = 14.0
extents(width)     = 320.0
extents(x_advance) = 322.0
extents(x_bearing) = 1.0
extents(y_advance) = 0.0
extents(y_bearing) = -11.0



Tuesday, December 18, 2012

New Pacakge gnocl::cairo

The process of tidying up the development code prior to a release increment has led me to weeding out some unused or difficult to maintain functions. One of these was the cairo.c module. This was experimental block of code which was an offshoot of developing the gnocl::splashscreen widget.  But, with the transition to Gtk3 on the horizon, all drawing operations will be performed by Cairo and so I thought that it was a good opportunity to make use of some of that old code. Hence, a new package. There's still way to go before it's finished and I'm still working on drawing to pixbufs but things are looking good. Here's the syntax for the command:

gnocl::cairo <drawable-id> -actions <script>

Where the script is simply a list of cairo operations.  Here's a snippet from my test script.

set actions "
# {sample set-line-join}
set_line_width 40.96
move_to {76.8 84.48}
rel_line_to {51.2 -51.2}
rel_line_to {51.2 51.2}
set_line_join miter
stroke

move_to {76.8 161.28}
rel_line_to {51.2 -51.2}
rel_line_to {51.2 51.2}
set_line_join bevel
stroke

move_to {76.8 238.08}
rel_line_to {51.2 -51.2}
rel_line_to {51.2 51.2}
set_line_join round
stroke
"
set pb(1) [gnocl::pixBuf new -width 512 -height 512]
gnocl::cairo $pb(1) -actions "$actions #"


This gives:



There will be some limitations. It's not possible to create several surfaces in advance and then to use them as required. There is only one surface and will be necessary to change its contents. I don't envisage implementing any introspection either, keeping track of what's taking place on the Cairo side of things will need to be handled Tcl-side. One change that I did make was to allow for angles to be specified in degrees rather than radians. I could make this optional with a -useDegree switch but that can be dealt with later.

Here's a list of the Cairo operations implemented so far. The names are derived from the Cairo api for which these are wraps.

        "image_surface_create_from_png",
        "paint",
        "line_to",
        "set_line_cap",
        "set_line_join",
        "rel_line_to",
        "rectangle",
        "arc",
        "arc_negative",
        "curve_to",
        "rel_curve_to",
        "stroke",
        "stroke_preserve",
        "set_source_rgb",
        "set_source_rgba",
        "fill",
        "fill_preserve",
        "set_dash",
        "set_line_width",
        "move_to",
        "pattern_create_linear",
        "pattern_add_color_stop_rgb",
        "pattern_add_color_stop_rgba",
        "set_source",
        "set_source_surface",
        "surface_destroy",
        "pattern_destroy",
        "select_font_face",
        "font_size",
        "show_text",
        "new_path",
        "close_path",
        "clip",
        "new_sub_path",
        "set_fill_rule",
        "scale",
        "rotate",
        "translate",
        "#"

Tuesday, November 06, 2012

gnocl::button -align

Yesterday I was playing with the Glade interface designer and noticed that the UI included buttons with icons whose contents were left aligned. The Gnocl default is centred and I thought that a gnocl::button -align option might be useful. The button icon and label sub-widgets are in turn held within a GtkHBox and positioned by a GtkAlignment container. It was a relatively simple task to create the code to add the alignment option. For the present, plain text will remain centred. Here's my test script.

# test-buttonAlign.tcl
#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

package require Gnocl

proc gnocl::buttonBox { {str <<TITLE>>} b} {
    set box(1) [gnocl::box -orientation vertical -padding 0 ]
    set but(1) [gnocl::button -icon %#Remove -text $str -align centre]
    set box(2) [gnocl::box -orientation vertical -padding 0 ]

    $box(1) add $but(1) -fill {1 1}
    $box(1) add $box(2)
    $box(2) add $b

    $box(1) configure -data "button $but(1) box %box(2)"
    $but(1) configure -data [::list state 1 child $box(2)]
    $but(1) configure -onClicked {
       
    foreach {a b} [%w cget -data] {    set tmp($a) $b     }
   
        if {  $tmp(state) == 1 } {
            %w configure -icon %#Add -align left
            set tmp(state) 0
        } else {
            %w configure -icon %#Remove -align right
            set tmp(state) 1
        }
       
        $tmp(child) configure -visible $tmp(state)
        %w configure -data [::list state $tmp(state) child $tmp(child)]
        }

    return $box(1)

}

set box1 [gnocl::box -orientation vertical]
foreach item {a b c d e} {
    set b1 [gnocl::button -text "Button $item" -onClicked "puts %w"]
    $box1 add $b1
}
set bb(1) [gnocl::buttonBox HELLO $box1]

#--------------------
set box2 [gnocl::box -orientation vertical]
foreach item {g h i j k} {
    set b2 [gnocl::button -text "Button $item" -onClicked "puts %w"]
    $box2 add $b2
}
set bb(2) [gnocl::buttonBox GOODBYE $box2]

#--------------------
set box3 [gnocl::box -orientation vertical]
foreach item {l m n o p} {
    set b3 [gnocl::button -text "Button $item" -onClicked "puts %w"]
    $box3 add $b3
}
set bb(3) [gnocl::buttonBox TATA $box3]

#--------------------
set box4 [gnocl::box -orientation vertical]
$box4 add $bb(1)
$box4 add $bb(2)
$box4 add $bb(3)

set scr [gnocl::scrolledWindow -child $box4]

set main [gnocl::window -child $scr -width 200 -height 400]
$main center


Sunday, November 04, 2012

Getting Closer to Gnocl 0.9.97

Over the past few months a lot of modifications have been made to the development code to warrant a release increment albeit just by 0.0.01! Before this happens, it would be useful to hear from any (?) Gnocl users out there who are using the package and what they expect to see. Foremost, some feedback from users working with glade/builder files would be useful. The code for loading the xml files is something of a load and it might be more suited to branching out into a separate Gnocl family package. What would also be useful is any bug or performance reports from users.

Many thanks...

gnocl::tooltip trigger

Did a little more to the demo script to show how the "trigger" sub-command actually works.

# test-tooltip.tcl
#! /bin/sh/
#\
exec tclsh "$0" "$@"
package require Gnocl

set lab(1) [gnocl::label -text "TANNOY" ]
set lab(2) [ gnocl::label -text "CAMPER 1" -tooltip "Hi-Di-Hi!!!" ]
set lab(3) [ gnocl::label -text "CAMPER 2" -tooltip "quiet" ]

set box [gnocl::box -orientation  vertical ]
$box add $lab(1) -expand 1 -fill {1 1}
$box add $lab(2) -expand 1 -fill {1 1}
$box add $lab(3) -expand 1 -fill {1 1}

set main [gnocl::window -title "Rise-n-Shine" -child $box -setSize 0.125]

#---------------
# create custom tooltip
#---------------
#
proc myToolTip {} {
   
    # create custom tooltip window
    set lab [gnocl::label -text "Ho-Di-Ho" -xPad 10]
    set im [gnocl::image -image %#About ]
    set box [gnocl::box -borderWidth 1 -shadow in]
   
    $box add $im
    $box add $lab

    # keep copy of component widget ids using -data option
    return [gnocl::window -visible 0 -type popup -child $box -backgroundColor #FBF998 -data "lab $lab im $im box $box" ]
}

set tip [myToolTip]
gnocl::tooltip $lab(3) -window $tip

# extract data and modify
eval [lindex [$tip cget -data] 1] configure -text \"Ho-Di-Ho!!!\"

# demonstrate the use of the trigger sub-command.
set i 0
proc update {} {
    global i
    global lab
        incr i
        $lab(1) configure -tooltip "Good Morning Campers -$i"
        gnocl::tooltip trigger
    after 1000 update
}

update

Saturday, November 03, 2012

Entry widget undo/redo in Tcl.

Hacked together a simple proof of method script to implement Undo/Redo functionality for the gnocl::entry widget. There are some improvements that can be done such as overloading the basic entry widget to allow undo/redo as a widget command. Keyboard bindings are fully there too. I'll take a look at these at a later time.



#---------------
# entryundo($ent)Redo.tcl
#---------------
# Created by William J Giddings
# October, 2012
#---------------
# Description:
# Demonstrates simple entry undo($ent)/redo functionality.
#---------------
# Notes:
# cd /Desktop/GnoclEdit_2/undo($ent)er/wjgstuff
#
# This version has a single buffer to show edit history
#
#---------------

# basic Tcl/Gnocl Script
#! /bin/sh/
#\
exec tclsh "$0" "$@"

package require Gnocl

#---------------
#
#---------------
#
proc entry:resetUndo {ent } {
    global undo idx
    set idx($ent) -1
    set undo($ent) ""
}

#---------------
#
#---------------
#
proc entry:undo {ent} {
    global idx undo
    incr idx($ent) -1
    if { $idx($ent) <= -1} {
        $ent configure -value ""
        set idx($ent) -1
        } else {
            $ent configure -value [lindex $undo($ent) $idx($ent)]
        }
    gnocl::update
}

#---------------
#
#---------------
#
proc entry:redo {ent} {
    global idx undo
    incr idx($ent)
    if { $idx($ent) >= [llength $undo($ent)] } { incr idx($ent) -1 }
    $ent configure -value [lindex $undo($ent) $idx($ent)]
    gnocl::update
}

#---------------
#
#---------------
#
proc entry:addUndo {ent} {
   
    global undo idx
   
    set undo($ent) ""
    set idx($ent) -1
   
    $ent configure -onKeyPress {
        if { "%s" == 20 && "%a" == "y" } { redo %w }
        if { "%s" == 20 && "%a" == "z" } { undo %w }
        set idx(%w) [llength $undo(%w)]
        incr idx(%w) -1
        }

    $ent configure -onKeyRelease {
        lappend undo(%w) [%w get]
        incr idx(%w)
        }

    $ent configure -onDestroy {
        puts "BYE BYE"
        array unset undo %w
        array unset idx %w
        }

}

set b(1) [gnocl::button -text Undo -onClicked { entry:undo $ent } ]
set b(2) [gnocl::button -text Redo -onClicked { entry:redo $ent } ]
set b(3) [gnocl::button -text Reset -onClicked { entry:resetUndo $ent }]
set ent [gnocl::entry]
set box [gnocl::box -orientation vertical]
$box add $b(1)
$box add $b(2)
$box add $b(3)
$box add $ent
gnocl::window -child $box

entry:addUndo $ent

Customized Tooltip Windows

Gtk allows the user to create custom tooltips which might include mixed items such as icons, images and text. The recent addition of the gnocl::tooltip command to allows such things to be done. The following test script shows how its done.

# test-tooltip.tcl
#! /bin/sh/
#\
exec tclsh "$0" "$@"
package require Gnocl


set lab(1) [ gnocl::label -text "LABEL 1" -tooltip "I'm label #1" ]
set lab(2) [ gnocl::label -text "LABEL 2" -tooltip "I'm label #2" ]
set box [gnocl::box]
$box add $lab(1) -expand 1 -fill {1 1}
$box add $lab(2) -expand 1 -fill {1 1}

set main [gnocl::window -title main -tooltip "HELLO WORLD!" -child $box -setSize 0.25]

#---------------
# create custom tooltip
#---------------
#
proc myToolTip {} {
   
    # create custom tooltip window
    set lab [gnocl::label -text HIDIHI -xPad 10]
    set im [gnocl::image -image %#About ]
    set box [gnocl::box -borderWidth 1 -shadow in]
   
    $box add $im
    $box add $lab

    # keep copy of component widget ids using -data option
    return [gnocl::window -visible 0 -type popup -child $box -backgroundColor #FBF998 -data "lab $lab im $im box $box" ]
}

set tip [myToolTip]
 
gnocl::tooltip $lab(2) -window $tip

eval [lindex [$tip cget -data] 1] configure -text HoDiHo!!!

json_shell_utils.tcl