#!/bin/sh
#CopyMove  -*- TCL -*- Created: ramsan Feb-1997, Modified: ramsan Feb-1997

#----------------------CopyMove ALONE---------------------------
# the next line restarts using wish \
	exec wish4.2 "$0" "$@"
	
	set CopyMoveRunningAlone 0
# catch {
#     if {$argv0 == [info script] } {
#         set CopyMoveRunningAlone 1
#         set tcl_interactive 0
#         set tk_strictMotif 1
    
#         if { [catch { set GIDDEFAULT $env(GIDDEFAULT)}]!=1 } {
#             set GIDDEFAULTTCL $GIDDEFAULT
#             catch { set GIDDEFAULTTCL $env(GIDDEFAULTTCL) }
#             set auto_path "$GIDDEFAULTTCL"
#             source $GIDDEFAULTTCL/init.tcl
#             source $GIDDEFAULTTCL/tk.tcl
#         }
#         wm withdraw .
#     }
# }

#  LOOK AT THE END OF THE FILE !!!
#-------------------------------------------------------------------
		    
		    
proc ChangeTransfInCopy  {name1 name2 op} {
    global CopyMoveInfo GidPriv

    set f $CopyMoveInfo(canvas).frame

    if { $CopyMoveInfo(Transformation) == "Sweep" && $CopyMoveInfo(EntitiesType) == "AllTypes" } {
	if { [catch { set whatuse [lindex [.central.s info Project] 4] }] } {
	    set whatuse GEOMETRYUSE
	}
	if { $whatuse == "GEOMETRYUSE" } {                                   
	    #::GidUtils::SetWarnLine [_ "Sweep cannot be used with all types selection"]
	    set win [winfo toplevel $f].err
	    tk_dialogRAM $win [_ "Error"] [_ "Sweep cannot be used with all types selection"] error 0 [_ "OK"]             
	    after idle [list set CopyMoveInfo(Transformation) $CopyMoveInfo(Transformation_old)]
	    return
	}
    }

    if { $CopyMoveInfo(Transformation) == "Offset" && 
    $CopyMoveInfo(EntitiesType) != "Elements" && 
    $CopyMoveInfo(EntitiesType) != "Lines" && 
    $CopyMoveInfo(EntitiesType) != "Surfaces" } {        
	set win [winfo toplevel $f].err
	tk_dialogRAM $win [_ "Error"] \
		[_ "Offset can only be used for lines, surfaces or mesh elements"] \
		error 0 [_ "OK"]   
	after idle [list set CopyMoveInfo(Transformation) $CopyMoveInfo(Transformation_old)]
	return
    }
    foreach i [winfo children $f] {
	destroy $i
    }
    for {set i 1} {$i <= 3} {incr i} {
	if { ![info exists CopyMoveInfo(Num,$i)] } {
	    set CopyMoveInfo(Num,$i) ""
	}
	foreach axe "x y z" {
	    if { ![info exists CopyMoveInfo($axe,$i)] } {
		set CopyMoveInfo($axe,$i) 0.0
	    }
	}
    }
    if { $CopyMoveInfo(what) == "Copy" } {     
	switch $CopyMoveInfo(Transformation) \
		Translation { CopyTrans_ui $f } \
		Rotation { CopyRot_ui $f } \
		Mirror { CopyMirror_ui $f } \
		Scale { CopyScale_ui $f } \
		Offset { CopyOffset_ui $f } \
		Sweep { CopySweep_ui $f } \
		Align { CopyAlign_ui $f }
    } else {
	switch $CopyMoveInfo(Transformation) \
		Translation { CopyTrans_ui $f } \
		Rotation { CopyRot_ui $f } \
		Mirror { CopyMirror_ui $f } \
		Scale { CopyScale_ui $f } \
		Offset { CopyOffset_ui $f } \
		Align { CopyAlign_ui $f }
    }
    
    update idletasks
    $CopyMoveInfo(canvas) conf -scrollregion "0 0 \
	    [winfo width $f] \
	    [winfo height $f]"
    
    
    foreach i [winfo children $f] {
	bind $i <Tab> {+focus [tk_focusNext %W] ; EnteringFocusInCopy \
		[tk_focusNext %W] ; break}
	bind $i <Shift-Tab> {+focus [tk_focusPrev %W] ; \
		EnteringFocusInCopy [tk_focusPrev %W] ; break}
    }
    SetDisabledInCopyNA
    set CopyMoveInfo(Transformation_old) $CopyMoveInfo(Transformation)
}

