# $Source: /usr/local/SimSoft/Sfx3D/bin/Gsl.tcl,v $
# $Revision: 1.10 $
# $Author: baum $
# $Date: 2001/12/06 13:26:51 $
#
# Login name of the user who locked the revision
# $Locker:  $
# Tag name used to checkout the file
# $Name: Release-14 $
# State assigned to revision
# $State: Exp $
#
#
# $Log: Gsl.tcl,v $
# Revision 1.10  2001/12/06 13:26:51  baum
# -RZS vervollständigt letzter commit hatte Fehler :-)
#
# Revision 1.9  2001/11/29 15:18:09  baum
# -Realzeitsteuerung eingebaut
# -Handsteuerung eingebaut
#

#############################################################
global baust
#nur für die Handsteuerung
#############################################################
if {$tcl_platform(platform) == "windows"} {
  set sfxSmallMinsize  {603 61}
  set sfxSmallMaxsize  {2000 61}
  set sfxSmallGeometry {603x61}
#-------Handsteuerung---------  
  set sfxrzsMinsize  {1000 61}
  set sfxrzsMaxsize  {2000 234}
  set sfxrzsGeometry {1000x234}
#-----------------------------
  set sfxBigMinsize    {603 234}
  set sfxBigMaxsize    {2000 2000}
  set sfxBigGeometry   {603x234}
  font create SFXFont -family Courier -size 10 -weight normal -slant roman \
                      -underline 0 -overstrike 0
} else {
  set sfxSmallMinsize  "623 59"
  set sfxSmallMaxsize  {2000 59}
  set sfxSmallGeometry {623x59}
  set sfxBigMinsize    {623 234}
  set sfxBigMaxsize    {2000 2000}
  set sfxBigGeometry   {623x234}
  font create SFXFont -family Courier -size 12 -weight normal -slant roman \
                      -underline 0 -overstrike 0
}

if { [catch {set ssylay $env(SSYLAY)}]} {set ssylay "/"};

proc fileDialog {w  defExt  action} {
    #   Type names		Extension(s)	Mac File Type(s)
    #
    #---------------------------------------------------------
    set types {
        {"Modelle"              {.2gs}}
		  {"XML-Modelle" 			  {.xml}}
        {"Text files"           {.txt .doc}}
        {"Text files"           {}           TEXT}
        {"All files"            *}
    }
    global ssylay
    global ttpfile
    if {[expr {$ssylay == ""}] || ![file isdirectory $ssylay]} { 
      set ssylay ".."
    }
    if {$action == "open"} {
	  set file [tk_getOpenFile -initialdir $ssylay -filetypes $types \
                -defaultextension $defExt -parent $w]

# ttpfile ist für die Handsteuerung von Bedeutung, damit werden die Bausteintypen
# des aktuellen Modells bekanntgemacht, siehe proc's initbst u. ttples 
          set ttpfile $file
          regsub .2gs $ttpfile .ttp ttpfile
# mit Befehl regsub wird dem ttpfile die Endung .2gs in .ttp geändert                
    } else {
      set file [tk_getSaveFile -filetypes $types -parent $w \
                -initialfile Untiteled -defauleextension .$defExt]
    }
    return $file
}

proc put_win { str } {
  global AusgabeFenster
  $AusgabeFenster insert end $str;
  $AusgabeFenster yview moveto 1.0;
}

#======================Realzeit, Modelltechnik , Hla =============
set modtechon 0
set realzeit 0
set rzsmanuell 0
set hla 0
set hlaAsynchronousDelivery 0
set hlaTimeConstrained 0
set hlaTimeRegulating 0
set hlaRzsEcho 0
set hlaRemoteControl 0

#======== RZS-Senden-Dialog ============================================

proc RzsSenden {bst} {
    set oldfocus [focus]
    global senden
    set f [toplevel .senden -borderwidth 30]
    message $f.msg -width 200 -text "RZS-Komando eingeben"
    if {$bst < 10 } {
       set senden(result) "0$bst,00,"
    } else {
       set senden(result) "$bst,00,"
    }
    entry $f.entry -textvariable senden(result)
    set b [frame $f.buttons -bd 10]
    pack $f.msg $f.entry $f.buttons -side top -fill x
    button $b.ok -text OK -command {set senden(ok) 1} \
        -underline 0
    button $b.cancel -text Cancel -command {set senden(ok) 0} \
        -underline 0
    pack $b.ok -side left
    pack $b.cancel -side right
    foreach w [list $f.entry $b.ok $b.cancel] {
        bindtags $w [list .senden [winfo class $w] $w all]
    }
    bind .senden <Alt-o> "focus $b.ok ; break"
    bind .senden <Alt-c> "focus $b.cancel ; break"
    bind .senden <Alt-Key> break
    bind .senden <Return> {set senden(ok) 1}
    bind .senden <Control-c> {set senden(ok) 0}
    grab $f
    focus $f.entry
    tkwait variable senden(ok)
    destroy $f
    focus $oldfocus
    if {$senden(ok)} {
        set erg [SFXCmd sende $senden(result) 0]
        put_win "Gesendet: $erg\n";
    }
}
#======== ENDE RZS-Senden-Dialog =======================================
proc rzsStart {} {
  set oldFocus [focus]
  global modtechon
  global realzeit
    global hla
    global hlaAsynchronousDelivery
    global hlaTimeConstrained
    global hlaTimeRegulating
    global hlaRzsEcho
    global hlaRemoteControl

  toplevel .rzs
  set f .rzs.radio
  frame $f -bd 2
  radiobutton $f.nsim -value 1 -text "normale Simulation" \
        -command { set modtechon 0; set realzeit 0 }
  radiobutton $f.msim -value 2 -text "mit Modelltechnik  " \
        -command { set modtechon 1; set realzeit 0 }
  radiobutton $f.zsim -value 3 -text "Realzeitsimulation  " \
        -command { set modtechon 0; set realzeit 1 }

    checkbutton $f.rzsecho -text "HLA-RzsEcho" -variable hlaRzsEcho -relief flat \
    	-command { setTitle }

    checkbutton $f.hadel -text "AsynchronousDelivery" -variable hlaAsynchronousDelivery -relief flat \
    	-command { setTitle }
    checkbutton $f.htcon -text "TimeConstrained     " -variable hlaTimeConstrained -relief flat \
	    -command {
	if {$hlaTimeConstrained} {
	    if {$modtechon} {
		pack .rzs.radio.hrmc -before .rzs.radio.htreg
	    }
	} else {
	    if [winfo exists .rzs.radio.hrmc] {
		set hlaRemoteControl 0
		pack forget .rzs.radio.hrmc
	    }
	}
	setTitle
    }	    

    checkbutton $f.hrmc -text "hlaRemoteControl" -variable hlaRemoteControl -relief flat
    checkbutton $f.htreg -text "TimeRegulating      " -variable hlaTimeRegulating -relief flat \
    	-command { setTitle }

    checkbutton $f.hsim -text  "mit HLA             " -variable hla -relief flat \
	    -command {
	if { $hla } {
	    pack  .rzs.radio.hadel .rzs.radio.htcon .rzs.radio.htreg
	    if  { $modtechon } {
		pack .rzs.radio.rzsecho -before .rzs.radio.zsim
	    }
	} else {
	    set hlaAsynchronousDelivery 0
	    set hlaTimeConstrained 0
	    set hlaTimeRegulating 0
	    pack forget .rzs.radio.hadel .rzs.radio.htcon .rzs.radio.htreg
	    if [winfo exists .rzs.radio.rzsecho] {
		set hlaRzsEcho 0
		pack forget .rzs.radio.rzsecho
	    }
	    if [winfo exists .rzs.radio.hrmc] {
		set hlaRemoteControl 0
		pack forget .rzs.radio.hrmc
	    }
	}
	setTitle
    }
  frame .rzs.ok
  button .rzs.ok.okButton -text OK -command {set fertig 1}

  pack $f -side top -expand yes -fill both
  pack $f.nsim $f.msim $f.zsim $f.hsim $f.hadel $f.htcon $f.htreg -side top
  pack forget  $f.hadel $f.htcon $f.htreg
  #pack $f.nsim $f.msim $f.zsim $f.hsim -side top
  pack .rzs.ok -side bottom  -expand yes -fill both
  pack .rzs.ok.okButton -side bottom -anchor center

  update

  $f.nsim select

  grab .rzs
  focus .rzs

  tkwait variable fertig
  destroy .rzs
  focus $oldFocus
}
#=============== Proc's für die Handsteuerung ====================

