#!/bin/sh
# the next line restarts this script with itcl/itk \
exec itkwish2.1 $0 $*
# ----------------------------------------------------------------------
#  EXAMPLE: simple Tree class (widget browser application)
# ----------------------------------------------------------------------
#     BOOK: Tcl/Tk Extensions for O'Reilly & Associates
#   AUTHOR: Michael J. McLennan, AT&T Bell Laboratories
# ======================================================================
#               Copyright (c) 1996  AT&T Bell Laboratories
# ======================================================================

lappend auto_path /home/mmc/oreilly/itcl/lib /home/mmc/oreilly/itcl/widgetree

# ----------------------------------------------------------------------

set root ""
proc load_tree {app} {
    global root

    if {$root != ""} {
        delete object $root
        set root ""
    }
    set root [create_node $app "."]
    $root configure -state open
    $root refresh

    .control.app configure -text $app

    return $root
}

proc config_widget {win} {
    set app [.apps get]

    if {[catch {send $app "$win configure"} optlist] == 0} {
        . configure -cursor watch
        update

        set panel [dialog .#auto -title "Configure: $win"]
        $panel hide Apply
        $panel hide Help
        $panel hide Cancel
        $panel buttonconfigure OK -text "Dismiss" -command "destroy $panel"
        set inner [$panel childsite]

        scrolledframe $inner.form -width 4i -height 3i \
            -vscrollmode dynamic -hscrollmode none
        pack $inner.form -expand yes -fill both -padx 4 -pady 4

        set form [$inner.form childsite]
        set entries {}

        set line 0
        foreach opt $optlist {
            if {[llength $opt] == 5} {
                set name [lindex $opt 0]
                set val [lindex $opt 4]
                set row "$form.row[incr line]"
                entryfield $row -labeltext $name -width 40 \
                    -command [format {
                        modify_widget {%s} %s %s [%s get]
                    } $app $win $name $row]
                pack $row -expand yes -fill x
                $row insert 0 $val

                lappend entries $row
            }
        }
        eval Labeledwidget::alignlabels $entries
        $panel activate

        . configure -cursor ""
    }
}

proc modify_widget {app win option val} {
    set cmd [list send $app [list $win configure $option $val]]
    catch $cmd
}

# ----------------------------------------------------------------------

optionmenu .apps -labeltext "Application:" -command {
    .viewer display [.apps get]
}
pack .apps -pady 4

foreach app [winfo interps] {
    .apps insert end $app
}

widgetviewer .viewer -selectcommand {
    .config.win configure -text %v
}
pack .viewer -expand yes -fill both -padx 4 -pady 4

frame .config
pack .config -fill x -padx 4 -pady 4
button .config.edit -text "Configure:" -command {
    config_widget [.config.win cget -text]
}
pack .config.edit -side left
label .config.win
pack .config.win -side left

.apps select [tk appname]
.viewer display [.apps get]
