proc CustomizeRender { } {    
    global GIDDEFAULT GidPriv 
    global CustomizeRenderPriv 

    catch { unset CustomizeRenderPriv }
    set w .gid.customizerender

    InitWindow $w [_ "Customize Render"] CustomizeRenderWindowGeom
  
    frame $w.frm1
    ComboBox $w.frm1.cb1 -editable 0 -textvariable CustomizeRenderPriv(CustomRenderCurrent) \
	    -values [.central.s info customrenders]
    GidHelp $w.frm1.cb1 [_ "It set the current customized render mode"]
    button $w.frm1.b1 -image [imagefrom check.gif] -command "NewCustomRender $w.frm1.cb1"
    GidHelp $w.frm1.b1 [_ "It creates a new customized render mode"]
    button $w.frm1.b2 -image [imagefrom rename2.gif] -command "RenameCustomRender $w.frm1.cb1"
    GidHelp $w.frm1.b2 [_ "It changes the customized render name"]
    button $w.frm1.b3 -image [imagefrom erase-cross.gif] -command "DeleteCustomRender $w.frm1.cb1"
    GidHelp $w.frm1.b3 [_ "It deletes the selected customized render mode."]

    frame $w.frm2
    label $w.frm2.l01 -text [_ "Show"] 
    label $w.frm2.l02 -text [_ "Color by"]
    label $w.frm2.l03 -text [_ "Size"]
    label $w.frm2.l04 -text [_ "Render"]

    foreach i {point line coonsurf volume} {
	if { ![info exist GidPriv(image_${i}16)] } {
	    set GidPriv(image_${i}16) [ image create photo -file $GIDDEFAULT/bitmaps/${i}16x16.gif]
	}
    }
    label $w.frm2.l10 -image $GidPriv(image_point16) -width 5
    label $w.frm2.l20 -image $GidPriv(image_line16)
    label $w.frm2.l30 -image $GidPriv(image_coonsurf16)
    label $w.frm2.l40 -image $GidPriv(image_volume16)

    checkbutton $w.frm2.ck1 -variable CustomizeRenderPriv(Show,Point) -width 3	
    checkbutton $w.frm2.ck2 -variable CustomizeRenderPriv(Show,Line)	
    checkbutton $w.frm2.ck3 -variable CustomizeRenderPriv(Show,Surface)
    checkbutton $w.frm2.ck4 -variable CustomizeRenderPriv(Show,Volume)		

    ComboBox $w.frm2.cb1 -editable 0 -modifycmd "ModifyComboBoxCommand $w.frm2.cb1 Point" \
	    -values [list [_ "Layer"] [_ "EntityType"]]
    ComboBox $w.frm2.cb2 -editable 0 -modifycmd "ModifyComboBoxCommand $w.frm2.cb2 Line" \
	    -values [list [_ "Layer"] [_ "EntityType"] [_ "Black"]]
    ComboBox $w.frm2.cb3 -editable 0 -modifycmd "ModifyComboBoxCommand $w.frm2.cb3 Surface" \
	    -values [list [_ "Layer"] [_ "EntityType"]]
    ComboBox $w.frm2.cb4 -editable 0 -modifycmd "ModifyComboBoxCommand $w.frm2.cb2 Volume" \
	    -values [list [_ "Layer"] [_ "EntityType"]]
    trace var CustomizeRenderPriv(ColorBy,Point) w "SetComboBoxTextColorBy $w.frm2.cb1 Point ;#"
    trace var CustomizeRenderPriv(ColorBy,Line) w "SetComboBoxTextColorBy $w.frm2.cb2 Line ;#"
    trace var CustomizeRenderPriv(ColorBy,Surface) w "SetComboBoxTextColorBy $w.frm2.cb3 Surface ;#"
    trace var CustomizeRenderPriv(ColorBy,Volume) w "SetComboBoxTextColorBy $w.frm2.cb4 Volume ;#"

    #valid for OpenGL specification of point size and line width  -range "0.5 10 0.5"
    SpinBox $w.frm2.sb1 -editable 1 -textvariable CustomizeRenderPriv(Size,Point) \
	    -values "1 2 3 4 5 6 7 8 9 10" -width 5
    SpinBox $w.frm2.sb2 -editable 1 -textvariable CustomizeRenderPriv(Size,Line)\
	    -values "1 2 3 4 5 6 7 8 9 10" -width 5
    SpinBox $w.frm2.sb3 -editable 1 -textvariable CustomizeRenderPriv(Size,Surface) \
	    -values "1 2 3 4 5 6 7 8 9 10" -width 5
    SpinBox $w.frm2.sb4 -editable 1 -textvariable CustomizeRenderPriv(Size,Volume) \
	    -values "1 2 3 4 5 6 7 8 9 10" -width 5

    frame $w.frm3
    checkbutton $w.frm3.ck1 -variable CustomizeRenderPriv(FillSurface) -text [_ "Fill Surfaces"]
    checkbutton $w.frm3.ck2 -variable CustomizeRenderPriv(Lighting) -text [_ "Lighting"]
    checkbutton $w.frm3.ck3 -variable CustomizeRenderPriv(Blend) -text [_ "Blend"]   
    
    set def_back [$w cget -background]
    frame $w.frmButtons -bg [CCColorActivo $def_back]
    button $w.frmButtons.btnApply -text [_ "Apply"] \
	    -command "SetTclValues" \
	    -underline 0 -width 6
    button $w.frmButtons.btnReset -text [_ "Reset"] \
	    -command "SetDefaultValues" \
	    -underline 0 -width 6
    button $w.frmButtons.btnclose -text [_ "Close"] \
	    -command "catch { unset CustomizeRenderPriv } ; destroy $w" \
	    -underline 0 -width 6
    #empaquetamiento de los widgets
   
    grid $w.frm1.cb1 $w.frm1.b1 $w.frm1.b2 $w.frm1.b3 -sticky w
    grid configure $w.frm1.cb1 -sticky ew
    grid columnconf $w.frm1 "0" -weight 1 
    grid $w.frm1 -padx 5 -pady 5 -sticky ew

    grid      x     $w.frm2.l01 $w.frm2.l02 $w.frm2.l03 -sticky ew
    grid $w.frm2.l10 $w.frm2.ck1 $w.frm2.cb1 $w.frm2.sb1 -sticky ew
    grid $w.frm2.l20 $w.frm2.ck2 $w.frm2.cb2 $w.frm2.sb2 -sticky ew
    grid $w.frm2.l30 $w.frm2.ck3 $w.frm2.cb3 $w.frm2.sb3 -sticky ew
    grid $w.frm2.l40 $w.frm2.ck4 $w.frm2.cb4 $w.frm2.sb4 -sticky ew
    grid columnconf $w.frm2 "2" -weight 1   
    grid $w.frm2 -padx 5 -pady 5 -sticky ew
    
    grid $w.frm3.ck1 $w.frm3.ck2 $w.frm3.ck3 -sticky ew
    grid $w.frm3 -padx 5 -pady 5 -sticky ew

    grid $w.frmButtons -sticky ews -columnspan 7
    grid $w.frmButtons.btnApply $w.frmButtons.btnReset $w.frmButtons.btnclose -padx 5 -pady 6
    grid columnconf $w "0" -weight 1
    grid rowconfigure $w "1" -weight 1
    
    focus $w.frmButtons.btnApply
    
    bind $w <Alt-c> "tkButtonInvoke $w.frmButtons.btnclose"
    bind $w <Escape> "tkButtonInvoke $w.frmButtons.btnclose"
    bind $w <Return> "tkButtonInvoke $w.frmButtons.btnApply"

    wm minsize $w 140 175

    
    SetGidValues

    set rendertype [lindex [.central.s info Project] 6]
    if { $rendertype == "custom" } {
	set CustomizeRenderPriv(CustomRenderCurrent) [.central.s info customrenders current]
    } else {
	set CustomizeRenderPriv(CustomRenderCurrent) ""
    }
    
    trace vdelete CustomizeRenderPriv(CustomRenderCurrent) w "SetCustomRenderCurrent ;#"
    trace var CustomizeRenderPriv(CustomRenderCurrent) w "SetCustomRenderCurrent ;#"

    trace var CustomizeRenderPriv(Show,Point) w "UpdateState $w Point ;#"
    trace var CustomizeRenderPriv(Show,Line) w "UpdateState $w Line ;#"
    trace var CustomizeRenderPriv(Show,Surface) w "UpdateState $w Surface ;#"
    trace var CustomizeRenderPriv(Show,Volume) w "UpdateState $w Volume ;#"
}

