#######################################################################################################################################
#create menu for the pulse-sequenze selection
#######################################################################################################################################

#setting up the favarits men
menu .main.oben2.buttonframe.right.menups.menue -tearoff 0
.main.oben2.buttonframe.right.menups.menue add command -label "$textpart(74)" -command {
	toplevel .setkappa
	frame .setkappa.oben
	frame .setkappa.unten
	label .setkappa.oben.l -text "$textpart(75)"
	entry .setkappa.oben.e -textvariable setkappa -width 20 -relief sunken
	pack .setkappa.oben  .setkappa.unten -side top
	button .setkappa.unten.b -text "$textpart(31)" -command {
		if {[regexp {^[0-9]*\.?[0-9]+$} $setkappa]} {
			set colortau1 black
			set colortau2 black
			set unteneins ""
			set untenzwei ""
			set sym ""
			set sym1 ""
			set sym2 ""
			set sym3 ""
			set symnb ""
			set symnb_b ""
			set symnbm1 ""
			set symnbh ""
			set symnbh_b ""
			set symnbd ""
			set symnbd_b ""
			set kappa $setkappa
			destroy .setkappa
			focus .main.mitte.rechts.e_depha
		} else {tk_messageBox -message "$textpart(76)" -title "$textpart(20)" -type ok}
	}
	pack  .setkappa.oben.l .setkappa.oben.e -side left
	pack 	.setkappa.unten.b 
}
.main.oben2.buttonframe.right.menups.menue add cascade -label "$textpart(77)" -menu .main.oben2.buttonframe.right.menups.menue.fav
menu .main.oben2.buttonframe.right.menups.menue.fav -tearoff 0
#read the favorits file
set favorits [updatefavmen $installdir]
#building the favarits menu out of variable favorits
for {set i 0} {$i <= [expr [llength $favorits]-1]} {incr i 1} {
	if {[lindex [lindex $favorits $i] 1] != ""} {
		.main.oben2.buttonframe.right.menups.menue.fav add command -label "[lindex [lindex $favorits $i] 0] ([lindex [lindex $favorits $i] 1])" -command {
			#set the variabels for the picture kappa shortcut and change the name from the button
			set kappa [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 2]
			set sym [lindex [split [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 0] -] 0]
			set symnb [lindex [split [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 0] -] 1]
			set symnbm1 [expr [format %2.0f $symnb] - 1]
			set symnbh [lindex [split [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 0] -] 3]
			set symnbd [lindex [split [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 0] -] 2]
			set symnbh [string trimleft $symnbh "_ ^"]
			set symnbd [string trimleft $symnbd "_ ^"]
			if {$sym == "C"} {
				set sym1 $sym
				set sym2 $sym
				set sym3 $sym
				set symnb_b $symnb
				set symnbh_b $symnbh
				set symnbd_b $symnbd
				set unteneins 0
				set untenzwei 1
			} elseif {$sym == "R"} {
				set unteneins ""
				set untenzwei ""
				set symnb_b $symnb
				set symnb ""
				set symnbm1 ""
				set symnbh_b $symnbh
				set symnbh ""
				set symnbd_b $symnbd
				set symnbd ""
				set sym1 R
				set sym2 R'
				set sym3 R'
			}
			.main.oben2.buttonframe.mid.l_short3 configure -text "[lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 3]"
			.main.oben2.buttonframe.right.menups configure -text "$sym $symnb_b _$symnbd_b ^$symnbh_b ([lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 1])"
			#update picure
			redraw .main.bild $installdir
			focus .main.mitte.rechts.e_depha
		}
	} else {
		# if the entry is wrong
		.main.oben2.buttonframe.right.menups.menue.fav add command -label "[lindex [lindex $favorits $i] 0]"
		break
	}
}
# set up the menu for the C-sqences
set list1 [array names CSequenz]
.main.oben2.buttonframe.right.menups.menue add cascade -label "$textpart(79)" -menu .main.oben2.buttonframe.right.menups.menue.c
menu .main.oben2.buttonframe.right.menups.menue.c -type normal -tearoff 0
#build the first cascade menue (C 07 ... C??
for {set i 0} {$i <= [expr $z1 - 1]} {incr i} {
	if {[expr fmod($i,30)]==0} {set colbreak 1} else {set colbreak 0}
		.main.oben2.buttonframe.right.menups.menue.c add cascade  -label "$CSequenz([expr $i + 1],0)$CSequenz([expr $i + 1],1)"  -columnbreak $colbreak -menu .main.oben2.buttonframe.right.menups.menue.c.$i
		menu .main.oben2.buttonframe.right.menups.menue.c.$i -tearoff 0
			set j 0
			while {[lsearch $list1 "[expr $i + 1],$j"] >= 0} {
			#building the second cascade menu (with the full symmetrie)
			.main.oben2.buttonframe.right.menups.menue.c.$i add cascade -label "$CSequenz([expr $i+1],0)$CSequenz([expr $i+1],1)_$CSequenz([expr $i+1],[expr $j+2])^$CSequenz([expr $i+1],[expr $j+3])" -menu .main.oben2.buttonframe.right.menups.menue.c.$i.$j
			menu .main.oben2.buttonframe.right.menups.menue.c.$i.$j -tearoff 0
					for {set h 4} {$h <= [expr [llength $Chead]-1]} {incr h 1} {
						#exclude non exissting Enties
						if {$CSequenz([expr $i + 1],[expr $j+$h]) != "x"} {
							#building the phase menu
							.main.oben2.buttonframe.right.menups.menue.c.$i.$j add command -label "[lindex $Chead $h]" -command {
								#If cindex is correct...
								if {[regexp {^[0-9]*\.?[0-9]+$} $cindex] && [regexp {^[0-9]*\.?[0-9]+$} $cindex1] && [regexp {^[0-9]*\.?[0-9]+$} $cindex2] } {
									#...set kappa and the variables for the pictures etc.
									set kappa $CSequenz($cindex,[expr ($cindex1-1)+3+$cindex2])
									.main.oben2.buttonframe.right.menups configure -text "$CSequenz($cindex,[expr ($cindex1 -1)]) $CSequenz($cindex,[expr ($cindex1)])_$CSequenz($cindex,[expr ($cindex1-1)+2])^$CSequenz($cindex,[expr ($cindex1-1)+3]) ([lindex $Chead [expr $cindex2 + 3 ]])"
									set sym $CSequenz($cindex,[expr ($cindex1-1)])
									set symnb [format %2.0f $CSequenz($cindex,[expr ($cindex1-1)+1])]
									set symnbm1 [expr [format %2.0f $symnb] - 1]
									set symnbh $CSequenz($cindex,[expr ($cindex1-1)+3])
									set symnbd $CSequenz($cindex,[expr ($cindex1-1)+2])
									set symnb_b $symnb
									set symnbh_b $symnbh
									set symnbd_b $symnbd
									set sym1 $sym
									set sym2 $sym
									set sym3 $sym
									set unteneins 0
									set untenzwei 1
									if {$symnb < 10} {set symnbexp [format %1.0f $symnb]} else {set symnbexp [format %2.0f $symnb]}
									#configure the button label
									.main.oben2.buttonframe.mid.l_short3 configure -text "C-$cindex-$cindex1-$cindex2"
									#redraw picture
									redraw .main.bild $installdir
									focus .main.mitte.rechts.e_depha
									#update the favarits menu
									updatefavfile $favorits "$sym-$symnbexp-_$symnbd-^$symnbh" [lindex $Chead [expr $cindex2 + 3 ]] "$kappa" "C-$cindex-$cindex1-$cindex2" $installdir
									set favorits [updatefavmen $installdir]
									for {set i 0} {$i <= [expr [llength $favorits]-1]} {incr i 1} {
										.main.oben2.buttonframe.right.menups.menue.fav add command -label "[lindex [lindex $favorits $i] 0] ([lindex [lindex $favorits $i] 1])" -command {
											set kappa [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 2]
											set sym [lindex [split [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 0] -] 0]
											set symnb [lindex [split [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 0] -] 1]
											set symnbm1 [expr [format %2.0f $symnb] - 1]
											set symnbh [lindex [split [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 0] -] 3]
											set symnbd [lindex [split [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 0] -] 2]
											set symnbh [string trimleft $symnbh "_ ^"]
											set symnbd [string trimleft $symnbd "_ ^"]
											if {$sym == "C"} {
												set sym1 $sym
												set sym2 $sym	
												set sym3 $sym
												set symnb_b $symnb
												set symnbh_b $symnbh
												set symnbd_b $symnbd
												set unteneins 0
												set untenzwei 1
											} elseif {$sym == "R"} {
												set unteneins ""
												set untenzwei ""
												set symnb_b $symnb
												set symnb ""
												set symnbm1 ""
												set symnbh_b $symnbh
												set symnbh ""
												set symnbd_b $symnbd
												set symnbd ""
												set sym1 R
												set sym2 R'
												set sym3 R'
											}
											.main.oben2.buttonframe.mid.l_short3 configure -text "[lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 3]"
											.main.oben2.buttonframe.right.menups configure -text "$sym $symnb_b _$symnbd_b ^$symnbh_b ([lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 1])"
											redraw .main.bild $installdir
											focus .main.mitte.rechts.e_depha
										}
									}

								} else {tk_messageBox -message "$textpart(80)" -title "$textpart(20)" -type ok}
							}
						}
						#getting the cindex2 form the Menu
						bind .main.oben2.buttonframe.right.menups.menue.c.$i.$j <ButtonPress> {
							foreach element [winfo children .main.oben2.buttonframe.right.menups.menue.c] {
									foreach element2 [winfo children $element] {
										if { [$element2 index active] != "none"} {
											set cindex  [expr [lindex [split $element2 .] [expr [llength [split $element2 .]] - 2]] + 1]
											set cindex1  [expr [lindex [split $element2 .] [expr [llength [split $element2 .]] - 1]] + 1]
											set cindex2 [expr [$element2 index active] + 1]
											#excluding the non exissting Enties
											set g 1
											set add 0
											set ref 1
											while { $ref <= $cindex2} {
												if {$CSequenz($cindex,[expr ($cindex1-1)+3+$g]) != "x"} {
													incr ref 1
													incr g 1
												} elseif {$CSequenz($cindex,[expr ($cindex1-1)+3+$g]) == "x"} {
													incr add 1
													incr g 1
												}
											}
											#set right cindex2
											set  cindex2 [expr $cindex2 + $add]
										}
									}
							}	
						}	
						bind .main.oben2.buttonframe.right.menups.menue.c.$i.$j <B1-ButtonRelease> {
							foreach element [winfo children .main.oben2.buttonframe.right.menups.menue.c] {
									foreach element2 [winfo children $element] {
										if { [$element2 index active] != "none"} {
											set cindex  [expr [lindex [split $element2 .] [expr [llength [split $element2 .]] - 2]] + 1]
											set cindex1  [expr [lindex [split $element2 .] [expr [llength [split $element2 .]] - 1]] + 1]
											set cindex2 [expr [$element2 index active] + 1]
											#excluding the non exissting Enties
											set g 1
											set add 0
											set ref 1
											while { $ref <= $cindex2} {
												if {$CSequenz($cindex,[expr ($cindex1-1)+3+$g]) != "x"} {
													incr ref 1
													incr g 1
												} elseif {$CSequenz($cindex,[expr ($cindex1-1)+3+$g]) == "x"} {
													incr add 1
													incr g 1
												}
											}
											#set right cindex2
											set  cindex2 [expr $cindex2 + $add]
										}
									}
							}
						}
					}
			incr j $clength
			}
	
}

