Displaying a table of values with Gnocl table, eventBox and label widgets

WJG (03/10/16) Needed a convenient object to display a range of values and allow the user to pick an item using the mouse pointer. As items are selected, these need to be highlighted and the values passed to a specified callback procedure. Was a lot easier than expected, and less bothersome than trying to do the same job with the standard list/tree widget.

[L1 ]

# test_label_matrix.tcl

# the next line restarts using tclsh \
exec tclsh "$0" "$@"

package require Gnocl

# create a table widget displaying list of values for single-item selection
# Args:
#        data      the list of items to be presented in the table
#        cols      number of columns (default: 2}
#        callBack  script to execure whenever item selected with mouse, 
#                  the value contained in the cell will be passed to the callback (default: puts}
#        fgclr     text colour (default: white)
#        bgclr     cell background colour (default: black)
#        roclr     text roll-over colour (default: red)
# Returns
#        table widget id

proc lable_matrix { data {cols 2} {callBack {}} {fgclr white} {bgclr black} {roclr cyan} } {

        set table [gnocl::table -homogeneous 1 -columnSpacing 0 -rowSpacing 0 ]

        set container [gnocl::scrolledWindow -child $table ]

        set len  [llength $data]
        set rows [expr $len.0 / $cols]

                if { $callBack == ""} { set callBack puts }
        set k 0 
        for {set i 0} { $i < $rows} { incr i } {
                for {set j 0} { $j < $cols} { incr j } {
                        set item [lindex $data $k]
                        set lab [gnocl::label -text $item  -align left -xPad 3]
                        set item [gnocl::eventBox \
                                                        -data "$callBack $item" \
                                                        -child $lab -background $bgclr \
                            -onEnter "%c configure -foreground $roclr " \
                            -onLeave "%c configure -foreground $fgclr" \
                            -onButtonPress { eval %d }]
                        lappend row $item
                        incr k
                $table addRow $row
                set row ""

        return $container


set data "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eget eros augue. Donec tempus odio at ante tempor, et tincidunt ex ornare. Maecenas tincidunt neque interdum lobortis lobortis. Praesent interdum cursus purus, a semper magna porta vitae. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Mauris mi massa, maximus non ipsum volutpat, porttitor cursus ipsum."

# remove punctuation and braces
set data [string map { \{ "" \} "" ; "" . "" , "" } [lsort -dictionary $data]]

set tab [lable_matrix $data 5 ]
gnocl::window -child $tab -width 400 -height 200