Index: trunk/gui/scripts/heightmapviewer.tcl
===================================================================
--- trunk/gui/scripts/heightmapviewer.tcl	(revision 1313)
+++ trunk/gui/scripts/heightmapviewer.tcl	(revision 1342)
@@ -32,5 +32,5 @@
 proc HeightmapViewer_init_resources {} {
     Rappture::resources::register \
-        nanovis_server Rappture::HeightmapViewer::SetServerList
+	nanovis_server Rappture::HeightmapViewer::SetServerList
 }
 
@@ -43,14 +43,14 @@
 
     constructor { hostlist args } {
-        Rappture::VisViewer::constructor $hostlist
+	Rappture::VisViewer::constructor $hostlist
     } {
-        # defined below
+	# defined below
     }
     destructor {
-        # defined below
+	# defined below
     }
 
     public proc SetServerList { namelist } {
-        Rappture::VisViewer::SetServerList "nanovis" $namelist
+	Rappture::VisViewer::SetServerList "nanovis" $namelist
     }
     public method add {dataobj {settings ""}}
@@ -60,5 +60,5 @@
     public method download {option args}
     public method parameters {title args} {
-        # do nothing
+	# do nothing
     }
     public method drawer {what who}
@@ -114,9 +114,9 @@
     $_dispatcher register !legend
     $_dispatcher dispatch $this !legend \
-        "[itcl::code $this _fixSettings legend]; list"
+	"[itcl::code $this _fixSettings legend]; list"
     # Send dataobjs event
     $_dispatcher register !send_dataobjs
     $_dispatcher dispatch $this !send_dataobjs \
-        "[itcl::code $this _send_dataobjs]; list"
+	"[itcl::code $this _send_dataobjs]; list"
     # Rebuild event
     $_dispatcher register !rebuild
@@ -133,30 +133,30 @@
     # Initialize the view to some default parameters.
     array set view_ {
-        theta   45
-        phi     45
-        psi     0
-        zoom    1.0
-        pan-x	0
-        pan-y	0
+	theta   45
+	phi     45
+	psi     0
+	zoom    1.0
+	pan-x	0
+	pan-y	0
     }
     set obj2id_(count) 0
 
     itk_component add zoom {
-        frame $itk_component(controls).zoom
+	frame $itk_component(controls).zoom
     } {
-        usual
-        rename -background -controlbackground controlBackground Background
+	usual
+	rename -background -controlbackground controlBackground Background
     }
     pack $itk_component(zoom) -side top
 
     itk_component add reset {
-        button $itk_component(zoom).reset \
-            -borderwidth 1 -padx 1 -pady 1 \
-            -image [Rappture::icon reset-view] \
-            -command [itcl::code $this _zoom reset]
+	button $itk_component(zoom).reset \
+	    -borderwidth 1 -padx 1 -pady 1 \
+	    -image [Rappture::icon reset-view] \
+	    -command [itcl::code $this _zoom reset]
     } {
-        usual
-        ignore -borderwidth
-        rename -highlightbackground -controlbackground controlBackground Background
+	usual
+	ignore -borderwidth
+	rename -highlightbackground -controlbackground controlBackground Background
     }
     pack $itk_component(reset) -side top -padx 2 -pady { 2 0 }
@@ -164,12 +164,12 @@
 
     itk_component add zoomin {
-        button $itk_component(zoom).zin \
-            -borderwidth 1 -padx 1 -pady 1 \
-            -image [Rappture::icon zoom-in] \
-            -command [itcl::code $this _zoom in]
+	button $itk_component(zoom).zin \
+	    -borderwidth 1 -padx 1 -pady 1 \
+	    -image [Rappture::icon zoom-in] \
+	    -command [itcl::code $this _zoom in]
     } {
-        usual
-        ignore -borderwidth
-        rename -highlightbackground -controlbackground controlBackground Background
+	usual
+	ignore -borderwidth
+	rename -highlightbackground -controlbackground controlBackground Background
     }
     pack $itk_component(zoomin) -side top -padx 2 -pady { 2 0 }
@@ -177,12 +177,12 @@
 
     itk_component add zoomout {
-        button $itk_component(zoom).zout \
-            -borderwidth 1 -padx 1 -pady 1 \
-            -image [Rappture::icon zoom-out] \
-            -command [itcl::code $this _zoom out]
+	button $itk_component(zoom).zout \
+	    -borderwidth 1 -padx 1 -pady 1 \
+	    -image [Rappture::icon zoom-out] \
+	    -command [itcl::code $this _zoom out]
     } {
-        usual
-        ignore -borderwidth
-        rename -highlightbackground -controlbackground controlBackground Background
+	usual
+	ignore -borderwidth
+	rename -highlightbackground -controlbackground controlBackground Background
     }
     pack $itk_component(zoomout) -side top -padx 2 -pady { 2 0 }
@@ -190,11 +190,11 @@
 
     itk_component add settings_button {
-        label $itk_component(controls).settingsbutton \
-            -borderwidth 1 -padx 1 -pady 1 \
-            -relief "raised" -image [Rappture::icon wrench]
+	label $itk_component(controls).settingsbutton \
+	    -borderwidth 1 -padx 1 -pady 1 \
+	    -relief "raised" -image [Rappture::icon wrench]
     } {
-        usual
-        ignore -borderwidth
-        rename -highlightbackground -controlbackground controlBackground \
+	usual
+	ignore -borderwidth
+	rename -highlightbackground -controlbackground controlBackground \
 	    Background
     }
@@ -204,16 +204,16 @@
 	"Configure settings"
     bind $itk_component(settings_button) <ButtonPress> \
