proc EditNURB { LineOrSurface } {
    # tk_messageBox -message "vaL=$LineOrSurface " -type ok
    global GidPriv
    set w .gid.nurbsurfmove
    set GidPriv(FixBoundary) 1
    set GidPriv(Decrement) 0.4
    set GidPriv(LastDecrement) 0.4
    set GidPriv(FixBoundary) 1 
    set entitytypename [_ $LineOrSurface]
    InitWindow $w [_ "Edit NURBS %s" $entitytypename] EditNURBWindowGeom
    frame $w.frmMovement 
    label $w.lblNumEntity -text [_ "unselected"]
    label $w.frmMovement.lblMoveType -text [_ "Movement type"]
    
    menubutton $w.frmMovement.mnuMovement \
	    -menu $w.frmMovement.mnuMovement.m \
	    -relief raised -text [_ "Screen"] 
    menu $w.frmMovement.mnuMovement.m -tearoff 0
    $w.frmMovement.mnuMovement.m add command -label [_ "Screen"] \
	    -command " .central.s process MoveScreen ; $w.frmMovement.mnuMovement configure -text [_ "Screen"] "
    if { $LineOrSurface == "surface" } {
	$w.frmMovement.mnuMovement.m add command -label [_ "Normal"] \
	    -command " .central.s process MoveNormal ; $w.frmMovement.mnuMovement configure -text [_ "Normal"] "
    }
    $w.frmMovement.mnuMovement.m add command -label [_ "Point"] \
	    -command " .central.s process MoveToPoint ; $w.frmMovement.mnuMovement configure -text [_ "Point"] "
    $w.frmMovement.mnuMovement.m add command -label [_ "X-Axis"] \
	    -command " .central.s process MoveXAxis ; $w.frmMovement.mnuMovement configure -text [_ "X-Axis"] "
    $w.frmMovement.mnuMovement.m add command -label [_ "Y-Axis"] \
	    -command " .central.s process MoveYAxis ; $w.frmMovement.mnuMovement configure -text [_ "Y-Axis"] "
    $w.frmMovement.mnuMovement.m add command -label [_ "Z-Axis"] \
	    -command " .central.s process MoveZAxis ; $w.frmMovement.mnuMovement configure -text [_ "Z-Axis"] "
    
    
    GidHelp "$w.frmMovement.mnuMovement" \
	[_ "User can choose the mode of select points dinamically."]
    
    frame $w.frmDecrement -borderwidth 2 -relief ridge
    
    button $w.buttonMinus -text - -disabledforeground grey60 
    
    scale $w.sclDecrement -from 0 -to 1 -resolution 0.05 -showvalue 1 \
	    -highlightbackground black \
	    -orient horizontal \
	    -label [_ "Influence factor"] -variable GidPriv(Decrement)
    

    button $w.buttonPlus -text + -disabledforeground grey60 \
	    
    proc DecrementMoving { val } {
	global GidPriv
	if { $val != $GidPriv(LastDecrement) } {
	    set GidPriv(LastDecrement) $val
	    .central.s process Decrement=$val 
	}
    }
    
    GidHelp "$w.sclDecrement" \
	[_ "Value 0 only move the control point\n value 1 move all control points."]
    
    checkbutton $w.chkFixBoundary -text [_ "Fix boundary"] \
	    -variable GidPriv(FixBoundary) \
	    -command SetBoundary
    
    proc SetBoundary { } {
	global GidPriv
	if { $GidPriv(FixBoundary) } {
	    .central.s process FixBoundary
	} else {
	    .central.s process FreeBoundary
	}
    }
    
    GidHelp "$w.chkFixBoundary" \
	[_ "Fix or free the boundary control points."]
    
    
    frame $w.frmLineSpecific
    frame $w.frmSurfaceSpecific
    button $w.btnInsertKnot -text [_ "Insert knot"] -command ".central.s process InsertKnot" \
	    -disabledforeground grey60 -width 14
    button $w.btnDegreeElevate -text [_ "Degree Elevate"] -command ".central.s process DegreeElevate" \
	    -disabledforeground grey60 -width 14
    
    button $w.btnWeightChange -text [_ "Change Weight"] -command ".central.s process WeightChange" \
	    -disabledforeground grey60 -width 14
    button $w.btnCancelWeights -text [_ "Cancel Weights"] -command ".central.s process CancelWeights" \
	    -disabledforeground grey60 -width 14

    button $w.btnReparametrize -text [_ "Reparametrize"] -command ".central.s process Reparametrize" \
	    -disabledforeground grey60 -width 14

    button $w.btnInsertUKnot -text [_ "Insert U knot"] -command ".central.s process InsertUKnot" \
	    -disabledforeground grey60 -width 14
    button $w.btnInsertVKnot -text [_ "Insert V knot"] -command ".central.s process InsertVKnot" \
	    -disabledforeground grey60 -width 14
    button $w.btnDegreeUElevate -text [_ "Degree U Elevate"] -command ".central.s process DegreeUElevate" \
	    -disabledforeground grey60 -width 14
    button $w.btnDegreeVElevate -text [_ "Degree V Elevate"] -command ".central.s process DegreeVElevate" \
	    -disabledforeground grey60 -width 14

    button $w.btnSimplify -text [_ "Simplify"] -command ".central.s process Simplify" \
	    -disabledforeground grey60 -width 14


    set def_back [$w cget -background]

    frame $w.frmButtons -bg [CCColorActivo $def_back]
    button $w.frmButtons.btnApply -text [_ "Apply"] \
	    -command ".central.s process escape" \
	    -underline 0 -width 6


    button $w.frmButtons.btnSelect -text [_ "Pick"] \
	    -underline 0 \
	    -command "SelEntity $w $LineOrSurface" -width 6
	    #-command "StartEditNurbSurface $w"
   
    button $w.frmButtons.btnclose -text [_ "Close"] \
	    -command "Close $w $LineOrSurface" \
	    -underline 0 -width 6

    grid $w.frmMovement -row 1 -column 1 -pady 5
    grid $w.frmMovement.lblMoveType -row 1 -column 1
    grid $w.frmMovement.mnuMovement -row 1 -column 2

    grid $w.frmDecrement -row 2 -column 1 -columnspan 2 -padx 6 -sticky ew
    grid $w.buttonMinus -in $w.frmDecrement -row 1 -column 1 
    grid $w.sclDecrement -in $w.frmDecrement -row 1 -column 2 -sticky ew
    grid $w.buttonPlus -in $w.frmDecrement -row 1 -column 3 
    grid columnconfigure $w.frmDecrement 2 -weight 1

    grid $w.chkFixBoundary -row 3 -column 1

    if { $LineOrSurface == "surface" } {
	grid $w.frmSurfaceSpecific -row 4 -column 1 -padx 5 -pady 5 
	grid $w.btnInsertUKnot -in $w.frmSurfaceSpecific -row 1 -column 1
	grid $w.btnInsertVKnot -in $w.frmSurfaceSpecific -row 1 -column 2
	grid $w.btnDegreeUElevate -in $w.frmSurfaceSpecific -row 2 -column 1
	grid $w.btnDegreeVElevate -in $w.frmSurfaceSpecific -row 2 -column 2
	grid $w.btnWeightChange -in $w.frmSurfaceSpecific -row 3 -column 1
	grid $w.btnCancelWeights -in $w.frmSurfaceSpecific -row 3 -column 2
	grid $w.btnReparametrize -in $w.frmSurfaceSpecific -row 4 -column 1
	grid $w.btnSimplify -in $w.frmSurfaceSpecific -row 4 -column 2
    } else {
	grid $w.frmLineSpecific -row 4 -column 1 -padx 5 -pady 5 
	grid $w.btnInsertKnot -in $w.frmLineSpecific -row 1 -column 1
	grid $w.btnDegreeElevate -in $w.frmLineSpecific -row 1 -column 2
	grid $w.btnWeightChange -in $w.frmLineSpecific -row 2 -column 1
	grid $w.btnCancelWeights -in $w.frmLineSpecific -row 2 -column 2
	grid $w.btnReparametrize -in $w.frmLineSpecific -row 3 -column 1
	grid $w.btnSimplify -in $w.frmLineSpecific -row 3 -column 2
    }

   
    grid $w.frmButtons.btnApply -row 1 -column 1 -padx 5 -pady 6
    grid $w.frmButtons.btnSelect -row 1 -column 2 -padx 5 -pady 6
    grid $w.frmButtons.btnclose -row 1 -column 3 -padx 5 -pady 6
    grid $w.frmButtons -row 5 -column 1  -sticky ews -columnspan 7

    # Resize behavior management
    grid columnconf $w 1 -minsize 80 -weight 1

    grid rowconfigure $w 1 -weight 0
    grid rowconfigure $w 2 -weight 0
    grid rowconfigure $w 3 -weight 0
    grid rowconfigure $w 4 -weight 0
    grid rowconfigure $w 5 -weight 2

    # grid $w.lblNumEntity -row 5 -column 1
    
    set GidPriv(auxvar) $w.sclDecrement
    $w.buttonMinus conf -command { $GidPriv(auxvar) set \
	    [expr $GidPriv(Decrement)-0.05] }

    $w.buttonPlus conf -command { $GidPriv(auxvar) set \
	    [expr $GidPriv(Decrement)+0.05] }

    after idle [$w.sclDecrement conf -command DecrementMoving]

    EnabledDisabled $w $LineOrSurface disabled

    focus $w.frmButtons.btnSelect

    bind $w <Alt-s> "tkButtonInvoke $w.frmButtons.btnSelect"
    bind $w <Alt-c> "tkButtonInvoke $w.frmButtons.btnclose"
    bind $w <Escape> "tkButtonInvoke $w.frmButtons.btnclose"
    bind $w <Return> "tkButtonInvoke $w.frmButtons.btnApply"
}