proc EnteringFocusInCopy { w } {
    global CopyMoveInfo
    set c $CopyMoveInfo(canvas)
    
    set sr [$c cget -scrollregion]
    
    set val [expr double([winfo y $w])/[lindex $sr 3]-0.1]
    if { [expr abs([lindex [$c yview] 0] - $val)] > 0.05 } {
	$c yview moveto $val
    }
    catch { 
	$w sel from 0
	$w sel to end
    }
    
}

proc SetFocusTo { varname } {
    global CopyMoveInfo
    foreach i [winfo children $CopyMoveInfo(canvas).frame] {
	catch {
	    if { [$i cget -textvariable] == $varname } {
		focus $i
		EnteringFocusInCopy $i
		break
	    }
	}
    }
}

proc SetDisabledInCopy {name1 name2 op} {
    SetDisabledInCopyNA
}
proc SetDisabledInCopyNA {} {
    global CopyMoveInfo GidPriv
    
    set w $CopyMoveInfo(mainframe)

    if { [catch { set whatuse [lindex [.central.s info Project] 4] }] } {
	set whatuse GEOMETRYUSE
    }

    if { ( $whatuse != "GEOMETRYUSE") && ( $whatuse != "MESHUSE") } {
	$w.mcopy conf -state normal
	$w.mcopy conf -fg black
	
	$w.doextrude conf -state normal
	return
    }

    if { $CopyMoveInfo(Transformation) == "Offset" && \
		 $CopyMoveInfo(EntitiesType) != "Elements" && \
	 $CopyMoveInfo(EntitiesType) != "Lines" && \
		 $CopyMoveInfo(EntitiesType) != "Surfaces" } {
	set CopyMoveInfo(EntitiesType) $CopyMoveInfo(EntitiesType_old)
	set win [winfo toplevel $w].err
	tk_dialogRAM $win [_ "error"] \
	    [_ "Offset can only be used for lines, surfaces or mesh elements"] \
	    error 0 [_ "OK"]
	return
    }


    $w.checkbutton#1 conf -state normal        
    $w.mcopy conf -state normal
    $w.createcontact conf -state normal
    .wcopymove.select conf -command "AcceptInCopy .wcopymove"


    if { $CopyMoveInfo(what) == "Move" } {
	$w.doextrude conf -state disabled
	$w.createcontact conf -state disabled
	set GidPriv(CopyMoveMCopies) 1
	$w.mcopy conf -state disabled
	$w.mcopy conf -fg grey60
    } else {
	if { $CopyMoveInfo(EntitiesType) == "Points" || 
	$CopyMoveInfo(EntitiesType) == "Lines" || 
	$CopyMoveInfo(EntitiesType) == "Surfaces" || 
	$CopyMoveInfo(EntitiesType) == "Nodes" || 
	$CopyMoveInfo(EntitiesType) == "Elements" } {
	    $w.doextrude conf -state normal
	} else { 
	    $w.doextrude conf -state disabled 
	    set GidPriv(CopyMoveDoExtrude) No
	}

	if { $CopyMoveInfo(EntitiesType) == "Surfaces" } {
	    if { $CopyMoveInfo(Transformation) != "Sweep" } { 
		$w.createcontact conf -state normal
	    }
	} else { 
	    $w.createcontact conf -state disabled 
	    set GidPriv(CopyMoveCreateContacts) 0
	}
	if { $CopyMoveInfo(Transformation) == "Mirror" } {
	    set GidPriv(CopyMoveMCopies) 1
	    $w.mcopy conf -state disabled
	    $w.mcopy conf -fg grey60
	}
	if { $CopyMoveInfo(Transformation) == "Align" } {
	    set GidPriv(CopyMoveMCopies) 1
	    $w.mcopy conf -state disabled
	    $w.mcopy conf -fg grey60
	    $w.createcontact conf -state disabled 
	    set GidPriv(CopyMoveCreateContacts) 0
	}
    }
    set CopyMoveInfo(EntitiesType_old) $CopyMoveInfo(EntitiesType)   
}

