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

# this example shows different widgets

load ../src/gnocl.so

set top [gnocl::window -title "Widget Test Window"]
set box [gnocl::box -orientation vertical -homogeneous 0]
$top configure -child $box

set menu [gnocl::menu \
      -children [gnocl::menuItem label -text "%#_Quit" -command exit]]

set menubar [gnocl::menubar -children \
      [gnocl::menuItem label -text "%__Edit" -submenu $menu]]

set hBox [gnocl::box -orientation horizontal -homogeneous 0]
$hBox add [gnocl::button -text "%_Bu_tton" -command {puts "do something"} \
      -tooltip "This is a button which does something when pressed" ]
$hBox add [gnocl::spinButton]
set om [gnocl::optionMenu -command {puts "option %v"}]
$om add "gtk+" -value GTK
$om add "gnome" -value GNOME
$om add "kde" -value KDE
$om add "other" -value OTHER
$hBox add $om

set notebook [gnocl::notebook -popup 1 -children [list \
      [gnocl::button -text "%_b_utton" -command "puts hello"] "%_boo_k1" \
      "menu1"] -switchPageCmd "puts page-change"]

$box add [list $menubar $hBox $notebook]

set check1 0
set check2 1
set check3 1
set entry1 "hello Entry"
set combo2 qqq

set widgets [list [gnocl::checkButton -text "%_Check _1" \
            -tooltip "Testcheck 1" -variable check1 \
            -command {puts "check1: $check1"}] \
      [gnocl::checkButton -text "%_Check _2" -tooltip "Testcheck 2" \
            -variable check2 -command {puts "check2: $check2"}] \
      [gnocl::checkButton -text "%_Check _3" -tooltip "Testcheck 3" \
            -variable check3 -command {puts "check3: $check3"}]]
set boxFrame1 [gnocl::box -orientation vertical -shadow etchedIn \
      -label "Checkbuttons" -children $widgets]
$notebook addBegin [list [list $boxFrame1 "%__check" "checkMenu"]]

#lappend widgets [gnocl::entry -variable entry1]
#$box addBegin $widgets
set boxHor [gnocl::box -orientation horizontal -homogeneous 0]
set entryWidget [gnocl::entry -variable entry1 \
      -tooltip "this is an entry widget"]
gnocl::bind $entryWidget focusIn {puts "focusIn focus: %f"}
gnocl::bind $entryWidget focusOut {puts "focusOut focus: %f"}

$boxHor addEnd [list $entryWidget \
      [gnocl::label -text "%_e_ntry " -bindUnderline $entryWidget]]
$box addBegin $boxHor

set boxHor [gnocl::box -label "Combo" -shadow etchedIn \
      -orientation horizontal -homogeneous 0]
$box addBegin $boxHor
$boxHor addBegin [gnocl::label -text "editable: " -justify left]
$boxHor addBegin [gnocl::combo -variable combo2 \
      -strings {111 222 333 444} -editable 1 -tooltip "editable combobox"]
$boxHor addBegin [gnocl::label -text "not editable: " -justify fill]
$boxHor addBegin [gnocl::combo -variable combo3 \
      -strings {aaa bbb ccc ddd} -editable 0 -tooltip "non editable combobox"]

set boxHor [gnocl::box -orientation horizontal]
$box add $boxHor

# first radio group
set box1 [gnocl::box -orientation vertical -shadow etchedIn \
      -label "Group 1"]
# $boxHor add $box1

set group1 1
$box1 add [gnocl::radioButton -text "%_G1 _radio 1" -variable group1 \
      -value 1 -tooltip "Button 1" -command {puts "R1: group1 = $group1 = %v"}]
$box1 add [gnocl::radioButton -text "%_G1 r_adio 2" -variable group1 \
      -value 2 -tooltip "Button 2" -command {puts "R2: group1 = $group1 = %v"}]
$box1 add [gnocl::radioButton -text "%_G1 ra_dio 3" -variable group1 \
      -value 3 -tooltip "Button 3" -command {puts "R3: group1 = $group1 = %v"}]
$box1 add [gnocl::radioButton -text "%_G1 rad_io 4" -variable group1 \
      -value 4 -tooltip "Button 4" -command {puts "R4: group1 = $group1 = %v"}]

# second radio group

set group2 3
set widgets [gnocl::radioButton -text "%__G2 radio 1" -variable group2 \
      -tooltip "Radio 1" -value 1 -command {puts "R1: group2 = $group2"}]
lappend widgets [gnocl::radioButton -text "%_G2 radi_o 2" -tooltip "Radio 2" \
      -variable group2 -value 2 -command {puts "R2: group2 = $group2"}]
lappend widgets [gnocl::radioButton -text "G2 radio 3" -tooltip "Radio 3" \
      -variable group2 -value 3 -command {puts "R3: group2 = $group2"}]
lappend widgets [gnocl::radioButton -text "G2 radio 4" -tooltip "Radio 4" \
      -variable group2 -value 4 -command {puts "R4: group2 = $group2"}]

set box2 [gnocl::box -orientation vertical -shadow etchedIn \
      -label "Group 2" -children $widgets]

set paned [gnocl::paned -orientation horizontal -children [list $box1 $box2]]
$boxHor add $paned

$box add [gnocl::scale -orientation horizontal]

set txt [gnocl::text -editable 1]
$box add $txt

set childs ""
for { set k 1} { $k < 5} { incr k } {
   lappend childs [list $k "label $k"]
}

proc listPrint { t w s r c b } {
   puts -nonewline "Event: $t "
   if { !$s } { puts -nonewline "not " }
   puts -nonewline "selected row: $r col: $c button: $b now selected: "
   puts [$w getSelection]
}
set list [gnocl::clist -columns 2 -selectionMode extended \
      -titles [list "No." "Text"] -children $childs \
      -justifications "right left" -height 200 \
      -selectionChanged {listPrint %t %w %s %r %c %b}]
$box add $list 

set children [list [gnocl::button -text %#Ok -command "$top delete"]]
      #[gnocl::button -text %#Cancel -command "$top delete"] \
      #[gnocl::button -text %#Help]
set boxHor [gnocl::box -orientation horizontal -buttonType 1 -expand 1 \
      -fill 1 -padding 10 -children $children]
$box add $boxHor
if { [info exists tk_patchLevel] } {
   wm withdraw .
} else {
   gnocl::mainLoop
}