proc setTitle {} {
	global hla
	global hlaTimeRegulating
	global hlaTimeConstrained
	global hlaAsynchronousDelivery

	if { ($hlaTimeRegulating) && ($hlaTimeConstrained) && ($hlaAsynchronousDelivery) } {
		wm title . "SIMFLEX/3D - Regulating, Constrained, Asynchronous"
	} elseif { ($hlaTimeRegulating) && ($hlaAsynchronousDelivery) } {
		wm title . "SIMFLEX/3D - Regulating, Asynchronous"
	} elseif { ($hlaTimeConstrained) && ($hlaAsynchronousDelivery) } {
		wm title . "SIMFLEX/3D - Constrained, Asynchronous"
	} elseif { ($hlaTimeRegulating) && ($hlaTimeConstrained) } {
		wm title . "SIMFLEX/3D - Regulating, Constrained"
	} elseif { $hlaTimeRegulating } {
		wm title . "SIMFLEX/3D - Regulating"
	} elseif { $hlaTimeConstrained } {
		wm title . "SIMFLEX/3D - Constrained"
	} elseif { $hlaAsynchronousDelivery } {
		wm title . "SIMFLEX/3D - AsynchronousDelivery"
	} elseif { $hla } {
		wm title . "SIMFLEX/3D - hla"
	} else {
		wm title . "SIMFLEX/3D"
	}
}


proc sende { h } { 
   	global anzbst	
	global baust
	catch {destroy $h} 
   	toplevel $h 
	set me $h.bst.menu
	menubutton $h.bst -text "  Bausteinnummer  " -menu $me
	set m [menu $me -tearoff 1]
	for { set i 1 } { $i <= $anzbst } { incr i 1 } {
	   $me add command -label "    Baustein  $i    " \
			 -command  "menuewaskann $h $i" 
	 } 
	pack $h.bst -side left -padx 10 -pady 10   
}

proc sendemehr { h } { 
	global anzbst
#	global baust
#	put_win $baust
	catch {destroy $h} 
        toplevel $h 
	set mevon $h.vonbst.menu
	menubutton $h.vonbst -text "Von  Baustein" -menu $mevon
	set menach $h.nachbst.menu
	menubutton $h.nachbst -text "Nach  Baustein " -menu $menach
	set mv [menu $mevon -tearoff 1]
	for { set i 1 } { $i <= $anzbst } { incr i 1 } { 
		$mevon add command -label "Von  Baustein  $i " \
			-command "vonnach $h $i"  
}

 pack $h.vonbst $h.nachbst -side left -padx 10 -pady 10   
}

