#!/usr/local/bin/wish8.2 -f

# tkxanim v 0.43
# Aaron Faby <aaron@yourlink.net>
# http://members.yourlink.net/aaron/
# Licensed Under GNU GPL

set types {
	{{All Types}	*	}
	{{Quicktime}	{.mov}	}
	{{Windows AVI}	{.avi}	}
	{{MPEG Video}	{.mpeg} }
	{{FLI Animations} {.fli}  }
	{{FLC Animations} {.flc}  }
	{{IFF Animations} {.iff}  }
}

set xanim_options ""
set volume 40
set loops 1
set debug 0

set background [image create photo -file /usr/local/share/icons/tkxanim.gif]
label .background -image $background
label .authorinfo -text "Copyright 1999, Aaron Faby <aaron@yourlink.net>" -justify center

# Volume slider

label .volumelabel -text "Volume" -justify left
scale .volumeslider -orient horizontal -from 0 -to 100 -tickinterval 10 -length 3i -showvalue true -variable volume
entry .volumeentry -justify right -textvariable volume -width 6

# Loop entry field

label .looplabel -text "Number of Times To Loop Animation:" 
entry .looptimes -justify right -textvariable loops -width 6

# Debug level field

label .debuglabel -text "Debug Level (0-5, 0=Off, 5=Most Detail):"
entry .debuglevel -justify right -textvariable debug -width 6

frame .bottom -relief flat -bd 0

button .bottom.open -text "Open" -command { OpenFile }
button .bottom.quit -text "Quit" -command { QuitDialog }
button .bottom.options -text "Options" -command { GetOptions }
button .bottom.manpage -text "man xanim" -command { exec xterm -e man xanim }
button .bottom.about -text "About" -command { ShowAbout }

pack .bottom.open -side left
pack .bottom.options -side left
pack .bottom.manpage -side left
pack .bottom.about -side left
pack .bottom.quit -side left

grid propagate . 1
grid .background -row 0 -column 0 -columnspan 3 -sticky "news"
grid .authorinfo -row 1 -column 0 -columnspan 3 -sticky "ew" -ipady 2
grid .volumelabel -row 2 -column 0 -ipady 2
grid .volumeslider -row 2 -column 1 -sticky "ew" -ipady 2
grid .volumeentry -row 2 -column 2 -ipady 2
grid .looplabel -row 3 -column 0 -columnspan 2 -sticky "w" 
grid .looptimes -row 3 -column 2 -sticky "e"
grid .debuglabel -row 4 -column 0 -columnspan 2 -sticky "w" -ipady 2
grid .debuglevel -row 4 -column 2 -sticky "e" -ipady 2
grid .bottom -row 5 -column 0 -columnspan 3 -sticky "ew"

grid columnconfigure . 0 -weight 1
grid rowconfigure . 0 -weight 1

proc OpenFile {} {
	upvar volume volume
	upvar types types
	upvar xanim_options xanim_options
	upvar loops loops
	upvar debug debug
	set filename [tk_getOpenFile -title "Open File" -initialdir ~ -filetypes $types]
	if {$filename != ""} {
        	if [catch { exec xanim $xanim_options +l$loops +d$debug +Av$volume $filename } result] {
        	tk_dialog .xanim_error "xanim error!" $result error 0 OK } 
	} 
}

proc ShowAbout {} {
        toplevel .about
        wm title .about "About tkxanim"
        label .about.version -text "tkxanim Version .43" -justify left -foreground blue
        label .about.author -text "Aaron Faby <aaron@yourlink.net>" -justify left
        label .about.homepage -text "http://members.yourlink.net/aaron" -justify left
        label .about.license -text "Licensed under the GNU GPL." -justify left
        button .about.ok -text "OK" -command { destroy .about }
        grid .about.version -row 0 -column 0
        grid .about.author -row 1 -column 0
        grid .about.homepage -row 2 -column 0
        grid .about.license -row 3 -column 0
        grid .about.ok -row 4 -column 0
}

proc QuitDialog {} {
        set result [tk_dialog .quit_dialog "Quit?" "Really Quit tkxanim?" info 0 OK Cancel]
        if {$result==0} { exit }
}	

