#!/usr/local/bin/wish
# -*- tcl -*-
# demo of trf features

wm withdraw .
wm title    . "Trf demonstration"

package require Trf

set fdata {
[encoded data, 12191 characters]
APCFMXghDV8EIAACEMAABDCAAQxgAAMgAAEIUAADHMAABzhAAAICADs=
}


label .logo -anchor ne -bd 2 -relief raised \
	-image [image create photo -data $fdata]

entry  .input  -relief sunken -border 2 -textvariable input ;#-width 70
frame  .output -relief raised -border 2
button .quit   -text Quit -command exit
button .demo   -text Demo -command startAnimation

pack   .logo   -side left -expand 0 -fill both
pack   .input  -side top  -expand 1 -fill x -ipadx 1m -ipady 1m
pack   .output -side top  -expand 1 -fill both
pack   .quit   -side left -expand 0 -padx 2m -pady 2m
pack   .demo   -side left -expand 0 -padx 2m -pady 2m

foreach {r m t} {
    0    rep Input:
    1    zip {uuencode Zip:}
    2    hex Hex:
    3    b64 Base64:
    4    md5 {hex'd MD5 Hash:}
} {
    label  .output.l$m -anchor w -text $t
    label  .output.x$m -anchor e -text "__"
    entry  .output.$m  -relief sunken -border 2 -width 100 -textvariable out$m

    grid   .output.l$m -row $r -column 1 -sticky swen
    grid   .output.x$m -row $r -column 2 -sticky swen
    grid   .output.$m  -row $r -column 3 -sticky swen -ipadx 1m -ipady 1m
}


proc startAnimation {} {
    global input
    set    input ""

    animate 300 \
	    "Hello world, this is a demonstration \
of the Trf extension to tcl, a package for transforming \
data in various ways, as can be seen below"
}


proc animate {delay text} {
    global input

    set first [string range $text 0 0]
    set text  [string range $text 1 end]

    if {$first == {}} {
	return
    }

    after $delay [list animate $delay $text]

    append input $first
}


proc trf {args} {
    global input outhex outmd5 outb64 outzip outrep

    # do various transformations on the current input
    # executed for every change
    #
    # convert to HEX
    # convert into Base64
    # compute MD5 message digest (display in HEX)
    # compress using ZIP (display in UUencoded form)

    set temp [zip -mode compress -level 9 $input]
    set mtmp [md5 $input]

    set outrep $input
    set outhex [hex      -mode encode $input]
    set outb64 [base64   -mode encode $input]
    set outmd5 [hex      -mode encode $mtmp]
    set outzip [uuencode -mode encode $temp]

    set i  [string length $outrep]
    set zi [string length $temp]

    # count length of real hash and zip, not the encoded forms

    .output.xrep configure -text $i
    .output.xhex configure -text [string length $outhex]
    .output.xb64 configure -text [string length $outb64]
    .output.xmd5 configure -text [string length $mtmp]
    .output.xzip configure -text $zi

    if {$zi < $i} {
	# indicate real compression in interface
	.output.xzip configure -bg seagreen
	.output.xrep configure -bg [.output.xhex cget -bg]
    } else {
	.output.xzip configure -bg [.output.xhex cget -bg]
	.output.xrep configure -bg coral
    }
}


proc out {args} {
    catch {trf $args} msg
    if {$msg != {}} {puts "error > $msg"}
}

trace variable input w trf




focus        .input 
wm deiconify .