proc EnterNumInCopy { num } {
    global CopyMoveInfo
    set inum $CopyMoveInfo(Num,$num)
    set w [winfo parent $CopyMoveInfo(mainframe)].err

    if { [regexp {^[ \t]*$} $inum] } { return void}

    if { ![regexp {^[ \t]*[0-9]+[ \t]*$} $inum] } {
	tk_dialogRAM $w [_ "error"] [_ "Number not valid"] error 0 [_ "OK"]
	SetFocusTo CopyMoveInfo(Num,$num)
	set CopyMoveInfo(Num,$num) ""
	return error
    }
    if { [regexp {^[ \t]*0+[ \t]*$} $inum] } {
	tk_dialogRAM $w [_ "error"] [_ "Number 0 not valid"] error 0 [_ "OK"]
	SetFocusTo CopyMoveInfo(Num,$num)
	set CopyMoveInfo(Num,$num) ""
	return error
    }
    set res [.central.s info Coordinates $inum]

    if { $res == "" } {
	tk_dialogRAM $w [_ "error"] [_ "Point number %d doesn't exist" $inum] error 0 [_ "OK"]
	SetFocusTo CopyMoveInfo(Num,$num)
	set CopyMoveInfo(Num,$num) ""
	return error
    }

    regexp {([^ {]*)[ ]+([^ ]*)[ ]+([^ }]*)} $res trash p(x) p(y) p(z)
    foreach i "x y z" {
	if { [expr abs($p($i)-$CopyMoveInfo($i,$num))] > 1e-7 } {
	    set CopyMoveInfo(Num,$num) ""
	    return void
	}
    }
}


proc PickPointInMove { num } {
    global CopyMoveInfo    
    if { $num == 1 } {
	set msg [_ "Enter first point (ESC to leave)"]
    } elseif { $num == 2 } {
	set msg [_ "Enter second point (ESC to leave)"]
    } elseif { $num == 3 } {
	set msg [_ "Enter third point (ESC to leave)"]
    } else {
	set msg [_ "Enter point (ESC to leave)"]
    }
    set p1 [::GidUtils::GetCoordinates $msg]
    if { $p1 != "" } {
	if { $::GidPriv(selcoord,id) > 0 } {
	    set CopyMoveInfo(Num,$num) $::GidPriv(selcoord,id)
	} else {
	    set CopyMoveInfo(Num,$num) ""
	}
	set CopyMoveInfo(x,$num) [lindex $p1 0]
	set CopyMoveInfo(y,$num) [lindex $p1 1]
	set CopyMoveInfo(z,$num) [lindex $p1 2]
    } else {
	GidUtils::SetWarnLine [_ "Leaving"]
    }
}

