#!/usr/X11/bin/wishx -f
#*** The previous line *must* be wishx, because we *need* TclX extensions

set  xtexsh_path  "/usr/local/src/XTeXShell-0.91"

set  auto_path    "$xtexsh_path $auto_path"
set  home_dir     $env(HOME)

#*** Syntax: xtexsh [filename] [-m mainfilename]

#***********************************************************************
#*** Setup *************************************************************
#***********************************************************************

#*** About XTeX-Shell

set version   "0.91 Beta"
set verdate   "21.2.94"

set about " \
XTeX-Shell  Version $version  ($verdate)
Copyright <C> 1994 by Michael Hofmann

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Please send error reports, comments and flames to:
Michael T. Hofmann
Fontanestr. 5
55127 Mainz, Germany
hofmann@vipmzw.physik.uni-mainz.de"

#*** Check for proper version of TCl and Tk  and if TclX is installed

scan    [info tclversion] "%d.%d" a b
if      {$a < 7} {
        puts stderr "Wrong version of Tcl loaded ([info tclversion]): need 7.x or later"
        exit 1
}

scan    $tk_version "%d.%d" a b
if      {$a < 3} {
        puts stderr "Wrong version of Tk loaded ($tk_version): need 3.x or later"
        exit 1
}

set     a [catch {set version_tclx [infox version]}]

if      {$a} {
        puts stderr "You haven't installed TclX. Cannot continue!!!"
        puts stderr "Please install TclX. You can find help on newsboard comp.lang.tcl"
        exit 1
}

#*** Check if xtexsh_path is set correctly and that all program files are available

set     a 0
foreach pgm [list "edit.tcl" "help.tcl" "util.tcl" "exec.tcl" "ldsetup.tcl" "fileselbox.tcl"] {
        if {![file exists "$xtexsh_path/$pgm"]} {
                puts stderr "Can't find the file $xtexsh_path/$pgm."
                set a 1
        }
}
if {$a} {
        puts stderr "Did you set the variable xtexsh_path in the 5th line of xtexsh correctly?"
        puts stderr "Please refer to the file INSTALL for help."
        exit 1
}

unset   a b

#*** Load setup file

set home_path "";                       #*** Only for compatibility with xtex 0.9               
loadsetup

#***********************************************************************
#*** Set up main Window ************************************************
#***********************************************************************

CreateTopWin mainwin "CREATE"
wm title    . "XTeX-Shell (V$version)"
wm iconname . "XTeX-Shell"

set isColorX [cequal [tk colormodel "."] "color"]

frame       .mainwin       -relief raised
frame       .menu          -relief raised   -borderwidth 1
frame       .wizard        -relief raised   -border 1

pack        .menu          -side top -fill  x
pack        .wizard        -side top -fill  x
pack        .mainwin       -side top -fill  both

#*** Wizards

set     i 0
foreach command $commandlist {
        button .wizard.but$i -text [lindex $command 0] \
                             -command "ExecProg \"[lindex $command 1]\""
        pack .wizard.but$i -side left -anchor w -padx 3m -pady 1m -ipadx 0.6m
        incr i
}

button      .wizard.clear  -text "Purge"      -command "#PurgeFiles"
button      .wizard.quit   -text "Quit"       -command "EndXTeX"

pack        .wizard.quit   .wizard.clear -side right -anchor e -padx 3m -pady 1m -ipadx 0.6m

#*** Menu bar

menubutton .menu.file -text "File"       -menu .menu.file.m -underline 0
menubutton .menu.edit -text "Edit"       -menu .menu.edit.m -underline 0
menubutton .menu.cuts -text "ShortCuts"  -menu .menu.cuts.m -underline 0
menubutton .menu.main -text "Main File"  -menu .menu.main.m -underline 0
menubutton .menu.conf -text "Configure"  -menu .menu.conf.m -underline 0
menubutton .menu.help -text "Help"       -menu .menu.help.m -underline 0

pack       .menu.file .menu.edit .menu.cuts .menu.main .menu.conf -side left -padx 2m -pady 1m
pack       .menu.help -side right -padx 3m -pady 1m

#*** Pull Down Menus

menu    .menu.file.m
        .menu.file.m add command -label "New"             -underline 0 -command {Edit ""}
        .menu.file.m add command -label "Open..."         -underline 0 -command {Edit "\n"}
        .menu.file.m add cascade -label "ReOpen..."       -underline 0 -menu .menu.file.m.reopen
        .menu.file.m add command -label "Save"            -underline 0 -command {SaveFile ""}
        .menu.file.m add command -label "Save As..."      -underline 5 -command {SaveFile "AS" }
        .menu.file.m add command -label "Print"           -underline 0 -command {PrintFile $textbuf}
        .menu.file.m add separator
        .menu.file.m add command -label "Dir..."          -underline 0 -command {FileSelBox "DIR"}
        .menu.file.m add command -label "Change Dir..."   -underline 0 -command {ChangeDir}
        .menu.file.m add command -label "Shell"           -underline 0 -command {exec xterm &}
        .menu.file.m add separator
        .menu.file.m add command -label "Quit"            -underline 0 -command "EndXTeX"