proc menuewaskann { h i } {
	global baust
	catch {destroy $h.bst}
	label $h.bst -text "  Baustein $i  " -justify left
	pack $h.bst -side left -padx 10 -pady 10
	set me $h.waskann.menu
	menubutton $h.waskann -text "  Ereignis  " -menu $me
	set m [menu $me -tearoff 1]
	pack $h.waskann -side left -padx 10 -pady 10  
	set fileID [open waskann.bst r]
	gets $fileID zeile
	set satznr [lindex $zeile 0]
	while { $satznr > -999 } { 
		if { $satznr > 2 } {
			set typ [lindex $zeile 1]
			set erei [lindex $zeile 2]
			set text [lindex $zeile 4]
			set befehl [lindex $zeile 5]
			if { $befehl == 0 } {
				set befehl "ancsenden"
				set param 0
			} else {
				set param $text
			}
			set aktbsttyp [lindex $baust [expr $i-1]]
			if { $aktbsttyp == $typ || $typ == 0 } {
	$me add command -label "   $text   " -command "$befehl $h $i $erei $param"
			}
		}
		gets $fileID zeile
		set satznr [lindex $zeile 0]
	}
	close $fileID
}
proc welcherichtung { h i erei et } { 
	catch { destroy $h.waskann }
	label $h.waskann -text "  $et  " -justify left
	pack $h.waskann -side left -padx 10 -pady 10  
	set me $h.richt.menu
	menubutton $h.richt -text "  Richtung  " -menu $me
	set m [menu $me -tearoff 1]
	pack $h.richt -side left -padx 10 -pady 10
	$me add command -label " positiv " \
		 -command  "ancsenden $h $i $erei 1" 
	$me add command -label " negativ " \
		 -command  "ancsenden $h $i $erei -1" 
}
proc typangeben { h i erei et } { 
	catch { destroy $h.waskann } 
	label $h.waskann -text "  $et  " -justify left
	pack $h.waskann -side left -padx 10 -pady 10  
	set me $h.typ.menu
	menubutton $h.typ -text "  Typbezeichnung  " -menu $me
	set m [menu $me -tearoff 1]
	pack $h.typ -side left -padx 10 -pady 10
 	$me add command -label " Quelle " -command "ancsenden $h $i $erei 1" 
 	$me add command -label " Senke " -command "ancsenden $h $i $erei 2"
	$me add command -label " Staubahn " -command "ancsenden $h $i $erei 3"
	$me add command -label " Weiche " -command "ancsenden $h $i $erei 4"
	$me add command -label " Wagen " -command "ancsenden $h $i $erei 5"
	$me add command -label " Maschine " -command "ancsenden $h $i $erei 7"
	$me add command -label " Drehpuffer " -command "ancsenden $h $i $erei 8"
	$me add command -label " FTS-Wagen " -command "ancsenden $h $i $erei 30"
}
proc wieweit { h i erei et } { 
	catch {destroy $h.waskann}
	label $h.waskann -text "  $et  " -justify left
	pack $h.waskann -side left -padx 10 -pady 10  
	set me $h.weit.menu
	menubutton $h.weit -text "  wie weit  " -menu $me
	set m [menu $me -tearoff 1]
	pack $h.weit -side left -padx 10 -pady 10
	for { set x 5 } { $x >= -5 } { incr x -1 } {
		if { $x != 0 } {   
			$me add command -label "  $x Einheiten " \
			 	-command "ancsenden $h $i $erei $x"
		}
	} 
}
proc plusminus { param } {
	if { $param > 0 } {
		set par [format "+%02i" $param] 
	} else {
		set par [format "-%02i" [expr -1 * $param]] 
	}
	return $par
}
proc ancsenden { h i erei param } {
	if { $erei == 80 } {
		set par [format "%03i" $param] 
	} elseif { $param == 0 } {
		set par "" 
	} else { 
		set par [plusminus $param]
 	}
	set erg [SFXCmd sende [format "%02i,00,%02i $par" $i $erei] ] 
	put_win "Senden: an Baustein $i ; Befehl $erei ; Parameter $param\n"
	catch {destroy $h}
}
proc initbst { } {
	global anzbst
	global baust
	for { set i 1 } { $i <= $anzbst } { incr i 1 } {
			lappend baust 0
#			frame .dummi
#			ancsenden .dummi $i 60 0
	}
 	set h .inibst 
   catch {destroy $h}
	toplevel $h 
	button $h.but -text " ttp einlesen  " -command "ttples $h $anzbst"
 	frame $h.a
	label $h.text -text "Bitte jeweils einen Typ des Bausteins angeben!!!" -justify left
	frame $h.ausw
	pack $h.text $h.but $h.ausw -side top  -fill both 
        pack $h.text $h.ausw -side top  -fill both
	for { set i 1 } { $i <= $anzbst } { incr i 1 } {
			frame $h.ausw.nr$i
			pack $h.ausw.nr$i -side top -fill both
			frame $h.ausw.nr$i.typb 
			button $h.ausw.nr$i.knop -text "Bausteinnr. $i" -justify left \
				-command "typang $h.ausw.nr$i.typb $i 80 Typ_angeben"
			pack $h.ausw.nr$i.knop -side left -padx 2 -pady 2
			pack $h.ausw.nr$i.typb -side right -padx 2 -padx 2
	}
}
proc typang { h i erei et } { 
	set me $h.typ.menu
	menubutton $h.typ -text "  Typbezeichnung  " -menu $me
	set m [menu $me -tearoff 1]
	pack $h.typ -side top -padx 2 -pady 2
 	$me add command -label " Quelle " -command "wegschicken $h $i $erei 1 Quelle" 
 	$me add command -label " Senke " -command "wegschicken $h $i $erei 2 Senke"
	$me add command -label " Staubahn " 	-command "wegschicken $h $i $erei 3 Staubahn"
	$me add command -label " Weiche " -command "wegschicken $h $i $erei 4 Weiche"
	$me add command -label " Wagen " -command "wegschicken $h $i $erei 5 Wagen"
	$me add command -label " Maschine " 	-command "wegschicken $h $i $erei 7 Maschine"
	$me add command -label " Drehpuffer " 	-command "wegschicken $h $i $erei 8 Drehpuffer"
	$me add command -label " FTS-Wagen " -command "wegschicken $h $i $erei 30 FTS-Wagen"		
}
proc wegschicken { h i erei param et } {
	global baust
	catch {destroy $h.typ}
	label $h.typ -text "  $et  " -justify left
	pack $h.typ -side left -padx 2 -pady 2  
	set x [expr $i-1]
	set baust [lreplace $baust $x $x $param]
	frame .dummi
	ancsenden .dummi $i $erei $param 	
}
proc ttples { h anz } {

	global baust
	global ttpfile
	catch {destroy $h}
	set hfile $ttpfile
	put_win "Datei >$hfile< wird gelesen\n"
	set zeile ""
	set fileID [open $hfile r]
	gets $fileID zeile
	set satznr [lindex $zeile 0]
	put_win $satznr
	while { $satznr > -999 } { 
		if { [lindex $zeile 0] > 2 } {
			set bst [lindex $zeile 1]
			set typ [lindex $zeile 2]
			put_win "bst = >$bst< typ = >$typ<\n"
			frame .dummi
			ancsenden .dummi $bst 80 $typ 	
			set x [expr $bst-1]
			set baust [lreplace $baust $x $x $typ]
			put_win "Bausteintypen :$baust"
		}
		gets $fileID zeile
		set satznr [lindex $zeile 0]
	}
	close $fileID
}
proc vonnach { h i } {
   	global siofile
	global anzbst
	global globzeichen
	set erg [split $globzeichen ", "]
	set an  [lindex $erg 0]
  	set von [lindex $erg 1]
  	set bef [lindex $erg 2]
  	set par [lindex $erg 3]	
	catch {destroy $h.vonbst}
	label $h.vonbst -text " Von Baustein $i  " -justify left
	pack $h.vonbst -side left -padx 10 -pady 10 
	catch {destroy $h.nachbst}
	if {$i == $anzbst} {
	put_win "Baustein $i ist der letzte Baustein!!\n"
	} else {
	label $h.nachbst -text " Nach  Baustein [expr $i+1]  " -justify left
	pack $h.nachbst -side left -padx 10 -pady 10 
	after 1000 ancsenden $h [expr $i+1] 01 1
	after 1000 ancsenden $h $i 52 1 
	after 1000 e_abgeben $h $i
	}
}
proc e_abgeben { h i } {
global globzeichen
global siofile 
gets $siofile globzeichen
set erg [split $globzeichen ", "]
  	set bef [lindex $erg 2]
if { $bef == "02" } {
	ancsenden $h $i 53 1
	} else {
	after 100 e_abgeben $h $i
	}
}
proc ncprogname {} {
    set oldfocus [focus]
    global senden
    set f [toplevel .senden -borderwidth 30]
    message $f.msg -width 300 -text "Dateiname für NC_Programm eingeben(ohne .nc)!"
    message $f.msg1 -width 300 -text "Baustein Nr. der Maschine eingeben!"
 #   if {$bst < 10 } {
 #      set senden(result) "0$bst,00,"
 #   } else {
  #     set senden(result) "$bst,00,"
  #  }
    entry $f.entry -textvariable senden(result)
    entry $f.entry1 -textvariable senden(result1)
    set b [frame $f.buttons -bd 10]
    pack $f.msg  $f.entry $f.msg1 $f.entry1 $f.buttons -side top -fill x
    button $b.ok -text OK -command {set senden(ok) 1} \
        -underline 0
    button $b.cancel -text Cancel -command {set senden(ok) 0} \
        -underline 0
    pack $b.ok -side left
    pack $b.cancel -side right
    foreach w [list $f.entry $f.entry1 $b.ok $b.cancel] {
        bindtags $w [list .senden [winfo class $w] $w all]
}
    bind .senden <Alt-o> "focus $b.ok ; break"
    bind .senden <Alt-c> "focus $b.cancel ; break"
    bind .senden <Alt-Key> break
    bind .senden <Return> {set senden(ok) 1}
    bind .senden <Control-c> {set senden(ok) 0}
    grab $f
    focus $f.entry
    tkwait variable senden(ok)
    destroy $f
    focus $oldfocus
    if {$senden(ok)} {
        set erg [SFXCmd ncprog $senden(result) $senden(result1)]
 #       put_win "Gesendet: $erg\n";
    } 
} 

proc holZeichen {} {
  global siofile globzeichen deltazeit zaehler
  # schleife
#      set zeichen [read -nonewline $siofile]
      gets $siofile zeichen
  if { $zeichen > "" } {
      set globzeichen $zeichen
      incr zaehler
      put_win "Empfangen($zaehler): >$globzeichen<\n"
#       put_win "Empfangen: >$globzeichen<\n"
  }
#  incr schleife
  after $deltazeit holZeichen
}

#=============== Diverse Funktionen ====================
proc Hilfe_Fenster Hilfedatei {
  toplevel .hilfeFen
  set f .hilfeFen.hFrame 
  frame $f 
  text $f.text -relief sunken -bd 2 -font ansifixed -wrap none\
               -yscrollcommand "$f.hilfeScroll set"
  scrollbar $f.hilfeScroll -command "$f.text yview"
  pack $f -expand yes -fill both
  pack $f.hilfeScroll -side right -fill y
  pack $f.text -side left -fill both

# Hilfetext aus Datei 'Hilfedatei' einlesen!!!

  $f.text delete 1.0 end
  set d [open $Hilfedatei]
  while {![eof $d]} {
    $f.text insert end [read $d 1000]
  }
  close $d
#  wm geometry .hilfeFen 380x230
  wm title .hilfeFen "FFS Hilfe"
}

proc SkipTimeQuery {} {
  set go 0
  toplevel .inp
  label .inp.lab -text "Modellzeit in Minuten"

  frame .inp.f -relief raised -borderwidth 1p
  radiobutton .inp.f.rel -text "relativ" -variable absTimeWrap -value 0 \
      -command {set time 0; .inp.f.entry icursor end; .inp.f.entry selection range 0 end}
  radiobutton .inp.f.abs -text "absolut" -variable absTimeWrap -value 1 \
      -command {set time [SFXCmd SimTime]; .inp.f.entry icursor end; .inp.f.entry selection range 0 end}

  entry .inp.f.entry -width 15 -relief sunken -bd 2 -textvariable time -justify center
  checkbutton .inp.check -text "iconify Views" -variable removeViews -onvalue 1 -offvalue 0
  frame .inp.ff
  button .inp.ff.ok     -text "OK"     -width 6 -command {set go 2}
  button .inp.ff.cancel -text "Cancel" -width 6 -command {set time 0; set go 2}
  pack .inp.lab .inp.f -side top -padx 1m -pady 1m
  pack .inp.f.rel .inp.f.abs -side top -anchor w -padx 2m
  pack .inp.f.entry   -side top -padx 1m -pady 2m
  pack .inp.check     -side top -padx 1m -pady 2m
  pack .inp.ff        -side top
  pack .inp.ff.ok     -side left  -padx 1m -pady 1m
  pack .inp.ff.cancel -side right -padx 1m -pady 1m
  .inp.f.entry icursor end
  .inp.f.entry selection range 0 end
  wm title .inp "Zeitsprung!"
  wm protocol .inp WM_DELETE_WINDOW {set time 0; set go 2}
  focus .inp.f.entry
  bind .inp.f.entry <Return> {set go 2}
  tkwait variable go
  destroy .inp
  unset go
}