proc CancelPick { w LineOrSurface } {
  .central.s process escape escape escape escape
    $w.frmButtons.btnSelect conf -command "SelEntity $w $LineOrSurface" -text [_ "Pick"]
}

proc Close { w LineOrSurface } {
  if { $LineOrSurface == "surface" } {
    .central.s process escape escape escape escape Geometry Edit EdNurbsSurf Close 
  } else { 
    .central.s process escape escape escape escape Geometry Edit EdNurbsLine Close 
  }  
  destroy $w
}

proc SelEntity { w LineOrSurface } {
    global GidPriv

    if { [trace vinfo GidPriv(selection)] != "" } { return }
    $w.lblNumEntity conf -text [_ "unselected"]
    
    if { $LineOrSurface == "surface" } {
	.central.s process SelectEntities JustOne Surfaces
    } else { 
	.central.s process SelectEntities JustOne Lines
    }
    $w.frmButtons.btnSelect conf -command "CancelPick $w $LineOrSurface" \
	-text [_ "Cancel"] 
    trace var GidPriv(selection) w "EndSelEntity $w $LineOrSurface ;#"
}

proc EndSelEntity { w LineOrSurface } {
    global GidPriv

    $w.frmButtons.btnSelect conf -command "cmdbreak $w $LineOrSurface" -text [_ "Cancel"]

    proc cmdbreak { w LineOrSurface } {
	.central.s process Break
	$w.frmButtons.btnSelect conf -command "SelEntity $w $LineOrSurface" -text [_ "Pick"]
    }


    foreach i [trace vinfo GidPriv(selection)] {
	trace vdelete GidPriv(selection) [lindex $i 0] [lindex $i 1] 
    }

    set num [lindex [lindex $GidPriv(selection) 0] 1]
    set nnum ""
    regexp {[0-9]*} $num nnum
    if { $nnum == "" } { return }
    # if { $nnum != "" } {
    #        $w.lblNumEntity conf -text $nnum
    #        raise $w
    # }
    set entitytypename [_ $LineOrSurface]    
    wm title $w [_ "Edit NURBS %s %d" $entitytypename $nnum]

    set GidPriv(selection) ""

    EnabledDisabled $w $LineOrSurface normal
    if { $LineOrSurface == "surface" } {
	.central.s process escape escape escape escape geometry edit EdNurbsSurf $nnum
    } else {
	.central.s process escape escape escape escape geometry edit EdNurbsLine $nnum
    }
    after idle [list delayedop changefunc "EnabledDisabled $w $LineOrSurface disabled"]
}

