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.
[
1]
# test_label_matrix.tcl
#!/bin/sh
# 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
# callBack script to execure whenever item selected with mouse,
# the value contained in the cell will be passed to the callback
# bgclr default bgclr of the table
# Returns
# table widget id
proc lable_matrix { data cols callBack {fgclr black} {bgclr ""}} {
set table [gnocl::table -homogeneous 1]
if { $bgclr eq ""} { set bgclr [$table cget -background] }
set container [gnocl::scrolledWindow -child $table]
set len [llength $data]
set rows [expr $len.0 / $cols]
set ::tmp($table) ""
set ::tmp($table,bgclr) $bgclr
set ::tmp($table,fgclr) $fgclr
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 $::tmp($table,bgclr) \
-onEnter {
%c configure -foreground red
} \
-onLeave {
%c configure -foreground $::tmp(%p,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."
set data [string map { \{ "" \} "" ; "" . "" , "" } [lsort -dictionary $data]]
lappend data HI DI HI
set tab [lable_matrix $data 4 puts]
gnocl::window -child $tab -width 400 -height 200