#************************************************************
# ipdlg.tcl
#
# IP listbox
#
# Author : Dan Gunter
# Created: 1/13/98
# Credits: Brent Welch, "Practical Programming in Tcl/Tk",
#          (c)1995 Prentice-Hall
#************************************************************

#########################
# IPSelect --
#
#    Widget to select IPs.
#
proc IPSelect { parent } {
    global param
    set bwid 10
    set bbg $param(color-buttonbg)
    set bag $param(color-buttonabg)
    set bfg $param(color-buttonfg)
    set cbg $param(color-ctlbg)
    set lbg #ffffff

    toplevel $parent
    frame $parent.1 -background $cbg
    frame $parent.2 -background $cbg
    frame $parent.3 -background $cbg
    frame $parent.1.spacer -background $cbg -width $bwid
    frame $parent.2.but -background $cbg

    # top frame

    label $parent.1.l1 -text "IP Addresses" -anchor w -justify left -width 20 \
	    -background $cbg
    pack $parent.1.l1 $parent.1.l1 -side left

    # middle frame

    set lsht 8
    set list1 [ScrolledLB $parent.2.lb1 -width 20 -selectmode 1 -height $lsht -background $lbg]
    $parent.2.lb1.pad configure -background $cbg
    bind $list1 <ButtonPress-1> \
	    [list SelectIP $list1 %y]
    button $parent.2.but.sel -text "+All" -width $bwid \
	    -background $bbg -foreground $bfg -activebackground $bag \
	    -borderwidth 4 -highlightbackground $cbg \
	    -command [list SetIPs $list1 +]
    button $parent.2.but.desel -text "-All" -width $bwid \
	    -background $bbg -foreground $bfg -activebackground $bag \
	    -borderwidth 4 -highlightbackground $cbg \
	    -command [list SetIPs $list1 -] 
    pack $parent.2.but.sel $parent.2.but.desel -side top
    pack $parent.2.lb1 $parent.2.but -side left

    # bottom frame

    button $parent.3.ok -text "OK" -padx 5 -pady 5 \
	    -background $bbg -foreground $bfg -activebackground $bag \
	    -borderwidth 4 -highlightbackground $cbg \
	    -command [list ipOKButtonPressed $parent]
    button $parent.3.cancel -text "Cancel" -padx 5 -pady 5 \
	    -background $bbg -foreground $bfg -activebackground $bag \
	    -borderwidth 4 -highlightbackground $cbg \
	    -command [list ipCancelButtonPressed $parent]
    pack $parent.3.ok $parent.3.cancel \
	    -side left -padx 20 -pady 10

    # whole thing

    pack $parent.1 $parent.2 $parent.3 -side top -fill both

    ShowIPs $list1
    $list1 selection set 0
    return $parent
}
#####################
# SetIPs --
#
#    Set all IPs to value
#
proc SetIPs { tgt_lb val } {
    global param
    set len [llength $param(IPList)]

    for { set i 0 } { $i < $len } { incr i } {
	SetAtIndex $i $val
    }
    ShowIPs $tgt_lb
    return
}

#####################
# ToggleIP
#
proc ToggleIP { ip } {
    set val [string range $ip 0 0]
    if { $val == "+" } {
	return "-[string range $ip 1 end]"
    } else {
	return "+[string range $ip 1 end]"
    }
}    

#####################
# SetIP
#
proc SetIP { ip newval } {
    return "$newval[string range $ip 1 end]"
}    

#####################
# ToggleAtIndex
#
proc ToggleAtIndex { idx } {
    global param
    set ip [lindex $param(IPList) $idx]
    set ip [ToggleIP $ip]
    set param(IPList) [lreplace $param(IPList) $idx $idx $ip]
}

#####################
# SetAtIndex
#
proc SetAtIndex { idx newval } {
    global param
    set ip [lindex $param(IPList) $idx]
    set ip [SetIP $ip $newval]
    set param(IPList) [lreplace $param(IPList) $idx $idx $ip]
}

#####################
# SelectIP --
#
#    Select/de-select IP
#
proc SelectIP { lb y } {
    global param
    
    set idx [$lb nearest $y]
    ToggleAtIndex $idx
    ShowIPs $lb
    return
}

#####################
# ShowIPs --
#
#     Refresh the list of IPs
#
proc ShowIPs { lb } {
    global param

    $lb delete 0 end
    set i 0
    foreach i $param(IPList) {
	$lb insert end $i
    }
}

#######################
# OKButtonPressed --
#
#     Process "OK" button
#
proc ipOKButtonPressed { parent } {
    global param didPause

    SetIPList
    set param(ViewChanged) 1
    Refresh
    destroy $parent
    update idletasks
    if { $didPause } {
	set param(Paused) 0
	Play
    }
}

########################
# CancelButtonPressed --
#
#    Process "Cancel" button
#
proc ipCancelButtonPressed { parent } {
    global didPause
    destroy $parent
    if { $didPause } {
	set param(Paused) 0
	Play
    }
}

#########################
# ShowIPDlg --
#
#    Initialize and draw the widget
#
proc ShowIPDlg { } {
    global param didPause

    if { $param(Paused) == 0 } { 
	set param(Paused) 1
	Pause
	set didPause 1 
    } else {
	set didPause 0
    }
    GetIPList
    set dlg [IPSelect .ip]
    wm title $dlg "Select IP Addresses to Display"
    wm geometry $dlg +100+200
}

global didPause
