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


#----------------------PostResults ALONE---------------------------
# the next line restarts using wish \
	exec wish8.3 "$0" "$@"

set PRRunningAlone 0
# if { ![info exists GIDDEFAULT] } {
#     set PRRunningAlone 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 FillPVariosResultsInfo {} {
    global PVariosResultsPriv GidPriv

    if { ![info exists GidPriv(PostVariosResultadosWindow)] || \
	    ![winfo exists $GidPriv(PostVariosResultadosWindow)] } { return }


    set w $GidPriv(PostVariosResultadosWindow)

    set PVariosResultsPriv(varios_resultados) [ string tolower [ .central.s info variables postresoverres]]
    #WarnWinText "varios_resultados = $PVariosResultsPriv(varios_resultados)"
    set res_lst [ .central.s info post get results_view_list]
    if { [ llength $res_lst] != 0} {
	$w.ll.list del 0 end
	foreach res $res_lst {
	    #WarnWinText "[ llength $res] -$res-"
	    set lst_ins ""
	    foreach res_txt $res {
		if { "$res_txt" != ""} {
		    if { "$lst_ins" != ""} {
			set lst_ins "$lst_ins * $res_txt"
		    } else {
			set lst_ins $res_txt
		    }
		}
	    }
	    $w.ll.list ins end $lst_ins
	}
	#WarnWinText "-----------------"
    }
}

proc ChangeEstadoVariosResultados { name1 name2 op} {
    global PVariosResultsPriv GidPriv

#    if { "[ string tolower $PVariosResultsPriv(varios_resultados)]" == "yes"} {
#	 set w $GidPriv(PostVariosResultadosWindow)
#	 $w.f0.l2 conf -text "one over another"
#    }
#    if { "[ string tolower $PVariosResultsPriv(varios_resultados)]" == "no"} {
#	 set w $GidPriv(PostVariosResultadosWindow)
#	 $w.f0.l2 conf -text "one by one"
#    }
}

proc PostVariosResultadosDelete { w} {
    global PVariosResultsPriv
    
    if { [ $w.ll.list size] == 1} {
	$w.ll.list selection set 0
    }

    set haysel [ catch { set setsel [ $w.ll.list curselection]}]

    if { ( $haysel != 0) || ( $setsel == {} )} {
	tk_dialogRAM $w.tmpwin [_ "error"] [_ "Sorry, no result selected."] error 0 [_ "OK"]
	return error
    } else {
	set del_lst ""
	foreach idx $setsel {
	    if { [ regexp {^([^\*]*) \*.*} [ $w.ll.list get $idx] dum id_res]} {
		lappend del_lst $id_res
	    }
	}
	foreach res $del_lst {
	    regsub -all { } $res {_} id
	    .central.s process escape escape escape escape results options DelResOverRes $id
	}
	.central.s process escape escape escape escape
    }

}

proc PostVariosResultadosAccept {} {
    global PVariosResultsPriv

    .central.s process escape escape escape escape utilities variables postresoverres $PVariosResultsPriv(varios_resultados)
    .central.s process escape escape escape escape Results Geometry NoResults Geometry Original
}

proc PostVariosResultadosClose { w} {
    global PVariosResultsPriv
    #catch {
    #	 trace vdelete PVariosResultsPriv(varios_resultados) w ChangeEstadoVariosResultados
    #}

    destroy $w
}

proc PostVariosResultados { { w .wPostVariosResults} } {
    global PVariosResultsPriv GidPriv

    InitWindow $w [_ "Several Results"] PostVariosResultadosWindowGeom PostVariosResultados

    #set GidPriv(PostVariosResultadosWindow) $w

    #catch {
    #	 trace vdelete PVariosResultsPriv(varios_resultados) w ChangeEstadoVariosResultados
    #}

    frame $w.f0

    label $w.f0.l1 -text [_ "Results view:"]
    radiobutton $w.f0.rd1 -text [_ "one by one"] \
	    -value no -variable PVariosResultsPriv(varios_resultados) \
	    -command "PostVariosResultadosAccept"
    radiobutton $w.f0.rd2 -text [_ "one over another"] \
	    -value yes -variable PVariosResultsPriv(varios_resultados) \
	    -command "PostVariosResultadosAccept"

    grid $w.f0.l1 -sticky w
    grid $w.f0.rd1 $w.f0.rd2 -sticky we

    frame $w.f1
    label $w.f1.l1 -text [_ "Current list of results:"]
    button $w.f1.del -text [_ "Delete"] -bd 1 -command "PostVariosResultadosDelete $w"
    grid $w.f1.l1 -sticky w
    grid $w.f1.del -row 0 -column 1 -sticky w
    grid columnconfigure $w.f1 0 -weight 1
    grid columnconfigure $w.f1 1 -weight 1
    grid rowconfigure $w.f1 0 -weight 1

    frame $w.ll
    scrollbar $w.ll.xscroll -relief sunken -orient horizontal -command "$w.ll.list xview" \
	    -bd 1 -elementborderwidth 1
    listbox $w.ll.list -height 4 -xscroll "$w.ll.xscroll set" -relief sunken \
	    -selectmode browse -bd 1 -font SmallFont
    #bind $w.ll.list <ButtonRelease-1> PRListboxToEntry
    scrollbar $w.ll.yscroll -relief sunken -command "$w.ll.list yview" -bd 1 \
	    -elementborderwidth 1
    $w.ll.list conf -yscroll "$w.ll.yscroll set"
    pack $w.ll.yscroll -side right -fill y
    pack $w.ll.xscroll -side bottom -fill x
    pack $w.ll.list -fill both -side top -expand yes


    set def_back [ $w cget -background]
    frame $w.but -bd 0 -background [ CCColorActivo $def_back]
    #button $w.but.accept -text [_ "Apply"] -bd 1 -command "PostVariosResultadosAccept"
    #button $w.but.original -text [_ "Original"] -bd 1 -command ""
    button $w.but.close -text [_ "Close"] -bd 1 -command "PostVariosResultadosClose $w"

    #grid $w.but.accept -row 0 -column 0 -padx 4 -pady 6 -sticky ew
    #grid $w.but.original -row 0 -column 1 -padx 4 -pady 10 -sticky ew
    #grid $w.but.close -row 0 -column 2 -padx 4 -pady 6 -sticky ew
    grid $w.but.close -row 0 -column 0 -padx 4 -pady 6 -sticky ew

    grid $w.f0 -sticky new -padx 3 -pady 3
    grid $w.f1 -sticky new -padx 3 -pady 3
    grid $w.ll -sticky news -padx 3 -pady 3
    grid $w.but -sticky sew
    grid columnconfigure $w 0 -weight 1
    grid rowconfigure $w 2 -weight 1

    #trace variable PVariosResultsPriv(varios_resultados) w ChangeEstadoVariosResultados
    FillPVariosResultsInfo
}

