#!/bin/sh
#PostDisplay -*- TCL -*- Created: ramsan&miguel Jan-1997, Modified: miguel ???-1998


#----------------------PostDisplay ALONE---------------------------
# the next line restarts using wish \
	exec wish4.2 "$0" "$@"

# set PDRunningAlone 0
# if { ![info exists GIDDEFAULT] } {
#     set PDRunningAlone 1
#     set tcl_interactive 0
#     set tk_strictMotif 1

#     if { [catch { set GIDDEFAULT $env(GIDDEFAULT)}]!=1 } {
# 	set GIDDEFAULTTCL $GIDDEFAULT
# 	catch { set GIDDEFAULTTCL $env(GIDDEFAULTTCL) }
# 	set auto_path "$GIDDEFAULTTCL"
# 	source $GIDDEFAULTTCL/init.tcl
# 	source $GIDDEFAULTTCL/tk.tcl
#     }
# 	wm withdraw .
# }

#  LOOK AT THE END OF THE FILE !!!
#-------------------------------------------------------------------


proc WOGLRotStart {x y W } {
    global startx starty xangle0 yangle0 xangle yangle
    set startx $x
    set starty $y
    set vPos [$W position]
    set xangle0 [lindex $vPos 0]
    set yangle0 [lindex $vPos 1]
}

proc WOGLRotMove {x y W} {
    global startx starty xangle0 yangle0 xangle yangle
    set xangle [expr $xangle0 + ($x - $startx)  ]
    set yangle [expr $yangle0 + ($y - $starty)  ]
    $W rotate $xangle $yangle
}

proc WOGLChangeColor { } {
    global WoglPriv

    $WoglPriv(w) makecurrent
    $WoglPriv(w) setcolor ambient $WoglPriv(ambient)
    $WoglPriv(w) setcolor diffuse $WoglPriv(diffuse)
    $WoglPriv(w) setcolor specular $WoglPriv(specular)
    $WoglPriv(w) setcolor shininess $WoglPriv(shininess2)
    $WoglPriv(w) setcolor transparency $WoglPriv(transparency)
    #$WoglPriv(w) setcolor shininess [ expr pow(10,$WoglPriv(shininess) - 3.0)]
    $WoglPriv(w) settransparent $WoglPriv(transparent)
}

proc WOGLChangeColorComponent { wb comp} {
    global WoglPriv

    # actualizamos las escalas de colores
    set WoglPriv(SlideComponent) $comp
    WOGLChangeSlideComponent $comp

    if { "$wb" != ""} {
	set entero WoglPriv($comp)
	CCCambiaColor $wb $entero
    }
    if { $WoglPriv(Old$comp) != $WoglPriv($comp) } {
	$WoglPriv(w) makecurrent
	$WoglPriv(w) setcolor $comp $WoglPriv($comp) 
	set WoglPriv(Old$comp) $WoglPriv($comp)
	if { $comp ==  $WoglPriv(SlideComponent)} {
	    scan $WoglPriv($comp) #%2x%2x%2x r g b
	    $WoglPriv(slide,red) set $r
	    $WoglPriv(slide,green) set $g
	    $WoglPriv(slide,blue) set $b
	}
    }
}