proc AcceptInCopy { w } {
    global CopyMoveInfo GidPriv
   
    
    for {set i 1} {$i <= 6} {incr i} {
	if { $CopyMoveInfo(Transformation) == "Translation" } {
	    if { $i == 3 } break
	} elseif { $CopyMoveInfo(Transformation) == "Rotation" } {
	    if { $i == 3 || ( $i == 2 && [lindex $CopyMoveInfo(TwoDim) 0] == 1 ) } break
	} elseif { $CopyMoveInfo(Transformation) == "Mirror" } {
	    if { $i == 4 || ( $i == 3 && [lindex $CopyMoveInfo(TwoDim) 0] == 1 ) } break  
	} elseif { $CopyMoveInfo(Transformation) == "Scale" } {
	    if { $i == 3 } break  
	} elseif { $CopyMoveInfo(Transformation) == "Offset" } { 
	    break
	} elseif { $CopyMoveInfo(Transformation) == "Sweep" } { 
	    break
	} elseif { $CopyMoveInfo(Transformation) == "Align" } {
	    if { $i==7 || ( $i == 5 && [lindex $CopyMoveInfo(TwoDim) 0] == 1 ) } break  
	}


	if { $i == 2 && $CopyMoveInfo(Transformation) == "Scale" } {
	    set ret void
	} else {
	    set ret [EnterNumInCopy $i]
	}
	if { $ret == "error" } { return }
	if { $ret == "void" } {
	    set Number($i) ""
	    foreach axe "x y z" {
		if { [regexp \
		        {^[  ]*$} $CopyMoveInfo($axe,$i)] || ![regexp \
		        {^[  ]*([+-]?[0-9]*\.?[0-9]*([eE][+-]?[0-9]*)?)[  ]*$} \
		        $CopyMoveInfo($axe,$i) trash number] } {
		    tk_dialogRAM $w.err [_ "error"] [_ "Number not valid"] \
		        error 0 [_ "OK"]
		    SetFocusTo CopyMoveInfo($axe,$i)
		    return
		}
		append Number($i) $number
		if { $axe != "z" } { append Number($i) , }
	    }
	} else { catch { unset Number($i) } }
    }

    if { ![regexp {^[  ]*[+]?[0-9]*[1-9]+[0-9]*[  ]*$} $GidPriv(CopyMoveMCopies)] } {
	tk_dialogRAM $w.err [_ "error"] [_ "Wrong number of copies"] \
	    error 0 [_ "OK"]
	return
    }
    if { $CopyMoveInfo(Transformation) == "Rotation" && ([regexp \
	    {^[  ]*$} $CopyMoveInfo(Degree)]   || ![regexp \
	    {^[  ]*([+-]?[0-9]*\.?[0-9]*([eE][+-]?[0-9]*)?)[  ]*$} \
	    $CopyMoveInfo(Degree)]) } {
	tk_dialogRAM $w.err [_ "error"] [_ "Angle not valid"] \
		error 0 [_ "OK"]
	SetFocusTo CopyMoveInfo(Degree)
	return
    } elseif { $CopyMoveInfo(Transformation) == "Offset" && ([regexp \
	    {^[  ]*$} $CopyMoveInfo(OffsetFactor)]   || ![regexp \
	    {^[  ]*([+-]?[0-9]*\.?[0-9]*([eE][+-]?[0-9]*)?)[  ]*$} \
	    $CopyMoveInfo(OffsetFactor)]) } {
	tk_dialogRAM $w.err [_ "error"] [_ "Offset factor not valid"] \
		error 0 [_ "OK"]
	SetFocusTo CopyMoveInfo(OffsetFactor)
	return
    } elseif { $CopyMoveInfo(Transformation) == "Sweep" } {
	set msgerr ""
	if { $CopyMoveInfo(path) == "" } {
	    set msgerr [_ "No path line entered"]   
	    tk_dialogRAM $w.err [_ "error"] $msgerr error 0 [_ "OK"]
	    return
	}
	if { $CopyMoveInfo(TwistAngle) == "" } {
	    set CopyMoveInfo(TwistAnge) 0.0
	}
	if { ($CopyMoveInfo(x,1) == "") || ($CopyMoveInfo(z,1) == "") \
	    || ($CopyMoveInfo(y,1) == "") } {
	    if { $CopyMoveInfo(EnabledCurveTwist) == 1 } {
		set msgerr [_ "Bad direction in twist mode."] 
		tk_dialogRAM $w.err [_ "error"] $msgerr error 0 [_ "OK"]
		return
	    }
	}
	if { $CopyMoveInfo(EntitiesType) == "Points" } {
	    if { $GidPriv(CopyMoveDoExtrude) == "Surfaces" || \
		$GidPriv(CopyMoveDoExtrude) == "Volumes" } {
	       set msgerr [_ "Sorry, cannot extrude to %s." $GidPriv(CopyMoveDoExtrude)]
		tk_dialogRAM $w.err [_ "error"] $msgerr error 0 [_ "OK"]
		return
	    }
	} elseif { $CopyMoveInfo(EntitiesType) == "Lines" } {
	    if { $GidPriv(CopyMoveDoExtrude) == "Volumes" } {
		set msgerr [_ "Sorry, cannot extrude to %s." $GidPriv(CopyMoveDoExtrude)]
		tk_dialogRAM $w.err [_ "error"] $msgerr error 0 [_ "OK"]
		return
	    }
	}
    }

    if { [catch { set whatuse [lindex [.central.s info Project] 4] }] } {
	set whatuse GEOMETRYUSE
    }

    set res ".central.s process escape escape escape escape utilities"
    
    if { ( $whatuse == "GEOMETRYUSE") || ( $whatuse == "MESHUSE") } {
	lappend res $CopyMoveInfo(what)
	lappend res $CopyMoveInfo(EntitiesType)
	if { $GidPriv(DuplicateEntities) != 0 } {
	    lappend res Duplicate
	}
	if { $GidPriv(CopyMoveDoExtrude) == "Lines" } {
	    lappend res DoExtrude Lines
	} elseif { $GidPriv(CopyMoveDoExtrude) == "Surfaces" } {
	    lappend res DoExtrude Surfaces
	} elseif { $GidPriv(CopyMoveDoExtrude) == "Volumes" } {
	    lappend res DoExtrude Volumes
	}
	if { $GidPriv(CopyMoveCreateContacts) == 1 } {
	    lappend res Contacts
	}
	if { $GidPriv(CopyMoveMaintainLayers)  == 1 } {
	    lappend res MaintainLayers
	}
	if { $GidPriv(CopyMoveMCopies) != 1 } {
	    lappend res MCopy
	    lappend res $GidPriv(CopyMoveMCopies)
	}
    } else {
	# Postprocess
	lappend res transformation 
	if { $GidPriv(CopyMoveMCopies) != 1 } {
	    lappend res RepeatTransf
	    lappend res $GidPriv(CopyMoveMCopies)
	}
	#lappend res transformation
	if { $GidPriv(PostDuplicateEntities)} {
	    if { $CopyMoveInfo(PostDuplicateWhat) == "Create volume/surface" } {
	    lappend res create yes
	    } else {
		# $GidPriv(PostDuplicateWhat) == "Extrude sets"
		lappend res create extrude
	    }
	}
    }

    lappend res $CopyMoveInfo(Transformation)


    for {set i 1} {$i <= 6} {incr i} {
	if { $CopyMoveInfo(Transformation) == "Translation" } {
	    if { ![info exists Number($i)] } {
		lappend res FJoin $CopyMoveInfo(Num,$i)
	    } else {
		lappend res FNoJoin $Number($i)
	    }
	    if { $i == 2 } break
	} elseif { $CopyMoveInfo(Transformation) == "Rotation" } {
	    if { $i == 2 && [lindex $CopyMoveInfo(TwoDim) 0] == 1 } {
		lappend res TwoDim
		lappend res $CopyMoveInfo(Degree)
		break
	    } elseif { ![info exists Number($i)] } {
		lappend res FJoin $CopyMoveInfo(Num,$i)
	    } else {
		lappend res FNoJoin $Number($i)
	    }
	    if { $i == 2 } { 
		lappend res $CopyMoveInfo(Degree)
		break
	    }
	} elseif { $CopyMoveInfo(Transformation) == "Mirror" } {
	    if { $i == 3 && [lindex $CopyMoveInfo(TwoDim) 0] == 1 } {
		lappend res TwoDim
		break
	    } elseif { ![info exists Number($i)] } {
		lappend res FJoin $CopyMoveInfo(Num,$i)
	    } else {
		lappend res FNoJoin $Number($i)
	    }
	    if { $i == 3 } break
	} elseif { $CopyMoveInfo(Transformation) == "Scale" } {
	    if { ![info exists Number($i)] } {
		lappend res FJoin $CopyMoveInfo(Num,$i)
	    } else {
		lappend res FNoJoin $Number($i)
	    }
	    if { $i == 2 } break
	} elseif { $CopyMoveInfo(Transformation) == "Offset" } { 
	    lappend res $CopyMoveInfo(OffsetFactor)
	    if { $i == 1 } break
	} elseif { $CopyMoveInfo(Transformation) == "Sweep" } { 
	    lappend res $CopyMoveInfo(path)
	    lappend res $CopyMoveInfo(TwistAngle)
	    lappend res $CopyMoveInfo(TwistMode)
	    if { $GidPriv(CopyMoveDoExtrude) == "No" } {
		if { $CopyMoveInfo(EnabledCurveTwist) == 0 } {
		    lappend res 2
		} else {
		    lappend res 3
		}
	    } else {
		if { $CopyMoveInfo(EnabledCurveTwist) == 0 } {
		    lappend res 0
		} else {
		    lappend res 1
		}
	    }
	    lappend res $CopyMoveInfo(x,1) $CopyMoveInfo(y,1) $CopyMoveInfo(z,1)
	    if { $i == 1 } break
	} elseif { $CopyMoveInfo(Transformation) == "Align" } {            
	    if { ($i == 5 && [lindex $CopyMoveInfo(TwoDim) 0] == 1) } {
		lappend res TwoDim
		break
	    } elseif { ![info exists Number($i)] } {
		lappend res FJoin $CopyMoveInfo(Num,$i)
	    } else {
		lappend res FNoJoin $Number($i)
	    }
	    if { $i == 6 } break  
	}
    }

    # That's for Windows
    #focus .central.s
    gidmakecurrent
    eval $res

    if { ( $whatuse == "GEOMETRYUSE") || ( $whatuse == "MESHUSE") } {
	set SmallWinSelecting [.central.s info variable SmallWinSelecting]
	FinishButton $w $w.buts [_ "Press 'Finish' to end selection"] \
		SetDisabledInCopyNA disableall $SmallWinSelecting
    }

}