proc SetCustomRenderCurrent { } {
    global CustomizeRenderPriv
    if { $CustomizeRenderPriv(CustomRenderCurrent) == "" } return
    .central.s process escape escape escape escape view render customized \
		changecustom $CustomizeRenderPriv(CustomRenderCurrent)
    .central.s process escape escape escape view redraw escape
    SetGidValues
}

proc NewCustomRender { w } {
    global CustomizeRenderPriv

    set Ret [tk_dialogEntryRAM .gid.tempwinw [_ "New customized render mode"] \
		 [_ "Enter new render mode name"]  gidquestionhead word "" ]
    if { $Ret == "--CANCEL--" ||  $Ret == "" } { return  }
    regsub -all {[ ]+} $Ret {_} Ret

    set customrenders [.central.s info customrenders]
    foreach i $customrenders {
	if { $i == $Ret } {
	    WarnWin [_ "Custom render '%s' already exists" $Ret ]  
	    return
	}
    }

    .central.s process escape escape escape escape View Render Customized \
	    NewCustom $Ret
    
    $w configure -values [.central.s info customrenders]

    set CustomizeRenderPriv(CustomRenderCurrent) $Ret
}

proc RenameCustomRender { w } {
    global CustomizeRenderPriv
    set oldname [$w cget -text]
    if { $oldname == "" } return
    set Ret [tk_dialogEntryRAM .gid.tempwinw [_ "Rename customized render" $] \
		 [_ "Enter new name for rende mode"]  gidquestionhead word $oldname ]
    if { $Ret == "--CANCEL--" ||  $Ret == "" } { return  }
    regsub -all {[ ]+} $Ret {_} Ret

    if { $Ret == $oldname } return

    set customrenders [.central.s info customrenders]
    foreach i $customrenders {
	if { $i == $Ret } {
	    WarnWin [_ "Custom render '%s' already exists" $Ret ]  
	    return
	}
    }

    .central.s process escape escape escape escape View Render Customized \
	    RenameCustom $oldname $Ret
    
    $w configure -values [.central.s info customrenders]

    set CustomizeRenderPriv(CustomRenderCurrent) $Ret
}