proc WOGLAplicaTipoColor { } {
    global WoglPriv

    scan $WoglPriv(diffuse) #%2x%2x%2x r g b
    set new_transparency 0.0
    switch "$WoglPriv(TipoColor)" {
	"metal" {
	    #set r2 [ expr int( 0.5 + 64 - 0.25 * $r)]
	    #set g2 [ expr int( 0.5 + 64 - 0.25 * $g)]
	    #set b2 [ expr int( 0.5 + 64 - 0.25 * $b)]
	    set r2 [ expr int( 0.5 + 0.5 * $r)]
	    set g2 [ expr int( 0.5 + 0.5 * $g)]
	    set b2 [ expr int( 0.5 + 0.5 * $b)]
	    set WoglPriv(ambient) [ format #%02x%02x%02x $r2 $g2 $b2]
	    set WoglPriv(specular) #ffffff
	    set new_shininess 0.3
	    set new_transparency 0.0
	}
	"plastico" {
	    set r2 [ expr int( 0.5 + 0.33333333333 * $r)]
	    set g2 [ expr int( 0.5 + 0.33333333333 * $g)]
	    set b2 [ expr int( 0.5 + 0.33333333333 * $b)]
	    set WoglPriv(ambient) [ format #%02x%02x%02x $r2 $g2 $b2]
	    #set r2 [ expr int( 0.5 + 1.2 * $r)]
	    #set g2 [ expr int( 0.5 + 1.2 * $g)]
	    #set b2 [ expr int( 0.5 + 1.2 * $b)]
	    set r2 [ expr int( 0.5 + 0.5 * $r)]
	    set g2 [ expr int( 0.5 + 0.5 * $g)]
	    set b2 [ expr int( 0.5 + 0.5 * $b)]
	    if { $r2 > 255} { set r2 255}
	    if { $g2 > 255} { set g2 255}
	    if { $b2 > 255} { set b2 255}
	    set WoglPriv(specular) [ format #%02x%02x%02x $r2 $g2 $b2]
	    set new_shininess 1.0
	    set new_transparency 0.0
	}
	"goma" {
	    set mx $r
	    if { $g > $mx} { set mx $g}
	    if { $b > $mx} { set mx $b}
	    set mn $r
	    if { $g < $mn} { set mn $g}
	    if { $b < $mn} { set mn $b}
	    if { $mx == $mn} {
		set r2 [ expr int( 0.5 + 0.3 * $r)]
		set g2 [ expr int( 0.5 + 0.3 * $g)]
		set b2 [ expr int( 0.5 + 0.3 * $b)]
		set r3 [ expr int( 0.5 + 0.6 * $r)]
		set g3 [ expr int( 0.5 + 0.6 * $g)]
		set b3 [ expr int( 0.5 + 0.6 * $b)]
	    } else {
		set div [ expr 1.0 / ( $mx - $mn)]
		set fa [ expr 0.1 * $mx * $div ]
		set fb [ expr - $fa * $mn]
		set r2 [ expr int( 0.5 + $fa * $r + $fb)]
		set g2 [ expr int( 0.5 + $fa * $g + $fb)]
		set b2 [ expr int( 0.5 + $fa * $b + $fb)]
		set fa [ expr ( 1.4 * $mx - 0.1 * $mn) * $div]
		set fb [ expr $mn * ( 0.1 - $fa)]
		set r3 [ expr int( 0.5 + $fa * $r + $fb)]
		set g3 [ expr int( 0.5 + $fa * $g + $fb)]
		set b3 [ expr int( 0.5 + $fa * $b + $fb)]
		if { $r3 > 255} { set r3 255}
		if { $g3 > 255} { set g3 255}
		if { $b3 > 255} { set b3 255}
	    }
	    set WoglPriv(ambient) [ format #%02x%02x%02x $r2 $g2 $b2]
	    set WoglPriv(specular) [ format #%02x%02x%02x $r3 $g3 $b3]
	    set new_shininess 0.078
	    set new_transparency 0.0
	}
	default {
	    set WoglPriv(ambient) $WoglPriv(diffuse)
	    set WoglPriv(specular) #ffffff
	    set new_shininess 0.1
	    set new_transparency 0.0
	}
    }

    WOGLChangeColorComponent "" diffuse
    WOGLChangeColorComponent "" ambient
    WOGLChangeColorComponent "" specular
    
    $WoglPriv(diffuse,boton) conf -background $WoglPriv(diffuse)
    $WoglPriv(ambient,boton) conf -background $WoglPriv(ambient)
    $WoglPriv(specular,boton) conf -background $WoglPriv(specular)

    set WoglPriv(shininess2) $new_shininess
    set WoglPriv(transparency) $new_transparency
}

proc WOGLChangeColorBasico { wb} {
    global WoglPriv

    #set WoglPriv(diffuse) #cc0000
    #WOGLChangeColorComponent $WoglPriv(diffuse,boton) diffuse
    #set WoglPriv(ambient) $WoglPriv(diffuse)
    #WOGLChangeColorComponent $WoglPriv(ambient,boton) ambient
    #set WoglPriv(specular) #ffffff
    #WOGLChangeColorComponent $WoglPriv(specular,boton) specular

    #set entero WoglPriv(diffuse)
    #CCCambiaColor $wb $entero

    #set nuevo [ BWChooseColorMenu $wb.mc [ list right $wb] -color $WoglPriv(diffuse)]
    set nuevo [ BWChooseColor $wb.mc -title [_ "Select color"] -color $WoglPriv(diffuse)]
    if { [ string length $nuevo]} { 
	set WoglPriv(diffuse) $nuevo
	$wb conf -bg $nuevo
    }
    if { $WoglPriv(Olddiffuse) != $WoglPriv(diffuse) } {
	WOGLAplicaTipoColor
    }
}

proc WOGLChangeTipoColor { name1 name2 op} {
    WOGLAplicaTipoColor
}

proc WOGLChangeShininess { name1 name2 op } {
    global WoglPriv

    if { "$WoglPriv(shininess)" == ""} { return}
    if { $WoglPriv(shininess) > 3.0} {
	bell
	set WoglPriv(shininess) 3.0
	return
    }
    if { $WoglPriv(shininess) < 0.0} {
	bell
	set WoglPriv(shininess) 0.0
	return
    }

    if { [ scan $WoglPriv(shininess) %g sh]} {
	if { $WoglPriv(Oldshininess) != $sh} {
	    $WoglPriv(w) makecurrent
	    $WoglPriv(w) setcolor shininess [ expr pow(10,$sh - 3.0)]
	    set WoglPriv(Oldshininess) $sh
	    set WoglPriv(shininess2) [ expr pow(10,$sh - 3.0)]
	    set WoglPriv(Oldshininess2) $WoglPriv(shininess2)
	}
    }
}

proc WOGLChangeTransparency { name1 name2 op } {
    global WoglPriv

    if { "$WoglPriv(transparency)" == ""} { return}
    if { $WoglPriv(transparency) > 1.0} {
	bell
	set WoglPriv(transparency) 1.0
	return
    }
    if { $WoglPriv(transparency) < 0.0} {
	bell
	set WoglPriv(transparency) 0.0
	return
    }

    if { [ scan $WoglPriv(transparency) %g tr]} {
	if { $WoglPriv(Oldtransparency) != $tr} {
	    $WoglPriv(w) makecurrent
	    $WoglPriv(w) setcolor transparency $tr
	    set WoglPriv(Oldtransparency) $tr
	}
    }
}

proc WOGLChangeShininess2 { name1 name2 op } {
    global WoglPriv

    if { "$WoglPriv(shininess2)" == ""} { return}
    if { $WoglPriv(shininess2) > 1.0} {
	bell
	set WoglPriv(shininess2) 1.0
	return
    }
    if { $WoglPriv(shininess2) < 0.0} {
	bell
	set WoglPriv(shininess2) 0.0
	return
    }

    if { [ scan $WoglPriv(shininess2) %g sh]} {
	if { $WoglPriv(Oldshininess2) != $sh} {
	    $WoglPriv(w) makecurrent
	    $WoglPriv(w) setcolor shininess $sh
	    set WoglPriv(Oldshininess2) $sh
	    #set WoglPriv(shininess2)  [ expr pow(10,$sh - 3.0)]
	    set WoglPriv(shininess)  [ expr log10( $WoglPriv(shininess2)) + 3.0]
	    set WoglPriv(Oldshininess) $WoglPriv(shininess)
	}
    }
}

proc WOGLChangeSlideComponent { valor } {
    global WoglPriv

    if { $WoglPriv(SlideComponent) == $WoglPriv(OldSlideComponent) } { return}
    
    # WoglPriv(slide,red) conf -variable WoglPriv($WoglPriv(SlideComponent),red)
    # WoglPriv(slide,green) conf -variable WoglPriv($WoglPriv(SlideComponent),green)
    # WoglPriv(slide,blue) conf -variable WoglPriv($WoglPriv(SlideComponent),blue)
     
    set n [ scan $WoglPriv($WoglPriv(SlideComponent)) #%2x%2x%2x r g b]
    #WarnWin " $WoglPriv($WoglPriv(SlideComponent)) -> $r - $g - $b -"
    if { $n == 3} {
	$WoglPriv(slide,red) set $r
	$WoglPriv(slide,green) set $g
	$WoglPriv(slide,blue) set $b

	set WoglPriv(OldSlideComponent) $WoglPriv(SlideComponent)
	foreach id "ambient diffuse specular" {
	    if { "$WoglPriv(SlideComponent)" == "$id"} {
		$WoglPriv($id,radio) conf -text "->" -foreground black
	    } else {
		$WoglPriv($id,radio) conf -text "->" -foreground [ $WoglPriv($id,radio) cget -background]
	    }
	}
    }
}

proc WOGLChangeSlideColor { color valor} {
    global WoglPriv

    set comp $WoglPriv(SlideComponent)

    scan $WoglPriv($comp) #%2x%2x%2x r g b

    if { "$color" == "red"} {
	set r $valor
    }
    if { "$color" == "green"} {
	set g $valor
    }
    if { "$color" == "blue"} {
	set b $valor
    }

    set WoglPriv($comp) [ format #%02x%02x%02x $r $g $b]

    $WoglPriv($comp,boton) conf -background $WoglPriv($comp)
    if { "$comp" == "diffuse"} {
	$WoglPriv(diffuse,botonbasico) conf -background $WoglPriv($comp)
    }
    $WoglPriv(w) makecurrent
    $WoglPriv(w) setcolor $comp $WoglPriv($comp)
}

proc WOGLAcceptColor { } {
    global WoglPriv

    foreach comp "ambient diffuse specular" {
	if { $WoglPriv(Original,$comp) != $WoglPriv($comp)} {
	    foreach iset $WoglPriv(sets) {
		set tipo [ string index $iset 0]
		.central.s process escape escape escape escape Utilities ChangeColor $tipo $iset $comp $WoglPriv($comp)
	    }
	}   
    }
    set comp shininess
    if { $WoglPriv(Original,$comp) != $WoglPriv($comp)} {
	foreach iset $WoglPriv(sets) {
	    set tipo [ string index $iset 0]
	    .central.s process escape escape escape escape Utilities ChangeColor $tipo $iset $comp [ expr pow(10,$WoglPriv($comp) - 3.0)]
	}
    }
    set comp transparency
    if { $WoglPriv(Original,$comp) != $WoglPriv($comp)} {
	foreach iset $WoglPriv(sets) {
	    set tipo [ string index $iset 0]
	    .central.s process escape escape escape escape Utilities ChangeColor $tipo $iset $comp $WoglPriv($comp)
	}
    }   
}

proc WOGLResetColor { } {
    global WoglPriv

    set WoglPriv(ambient) $WoglPriv(Original,ambient)  
    set WoglPriv(diffuse) $WoglPriv(Original,diffuse)  
    set WoglPriv(specular) $WoglPriv(Original,specular) 
    set WoglPriv(shininess) $WoglPriv(Original,shininess)
    set WoglPriv(shininess2) $WoglPriv(Original,shininess2)
    set WoglPriv(transparency) $WoglPriv(Original,transparency)
    # actualizamos los colores del widget
    WOGLChangeColor
    set WoglPriv(OldSlideComponent) ""
    set WoglPriv(SlideComponent) ambient
    WOGLChangeSlideComponent ambient
    foreach comp "ambient diffuse specular" {
	$WoglPriv($comp,boton) conf -background $WoglPriv($comp)
	if { "$comp" == "diffuse"} {
	    $WoglPriv(diffuse,botonbasico) conf -background $WoglPriv($comp)
	}
    }
}

proc PDColorWindow { parent} {
    global GidPriv
    global WoglPriv tcl_platform

    # Get Set values
    set w $GidPriv(PostDisplayWindowList)
    set tbl $w.list.f2.tbl
    if { [ $tbl size] == 1} {
	$tbl selection set 0
    } 
    set setsel [$tbl curselection]

    catch { 
	trace vdelete WoglPriv(shininess) w WOGLChangeShininess
	trace vdelete WoglPriv(shininess2) w WOGLChangeShininess2
	trace vdelete WoglPriv(TipoColor) w WOGLChangeTipoColor
	trace vdelete WoglPriv(transparency) w WOGLChangeTransparency
    }

    # si hay varios, hace una media de los colores
    # con el accept imponemos esta media a todos los colores

    if { $setsel == {} } {
	tk_dialogRAM $w.tmpwin [_ "error"] [_ "Sorry, a set must be selected."] error 0 [_ "OK"]
	return error
    } else {
	#hacemos media de todos los seleccionados:
	set t_amb(r) 0
	set t_amb(g) 0
	set t_amb(b) 0
	set t_dif(r) 0
	set t_dif(g) 0
	set t_dif(b) 0
	set t_spe(r) 0
	set t_spe(g) 0
	set t_spe(b) 0
	set t_shini 0.0
	set t_transp 0.0
	set total 0
	set WoglPriv(sets) ""
	set WoglPriv(transparent) no
	foreach idx $setsel {
	    regsub -all { } [lindex [$tbl get $idx] 1] {_} curr
	    lappend WoglPriv(sets) $curr
	    # col_set = { #ambient #diffuse #specular shininess }
	    # set GidPriv(PD$cur,color) $col_set
	    set col_set $GidPriv(PD$curr,color)
	    #ambient
	    set color [ lindex $col_set 0]
	    set n [ scan $color #%2x%2x%2x r g b]
	    if { $n == 3} {
		set t_amb(r) [ expr $t_amb(r) + $r]
		set t_amb(g) [ expr $t_amb(g) + $g]
		set t_amb(b) [ expr $t_amb(b) + $b]
	    }
	    #diffuse
	    set color [ lindex $col_set 1]
	    set n [ scan $color #%2x%2x%2x r g b]
	    if { $n == 3} {
		set t_dif(r) [ expr $t_dif(r) + $r]
		set t_dif(g) [ expr $t_dif(g) + $g]
		set t_dif(b) [ expr $t_dif(b) + $b]
	    }
	    #specular
	    set color [ lindex $col_set 2]
	    set n [ scan $color #%2x%2x%2x r g b]
	    if { $n == 3} {
		set t_spe(r) [ expr $t_spe(r) + $r]
		set t_spe(g) [ expr $t_spe(g) + $g]
		set t_spe(b) [ expr $t_spe(b) + $b]
	    }
	    if { [ scan [ lindex $col_set 3] %g sh]} {
		set t_shini [ expr $t_shini + $sh]
	    }

	    incr total

	    set WoglPriv(transparent) [ .central.s info post get property transparent $curr]
	    set tt [ .central.s info post get property transparency $curr]
	    if { "$tt" != ""} {
		set t_transp [ expr $t_transp + $tt]
	    }
	}

	if { $total} {
	    set t_amb(r) [ expr int( 0.5 + double( $t_amb(r)) / double( $total))]
	    set t_amb(g) [ expr int( 0.5 + double( $t_amb(g)) / double( $total))]
	    set t_amb(b) [ expr int( 0.5 + double( $t_amb(b)) / double( $total))]
	    set t_dif(r) [ expr int( 0.5 + double( $t_dif(r)) / double( $total))]
	    set t_dif(g) [ expr int( 0.5 + double( $t_dif(g)) / double( $total))]
	    set t_dif(b) [ expr int( 0.5 + double( $t_dif(b)) / double( $total))]
	    set t_spe(r) [ expr int( 0.5 + double( $t_spe(r)) / double( $total))]
	    set t_spe(g) [ expr int( 0.5 + double( $t_spe(g)) / double( $total))]
	    set t_spe(b) [ expr int( 0.5 + double( $t_spe(b)) / double( $total))]
	    set t_shini [ expr double( $t_shini) / double( $total)]
	    set t_transp [ expr double( $t_transp) / double( $total)]
	}
    
	set WoglPriv(ambient) [ format #%02x%02x%02x $t_amb(r) $t_amb(g) $t_amb(b)]
	set WoglPriv(diffuse) [ format #%02x%02x%02x $t_dif(r) $t_dif(g) $t_dif(b)]
	set WoglPriv(specular) [ format #%02x%02x%02x $t_spe(r) $t_spe(g) $t_spe(b)]
	if { $t_shini == 0.0} {
	    set t_shini 1e-6
	}
	set WoglPriv(shininess) [ expr log10( $t_shini) + 3.0]
	set WoglPriv(shininess2) $t_shini
	set WoglPriv(transparency) $t_transp

	set WoglPriv(Original,ambient) $WoglPriv(ambient)
	set WoglPriv(Original,diffuse) $WoglPriv(diffuse)
	set WoglPriv(Original,specular) $WoglPriv(specular)
	set WoglPriv(Original,shininess) $WoglPriv(shininess)
	set WoglPriv(Original,shininess2) $WoglPriv(shininess2)
	set WoglPriv(Original,transparency) $WoglPriv(transparency)

	if { $total != 1} {
	    set WoglPriv(transparent) no
	}

    }

    #set WoglPriv(ambient) #543907
    #set WoglPriv(diffuse) #c7911d
    #set WoglPriv(specular) #fdf0ce
    #set WoglPriv(shininess) 0.217949
    
    set WoglPriv(Oldambient) $WoglPriv(ambient)
    set WoglPriv(Olddiffuse) $WoglPriv(diffuse)
    set WoglPriv(Oldspecular) $WoglPriv(specular)
    set WoglPriv(Oldshininess) $WoglPriv(shininess)
    set WoglPriv(Oldshininess2) $WoglPriv(shininess2)
    set WoglPriv(Oldtransparency) $WoglPriv(transparency)


    if { "$parent" != "."} {
	set w $parent.cw
    } else {
	set w .__pd__cw
    }

    if { [ winfo exist $w] } {
	# wm deiconify $w
	# vamos a ajustar el lugar
	update idletasks
	
	# extrange errors with updates
	if { ![winfo exists $w] } { return 0}
	#set pare [winfo toplevel [winfo parent $w]]
	set pare [winfo toplevel .gid.central.s]
	set x [expr [winfo x $pare]+[winfo width $pare ]/2- [winfo reqwidth $w]/2]
	set y [expr [winfo y $pare]+[winfo height $pare ]/2- [winfo reqheight $w]]
	if { $x < 0 } { set x 0 }
	if { $y < 0 } { set y 0 }
	WmGidGeom $w +$x+$y
	update
	wm deiconify $w    

    } else {
	toplevel $w

	wm title $w [_ "Change color window"]
	wm minsize $w 300 200

	if { $tcl_platform(platform) == "windows" } {
	    wm transient $w [winfo toplevel [winfo parent $w]]
	    togl $w.wogl -width 256 -height 256 -rgba true -double true \
		    -depth true -ident PDColorWindow -privatecmap false -overlay 0 -softrender 1
	} else {
	    togl $w.wogl -width 256 -height 256 -rgba true -double true \
		    -depth true -ident PDColorWindow -privatecmap false -overlay 0
	}
	set WoglPriv(w) $w.wogl
	
	bind $w.wogl <ButtonPress-1> { WOGLRotStart %x %y %W}
	bind $w.wogl <B1-Motion> { WOGLRotMove %x %y %W}

	frame $w.fcomp -relief sunken -bd 1

	NoteBook $w.fcomp.nb -internalborderwidth 0


	set pagina_basica [ $w.fcomp.nb insert end ConfigBasic -text [_ "Basic"]]

	label $pagina_basica.lc -text [_ "Color"]:
	button $pagina_basica.color -bd 1 -width 3 -background $WoglPriv(diffuse) \
		-command "WOGLChangeColorBasico $pagina_basica.color"
	set WoglPriv(diffuse,botonbasico) $pagina_basica.color

	#label $pagina_basica.ll -text [_ "Look:"]
	TitleFrame $pagina_basica.ll -text [_ "Look"]
	set f [ $pagina_basica.ll getframe]
	radiobutton $f.rd1 -text [_ "Metallic"] -value metal -variable WoglPriv(TipoColor)
	radiobutton $f.rd2 -text [_ "Plastic"] -value plastico -variable WoglPriv(TipoColor)
	radiobutton $f.rd3 -text [_ "Rubber"] -value goma -variable WoglPriv(TipoColor)
	set WoglPriv(TipoColor) plastico

	
	label $pagina_basica.l_transparency -text [_ "Transparency:"]
	#entry $pagina_basica.e_transparency -textvariable WoglPriv(transparency) -width 6
	scale $pagina_basica.ss -highlightthickness 0 \
		-variable WoglPriv(transparency) \
		-orient v -from 0.0 -to 1.0 -resolution 0.05 -bd 1

	grid $f.rd1 -sticky news -padx 3 -pady 2 -ipadx 2
	grid $f.rd2 -sticky news -padx 3 -pady 2 -ipadx 2
	grid $f.rd3 -sticky news -padx 3 -pady 2 -ipadx 2

	grid $pagina_basica.lc $pagina_basica.color -sticky news -padx 4 -pady 2 -ipadx 2
	grid $pagina_basica.ll -columnspan 2 -sticky news -padx 3 -pady 2 -ipadx 2

	grid $pagina_basica.l_transparency -column 2 -row 0 -sticky news -padx 3 -pady 2 -ipadx 2
	grid $pagina_basica.ss -column 2 -row 1 -sticky news -padx 3 -pady 2 -ipadx 2

	set pagina_avanzada [ $w.fcomp.nb insert end ConfigAvz -text [_ "Advanced"]]
	
	frame $pagina_avanzada.avz -relief flat -bd 0
	frame $pagina_avanzada.avz.but -relief sunken -bd 0

	set def_back [ $w cget -background]
	label $pagina_avanzada.avz.but.l -text [_ " Color components "] \
		-font BigFont -background [ CCColorActivo $def_back]
	
	grid $pagina_avanzada.avz.but.l -sticky wens -padx 10 -pady 10 -columnspan 3

	foreach comp "Ambient Diffuse Specular" {
	    set id [ string tolower $comp]
	    label $pagina_avanzada.avz.but.l_$id -text $comp\:
	    set entero WoglPriv($id)
	    button $pagina_avanzada.avz.but.b_$id -bd 1 -width 3 -background [ subst $$entero] \
		    -command "WOGLChangeColorComponent $pagina_avanzada.avz.but.b_$id $id"
	    radiobutton $pagina_avanzada.avz.but.r_$id -value $id -variable WoglPriv(SlideComponent) \
		    -command "WOGLChangeSlideComponent $id"
	    grid $pagina_avanzada.avz.but.l_$id $pagina_avanzada.avz.but.b_$id $pagina_avanzada.avz.but.r_$id \
		    -sticky e -padx 4 -pady 2 -ipadx 2
	    set WoglPriv($id,boton) $pagina_avanzada.avz.but.b_$id
	    set WoglPriv($id,radio) $pagina_avanzada.avz.but.r_$id
	}
	
	label $pagina_avanzada.avz.but.l_shininess -text [_ "Shininess:"]
	#entry $pagina_avanzada.avz.but.e_shininess -textvariable WoglPriv(shininess) -width 6
	entry $pagina_avanzada.avz.but.e_shininess -textvariable WoglPriv(shininess2) -width 6
	#entry $pagina_avanzada.avz.but.e_shininess2 -textvariable WoglPriv(shininess2) -width 6
	scale $pagina_avanzada.avz.but.ss -highlightthickness 0 \
		-variable WoglPriv(shininess) \
		-orient h -from 0.0 -to 3.0 -resolution 0.05 -bd 1 -showvalue 0
	
	#grid $pagina_avanzada.avz.but.l_shininess $pagina_avanzada.avz.but.e_shininess $pagina_avanzada.avz.but.e_shininess2 -sticky e -padx 4 -pady 2 -ipadx 2
	grid $pagina_avanzada.avz.but.l_shininess $pagina_avanzada.avz.but.e_shininess -sticky e -padx 4 -pady 2 -ipadx 2
	grid $pagina_avanzada.avz.but.ss -sticky ew -padx 4 -pady 2 -columnspan 3
	
	#set colm [ tk_optionMenu $pagina_avanzada.avz.but.om GidPriv(PostDisplayColorComponent) Ambient Diffuse Specular Shininess Default]
	#$pagina_avanzada.avz.but.om conf -width 7 -bd 1 -highlightthickness 1 -padx 4 -pady 2
	#$colm conf -bd 1 -activeborderwidth 1
	#set GidPriv(PostDisplayColorComponent) Diffuse
	
	# button $pagina_avanzada.avz.but.change -text [_ "Change color"] -command "WOGLChangeColor $w.wogl"
	
	# grid $pagina_avanzada.avz.but.om -padx 1 -ipadx 0 -ipady 0 -pady 1
	# grid $pagina_avanzada.avz.but.change -pady 4 -padx 4 -columnspan 2
	

	frame $pagina_avanzada.avz.scl -relief flat -bd 0
	
	label $pagina_avanzada.avz.scl.lred -text [_ "Red"]
	label $pagina_avanzada.avz.scl.lgreen -text [_ "Green"]
	label $pagina_avanzada.avz.scl.lblue -text [_ "Blue"]
	scale $pagina_avanzada.avz.scl.red -highlightthickness 0 -orient v -from 0 -to 255 -resolution 1 -bd 1 -command "WOGLChangeSlideColor red"
	scale $pagina_avanzada.avz.scl.green -highlightthickness 0 -orient v -from 0 -to 255 -resolution 1 -bd 1 -command "WOGLChangeSlideColor green"
	scale $pagina_avanzada.avz.scl.blue -highlightthickness 0 -orient v -from 0 -to 255 -resolution 1 -bd 1 -command "WOGLChangeSlideColor blue"
	set WoglPriv(slide,red) $pagina_avanzada.avz.scl.red
	set WoglPriv(slide,green) $pagina_avanzada.avz.scl.green
	set WoglPriv(slide,blue) $pagina_avanzada.avz.scl.blue
	
	grid $pagina_avanzada.avz.scl.lred -padx 4 -pady 2 -row 0 -column 0
	grid $pagina_avanzada.avz.scl.lgreen -padx 4 -pady 2 -row 0 -column 1
	grid $pagina_avanzada.avz.scl.lblue -padx 4 -pady 2 -row 0 -column 2
	grid $pagina_avanzada.avz.scl.red -sticky ns -padx 4 -pady 2 -row 1 -column 0
	grid $pagina_avanzada.avz.scl.green -sticky ns -padx 4 -pady 2 -row 1 -column 1
	grid $pagina_avanzada.avz.scl.blue -sticky ns -padx 4 -pady 2 -row 1 -column 2

	grid rowconfigure $pagina_avanzada.avz.scl 1 -weight 1
	grid columnconfigure $pagina_avanzada.avz.scl 0 -weight 1
	grid columnconfigure $pagina_avanzada.avz.scl 1 -weight 1
	grid columnconfigure $pagina_avanzada.avz.scl 2 -weight 1

	label $pagina_avanzada.avz.l -width 1

	grid $pagina_avanzada.avz.but $pagina_avanzada.avz.scl $pagina_avanzada.avz.l \
		-sticky ewns -padx 1 -pady 1
	grid conf $pagina_avanzada.avz.l -sticky e
	grid rowconfigure $pagina_avanzada.avz 0 -weight 1
	grid columnconfigure $pagina_avanzada.avz 0 -weight 1
	grid columnconfigure $pagina_avanzada.avz 1 -weight 1

	grid $pagina_avanzada.avz -sticky news


	grid $w.fcomp.nb -padx 2 -pady 2 -sticky news
	grid rowconfigure $w.fcomp 0 -weight 1
	grid columnconfigure $w.fcomp 0 -weight 1

	grid $w.fcomp -sticky news

	grid rowconfigure $w.fcomp 0 -weight 1
	grid columnconfigure $w.fcomp 0 -weight 1
	grid columnconfigure $w.fcomp 1 -weight 1

	#grid $w.fcomp.ftype.rd1 $w.fcomp.ftype.rd2 -sticky ew
	#grid rowconfigure $w.fcomp.ftype 0 -weight 1
	#grid columnconfigure $w.fcomp.ftype 0 -weight 1
	#grid $w.fcomp.ftype -sticky ew

	#grid rowconfigure $w.fcomp 0 -weight 1
	#grid rowconfigure $w.fcomp 1 -weight 1
	#grid columnconfigure $w.fcomp 0 -weight 1
        

	set def_back [ $w cget -background]
	
	# frame $w.but -bd 7 -background [ CCColorActivo $def_back] -relief flat
	frame $w.but -bd 0 -background [ CCColorActivo $def_back] -relief flat
	
	button $w.but.accept -text [_ "Apply"] -command "WOGLAcceptColor"
	button $w.but.reset -text [_ "Reset"] -command "WOGLResetColor"
	button $w.but.close -text [_ "Close"] -command "wm withdraw $w"
	
	# grid $w.but.accept -sticky ews -padx 4 -pady 3
	# grid $w.but.close -sticky ews -padx 4 -pady 3
	grid $w.but.accept $w.but.reset $w.but.close -sticky ews -padx 4 -pady 10
	

	grid $w.wogl -sticky news -row 0 -column 0 -rowspan 2
	grid $w.fcomp -sticky news -row 0 -column 1
	grid $w.but -sticky ews -row 1 -column 1
	
	grid columnconfigure $w 0 -weight 1
	grid rowconfigure $w 0 -weight 1

	

	
	# vamos a ajustar el lugar
	wm withdraw $w
	update idletasks
	
	# extrange errors with updates
	if { ![winfo exists $w] } { return 0}
	#set pare [winfo toplevel [winfo parent $w]]
	set pare [winfo toplevel .gid.central.s]
	set x [expr [winfo x $pare]+[winfo width $pare ]/2- [winfo reqwidth $w]/2]
	set y [expr [winfo y $pare]+[winfo height $pare ]/2- [winfo reqheight $w]]
	if { $x < 0 } { set x 0 }
	if { $y < 0 } { set y 0 }
	WmGidGeom $w +$x+$y
	$w.fcomp.nb compute_size
	$w.fcomp.nb raise [ $w.fcomp.nb page 0]
	update
	wm deiconify $w    




    }

    foreach comp "Ambient Diffuse Specular" {
	set id [ string tolower $comp]
	set entero WoglPriv($id)
	$WoglPriv($id,boton) conf -background [ subst $$entero]
	if { "$id" == "diffuse"} {
	    $WoglPriv(diffuse,botonbasico) conf -background [ subst $$entero]
	}
    }

    # actualizamos los colores del widget
    WOGLResetColor
    trace variable WoglPriv(shininess) w WOGLChangeShininess
    trace variable WoglPriv(shininess2) w WOGLChangeShininess2
    trace variable WoglPriv(TipoColor) w WOGLChangeTipoColor
    trace variable WoglPriv(transparency) w WOGLChangeTransparency
}

proc FillPDInfo {} {
    global GidPriv

# anyadimos 'cath' porque esta rutina tambien es llamada desde el programa
# y puede ser que no tengamos la ventana abierta.
    if { ![info exists GidPriv(PostDisplayWindow)] || \
	    ![winfo exists $GidPriv(PostDisplayWindow)] } { return }
    set w $GidPriv(PostDisplayWindow) 
    #set ret [ catch { set w $GidPriv(PostDisplayWindow) } ]
    #if { $ret != 0} { return }
    if { $GidPriv(PostDisplayDoingAccept) } { return }
    
#       foreach i "volumesets surfacesets cutsets" {
#  	set ret [ catch { $w.sel.$i.m del 0 end } ]
#  	if { $ret != 0 } { break }
#  	set var [.central.s info post get cur_$i]
#  	set colors [ .central.s info post get all_$i\_colors ]
#  	set jc 0
#  	foreach j [.central.s info post get all_$i] {
#  	    regsub -all {_} $j { } lab
#  	    set col_set [ lindex $colors $jc]
#  	    # col_set = { #ambient #diffuse #specular shininess }
#  	    $w.sel.$i.m add checkbutton -label $lab -variable GidPriv(PD$j,OnOff) -selectcolor [ lindex $col_set 1]
#  	    set jc [ expr $jc + 1]
#  	    if { [lsearch $var $j] != -1 } {
#  		set GidPriv(PD$j,OnOff) 1
#  	    } else {
#  		set GidPriv(PD$j,OnOff) 0
#  	    }
#  	}
#    }
    
    set var [.central.s info post get cur_display_style]
    regsub -all {_} $var { } var
    if { $GidPriv(PostDisplayDS) != "$var" } {
        set GidPriv(PostDisplayDS) "$var"
    }
    if { $GidPriv(PostDisplayRender) != [.central.s info post get cur_display_render] } {
        set GidPriv(PostDisplayRender) [.central.s info post get cur_display_render]
    }
    if { $GidPriv(PostDisplayCulling) != [.central.s info post get cur_display_culling] } {
        set GidPriv(PostDisplayCulling) [.central.s info post get cur_display_culling]
    }
    if { $GidPriv(PostDisplayConditions) != [.central.s info post get cur_show_conditions] } {
        set GidPriv(PostDisplayConditions) [.central.s info post get cur_show_conditions]
    }

    set var [ .central.s info post get cur_display_transparence]
    if { $var == "Transparent"} {
	set GidPriv(PostDisplayTransparent) 1
    } else {
	set GidPriv(PostDisplayTransparent) 0
    }

    set var [ .central.s info post get cur_display_body_type]
    if { $var == "Massive"} {
	set GidPriv(PostDisplayMassiveBodies) 1
    } else {
	set GidPriv(PostDisplayMassiveBodies) 0
    }

    FillPDListInfo

}


proc PDSetCheck { menu state } {
    global GidPriv
    set i 0
    while { $i == [$menu index $i] } {
	set varname [$menu entrycget $i -variable]
	set $varname $state
	incr i
    }
}


proc PDDeleteCosas {} {
    global GIDDEFAULT
    global GidPriv

    set w $GidPriv(PostDisplayWindowList) 
    set GidPriv(PostDisplayDoingAccept) 1

    set lista_cosas {}

    if { $GidPriv(WinList_volumes)} {
	set lista_cosas [ concat $lista_cosas volumes]
    }

    if { $GidPriv(WinList_surfaces)} {
	set lista_cosas [ concat $lista_cosas surfaces]
    }

    if { $GidPriv(WinList_cuts)} {
	set lista_cosas [ concat $lista_cosas cuts]
    }

    set tbl $w.list.f2.tbl
    if { [ $tbl size] == 1} {
	$tbl selection set 0
    } 
    set setsel [$tbl curselection]

    if { $setsel == {} } {
	tk_dialogRAM $w.tmpwin [_ "error"] [_ "Sorry, no set selected."] error 0 [_ "OK"]
	return
    } 

    set lst ""
    set lst_tipos ""
    foreach idx $setsel {
	regsub -all { } [lindex [$tbl get $idx] 1] {_} curr
	#set GidPriv(PD$curr,OnOff) 1
	lappend lst $curr
	set tipo [ lindex [lindex [$tbl get $idx] 1] 0]
	lappend lst_tipos $tipo
    }
    #FillPDListInfo 0
    foreach idx $setsel {
	$tbl selection set $idx
    }

    set res [tk_dialogRAM .gid.tempwinPD [_ "Delete Volumes/Surfaces/Cuts"] \
		 [_ "Are you sure you want to delete '%s'?" $lst]\
		 gidquestionhead 1 [_ "Yes"] [_ "No"]]
    if { $res == 1 } {
	return
    }

    set oldAutomaticRedraw [.central.s info variables AutomaticRedraw]
    .central.s process escape escape escape escape utilities variables \
	    AutomaticRedraw -1 escape escape 

    .central.s process escape escape escape escape

    foreach i $lst t $lst_tipos {
	.central.s process utilities delete $t $i yes escape escape
    }

    .central.s process escape escape escape escape utilities variables \
	    AutomaticRedraw $oldAutomaticRedraw escape escape 
#    .central.s makecurrent
#    .central.s process redraw
    .central.s render
    set GidPriv(PostDisplayDoingAccept) 0
    FillPDListInfo
}

proc PDAcceptOnOff {} {
    global GIDDEFAULT
    global GidPriv

    set oldAutomaticRedraw [.central.s info variables AutomaticRedraw]
    .central.s process escape escape escape escape utilities variables \
	    AutomaticRedraw -1 escape escape 

    set w $GidPriv(PostDisplayWindow)
    set GidPriv(PostDisplayDoingAccept) 1
    .central.s process escape escape escape escape

    set lista_cosas {}

    if { $GidPriv(WinList_volumes)} {
	set lista_cosas [ concat $lista_cosas volumes]
    }

    if { $GidPriv(WinList_surfaces)} {
	set lista_cosas [ concat $lista_cosas surfaces]
    }

    if { $GidPriv(WinList_cuts)} {
	set lista_cosas [ concat $lista_cosas cuts]
    }

    foreach i $lista_cosas {
	.central.s process select $i
	set var [.central.s info post get cur_$i]
	foreach j [.central.s info post get all_$i] {
	    if { [lsearch $var $j] != -1 } {
		if { $GidPriv(PD$j,OnOff) == 0 } {
		    .central.s process $j
		}
	    } else {
		if { $GidPriv(PD$j,OnOff) == 1 } {
		    .central.s process $j
		}
	    }
	}
	.central.s process escape
    }

    .central.s process escape escape escape escape utilities variables \
	    AutomaticRedraw $oldAutomaticRedraw escape escape 
#    .central.s makecurrent
#    .central.s process redraw
    .central.s render
    set GidPriv(PostDisplayDoingAccept) 0
    FillPDListInfo
}

proc PDAcceptPropiedad { cual} {
    global GIDDEFAULT
    global GidPriv

    set oldAutomaticRedraw [.central.s info variables AutomaticRedraw]
    .central.s process escape escape escape escape utilities variables \
	    AutomaticRedraw -1 escape escape 

    set w $GidPriv(PostDisplayWindow)
    set GidPriv(PostDisplayDoingAccept) 1
    .central.s process escape escape escape escape

    set lista_cosas {}

    if { $GidPriv(WinList_volumes)} {
	set lista_cosas [ concat $lista_cosas volumes]
    }

    if { $GidPriv(WinList_surfaces)} {
	set lista_cosas [ concat $lista_cosas surfaces]
    }

    if { $GidPriv(WinList_cuts)} {
	set lista_cosas [ concat $lista_cosas cuts]
    }

    foreach i $lista_cosas {
	foreach j [.central.s info post get all_$i] {
	    set is_prop [ string tolower [ string index [ .central.s info post get property $cual $j] 0]]
	    if { "$is_prop" == "y" } {
		if { $GidPriv(PD$j,$cual) == 0 } {
		    .central.s process escape escape escape escape utilities changeproperty $i $j $cual no
		}
	    } else {
		if { $GidPriv(PD$j,$cual) == 1 } {
		    .central.s process escape escape escape escape utilities changeproperty $i $j $cual yes
		}
	    }
	}
    }

    .central.s process escape escape escape escape utilities variables \
	    AutomaticRedraw $oldAutomaticRedraw escape escape 
#    .central.s makecurrent
#    .central.s process redraw
    .central.s render
    set GidPriv(PostDisplayDoingAccept) 0
    FillPDListInfo
}

proc PDChangeCullingToWogl { name1 name2 op} {
    global GidPriv WoglPriv

    # actualizamos las ruedas dentadas
    if { [ info exist WoglPriv(w)] && [ winfo exist $WoglPriv(w)]} {
	$WoglPriv(w) makecurrent
	$WoglPriv(w) setculling $GidPriv(PostDisplayCulling)
    }
}

proc PDChangeRenderToWogl { name1 name2 op} {
    global GidPriv WoglPriv

    # actualizamos las ruedas dentadas
    if { [ info exist WoglPriv(w)] && [ winfo exist $WoglPriv(w)]} {
	$WoglPriv(w) makecurrent
	$WoglPriv(w) setrender $GidPriv(PostDisplayRender)
    }
}

# proc PDChangeTransparentToWogl { name1 name2 op} {
#     global GidPriv WoglPriv
# 
#     # actualizamos las ruedas dentadas
#     if { [ info exist WoglPriv(w)] && [ winfo exist $WoglPriv(w)]} {
# 	  if { $GidPriv(PostDisplayTransparent)} {
# 	      $WoglPriv(w) settransparent yes
# 	  } else {
# 	      $WoglPriv(w) settransparent no
# 	  }
#     }
# }

proc PDChangeTransparentToWogl { transparente} {
    global WoglPriv

    # actualizamos las ruedas dentadas
    if { [ info exist WoglPriv(w)] && [ winfo exist $WoglPriv(w)]} {
	$WoglPriv(w) makecurrent
	$WoglPriv(w) settransparent $transparente
    }
}

proc PDAcceptStyle {} {
    global GIDDEFAULT
    global GidPriv

    #set oldAutomaticRedraw [.central.s info variables AutomaticRedraw]
    #.central.s process escape escape escape escape utilities variables \
    #	    AutomaticRedraw -1 escape escape 

    set olddisablegraphics [.central.s disable graphics]
    #set olddisablewindows [.central.s disable windows]
    #set olddisablegraphinput [.central.s disable graphinput]
    .central.s disable graphics 1
    #.central.s disable windows 1
    #.central.s disable graphinput 1 

    set w $GidPriv(PostDisplayWindow)
    set GidPriv(PostDisplayDoingAccept) 1

    regsub { } $GidPriv(PostDisplayDS) {} var
    if { $var != [.central.s info post get cur_display_style] } {
	.central.s process escape escape escape escape DisplayStyle $var
	if { [ info exist GidPriv(PostDisplayStyleBitmap)] && ( $GidPriv(PostDisplayStyleBitmap) != "") } {
	    set var [ string tolower $var]
	    $GidPriv(PostDisplayStyleBitmap) conf -bitmap @$GIDDEFAULT/bitmaps/$var
	}
    }

    if { $GidPriv(PostDisplayRender) != [ .central.s info post get cur_display_render] } {
	.central.s process escape escape escape escape utilities render $GidPriv(PostDisplayRender)
    }

    if { $GidPriv(PostDisplayCulling) != [ .central.s info post get cur_display_culling] } {
	.central.s process escape escape escape escape utilities render culling $GidPriv(PostDisplayCulling)
	if { [ info exist GidPriv(PostDisplayCullBitmap)] && ( $GidPriv(PostDisplayCullBitmap) != "") } {
	    set var [ string tolower $GidPriv(PostDisplayCulling)]
	    $GidPriv(PostDisplayCullBitmap) conf -bitmap @$GIDDEFAULT/bitmaps/cull$var
	}
    }

    if { $GidPriv(PostDisplayConditions) != [ .central.s info post get cur_show_conditions] } {
	.central.s process escape escape escape escape utilities show conditions $GidPriv(PostDisplayConditions)
    }

    if { [ .central.s info post get cur_display_transparence] == "Transparent" } {
	set vv 1 
    } else {
	set vv 0
    }
    if { $GidPriv(PostDisplayTransparent) != $vv } {
	if { $GidPriv(PostDisplayTransparent) == 1} {
	    .central.s process escape escape escape escape utilities render transparent
	} else {
	    .central.s process escape escape escape escape utilities render opaque
	}
    }
    if { [ .central.s info post get cur_display_body_type] == "Massive" } {
	set vv 1 
    } else {
	set vv 0
    }
    if { $GidPriv(PostDisplayMassiveBodies) != $vv } {
	if { $GidPriv(PostDisplayMassiveBodies) == 1} {
	    .central.s process escape escape escape escape results options massivebody yes
	} else {
	    .central.s process escape escape escape escape results options massivebody no
	}
    }
    #.central.s process escape escape escape escape utilities variables \
    #	    AutomaticRedraw $oldAutomaticRedraw escape escape 
    .central.s disable graphics $olddisablegraphics
    #.central.s disable windows $olddisablewindows
    #.central.s disable graphinput $olddisablegraphinput

#    .central.s makecurrent
#    .central.s process redraw

    #.central.s render
    .central.s process redraw
    set GidPriv(PostDisplayDoingAccept) 0
    FillPDListInfo
}

####################
# List Window
####################


proc FillPDListInfo { { recreate 1}} {
    global GidPriv
    if { ![info exists GidPriv(PostDisplayWindowList)] || \
	    ![winfo exists $GidPriv(PostDisplayWindowList)] } { return }
    set w $GidPriv(PostDisplayWindowList) 
    set tbl $w.list.f2.tbl

    if { $GidPriv(PostDisplayDoingAccept) } { return }

    # conseguimos la lista:
    set lista_cosas {}
    if { $GidPriv(WinList_volumes) == 1 } {
	set lista_cosas [ concat $lista_cosas volumes]
    }
    if { $GidPriv(WinList_surfaces) == 1} {
	set lista_cosas [ concat $lista_cosas surfaces]
    }
    if { $GidPriv(WinList_cuts) == 1} {
	set lista_cosas [ concat $lista_cosas cuts]
    }

    $tbl delete 0 end
   
    foreach i $lista_cosas {
	if { $recreate == 1} {
	    set var [.central.s info post get cur_$i]
	}
	
	set SetNames [.central.s info post get all_$i]
	set colors [ .central.s info post get all_$i\_colors ]
		
	foreach j $SetNames k $colors {
	    regsub -all {_} $j { } lab
	    # col_set = { #ambient #diffuse #specular shininess }
	    set GidPriv(PD$j,color) $k
	}

	if { [info exists GidPriv(SetsAlphabetic)] && $GidPriv(SetsAlphabetic) } {
	    set SetNames [lsort -dictionary $SetNames]
	}

	set SetsEscondidos ""
	foreach j $SetNames {
	    set ini ""
	    set end ""

	    if { [regexp {^([^_]*)[^(]*[(](.+)[)]} $j {} ini end] } {
		set oldset ${ini}_$end
		if { [regexp {[*]{5}} $j] && [lsearch $SetNames $oldset] != -1 } {
		    lappend SetsEscondidos $oldset
		}
	    }
	}

	foreach j $SetNames {
	    set listar 1
	    if { !$::PostDisplay(debug) } {
		if { [regexp {[*]{5}} $j] } { 
		    set listar 0
		}
	    }
	    set hay_back 0
	    if { [ lsearch $SetsEscondidos $j] != -1} {
		set hay_back 1
	    }
	    regsub -all {_} $j { } lab
	    set col_set $GidPriv(PD$j,color)
	    # col_set = { #ambient #diffuse #specular shininess }


	    set cur_col [ lindex $col_set 1]
	    if { $listar} {
		$tbl insert end [list "" $lab "" "" "" ""]
		$tbl cellconfigure end,0 -background $cur_col -text "" -selectbackground $cur_col
	    }
	
	    if { $recreate == 1} {
		if { [lsearch $var $j] != -1 } {
		    set GidPriv(PD$j,OnOff) 1
		} else {
		    set GidPriv(PD$j,OnOff) 0
		}
	    }
	    if { $listar} {
		if { $GidPriv(PD$j,OnOff) == 1} {
		    $tbl cellconfigure end,2 -image [GetImage layer_on.gif]
		} else {
		    $tbl cellconfigure end,2 -image [GetImage layer_off.gif]
		}
	    }

	    foreach cual "massive transparent" txt_flag "I T" {
		set is_prop [ string tolower [ string index [ .central.s info post get property $cual $j] 0]]
		if { $recreate == 1} {
		    if { "$is_prop" == "y"} {
			set GidPriv(PD$j,$cual) 1
		    } else {
			set GidPriv(PD$j,$cual) 0
		    }
		}
		if { $listar} {
		    if { $cual == "massive" } { 
			set col 3 
		    } else {
			set col 4
		    }
		    if { $GidPriv(PD$j,$cual) == 1} {
			$tbl cellconfigure end,$col -image [GetImage layer_${cual}_on.gif]
		    } else {
			$tbl cellconfigure end,$col -image [GetImage layer_${cual}_off.gif]
		    }
		}
	    }

	    if { $listar} {
		if { $hay_back} {
		    $tbl cellconfigure end,5 -image [GetImage layer_bringtofront.gif]
		} else {
		    $tbl cellconfigure end,5 -image [GetImage layer_sendtoback.gif] ;#void image
		}
	    }
	}
    }
}

proc PDListOn { { wboton ""}} {
    global GidPriv PostDisplay

    set w $GidPriv(PostDisplayWindowList)
    set tbl $w.list.f2.tbl
    if { [ $tbl size] == 1} {
	$tbl selection set 0
    } 
    set setsel [$tbl curselection] 

    if { $setsel == {} } {
	tk_dialogRAM $w.tmpwin [_ "error"] [_ "Sorry, no set selected."] error 0 [_ "OK"]
	return error
    } else {
	foreach idx $setsel {
	    regsub -all { } [lindex [$tbl get $idx] 1] {_} curr
	    set GidPriv(PD$curr,OnOff) 1
	}
	FillPDListInfo 0
	foreach idx $setsel {
	    $tbl selection set $idx
	}
    }

    PDAcceptOnOff

    foreach idx $setsel {
	$tbl selection set $idx
    }

    if { $wboton != ""} {
	$wboton conf -image $PostDisplay(OnImage)
    }
}

proc PDListPropiedad { cual { wboton ""}} {
    global GidPriv PostDisplay

    set w $GidPriv(PostDisplayWindowList)
    set tbl $w.list.f2.tbl
    if { [ $tbl size] == 1} {
	$tbl selection set 0
    } 
    set setsel [$tbl curselection]

    if { $setsel == {} } {
	tk_dialogRAM $w.tmpwin [_ "error"] [_ "Sorry, no set selected."] error 0 [_ "OK"]
	return error
    } else {
	foreach idx $setsel {
	    regsub -all { } [lindex [$tbl get $idx] 1] {_} curr
	    set GidPriv(PD$curr,$cual) 1
	}
	FillPDListInfo 0
	foreach idx $setsel {
	    $tbl selection set $idx
	}
    }

    PDAcceptPropiedad $cual

    foreach idx $setsel {
	$tbl selection set $idx
    }

    if { $wboton != ""} {
	$wboton conf -image $PostDisplay(OnImage)
    }

    if { "$cual" == "transparent"} {
	PDChangeTransparentToWogl yes
    }

}

proc PDChangeColor { } {
    global GidPriv

    set w $GidPriv(PostDisplayWindowList) 
    set tbl $w.list.f2.tbl
    if { [ $tbl size] == 1} {
	$tbl selection set 0
    } 
    set setsel [$tbl curselection]

    if { $setsel == {} } {
	tk_dialogRAM $w.terr [_ "error"] [_ "Sorry, no set selected."] error 0 [_ "OK"]
    } else {
	if { [ llength $setsel] == 1} {
	    set tipo [ lindex [lindex [$tbl get $setsel] 1] 0]
	    regsub -all { } [lindex [$tbl get $setsel] 1] {_} curr
	    .central.s process escape escape escape escape Utilities ChangeColor $tipo $curr $GidPriv(PostDisplayColorComponent)
	} else {
	    # Ambient Diffuse Specular Shininess Default
	    switch "$GidPriv(PostDisplayColorComponent)" {
		"Ambient" {
		    set res [ tk_chooseColor -title [_ "Change ambient color for %s" $setsel ]]
		}
		"Diffuse" {
		    set res [ tk_chooseColor -title [_ "Change diffuse color for %s" $setsel]]
		}
		"Specular" {
		    set res [ tk_chooseColor -title [_ "Change specular color for %s" $setsel]]
		}
		"Shininess" {
		    set res [ tk_dialogEntryRAM $w [_ "Change Shininess for %s" $setsel ] \
				  [_ "Enter shininess value( 0.0 - 1.0):"] gidquestionhead real+ 0.0]
		}
		"Default" {
		    set res Default
		}
	    }
	    if { "$res" != ""} {
		foreach idx $setsel {
		    set tipo [ lindex [lindex [$tbl get $idx] 1] 0]
		    regsub -all { } [lindex [$tbl get $idx] 1] {_} curr
		    .central.s process escape escape escape escape Utilities ChangeColor $tipo $curr $GidPriv(PostDisplayColorComponent) $res
		}
	    }
		
	}
	#FillPDListInfo 0
	foreach idx $setsel {
	    $tbl selection set $idx
	}
    }
}

proc PDChangeName { } {
    global GidPriv

    set w $GidPriv(PostDisplayWindowList) 
    set tbl $w.list.f2.tbl
    if { [ $tbl size] == 1} {
	$tbl selection set 0
    } 
    set setsel [$tbl curselection]

    if { $setsel == {} } {
	tk_dialogRAM $w.terr error [_ "Sorry, no set selected."] error 0 [_ "OK"]
    } else {
	if { [ llength $setsel] == 1} {
	    set tipo [lindex [lindex [$tbl get $setsel] 1] 0]
	    regsub -all { } [lindex [$tbl get $setsel] 1] {_} curr
	    .central.s process escape escape escape escape Utilities ChangeName $tipo $curr
	} else {
	    tk_dialogRAM $w.terr error [_ "Please select only one Volume/Surface/Cut."] error 0 [_ "OK"]
	}
	#FillPDListInfo 0
	foreach idx $setsel {
	    $tbl selection set $idx
	}
    }
}

proc PDListOff { { wboton ""}} {
    global GidPriv PostDisplay

    set w $GidPriv(PostDisplayWindowList) 
    set tbl $w.list.f2.tbl
    if { [ $tbl size] == 1} {
	$tbl selection set 0
    } 
    set setsel [$tbl curselection]
 
    if { $setsel == {} } {
	tk_dialogRAM $w.tmpwin [_ "error"] [_ "Sorry, no set selected."] error 0 [_ "OK"]
	return error
    } else {
	foreach idx $setsel {
	    regsub -all { } [lindex [$tbl get $idx] 1] {_} curr
	    set GidPriv(PD$curr,OnOff) 0
	}
	FillPDListInfo 0
	foreach idx $setsel {
	    $tbl selection set $idx
	}
    }

    PDAcceptOnOff

    
    foreach idx $setsel {
	$tbl selection set $idx
    }

    if { $wboton != ""} {
	$wboton conf -image $PostDisplay(OffImage)
    }
}

proc PDListPropiedadNo { cual { wboton ""}} {
    global GidPriv PostDisplay

    set w $GidPriv(PostDisplayWindowList) 
    set tbl $w.list.f2.tbl
    if { [ $tbl size] == 1} {
	$tbl selection set 0
    } 
    set setsel [$tbl curselection]
    if { $setsel == {} } {
	tk_dialogRAM $w.tmpwin [_ "error"] [_ "Sorry, no set selected."] error 0 [_ "OK"]
	return error
    } else {
	foreach idx $setsel {
	    regsub -all { } [lindex [$tbl get $idx] 1] {_} curr
	    set GidPriv(PD$curr,$cual) 0
	}
	FillPDListInfo 0
	foreach idx $setsel {
	    $tbl selection set $idx
	}
    }

    PDAcceptPropiedad $cual

    
    foreach idx $setsel {
	$tbl selection set $idx
    }

    if { $wboton != ""} {
	$wboton conf -image $PostDisplay(OffImage)
    }

    if { "$cual" == "transparent"} {
	PDChangeTransparentToWogl no
    }
}

proc PDListAll {} {
    global GidPriv

    set w $GidPriv(PostDisplayWindowList) 

    $w.list.f2.tbl selection set 0 end
}

proc PDListNone {} {
    global GidPriv

    set w $GidPriv(PostDisplayWindowList) 

    $w.list.f2.tbl selection clear 0 end
}

proc DoubleClickTableListSets { x y w tbl } { 
    set tablelist::x [expr {$x + [winfo x $w]}]
    set tablelist::y [expr {$y + [winfo y $w]}]
    set cell [$tbl nearestcell $tablelist::x $tablelist::y]
    #tk_messageBox -message "Selection: [$tbl curselection]. Clicked on cell $cell"
    foreach {row col} [ split $cell ,] break
    $tbl selection set $row
    if { $col == 0 } {
	PDColorWindow $::GidPriv(PostDisplayWindowList)
    } elseif { $col == 1 } {
	PDChangeName
    } elseif { $col == 2 } {
	if { [lindex [$tbl cellconfigure $row,$col -image] 4] == [GetImage layer_off.gif] } {
	    PDListOn
	    #$tbl cellconfigure $row,$col -image [GetImage layer_on.gif]
	} else {
	    PDListOff
	    #$tbl cellconfigure $row,$col -image [GetImage layer_off.gif]
	}
    } elseif { $col == 3 } {
	if { [lindex [$tbl cellconfigure $row,$col -image] 4] == [GetImage layer_massive_off.gif] } {
	    PDListPropiedad massive
	    #$tbl cellconfigure $row,$col -image [GetImage layer_massive_off.gif]
	} else {
	    PDListPropiedadNo massive
	    #$tbl cellconfigure $row,$col -image [GetImage layer_massive_on.gif]
	}
    } elseif { $col == 4 } {
	if { [lindex [$tbl cellconfigure $row,$col -image] 4] == [GetImage layer_transparent_off.gif] } {
	    PDListPropiedad transparent
	    #$tbl cellconfigure $row,$col -image [GetImage layer_transparent_off.gif]
	} else {
	    PDListPropiedadNo transparent
	    #$tbl cellconfigure $row,$col -image [GetImage layer_transparent_on.gif]
	}
    } elseif { $col == 5 } {
	if { [lindex [$tbl cellconfigure $row,$col -image] 4] == [GetImage layer_bringtofront.gif] } {
	    PDSendToBack $::GidPriv(PostDisplayWindowList).but ToFront
	    #$tbl cellconfigure $row,$col -image layer_bringtofront.gif
	} else {
	    #$tbl cellconfigure $row,$col -image layer_sendtoback.gif
	}
    }  
}

proc PDSetsCreateListWin { w } {
    global GidPriv

    set vsb $w.vsb1
    set hsb $w.hsb1
    set tbl [tablelist::tablelist $w.tbl \
            -exportselection 0 \
            -columns [list \
                0 "" left \
                5 "Name" left \
                0 "" left \
                0 "" left \
		0 "" left \
		0 "" left \
                ] \
            -selectbackground black -selectforeground white \
            -background $GidPriv(Color,BackgroundListbox) \
            -stretch 1 \
            -selectmode extended \
            -highlightthickness 0 \
            -labelborderwidth 1 \
	    -labelcommand tablelist::sortByColumn \
            -labelpady 0 \
            -showarrow 0 \
            -showseparators 0 \
            -xscrollcommand [list $hsb set] \
            -yscrollcommand [list $vsb set] \
            -resizablecolumns 0]
            #-labelcommand tablelist::sortByColumn -showarrow 1

    $tbl columnconfigure 2 -selectbackground $GidPriv(Color,BackgroundListbox) -selectforeground black
    $tbl columnconfigure 3 -selectbackground $GidPriv(Color,BackgroundListbox) -selectforeground black
    $tbl columnconfigure 4 -selectbackground $GidPriv(Color,BackgroundListbox) -selectforeground black
    $tbl columnconfigure 5 -selectbackground $GidPriv(Color,BackgroundListbox) -selectforeground black

    scrollbar $vsb -orient vertical   -command [list $tbl yview]
    scrollbar $hsb -orient horizontal -command [list $tbl xview]

    bind [$tbl bodypath] <Double-Button-1> "DoubleClickTableListSets %x %y %W $tbl" 

    grid $tbl $vsb -sticky nsew
    grid configure $vsb -sticky ns
    grid $hsb -sticky ew
    grid remove $vsb $hsb
    bind $tbl <Configure> [list ConfigureListScrollbars $tbl $hsb $vsb]

    grid columnconfigure $w 0 -weight 1
    grid rowconfigure $w 0 -weight 1
}

proc PDCreateListWin { { w .wPostDisplayListWin }} {
    global GidPriv PostDisplay GIDDEFAULT

#    InitWindow $w "Meshes, sets & cuts's list" PostDisplayWindowGeomList PDCreateListWin

    set GidPriv(PostDisplayWindowList) $w

    if { ![ info exist GidPriv(WinList_volumes) ] } {
	set GidPriv(WinList_volumes)    1
    }
    if { ![ info exist GidPriv(WinList_surfaces) ] } {
	set GidPriv(WinList_surfaces)      1
    }
    if { ![ info exist GidPriv(WinList_cuts) ] } {
	set GidPriv(WinList_cuts)      1
    }

    #wm minsize [ winfo toplevel $w] 250 400

    frame $w.list -relief ridge -bd 0
    frame $w.list.f1 -relief sunken -bd 0
    checkbutton $w.list.f1.cb1 -text [_ "Volumes"] -variable GidPriv(WinList_volumes) \
	    -command "FillPDListInfo 1" 
    checkbutton $w.list.f1.cb2 -text [_ "Surfaces"] -variable GidPriv(WinList_surfaces) \
	    -command "FillPDListInfo 1" 
    checkbutton $w.list.f1.cb3 -text [_ "Cuts"] -variable GidPriv(WinList_cuts) \
	    -command "FillPDListInfo 1"

    checkbutton $w.list.f1.sort -text [_ "alphabetic order"] -variable GidPriv(SetsAlphabetic) \
	    -command FillPDListInfo
    GidHelp "$w.list.f1.sort"  [_ "Sort the list of sets by name."]

    grid $w.list.f1.cb1 $w.list.f1.cb2 $w.list.f1.cb3 -sticky we
    grid $w.list.f1.sort -columnspan 3 -sticky w

    frame $w.list.f2 -relief sunken -bd 1

    PDSetsCreateListWin $w.list.f2
   
    GiDPopUpHelp $w.list.f2 "Properties of the listed sets showing:\n"\
	    "   Color\n"\
	    "   Name\n"\
	    "   Show/Hide\n"\
	    "   Draws interior/Not drawn\n"\
	    "   Transparent/Opaque\n"\
	    "   Exists background hidden elements."

    grid $w.list.f1 -row 0 -column 0 -sticky we
    grid $w.list.f2 -row 1 -column 0 -sticky wens
    grid columnconfigure $w.list 0 -weight 1
    grid rowconfigure $w.list 1 -weight 1


    frame $w.but


    if { 0} {
	frame $w.but.but3

	#button $w.but.but3.cw -text "Color..." -command "PDColorWindow $w" -takefocus 0 -bd 1 -padx 5 -pady 3
	if { ![ info exist PostDisplay(ColorImage)] } {
	    set PostDisplay(ColorImage) [ image create photo -file [ file join $GIDDEFAULT bitmaps HSV_color.gif]]	    
	}
	if { 0} {
	    button $w.but.but3.cw -image $PostDisplay(ColorImage) -width 32 -height 32\
		    -command "PDColorWindow $w" -takefocus 0 -bd 1 -padx 5 -pady 3 
	} else {
	    set def_back [ $w.but.but3 cget -background]

	    BotonPlano $w.but.but3.cw "Color..." "PDColorWindow $w" [ CCColorSombra $def_back] [ CCColorActivo $def_back]
	    $w.but.but3.cw conf -image $PostDisplay(ColorImage) -takefocus 0 -padx 5 -pady 3 -width 32 -height 32
	}
	button $w.but.but3.bnombre -text [_ "Rename"] -command PDChangeName -takefocus 0 -bd 1 -padx 5 -pady 3
	button $w.but.but3.del -text [_ "Delete"] -command "PDDeleteCosas" -takefocus 0 -bd 1 -padx 5 -pady 3

	# checkbutton $w.but.but3.short -text [_ "alphabetic"] -variable GidPriv(SetsAlphabetic) \
		# 	      -command FillPDListInfo
	# GidHelp "$w.but.but3.short"  [_ "Sort the list of sets by name."]

	#grid $w.but.but3.cw $w.but.but3.del $w.but.but3.bnombre $w.but.but3.short -padx 1 -ipadx 0 -pady 1
	grid $w.but.but3.cw $w.but.but3.del $w.but.but3.bnombre -padx 1 -ipadx 0 -pady 1
	#grid $w.but.but3.bnombre $w.but.but3.del -padx 1 -ipadx 0 -pady 1
	

	if { ![ info exist PostDisplay(OnImage)] } {
	    set PostDisplay(OnImage) [ image create photo -file [ file join $GIDDEFAULT bitmaps eye_open.gif]]	    
	}
	if { ![ info exist PostDisplay(OffImage)] } {
	    set PostDisplay(OffImage) [ image create photo -file [ file join $GIDDEFAULT bitmaps eye_closed.gif]]
	}

	if { ![ info exist PostDisplay(IntImage)] } {
	    set PostDisplay(IntImage) [ image create photo -file [ file join $GIDDEFAULT bitmaps interior.gif]]
	}
	if { ![ info exist PostDisplay(IntNoImage)] } {
	    set PostDisplay(IntNoImage) [ image create photo -file [ file join $GIDDEFAULT bitmaps interior_no.gif]]
	}

	if { ![ info exist PostDisplay(TranspImage)] } {
	    set PostDisplay(TranspImage) [ image create photo -file [ file join $GIDDEFAULT bitmaps transparente.gif]]
	}
	if { ![ info exist PostDisplay(TranspNoImage)] } {
	    set PostDisplay(TranspNoImage) [ image create photo -file [ file join $GIDDEFAULT bitmaps opaco.gif]]
	}

	frame $w.but.but4
	if { 0} {
	    button $w.but.but4.on -image $PostDisplay(OnImage) -command "PDListOn" \
		    -takefocus 0 -bd 1 -padx 4 -pady 2 -width 32 -height 32
	    button $w.but.but4.off -image $PostDisplay(OffImage) -command "PDListOff" \
		    -takefocus 0 -bd 1 -padx 4 -pady 2 -width 32 -height 32
	    button $w.but.but4.interior -image $PostDisplay(IntImage) -command "PDListPropiedad massive" \
		    -takefocus 0 -bd 1 -padx 4 -pady 2 -width 32 -height 32
	    button $w.but.but4.hollow -image $PostDisplay(IntNoImage) -command "PDListPropiedadNo massive" \
		    -takefocus 0 -bd 1 -padx 4 -pady 2 -width 32 -height 32
	    button $w.but.but4.transp -image $PostDisplay(TranspImage) -command "PDListPropiedad transparent" \
		    -takefocus 0 -bd 1 -padx 4 -pady 2 -width 32 -height 32
	    button $w.but.but4.opaco -image $PostDisplay(TranspNoImage) -command "PDListPropiedadNo transparent" \
		    -takefocus 0 -bd 1 -padx 4 -pady 2 -width 32 -height 32
	} else {
	    set def_back [ $w.but.but4 cget -background]

	    BotonPlano $w.but.but4.on On PDListOn [ CCColorSombra $def_back] [ CCColorActivo $def_back]
	    $w.but.but4.on conf -image $PostDisplay(OnImage) -takefocus 0 -padx 4 -pady 2 -width 32 -height 32
	    BotonPlano $w.but.but4.off Off PDListOff [ CCColorSombra $def_back] [ CCColorActivo $def_back]
	    $w.but.but4.off conf -image $PostDisplay(OffImage) -takefocus 0 -padx 4 -pady 2 -width 32 -height 32

	    BotonPlano $w.but.but4.interior "Draw interiors" "PDListPropiedad massive" [ CCColorSombra $def_back] [ CCColorActivo $def_back]
	    $w.but.but4.interior conf -image $PostDisplay(IntImage) -takefocus 0 -padx 4 -pady 2 -width 32 -height 32
	    BotonPlano $w.but.but4.hollow "Hollow bodies" "PDListPropiedadNo massive" [ CCColorSombra $def_back] [ CCColorActivo $def_back]
	    $w.but.but4.hollow conf -image $PostDisplay(IntNoImage) -takefocus 0 -padx 4 -pady 2 -width 32 -height 32

	    BotonPlano $w.but.but4.transp "Transparent" "PDListPropiedad transparent" [ CCColorSombra $def_back] [ CCColorActivo $def_back]
	    $w.but.but4.transp conf -image $PostDisplay(TranspImage) -takefocus 0 -padx 4 -pady 2 -width 32 -height 32
	    BotonPlano $w.but.but4.opaco "Opaque" "PDListPropiedadNo transparent" [ CCColorSombra $def_back] [ CCColorActivo $def_back]
	    $w.but.but4.opaco conf -image $PostDisplay(TranspNoImage) -takefocus 0 -padx 4 -pady 2 -width 32 -height 32
	    
	}

	GiDPopUpHelp $w.but.but4.on "Turns on the selected sets."
	GiDPopUpHelp $w.but.but4.off "Turns off the selected sets."
	GiDPopUpHelp $w.but.but4.interior "Draws the interior elements and vectors of sets."
	GiDPopUpHelp $w.but.but4.hollow "Does not drawn interior vectors of the selected sets and elements of volume sets."
	GiDPopUpHelp $w.but.but4.transp "Turns the selected sets transparent."
	GiDPopUpHelp $w.but.but4.opaco "Turns the selected sets opaque."

	grid $w.but.but4.interior -row 0 -column 0 -padx 1 -ipadx 0 -pady 0 -sticky e
	grid $w.but.but4.hollow -row 0 -column 1 -padx 1 -ipadx 0 -pady 0 -sticky w
	grid $w.but.but4.on -row 0 -column 2 -padx 1 -ipadx 0 -pady 0 -sticky e
	grid $w.but.but4.off -row 0 -column 3 -padx 1 -ipadx 0 -pady 0 -sticky w
	grid $w.but.but4.transp -row 0 -column 4 -padx 1 -ipadx 0 -pady 0 -sticky e
	grid $w.but.but4.opaco -row 0 -column 5 -padx 1 -ipadx 0 -pady 0 -sticky w
	# button $w.but.but4.on -text On -command "PDListOn" -takefocus 0 -bd 1 -padx 4 -pady 2
	# button $w.but.but4.off -text Off -command "PDListOff" -takefocus 0 -bd 1 -padx 4 -pady 2
	# button $w.but.but4.interior -text "Draw interiors" -command "PDListPropiedad massive" -takefocus 0 -bd 1 -padx 4 -pady 2
	# button $w.but.but4.hollow -text "Hollow bodies" -command "PDListPropiedadNo massive" -takefocus 0 -bd 1 -padx 4 -pady 2
	# button $w.but.but4.transp -text Transparent -command "PDListPropiedad transparent" -takefocus 0 -bd 1 -padx 4 -pady 2
	# button $w.but.but4.opaco -text Opaque -command "PDListPropiedadNo transparent" -takefocus 0 -bd 1 -padx 4 -pady 2
	# grid $w.but.but4.on $w.but.but4.interior $w.but.but4.transp -padx 1 -ipadx 0 -pady 0 -sticky ew
	# grid $w.but.but4.off $w.but.but4.hollow $w.but.but4.opaco -padx 1 -ipadx 0 -pady 0 -sticky ew

	grid rowconfigure $w.but.but4 0 -weight 1
	grid columnconfigure $w.but.but4 0 -weight 1
	grid columnconfigure $w.but.but4 1 -weight 1
	grid columnconfigure $w.but.but4 2 -weight 1
	grid columnconfigure $w.but.but4 3 -weight 1
	grid columnconfigure $w.but.but4 4 -weight 1
	grid columnconfigure $w.but.but4 5 -weight 1

	
	#cw#    grid $w.but.but2 
	grid $w.but.but3 -sticky ew 
	grid $w.but.but4 -sticky ew -padx 0 -ipadx 0 -pady 2 

    } else { ;# otro empaquetamiento

	frame $w.but.but3

	if { ![ info exist PostDisplay(ColorImage)] } {
	    set PostDisplay(ColorImage) [ image create photo -file [ file join $GIDDEFAULT bitmaps HSV_color.gif]]	    
	}

	set def_back [ $w.but.but3 cget -background]
	
	BotonPlano $w.but.but3.cw "Color..." "PDColorWindow $w" [ CCColorSombra $def_back] [ CCColorActivo $def_back]
	$w.but.but3.cw conf -image $PostDisplay(ColorImage) -takefocus 0 -padx 5 -pady 3 -width 32 -height 32

	button $w.but.but3.bnombre -text [_ "Rename"] -command PDChangeName -takefocus 0 -bd 1 -padx 5 -pady 3
	button $w.but.but3.del -text [_ "Delete"] -command "PDDeleteCosas" -takefocus 0 -bd 1 -padx 5 -pady 3

	if { ![ info exist PostDisplay(OnImage)] } {
	    set PostDisplay(OnImage) [ image create photo -file [ file join $GIDDEFAULT bitmaps eye_open.gif]]	    
	}
	if { ![ info exist PostDisplay(OffImage)] } {
	    set PostDisplay(OffImage) [ image create photo -file [ file join $GIDDEFAULT bitmaps eye_closed.gif]]
	}

	if { ![ info exist PostDisplay(IntImage)] } {
	    set PostDisplay(IntImage) [ image create photo -file [ file join $GIDDEFAULT bitmaps interior.gif]]
	}
	if { ![ info exist PostDisplay(IntNoImage)] } {
	    set PostDisplay(IntNoImage) [ image create photo -file [ file join $GIDDEFAULT bitmaps interior_no.gif]]
	}

	if { ![ info exist PostDisplay(TranspImage)] } {
	    set PostDisplay(TranspImage) [ image create photo -file [ file join $GIDDEFAULT bitmaps transparente.gif]]
	}
	if { ![ info exist PostDisplay(TranspNoImage)] } {
	    set PostDisplay(TranspNoImage) [ image create photo -file [ file join $GIDDEFAULT bitmaps opaco.gif]]
	}

	BotonPlano $w.but.but3.on On PDListOn [ CCColorSombra $def_back] [ CCColorActivo $def_back]
	$w.but.but3.on conf -image $PostDisplay(OnImage) -takefocus 0 -padx 4 -pady 2 -width 32 -height 32
	BotonPlano $w.but.but3.off Off PDListOff [ CCColorSombra $def_back] [ CCColorActivo $def_back]
	$w.but.but3.off conf -image $PostDisplay(OffImage) -takefocus 0 -padx 4 -pady 2 -width 32 -height 32
	
	BotonPlano $w.but.but3.interior "Draw interiors" "PDListPropiedad massive" [ CCColorSombra $def_back] [ CCColorActivo $def_back]
	$w.but.but3.interior conf -image $PostDisplay(IntImage) -takefocus 0 -padx 4 -pady 2 -width 32 -height 32
	BotonPlano $w.but.but3.hollow "Hollow bodies" "PDListPropiedadNo massive" [ CCColorSombra $def_back] [ CCColorActivo $def_back]
	$w.but.but3.hollow conf -image $PostDisplay(IntNoImage) -takefocus 0 -padx 4 -pady 2 -width 32 -height 32
	
	BotonPlano $w.but.but3.transp "Transparent" "PDListPropiedad transparent" [ CCColorSombra $def_back] [ CCColorActivo $def_back]
	$w.but.but3.transp conf -image $PostDisplay(TranspImage) -takefocus 0 -padx 4 -pady 2 -width 32 -height 32
	BotonPlano $w.but.but3.opaco "Opaque" "PDListPropiedadNo transparent" [ CCColorSombra $def_back] [ CCColorActivo $def_back]
	$w.but.but3.opaco conf -image $PostDisplay(TranspNoImage) -takefocus 0 -padx 4 -pady 2 -width 32 -height 32
	
	GiDPopUpHelp $w.but.but3.on "Turns on the selected sets."
	GiDPopUpHelp $w.but.but3.off "Turns off the selected sets."
	GiDPopUpHelp $w.but.but3.interior "Draws the interior elements and vectors of sets."
	GiDPopUpHelp $w.but.but3.hollow "Does not drawn interior vectors of the selected sets and elements of volume sets."
	GiDPopUpHelp $w.but.but3.transp "Turns the selected sets transparent."
	GiDPopUpHelp $w.but.but3.opaco "Turns the selected sets opaque."

	grid $w.but.but3.interior $w.but.but3.on $w.but.but3.transp $w.but.but3.cw $w.but.but3.bnombre \
		-padx 1 -ipadx 0 -pady 0 -sticky w
	grid conf $w.but.but3.bnombre -padx 5 -sticky e

	grid $w.but.but3.hollow $w.but.but3.off $w.but.but3.opaco -padx 1 -ipadx 0 -pady 0 -sticky w
	grid $w.but.but3.del -column 4 -row 1 -padx 5 -ipadx 0 -pady 0 -sticky e

	grid $w.but.but3 -sticky ew -padx 0 -ipadx 0 -pady 2 
	#grid columnconf $w.but.but3 4 -weight 1
	#grid rowconf $w.but.but3 0 -weight 1
	#grid rowconf $w.but.but3 1 -weight 1
    }

    grid columnconfigure $w.but 0 -weight 1

#    grid $w.but.close -sticky w -padx 5 -pady 3 -row 0 -column 2

    grid $w.list -sticky wens
    grid $w.but -sticky we

    grid columnconfigure $w 0 -weight 1
    grid rowconfigure $w 0 -weight 1

    FillPDListInfo

}











proc PostDisplay { { w .wPostDisplay } } {
    global GidPriv
    global GIDDEFAULT

    trace vdelete GidPriv(PostDisplayDS) w "PDAcceptStyle ;#"
    trace vdelete GidPriv(PostDisplayRender) w "PDAcceptStyle ;#"
    trace vdelete GidPriv(PostDisplayCulling) w "PDAcceptStyle ;#"
    trace vdelete GidPriv(PostDisplayConditions) w "PDAcceptStyle ;#"

    InitWindow $w [_ "Select & Display Style"] PostDisplayWindowGeom PostDisplay


    set GidPriv(PostDisplayWindow) $w
    set GidPriv(PostDisplayDoingAccept) 0

    frame $w.sel -relief ridge -bd 2
    frame $w.dis -relief ridge -bd 2

#    label $w.sel.l1 -text "Selection:" 

    PDCreateListWin $w.sel

#     button $w.sel.butlist -text "In a list" -command "PDCreateListWin" 

#     menubutton $w.sel.meshes -text "Meshes" -menu $w.sel.meshes.m -relief raised \
# 	    -width 7
#     if { [ catch { menu $w.sel.meshes.m -tearoff 0 -overlay 0 } ] != 0 } {
# 	menu $w.sel.meshes.m -tearoff 0
#     }
#     frame $w.sel.but1
#     button $w.sel.but1.b1 -text All -command "PDSetCheck $w.sel.meshes.m 1"
#     button $w.sel.but1.b2 -text None -command "PDSetCheck $w.sel.meshes.m 0"
#     pack $w.sel.but1.b1 $w.sel.but1.b2 -side top -fill x
    
#     menubutton $w.sel.sets -text "Sets" -menu $w.sel.sets.m -relief raised \
# 	    -width 7
#     if { [ catch { menu $w.sel.sets.m -tearoff 0 -overlay 0 } ] != 0 } {
# 	menu $w.sel.sets.m -tearoff 0 
#     }
    
#     frame $w.sel.but2
#     button $w.sel.but2.b1 -text All -command "PDSetCheck $w.sel.sets.m 1"
#     button $w.sel.but2.b2 -text None -command "PDSetCheck $w.sel.sets.m 0"
#     pack $w.sel.but2.b1 $w.sel.but2.b2 -side top -fill x
    
#     menubutton $w.sel.cuts -text "Cuts" -menu $w.sel.cuts.m -relief raised \
# 	    -width 7
#     if { [ catch { menu $w.sel.cuts.m -tearoff 0 -overlay 0 } ] != 0 } {
# 	menu $w.sel.cuts.m -tearoff 0 
#     }
    
#     frame $w.sel.but3
#     button $w.sel.but3.b1 -text All -command "PDSetCheck $w.sel.cuts.m 1"
#     button $w.sel.but3.b2 -text None -command "PDSetCheck $w.sel.cuts.m 0"
#     pack $w.sel.but3.b1 $w.sel.but3.b2 -side top -fill x

    frame $w.dis.fs

    label $w.dis.fs.l -text [_ "Style:"]
    
    regsub -all {[a-zA-Z_]+} [.central.s info post get all_display_styles] \
	    {{&}} disptypes
    regsub -all {_} $disptypes { } disptypes
    set menu [eval [ concat tk_optionMenu $w.dis.fs.om  GidPriv(PostDisplayDS) $disptypes]]

    $w.dis.fs.om conf -width 11 -bd 1 -highlightthickness 1
    $menu conf -bd 1 -activeborderwidth 1
    set i 0
    
    frame $w.dis.fr
    label $w.dis.fr.l2 -text [_ "Render:"]
    set menu [eval [ concat tk_optionMenu $w.dis.fr.om2 GidPriv(PostDisplayRender) \
	    [.central.s info post get all_display_renders]]]
    $w.dis.fr.om2 conf -width 6 -bd 1 -highlightthickness 1
    $menu conf -bd 1 -activeborderwidth 1

    button $w.dis.fr.b -bitmap @$GIDDEFAULT/bitmaps/changelight -bd 1 -command {
# 	set whatuse [lindex [.central.s info Project] 4]
# 	switch $whatuse {
# 	    GEOMETRYUSE {
# 		.central.s process escape escape escape escape utilities changelightvec
# 	    }
# 	    MESHUSE {
# 		.central.s process escape escape escape escape utilities changelightvec
# 	    }
# 	    POSTUSE {
		.central.s process escape escape escape escape utilities render changelightvec
# 	    }
# 	    default {
# 	    }
# 	}
    }

    frame $w.dis.fc
    label $w.dis.fc.l2 -text [_ "Culling"]:
    set menu [eval [ concat tk_optionMenu $w.dis.fc.om2 GidPriv(PostDisplayCulling) \
	    [.central.s info post get all_display_culling]]]
    $w.dis.fc.om2 conf -width 9 -bd 1 -highlightthickness 1
    $menu conf -bd 1 -activeborderwidth 1

    frame $w.dis.fcnd
    label $w.dis.fcnd.l2 -text [_ "Conditions"]:
    set menu [eval [ concat tk_optionMenu $w.dis.fcnd.om2 GidPriv(PostDisplayConditions) \
	    [.central.s info post get all_show_conditions]]]
    $w.dis.fcnd.om2 conf -width 8 -bd 1 -highlightthickness 1
    $menu conf -bd 1 -activeborderwidth 1

    frame $w.dis.fo
    checkbutton $w.dis.fo.cb -text [_ "Transparent"] \
	    -variable GidPriv(PostDisplayTransparent)

    checkbutton $w.dis.fo.cb2 -text [_ "Draw interiors"] \
	    -variable GidPriv(PostDisplayMassiveBodies)

    ###frame $w.dis.but -relief sunken -bd 1
    ###button $w.dis.but.accept -text A\np\n\p\nl\ny -command PDAcceptStyle -takefocus 0 -bd 1
    ###grid $w.dis.but.accept -sticky news

    set def_back [ $w cget -background]

    frame $w.but -bd 0 -background [ CCColorActivo $def_back]

    frame $w.but.dis -relief sunken -bd 0


    menubutton $w.but.back -text [_ "To back"] -menu $w.but.back.m  -relief raised -width 6
    GidHelp "$w.but.back " \
	[_ "Send entities to back/front of a Set (entities in back are not drawn)"]
    menu $w.but.back.m
    $w.but.back.m add check -label [_ "Opposite"] \
	-var GidPriv(LayersBackOpposite)
    $w.but.back.m add command -label [_ "Elements"] -command "PDSendToBack $w.but Elements"
    $w.but.back.m add command -label [_ "Bring to front"] -command "PDSendToBack $w.but ToFront"
    $w.but.back.m add separator
    $w.but.back.m add command -label [_ "Bring ALL to front"] -command "PDSendToBack $w.but AllToFront"

    $w.but.back conf -bd 1 -highlightthickness 1
    $w.but.back.m conf -bd 1 -activeborderwidth 1

    menubutton $w.but.send -text [_ "Send to"] -menu $w.but.send.m  -relief raised -width 6
    GidHelp "$w.but.send " [_ "Send entities to another set"]
    menu $w.but.send.m
    $w.but.send.m add command -label [_ "Elements"] -command "PDSendToBack $w.but ToSelected"
    $w.but.send.m add separator
    $w.but.send.m add command -label [_ "New set long name"] -command "PDSendToBack $w.but ToNewSetLong"
    $w.but.send.m add command -label [_ "New set short name"] -command "PDSendToBack $w.but ToNewSetShort"

    $w.but.send conf -bd 1 -highlightthickness 1
    $w.but.send.m conf -bd 1 -activeborderwidth 1


    #button $w.but.accept -text Apply -command PDAcceptStyle -bd 1 -width 6
    button $w.but.close -text [_ "Close"] -command "destroy $w" -bd 1 -width 6


    #grid $w.but.back $w.but.send $w.but.accept $w.but.close -sticky ew -padx 5 -pady 6
    grid $w.but.back $w.but.send $w.but.close -sticky ew -padx 5 -pady 6

#     grid $w.sel.l1 $w.sel.butlist 

#     grid $w.sel.meshes $w.sel.but1 -sticky e -pady 5 -padx 5
#     grid $w.sel.sets $w.sel.but2 -sticky e -pady 5 -padx 5
#     grid $w.sel.cuts $w.sel.but3 -sticky e -pady 5 -padx 5

    grid columnconf $w 0 -weight 1

    grid $w.dis.fs.l $w.dis.fs.om -pady 1 -padx 1
    grid $w.dis.fr.l2 $w.dis.fr.om2 $w.dis.fr.b -pady 1 -padx 1
    grid $w.dis.fc.l2 $w.dis.fc.om2 -pady 1 -padx 1
    grid $w.dis.fcnd.l2 $w.dis.fcnd.om2 -pady 1 -padx 1
    grid $w.dis.fo.cb2 $w.dis.fo.cb -pady 1 -padx 1
    ###grid $w.dis.but -pady 1 -padx 1 -row 0 -column 2 -rowspan 4 -sticky e
#     grid $w.dis.cb -columnspan 2
#     grid $w.dis.cb2 -columnspan 2


    grid $w.dis.fs -pady 0 -row 0 -column 0
    grid $w.dis.fr -pady 0 -row 1 -column 0
    grid $w.dis.fc -pady 0 -row 2 -column 0
    grid $w.dis.fcnd -pady 0 -row 3 -column 0
#######    grid $w.dis.fo -pady 0 -row 4 -column 0

    grid $w.sel -sticky wens
    grid $w.dis -sticky wens
    grid columnconfigure $w 0 -weight 1
    grid rowconfigure $w 0 -weight 1
#    grid rowconfigure $w 1 -weight 1
    grid $w.but -sticky wes

    FillPDInfo
    
    #trace variable GidPriv(PostDisplayRender) w PDChangeRenderToWogl
    #trace variable GidPriv(PostDisplayCulling) w PDChangeCullingToWogl
    ##trace variable GidPriv(PostDisplayTransparent) w PDChangeTransparentToWogl

    trace variable GidPriv(PostDisplayDS) w "PDAcceptStyle ;#"
    trace variable GidPriv(PostDisplayRender) w "PDAcceptStyle ;#"
    trace variable GidPriv(PostDisplayCulling) w "PDAcceptStyle ;#"
    trace variable GidPriv(PostDisplayConditions) w "PDAcceptStyle ;#"
}



#----------------------SetCoord ALONE---------------------------
# if { $PDRunningAlone == 1 } {
#     PostDisplay .wPostDS
# } 
#-----------------------------------------------------------------

proc PDSendToBack { frame what } {

    set w [winfo toplevel $frame]

    set tbl $::GidPriv(PostDisplayWindowList).list.f2.tbl
    if { $what == "ToSelected" } {
	if { [ $tbl size] == 1} {
	    $tbl selection set 0
	} 
	set setsel [$tbl curselection]
	if { [llength $setsel] != 1 } {
	    WarnWin [_ "One set should be selected"]
	    return
	}
	regsub -all { } [lindex [$tbl get [lindex $setsel 0]] 1] {_} curr
	.central.s process SelectEntities Elements
	set comm "[list PDSendToSet $curr] ;#"
	trace var ::GidPriv(selection) w $comm
	set SmallWinSelecting [.central.s info variable SmallWinSelecting]
	FinishButton $w $frame \
	    [_ "Press 'Finish' to end selection"] \
	    "" disableall $SmallWinSelecting
    } elseif { $what == "ToNewSetLong" || $what == "ToNewSetShort" } {
	.central.s process SelectEntities Elements
	set comm "[list PDSendToSet "" $what] ;#"
	trace var ::GidPriv(selection) w $comm
	set SmallWinSelecting [.central.s info variable SmallWinSelecting]
	FinishButton $w $frame \
	    [_ "Press 'Finish' to end selection"] \
	    "" disableall $SmallWinSelecting
    } elseif { $what == "Elements" } {
	.central.s process SelectEntities Elements
	trace var ::GidPriv(selection) w "PDSendToBackEnd;#"
	set SmallWinSelecting [.central.s info variable SmallWinSelecting]
	FinishButton $w $frame \
	    [_ "Press 'Finish' to end selection"] \
	    "" disableall $SmallWinSelecting
    } elseif { $what == "ToFront" } {

	set setsel [$tbl curselection]
	if { $setsel == "" } {
	    WarnWin [_ "One or more sets should be selected"]
	    return
	}
	set selectedsets ""
	foreach idx $setsel {
	    regsub -all { } [lindex [$tbl get $idx] 1] {_} curr
	    lappend selectedsets $curr
	}
	PdSendToFront $selectedsets
    } else {
	PdSendToFront
    }
}

proc PdSendToFront { { selectedsets "" } } {

    if { !$::PostDisplay(debug) } {
	set DisableWarnLine [.central.s disable warnline]
	.central.s disable warnline 1
	set DisableGraphics [.central.s disable graphics]
	.central.s disable graphics 1
    }
    foreach j "volumesets surfacesets cutsets" {
	set currents [.central.s info post get cur_$j]
	if { $currents != "" } {
	    .central.s process escape escape escape escape select $j no_$j escape
	}
	foreach i [.central.s info post get all_$j] {
	    set ini ""
	    set end ""
	    if { [regexp {^([^_]*)[^(]*[(](.+)[)]} $i {} ini end] } {
		set oldset ${ini}_$end
		if { [regexp {[*]{5}} $i] && ($selectedsets == "" || \
						  [lsearch $selectedsets $oldset] != -1) } {
		    .central.s process escape escape escape escape select $j $i escape
		    .central.s process escape escape escape escape select Elements
		    .central.s process 1:
		    .central.s process escape MoveToOldSet $oldset
		    .central.s process escape escape escape escape Utilities Delete $j $i yes
		}
	    }
	}
	if { $currents != "" } {
	    .central.s process select $j
	    foreach i $currents {
		.central.s process $i
	    }
	    .central.s process escape
	}
    }
    if { !$::PostDisplay(debug) } {
	if { !$DisableWarnLine } { .central.s disable warnline 0 }
	if { !$DisableGraphics } { .central.s disable graphics 0 }
    }
    .central.s process redraw
}

proc PDSendToBackEnd {} {

    trace vdelete ::GidPriv(selection) w "PDSendToBackEnd;#"
    set DisableWarnLine 0
    set DisableGraphics 0

    # it is supposed that word: Elements is at the beginning
    if { [string length $::GidPriv(selection)] <= 10 } {
	return
    }

    if { !$::PostDisplay(debug) } {
	set DisableWarnLine [.central.s disable warnline]
	.central.s disable warnline 1
	set DisableGraphics [.central.s disable graphics]
	.central.s disable graphics 1
    }
    .central.s process escape escape escape escape Select Elements 
    if { $::GidPriv(LayersBackOpposite) } {
	.central.s process 1:
	.central.s process SwapSelection
    }
    .central.s process [string range $::GidPriv(selection) 10 end-1]
    .central.s process escape MoveToNewSet ***** escape escape escape

    after 0 PDSendToBackEnd2 $DisableWarnLine $DisableGraphics

}
proc PDSendToBackEnd2 { DisableWarnLine DisableGraphics } {

    foreach j "volumesets surfacesets cutsets" {
	.central.s process escape escape escape escape select $j
	set currents [.central.s info post get cur_$j]
	foreach i [.central.s info post get all_$j] {
	    if { [regexp {[*]{5}} $i] && [lsearch $currents $i] != -1 } {
		.central.s process $i
	    }
	}
	.central.s process escape
    }
    if { !$::PostDisplay(debug) } {
	if { !$DisableWarnLine } { .central.s disable warnline 0 }
	if { !$DisableGraphics } { .central.s disable graphics 0 }
    }
    .central.s process redraw
}

proc PDSendToSet { SetName { what "" } } {

    set comm "[list PDSendToSet $SetName $what] ;#"
    trace vdelete ::GidPriv(selection) w $comm

    # it is supposed that word: Elements is at the beginning
    if { [string length $::GidPriv(selection)] <= 10 } {
	return
    }

    set DisableWarnLine 0
    set DisableGraphics 0
    if { !$::PostDisplay(debug) } {
	set DisableWarnLine [.central.s disable warnline]
	.central.s disable warnline 1
	set DisableGraphics [.central.s disable graphics]
	.central.s disable graphics 1
    }

    .central.s process escape escape escape escape Select Elements 
    .central.s process [string range $::GidPriv(selection) 10 end-1]
    if { $SetName != "" } {
	.central.s process escape MoveToOldSet $SetName escape escape escape
    } else {
	set newname [tk_dialogEntryRAM .gid.pp [_ "Enter set name"] [_ "Enter new set name"] gidquestionhead \
			 wordtype ""]
	if { !$::PostDisplay(debug) } {
	    if { !$DisableWarnLine } { .central.s disable warnline 0 }
	    if { !$DisableGraphics } { .central.s disable graphics 0 }
	}
	if { $newname == "--CANCEL--" } { return }
	.central.s process escape MoveToNewSet $newname escape
	if { $what == "ToNewSetShort" } {
	    after 0 PDSendToSet2 $newname $DisableWarnLine $DisableGraphics
	    return
	}
    }
    if { !$::PostDisplay(debug) } {
	if { !$DisableWarnLine } { .central.s disable warnline 0 }
	if { !$DisableGraphics } { .central.s disable graphics 0 }
    }
}

proc PDSendToSet2 { oldname DisableWarnLine DisableGraphics } {

    foreach j "volumesets surfacesets cutsets" {
	.central.s process escape escape escape escape select $j
	foreach i [.central.s info post get all_$j] {
	    if { ![string match *$oldname* $i] } { continue }
	    if { [regexp {^([^_]+)_([^(]*)_([0-9]+)_[(](.+)[)]$} $i {} type name num parent] } {
		set newname ${type}_${name}_$num
	    .central.s process escape escape escape escape Utilities ChangeName $j $i \
		    $newname escape
	    }
	}
    }
    if { !$::PostDisplay(debug) } {
	if { !$DisableWarnLine } { .central.s disable warnline 0 }
	if { !$DisableGraphics } { .central.s disable graphics 0 }
    }
    .central.s process redraw
}

set PostDisplay(debug) 0