#======== MENUBAR ============================================================
set simon        0
set time         0
set removeViews  1
set absTimeWrap  0
set simTrace     0
set simRealZeit  0
set simTakten    0
set simSpeed     0.0
set simSpeedStep 0.01
set simTime      "        ---        "

#miro, feb2003, inifile einstellungen im sfx3d-menu darstellen

set simdebugf  0
set siminitprog   0
set simtestprog  0
set simprgtest   0
set simlogfile     0
set simnixlog    0
#miro, feb2003, inifile einstellungen im sfx3d-menu darstellen


proc ExitOrCloseSim {action text} {
  global simon
  set antwort "gjfdg"
  set saveSimOn $simon
  if { $simon == 1 } {set simon [SFXCmd Stop 0 0];}
  set antwort [tk_messageBox -parent . -title Achtung \
               -icon warning  -message $text \
               -type okcancel -default cancel]
  grab .
  if {$antwort == "ok"} {
    SFXCmd $action 0 0;
    return 1;
  } elseif {$saveSimOn == 1} {
    set simon [SFXCmd Start 0 0];
    return 0;
  }
}

set menustatus "    "
set showInfo   0
frame .statusBar -bd 1
label .statusBar.label -textvariable menustatus -height 1 -anchor w \
                      -relief sunken -bd 2 -font SFXFont
checkbutton .statusBar.bot -text "Messages" -bd 1 -width 10 -pady 0 -variable showInfo \
                           -onvalue 1 -offvalue 0 -indicatoron false -command {
          if {$showInfo == 0} {
            wm minsize  . [lindex $sfxSmallMinsize 0] [lindex $sfxSmallMinsize 1] 
            wm maxsize  . [lindex $sfxSmallMaxsize 0] [lindex $sfxSmallMaxsize 1] 
            wm geometry . $sfxSmallGeometry
            pack forget .text
          } else {
            wm minsize  . [lindex $sfxBigMinsize 0] [lindex $sfxBigMinsize 1] 
            wm maxsize  . [lindex $sfxBigMaxsize 0] [lindex $sfxBigMaxsize 1] 
            wm geometry . $sfxBigGeometry
            pack .text -after .knoeppe -expand yes -side top -fill both
          }
}

menu .menu -tearoff 0

set mfile .menu.file
menu $mfile -tearoff 0
.menu add cascade -label "Datei" -menu $mfile -underline 0

set mabl .menu.ablauf
menu $mabl -tearoff 0
.menu add cascade -label "Ablauf" -menu $mabl -underline 0

set minfo .menu.info
menu $minfo -tearoff 0
.menu add cascade -label "Modellelemente" -menu $minfo -underline 0

set mbde .menu.bde
menu $mbde -tearoff 0
.menu add cascade -label "BDE" -menu $mbde -underline 0

set mextra .menu.extra
menu $mextra -tearoff 0
.menu add cascade -label "Extras" -menu $mextra -underline 0

###miro, feb2003, inifile einstellungen im sfx3d-menu darstellen

set mdebug .menu.debug
menu $mdebug -tearoff 0
.menu add cascade -label "Debug" -menu $mdebug -underline 0

###miro, feb2003, inifile einstellungen im sfx3d-menu darstellen

set mhilfe .menu.help
menu $mhilfe -tearoff 0
.menu add cascade -label "Hilfe" -menu $mhilfe -underline 0


proc DateiOeffnen {{name ""}} {
        global simSprache
        global modtechon
    global realzeit
    global hla
    global hlaAsynchronousDelivery
    global hlaTimeConstrained
    global hlaTimeRegulating
    global hlaRzsEcho
    global hlaRemoteControl
        global simRealZeit
    global simSpeed
    global mfile
    global mabl
    global mbde
    global minfo
    global mextra
    global mdebug
    #tk_messageBox -parent . -title About -icon info -type ok -message $name
    if {[expr {$name == ""}]} {     
	set file [fileDialog . 2gs open];
    } else {
	set file $name;	
	#tk_messageBox -parent . -title About -icon info -type ok -message $file
    }


        if {$file != ""} {
#         rzsStart;
          set simTime     "  000:00:00:00:000 "
          SFXCmd ModTechnik $modtechon 0 0;

	SFXCmd RealzeitSim $simSpeed  0 0;
	SFXCmd Hla4Sfx3D $hla $hlaAsynchronousDelivery $hlaTimeRegulating $hlaTimeConstrained $hlaRzsEcho $hlaRemoteControl;
          set dat [file rootname $file]
          set werte [SFXCmd InitBst $dat [file extension $file] 0 ];
          set simSprache [SFXCmd SprachAusgabe 0 0];

          $mfile  entryconfigure 0 -state disabled;
          $mfile  entryconfigure 1 -state disabled;
          $mfile  entryconfigure 2 -state active;
          $mabl   entryconfigure 0 -state active;
          $mabl   entryconfigure 1 -state disabled;
          $mabl   entryconfigure 4 -state active;
          $mabl   entryconfigure 5 -state active;
          $mabl   entryconfigure 7 -state active;

          $mbde   entryconfigure 0 -state active;
          $mbde   entryconfigure 1 -state active;
          $mbde   entryconfigure 3 -state active;
          $mbde   entryconfigure 4 -state active;
          $mbde   entryconfigure 6 -state active;

          $minfo  entryconfigure 0 -state active;
          $minfo  entryconfigure 1 -state active;
          $minfo  entryconfigure 2 -state active;

          $mextra entryconfigure 0 -state active;
          $mextra entryconfigure 1 -state active;
          $mextra entryconfigure 3 -state active;

#miro, feb2003, inifile einstellungen im sfx3d-menu darstellen
          $mdebug   entryconfigure 0 -state active;
          $mdebug   entryconfigure 2 -state active;
          $mdebug   entryconfigure 3 -state active;
          $mdebug   entryconfigure 4 -state active;
          $mdebug   entryconfigure 6 -state active;
          $mdebug   entryconfigure 7 -state active;
#miro, feb2003, inifile einstellungen im sfx3d-menu darstellen

          .knoeppe.startstop.start    configure -state active;
          .knoeppe.startstop.stop     configure -state disabled;
          .knoeppe.startstop.skip     configure -state active;
          .knoeppe.startstop.skipTime configure -state active;
          .knoeppe.startstop.step     configure -state active;
          .knoeppe.misc.takten        configure -state active;
          .knoeppe.misc.trace         configure -state active;
          .knoeppe.rzsmanuell.befehl  configure -state active;
          .knoeppe.rzsmanuell.ereignis  configure -state active;
          .knoeppe.rzsmanuell.palette  configure -state active;
          .knoeppe.rzsmanuell.hilfe  configure -state active;
          .knoeppe.rzsmanuell.exit  configure -state active;
          .knoeppe.rzsmanuell.init  configure -state active;
          .knoeppe.rzsmanuell.test  configure -state active;
          update idletasks;
        }
}

$mfile add command -label "Öffnen..." -command {DateiOeffnen}

