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

load ../src/gnocl.so

# Test "invoke" subcommand

# TODO: set sensitive 0
#       menu invoke

proc cmd { widg val } {
   incr ::noCmd
   if { abs( $val - [$widg getValue] ) > 1e-8 } {
      puts -nonewline "ERROR: in command $widg: "
      puts -nonewline $val
      puts -nonewline " != "
      puts [$widg getValue]
   }
}


set mainBox [gnocl::box -orientation vertical]
set menuBar [gnocl::menubar]
set menu [gnocl::menu]
$menuBar add [gnocl::menuItem label -text "%__Menu" -submenu $menu]
$mainBox add $menuBar

# button
set noCmd 0
set widg [gnocl::button -text "Button" -command {incr noCmd}]
$mainBox add $widg
$widg invoke
if { $noCmd != 1 } {
   puts "ERROR: button invoke: $noCmd"
}
$widg configure -sensitive 0            ;# no change, if not sensitive
$widg invoke
if { $noCmd != 1 } {
   puts "ERROR: button invoke: $noCmd"
}
$widg configure -sensitive 1
$widg invoke
if { $noCmd != 2 } {
   puts "ERROR: button invoke: $noCmd"
}

# menuItem label
set noCmd 0
set widg [gnocl::menuItem label -text "Label" -command {incr noCmd}]
$menu add $widg
$widg invoke
if { $noCmd != 1 } {
   puts "ERROR: menuItem label invoke: $noCmd"
}
$widg configure -sensitive 0            ;# no change, if not sensitive
$widg invoke
if { $noCmd != 1 } {
   puts "ERROR: menuItem label invoke: $noCmd"
}
$widg configure -sensitive 1
$widg invoke
if { $noCmd != 2 } {
   puts "ERROR: menuItem label invoke: $noCmd"
}

# checkButton
set noCmd 0
set widg [gnocl::checkButton -text "check" -command "cmd %w %v" \
      -variable checkButtonVar]
$mainBox add $widg
$widg invoke
if { $noCmd != 1 || $checkButtonVar != 1 } {
   puts "ERROR: checkButton invoke: $noCmd $checkButtonVar"
}
$widg invoke
if { $noCmd != 2 || $checkButtonVar != 0 } {
   puts "ERROR: checkButton 2 invoke: $noCmd $checkButtonVar"
}
$widg invoke
if { $noCmd != 3 || $checkButtonVar != 1 } {
   puts "ERROR: checkButton 3 invoke: $noCmd $checkButtonVar"
}
$widg configure -sensitive 0            ;# no change, if not sensitive
$widg invoke
if { $noCmd != 3 || $checkButtonVar != 1 } {
   puts "ERROR: checkButton 4 invoke: $noCmd $checkButtonVar"
}
$widg configure -sensitive 1
$widg invoke
if { $noCmd != 4 || $checkButtonVar != 0 } {
   puts "ERROR: checkButton 5 invoke: $noCmd $checkButtonVar"
}

# menuItem checkButton
set noCmd 0
set widg [gnocl::menuItem checkButton -text "check" -command "cmd %w %v" \
      -variable menuCheckButtonVar]
$menu add $widg
$widg invoke
if { $noCmd != 1 || $menuCheckButtonVar != 1 } {
   puts "ERROR: menuItem checkButton invoke: $noCmd $menuCheckButtonVar"
}
$widg invoke
if { $noCmd != 2 || $menuCheckButtonVar != 0 } {
   puts "ERROR: menuItem checkButton 2 invoke: $noCmd $menuCheckButtonVar"
}
$widg invoke
if { $noCmd != 3 || $menuCheckButtonVar != 1 } {
   puts "ERROR: menuItem checkButton 3 invoke: $noCmd $menuCheckButtonVar"
}
$widg configure -sensitive 0            ;# no change, if not sensitive
$widg invoke
if { $noCmd != 3 || $menuCheckButtonVar != 1 } {
   puts "ERROR: menuItem checkButton 4 invoke: $noCmd $menuCheckButtonVar"
}
$widg configure -sensitive 1
$widg invoke
if { $noCmd != 4 || $menuCheckButtonVar != 0 } {
   puts "ERROR: menuItem checkButton 5 invoke: $noCmd $menuCheckButtonVar"
}

# radioButton
set noCmd 0
set widg1 [gnocl::radioButton -text "radio 1" -command "cmd %w %v" \
      -variable radioButtonVar -value 0]
set widg2 [gnocl::radioButton -text "radio 2" -command "cmd %w %v" \
      -variable radioButtonVar -value 1]
set widg3 [gnocl::radioButton -text "radio 3" -command "cmd %w %v" \
      -variable radioButtonVar -value 2]
$mainBox add "$widg1 $widg2 $widg3"
if { $noCmd != 0 || $radioButtonVar != 0 } {
   puts "ERROR: radioButton 1 invoke: $noCmd $radioButtonVar"
}
$widg1 invoke           ;# no change, if selected radio is invoked
if { $noCmd != 0 || $radioButtonVar != 0 } {
   puts "ERROR: radioButton 2 invoke: $noCmd $radioButtonVar"
}
$widg2 invoke
if { $noCmd != 1 || $radioButtonVar != 1 } {
   puts "ERROR: radioButton 3 invoke: $noCmd $radioButtonVar"
}
$widg3 invoke
if { $noCmd != 2 || $radioButtonVar != 2 } {
   puts "ERROR: radioButton 4 invoke: $noCmd $radioButtonVar"
}
$widg3 invoke           ;# no change, if selected radio is invoked
if { $noCmd != 2 || $radioButtonVar != 2 } {
   puts "ERROR: radioButton 5 invoke: $noCmd $radioButtonVar"
}
$widg2 configure -sensitive 0
$widg2 invoke           ;# no change, if button is not sensitive
if { $noCmd != 2 || $radioButtonVar != 2 } {
   puts "ERROR: radioButton 6 invoke: $noCmd $radioButtonVar"
}
$widg2 configure -sensitive 1
$widg2 invoke
if { $noCmd != 3 || $radioButtonVar != 1 } {
   puts "ERROR: radioButton 7 invoke: $noCmd $radioButtonVar"
}



puts "-------------------- end of automatic test -------------------"
if { $argc == 1 } {
   set win [gnocl::plug [lindex $argv 0] -child $mainBox]
} else {
   set win [gnocl::window -child $mainBox]
}

gnocl::mainLoop