-        [itcl::code $this drawer toggle settings]
+	[itcl::code $this drawer toggle settings]
     pack $itk_component(settings_button) -side bottom \
 	-padx 2 -pady 2 -anchor e
 
     itk_component add camera_button {
-        label $itk_component(controls).camerabutton \
-            -borderwidth 1 -padx 1 -pady 1 \
-            -relief "raised" -image [Rappture::icon camera]
+	label $itk_component(controls).camerabutton \
+	    -borderwidth 1 -padx 1 -pady 1 \
+	    -relief "raised" -image [Rappture::icon camera]
     } {
-        usual
-        ignore -borderwidth
-        rename -highlightbackground -controlbackground controlBackground \
+	usual
+	ignore -borderwidth
+	rename -highlightbackground -controlbackground controlBackground \
 	    Background
     }
@@ -221,5 +221,5 @@
 	"Camera settings"
     bind $itk_component(camera_button) <ButtonPress> \
-        [itcl::code $this drawer toggle camera]
+	[itcl::code $this drawer toggle camera]
     pack $itk_component(camera_button) -side bottom \
 	-padx 2 -pady { 0 2 } -ipadx 1 -ipady 1
@@ -231,63 +231,63 @@
     set _image(legend) [image create photo]
     itk_component add legend {
-        canvas $itk_component(area).legend -width 30 -highlightthickness 0
+	canvas $itk_component(area).legend -width 30 -highlightthickness 0
     } {
-        usual
-        ignore -highlightthickness
-        rename -background -plotbackground plotBackground Background
+	usual
+	ignore -highlightthickness
+	rename -background -plotbackground plotBackground Background
     }
     pack $itk_component(legend) -side right -fill y
     pack $itk_component(3dview) -side left -expand yes -fill both
     bind $itk_component(legend) <Configure> \
-        [list $_dispatcher event -idle !legend]
+	[list $_dispatcher event -idle !legend]
 
     # Bindings for rotation via mouse
     bind $itk_component(3dview) <ButtonPress-1> \
-        [itcl::code $this _rotate click %x %y]
+	[itcl::code $this _rotate click %x %y]
     bind $itk_component(3dview) <B1-Motion> \
-        [itcl::code $this _rotate drag %x %y]
+	[itcl::code $this _rotate drag %x %y]
     bind $itk_component(3dview) <ButtonRelease-1> \
-        [itcl::code $this _rotate release %x %y]
+	[itcl::code $this _rotate release %x %y]
     bind $itk_component(3dview) <Configure> \
-        [itcl::code $this _send "screen %w %h"]
+	[itcl::code $this _send "screen %w %h"]
 
     # Bindings for panning via mouse
     bind $itk_component(3dview) <ButtonPress-2> \
-        [itcl::code $this _pan click %x %y]
+	[itcl::code $this _pan click %x %y]
     bind $itk_component(3dview) <B2-Motion> \
-        [itcl::code $this _pan drag %x %y]
+	[itcl::code $this _pan drag %x %y]
     bind $itk_component(3dview) <ButtonRelease-2> \
-        [itcl::code $this _pan release %x %y]
+	[itcl::code $this _pan release %x %y]
 
     # Bindings for panning via keyboard
     bind $itk_component(3dview) <KeyPress-Left> \
-        [itcl::code $this _pan set -10 0]
+	[itcl::code $this _pan set -10 0]
     bind $itk_component(3dview) <KeyPress-Right> \
-        [itcl::code $this _pan set 10 0]
+	[itcl::code $this _pan set 10 0]
     bind $itk_component(3dview) <KeyPress-Up> \
-        [itcl::code $this _pan set 0 -10]
+	[itcl::code $this _pan set 0 -10]
     bind $itk_component(3dview) <KeyPress-Down> \
-        [itcl::code $this _pan set 0 10]
+	[itcl::code $this _pan set 0 10]
     bind $itk_component(3dview) <Shift-KeyPress-Left> \
-        [itcl::code $this _pan set -2 0]
+	[itcl::code $this _pan set -2 0]
     bind $itk_component(3dview) <Shift-KeyPress-Right> \
-        [itcl::code $this _pan set 2 0]
+	[itcl::code $this _pan set 2 0]
     bind $itk_component(3dview) <Shift-KeyPress-Up> \
-        [itcl::code $this _pan set 0 -2]
+	[itcl::code $this _pan set 0 -2]
     bind $itk_component(3dview) <Shift-KeyPress-Down> \
-        [itcl::code $this _pan set 0 2]
+	[itcl::code $this _pan set 0 2]
 
     # Bindings for zoom via keyboard
     bind $itk_component(3dview) <KeyPress-Prior> \
-        [itcl::code $this _zoom out]
+	[itcl::code $this _zoom out]
     bind $itk_component(3dview) <KeyPress-Next> \
-        [itcl::code $this _zoom in]
+	[itcl::code $this _zoom in]
 
     bind $itk_component(3dview) <Enter> "focus $itk_component(3dview)"
 
     if {[string equal "x11" [tk windowingsystem]]} {
-        # Bindings for zoom via mouse
-        bind $itk_component(3dview) <4> [itcl::code $this _zoom out]
-        bind $itk_component(3dview) <5> [itcl::code $this _zoom in]
+	# Bindings for zoom via mouse
+	bind $itk_component(3dview) <4> [itcl::code $this _zoom out]
+	bind $itk_component(3dview) <5> [itcl::code $this _zoom in]
     }
 
@@ -320,21 +320,21 @@
 itcl::body Rappture::HeightmapViewer::add {dataobj {settings ""}} {
     array set params {
-        -color auto
-        -width 1
-        -linestyle solid
-        -brightness 0
-        -raise 0
-        -description ""
-        -param ""
+	-color auto
+	-width 1
+	-linestyle solid
+	-brightness 0
+	-raise 0
+	-description ""
+	-param ""
     }
     foreach {opt val} $settings {
-        if {![info exists params($opt)]} {
-            error "bad setting \"$opt\": should be [join [lsort [array names params]] {, }]"
-        }
-        set params($opt) $val
+	if {![info exists params($opt)]} {
+	    error "bad setting \"$opt\": should be [join [lsort [array names params]] {, }]"
+	}
+	set params($opt) $val
     }
     if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
-        # can't handle -autocolors yet
-        set params(-color) black
+	# can't handle -autocolors yet
+	set params(-color) black
     }
     set location [$dataobj hints camera]