$mfile add command -label "erw. Öffnen..." -command {
    global simSprache
    global modtechon
    global realzeit
    global hla
    global hlaAsynchronousDelivery
    global hlaTimeConstrained
    global hlaTimeRegulating
    global hlaRzsEcho
    global hlaRemoteControl
    global simRealZeit
    set file [fileDialog . 2gs open]
    if {$file != ""} {
	#bei Bedarf            
         rzsStart;
          set simTime     "  000:00:00:00:000 "
          SFXCmd ModTechnik $modtechon 0 0;
	SFXCmd RealzeitSim $realzeit 0 0;
	SFXCmd Hla4Sfx3D $hla $hlaAsynchronousDelivery $hlaTimeRegulating $hlaTimeConstrained $hlaRzsEcho $hlaRemoteControl;
          set dat [file rootname $file]
          set werte [SFXCmd InitBst $dat 0 ];
          set simSprache [SFXCmd SprachAusgabe 0 0];

          $mfile  entryconfigure 0 -state disabled;
          $mfile  entryconfigure 1 -state disabled;
          $mfile  entryconfigure 2 -state active;
          $mabl   entryconfigure 0 -state active;
          $mabl   entryconfigure 1 -state disabled;
          $mabl   entryconfigure 4 -state active;
          $mabl   entryconfigure 5 -state active;
          $mabl   entryconfigure 7 -state active;

          $mbde   entryconfigure 0 -state active;
          $mbde   entryconfigure 1 -state active;
          $mbde   entryconfigure 3 -state active;
          $mbde   entryconfigure 4 -state active;
          $mbde   entryconfigure 6 -state active;

          $minfo  entryconfigure 0 -state active;
          $minfo  entryconfigure 1 -state active;
          $minfo  entryconfigure 2 -state active;

          $mextra entryconfigure 0 -state active;
          $mextra entryconfigure 1 -state active;
          $mextra entryconfigure 3 -state active;

          .knoeppe.startstop.start    configure -state active;
          .knoeppe.startstop.stop     configure -state disabled;
          .knoeppe.startstop.skip     configure -state active;
          .knoeppe.startstop.skipTime configure -state active;
          .knoeppe.startstop.step     configure -state active;
          .knoeppe.misc.takten        configure -state active;
          .knoeppe.misc.trace         configure -state active;
          .knoeppe.rzsmanuell.befehl  configure -state active;
          .knoeppe.rzsmanuell.ereignis  configure -state active;
          .knoeppe.rzsmanuell.palette  configure -state active;
          .knoeppe.rzsmanuell.hilfe  configure -state active;
          .knoeppe.rzsmanuell.exit  configure -state active;
          .knoeppe.rzsmanuell.init  configure -state active;
          .knoeppe.rzsmanuell.test  configure -state active;
          update idletasks;
        }
}

$mfile add command -label "Schliessen" -state disabled -command {
        set simTime     "        ---        "
        set simTakten 0
        SFXCmd Takten $simTakten 0
        set simTrace  0
        SFXCmd Trace  $simTrace  0

        if { [ExitOrCloseSim CloseBst "Simulation beenden??"] == 1} {
          $mfile  entryconfigure 0 -state active;
          $mfile  entryconfigure 1 -state active;
          $mfile  entryconfigure 2 -state disabled;

          $mabl   entryconfigure 0 -state disabled;
          $mabl   entryconfigure 1 -state disabled;
          $mabl   entryconfigure 4 -state disabled;
          $mabl   entryconfigure 5 -state disabled;
          $mabl   entryconfigure 7 -state disabled;

          $mbde   entryconfigure 0 -state disabled;
          $mbde   entryconfigure 1 -state disabled;
          $mbde   entryconfigure 3 -state disabled;
          $mbde   entryconfigure 4 -state disabled;
          $mbde   entryconfigure 6 -state disabled;

          $minfo  entryconfigure 0 -state disabled;
          $minfo  entryconfigure 1 -state disabled;
          $minfo  entryconfigure 2 -state disabled;

          $mextra entryconfigure 0 -state disabled;
          $mextra entryconfigure 1 -state disabled;
          $mextra entryconfigure 3 -state disabled;

#miro, feb2003, inifile einstellungen im sfx3d-menu darstellen
          $mdebug   entryconfigure 0 -state disabled;
          $mdebug   entryconfigure 2 -state disabled;
          $mdebug   entryconfigure 3 -state disabled;
          $mdebug   entryconfigure 4 -state disabled;
          $mdebug   entryconfigure 6 -state disabled;
          $mdebug   entryconfigure 7 -state disabled;
#miro, feb2003, inifile einstellungen im sfx3d-menu darstellen

          .knoeppe.startstop.start    configure -state disabled;
          .knoeppe.startstop.stop     configure -state disabled;
          .knoeppe.startstop.skip     configure -state disabled;
          .knoeppe.startstop.skipTime configure -state disabled;
          .knoeppe.startstop.step     configure -state disabled;
          .knoeppe.misc.takten        configure -state disabled;
          .knoeppe.misc.trace         configure -state disabled;
          .knoeppe.rzsmanuell.befehl    configure -state disabled;
          .knoeppe.rzsmanuell.ereignis  configure -state disabled;
          .knoeppe.rzsmanuell.palette   configure -state disabled;
          .knoeppe.rzsmanuell.hilfe     configure -state disabled;
          .knoeppe.rzsmanuell.exit      configure -state disabled;
          .knoeppe.rzsmanuell.init  configure -state disabled;
          .knoeppe.rzsmanuell.test  configure -state disabled;
       }
}

$mfile add separator

$mfile add command -label "Beenden" -command {ExitOrCloseSim ExitBst "SIMFLEX/3D verlassen??"}

$mabl add command -label "Start" -state disabled -command {.knoeppe.startstop.start invoke}

$mabl add command -label "Stop" -state disabled -command {.knoeppe.startstop.stop invoke}

$mabl add command -label "Langsam" -command {
        global simSpeed;
        if {$simSpeed <= 1.0} {
          set simSpeed [expr $simSpeed + $simSpeedStep]
          SFXCmd Speed $simSpeed;
        }
}

$mabl add command -label "Schnell" -command {
        global simSpeed;
        if {$simSpeed >= $simSpeedStep} {
          set simSpeed [expr $simSpeed - $simSpeedStep]
          SFXCmd Speed $simSpeed;
        }
}

$mabl add command -label "Zeitsprung" -state disabled -command {.knoeppe.startstop.skipTime invoke}

$mabl add command -label "Einzelschritt" -state disabled -command {.knoeppe.startstop.step invoke}

$mabl add separator

$mabl add checkbutton -label "Takten" -variable simTakten -state disabled \
                      -onvalue 1 -offvalue 0 -command {SFXCmd Takten $simTakten 0}

$mabl add checkbutton -label "Realzeit" -variable simSpeed -onvalue 1.00 -offvalue 0.00 \
  -command {SFXCmd RealzeitSim $simSpeed}

$minfo add command -label "Arbeitspläne..." -state disabled -command {}

$minfo add command -label "Transportrouten..." -state disabled -command {}

$minfo add command -label "Bausteine..." -state disabled -command {}

$mbde add checkbutton -label "Trace" -variable simTrace -state disabled \
                      -onvalue 1 -offvalue 0 -command {SFXCmd Trace $simTrace 0}

$mbde add command -label "Trace planen..." -state disabled -command {}

$mbde add separator

$mbde add command -label "Systemprotokoll" -state disabled -command {SFXCmd SysPro 0 0}

$mbde add command -label "Kennzahlen..." -state disabled -command {}

$mbde add separator

$mbde add command -label "BDE reset" -state disabled -command {}

$mextra add checkbutton -label "Sprachausgabe" -variable simSprache -state disabled -command {}

##############################-----Handsteuerung-----#############################################

$mextra add checkbutton -label "Handsteuerung" -variable rzsmanuell -state disabled -command {
#.knoeppe.startstop.stop invoke

set zaehler 0
#set schleife 0
set globzeichen -
set deltazeit 100
#exec play ../wav/dontmak2.wav
#set showInfo   1
#------das Fenster anpassen-----------------
wm title . "SIMFLEX/3D >>HANDSTEUERUNG IST AKTIV!!!<<"
wm minsize  . [lindex $sfxrzsMinsize 0] [lindex $sfxrzsMinsize 1] 
wm maxsize  . [lindex $sfxrzsMaxsize 0] [lindex $sfxrzsMaxsize 1] 
wm geometry . $sfxrzsGeometry+0+0
 pack .text -after .knoeppe -expand yes -side top -fill both
#-------------------------------------------

set anzbst [SFXCmd i_anzbst 0 0]
#       set anzbst  

set siofile [SFXCmd i_siodevnr 0 0]
# Zugriff auf die serielle Schnittstelle zulassen
       
        holZeichen 
# Überprüfung des Puffers der ser. Schnittstelle,
# mit deltazeit einstellbar (in milisekunden)

 put_win "Die Handsteuerung ist aktiviert!\n" 

 put_win "Bausteinanzahl = >$anzbst<\n"

 put_win "siofile = $siofile\n";

 }