proc DeleteCustomRender { w } {
    global CustomizeRenderPriv
    if { $CustomizeRenderPriv(CustomRenderCurrent) == "" } return

    set retval [tk_dialogRAMFull .gid.tempwinw [_ "Delete customized render mode"] \
	    [_ "Are you sure to delete\n custom render '%s'?" $CustomizeRenderPriv(CustomRenderCurrent)] \
	     "" "" gidquestionhead 0 [_ "Yes"] [_ "No"]]
    if { $retval == 1 } { return }

    .central.s process escape escape escape escape View Render Customized \
	    DeleteCustom $CustomizeRenderPriv(CustomRenderCurrent)

    set customrenders [.central.s info customrenders]
    $w configure -values $customrenders
    
    set CustomizeRenderPriv(CustomRenderCurrent) [lindex $customrenders 0]
    if { $CustomizeRenderPriv(CustomRenderCurrent) == "" } {
	.central.s process escape escape escape view redraw escape
    }
}

proc UpdateState { w EntityType } {
    global CustomizeRenderPriv
    if { $EntityType == "Point" } {
	if { $CustomizeRenderPriv(Show,Point) } {
	    $w.frm2.cb1 configure -state normal
	    $w.frm2.sb1 configure -state normal
	} else {
	    $w.frm2.cb1 configure -state disabled
	    $w.frm2.sb1 configure -state disabled
	}
    } elseif { $EntityType == "Line" } {
	if { $CustomizeRenderPriv(Show,Line) } {
	    $w.frm2.cb2 configure -state normal
	    $w.frm2.sb2 configure -state normal
	} else {
	    $w.frm2.cb2 configure -state disabled
	    $w.frm2.sb2 configure -state disabled
	}
    } elseif { $EntityType == "Surface" } {
	if { $CustomizeRenderPriv(Show,Surface) } {
	    $w.frm2.cb3 configure -state normal
	    $w.frm2.sb3 configure -state normal
	    $w.frm3.ck1 configure -state normal
	} else {
	    $w.frm2.cb3 configure -state disabled
	    $w.frm2.sb3 configure -state disabled
	    $w.frm3.ck1 configure -state disabled
	}
    } elseif { $EntityType == "Volume" } {
	if { $CustomizeRenderPriv(Show,Volume) } {
	    $w.frm2.cb4 configure -state normal
	    $w.frm2.sb4 configure -state normal
	} else {
	    $w.frm2.cb4 configure -state disabled
	    $w.frm2.sb4 configure -state disabled
	}
    }
}

