#!/usr/bin/wish
#############################################################################
# Visual Tcl v1.10 Project
#

#################################
# GLOBAL VARIABLES
#
global widget; 
    set widget(rev,.01.02) {CANVAS}
    set widget(rev,.tclet.01) {CANVAS}
    set widget(rev,.top17.fra18.01) {TEXT}
    set widget(rev,.top26.tex30) {text}
    set widget(rev,.top28.fra29.can30) {CANVAS}

#################################
# USER DEFINED PROCEDURES
#
proc init {argc argv} {

}

init $argc $argv


proc read_pipe {PipeHandle} {
# Read one line from the Pipe
global Recurse

catch {
    if {[string compare $Recurse True]==0} then {
        return
    }
}

set Recurse True

if [eof $PipeHandle] {
    catch {close $PipeHandle}
    return
}

gets $PipeHandle data
if {[string compare $data *\[up\]*]==0} {
    set pos [.top26.tex30 index end]
    set pos [.top26.tex30 index "$pos -1 lines"]
    .top26.tex30 delete "$pos linestart" end
    return
}

.top26.tex30 insert end "\n$data"
.top26.tex30 see end

update
update idletasks

set Recurse False
}

proc start_nserver {} {
# start up the Nserver engine
global Nserver_PID

set PipeHandle [open "|nserver -gui"]

# Set to non-blocking
fconfigure $PipeHandle -blocking 0

# Grab the PID
set Nserver_PID [pid $PipeHandle]

# and setup the stdio capture
fileevent $PipeHandle readable [list read_pipe $PipeHandle]
}

proc main {argc argv} {
after 500 start_nserver
}

proc Window {args} {
global vTcl
    set cmd [lindex $args 0]
    set name [lindex $args 1]
    set newname [lindex $args 2]
    set rest [lrange $args 3 end]
    if {$name == "" || $cmd == ""} {return}
    if {$newname == ""} {
        set newname $name
    }
    set exists [winfo exists $newname]
    switch $cmd {
        show {
            if {$exists == "1" && $name != "."} {wm deiconify $name; return}
            if {[info procs vTclWindow(pre)$name] != ""} {
                eval "vTclWindow(pre)$name $newname $rest"
            }
            if {[info procs vTclWindow$name] != ""} {
                eval "vTclWindow$name $newname $rest"
            }
            if {[info procs vTclWindow(post)$name] != ""} {
                eval "vTclWindow(post)$name $newname $rest"
            }
        }
        hide    { if $exists {wm withdraw $newname; return} }
        iconify { if $exists {wm iconify $newname; return} }
        destroy { if $exists {destroy $newname; return} }
    }
}

#################################
# VTCL GENERATED GUI PROCEDURES
#

proc vTclWindow. {base} {
    if {$base == ""} {
        set base .
    }
    ###################
    # CREATING WIDGETS
    ###################
    wm focusmodel $base passive
    wm geometry $base 200x200+0+0
    wm maxsize $base 1137 870
    wm minsize $base 1 1
    wm overrideredirect $base 0
    wm resizable $base 1 1
    wm withdraw $base
    wm title $base "vt.tcl"
    ###################
    # SETTING GEOMETRY
    ###################
}

proc vTclWindow.top26 {base} {
    if {$base == ""} {
        set base .top26
    }
    if {[winfo exists $base]} {
        wm deiconify $base; return
    }
    ###################
    # CREATING WIDGETS
    ###################
    toplevel $base -class Toplevel \
        -cursor xterm 
    wm focusmodel $base passive
    wm geometry $base 513x239+70+131
    wm maxsize $base 785 570
    wm minsize $base 1 1
    wm overrideredirect $base 0
    wm resizable $base 1 1
    wm deiconify $base
    wm title $base "Nokia Communicator Server"
    text $base.tex30 \
        -wrap word -yscrollcommand {.top26.scr18 set} 
    frame $base.fra32 \
        -borderwidth 2 -cursor top_left_arrow -height 75 -relief groove \
        -width 125 
    button $base.fra32.but33 \
        \
        -command {catch {close $PipeHandle}

catch {exec kill $Nserver_PID}

exit} \
        -cursor top_left_arrow -padx 9 -pady 3 -text Exit 
    scrollbar $base.scr18 \
        -command {.top26.tex30 yview} -cursor top_left_arrow -orient vert 
    ###################
    # SETTING GEOMETRY
    ###################
    place $base.tex30 \
        -x 0 -y 0 -width -15 -relwidth 1 -height -40 -relheight 1 -anchor nw \
        -bordermode ignore 
    place $base.fra32 \
        -x 0 -y -40 -rely 1 -width 0 -relwidth 1 -height 40 -relheight 0 \
        -anchor nw -bordermode ignore 
    place $base.fra32.but33 \
        -x 10 -y 5 -width -20 -relwidth 1 -height 28 -relheight 0 -anchor nw \
        -bordermode ignore 
    place $base.scr18 \
        -x -18 -relx 1 -y 0 -width 18 -height -40 -relheight 1 -anchor nw \
        -bordermode ignore 
}

Window show .
Window show .top26

main $argc $argv
