

#
#
#  main program is after processes so as to be read
#
#

set CondNumFields 0
set CondNameList ""
set CondValueList ""

proc CondUpDateCondsForThisEntity { line condname} {
    global CondNumFields CondNameList CondValueList GidPriv
    set GidPriv(CondName) $condname
    set cnd $GidPriv(CondName)
    if { $cnd == "" } {
	set CondNumFields 0
    } else {
	set input [.central.s info conditions $cnd]
	set input [lrange $input 1 end]
	set CondNumFields [lindex $input 0]
    }
    set CondNameList ""
    set CondValueList ""
    regexp {[^-]*-(.*)$} $line {} line
    for { set i 0 } { $i < $CondNumFields } { incr i 1 } {
	lappend CondNameList [lindex $input [expr $i*2+1]]
	lappend CondValueList [lindex $line $i]
    }
    CondFillInfo $GidPriv(DatawCondWindow)
}

proc CreateCondEntitiesWin { wCond } {
    global CondNumFields GidPriv

    if { $GidPriv(CondName) == ""} {
	WarnWin [_ "Sorry, but first a condition must be selected"] $wCond
	return
    }

    InitWindow $wCond.cndwind $GidPriv(CondName) DatawCondListEntWindowGeom
    wm minsize  $wCond.cndwind 1 1

    frame $wCond.cndwind.lb -bd 0
    scrollbar $wCond.cndwind.lb.yscroll -relief sunken -command \
	    "$wCond.cndwind.lb.list yview" -bd 1 -elementborderwidth 1
    scrollbar $wCond.cndwind.lb.xscroll -relief sunken -orient horizontal \
	    -command "$wCond.cndwind.lb.list xview" -bd 1 -elementborderwidth 1
    listbox $wCond.cndwind.lb.list -yscroll \
	    "$wCond.cndwind.lb.yscroll \
	    set" -xscroll "$wCond.cndwind.lb.xscroll set" -relief sunken \
	    -selectmode extended -width 30 -height 20 \
	    -borderwidth 0 -relief flat -highlightthickness 0

    pack $wCond.cndwind.lb.yscroll -side right -fill y
    pack $wCond.cndwind.lb.xscroll -side bottom -fill x
    pack $wCond.cndwind.lb.list -expand yes -fill both

    button $wCond.cndwind.close -text [_ "Close"] \
	    -command "destroy $wCond.cndwind" -bd 1

    pack $wCond.cndwind.close -side bottom
    pack $wCond.cndwind.lb -expand yes -fill both -side top
    if { [lindex [.central.s info Project] 4] == "GEOMETRYUSE" } {
	set type geometry
    } else { set type mesh }
    set aa [.central.s info conditions $GidPriv(CondName) $type]
    foreach i $aa { $wCond.cndwind.lb.list insert end $i }


    bind $wCond.cndwind.lb.list <Double-1> "CondUpDateCondsForThisEntity \
	    \[selection get] $GidPriv(CondName)"


}

proc CondCancelVarWindow { wCond inum } {
    global CondNameList CondValueList
    set rows 1
    while { [winfo exists $wCond.frame2.c.f.win$inum.f.$rows] } \
	    { incr rows }
    set columns 1
    while { [winfo exists $wCond.frame2.c.f.win$inum.f.1.e$columns] } \
	    { incr columns}
    incr rows -1 ; incr columns -1
    set fvalue "#N# [expr $rows*$columns]"
    for {set i 1} {$i <= $columns} {incr i} {
	for {set j 1} {$j <= $rows} {incr j} {
	    lappend fvalue [$wCond.frame2.c.f.win$inum.f.$j.e$i \
		    get]
	}
    }
    set CondValueList [lreplace $CondValueList [expr $inum-1] \
	    [expr $inum-1] \
	    $fvalue]
    destroy $wCond.frame2.c.f.win$inum

}

