#!/usr/local/bin/wish -f

# Demonstrate some of the more interesting features of tkauxlib

# Load the controller

source [file dirname [info script]]/init.tcl

# Pack up the top level of the user interface

pack append . \
	[collapsible frame .simpleform \
		-class Simpleform -title "Simple Form"] \
			{top frame w} \
	[collapsible text .text -width 80 -height 24 \
		-relief sunken -borderwidth 2 -title "Text Widget"] \
			{top frame w} \
	[collapsible choicebox .choicebox \
		-visible 1 \
		-title "Choice Box Sample" \
		-text "This is a sample choice box" \
		-icon weird \
		-buttons {"Fileselect" "Progress" "Error" "Quit"} \
		-textvariable choice] \
			{top frame w}

# Action to take on choicebox buttons.

trace variable choice w choose

proc choose args {
	after 1 {
		global choice
		case $choice in {
			"Fileselect" {
				demo_fileselect
			}
			"Progress" {
				demo_progress
			}
			"Error" {
				error "Sample error" "Sample error" SAMPLE
			}
			"Quit" {
				after 1 exit
			}
		}
	}
}

# Simple form to demonstrate focus management.

pack append .simpleform.b \
	[labeledentry .simpleform.b.n -head Name \
		-width 24 -textvariable form_name] \
			{top frame w} \
	[labeledentry .simpleform.b.a -head Age -tail years \
		-width 4 -validate checkInteger -textvariable form_age] \
			{top frame w} \
	[labeledentry .simpleform.b.w -head Weight -tail lbs \
		-width 8 -validate checkReal -textvariable form_weight] \
			{top frame w} \
	[focusable button .simpleform.b.b -text "Show values" \
		-command {
			if {[focus_update] == 0} {
				set w .simpleform.[gensym showvalues]
				transient showvalues $w
				focus_goToFirst $w
			}

		}] \
			{left pady 8 expand frame center} \
	[focusable button .simpleform.b.r -text "Reset values" \
		-command {resetvalues}] \
			{left pady 8 expand frame center}

# Show the values from the simple form

proc showvalues w {
	global form_name form_age form_weight
	frame $w -class Showvalues
	pack append $w \
		[message $w.t -width 400] \
				{top} \
		[focusable button $w.d -text "Dismiss" \
			-command "after 1 destroy $w"] \
				{top pady 10 frame center}
	$w.t configure -text "\
Name: $form_name
Age: $form_age
Weight: $form_weight"
	button_makeDefault $w.d.b
	return $w
}

# Reset the values in the simple form.

proc resetvalues {} {
	global form_name form_age form_weight
	set form_name "Joe Smith"
	set form_age 37
	set form_weight 195
}

resetvalues

# Demonstrate the file selection mechanism

proc demo_fileselect {} {
	set name [selectfile]
	errormessage .didit "You selected the file, ``$name''"
}

# Demonstrate the progress mechanism

proc demo_progress {} {
	set vname [gensym progress]
	uplevel #0 set $vname 0.0
	transient progress .$vname -text "Demo of progress widget" \
		-variable $vname
	after 100 run_progress $vname
}

proc run_progress vname {
	if {![winfo exists .$vname]} {
		errormessage .quit "You quit prematurely"
		return
	}
	set val [uplevel #0 set $vname]
	if {$val < 1.0} {
		uplevel #0 set $vname [expr $val+0.04]
		after 100 run_progress $vname
	} else {
		after 1 destroy .$vname
		errormessage .done "Progress demo finished."
	}
}

tkwait visibility .
update
focus_goToFirst .
button_setDefault .choicebox.b