proc ModifyComboBoxCommand { w entitytype } {
    global CustomizeRenderPriv 
    set CustomizeRenderPriv(ColorBy,$entitytype) [$w getvalue]
}

proc SetComboBoxTextColorBy { w entitytype } {
    global CustomizeRenderPriv
    if {  $CustomizeRenderPriv(ColorBy,$entitytype) == "0" } {
	$w configure -text [_ "Layer"]
    } elseif {  $CustomizeRenderPriv(ColorBy,$entitytype) == "1" } {
	$w configure -text [_ "EntityType"]
    } else {
	$w configure -text [_ "Black"]
    }
}

proc SetDefaultValues { } {
    global CustomizeRenderPriv 
    set CustomizeRenderPriv(Show,Point)	1
    set CustomizeRenderPriv(Show,Line) 1
    set CustomizeRenderPriv(Show,Surface) 1
    set CustomizeRenderPriv(Show,Volume) 1	
    #0 color by layer	
    #1 color by entity type
    set CustomizeRenderPriv(ColorBy,Point) 1
    set CustomizeRenderPriv(ColorBy,Line) 0
    set CustomizeRenderPriv(ColorBy,Surface) 1
    set CustomizeRenderPriv(ColorBy,Volume) 1

    set CustomizeRenderPriv(Size,Point) 3
    set CustomizeRenderPriv(Size,Line) 1
    set CustomizeRenderPriv(Size,Surface) 1
    set CustomizeRenderPriv(Size,Volume) 1

    set CustomizeRenderPriv(FillSurface) 0
    set CustomizeRenderPriv(Lighting) 0
    set CustomizeRenderPriv(Blend) 0
}

