# network.tcl
# Copyright (c) 2003 Digi International Inc., All Rights Reserved
# 
# This software contains proprietary and confidential information of Digi
# International Inc.  By accepting transfer of this copy, Recipient agrees
# to retain this software in confidence, to prevent disclosure to others,
# and to make no use of this software other than that for which it was
# delivered.  This is an unpublished copyrighted work of Digi International
# Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
# prohibited.
# 
# Restricted Rights Legend
#
# Use, duplication, or disclosure by the Government is subject to
# restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
# Technical Data and Computer Software clause at DFARS 252.227-7031 or
# subparagraphs (c)(1) and (2) of the Commercial Computer Software -
# Restricted Rights at 48 CFR 52.227-19, as applicable.
#
# Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
#
# ***************************************************************************

namespace eval Network {}

proc Network::MainGuts {w} {
    variable Tab1st
    set frmMain $w.frmnetwork
    if {![winfo exists $frmMain]} {
        frame $frmMain
        BuildPage $frmMain
        grid $frmMain -row 0 -column 0 -sticky news \
         -padx [expr $::numTitleBoldXPad + $::numBorderWidth] -pady 5
    }
    raise $frmMain
    set ::Tab1st $Tab1st
    bind $::Tab1st <<ShiftTab>> {focus $::btnHelp;break}
}

proc Network::IsConfigurable {lstDevice} {
    set ret 1
    if {[string equal [lindex $lstDevice 0] [mc discover.notfound]]} {
        set ret 0
        ::ShowHelp #Notfound
    } elseif {[string equal [lindex $lstDevice 0] "noneselected"]} {
        set ret 0
        tk_messageBox -message [mc msg.nonselected]
    }
    return $ret
}

proc Network::GetHardware {lstDevice} {
    return [lindex $lstDevice 3]
}

proc Network::BuildPage {w} {
    variable Tab1st
    set lblHeading $w.lblheading
    ::LabelHeading $lblHeading [mc network.body.heading] 
    set radioDHCP [radiobutton $w.rddhcp -text [mc network.body.dhcp] \
     -variable ::Config::arrSettings(DHCP) -value On -command {::Network::DHCPState}]
    set radioUse [radiobutton $w.rduse -text [mc network.body.usethe] \
     -variable ::Config::arrSettings(DHCP) -value Off -command {::Network::DHCPState}]
    set frmUse [frame $w.frmuse -relief groove -width $::numGutsHeadingWraplength -borderwidth 2]
    Network::BuildEntries $frmUse
    set lblContinue [label $w.lblcont -text [mc guts.tocontinue] -font fontRegularBold]
    grid $lblHeading -row 0 -column 0 -sticky nw
    grid $radioDHCP -row 1 -column 0 -sticky nw -padx 5
    grid $radioUse -row 2 -column 0 -sticky nw -padx 5
    grid $frmUse -row 2 -column 0 -sticky ew -ipadx 10 -ipady 15 -pady 10
    raise $radioUse
    grid $lblContinue -row 3 -column 0 -sticky sw

    grid rowconfigure $w 3 -weight 1
    grid columnconfigure $w 0 -weight 1
    set Tab1st $radioDHCP
}

proc Network::BuildEntries {frmEntry} {
    variable arrEntries
    array set arrEntries {}
    set lblIP [label $frmEntry.lblip -text [mc network.ip]]
    set entryIP [mentry::ipAddrMentry $frmEntry.entip -textvariable ::Config::arrIP \
     -bg white -justify center -state disabled]
    set lblSub [label $frmEntry.lblsub -text [mc network.submask]]
    set entrySubmask [mentry::ipAddrMentry $frmEntry.entsub -textvariable ::Config::arrSubmask \
     -bg white -justify center -state disabled]
    set lblGate [label $frmEntry.lblgate -text [mc network.gateway]]
    set entryGateway [mentry::ipAddrMentry $frmEntry.entgate -textvariable ::Config::arrGateway \
     -bg white -justify center -state disabled]
    
    ## Configure Subwidget width
    foreach mwName {entryIP entrySubmask entryGateway} {
        upvar 0 $mwName mw
        foreach sw [$mw entries] {
            $sw configure -width 4
        }
    }

    set lstEntries {IP Submask Gateway}
    foreach wName $lstEntries {
        upvar 0 entry$wName w 
        set arrEntries($wName) $w
    }

    grid $lblIP    -row 0 -column 0 -sticky e -padx 5
    grid $entryIP  -row 0 -column 1 -sticky w -pady 2
    grid $lblSub    -row 1 -column 0 -sticky e -padx 5
    grid $entrySubmask  -row 1 -column 1 -sticky w -pady 2
    grid $lblGate    -row 2 -column 0 -sticky e -padx 5
    grid $entryGateway  -row 2 -column 1 -sticky w -pady 2
    
    grid columnconfigure $frmEntry 1 -weight 1
}

proc Network::DHCPState {} {
    variable arrEntries
    switch $::Config::arrSettings(DHCP) {
        NA -
        Off {
            EntryDisable
            EntryEnable
        }
        On {
            EntryDisable
        }
    }
}

proc Network::EntryDisable {} {
    variable arrEntries
    foreach {index value} [array get arrEntries] {
        $value configure -state disabled -bg $::colorSystem -fg grey50
    }
}

proc Network::EntryEnable {} {
    variable arrEntries
    foreach {index value} [array get arrEntries] {
        if {[lsearch $::Config::arrSettings(lstNSettable) $index] >= 0 && \
            ![string equal $::Discover::arrSettings($index) "NA"] && \
            ![string equal $::Discover::arrSettings($index) "(null)"]} {
            $value configure -state normal -bg white -fg black
        }
    }
}