proc GetOptions {} {
	upvar xanim_options xanim_options
	toplevel .options
	wm title .options "xanim Options"
	label .options.audio -text "Audio Options" -justify left -foreground blue
	checkbutton .options.audiodisable -text "Disable Audio" -relief flat -variable audiodisable -onvalue "-Ae" -offvalue ""
	checkbutton .options.donotsync -text "Do not skip video frames to keep in sync with audio" -relief flat -variable donotsync -onvalue "-Ak" -offvalue ""
	label .options.color -text "Color Options" -justify left -foreground  blue
	checkbutton .options.dithering -text "Use Floyd-Steinberg Dithering" -relief flat -variable dithering -onvalue "+Cd" -offvalue ""
	checkbutton .options.cmapfirst -text "Forcibly remap all frames to 1st frame's colormap" -relief flat -variable cmapfirst -onvalue "+Cf" -offvalue ""
	checkbutton .options.greyscale -text "Convert True Color To Greyscale" -relief flat -variable greyscale -onvalue "+Cg" -offvalue ""
	checkbutton .options.histogram -text "Use histogram to aid in color reduction" -relief flat -variable histogram -onvalue "+Ch" -offvalue ""
	checkbutton .options.x11colormap -text "Allocate colors from X11's default colormap" -relief flat -variable x11colormap -onvalue "+Cn" -offvalue ""
	label .options.scaling -text "Scaling Options" -justify left -foreground blue
	checkbutton .options.playresize -text "Allow Resize During Play" -relief flat -variable playresize -onvalue "+Sr" -offvalue ""
	checkbutton .options.iffinterlace -text "Half height of IFF animations if interlaced" -relief flat -variable iffinterlace -onvalue "+Si" -offvalue ""	
	checkbutton .options.x11resize -text "Prevent X11 from resizing to match animations size" -relief flat -variable x11resize -onvalue "+Sn" -offvalue ""
	label .options.remote -text "Remote Window and Control Options" -justify left -foreground blue
	checkbutton .options.endrefresh -text "Don't refresh window at end of anim" -relief flat -variable endrefresh -onvalue "+Wd" -offvalue ""
	checkbutton .options.prepare -text "Prepare anim, but dont start playing it" -relief flat -variable prepare -onvalue "+Wp" -offvalue ""
	checkbutton .options.resizefit -text "Resize X11 Window to fit anim" -relief flat -variable resizefit -onvalue "+Wr" -offvalue ""
	checkbutton .options.center -text "Postion relative to center of anim" -relief flat -variable center -onvalue "+Wc" -offvalue ""
	label .options.special -text "Special Options" -justify left -foreground blue
	checkbutton .options.lastframe -text "Pause On Last Frame" -relief flat -variable lastframe -onvalue "+Zpe" -offvalue ""
	label .options.normal -text "Normal Options" -justify left -foreground blue
	checkbutton .options.root -text "Tile In Root Window" -relief flat -variable root -onvalue "+root" -offvalue ""
	checkbutton .options.multibuffer -text "Disable X11 Multi Buffering" -relief flat -variable multibuffer -onvalue "-D" -offvalue ""
	checkbutton .options.noimages -text "Dont display images, useful for debugging" -relief flat -variable noimages -onvalue "+N" -offvalue ""
	checkbutton .options.iffnoloop -text "Let xanim know that iff anim is a nonlooping one" -relief flat -variable iffnoloop -onvalue "+c" -offvalue ""
	checkbutton .options.iffcycle -text "Allow color cycling for IFF single images" -relief flat -variable iffcycle -onvalue "+r" -offvalue ""
	checkbutton .options.iffcycleanim -text "Allow color cycling for IFF anims" -relief flat -variable iffcycleanim -onvalue "+R" -offvalue ""
	checkbutton .options.verbose -text "Run in X11 verbose mode" -relief flat -variable verbose -onvalue "+X" -offvalue ""

	button .options.ok -text "OK" -command { set xanim_options [concat $audiodisable $donotsync $dithering $cmapfirst $greyscale $histogram $x11colormap $playresize $iffinterlace $x11resize $endrefresh $prepare $resizefit $center $lastframe $root $multibuffer $noimages $iffnoloop $iffcycle $iffcycleanim $verbose]; destroy .options }
	button .options.cancel -text "Cancel" -command { destroy .options }

	grid .options.audio -row 0 -column 0 -sticky "w" 
	grid .options.audiodisable -row 1 -column 0 -sticky "w"
	grid .options.donotsync -row 2 -column 0 -sticky "w"
	grid .options.color -row 3 -column 0 -sticky "w"
	grid .options.dithering -row 4 -column 0 -sticky "w"
	grid .options.cmapfirst -row 5 -column 0 -sticky "w"
	grid .options.greyscale -row 6 -column 0 -sticky "w"
	grid .options.histogram -row 7 -column 0 -sticky "w"
	grid .options.x11colormap -row 8 -column 0 -sticky "w"
	grid .options.scaling -row 9 -column 0 -sticky "w"
	grid .options.playresize -row 10 -column 0 -sticky "w"
	grid .options.iffinterlace -row 11 -column 0 -sticky "w"
	grid .options.x11resize -row 12 -column 0 -sticky "w"
	grid .options.remote -row 0 -column 1 -sticky "w"
	grid .options.endrefresh -row 1 -column 1 -sticky "w"
	grid .options.prepare -row 2 -column 1 -sticky "w"
	grid .options.resizefit -row 3 -column 1 -sticky "w"
	grid .options.center -row 4 -column 1 -sticky "w"
	grid .options.special -row 5 -column 1 -sticky "w"
	grid .options.lastframe -row 6 -column 1 -sticky "w"
	grid .options.normal -row 7 -column 1 -sticky "w"
	grid .options.root -row 8 -column 1 -sticky "w" 
	grid .options.multibuffer -row 9 -column 1 -sticky "w"
	grid .options.noimages -row 10 -column 1 -sticky "w" 
	grid .options.iffnoloop -row 11 -column 1 -sticky "w"
	grid .options.iffcycle -row 12 -column 1 -sticky "w"
	grid .options.iffcycleanim -row 13 -column 1 -sticky "w"
	grid .options.verbose -row 14 -column 1 -sticky "w"
	grid .options.ok -row 15 -column 0 -sticky "ew"
	grid .options.cancel -row 15 -column 1 -sticky "ew" 
}
