#*******************************************************************************
# E.S.O. - VLT project
#
# "@(#) $Id: rtdq4Image.tcl,v 1.14+ 1999/03/25 10:45:51 vltsccm Exp $"
#
# who       when      what
# --------  --------  ----------------------------------------------
# pbiereic  22/03/99  created

itk::usual rtdq4Image {}

class rtdq4Image {
    inherit rtd::RtdImage

    constructor {args} {
        eval itk_initialize $args

        # register with colormap handler window if it is already there (clones)
        RtdImageColors::add_image $this

        # set the colormap on this window to the one used by the rtdimage
        # and arrange for all other top level windows to use that colormap
        rtd_set_cmap $w_
        rtd_set_cmap [winfo toplevel $w_]
        TopLevelWidget::set_command "rtd_set_cmap"

	bind $canvas_ <Any-Enter> "[bind $canvas_ <Any-Enter>]; $this cfgXyv"

	make_widgets
    }

    # configure text variables for X,Y and value when cursor enters window
    # at the same time update other widgets (like RtdImagePick)

    method cfgXyv {} {
        # the RtdImage code sets this array for us to speed up the panel
        # update by using the -textvariable option
        set var $image_
        global ::$var
	
	[[winfo toplevel $w_] component x] config -textvariable ${var}(X)
	[[winfo toplevel $w_] component y] config -textvariable ${var}(Y)
	[[winfo toplevel $w_] component value] config -textvariable ${var}(VALUE)

	set pickwin [winfo toplevel $w_].pick
	if [winfo exists $pickwin] {
	    set pickb [$pickwin component pick]
	    # check if pick is enabled
	    if {"[$pickb cget -state]" != "normal"} {
		$pickwin cancel
		update
		after idle "$pickwin config -target_image $this; $pickwin pick_object"
	    }
	}
    }

    # create widgets which are not packed

    method make_widgets {} {
	itk_component add trans {rtdq4Trans $w_.trans \
		-image $this}
    }

    # if the image is smaller than the canvas window, center it
    # redefines method RtdImage::maybe_center
    
    method maybe_center {} {
        set cw [winfo width $canvas_]
        set ch [winfo height $canvas_]
        set dw [$image_ dispwidth]
        set dh [$image_ dispheight]

        if {$cw != 1} {
            if {$dw < $cw && $dw} {
                set x [expr (($dw-$cw)/2.0)/$dw]
                $canvas_ xview moveto $x
            }
            if {$dh < $ch && $dh} {
                set y [expr (($dh-$ch)/2.0)/$dh]
                $canvas_ yview moveto $y
            }
        } else {
            update
            after 100 "$this maybe_center"
            return
        }
        set_scrollregion 0 0 $dw $dh
    }
    
    # add the given increment to the current zoom factor and re-scale
    # the image
    
    method inc_zoom {inc} {
        $itk_component(trans) inc_zoom $inc
    }

    # This method is called when the colormap has been changed to
    # update the display

    method update_colors {} {
	[[winfo toplevel $w_] component colorramp] update_colors
    }

    # -- options --

    itk_option define -graphics graphics Graphics {1} {}

    # -- protected vars -- 

}