$mextra add separator

$mextra add command -label "Neue Ansicht" -state disabled -command {put_win [SFXCmd NewView 0 0]}

######miro, feb2003, inifile einstellungen im sfx3d-menu darstellen

$mdebug add checkbutton -label "DebugWindow" -variable simdebugf -state disabled -command {SFXCmd DebugWindow $simdebugf 0}

$mdebug add separator

$mdebug add checkbutton -label "InitProg" -variable siminitprog -state disabled -command {SFXCmd InitProg $siminitprog 0}

$mdebug add checkbutton -label "TestProg" -variable simtestprog -state disabled -command {SFXCmd TestProg $simtestprog 0}


$mdebug add checkbutton -label "PrgTest" -variable simprgtest -state disabled -command {SFXCmd PrgTest $simprgtest 0}

$mdebug add separator

$mdebug add checkbutton -label "Logfile" -variable simlogfile -state disabled -command {SFXCmd Logfile $simlogfile 0}

$mdebug add checkbutton -label "Keine Ausschriebe" -variable simnixlog -state disabled -command {SFXCmd NixLog $simnixlog 0}


######miro, feb2003, inifile einstellungen im sfx3d-menu darstellen

$mhilfe add command -label "Hilfe" -command  {Hilfe_Fenster hilfe.txt}

$mhilfe add separator

$mhilfe add command -label "About" -command {SFXCmd AboutWindow}

#======== BUTTONBAR ==========================================================

frame .knoeppe
frame .knoeppe.startstop -bd 2 -relief groove
frame .knoeppe.time      -bd 2 -relief groove
frame .knoeppe.misc      -bd 2 -relief groove
frame .knoeppe.rzsmanuell -bd 2 -relief groove
button .knoeppe.startstop.start -text "|>" -width 4 -state disabled -command {
        global simon;
        set simon [SFXCmd Start 0 0];
        $mabl  entryconfigure 0 -state disabled;
        $mabl  entryconfigure 1 -state active;
        .knoeppe.startstop.start configure -state disabled
        .knoeppe.startstop.stop  configure -state active
}

button .knoeppe.startstop.stop -text "||" -width 4 -state disabled -command {
        global simon;
        set simon [SFXCmd Stop 0 0];
        $mabl  entryconfigure 0 -state active;
        $mabl  entryconfigure 1 -state disabled;
        .knoeppe.startstop.start configure -state active
        .knoeppe.startstop.stop  configure -state disabled
}

button .knoeppe.startstop.skip  -text ">>" -width 4 -state disabled
bind .knoeppe.startstop.skip <ButtonPress-1>   {SFXCmd SkipStart $simon 0}
bind .knoeppe.startstop.skip <ButtonRelease-1> {SFXCmd SkipStop  $simon 0}

button .knoeppe.startstop.skipTime  -text ">>|" -width 4 -state disabled -command {
        global simon;
        global time;   
        global absTimeWrap;

        SkipTimeQuery;
        if {$time != 0} {
          if {$simon == 0} {
            .knoeppe.startstop.start invoke
            put_win [SFXCmd SkipTime $time $absTimeWrap $removeViews 0]
          } else {
            put_win [SFXCmd SkipTime $time $absTimeWrap $removeViews 1]
          }
        }
}

button .knoeppe.startstop.step  -text "Step" -width 4 -state disabled -command {
        global simon;
        if {$simon != 0} {.knoeppe.startstop.stop invoke}
        SFXCmd Step 0 0
}

label .knoeppe.time.label -textvariable simTime -width 17 -anchor center -justify center \
                      -relief sunken -bg white -font SFXFont

if {$tcl_platform(platform) == "windows"} {
  set CheckPadY 1
} else {
  set CheckPadY 3
}
checkbutton .knoeppe.misc.trace -text "Trace" -width 5 -pady $CheckPadY -variable simTrace -state disabled \
                                 -onvalue 1 -offvalue 0 -indicatoron false -command {SFXCmd Trace $simTrace 0}

checkbutton .knoeppe.misc.takten -text "Takten" -width 5 -pady $CheckPadY -variable simTakten -state disabled \
                                 -onvalue 1 -offvalue 0 -indicatoron false -command {SFXCmd Takten $simTakten 0}

unset CheckPadY

label .knoeppe.misc.aktSpeed -textvariable simSpeed -justify center -width 4

scale .knoeppe.misc.speed -resolution $simSpeedStep -digit 3 -orient horizontal -showvalue 0 \
    -length 3c -from 0 -to 1 -variable simSpeed -command {SFXCmd Speed $simSpeed}
#======== HANDSTEUERUNG ==========================================================    
button .knoeppe.rzsmanuell.befehl    -text "Befehl Senden" -width 11 -state disabled -command {
			put_win "Befehl Senden::\n"; 
                               RzsSenden 1;
                               }
button .knoeppe.rzsmanuell.ereignis  -text "Ereignis Senden" -width 11 -state disabled -command {
 				put_win "Ereignis Senden::\n";  \
			   catch {destroy .send}; \
			 frame .send;   \
			pack .send -side bottom -side left; \
			sende .send;
			}
button .knoeppe.rzsmanuell.palette   -text " NC_Programm " -width 11 -state disabled -command {
#	put_win "Palette schicken von BstNr nach BstNr\n";  \
#			   catch {destroy .send}; \
#			 frame .send;   \
#			pack .send -side bottom -side left; \
#			sendemehr .send;
			ncprogname;
			}
button .knoeppe.rzsmanuell.hilfe     -text "Hilfe" -width 4 -state disabled -command {
				put_win  "Hilfe\n"; 
                                Hilfe_Fenster hilfe2.txt
                                }
button .knoeppe.rzsmanuell.exit      -text "Ende" -width 4 -state disabled -command {
			set rzsmanuell 0
			SFXCmd ExitH 0 0;	
			put_win "Ende von Handsteuerung!!!\n";
			wm title . "SIMFLEX/3D"
			wm minsize  . [lindex $sfxSmallMinsize 0] [lindex $sfxSmallMinsize 1] 
			wm maxsize  . [lindex $sfxSmallMaxsize 0] [lindex $sfxSmallMaxsize 1] 
			wm geometry . $sfxSmallGeometry+0+0
			pack forget .text
			}
button .knoeppe.rzsmanuell.init      -text "Init" -width 4 -state disabled -command {
			set rzsmanuell 1
			SFXCmd RzsManuell $rzsmanuell 0;
			initbst;
			}
button .knoeppe.rzsmanuell.test      -text "Test" -width 4 -state disabled -command {
			put_win "siofile = $siofile\n";
			put_win "anzbst = $anzbst\n";
			put_win "rzsmanuell = $rzsmanuell\n";
			put_win "file = $file\n"
			SFXCmd  rzsprint 0 0;
			}
#======== TEXTWINDOW ==========================================================

set AusgabeFenster .text.lb

frame .text -relief sunken

text $AusgabeFenster -height 10 -width 80 -yscrollcommand ".text.scy set" -font SFXFont -wrap none

scrollbar .text.scy -command {global AusgabeFenster; $AusgabeFenster yview}

wm title . "SIMFLEX/3D"
wm protocol . WM_DELETE_WINDOW {ExitOrCloseSim ExitBst "SIMFLEX/3D verlassen??"}

wm minsize  . [lindex $sfxSmallMinsize 0] [lindex $sfxSmallMinsize 1] 
wm maxsize  . [lindex $sfxSmallMaxsize 0] [lindex $sfxSmallMaxsize 1] 
wm geometry . $sfxSmallGeometry+0+0

