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!!!