proc SetGidValues { } {
    global CustomizeRenderPriv 
    if { [.central.s info customrenders current] == "" } return

    set CustomizeRenderPriv(Show,Point) [.central.s info variables CustomRender(Show,Point)]
    set CustomizeRenderPriv(Show,Line) [.central.s info variables CustomRender(Show,Line)]
    set CustomizeRenderPriv(Show,Surface) [.central.s info variables CustomRender(Show,Surface)]
    set CustomizeRenderPriv(Show,Volume) [.central.s info variables CustomRender(Show,Volume)]	
    #0 color by layer	
    #1 color by entity type
    set CustomizeRenderPriv(ColorBy,Point) [.central.s info variables CustomRender(ColorBy,Point)]
    set CustomizeRenderPriv(ColorBy,Line) [.central.s info variables CustomRender(ColorBy,Line)]
    set CustomizeRenderPriv(ColorBy,Surface) [.central.s info variables CustomRender(ColorBy,Surface)]
    set CustomizeRenderPriv(ColorBy,Volume) [.central.s info variables CustomRender(ColorBy,Volume)]
    
    set CustomizeRenderPriv(Size,Point) [expr int([.central.s info variables CustomRender(Size,Point)])]
    set CustomizeRenderPriv(Size,Line) [expr int([.central.s info variables CustomRender(Size,Line)])]
    set CustomizeRenderPriv(Size,Surface) [expr int([.central.s info variables CustomRender(Size,Surface)])]
    set CustomizeRenderPriv(Size,Volume) [expr int([.central.s info variables CustomRender(Size,Volume)])]
    
    set CustomizeRenderPriv(FillSurface) [.central.s info variables CustomRender(FillSurface)]
    set CustomizeRenderPriv(Lighting) [.central.s info variables CustomRender(Lighting)]
    set CustomizeRenderPriv(Blend) [.central.s info variables CustomRender(Blend)]
}

proc SetTclValues { } {
    global CustomizeRenderPriv 
    if { [.central.s info customrenders current] == "" } return
    set oldwarnlinestate [::GidUtils::DisableWarnLine]
    .central.s process escape escape escape escape utilities variables \
	    CustomRender(Show,Point) $CustomizeRenderPriv(Show,Point)
    .central.s process escape utilities variables \
	    CustomRender(Show,Line) $CustomizeRenderPriv(Show,Line)
    .central.s process escape utilities variables \
	    CustomRender(Show,Surface) $CustomizeRenderPriv(Show,Surface)
    .central.s process escape utilities variables \
	    CustomRender(Show,Volume) $CustomizeRenderPriv(Show,Volume)
    .central.s process escape utilities variables \
	    CustomRender(ColorBy,Point) $CustomizeRenderPriv(ColorBy,Point)
    .central.s process escape utilities variables \
	    CustomRender(ColorBy,Line) $CustomizeRenderPriv(ColorBy,Line)
    .central.s process escape utilities variables \
	    CustomRender(ColorBy,Surface) $CustomizeRenderPriv(ColorBy,Surface)
    .central.s process escape utilities variables \
	    CustomRender(ColorBy,Volume) $CustomizeRenderPriv(ColorBy,Volume)
    .central.s process escape utilities variables \
	    CustomRender(Size,Point) $CustomizeRenderPriv(Size,Point)
    .central.s process escape utilities variables \
	    CustomRender(Size,Line) $CustomizeRenderPriv(Size,Line)
    .central.s process escape utilities variables \
	    CustomRender(Size,Surface) $CustomizeRenderPriv(Size,Surface)
    .central.s process escape utilities variables \
	    CustomRender(Size,Volume) $CustomizeRenderPriv(Size,Volume)
    .central.s process escape utilities variables \
	    CustomRender(FillSurface) $CustomizeRenderPriv(FillSurface) 

    if { [.central.s info variables CustomRender(Lighting)] != $CustomizeRenderPriv(Lighting) } {
	.central.s process escape utilities variables \
		CustomRender(Lighting) $CustomizeRenderPriv(Lighting)
	.central.s process escape escape escape view render \
		customized changecustom [.central.s info customrenders current] escape
    }
    .central.s process escape utilities variables \
	    CustomRender(Blend) $CustomizeRenderPriv(Blend)

    .central.s process escape escape escape view redraw escape

    if { $oldwarnlinestate != 1 } { ::GidUtils::EnableWarnLine }
}
