#!/usr/local/bin/wishx3.3 -f
global tkisp_ed
set tkisp_ed(lib) /a/juno/u1/ra/raines/prog/tk/tkmail

# $Header: /a/juno/u1/ra/raines/src/tk/tkispell/tkispell,v 1.3 1993/12/11 01:31:11 raines Exp $
###########################################################################
#
#   TkIspell v1.5 -- A Tk/Tcl interface to Ispell
#	    		by Paul Raines (raines@bohr.physics.upenn.edu)
#
#   Note: this file is only a simple editor to demonstrate use of ispell.tk
#
#
# INSTALLATION:
#    TkIspell can be installed through the Makefile. Otherwise, do the
#    following:
#
#    (1) edit the first line above to reflect the location of wish.
#    (2) edit the third line above to reflect the location of utils.tk,
#	ispell.tk, bindings.tk and tclIndex.
#    (3) edit the ispell.tk file and follow the Installation 
#	instructions there.
#
#    This files contains a simple editor that calls the tkispell library.
#    It is only intended to demostrate the use of that library and is
#    not meant to be a general purpose editor. This program gives you
#    a basic Tk text widget with a row of buttons on the bottom to
#    read and write files and call the tkispell checker.
#
#

# globals
set tkisp_ed(file) ""
set tkisp_ed(top) .tkisp_ed

proc readfile { filename tw msg} {
  global tkisp_ed

  set tkisp_ed(file) $filename
  if {![file exists $filename]} then {
    $msg configure -text "File does not exist"
    set tkisp_ed(file) ""
    return 0
  } else {
    $msg configure -text "File: $filename"
    $tw delete 1.0 end
    $tw insert end  [exec cat $filename]
    $tw insert end "\n"
    $tw mark set insert 1.0
    return 1
  }
}

proc savefile {filename tw msg} {
    global tkisp_ed

    if {![llength $filename]} {return 0}

    if {[file exists $filename] && \
	    ![string match $tkisp_ed(file) $filename]} {
	if {![tkgetokay "Replace existing file?" $tkisp_ed(top)]} {return 0}
    }

    exec cat > $filename << [$tw get 0.0 end]
    set tkisp_ed(file) $filename
    $msg configure -text "Saved file $filename"
    return 1
}

if {[info exists auto_path]} {
    lappend auto_path $tkisp_ed(lib)
} else {
    set auto_path $tkisp_ed(lib)
}

wm withdraw .
toplevel $tkisp_ed(top) -class TkIspell

frame $tkisp_ed(top).tframe
scrollbar $tkisp_ed(top).tscr -command "$tkisp_ed(top).txt yview"
text $tkisp_ed(top).txt \
	-yscroll "bind_textscrollset $tkisp_ed(top).tscr $tkisp_ed(top).txt" \
	-setgrid true -width 80 -height 24
label $tkisp_ed(top).msg -text "No file" -relief raised

frame $tkisp_ed(top).bb
button $tkisp_ed(top).bb.ispell -text Ispell \
	-command "tkispell_text $tkisp_ed(top).txt"
button $tkisp_ed(top).bb.file -text File \
    -command {ut_fsbox -okcmd readfile -purpose {Read File:} \
	-grab 1 -master $tkisp_ed(top) \
	-okargs "$tkisp_ed(top).txt $tkisp_ed(top).msg"}
button $tkisp_ed(top).bb.save -text Save -command {savefile \
	$tkisp_ed(file) $tkisp_ed(top).txt $tkisp_ed(top).msg}
button $tkisp_ed(top).bb.saveas -text "Save As" \
    -command {ut_fsbox -okcmd savefile -purpose {Save File:} \
	-grab 1  -master $tkisp_ed(top) \
	-okargs "$tkisp_ed(top).txt $tkisp_ed(top).msg"}
button $tkisp_ed(top).bb.quit -text Quit -command { destroy . ; exit }

pack append $tkisp_ed(top).bb \
    $tkisp_ed(top).bb.ispell {left expand fill} \
    $tkisp_ed(top).bb.file {left expand fill} \
    $tkisp_ed(top).bb.save {left expand fill} \
    $tkisp_ed(top).bb.saveas {left expand fill} \
    $tkisp_ed(top).bb.quit {left expand fill}

pack append $tkisp_ed(top).tframe \
    $tkisp_ed(top).msg {top fillx} \
    $tkisp_ed(top).tscr {left filly} \
    $tkisp_ed(top).txt {left expand fill}

pack append $tkisp_ed(top) \
    $tkisp_ed(top).tframe {top expand fill} \
    $tkisp_ed(top).bb {top fillx}

set file [lindex $argv 0]
if {[llength $file]} {
    readfile $file $tkisp_ed(top).txt $tkisp_ed(top).msg
}

bind_motiftext $tkisp_ed(top).txt
bind_motifentry Entry
bind_emacstext $tkisp_ed(top).txt
bind_emacsentry Entry