source $GIDDEFAULTTCL/CopyMain.ui.tcl
source $GIDDEFAULTTCL/CopyTrans.ui.tcl
source $GIDDEFAULTTCL/CopyRot.ui.tcl
source $GIDDEFAULTTCL/CopyScale.ui.tcl
source $GIDDEFAULTTCL/CopyMirror.ui.tcl
source $GIDDEFAULTTCL/CopyOffset.ui.tcl
source $GIDDEFAULTTCL/CopySweep.ui.tcl
source $GIDDEFAULTTCL/CopyAlign.ui.tcl


proc PostTransformations { { w .wtransformation } } {
    if { [catch { set whatuse [lindex [.central.s info Project] 4] }] } {
	set whatuse GEOMETRYUSE
    }
    if { ( $whatuse != "GEOMETRYUSE") && ( $whatuse != "MESHUSE") } {
	CopyMove "Copy" $w
    }
}

proc PostChangedShowTransf { name1 name2 op} {
    global CopyMoveInfo GidPriv
    
    if { $GidPriv(CopyMoveDoExtrude) == "Show" } {
	.central.s process utilities transformation usetransfs Yes
    } else {
	.central.s process utilities transformation usetransfs No
    }
}

proc PostChangedDuplicate { name1 name2 op} {
    global CopyMoveInfo GidPriv

    if { $GidPriv(PostDuplicateEntities) } {
	#$CopyMoveInfo(mainframe).doextrudel conf -foreground grey60
	#$CopyMoveInfo(mainframe).doextrude conf -state disabled
	$CopyMoveInfo(mainframe).fopt.bdellast conf -state disabled
	$CopyMoveInfo(mainframe).fopt.bclearall conf -state disabled

	destroy $CopyMoveInfo(mainframe).doextrude
	menubutton $CopyMoveInfo(mainframe).doextrude \
		-disabledforeground grey60 \
		-text menubutton 
	set menu [ tk_optionMenuRAM $CopyMoveInfo(mainframe).doextrude CopyMoveInfo(PostDuplicateWhat) \
		[list [_ "Create volume/surface"] [_ "Extrude surface"]] \
		{"Create volume/surface" "Extrude surface"} ]
	
	$CopyMoveInfo(mainframe).doextrude conf -bd 1 -highlightthickness 1
	$menu conf -bd 1 -activeborderwidth 1
	
	grid $CopyMoveInfo(mainframe).doextrude -in $CopyMoveInfo(mainframe).fopt -row 0 -column 0 \
		-sticky we -pady 1 -padx 0 -ipady 1 -columnspan 2

    } else {
	$CopyMoveInfo(mainframe).doextrudel conf -foreground black
	#$CopyMoveInfo(mainframe).doextrude conf -state normal
	$CopyMoveInfo(mainframe).fopt.bdellast conf -state normal
	$CopyMoveInfo(mainframe).fopt.bclearall conf -state normal

	destroy $CopyMoveInfo(mainframe).doextrude
	menubutton $CopyMoveInfo(mainframe).doextrude \
		-disabledforeground grey60 \
		-text menubutton -width 7
	set menu [ tk_optionMenuRAM $CopyMoveInfo(mainframe).doextrude GidPriv(CopyMoveDoExtrude) \
		[list [_ "Show"] [_ "Hide"]] \
		{Show Hide} ]
	
	$CopyMoveInfo(mainframe).doextrude conf -bd 1 -highlightthickness 1
	$menu conf -bd 1 -activeborderwidth 1
	
	grid $CopyMoveInfo(mainframe).doextrude -in $CopyMoveInfo(mainframe).fopt -row 0 -column 0 \
		-sticky e -pady 1 -padx 0 -ipady 1
    }
}