proc CondOpenVarWindow { wCond i fvalue } {
    global CondNameList CondValueList

    if { [winfo exists $wCond.frame2.c.f.win$i] } { return }
    catch {destroy $wCond.frame2.c.f.win$i}
    toplevel $wCond.frame2.c.f.win$i
    set x [expr [winfo x $wCond]+300]
    set y [expr [winfo y $wCond]+[winfo y $wCond.frame2.c.f.b$i]]
    wm geometry $wCond.frame2.c.f.win$i +$x+$y
    wm title $wCond.frame2.c.f.win$i  [lindex [$wCond.frame2.c.f.label$i \
	    config -text] 4]
    wm iconname $wCond.frame2.c.f.win$i [lindex [$wCond.frame2.c.f.label$i \
	    config -text] 4]
    wm minsize  $wCond.frame2.c.f.win$i 1 1
    frame $wCond.frame2.c.f.win$i.f -bd 0
    set titlelist [split [lindex  $CondNameList [expr $i-1]] (),]
    set titlelist [lrange $titlelist 1 [expr [llength $titlelist]-2]]
    set fvalue [lindex $CondValueList [expr $i-1]]
    set numitems [expr [lindex $fvalue 1]/[llength $titlelist]]
    set numnewitems [$wCond.frame2.c.f.res$i get]
    set fvalue [lrange $fvalue 2 end]
    set num 1
    foreach j $titlelist {
	frame $wCond.frame2.c.f.win$i.f.$num -bd 0
	label $wCond.frame2.c.f.win$i.f.$num.l -text $j
	pack $wCond.frame2.c.f.win$i.f.$num.l -side top -padx 0 -pady 0 -ipadx 0 -ipady 0
	for {set k 1} {$k <= $numnewitems} {incr k} {
	    entry $wCond.frame2.c.f.win$i.f.$num.e$k -relief sunken -width 8 -bd 1
	    #-width 12
	    if { $k <= $numitems } {
		set idx [expr ($k-1)*[llength $titlelist]+$num-1]
		$wCond.frame2.c.f.win$i.f.$num.e$k insert 0 [lindex $fvalue \
		        $idx]
	    }
	    pack $wCond.frame2.c.f.win$i.f.$num.e$k -side top  -padx 0 -pady 0 -ipadx 0 -ipady 0
	}
	pack $wCond.frame2.c.f.win$i.f.$num -side left -padx 0 -pady 0 -ipadx 0 -ipady 0
	incr num
    }
    button $wCond.frame2.c.f.win$i.cancel -text [_ "Close"] \
	    -command "CondCancelVarWindow $wCond $i " -bd 1

    pack $wCond.frame2.c.f.win$i.f $wCond.frame2.c.f.win$i.cancel -side top -padx 0 -pady 0 -ipadx 0 -ipady 0
}

proc CondMakeVarWindow { wCond i fvalue } {
    set titlelist [split [lindex [$wCond.frame2.c.f.label$i config -text] 4] (),]
    $wCond.frame2.c.f.label$i config -text [lindex $titlelist 0]
    set titlelist [lrange $titlelist 1 [expr [llength $titlelist]-2]]
    set numitems [lindex $fvalue 1]
    set fvalue [lrange $fvalue 2 end]
    $wCond.frame2.c.f.res$i delete 0 end
    $wCond.frame2.c.f.res$i insert 0 [expr $numitems/[llength $titlelist]]
    $wCond.frame2.c.f.res$i config -width 5
    if { ![winfo exists $wCond.frame2.c.f.b$i] } {
	button $wCond.frame2.c.f.b$i -text [_ "Values"] \
		-command " CondOpenVarWindow $wCond $i \"$fvalue\" " -bd 1 
#-width 6

	grid $wCond.frame2.c.f.b$i -row [ expr $i - 1] -column 2 -sticky e \
		-padx 0 -pady 0 -ipadx 0 -ipady 0 -columnspan 1
	#pack $wCond.frame2.c.f.b$i -side right -padx 0 -pady 0 -ipadx 0 -ipady 0
    } else { $wCond.frame2.c.f.b$i conf \
	    -command " CondOpenVarWindow $wCond $i \"$fvalue\" "}

}