proc EnabledDisabled { w LineOrSurface stat } { 
    $w.frmMovement.mnuMovement conf -state $stat
    $w.sclDecrement conf -state $stat 
    if { $stat == "disabled" } {
	set entitytypename [_ "%s" $LineOrSurface]
	wm title $w [_ "Edit NURBS %s" $entitytypename]
	$w.frmButtons.btnSelect conf -command "SelEntity $w $LineOrSurface" -text [_ "Pick"]
	$w.sclDecrement conf -fg grey60 
    } else { 
	$w.sclDecrement conf -fg black
    }
    $w.buttonMinus conf -state $stat
    $w.buttonPlus conf -state $stat
    $w.chkFixBoundary conf -state $stat
    if { $LineOrSurface == "surface" } {
	#RecursiveChangeState $w.frmSurfaceSpecific $stat 
	$w.btnInsertUKnot conf -state $stat
	$w.btnInsertVKnot conf -state $stat
	$w.btnDegreeUElevate conf -state $stat
	$w.btnDegreeVElevate conf -state $stat
	$w.btnWeightChange conf -state $stat
	$w.btnCancelWeights conf -state $stat
	$w.btnReparametrize conf -state $stat
	$w.btnSimplify conf -state $stat
    } else {
	#RecursiveChangeState $w.frmLineSpecific $stat 
	$w.btnInsertKnot conf -state $stat
	$w.btnDegreeElevate conf -state $stat
	$w.btnWeightChange conf -state $stat
	$w.btnCancelWeights conf -state $stat
	$w.btnReparametrize conf -state $stat
	$w.btnSimplify conf -state $stat
    }
    $w.frmButtons.btnApply conf -state $stat
}




#proc EndEditNurbSurface { w } {
#    global GidPriv
#  tk_messageBox -message "EndEditNurbSurface, $GidPriv(IdFunction)" -type ok
#    if { GidPriv(IdFunction) == "SetGeom::EditNURBSurface" } { return }
#  tk_messageBox -message "EndEditNurbSurface, Dentro" -type ok
#    trace vdelete GidPriv(IdFunction)
#    $w.lblNumEntity conf -text $GidPriv(IdFunction)
#    # poner opciones disabled
#    raise $w
#}

#proc StartEditNurbSurface { w } {
#   global GidPriv
#  tk_messageBox -message "StartEditNurbSurface, $GidPriv(IdFunction)" -type ok
#if { [trace vinfo GidPriv(IdFunction)] != "" } { return }
#   if { $GidPriv(IdFunction) == "SetGeom::EditNURBSurface" } { return }   
#  tk_messageBox -message "StartEditNurbSurface,Dentro" -type ok
#   .central.s process escape escape escape escape Geometry Edit EdNurbsSurf 
#   $w.lblNumEntity conf -text $GidPriv(IdFunction)
#   # poner opciones enabled
#   trace var GidPriv(IdFunction) w "EndEditNurbSurface $w ;#"        
#}
	
	
