#!/usr/local/bin/moat

global wdisplay # the window in which the results are shown
global Lessons	# array of lessons
global nlessons	# total count of lessons
global curlesson	# which lesson is being run
set nlessons 0

###############################################
#
# Lesson handling stuff
#
###############################################

###############################################
#
# apply-changes

proc apply-changes {} {
    global wdisplay

    send $wdisplay {
	. getValues -children children
	foreach w $children {
	    $w destroyWidget
	}
    }

    .main.msgbox.text getValues -value cmd
    send $wdisplay $cmd
}

##############################################
#
# previous

proc previous-lesson {} {
    adjust-lesson -1
}

#############################################
#
# next

proc next-lesson {} {
    adjust-lesson 1
}

############################################
#
# lesson - create info about a new lesson
#          create a new menu item if a new lesson is there
#

proc lesson {mname name file} {
    global Lessons nlessons

    set Lessons($nlessons) $file

    if { [info command .main.menuBar.$mname] != ".main.menuBar.$mname"} {
	xmPulldownMenu .main.pane-$mname
	xmCascadeButton .main.menuBar.$mname managed \
	        -subMenuId .main.pane-$mname
    }

    if {$name != ""} {
	xmPushButton .main.pane-$mname.$name managed
	.main.pane-$mname.$name activateCallback "set-lesson $nlessons"
        incr nlessons
    } {
                $mb add separator
    }
}

#############################################
#
#  Set the current lesson

proc set-lesson {n} {
        global Lessons curlesson

        set curlesson $n
        do-open-file $Lessons($n)
}

####################################################
#
#  Adjust the current lesson by some increment
#
proc adjust-lesson {i} {
    global curlesson nlessons

    incr curlesson $i
    if {$curlesson >= $nlessons} {
        xmWarningDialog .err managed \
	    -messageString "That was the last lesson!"
	.err okCallback {.err destroyWidget}
	.err.Help unmanageChild
	.err.Cancel unmanageChild

        set curlesson [expr $nlessons-1]
    }
    if {$curlesson < 0} {
        xmWarningDialog .err managed \
	    -messageString "That was the first lesson!"
	.err okCallback {.err destroyWidget}
	.err.Help unmanageChild
	.err.Cancel unmanageChild

        set curlesson 0
    }
    set-lesson $curlesson
}


############################################
#
#  Load the edit buffer from a file

proc do-open-file {fname} {
    global filename errorCode

    if [catch {set f [open $fname]}] {
        set msg "Error: can't open the file `$fname'"
        if {[lindex $errorCode 0] == "UNIX"} {
            append msg " -- [lindex $errorCode 2]"
        }
	xmErrorDialog .err managed \
	    -messageString $msg
	.err okCallback ".err destroyWidget"
	.err.Cancel unmanageChild
	.err.Help unmanageChild
    } {
        set filename $fname
        .main.msgbox.text setValues \
	    -value [read $f]
        close $f
    }
    apply-changes
}

###################################################
#
# helpAboutCB
#

proc helpAboutCB {} {
    xmInformationDialog .helpAboutDialog managed \
        -messageAlignment alignment_center \
        -messageString "About wtour\n\
Jan Newmarch\n\
Version 1.0\n\
January, 1993"
    .helpAboutDialog okCallback {[%w parent] destroyWidget}
    .helpAboutDialog.Cancel unmanageChild
    .helpAboutDialog.Help unmanageChild
}



############################################
# 
# start up a display window for all widgets:

set pid [pid]
set wdisplay wtour-display-$pid
exec wtour-display -geometry 200x200 \
	-title $wdisplay &


# busy wait till wtour-display gets going
set n 1
while {$n < 10000} {incr n}

xtAppInitialize

xmMainWindow .main managed

xmMessageBox .main.msgbox managed \
    -okLabelString Apply \
    -cancelLabelString Next \
    -helpLabelString Previous \
    -defaultButtonType dialog_cancel_button
.main.msgbox okCallback apply-changes
.main.msgbox cancelCallback next-lesson
.main.msgbox helpCallback previous-lesson
.main.msgbox.Message unmanageChild 
.main.msgbox.Symbol unmanageChild

xmScrolledText .main.msgbox.text managed \
    -editMode multi_line_edit \
    -columns 60 \
    -rows 10

#
# menu system along top
#

# menu bar
xmMenuBar .main.menuBar managed

# file pulldown
xmPulldownMenu .main.filePane
xmCascadeButton .main.menuBar.File managed \
        -subMenuId .main.filePane

xmPushButton .main.filePane.Quit managed
.main.filePane.Quit activateCallback {
	send wtour-display-$pid ". addTimer 1000 exit"
	exit
}

#
# help
#
xmPulldownMenu .main.helpPane
xmCascadeButton .main.menuBar.Help managed \
        -subMenuId .main.helpPane
.main.menuBar setValues \
        -menuHelpWidget .main.menuBar.Help

xmPushButton .main.helpPane.about managed \
        -labelString "About..."
.main.helpPane.about activateCallback helpAboutCB


.main setValues -menuBar .main.menuBar \
                -workWindow .main.msgbox

###########################################
#
# load the lesson index

source index
set-lesson 0

. realizeWidget
. mainLoop