pack .knoeppe -side top -fill x

pack .knoeppe.time -side left -anchor center -ipadx 1 -ipady 1
pack .knoeppe.time.label -side left -padx 2 -pady 3

pack .knoeppe.startstop -side left -ipadx 1 -ipady 1
pack .knoeppe.startstop.start     .knoeppe.startstop.stop .knoeppe.startstop.skip \
     .knoeppe.startstop.skipTime  .knoeppe.startstop.step -side left

pack .knoeppe.misc -side left -ipadx 1 -ipady 1 
pack .knoeppe.misc.trace .knoeppe.misc.takten .knoeppe.misc.speed .knoeppe.misc.aktSpeed -side left

#-------HANDSTEUERUNG----------------------------
pack .knoeppe.rzsmanuell -side left -ipadx 1 -ipady 1 -fill y
pack .knoeppe.rzsmanuell.befehl .knoeppe.rzsmanuell.ereignis .knoeppe.rzsmanuell.palette \
     .knoeppe.rzsmanuell.init .knoeppe.rzsmanuell.hilfe .knoeppe.rzsmanuell.test \
     .knoeppe.rzsmanuell.exit -side left
#-------HANDSTEUERUNG----------------------------
#pack .text -expand yes -side top -fill both

pack .text.scy -side right -fill y
pack .text.lb  -expand yes -fill both

pack .statusBar -side top -fill both
pack .statusBar.bot   -side right -padx 1 -ipady 0
pack .statusBar.label -side left  -padx 0 -expand yes -fill x

. configure -menu .menu

bind Menu <<MenuSelect>> {
    global $menustatus
    if {[catch {%W entrycget active -label} label]} {
	set label "    "
    }
    set menustatus $label
    update idletasks
}

bind .knoeppe <Leave> {
    global $menustatus
    set menustatus "     "
    update idletasks
}

bind .statusBar.bot <Leave> {
    global $menustatus
    set menustatus "     "
    update idletasks
}

bind .knoeppe.startstop.start <Enter> {
    global $menustatus
    set menustatus "Schaltet Modellablauf An"
    update idletasks
}

bind .knoeppe.startstop.stop <Enter> {
    global $menustatus
    set menustatus "Schaltet Modellablauf Aus"
    update idletasks
}

bind .knoeppe.startstop.skip <Enter> {
    global $menustatus
    set menustatus "Grafik abhängen"
    update idletasks
}

bind .knoeppe.startstop.skipTime <Enter> {
    global $menustatus
    set menustatus "Zeitsprung einplanen"
    update idletasks
}

bind .knoeppe.startstop.step <Enter> {
    global $menustatus
    set menustatus "Einzelschritt"
    update idletasks
}

bind .knoeppe.time.label <Enter> {
    global $menustatus
    set menustatus "aktuelle Modellzeit"
    update idletasks
}

bind .knoeppe.misc.takten <Enter> {
    global $menustatus
    set menustatus "Schaltet Takten An/Aus"
    update idletasks
}

bind .knoeppe.misc.aktSpeed <Enter> {
    global $menustatus
    set menustatus "aktuelle Ablaufverzögerung"
    update idletasks
}

bind .knoeppe.misc.speed <Enter> {
    global $menustatus
    set menustatus "Ablaufverzögerung"
    update idletasks
}
#------------------------HANDSTEUERUNG------------------------------------
bind .knoeppe.rzsmanuell.befehl <Enter> {
    global $menustatus
    set menustatus "RZS_Befehl an die MSPS schicken"
    update idletasks
}

bind .knoeppe.rzsmanuell.ereignis <Enter> {
    global $menustatus
    set menustatus "Ereignis_Befehl an die Anlage schicken"
    update idletasks
}

bind .knoeppe.rzsmanuell.palette <Enter> {
    global $menustatus
    set menustatus "Palette von einem zu nächstem Baustein bewegen (Befehlskette)"
    update idletasks
}

bind .knoeppe.rzsmanuell.hilfe <Enter> {
    global $menustatus
    set menustatus "Hilfe zu der Handsteuerung"
    update idletasks
}

bind .knoeppe.rzsmanuell.exit <Enter> {
    global $menustatus
    set menustatus "Handsteuerung verlassen?"
    update idletasks
}

bind .knoeppe.rzsmanuell.test <Enter> {
    global $menustatus
    set menustatus "Nur für Testzwecke (RZSKalender->print)!"
    update idletasks
}

bind .knoeppe.rzsmanuell.init <Enter> {
    global $menustatus
    set menustatus "Bausteine initialisieren!"
    update idletasks
}
#-----------------------------------HANDSTEUERUNG---------------------------------------------
bind .statusBar.bot <Enter> {
    global $menustatus
    set menustatus "Anzeige des Meldungsfensters"
    update idletasks
}
focus .
# ==============================================================================
# ===========    TCL-Code for Views   ==========================================
# ==============================================================================

proc SfxDoGlobal {what forWhom varName} {
  upvar #0 $varName value
  SFXView $what $forWhom $value
}