menu    .menu.file.m.reopen

menu    .menu.edit.m
        .menu.edit.m add command -label "Search"          -underline 0 -command {EditSrch ""}
        .menu.edit.m add command -label "Replace"         -underline 0 -command {EditSrch "REPLACE"}
        .menu.edit.m add command -label "Continue"        -underline 0 -command {EditSrch "CONTINUE"}

menu    .menu.cuts.m

menu    .menu.main.m
        .menu.main.m add check   -label "Use Main File"   -underline 0 -variable mainflag
        .menu.main.m add command -label "Select New Main File" -underline 0 -command {SelNewMain ""}
        .menu.main.m add separator

menu    .menu.conf.m
        .menu.conf.m add radio   -label "XTeX-Edit (Built In)" -underline 0 -variable editor -value xtex

        set     a 0

        foreach command $editorlist {
                set varval [string tolower [lindex $command 1]]
                .menu.conf.m add radio   -label [lindex $command 0] -underline 0 \
                                         -variable editor -value $varval
                eval set command_editor_$varval \"[lindex $command 2]\"
                if {"$varval"=="$editor"} {
                        set a 1
                        .menu.conf.m invoke last
                }       
        }

        if {!$a} {                                              #*** If selected editor is not available, use XTeX-Edit
                .menu.conf.m invoke 0
        }

        .menu.conf.m add separator
        .menu.conf.m add cascade -label "Config XTeX-Edit" -underline 0 -menu .menu.conf.m.m
        .menu.conf.m add cascade -label "Save Options"    -underline 0 -menu .menu.conf.m.s

menu    .menu.conf.m.m
        .menu.conf.m.m add radio -label "Turbo Edit Mode" -underline 0 -variable editmode -value TURBO
        .menu.conf.m.m add radio -label "E2 Edit Mode"    -underline 0 -variable editmode -value E2
        .menu.conf.m.m add separator
        .menu.conf.m.m add check -label "Word Wrapping"   -underline 0 -variable editwrap
        .menu.conf.m.m add check -label "Character Translation" -underline 0 -variable edittrans

menu    .menu.conf.m.s
        .menu.conf.m.s add command -label "Save Global"   -underline 0 -command "writesetup 1"
        .menu.conf.m.s add command -label "Save Local"    -underline 0 -command "writesetup 0"

menu    .menu.help.m
        .menu.help.m add command -label "LaTeX Help Index" -underline 0 -command "HyperHelp Index"
        .menu.help.m add command -label "LaTeX Symbols"   -underline 0 -command "HyperHelp symbols"
        .menu.help.m add command -label "LaTeX Fonts"     -underline 0 -command "HyperHelp fonts"
        .menu.help.m add separator
        .menu.help.m add command -label "XTeX Help"       -underline 0 -command "HyperHelp _helpxtex"
        .menu.help.m add command -label "XTeX Editor Help" -underline 0 -command "HyperHelp _helpxtexedit"
        .menu.help.m add command -label "XTeX License"    -underline 0 -command "DisplayInfo \"$about\" \"$bold_font\""
        .menu.help.m add separator
        .menu.help.m add command -label "TeX/LaTeX"       -underline 0 -command "ManHelp tex"
        .menu.help.m add command -label "xdvi"            -underline 0 -command "ManHelp xdvi"
        .menu.help.m add command -label "dvips"           -underline 0 -command "ManHelp dvips"
        .menu.help.m add command -label "ghostview"       -underline 0 -command "ManHelp ghostview"
        .menu.help.m add separator
        .menu.help.m add command -label "Emacs"           -underline 0 -command "ManHelp emacs"
        .menu.help.m add command -label "Joe"             -underline 0 -command "ManHelp joe"

#*** Load short cuts

loadshortcuts

#*** Set up for keyboard-based menu traversal

bind . <Any-FocusIn> {
        if {("%d" == "NotifyVirtual") && ("%m" == "NotifyNormal")} {
                focus .menu
        }
}

tk_menuBar .menu .menu.file
tk_menuBar .menu .menu.edit
tk_menuBar .menu .menu.cuts
tk_menuBar .menu .menu.main
tk_menuBar .menu .menu.conf
tk_menuBar .menu .menu.help

#*** Scan command-line arguments

for {set i 0} {$i<$argc} {incr i} {
        if {[cequal [lindex $argv $i] "-m"]} {
                incr i          
                SelNewMain [lindex $argv $i]
                continue                
        }       
        Edit [lindex $argv $i]
}

#******************************************************************************
#*** Quit was selected. Save Data and exit ************************************
#******************************************************************************

proc EndXTeX {} {

        EndXTeXEdit
        destroy .
}