proc PreCloseWin { w} {
    global CopyMoveInfo
    if { [catch { set whatuse [lindex [.central.s info Project] 4] }] } {
	set whatuse GEOMETRYUSE
    }
    if { ( $whatuse == "GEOMETRYUSE") || ( $whatuse == "MESHUSE") } {
	trace vdelete CopyMoveInfo(Transformation) w ChangeTransfInCopy
	trace vdelete CopyMoveInfo(EntitiesType) w SetDisabledInCopy
	trace vdelete CopyMoveInfo(TwistMode) w ChangeTwistOptions
    }
    destroy $w
}

proc PostCloseWin { w} {
    global CopyMoveInfo GidPriv

    if { [catch { set whatuse [lindex [.central.s info Project] 4] }] } {
	set whatuse GEOMETRYUSE
    }
    
    if { ( $whatuse == "GEOMETRYUSE") || ( $whatuse == "MESHUSE") } {
    } else {
	#PostTransformations
	trace vdelete GidPriv(PostDuplicateEntities) w PostChangedDuplicate
	trace vdelete GidPriv(CopyMoveDoExtrude) w PostChangedShowTransf

    }

    destroy $w
}

proc UpdateCopyMove { { w .wcopymove } } {
    if {![winfo exists $w]} return
    
    set what [wm title $w]
    if { $what == [_ "Copy"] } {
	set what "Copy"
    } elseif { $what == [_ "Move"] } {
	set what "Move"
    }
    CopyMove $what
}

