#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

load ../src/gnocl.so

set mainBox [gnocl::box -orientation vertical -homogeneous 0]

set box [gnocl::box -orientation horizontal]
set clist [gnocl::clist -columns 3 -titles {"1 col" "2 col" "3 col"} \
      -children { {row4 1 "abc 3"} {row5 2 "abc 1"} {row6 10 "abc 5"}}]
$clist addEnd {{row7 20 "abc 7"} {row8 15 "abc 12"}}
$clist addBegin {{row3 22 "abc 60"} {row2 5 "abc 22"}}
$clist add {{row9 32 "abc 70"} {row10 7 "abc 22"}}
$clist add {{row9 a32 "abc 70"} {row10 b7 "abc 22"}}
$clist addBegin {{row1 22 "abc 60"}}

set option [gnocl::optionMenu -command {eval %v -reverse $reverse} \
      -variable sortCmd]
set rev [gnocl::checkButton -text reverse -variable reverse \
      -command {eval "$sortCmd -reverse %v"}]
$box add [list [gnocl::label -text "sort: "] $option $rev]

$option add "col 2 default" -value "$clist sort 1"
$option add "col 3 default" -value "$clist sort 2"
$option add "col 2 numeric" -value "$clist sort 1 -command cmpNum"

$mainBox add $box -fill 1 -expand 0
$mainBox add $clist -fill 1 -expand 1

proc cmpNum { val1 val2 } {
   # first test, if numerical comparison succeeds
   if { [catch {expr {$val1-$val2}} res] } {
      # test, if one of vals is numeric
      # else compare with string function
      if { [catch {expr {$val1+0}}] == 0 } {
         set res 1
      } elseif { [catch {expr {$val2+0}}] == 0 } {
         set res -1
      } else {
         set res [string compare $val1 $val2]
      }
   } 

   return $res
}

if { $argc == 1 } {
   set win [gnocl::plug [lindex $argv 0] -child $mainBox]
} else {
   set win [gnocl::window -child $mainBox]
}

gnocl::mainLoop