@@ -344,9 +344,9 @@
     set pos [lsearch -exact $dataobj $dlist_]
     if {$pos < 0} {
-        lappend dlist_ $dataobj
-        set obj2ovride_($dataobj-color) $params(-color)
-        set obj2ovride_($dataobj-width) $params(-width)
-        set obj2ovride_($dataobj-raise) $params(-raise)
-        $_dispatcher event -idle !rebuild
+	lappend dlist_ $dataobj
+	set obj2ovride_($dataobj-color) $params(-color)
+	set obj2ovride_($dataobj-width) $params(-width)
+	set obj2ovride_($dataobj-raise) $params(-raise)
+	$_dispatcher event -idle !rebuild
     }
 }
@@ -362,5 +362,5 @@
 itcl::body Rappture::HeightmapViewer::get {args} {
     if {[llength $args] == 0} {
-        set args "-objects"
+	set args "-objects"
     }
 
@@ -368,36 +368,36 @@
     switch -- $op {
       -objects {
-        # put the dataobj list in order according to -raise options
-        set dlist $dlist_
-        foreach obj $dlist {
-            if { [info exists obj2ovride_($obj-raise)] && 
-                 $obj2ovride_($obj-raise)} {
-                set i [lsearch -exact $dlist $obj]
-                if {$i >= 0} {
-                    set dlist [lreplace $dlist $i $i]
-                    lappend dlist $obj
-                }
-            }
-        }
-        return $dlist
+	# put the dataobj list in order according to -raise options
+	set dlist $dlist_
+	foreach obj $dlist {
+	    if { [info exists obj2ovride_($obj-raise)] && 
+		 $obj2ovride_($obj-raise)} {
+		set i [lsearch -exact $dlist $obj]
+		if {$i >= 0} {
+		    set dlist [lreplace $dlist $i $i]
+		    lappend dlist $obj
+		}
+	    }
+	}
+	return $dlist
       }
       -image {
-        if {[llength $args] != 2} {
-            error "wrong # args: should be \"get -image 3dview|legend\""
-        }
-        switch -- [lindex $args end] {
-            3dview {
-                return $_image(plot)
-            }
-            legend {
-                return $_image(legend)
-            }
-            default {
-                error "bad image name \"[lindex $args end]\": should be 3dview or legend"
-            }
-        }
+	if {[llength $args] != 2} {
+	    error "wrong # args: should be \"get -image 3dview|legend\""
+	}
+	switch -- [lindex $args end] {
+	    3dview {
+		return $_image(plot)
+	    }
+	    legend {
+		return $_image(legend)
+	    }
+	    default {
+		error "bad image name \"[lindex $args end]\": should be 3dview or legend"
+	    }
+	}
       }
       default {
-        error "bad option \"$op\": should be -objects or -image"
+	error "bad option \"$op\": should be -objects or -image"
       }
     }
@@ -412,5 +412,5 @@
 itcl::body Rappture::HeightmapViewer::delete {args} {
     if {[llength $args] == 0} {
-        set args $dlist_
+	set args $dlist_
     }
 
@@ -418,17 +418,17 @@
     set changed 0
     foreach dataobj $args {
-        set pos [lsearch -exact $dlist_ $dataobj]
-        if {$pos >= 0} {
-            set dlist_ [lreplace $dlist_ $pos $pos]
-            foreach key [array names obj2ovride_ $dataobj-*] {
-                unset obj2ovride_($key)
-            }
-            set changed 1
-        }
+	set pos [lsearch -exact $dlist_ $dataobj]
+	if {$pos >= 0} {
+	    set dlist_ [lreplace $dlist_ $pos $pos]
+	    foreach key [array names obj2ovride_ $dataobj-*] {
+		unset obj2ovride_($key)
+	    }
+	    set changed 1
+	}
     }
 
     # if anything changed, then rebuild the plot
     if {$changed} {
-        $_dispatcher event -idle !rebuild
+	$_dispatcher event -idle !rebuild
     }
 }
@@ -445,24 +445,24 @@
 itcl::body Rappture::HeightmapViewer::scale {args} {
     foreach val {xmin xmax ymin ymax zmin zmax vmin vmax} {
-        set limits_($val) ""
+	set limits_($val) ""
     }
     foreach obj $args {
-        foreach axis {x y z v} {
-            foreach {min max} [$obj limits $axis] break
-            if {"" != $min && "" != $max} {
-                if {"" == $limits_(${axis}min)} {
-                    set limits_(${axis}min) $min
-                    set limits_(${axis}max) $max
-                } else {
-                    if {$min < $limits_(${axis}min)} {
-                        set limits_(${axis}min) $min
-                    }
-                    if {$max > $limits_(${axis}max)} {
-                        set limits_(${axis}max) $max
-                    }
-                }
+	foreach axis {x y z v} {
+	    foreach {min max} [$obj limits $axis] break
+	    if {"" != $min && "" != $max} {
+		if {"" == $limits_(${axis}min)} {
+		    set limits_(${axis}min) $min
+		    set limits_(${axis}max) $max
+		} else {
+		    if {$min < $limits_(${axis}min)} {
+			set limits_(${axis}min) $min
+		    }
+		    if {$max > $limits_(${axis}max)} {
+			set limits_(${axis}max) $max
+		    }
+		}
 		set limits_(${axis}range) [expr {$max - $min}]
-            }
-        }
+	    }
+	}
     }
 }
@@ -480,34 +480,27 @@
 itcl::body Rappture::HeightmapViewer::download {option args} {
     switch $option {
-        coming {
-            if {[catch {
-                blt::winop snap $itk_component(area) $_image(download)
-            }]} {
-                $_image(download) configure -width 1 -height 1
-                $_image(download) put #000000
-            }
-        }
-        controls {
-            # no controls for this download yet
-            return ""
-        }
-        now {
-            #
-            # Hack alert!  Need data in binary format,
-            # so we'll save to a file and read it back.
-            #
-            set tmpfile /tmp/image[pid].jpg
-            $_image(download) write $tmpfile -format jpeg
-            set fid [open $tmpfile r]
-            fconfigure $fid -encoding binary -translation binary
-            set bytes [read $fid]
-            close $fid
-            file delete -force $tmpfile
-
-            return [list .jpg $bytes]
-        }
-        default {
-            error "bad option \"$option\": should be coming, controls, now"
-        }
+	coming {
+	    if {[catch {
+		blt::winop snap $itk_component(area) $_image(download)
+	    }]} {
+		$_image(download) configure -width 1 -height 1
+		$_image(download) put #000000
+	    }
+	}
+	controls {
+	    # no controls for this download yet
+	    return ""
+	}
+	now {
+	    # Get image data (as base64) and decode back to binary.  This is
+	    # better than writing to temporary files.  When we switch the BLT
+	    # picture image it won't be necessary to decode the image data.
+	    set bytes [$_image(download) data -format "jpeg -quality 100"]
+	    set bytes [Rappture::encoding::decode -as b64 $bytes]
+	    return [list .jpg $bytes]
+	}
+	default {
+	    error "bad option \"$option\": should be coming, controls, now"
+	}
     }
 }
@@ -524,5 +517,5 @@
     set _hosts [GetServerList "nanovis"]
     if { "" == $_hosts } {
-        return 0
+	return 0
     }
     set result [VisViewer::Connect $_hosts]
@@ -557,11 +550,11 @@
 itcl::body Rappture::HeightmapViewer::_send {string} {
     if {[llength $sendobjs_] > 0} {
-        append outbuf_ $string "\n"
+	append outbuf_ $string "\n"
     } else {
-        if {[SendBytes $string]} {
-            foreach line [split $string \n] {
-                SendEcho >>line $line
-            }
-        }
+	if {[SendBytes $string]} {
+	    foreach line [split $string \n] {
+		SendEcho >>line $line
+	    }
+	}
     }
 }
@@ -579,45 +572,45 @@
     # Reset the overall limits 
     if { $sendobjs_ != "" } {
-        set limits_(vmin) ""
-        set limits_(vmax) ""
+	set limits_(vmin) ""
+	set limits_(vmax) ""
     }
     foreach dataobj $sendobjs_ {
-        foreach comp [$dataobj components] {
-            set data [$dataobj blob $comp]
-
-            foreach { vmin vmax }  [$dataobj limits v] break
-            if { $limits_(vmin) == "" || $vmin < $limits_(vmin) } {
-                set limits_(vmin) $vmin
-            }
-            if { $limits_(vmax) == "" || $vmax > $limits_(vmax) } {
-                set limits_(vmax) $vmax
-            }
-
-            # tell the engine to expect some data
-            set nbytes [string length $data]
-            if { ![SendBytes "heightmap data follows $nbytes"] } {
-                return
-
-            }
-            if { ![SendBytes $data] } {
-                return
-            }
-            set id $obj2id_(count)
-            incr obj2id_(count)
-            set id2obj_($id) [list $dataobj $comp]
-            set obj2id_($dataobj-$comp) $id
-            set receiveIds_($id) 1
-
-            #
-            # Determine the transfer function needed for this volume
-            # and make sure that it's defined on the server.
-            #
-            foreach {sname cmap wmap} [_getTransfuncData $dataobj $comp] break
-            set cmdstr [list "transfunc" "define" $sname $cmap $wmap]
-            if {![SendBytes $cmdstr]} {
-                return
-            }
-            set obj2style_($dataobj-$comp) $sname
-        }
+	foreach comp [$dataobj components] {
+	    set data [$dataobj blob $comp]
+
+	    foreach { vmin vmax }  [$dataobj limits v] break
+	    if { $limits_(vmin) == "" || $vmin < $limits_(vmin) } {
+		set limits_(vmin) $vmin
+	    }
+	    if { $limits_(vmax) == "" || $vmax > $limits_(vmax) } {
+		set limits_(vmax) $vmax
+	    }
+
+	    # tell the engine to expect some data
+	    set nbytes [string length $data]
+	    if { ![SendBytes "heightmap data follows $nbytes"] } {
+		return
+
+	    }
+	    if { ![SendBytes $data] } {
+		return
+	    }
+	    set id $obj2id_(count)
+	    incr obj2id_(count)
+	    set id2obj_($id) [list $dataobj $comp]
+	    set obj2id_($dataobj-$comp) $id
+	    set receiveIds_($id) 1
+
+	    #
+	    # Determine the transfer function needed for this volume
+	    # and make sure that it's defined on the server.
+	    #
+	    foreach {sname cmap wmap} [_getTransfuncData $dataobj $comp] break
+	    set cmdstr [list "transfunc" "define" $sname $cmap $wmap]
+	    if {![SendBytes $cmdstr]} {
+		return
+	    }
+	    set obj2style_($dataobj-$comp) $sname
+	}
     }
     set sendobjs_ ""
@@ -627,16 +620,16 @@
     set first [lindex [get] 0]
     if {"" != $first} {
-        set axis [$first hints updir]
-        if {"" != $axis} {
-            _send "up $axis"
-        }
+	set axis [$first hints updir]
+	if {"" != $axis} {
+	    _send "up $axis"
+	}
     }
 
     foreach key [array names obj2id_ *-*] {
-        set state [string match $first-* $key]
-        _send "heightmap data visible $state $obj2id_($key)"
-        if {[info exists obj2style_($key)]} {
-            _send "heightmap transfunc $obj2style_($key) $obj2id_($key)"
-        }
+	set state [string match $first-* $key]
+	_send "heightmap data visible $state $obj2id_($key)"
+	if {[info exists obj2style_($key)]} {
+	    _send "heightmap transfunc $obj2style_($key) $obj2id_($key)"
+	}
     }
 
@@ -657,7 +650,7 @@
 itcl::body Rappture::HeightmapViewer::ReceiveImage {option size} {
     if {[IsConnected]} {
-        set bytes [ReceiveBytes $size]
-        $_image(plot) configure -data $bytes
-        ReceiveEcho <<line "<read $size bytes for [image width $_image(plot)]x[image height $_image(plot)] image>"
+	set bytes [ReceiveBytes $size]
+	$_image(plot) configure -data $bytes
+	ReceiveEcho <<line "<read $size bytes for [image width $_image(plot)]x[image height $_image(plot)] image>"
     }
 }
@@ -672,6 +665,6 @@
 itcl::body Rappture::HeightmapViewer::_ReceiveLegend {tf vmin vmax size} {
     if { [IsConnected] } {
-        set bytes [ReceiveBytes $size]
-        ReceiveEcho <<line "<read $size bytes for [image width $_image(legend)]x[image height $_image(legend)] legend>"
+	set bytes [ReceiveBytes $size]
+	ReceiveEcho <<line "<read $size bytes for [image width $_image(legend)]x[image height $_image(legend)] legend>"
 	if 1 {
 	set src [image create photo -data $bytes]
@@ -681,21 +674,21 @@
 	$_image(legend) configure -data $bytes
 	}
-        set c $itk_component(legend)
-        set w [winfo width $c]
-        set h [winfo height $c]
+	set c $itk_component(legend)
+	set w [winfo width $c]
+	set h [winfo height $c]
 	set lineht [expr [font metrics $itk_option(-font) -linespace] + 4]
-        if {"" == [$c find withtag transfunc]} {
-            $c create image 0 [expr $lineht] -anchor ne \
-                 -image $_image(legend) -tags transfunc
-            $c create text 10 [expr {$h-8}] -anchor se \
-                 -fill $itk_option(-plotforeground) -tags vmin
-            $c create text [expr {$w-10}] [expr {$h-8}] -anchor ne \
-                 -fill $itk_option(-plotforeground) -tags vmax
-        }
+	if {"" == [$c find withtag transfunc]} {
+	    $c create image 0 [expr $lineht] -anchor ne \
+		 -image $_image(legend) -tags transfunc
+	    $c create text 10 [expr {$h-8}] -anchor se \
+		 -fill $itk_option(-plotforeground) -tags vmin
+	    $c create text [expr {$w-10}] [expr {$h-8}] -anchor ne \
+		 -fill $itk_option(-plotforeground) -tags vmax
+	}
 	$c coords transfunc [expr $w - 5] [expr $lineht]
-        $c itemconfigure vmin -text $vmin
-        $c itemconfigure vmax -text $vmax
-        $c coords vmax [expr $w - 5] 2
-        $c coords vmin [expr $w - 5] [expr $h - 2]
+	$c itemconfigure vmin -text $vmin
+	$c itemconfigure vmax -text $vmax
+	$c coords vmax [expr $w - 5] 2
+	$c coords vmin [expr $w - 5] [expr $h - 2]
     }
 }
@@ -711,5 +704,5 @@
     # in the midst of sending data? then bail out
     if {[llength $sendobjs_] > 0} {
-        return
+	return
     }
     # Find any new data that needs to be sent to the server.  Queue this up on
@@ -717,32 +710,32 @@
     # before we rebuild the rest.
     foreach dataobj [get] {
-        set comp [lindex [$dataobj components] 0]
-        if {![info exists obj2id_($dataobj-$comp)]} {
-            set i [lsearch -exact $sendobjs_ $dataobj]
-            if {$i < 0} {
-                lappend sendobjs_ $dataobj
-            }
-        }
+	set comp [lindex [$dataobj components] 0]
+	if {![info exists obj2id_($dataobj-$comp)]} {
+	    set i [lsearch -exact $sendobjs_ $dataobj]
+	    if {$i < 0} {
+		lappend sendobjs_ $dataobj
+	    }
+	}
     }
     if {[llength $sendobjs_] > 0} {
-        # Send off new data objects
-        $_dispatcher event -idle !send_dataobjs
+	# Send off new data objects
+	$_dispatcher event -idle !send_dataobjs
     } else {
-        # Nothing to send -- activate the proper volume
-        set first [lindex [get] 0]
-        if {"" != $first} {
-            set axis [$first hints updir]
-            if {"" != $axis} {
-                _send "up $axis"
-            }
-        }
-        foreach key [array names obj2id_ *-*] {
-            set state [string match $first-* $key]
-            _send "heightmap data visible $state $obj2id_($key)"
-            if {[info exists obj2style_($key)]} {
-                _send "heightmap transfunc $obj2style_($key) $obj2id_($key)"
-            }
-        }
-        $_dispatcher event -idle !legend
+	# Nothing to send -- activate the proper volume
+	set first [lindex [get] 0]
+	if {"" != $first} {
+	    set axis [$first hints updir]
+	    if {"" != $axis} {
+		_send "up $axis"
+	    }
+	}
+	foreach key [array names obj2id_ *-*] {
+	    set state [string match $first-* $key]
+	    _send "heightmap data visible $state $obj2id_($key)"
+	    if {[info exists obj2style_($key)]} {
+		_send "heightmap transfunc $obj2style_($key) $obj2id_($key)"
+	    }
+	}
+	$_dispatcher event -idle !legend
     }
 
@@ -759,5 +752,5 @@
 
      if {"" == $itk_option(-plotoutline)} {
-         _send "grid linecolor [Color2RGB $itk_option(-plotoutline)]"
+	 _send "grid linecolor [Color2RGB $itk_option(-plotoutline)]"
      }
     set settings_($this-theta) $view_(theta)
@@ -784,21 +777,21 @@
 itcl::body Rappture::HeightmapViewer::_zoom {option} {
     switch -- $option {
-        "in" {
-            set view_(zoom) [expr {$view_(zoom)*1.25}]
+	"in" {
+	    set view_(zoom) [expr {$view_(zoom)*1.25}]
 	    set settings_($this-zoom) $view_(zoom)
-        }
-        "out" {
-            set view_(zoom) [expr {$view_(zoom)*0.8}]
+	}
+	"out" {
+	    set view_(zoom) [expr {$view_(zoom)*0.8}]
 	    set settings_($this-zoom) $view_(zoom)
-        }
-        "reset" {
-            array set view_ {
-                theta   45
-                phi     45
-                psi     0
-                zoom	1.0
+	}
+	"reset" {
+	    array set view_ {
+		theta   45
+		phi     45
+		psi     0
+		zoom	1.0
 		pan-x	0
 		pan-y	0
-            }
+	    }
 	    set first [lindex [get] 0]
 	    if { $first != "" } {
@@ -808,7 +801,7 @@
 		}
 	    }
-            set xyz [Euler2XYZ $view_(theta) $view_(phi) $view_(psi)]
-            _send "camera angle $xyz"
-            _PanCamera
+	    set xyz [Euler2XYZ $view_(theta) $view_(phi) $view_(psi)]
+	    _send "camera angle $xyz"
+	    _PanCamera
 	    set settings_($this-theta) $view_(theta)
 	    set settings_($this-phi) $view_(phi)
@@ -817,5 +810,5 @@
 	    set settings_($this-pan-y) $view_(pan-y)
 	    set settings_($this-zoom) $view_(zoom)
-        }
+	}
     }
     _send "camera zoom $view_(zoom)"
@@ -842,10 +835,10 @@
 	set settings_($this-pan-x) $view_(pan-x)
 	set settings_($this-pan-y) $view_(pan-y)
-        return
+	return
     }
     if { $option == "click" } {
-        set click_(x) $x
-        set click_(y) $y
-        $itk_component(3dview) configure -cursor hand1
+	set click_(x) $x
+	set click_(y) $y
+	$itk_component(3dview) configure -cursor hand1
     }
     if { $option == "drag" || $option == "release" } {
@@ -861,5 +854,5 @@
     }
     if { $option == "release" } {
-        $itk_component(3dview) configure -cursor ""
+	$itk_component(3dview) configure -cursor ""
     }
 }