proc CopyMove { what { w .wcopymove } } {
    global CopyMoveInfo GidPriv    

    if { [catch { set whatuse [lindex [.central.s info Project] 4] }] } {
	set whatuse GEOMETRYUSE
    }

    if { $what == [_ "Transform"]} { 
	set whatuse POSTUSE
    }

    if { ( $whatuse == "GEOMETRYUSE") || ( $whatuse == "MESHUSE") } {
	if { ![info exists GidPriv(DuplicateEntities)] } {
	    set GidPriv(DuplicateEntities) 0
	}
	if { ![info exists GidPriv(CopyMoveDoExtrude)] } {
	    set GidPriv(CopyMoveDoExtrude) No
	}
	if { ![info exists GidPriv(CopyMoveMaintainLayers)] } {
	    set GidPriv(CopyMoveMaintainLayers) 1
	}
    } else {
	if { ![info exists GidPriv(CopyMoveDoExtrude)] } {
	    set GidPriv(CopyMoveDoExtrude) Show
	}
	if { ![info exists GidPriv(PostDuplicateEntities)] } {
	    set GidPriv(PostDuplicateEntities) 0
	}
    }
    if { ![info exists GidPriv(CopyMoveCreateContacts)] } {
	set GidPriv(CopyMoveCreateContacts) 0
    }
    if { ![info exists GidPriv(CopyMoveMCopies)] } {
	set GidPriv(CopyMoveMCopies) 1
    }

    if { ( $whatuse == "GEOMETRYUSE") || ( $whatuse == "MESHUSE") } {
	if { $what == "Copy" } {
	    set title [_ "Copy"]
	} elseif { $what == "Move" } {
	    set title [_ "Move"]
	} else { 
	    set title [_ $what]
	}
	InitWindow $w $title CopyMoveWindowGeom "CopyMove $what"
    } else {
	if { $w == ".wcopymove"} {
	    set w .wtransformation
	}
	InitWindow $w [_ "Transformations"] PostTransformWindowGeom "CopyMove Transform"
	#PostTransformations
	trace vdelete GidPriv(PostDuplicateEntities) w PostChangedDuplicate
    }
    catch { unset CopyMoveInfo }

    set CopyMoveInfo(whatwhat) $what

    frame $w.f -relief ridge -bd 2
    set CopyMoveInfo(canvas) [CopyMain_ui $w.f]
    set CopyMoveInfo(mainframe) $w.f

    set CopyMoveInfo(what) $what

    frame $w.buts -bg [CCColorActivo [$w cget -background]]

    if { ( $whatuse == "GEOMETRYUSE") || ( $whatuse == "MESHUSE") } {
	button $w.select -text [_ "Select"] -command "AcceptInCopy $w" -bd 1 -width 8
	button $w.cancel -text [_ "Cancel"] -command "PreCloseWin $w" -bd 1 -width 8
    } else {
	button $w.select -text [_ "Do it"] -command "AcceptInCopy $w" -bd 1 -width 8
	button $w.cancel -text [_ "Close"] -command "PostCloseWin $w" -bd 1 -width 8
    }

    grid $w.f -ipadx 2 -pady 2 -sticky nswe
    grid $w.buts -sticky ew
    grid $w.select $w.cancel -in $w.buts -pady 3 -ipady 0 -padx 0 -ipadx 0 -padx 5
    grid columnconf $w 0 -weight 1
    #grid columnconf $w 1 -weight 1
    grid rowconf $w 0 -weight 1

    frame $CopyMoveInfo(canvas).frame -bd 0
    $CopyMoveInfo(canvas) create window 0 0 -anchor nw -window \
	    $CopyMoveInfo(canvas).frame

    trace var CopyMoveInfo(Transformation) w ChangeTransfInCopy
    trace var CopyMoveInfo(EntitiesType) w SetDisabledInCopy
    set CopyMoveInfo(Transformation_old) "Translation"
    set CopyMoveInfo(Transformation) "Translation"

    set CopyMoveInfo(EntitiesType_old) $CopyMoveInfo(EntitiesType)

    if { ( $whatuse == "GEOMETRYUSE") || ( $whatuse == "MESHUSE") } {
    } else {
	trace variable GidPriv(CopyMoveDoExtrude) w PostChangedShowTransf
	trace variable GidPriv(PostDuplicateEntities) w PostChangedDuplicate
    }

    SetDisabledInCopyNA

    bind $w <Control-a> [bind .gid <Control-a>]

}


#----------------------CopyMove ALONE---------------------------
# if { $CopyMoveRunningAlone == 1 } {
#     CopyMove Copy
# } 
#-----------------------------------------------------------------
