# This software is distributed under the Lesser General Public License
#
# inspector/edge_anchor_ctl.tcl
#
# Control for selecting edge_anchor
#
#------------------------------------------ CVS
#
# CVS Headers -- The following headers are generated by the CVS
# version control system. Note that especially the attribute
# Author is not necessarily the author of the code.
#
# $Source: /home/br/CVS/graphlet/lib/graphscript/inspector/edge_anchor_ctl.tcl,v $
# $Author: forster $
# $Revision: 1.6 $
# $Date: 1999/03/01 16:58:31 $
# $Locker:  $
# $State: Exp $
#
#------------------------------------------ CVS
#
# (C) University of Passau 1995-1999, Graphlet Project
#     Author: Michael Forster

package require Graphlet
package provide Graphscript [gt_version]

package require Combobox
package require Numentry

namespace eval ::GT::IS::edge_anchor_ctl {

    namespace export create
    namespace import ::GT::IS::define_attrs
    namespace import ::GT::IS::bind_attrs

    proc create { IS ctl attrs } {
	define_attrs $IS $ctl $attrs { node anchor anchor_x anchor_y }
	
	# creation
	
	frame $ctl

	create_widgets $IS $ctl $attrs
	create_layout  $IS $ctl
	
	return $ctl
    }

    proc create_widgets { IS ctl attrs } {
	variable ::GT::IS::_Options

	Combobox::create $ctl.cb \
	    -width 5 \
	    -textvariable ::GT::IS::_Value($IS,[lindex $attrs 1])

	# canvas & marker
	
	::GT::IS::create_node_canvas $ctl.c

	$ctl.c create rectangle \
	    0 0 0 0 \
	    -fill $_Options(color,marker,fill,selected) \
	    -outline $_Options(color,marker,outline,selected) \
	    -tag marker

 	$ctl.c bind marker <B1-Motion> \
	    [namespace code "ev_move_marker $IS $ctl %x %y"]

	
	# numentries
	
	Numentry::create $ctl.x \
	    -width 3 \
	    -textvariable ::GT::IS::_Value($IS,[lindex $attrs 2]) \
	    -min -1 -max 1 -step 0.1 -acceleration 5 

	Numentry::create $ctl.y \
	    -width 3 \
	    -textvariable ::GT::IS::_Value($IS,[lindex $attrs 3]) \
	    -min -1 -max 1 -step 0.1 -acceleration 5 
    }

    proc create_layout { IS ctl } {

	grid columnconfigure $ctl { 0 1 } -weight 1
	
	grid $ctl.cb - -sticky news
    }

    proc update { IS ctl } {
	variable ::GT::IS::_HaveEdges
	global GT
	bind_attrs $IS $ctl

	# Combobox
	
	if $_HaveEdges($IS) {
	    
	    $ctl.cb configure -state normal

	    set g $GT([winfo toplevel $IS],graph)	

	    if { $node != {} } {

		set values {
		    "Orthogonal"
		    "Next middle"
		    "Next corner"
		    "Explicit"
		}
		
		foreach p [$g get $node -ports] {
		    lappend values [lindex $p 0]
		}
		
		$ctl.cb configure -values $values
	    }
	    
	} else {

	    $ctl.cb configure -state disabled
	    
	}

	if { $anchor == "Explicit" } {
	    
	    grid $ctl.c  -      -sticky news
	    grid $ctl.x  $ctl.y -sticky news

	    # place marker

	    variable ::GT::IS::_Options

	    GT::pset { bx by iw ih } [::GT::IS::get_canvas_coords $ctl.c]
	    GT::pset { mw mh } $_Options(marker_size)

	    set mx [expr $bx + $iw*($anchor_x+1)/2.0]
	    set my [expr $by + $ih*($anchor_y+1)/2.0]

	    $ctl.c coords marker \
		[expr $mx - $mw/2] [expr $my - $mh/2] \
		[expr $mx + $mw/2] [expr $my + $mh/2]
	    
	} else {
	    
	    grid forget $ctl.c $ctl.x $ctl.y
	}
    }

    proc ev_move_marker { IS ctl x y } {
	bind_attrs $IS $ctl
	
	GT::pset { bx by iw ih } [::GT::IS::get_canvas_coords $ctl.c]

	set x [expr ($x-$bx)*2.0/$iw - 1]
	set y [expr ($y-$by)*2.0/$ih - 1]

	set anchor_x [expr int($x*100.0)/100.0]
	set anchor_y [expr int($y*100.0)/100.0]
    }
}

#---------------------------------------------------------------------------
#   Set emacs variables
#---------------------------------------------------------------------------
# ;;; Local Variables: ***
# ;;; mode: tcl ***
# ;;; tcl-indent-level: 4 ***
# ;;; End: ***
#---------------------------------------------------------------------------
#   end of file
#---------------------------------------------------------------------------