@@ -881,74 +874,74 @@
 itcl::body Rappture::HeightmapViewer::_rotate {option x y} {
     switch -- $option {
-        click {
-            $itk_component(3dview) configure -cursor fleur
-            array set click_ [subst {
-                x       $x
-                y       $y
-                theta   $view_(theta)
-                phi     $view_(phi)
-            }]
-        }
-        drag {
-            if {[array size click_] == 0} {
-                _rotate click $x $y
-            } else {
-                set w [winfo width $itk_component(3dview)]
-                set h [winfo height $itk_component(3dview)]
-                if {$w <= 0 || $h <= 0} {
-                    return
-                }
-
-                if {[catch {
-                    # this fails sometimes for no apparent reason
-                    set dx [expr {double($x-$click_(x))/$w}]
-                    set dy [expr {double($y-$click_(y))/$h}]
-                }] != 0 } {
-                    return
-                }
-
-                #
-                # Rotate the camera in 3D
-                #
-                if {$view_(psi) > 90 || $view_(psi) < -90} {
-                    # when psi is flipped around, theta moves backwards
-                    set dy [expr {-$dy}]
-                }
-                set theta [expr {$view_(theta) - $dy*180}]
-                while {$theta < 0} { set theta [expr {$theta+180}] }
-                while {$theta > 180} { set theta [expr {$theta-180}] }
-
-                if {abs($theta) >= 30 && abs($theta) <= 160} {
-                    set phi [expr {$view_(phi) - $dx*360}]
-                    while {$phi < 0} { set phi [expr {$phi+360}] }
-                    while {$phi > 360} { set phi [expr {$phi-360}] }
-                    set psi $view_(psi)
-                } else {
-                    set phi $view_(phi)
-                    set psi [expr {$view_(psi) - $dx*360}]
-                    while {$psi < -180} { set psi [expr {$psi+360}] }
-                    while {$psi > 180} { set psi [expr {$psi-360}] }
-                }
-
-                set view_(theta)        $theta
-                set view_(phi)          $phi
-                set view_(psi)          $psi
-                set xyz [Euler2XYZ $view_(theta) $view_(phi) $view_(psi)]
+	click {
+	    $itk_component(3dview) configure -cursor fleur
+	    array set click_ [subst {
+		x       $x
+		y       $y
+		theta   $view_(theta)
+		phi     $view_(phi)
+	    }]
+	}
+	drag {
+	    if {[array size click_] == 0} {
+		_rotate click $x $y
+	    } else {
+		set w [winfo width $itk_component(3dview)]
+		set h [winfo height $itk_component(3dview)]
+		if {$w <= 0 || $h <= 0} {
+		    return
+		}
+
+		if {[catch {
+		    # this fails sometimes for no apparent reason
+		    set dx [expr {double($x-$click_(x))/$w}]
+		    set dy [expr {double($y-$click_(y))/$h}]
+		}] != 0 } {
+		    return
+		}
+
+		#
+		# Rotate the camera in 3D
+		#
+		if {$view_(psi) > 90 || $view_(psi) < -90} {
+		    # when psi is flipped around, theta moves backwards
+		    set dy [expr {-$dy}]
+		}
+		set theta [expr {$view_(theta) - $dy*180}]
+		while {$theta < 0} { set theta [expr {$theta+180}] }
+		while {$theta > 180} { set theta [expr {$theta-180}] }
+
+		if {abs($theta) >= 30 && abs($theta) <= 160} {
+		    set phi [expr {$view_(phi) - $dx*360}]
+		    while {$phi < 0} { set phi [expr {$phi+360}] }
+		    while {$phi > 360} { set phi [expr {$phi-360}] }
+		    set psi $view_(psi)
+		} else {
+		    set phi $view_(phi)
+		    set psi [expr {$view_(psi) - $dx*360}]
+		    while {$psi < -180} { set psi [expr {$psi+360}] }
+		    while {$psi > 180} { set psi [expr {$psi-360}] }
+		}
+
+		set view_(theta)        $theta
+		set view_(phi)          $phi
+		set view_(psi)          $psi
+		set xyz [Euler2XYZ $view_(theta) $view_(phi) $view_(psi)]
 		set settings_($this-theta) $view_(theta)
 		set settings_($this-phi) $view_(phi)
 		set settings_($this-psi) $view_(psi)
-                _send "camera angle $xyz"
-                set click_(x) $x
-                set click_(y) $y
-            }
-        }
-        release {
-            _rotate drag $x $y
-            $itk_component(3dview) configure -cursor ""
-            catch {unset click_}
-        }
-        default {
-            error "bad option \"$option\": should be click, drag, release"
-        }
+		_send "camera angle $xyz"
+		set click_(x) $x
+		set click_(y) $y
+	    }
+	}
+	release {
+	    _rotate drag $x $y
+	    $itk_component(3dview) configure -cursor ""
+	    catch {unset click_}
+	}
+	default {
+	    error "bad option \"$option\": should be click, drag, release"
+	}
     }
 }