proc CondCalcLongest {} {
    global CondNumFields CondNameList CondValueList 
    
    set lengthName 0
    set lengthValue 0
 
    for {set i 1} {$i <= $CondNumFields} {incr i} {
	set fname [lindex $CondNameList [expr $i-1]]
	if { [regexp {([^#]*)#(CB|LA)#\(([^)]*)\)} $fname trash cbname] } {
	    if { [string length $cbname] > $lengthName } { 
		set lengthName [string length $cbname]
	    }
	} elseif { [regexp {([^#]*)#FUNC#} $fname trash cbname] } {
	    if { [string length $cbname] > $lengthName } { 
		set lengthName [string length $cbname]
	    }
	} else {
	    if { [string length $fname] > $lengthName } { 
		set lengthName [string length $fname]
	    }
	}

	set fvalue [lindex $CondValueList [expr $i-1]]
	if { [string length $fvalue] > $lengthValue } { 
	    set lengthValue [string length $fvalue]
	}
    }
    
    return "$lengthName $lengthValue"
}

proc CondFillInfo { wCond } {
    global CondNumFields CondNameList CondValueList

    set HasLocalAxes 0
    set aa [CondCalcLongest]
    set lengthName [expr [lindex $aa 0]+4]
    set lengthValue [expr [lindex $aa 1]+4]

#     if { [winfo exists $wCond.frame2.c.f ] } { 
#         set old 1
#     } else {  set old 0 }
    
#     if { $old == 0 } { 
#         frame $wCond.frame2.c.f -bd 0
#     }

    if { [winfo exists $wCond.frame2.c.f ] } { 
	destroy $wCond.frame2.c.f
    }
    frame $wCond.frame2.c.f -bd 0

    for {set i 1} {$i <= $CondNumFields} {incr i} {
	set fname [lindex $CondNameList [expr $i-1]]
	if { ![regexp {#(CB|LA)#} $fname] } {
	    regsub -all {_} $fname { } fname
	    regsub -all -- {-} $fname { } fname
	    if { [regexp {([^#]*)#FUNC#} $fname trash cbname] } {
		set fname $cbname
	    }
		
	    label $wCond.frame2.c.f.label$i -text $fname -anchor w
	    #-width $lengthName
	    #pack $wCond.frame2.c.$i.label -side left -padx 0 -pady 0 -ipadx 0 -ipady 0
	    grid $wCond.frame2.c.f.label$i -row [ expr $i - 1] -column 0 -sticky w \
		    -padx 0 -pady 0 -ipadx 0 -ipady 0
	    set globalcblist ""
	} else {
	    regexp {([^#]*)#(CB|LA)#\(([^)]*)\)} $fname trash globalcbname type \
		    globalcblist
	    regsub -all , $globalcblist " " globalcblist
	    regsub -all {_} $globalcbname { } globalcbname
	    regsub -all -- {-} $globalcbname { } globalcbname

	    label $wCond.frame2.c.f.label$i -text $globalcbname -anchor w
	    #-width $lengthName
	    #pack $wCond.frame2.c.$i.label -side left -padx 0 -pady 0 -ipadx 0 -ipady 0
	    grid $wCond.frame2.c.f.label$i -row [ expr $i - 1] -column 0 -sticky w \
		    -padx 0 -pady 0 -ipadx 0 -ipady 0
	}
	set fvalue [lindex $CondValueList [expr $i-1]]
	set lengthValue [expr [string length $fvalue]+4]
	if { $lengthValue < 15 } { set lengthValue 15 }
	if { $globalcblist == "" } {
	    set espaciocolumnas 2
	    set width 12
	    if { [string match #N#* $fvalue] } {
		set espaciocolumnas 1
		set width 6
	    }
	    if { ![winfo exists $wCond.frame2.c.f.res$i] } {
		entry $wCond.frame2.c.f.res$i -relief sunken -bd 1 -width 8
#$width
		#-width $lengthValue 
		#pack $wCond.frame2.c.$i.res -side left -padx 0 -pady 0 -ipadx 0 -ipady 0
		grid $wCond.frame2.c.f.res$i -row [ expr $i - 1] -column 1 -sticky we \
		        -padx 0 -pady 0 -ipadx 0 -ipady 0 -columnspan $espaciocolumnas
	    }
	    $wCond.frame2.c.f.res$i delete 0 end
	    $wCond.frame2.c.f.res$i insert end $fvalue
	    if { [string match #N#* $fvalue] } {
		CondMakeVarWindow $wCond $i $fvalue
	    }
	} else {
	    if { ![winfo exists $wCond.frame2.c.f.res$i] } {
		menubutton $wCond.frame2.c.f.res$i \
		        -menu $wCond.frame2.c.f.res$i.m -relief raised 
		#-padx 5 
		#pack $wCond.frame2.c.f.res$i -side left -padx 0 -pady 0 -ipadx 0 -ipady 0
		$wCond.frame2.c.f.res$i conf -bd 1 -highlightthickness 1
		grid $wCond.frame2.c.f.res$i -row [ expr $i - 1] -column 1 -sticky we \
		        -padx 0 -pady 0 -ipadx 0 -ipady 0 -columnspan 2
		
	    }
	    $wCond.frame2.c.f.res$i conf -text $fvalue 
	    #-width $lengthValue
	    if { [winfo exists $wCond.frame2.c.f.res$i.m] } { \
		    destroy $wCond.frame2.c.f.res$i.m }
	    menu $wCond.frame2.c.f.res$i.m -tearoff 0
	    $wCond.frame2.c.f.res$i.m conf -bd 1 -activeborderwidth 1

	    proc UpdateLocalAxesInMenu { menubut menu } {
		set idx 0
		set last [$menu index end]
		while { [string match -*- [$menu entrycget $idx -label]] } {
		    if { $idx > $last } { break }
		    incr idx
		}

		if { $last >= $idx } {
		    $menu del $idx end
		}
		set aa [.central.s info localaxes]
		foreach j $aa {
		    set namelab $j
		    if { [string length $j] < 7 } {
		        set namelab [format "%7s" $j]
		    }
		    $menu add command -label $namelab -command \
		            "$menubut conf -text $j"
		}
	    }
	    
	    if { $type == "LA" } {
		append globalcblist " " [.central.s info localaxes]
		set HasLocalAxes 1
		 $wCond.frame2.c.f.res$i.m conf -postcommand "UpdateLocalAxesInMenu \
		         $wCond.frame2.c.f.res$i $wCond.frame2.c.f.res$i.m"
	    }

	    foreach j $globalcblist {
		set namelab $j
		if { [string length $j] < 7 } {
		    set namelab [format "%7s" $j]
		}
		$wCond.frame2.c.f.res$i.m add command -label $namelab -command \
		        "$wCond.frame2.c.f.res$i conf -text $j"
	    }
	}
    }
    $wCond.frame2.c create window 0 0 -window $wCond.frame2.c.f -anchor nw 
    #-tag w$i
    #0.2c [expr $i-.9]c

#     set i [expr $CondNumFields+1]
#     while { [winfo exists $wCond.frame2.c.f] } {
#         destroy $wCond.frame2.c.f
#         $wCond.frame2.c delete w$i
#         incr i
#     }

#     set maxy [lindex [$wCond.frame2.c bbox w$CondNumFields] 3]
#      $wCond.frame2.c conf -scrollregion "0c 0c 15c [expr $i]c"
     $wCond.frame2.c conf -scrollregion "0c 0c [ expr $lengthName + $lengthValue]c [expr $CondNumFields]c"
#"0 0 [$wCond.frame2.c cget -width] [$wCond.frame2.c cget -height]"
     $wCond.frame2.c yview moveto 0

    if { $HasLocalAxes == 0 } {
	$wCond.frame25.b3 conf -menu $wCond.frame25.b3.nolocal
    } else { $wCond.frame25.b3 conf -menu $wCond.frame25.b3.local}


}

proc CondUpDateVars { wCond } {
    global CondNumFields CondNameList CondValueList
    for {set i 1} {$i <= $CondNumFields} {incr i} {
	if {[winfo exists $wCond.frame2.c.f.win$i.cancel]} {
	    $wCond.frame2.c.f.win$i.cancel invoke
	} else {
	    if { [string compare [winfo class $wCond.frame2.c.f.res$i] \
		    "Entry"] ==0 && ![winfo exists $wCond.frame2.c.f.b$i] } {
		set tmpchar [$wCond.frame2.c.f.res$i get]
	    } else { set tmpchar [lindex [$wCond.frame2.c.f.res$i \
		    config -text] 4]}
	    if { ![winfo exists $wCond.frame2.c.f.b$i] } {
		set CondValueList [lreplace $CondValueList [expr $i-1] [expr $i-1] \
		        $tmpchar]
	    }
	}
    }
}

#  CondUpDateCondsNames --
#      
#
#

proc CondUpDateCondsNames { w overwhat } {
    global GidPriv
    $w.buttonframe2.mb2 listbox del 0 end
    set inum 1
    foreach i [.central.s info conditions $overwhat] {
	if { $inum==1 } { 
	    set GidPriv(CondName) $i
	}
	$w.buttonframe2.mb2 add $i
	incr inum
    }
    if { $inum==1 } { set GidPriv(CondName) "" }
}

proc CondUpDateCondsT { name1 name2 op } { CondUpDateConds }

proc CondUpDateConds {} {
    global CondNumFields CondNameList CondValueList GidPriv
    set cnd $GidPriv(CondName)
    if { $cnd == "" } {
	set CondNumFields 0
    } else {
	set input [.central.s info conditions $cnd]
	set input [lrange $input 1 end]
	set CondNumFields [lindex $input 0]
    }
    set CondNameList ""
    set CondValueList ""
    for { set i 0 } { $i < $CondNumFields } { incr i 1 } {
	lappend CondNameList [lindex $input [expr $i*2+1]]
	lappend CondValueList [lindex $input [expr $i*2+2]]
    }
    CondFillInfo $GidPriv(DatawCondWindow)
}

#  CondUpDate --
#      Inserta controles asociados a las condiciones del tipo de problema actual
#      en la ventana especifiaca. Le pide la información a GID.
#  Argumentos:
#      w --> ventana donde colocará los controles
#  Resultados:
#      w lista para trabajar.

proc CondUpDate { w } {
    global CondNumFields CondNameList CondValueList

    $w.buttonframe.mb1 conf -text [_ "Over points"]
    set aa { "over points" "over lines" "over surfaces" "over volumes" }
    set aa2 { ovpnt ovline ovsurf ovvol }

#    set TheConds [.central.s info conditions ovpnt]
#    set NumConditions [llength $TheConds]

    $w.buttonframe.mb1.m del 0 end

    set j 0
    foreach i $aa { 
	$w.buttonframe.mb1.m add command -label $i -command \
		"$w.buttonframe.mb1 conf -text \"$i\" ; \
		CondUpDateCondsNames $w [lindex $aa2 $j]"
	incr j
    }

    # Establece la selección over points como la inicial.

    CondUpDateCondsNames $w ovpnt

#     set OldNumConds [$w.buttonframe2.mb2.m index end]
#     set input 0
#     set inum 1
#     foreach i $TheConds {
#         if { $inum==1 } { 
#             $w.buttonframe2.mb2 conf -text $i 
#             set input [.central.s info conditions $i]
#             set input [lrange $input 1 end]
#         }
#         if { $inum <= $OldNumConds } {
#             $w.buttonframe2.mb2.m entryconfigure $inum -label $i \
#                     -command "CondUpDateConds $w $i"
#         } else {
#             $w.buttonframe2.mb2.m add command -label $i -command \
#                     "CondUpDateConds $w $i"
#         }
#         incr inum
#     }

#     set CondNumFields [lindex $input 0]
#     set CondNameList ""
#     set CondValueList "" 
#     for { set i 0 } { $i < $CondNumFields } { incr i 1 } {
#         lappend CondNameList [lindex $input [expr $i*2+1]]
#         lappend CondValueList [lindex $input [expr $i*2+2]]
#     }
#     CondFillInfo $w
}

proc RecursiveChangeState {w {what "restore"}} {
  foreach i [winfo children $w] {
    RecursiveChangeState $i $what
  }
  catch {
    if {$what eq "restore"} {
      $w conf -state $::Gid_ChangeState($w)
    } else {
      set ::Gid_ChangeState($w) [$w cget -state]
      $w conf -state $what
    }
  }
}

proc FinishButton { w win text endaction disable {small 0} } {
  if { $small } {
    set w2 .gid.finish
    toplevel $w2
    wm transient $w2 .gid
    if { $::tcl_platform(platform) == "windows" } {
      AddAlwaysOnTopFlag $w2
    }
    wm title $w2 [wm title $w]
    scan [wm geometry $w] "%dx%d+%d+%d" width height x y
    if { ![regexp {[+]} $x] } { set x +$x}
    if { ![regexp {[+]} $y] } { set y +$y}
    wm geometry $w2 $x$y
    wm protocol $w2 WM_DELETE_WINDOW [list EndFinishButton $w $endaction $small]
    wm minsize $w2 50 25
    
    frame $w2.finf -relief ridge -bd 2
    button $w2.finf.f -text [_ "Finish"] -width 6 -command [list EndFinishButton $w $endaction $small]
    label $w2.finf.l -text $text
    grid $w2.finf.f $w2.finf.l -sticky ew -padx 2
    #grid configure $w2.finf.f -sticky w
    grid columnconf $w2.finf 1 -weight 1
    grid $w2.finf -sticky nsew -columnspan 2
    grid columnconf $w2 0 -weight 1
    grid rowconfig $w2 0 -weight 1
    
    grid $w2.finf -sticky nsew -columnspan 2
    grid columnconf $w2 0 -weight 1
    grid rowconfig $w2 0 -weight 1
    wm withdraw $w
  } else {
    set w2 $w
    
    frame $w2.finf -relief ridge -bd 2
    button $w2.finf.f -text [_ "Finish"] -width 6 -command [list EndFinishButton $w $endaction $small]
    label $w2.finf.l -text $text
    pack $w.finf.f $w.finf.l -side left
    place $w2.finf -in $win -x 0 -rely 1 -anchor sw -bordermode outside
    if { $disable == "disableall" } {
      RecursiveChangeState $w disabled
    }
    $w2.finf.f conf -state normal
  }
  focus $w2.finf.f
  bind $w2 <Escape> [list EndFinishButton $w $endaction $small]
  bind $w2 <Return> [list EndFinishButton $w $endaction $small]
  delayedop changefunc [list EndFinishButton $w $endaction $small]
}

proc EndFinishButton { w endaction small } {
  global GidPriv
  
  set found [delayedop -cancel changefunc "$w.finf.f invoke"]
  .central.s process escape escape
  
  if { $small } {
    set w2 .gid.finish
    if { [winfo exists $w2] } { 
      scan [wm geometry $w2] "%dx%d+%d+%d" width height x y
      if { ![regexp {[+]} $x] } { set x +$x}
      if { ![regexp {[+]} $y] } { set y +$y}
      destroy $w2
      wm geometry $w $x$y
    }
    wm deiconify $w
  } else {
    destroy $w.finf
    RecursiveChangeState $w
    array unset ::Gid_ChangeState
  }  
  if { $endaction != "" } { eval $endaction }
}

proc DrawConds { wCond what {field ""}} {
    global GidPriv

    switch $what {
	thiscond {
	    if { $GidPriv(CondName) == ""} {
		WarnWin [_ "Sorry, but first a condition must be selected"] $wCond
		return
	    }
	    if { $field == "" } {
		.central.s process escape escape escape escape data cond draw \
		        $GidPriv(CondName) \
		        -draw-
	    } else {
		.central.s process escape escape escape escape data cond draw \
		        $GidPriv(CondName) \
		        $field
	    }
	}
	colors {
	    if { $GidPriv(CondName) == ""} {
		WarnWin [_ "Sorry, but first a condition must be selected"] $wCond
		return
	    }
	    if { $field == "" } {
		.central.s process escape escape escape escape data cond draw \
		        -ByColor- $GidPriv(CondName) \
		        -draw-
	    } else {
		.central.s process escape escape escape escape data cond draw \
		        -ByColor- $GidPriv(CondName) \
		        $field
	    }
	}
	allconds {
	    .central.s process escape escape escape escape data cond draw \
		    -drawall-
	}
	thiscondLA {
	    if { $GidPriv(CondName) == ""} {
		WarnWin [_ "Sorry, but first a condition must be selected"] $wCond
		return
	    }
	    .central.s process escape escape escape escape data cond draw \
		    -LocalAxes- $GidPriv(CondName) -draw-
	}
	thiscondLAC {
	    if { $GidPriv(CondName) == ""} {
		WarnWin [_ "Sorry, but first a condition must be selected"] $wCond
		return
	    }
	    .central.s process escape escape escape escape data cond draw \
		    -LocalAxes- -LocalAxes- $GidPriv(CondName) -draw-
	}
	allcondsLA {
	    .central.s process escape escape escape escape data cond draw \
		    -LocalAxes- -drawall-
	}
	allcondsLAC {
	    .central.s process escape escape escape escape data cond draw \
		    -LocalAxes- -LocalAxes- -drawall-
	}  
    }
    set SmallWinSelecting [.central.s info variable SmallWinSelecting]
    FinishButton $wCond $wCond.frame25.b1 [_ "Press 'Finish' to end selection"] \
	    ".central.s process escape escape escape" nodisableall $SmallWinSelecting
}

proc AssignCond { wCond } {
    global CondValueList GidPriv

    if { $GidPriv(CondName) == ""} {
	WarnWin [_ "Sorry, but first a condition must be selected"] $wCond
	return
    }

    CondUpDateVars $wCond
    set retval ""
    foreach i $CondValueList {
	append retval "$i "
    }
    .central.s process escape escape escape escape data cond assign \
	    $GidPriv(CondName) \
	    change $retval

    set SmallWinSelecting [.central.s info variable SmallWinSelecting]
    FinishButton $wCond $wCond.frame25.b1 [_ "Press 'Finish' to end selection"] \
	    ".central.s process escape escape escape" disableall $SmallWinSelecting
}

proc UnAssignCond { wCond } {
    global CondValueList GidPriv

    .central.s process escape escape escape escape data cond \
	    assign $GidPriv(CondName) unassign

    set SmallWinSelecting [.central.s info variable SmallWinSelecting]
    FinishButton $wCond $wCond.frame25.b1 [_ "Press 'Finish' to end selection"] \
	    ".central.s process escape escape escape" disableall $SmallWinSelecting
}


proc FillCondDrawFields { wCond menu { colors no } } {
    global CondNameList
    $menu del 0 end
    foreach i $CondNameList {
	if { [regexp {([^#]*)#(CB|LA)#\(([^)]*)\)} $i trash cbname] } {
	    set i $cbname
	} elseif { [regexp {([^#]*)#(FUNC)#.*} $i trash cbname] } {
	    set i $cbname
	}
	
	if { $colors == "no" } {
	    $menu add command -label $i -command "DrawConds $wCond thiscond $i"
	} else {
	    $menu add command -label $i -command "DrawConds $wCond colors $i"
	}
    }
}

#
#  Main Program
#


proc DatawCond { { w .wdat }  { NFields 0 } {args "" } } {
    global CondNumFields CondNameList CondValueList wCond GidPriv
    set wCond $w

    if { [InitWindow $w [_ "Conditions"] DatawCondWindowGeom] == "close" } {
	return
    }

#     message $wCond.msg -font -Adobe-Times-Medium-R-Normal--*-180-* \
#             -width 13c -bd 2
    frame $wCond.buttonframe -bd 0


    frame $wCond.buttonframe2 -bd 0
#     $wCond.msg conf -text [_ "Conditions"]
    label $wCond.buttonframe.l1 -text [_ "Type"]:
    menubutton $wCond.buttonframe.mb1 -relief raised -menu \
	    $wCond.buttonframe.mb1.m -width 12
    $wCond.buttonframe.mb1 conf -bd 1 -highlightthickness 1
    menu $wCond.buttonframe.mb1.m
    $wCond.buttonframe.mb1.m conf -bd 1 -activeborderwidth 1

    label $wCond.buttonframe2.l2 -text [_ "Conditions"]:
    combobox $wCond.buttonframe2.mb2 -width 25 -textvariable GidPriv(CondName)
    trace variable GidPriv(CondName) w CondUpDateCondsT
    pack $wCond.buttonframe.l1 $wCond.buttonframe.mb1 -side left -padx 0 -pady 0 -ipadx 0 -ipady 0
    pack $wCond.buttonframe2.l2 $wCond.buttonframe2.mb2 -side left -padx 0 -pady 0 -ipadx 0 -ipady 0
    frame $wCond.frame25 -bd 0
    button $wCond.frame25.b1 -text [_ "Assign"] -padx 5 -command "AssignCond $wCond" -bd 1
 
    button $wCond.frame25.b2 -text [_ "Entities"] -padx 5 \
	    -command "CreateCondEntitiesWin $wCond" -bd 1

    menubutton $wCond.frame25.b3 -text [_ "Draw"] -padx 5 -relief raised \
	    -menu $wCond.frame25.b3.local
    $wCond.frame25.b3 conf -bd 1 -highlightthickness 1

    menu $wCond.frame25.b3.nolocal -bd 1 -activeborderwidth 1
    $wCond.frame25.b3.nolocal add command -label [_ "Draw this condition"] -command \
	    "DrawConds $wCond thiscond"

    $wCond.frame25.b3.nolocal add command -label [_ "Colors"] -command \
	    "DrawConds $wCond colors"

    $wCond.frame25.b3.nolocal add cascade -label [_ "Draw all conditions"] -menu \
	    $wCond.frame25.b3.nolocal.m2
    menu $wCond.frame25.b3.nolocal.m2 -bd 1 -activeborderwidth 1
    $wCond.frame25.b3.nolocal.m2 add command -label [_ "Only conditions"] -command \
	    "DrawConds $wCond allconds"
    $wCond.frame25.b3.nolocal.m2 add command -label [_ "Only local axes"] -command \
	    "DrawConds $wCond allcondsLA"
    $wCond.frame25.b3.nolocal.m2 add command -label [_ "Conds + LA"] -command \
	    "DrawConds $wCond allcondsLAC"

    $wCond.frame25.b3.nolocal add cascade -label [_ "Draw condition field"] -menu \
	    $wCond.frame25.b3.nolocal.f
    menu $wCond.frame25.b3.nolocal.f -postcommand \
	    "FillCondDrawFields $wCond $wCond.frame25.b3.nolocal.f" \
	    -bd 1 -activeborderwidth 1

    $wCond.frame25.b3.nolocal add cascade -label [_ "Colors field"] -menu \
	    $wCond.frame25.b3.nolocal.f2
    menu $wCond.frame25.b3.nolocal.f2 -postcommand \
	    "FillCondDrawFields $wCond $wCond.frame25.b3.nolocal.f2 colors" \
	    -bd 1 -activeborderwidth 1


    menu $wCond.frame25.b3.local -bd 1 -activeborderwidth 1

    $wCond.frame25.b3.local add cascade -label [_ "Draw this condition"] -menu \
	    $wCond.frame25.b3.local.m1
    menu $wCond.frame25.b3.local.m1 -bd 1 -activeborderwidth 1
    $wCond.frame25.b3.local add command -label [_ "Colors"] -command \
	    "DrawConds $wCond colors"
    $wCond.frame25.b3.local.m1 add command -label [_ "Only condition"] -command \
	    "DrawConds $wCond thiscond"
    $wCond.frame25.b3.local.m1 add command -label [_ "Only local axes"] -command \
	    "DrawConds $wCond thiscondLA"
    $wCond.frame25.b3.local.m1 add command -label [_ "Cond + LA"] -command \
	    "DrawConds $wCond thiscondLAC"

    $wCond.frame25.b3.local add cascade -label [_ "Draw all conditions"] -menu \
	    $wCond.frame25.b3.local.m2
    menu $wCond.frame25.b3.local.m2 -bd 1 -activeborderwidth 1
    $wCond.frame25.b3.local.m2 add command -label [_ "Only conditions"] -command \
	    "DrawConds $wCond allconds"
    $wCond.frame25.b3.local.m2 add command -label [_ "Only local axes"] -command \
	    "DrawConds $wCond allcondsLA"
    $wCond.frame25.b3.local.m2 add command -label [_ "Conds + LA"] -command \
	    "DrawConds $wCond allcondsLAC"

    $wCond.frame25.b3.local add cascade -label [_ "Draw condition field"] -menu \
	    $wCond.frame25.b3.local.f
    menu $wCond.frame25.b3.local.f -postcommand \
	    "FillCondDrawFields $wCond $wCond.frame25.b3.local.f" \
	    -bd 1 -activeborderwidth 1
    
    $wCond.frame25.b3.local add cascade -label [_ "Colors field"] -menu \
	    $wCond.frame25.b3.nolocal.f2
    menu $wCond.frame25.b3.local.f2 -postcommand \
	    "FillCondDrawFields $wCond $wCond.frame25.b3.nolocal.f2 colors" \
	    -bd 1 -activeborderwidth 1

### OJO ver el nolocal en la sentencia anterior


    menubutton $wCond.frame25.b4 -text [_ "UnAssign"] -padx 5 -relief raised \
	    -menu $wCond.frame25.b4.m
    $wCond.frame25.b4 conf -bd 1 -highlightthickness 1
    menu $wCond.frame25.b4.m -bd 1 -activeborderwidth 1
    $wCond.frame25.b4.m add command -label [_ "Some entities"] -command \
	    "UnAssignCond $wCond"

    $wCond.frame25.b4.m add command -label [_ "All this condition"] -command {
	set aa [mkDialogN $wCond.modal \
		[list -text [_ "Sure, unassign all this condition's entities for this interval?"]  \
		-aspect 250 -justify left] [list [_ "Cancel"] Cancel] [list [_ "Yes"] YES]]
	if { $aa == "Cancel" } { return }
	.central.s process escape escape escape escape data cond \
		assign $GidPriv(CondName) delete yes escape 
    }
    $wCond.frame25.b4.m add command -label [_ "All conditions"] -command {
	set aa [mkDialogN $wCond.modal \
		[list -text [_ "Sure, unassign all conditions' entities?"]  \
		-aspect 250 -justify left] [list [_ "Cancel"] Cancel] [list [_ "Yes"] YES]]
	if { $aa == "Cancel" } { return }
	.central.s process escape escape escape escape data cond unassign yes escape 
    }
    
    pack $wCond.frame25.b1 $wCond.frame25.b2 $wCond.frame25.b3 \
	    $wCond.frame25.b4 -side left -padx 2 -pady 2 -anchor nw -ipadx 0 -ipady 0
    
    frame $wCond.frame2 -relief raised -bd 1
    frame $wCond.frame3 -bd 0
    
    button $wCond.frame3.dismiss -text [_ "Close"] -command "destroy $wCond" -bd 1

    pack $wCond.frame3.dismiss -side left  -padx 0 -pady 2 -ipadx 0 -ipady 0
#-padx 15 -ipadx 5
#     pack $wCond.msg -side top -fill x
    pack $wCond.buttonframe -fill x -padx 0 -pady 2 -ipadx 0 -ipady 0
#-pady 5  -padx 5

    pack $wCond.buttonframe2 -fill x -padx 0 -pady 2 -ipadx 0 -ipady 0
#-pady 5  -padx 5 

    pack $wCond.frame3 -side bottom -anchor center -padx 0 -pady 2 -ipadx 0 -ipady 0
#-pady 5

    pack $wCond.frame25 -side bottom -fill x -padx 0 -pady 2 -ipadx 0 -ipady 0
#-pady 5

    pack $wCond.frame2 -side top -fill both -expand yes -padx 0 -pady 0 -ipadx 0 -ipady 0

    canvas $wCond.frame2.c -scrollregion {0c 0c 15c 80c} -width 275 \
	    -height 350 -yscrollcommand "$wCond.frame2.vscroll set" \
	    -xscrollcommand "$wCond.frame2.hscroll set" -takefocus 0
    scrollbar $wCond.frame2.vscroll  -relief sunken -command \
	    "$wCond.frame2.c yview" -takefocus 0 -bd 1 -elementborderwidth 1
    scrollbar $wCond.frame2.hscroll  -relief sunken -command \
	    "$wCond.frame2.c xview" -orient horizontal -takefocus 0 -bd 1 -elementborderwidth 1
    pack $wCond.frame2.vscroll -side right -fill y -padx 0 -pady 0 -ipadx 0 -ipady 0
    pack $wCond.frame2.hscroll -side bottom -fill x -padx 0 -pady 0 -ipadx 0 -ipady 0
    pack $wCond.frame2.c -in $wCond.frame2 -expand yes -fill both -padx 0 -pady 0 -ipadx 0 -ipady 0

    # A partir de aquí se comienza a rellenar la información dinámica

    set OldCondNumFields 0
    if { $NFields > 0 } {
	set CondNumFields $NFields
	set CondNameList ""
	set CondValueList ""
	for { set i 0 } { $i < $CondNumFields } { incr i 1 } {
	    lappend CondNameList [lindex $args [expr $i*2]]
	    lappend CondValueList [lindex $args [expr $i*2+1]]
	}
	CondFillInfo $w
    } else {
	CondUpDate $w
    }

}