proc makeSFXView {WinName WinTitle Width Height} {
  global sfxMouseHandling
  global sfxStatusText

  set WidgetName $WinName.togl
  set sfxMouseHandling($WidgetName) 0
  set sfxStatusText($WidgetName)    "             "

  toplevel $WinName

# ===========    Menue    =====================================================

  menu $WinName.menu -tearoff 0

  set mfile $WinName.menu.file
  menu $mfile -tearoff 0   -background gray
  $WinName.menu add cascade -label "Datei" -menu $mfile -underline 0

  set mview $WinName.menu.ansicht
  menu $mview -tearoff 0  -background gray
  $WinName.menu add cascade -label "Ansicht" -menu $mview -underline 0

  set mmouse $WinName.menu.mouse
  menu $mmouse -tearoff 0 -background gray
  $WinName.menu add cascade -label "Mouse" -menu $mmouse -underline 0

  set mhelp $WinName.menu.help
  menu $mhelp -tearoff 0  -background gray
  $WinName.menu add cascade -label "Hilfe" -menu $mhelp -underline 0

# ===========    File-Menue    =====================================================

  $mfile add command -label "Sichern" -command \
      [list SFXView SaveSettings $WidgetName {}] -underline 0

  $mfile add separator

  $mfile add command -label "Beenden" -command \
      [list SFXView Quit $WidgetName {}] -underline 0

# ===========    Ansicht-Menue    =====================================================

# filled by ToglView!!!

# ===========    Mouse-Menue    =====================================================

  $mmouse add radiobutton -label StdHandling -variable sfxMouseHandling($WidgetName) -value 0 \
                      -command [list SfxDoGlobal MouseDefault $WidgetName sfxMouseHandling($WidgetName)]

  $mmouse add radiobutton -label Walking     -variable sfxMouseHandling($WidgetName) -value 1 \
                      -command [list SfxDoGlobal MouseWalk $WidgetName sfxMouseHandling($WidgetName)]

  $mmouse add radiobutton -label Zooming     -variable sfxMouseHandling($WidgetName) -value 2 \
      -command [list SfxDoGlobal MouseZoom $WidgetName sfxMouseHandling($WidgetName)]

  $mmouse add radiobutton -label MoveLight   -variable sfxMouseHandling($WidgetName) -value 3 \
      -command [list SfxDoGlobal MouseLight $WidgetName sfxMouseHandling($WidgetName)]

# ===========    Hilfe-Menue    =====================================================

  $mhelp add command -label "Hilfethemen" -command { } -underline 0

  $mhelp add separator

  $mhelp add command -label "About" -underline 0 -command \
          {tk_messageBox -parent . -title About -icon info -type ok \
                         -message {Hallo, its me!}}

# ===========    StatusBar    =====================================================

  frame $WinName.statusBar
  label $WinName.statusBar.label -textvariable sfxStatusText($WidgetName) \
                                 -relief sunken -bd 1 -font {-family Courier -size 9} -anchor w

# ===========    Pack-Menue    =====================================================

  $WinName configure -menu $WinName.menu
  pack $WinName.statusBar.label -side left -padx 2 -expand yes -fill both
  pack $WinName.statusBar -side bottom -fill x -pady 2

  wm title $WinName "$WinTitle"
  wm protocol $WinName WM_DELETE_WINDOW [list SFXView Quit $WidgetName {}]
  tkwait visibility $WinName

  bind $WinName <Escape> { if {$sfxMouseHandling(%W.togl) == 10} {
                             set sfxMouseHandling(%W.togl) 0
                             SFXView MouseDefault %W.togl $sfxMouseHandling(%W.togl)
                           }
                           SFXView ViewDefault %W.togl {}
                         }
  bind $WinName <KeyPress-w>  { SFXView KeyPress-w %W.togl {}
                              }                       
  bind $WinName <KeyPress-s>  { SFXView KeyPress-s %W.togl {}
                              }
  bind $WinName <KeyPress-a>  { SFXView KeyPress-a %W.togl {}
                              }                       
  bind $WinName <KeyPress-d>  { SFXView KeyPress-d %W.togl {}
                              }
  bind $WinName <KeyPress-Prior>  { SFXView KeyPress-BildHoch %W.togl {}
                              }                        
  bind $WinName <KeyPress-Next>  { SFXView KeyPress-BildRunter %W.togl {}
                              }                       
  bind $WinName <Control-KeyPress-1>  { SFXView SaveViewPoint %W.togl {} 1}
  bind $WinName <KeyPress-1>  { SFXView ShowViewPoint %W.togl {} 1}    
  bind $WinName <Control-KeyPress-2>  { SFXView SaveViewPoint %W.togl {} 2}
  bind $WinName <KeyPress-2>  { SFXView ShowViewPoint %W.togl {} 2}  
  bind $WinName <Control-KeyPress-3>  { SFXView SaveViewPoint %W.togl {} 3}
  bind $WinName <KeyPress-3>  { SFXView ShowViewPoint %W.togl {} 3}  
  bind $WinName <Control-KeyPress-4>  { SFXView SaveViewPoint %W.togl {} 4}
  bind $WinName <KeyPress-4>  { SFXView ShowViewPoint %W.togl {} 4}  
  bind $WinName <Control-KeyPress-5>  { SFXView SaveViewPoint %W.togl {} 5}
  bind $WinName <KeyPress-5>  { SFXView ShowViewPoint %W.togl {} 5}  
  bind $WinName <Control-KeyPress-6>  { SFXView SaveViewPoint %W.togl {} 6}
  bind $WinName <KeyPress-6>  { SFXView ShowViewPoint %W.togl {} 6}  
  bind $WinName <Control-KeyPress-7>  { SFXView SaveViewPoint %W.togl {} 7}
  bind $WinName <KeyPress-7>  { SFXView ShowViewPoint %W.togl {} 7}  
  bind $WinName <Control-KeyPress-8>  { SFXView SaveViewPoint %W.togl {} 8}
  bind $WinName <KeyPress-8>  { SFXView ShowViewPoint %W.togl {} 8}
  bind $WinName <Control-KeyPress-9>  { SFXView SaveViewPoint %W.togl {} 9}
  bind $WinName <KeyPress-9>  { SFXView ShowViewPoint %W.togl {} 9}                      
}

# ===========    SFXDialog     ==============================================

proc makeSFXDialog {WinName forWhom showWhat InitString} {
  toplevel $WinName

  label $WinName.label -text "$forWhom" -relief groove -bd 2 -padx 1m -pady 1m
  pack $WinName.label -expand yes -fill both

  set index 0
  foreach i $InitString {
    set FrameName $WinName.frame$index
    frame $FrameName -relief ridge -borderwidth 2
    label $FrameName.text -text $i -width 12
    entry $FrameName.wert -textvariable sfxVal($WinName$index) -relief sunken \
                  -width 15 -justify center -bg lightgrey -fg Black -state disabled
    pack $FrameName -expand yes -fill both
    pack $FrameName.text $FrameName.wert -expand yes -side left -fill x
    incr index
  }
  button $WinName.quit -text Quit -command [list SFXContext Quit $WinName]
  pack $WinName.quit -side bottom
  wm title $WinName "$showWhat"
  wm protocol $WinName WM_DELETE_WINDOW [list SFXContext Quit $WinName]
  tkwait visibility $WinName
}

# ===========    SelectPermMenu     ==============================================
proc SelectPermMenu {WidgetName GeoAddress forWhom mouseX mouseY hasUserData} {
  catch {destroy $WidgetName.m}
  menu $WidgetName.m -tearoff 0 -background gray
#  $WidgetName.m add label -text "Baustein $forWhom" -font bold 
#  $WidgetName.m add separator
  $WidgetName.m add command -label "Status"    -command [list SFXContext PermState $forWhom]
  $WidgetName.m add command -label "Steuerung" -command [list SFXContext PermControl $forWhom]
  $WidgetName.m add command -label "Tech. Parameter" -state disabled
#  $WidgetName.m add command -label "Tech. Parameter" -command [list SFXContext PermPara $forWhom]
  if {$hasUserData == 1} {  
    $WidgetName.m add command -label "UserData"  -command [list SFXContext PermUserData $forWhom]
  } else {
    $WidgetName.m add command -label "UserData" -state disabled
  }    
  $WidgetName.m add separator
  $WidgetName.m add command -label "Sperren/Entsperren" -command [list SFXContext PermLocking $forWhom]
  $WidgetName.m add command -label "Aktivieren" -command [list SFXContext PermActivating $forWhom]
  $WidgetName.m add separator
  $WidgetName.m add command -label "Setze Camera" -command [list SFXView CamToPermObject $WidgetName $GeoAddress]
  $WidgetName.m add command -label "Setze Focus" -command [list SFXView ObjectToFocus $WidgetName $GeoAddress]
  $WidgetName.m add separator
  menu $WidgetName.m.look -tearoff 0  -background gray
  $WidgetName.m add cascade -label "Ansicht" -menu $WidgetName.m.look -underline 0
  TgvAddToMenu $WidgetName $WidgetName.m.look
  $WidgetName.m post [expr [winfo rootx $WidgetName] + $mouseX] [expr [winfo rooty $WidgetName] +$mouseY]
}

#===========    SelectTempMenu-Menue     ==============================================
proc SelectTempMenu {WidgetName GeoAddress tempID route workS mouseX mouseY hasUserData} {
  catch {destroy $WidgetName.m}
  menu $WidgetName.m -tearoff 0 -background gray
#  $WidgetName.m add command -label "Werkstück"
#  $WidgetName.m add separator
  $WidgetName.m add command -label "TempStatus     $tempID" -command [list SFXContext TempState $GeoAddress]
  if {$route != 0} {
    $WidgetName.m add command -label "TransportRoute $route"  -command [list SFXContext RouteState $GeoAddress]
  } else {
    $WidgetName.m add command -label "TransportRoute" -state disabled
  }
  $WidgetName.m add command -label "Arbeitsplan    $workS"  -command [list SFXContext WorkScheduleState $GeoAddress]
  if {$hasUserData == 1} {
    $WidgetName.m add command -label "UserData" -command [list SFXContext TempUserData $GeoAddress]
  } else {
    $WidgetName.m add command -label "UserData" -state disabled
  }
  $WidgetName.m add separator
  $WidgetName.m add command -label "Arbeitsplan freigeben" -command [list SFXContext StartWorkSchedule $workS]
  $WidgetName.m add separator
  $WidgetName.m add command -label "Setze Camera" -command [list SFXView CamToTempObject $WidgetName $GeoAddress]
  $WidgetName.m add command -label "Setze Focus" -command [list SFXView ObjectToFocus $WidgetName $GeoAddress]
  $WidgetName.m add separator
  menu $WidgetName.m.look -tearoff 0  -background gray
  $WidgetName.m add cascade -label "Ansicht" -menu $WidgetName.m.look -underline 0
  TgvAddToMenu $WidgetName $WidgetName.m.look
  $WidgetName.m post [expr [winfo rootx $WidgetName] + $mouseX] [expr [winfo rooty $WidgetName] +$mouseY]
}