# set up the menu for the R-sqences
set list2 [array names RSequenz]
.main.oben2.buttonframe.right.menups.menue add cascade -label "$textpart(81)" -menu .main.oben2.buttonframe.right.menups.menue.r
menu .main.oben2.buttonframe.right.menups.menue.r -type normal -tearoff 0
#build the first cascade menue (R 07 ... R??)
for {set i 0} {$i <= [expr $z2 -1 ]} {incr i} {
	if {[expr fmod($i,30)]==0} {set colbreak 1} else {set colbreak 0}
		.main.oben2.buttonframe.right.menups.menue.r add cascade  -label "$RSequenz([expr $i + 1],0)$RSequenz([expr $i + 1],1)"  -columnbreak $colbreak -menu .main.oben2.buttonframe.right.menups.menue.r.$i
		menu .main.oben2.buttonframe.right.menups.menue.r.$i -tearoff 0
			set j 0
			#building the second cascade menu (with the full symmetrie)
			while {[lsearch $list2 "[expr $i + 1],$j"] >= 0} {
			.main.oben2.buttonframe.right.menups.menue.r.$i add cascade -label "$RSequenz([expr $i + 1],0)$RSequenz([expr $i + 1],1)_$RSequenz([expr $i + 1],[expr $j+2])^$RSequenz([expr $i + 1],[expr $j+3])" -menu .main.oben2.buttonframe.right.menups.menue.r.$i.$j
			menu .main.oben2.buttonframe.right.menups.menue.r.$i.$j -tearoff 0
					for {set h 4} {$h <= [expr [llength $Rhead]-1]} {incr h 1} {
						#exclude non exissting Enties
						if {$RSequenz([expr $i + 1],[expr $j+$h]) != "x"} {
							#building the phase men
							.main.oben2.buttonframe.right.menups.menue.r.$i.$j add command -label "[lindex $Rhead $h]" -command {
								#If cindex is correct...
								if {[regexp {^[0-9]*\.?[0-9]+$} $rindex] && [regexp {^[0-9]*\.?[0-9]+$} $rindex1] && [regexp {^[0-9]*\.?[0-9]+$} $rindex2] } {
									#...set kappa and the variables for the pictures etc.
									set kappa $RSequenz($rindex,[expr ($rindex1-1)+3+$rindex2])
									#configure the button label
									.main.oben2.buttonframe.right.menups configure -text "$RSequenz($rindex,[expr ($rindex1-1)]) $RSequenz($rindex,[expr ($rindex1-1)+1])_$RSequenz($rindex,[expr ($rindex1-1)+2])^$RSequenz($rindex,[expr ($rindex1-1)+3]) ([lindex $Rhead [expr $rindex2 + 3 ]])"
									set sym $RSequenz($rindex,[expr ($rindex1-1)])
									set symnb $RSequenz($rindex,[expr ($rindex1-1)+1])
									set symnbm1 [expr $RSequenz($rindex,[expr ($rindex1-1)+1]) - 1]
									set symnbh $RSequenz($rindex,[expr ($rindex1-1)+3])
									set symnbd $RSequenz($rindex,[expr ($rindex1-1)+2])
									set symnb_b $symnb
									set symnb ""
									set symnbm1 ""
									set symnbh_b $symnbh
									set symnbh ""
									set symnbd_b $symnbd
									set symnbd ""
									set sym1 R
									set sym2 R'
									set sym3 R'
									set unteneins ""
									set untenzwei ""
									if {$symnb_b < 10} {set symnbexp [format %1.0f $symnb_b]} else {set symnbexp [format %2.0f $symnb_b]}
									.main.oben2.buttonframe.mid.l_short3 configure -text "R-$rindex-$rindex1-$rindex2"
									#redraw picture
									redraw .main.bild $installdir
									focus .main.mitte.rechts.e_depha
									#update the favarits menu
									updatefavfile $favorits "$sym-$symnbexp-_$symnbd_b-^$symnbh_b" [lindex $Rhead [expr $rindex2 + 3 ]] "$kappa" "R-$rindex-$rindex1-$rindex2" $installdir
									set favorits [updatefavmen $installdir]
									for {set i 0} {$i <= [expr [llength $favorits]-1]} {incr i 1} {
										.main.oben2.buttonframe.right.menups.menue.fav add command -label "[lindex [lindex $favorits $i] 0] ([lindex [lindex $favorits $i] 1])" -command {
											set kappa [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 2]
											set sym [lindex [split [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 0] -] 0]
											set symnb [lindex [split [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 0] -] 1]
											set symnbm1 [expr [format %2.0f $symnb] - 1]
											set symnbh [lindex [split [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 0] -] 3]
											set symnbd [lindex [split [lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 0] -] 2]
											set symnbh [string trimleft $symnbh "_ ^"]
											set symnbd [string trimleft $symnbd "_ ^"]
											if {$sym == "C"} {
												set sym1 $sym
												set sym2 $sym
												set sym3 $sym
												set symnb_b $symnb
												set symnbh_b $symnbh
												set symnbd_b $symnbd
												set unteneins 0
												set untenzwei 1
											} elseif {$sym == "R"} {
												set unteneins ""
												set untenzwei ""
												set symnb_b $symnb
												set symnb ""
												set symnbm1 ""
												set symnbh_b $symnbh
												set symnbh ""
												set symnbd_b $symnbd
												set symnbd ""
												set sym1 R
												set sym2 R'
												set sym3 R'
											}
											.main.oben2.buttonframe.mid.l_short3 configure -text "[lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 3]"
											.main.oben2.buttonframe.right.menups configure -text "$sym $symnb_b _$symnbd_b ^$symnbh_b ([lindex [lindex $favorits [.main.oben2.buttonframe.right.menups.menue.fav index active]] 1])"
											redraw .main.bild $installdir
											focus .main.mitte.rechts.e_depha
										}
									}
								} else {tk_messageBox -message "$textpart(80)" -title "$textpart(20)" -type ok}
							}
							#getting the cindex2 form the Menu
							bind .main.oben2.buttonframe.right.menups.menue.r.$i.$j <ButtonPress> {
								foreach element [winfo children .main.oben2.buttonframe.right.menups.menue.r] {
									foreach element2 [winfo children $element] {
										if { [$element2 index active] != "none"} {
											set rindex  [expr [lindex [split $element2 .] [expr [llength [split $element2 .]] - 2]] + 1]
											set rindex1  [expr [lindex [split $element2 .] [expr [llength [split $element2 .]] - 1]] + 1]
											set rindex2 [expr [$element2 index active] + 1]
											#excluding the non exissting Enties
											set g 1
											set add 0
											set ref 1
											while { $ref <= $rindex2} {
												if {$RSequenz($rindex,[expr ($rindex1-1)+3+$g]) != "x"} {
													incr ref 1
													incr g 1
												} elseif {$RSequenz($rindex,[expr ($rindex1-1)+3+$g]) == "x"} {
													incr add 1
													incr g 1
												}
											}
											#set right cindex2
											set  rindex2 [expr $rindex2 + $add]
										}
									}
								}	
							}
							bind .main.oben2.buttonframe.right.menups.menue.r.$i.$j <B1-ButtonRelease> {
								foreach element [winfo children .main.oben2.buttonframe.right.menups.menue.r] {
									foreach element2 [winfo children $element] {
										if { [$element2 index active] != "none"} {
											set rindex  [expr [lindex [split $element2 .] [expr [llength [split $element2 .]] - 2]] + 1]
											set rindex1  [expr [lindex [split $element2 .] [expr [llength [split $element2 .]] - 1]] + 1]
											set rindex2 [expr [$element2 index active] + 1]
											#excluding the non exissting Enties
											set g 1
											set add 0
											set ref 1
											while { $ref <= $rindex2} {
												if {$RSequenz($rindex,[expr ($rindex1-1)+3+$g]) != "x"} {
													incr ref 1
													incr g 1
												} elseif {$RSequenz($rindex,[expr ($rindex1-1)+3+$g]) == "x"} {
													incr add 1
													incr g 1
												}
											}
											#set right cindex2
											set  rindex2 [expr $rindex2 + $add]
										}
									}
								}
							}
						}
					}
			incr j $rlength
			}
}