@@ -963,5 +956,5 @@
 itcl::body Rappture::HeightmapViewer::_state {comp} {
     if {[$itk_component($comp) cget -relief] == "sunken"} {
-        return "on"
+	return "on"
     }
     return "off"
@@ -977,5 +970,5 @@
 itcl::body Rappture::HeightmapViewer::_fixSettings { what {value ""} } {
     switch -- $what {
-        "legend" {
+	"legend" {
 	    if { $settings_($this-legend) } {
 		pack $itk_component(legend) -side right -fill y
@@ -983,52 +976,52 @@
 		pack forget $itk_component(legend) 
 	    }
-            set lineht [expr [font metrics $itk_option(-font) -linespace] + 4]
-            set w [expr {[winfo height $itk_component(legend)] - 2*$lineht}]
-            set h [expr {[winfo width $itk_component(legend)] - 16}]
-            set imap ""
-            set dataobj [lindex [get] 0]
-            if {"" != $dataobj} {
-                set comp [lindex [$dataobj components] 0]
-                if {[info exists obj2id_($dataobj-$comp)]} {
-                    set imap $obj2id_($dataobj-$comp)
-                }
-            }
-            if {$w > 0 && $h > 0 && "" != $imap} {
-                _send "heightmap legend $imap $w $h"
-            } else {
-                $itk_component(legend) delete all
-            }
-        }
-        "grid" {
-            if { [IsConnected] } {
-                _send "grid visible $settings_($this-grid)"
-            }
-        }
-        "axes" {
-            if { [IsConnected] } {
-                _send "axis visible $settings_($this-axes)"
-            }
-        }
-        "wireframe" {
-            if { [IsConnected] } {
-                _send "heightmap polygon $settings_($this-wireframe)"
-            }
-        }
-        "contourlines" {
-            if {[IsConnected]} {
-                set dataobj [lindex [get] 0]
-                if {"" != $dataobj} {
-                    set comp [lindex [$dataobj components] 0]
-                    if {[info exists obj2id_($dataobj-$comp)]} {
-                        set i $obj2id_($dataobj-$comp)
-                        set bool $settings_($this-contourlines)
-                        _send "heightmap linecontour visible $bool $i"
-                    }
-                }
-            }
-        }
-        default {
-            error "don't know how to fix $what: should be grid, axes, contourlines, or legend"
-        }
+	    set lineht [expr [font metrics $itk_option(-font) -linespace] + 4]
+	    set w [expr {[winfo height $itk_component(legend)] - 2*$lineht}]
+	    set h [expr {[winfo width $itk_component(legend)] - 16}]
+	    set imap ""
+	    set dataobj [lindex [get] 0]
+	    if {"" != $dataobj} {
+		set comp [lindex [$dataobj components] 0]
+		if {[info exists obj2id_($dataobj-$comp)]} {
+		    set imap $obj2id_($dataobj-$comp)
+		}
+	    }
+	    if {$w > 0 && $h > 0 && "" != $imap} {
+		_send "heightmap legend $imap $w $h"
+	    } else {
+		$itk_component(legend) delete all
+	    }
+	}
+	"grid" {
+	    if { [IsConnected] } {
+		_send "grid visible $settings_($this-grid)"
+	    }
+	}
+	"axes" {
+	    if { [IsConnected] } {
+		_send "axis visible $settings_($this-axes)"
+	    }
+	}
+	"wireframe" {
+	    if { [IsConnected] } {
+		_send "heightmap polygon $settings_($this-wireframe)"
+	    }
+	}
+	"contourlines" {
+	    if {[IsConnected]} {
+		set dataobj [lindex [get] 0]
+		if {"" != $dataobj} {
+		    set comp [lindex [$dataobj components] 0]
+		    if {[info exists obj2id_($dataobj-$comp)]} {
+			set i $obj2id_($dataobj-$comp)
+			set bool $settings_($this-contourlines)
+			_send "heightmap linecontour visible $bool $i"
+		    }
+		}
+	    }
+	}
+	default {
+	    error "don't know how to fix $what: should be grid, axes, contourlines, or legend"
+	}
     }
 }
@@ -1043,7 +1036,7 @@
 itcl::body Rappture::HeightmapViewer::_getTransfuncData {dataobj comp} {
     array set style {
-        -color rainbow
-        -levels 6
-        -opacity 0.5
+	-color rainbow
+	-levels 6
+	-opacity 0.5
     }
     array set style [lindex [$dataobj components -style $comp] 0]
@@ -1051,5 +1044,5 @@
 
     if {$style(-color) == "rainbow"} {
-        set style(-color) "white:yellow:green:cyan:blue:magenta"
+	set style(-color) "white:yellow:green:cyan:blue:magenta"
     }
     set clist [split $style(-color) :]
@@ -1058,7 +1051,7 @@
     set range [expr $limits_(vmax) - $limits_(vmin)]
     for {set i 0} {$i < [llength $clist]} {incr i} {
-        set xval [expr {double($i+1)/([llength $clist]+1)}]
-        set color [lindex $clist $i]
-        append cmap "$xval [Color2RGB $color] "
+	set xval [expr {double($i+1)/([llength $clist]+1)}]
+	set color [lindex $clist $i]
+	append cmap "$xval [Color2RGB $color] "
     }
     append cmap "1.0 [Color2RGB $color] "
@@ -1068,24 +1061,24 @@
     set wmap {}
     if {[string is int $levels]} {
-        lappend wmap 0.0 0.0
-        set delta [expr {0.125/($levels+1)}]
-        for {set i 1} {$i <= $levels} {incr i} {
-            # add spikes in the middle
-            set xval [expr {double($i)/($levels+1)}]
-            lappend wmap [expr {$xval-$delta-0.01}] 0.0
-            lappend wmap [expr {$xval-$delta}] $opacity 
-            lappend wmap [expr {$xval+$delta}] $opacity
-            lappend wmap [expr {$xval+$delta+0.01}] 0.0
-        }
-        lappend wmap 1.0 0.0
+	lappend wmap 0.0 0.0
+	set delta [expr {0.125/($levels+1)}]
+	for {set i 1} {$i <= $levels} {incr i} {
+	    # add spikes in the middle
+	    set xval [expr {double($i)/($levels+1)}]
+	    lappend wmap [expr {$xval-$delta-0.01}] 0.0
+	    lappend wmap [expr {$xval-$delta}] $opacity 
+	    lappend wmap [expr {$xval+$delta}] $opacity
+	    lappend wmap [expr {$xval+$delta+0.01}] 0.0
+	}
+	lappend wmap 1.0 0.0
     } else {
-        lappend wmap 0.0 0.0
-        set delta 0.05
-        foreach xval [split $levels ,] {
-            lappend wmap [expr {$xval-$delta}] 0.0
-            lappend $xval $opacity
-            lappend [expr {$xval+$delta}] 0.0
-        }
-        lappend wmap 1.0 0.0
+	lappend wmap 0.0 0.0
+	set delta 0.05
+	foreach xval [split $levels ,] {
+	    lappend wmap [expr {$xval-$delta}] 0.0
+	    lappend $xval $opacity
+	    lappend [expr {$xval+$delta}] 0.0
+	}
+	lappend wmap 1.0 0.0
     }
     return [list $sname $cmap $wmap]
@@ -1115,5 +1108,5 @@
 itcl::configbody Rappture::HeightmapViewer::plotoutline {
     if {[IsConnected]} {
-        _send "grid linecolor [Color2RGB $itk_option(-plotoutline)]"
+	_send "grid linecolor [Color2RGB $itk_option(-plotoutline)]"
     }
 }
@@ -1193,28 +1186,28 @@
     label $inner.title -text "View Settings" -font "Arial 10 bold"
     checkbutton $inner.grid \
-        -text "grid" \
-        -variable [itcl::scope settings_($this-grid)] \
-        -command [itcl::code $this _fixSettings grid] \
+	-text "grid" \
+	-variable [itcl::scope settings_($this-grid)] \
+	-command [itcl::code $this _fixSettings grid] \
 	-font "Arial 9"
     checkbutton $inner.axes \
-        -text "axes" \
-        -variable ::Rappture::HeightmapViewer::settings_($this-axes) \
-        -command [itcl::code $this _fixSettings axes] \
+	-text "axes" \
+	-variable ::Rappture::HeightmapViewer::settings_($this-axes) \
+	-command [itcl::code $this _fixSettings axes] \
 	-font "Arial 9"
     checkbutton $inner.contourlines \
-        -text "contour lines" \
-        -variable ::Rappture::HeightmapViewer::settings_($this-contourlines) \
-        -command [itcl::code $this _fixSettings contourlines]\
+	-text "contour lines" \
+	-variable ::Rappture::HeightmapViewer::settings_($this-contourlines) \
+	-command [itcl::code $this _fixSettings contourlines]\
 	-font "Arial 9"
     checkbutton $inner.wireframe \
-        -text "wireframe" \
+	-text "wireframe" \
 	-onvalue "wireframe" -offvalue "fill" \
-        -variable ::Rappture::HeightmapViewer::settings_($this-wireframe) \
-        -command [itcl::code $this _fixSettings wireframe]\
+	-variable ::Rappture::HeightmapViewer::settings_($this-wireframe) \
+	-command [itcl::code $this _fixSettings wireframe]\
 	-font "Arial 9"
     checkbutton $inner.legend \
-        -text "legend" \
-        -variable ::Rappture::HeightmapViewer::settings_($this-legend) \
-        -command [itcl::code $this _fixSettings legend]\
+	-text "legend" \
+	-variable ::Rappture::HeightmapViewer::settings_($this-legend) \
+	-command [itcl::code $this _fixSettings legend]\
 	-font "Arial 9"
 
