#!/usr/bin/wish -f
#
# $Id: xbuch,v 1.11 1999/12/01 15:01:32 ke Exp $
# $Locker:  $  
# 
# $Log: xbuch,v $
# Revision 1.11  1999/12/01 15:01:32  ke
# wish path (max)
#
# Revision 1.10  1999/05/19 16:47:26  ke
# change susebuchdir again
#
# Revision 1.9  1999/02/25 21:09:07  wey
# Pfad auf /home/alex/lxbuch/src/susebuch
# gesetzt
#
# Revision 1.8  1997/10/09 08:44:44  ke
# #! auf wish umgestellt
#
# Revision 1.7  1997/03/26 17:17:41  maddin
# an neues Format fr xbuch2 angepat
#
# Revision 1.6  1997/03/25 14:23:59  maddin
# maxrows
#
# Revision 1.5  1997/03/24 15:36:25  maddin
# new functions: show template.tex, show documentation, short help function
# clear selection when requested by other application
# better window to display the pasted makro
#
# Revision 1.4  1997/03/20 15:40:19  maddin
# Sortieren der Makroliste
#
# Revision 1.3  1997/03/19 21:48:49  werner
# Korrekter Pfad vom template
#
# Revision 1.2  1997/03/19 21:46:40  werner
# bernahme ins RCS
#
#

set susebuchdir "/usr/share/texmf/tex/latex/susebuch"
set template    "$susebuchdir/template"
set textemplate "$susebuchdir/template.tex"
set anleitung   "$susebuchdir/doc/docu.dvi"

set maxrow 35

proc readtemplate {} {

    global template var ptext pname

    set fileID [ open "$template" "r" ]
    if { $fileID == -1 } {

	puts "open error"
	return
    }

    set var(topiclist) {}
    set buffer {}
    while { [ gets $fileID buffer ] != -1 } {

	set dummy {}
	set rawvalue {}
	if { [ regexp {%% -----[\-]+(.+)} $buffer dummy rawvalue ] == 0 } {

	    continue
	}

	set value {}
	if { [ regexp {.+\|(.+)} $rawvalue dummy value] == 0 } {

	    set value $rawvalue
	}

	regsub -all {\.} $value "_" realval
	regsub -all { } $realval "_" realval
	regsub -all {\\} $realval "_" realval
	regsub -all {\$} $realval "_" realval

	set realval [ string tolower $realval ]

	while { [ lsearch -exact $var(topiclist) $realval ] != -1 } {

	    append realval "_"
	}

	set pname($realval) $value 
	set ptext($realval) {}
	set linecount 0

	while { [ gets $fileID buffer ] != -1 } {
	
	    if { [ regexp {%% -----[\-]+} $buffer ] == 1 } {
		
		break
	    }

	    if { $linecount > 0 } {
		append ptext($realval) "\n"
	    }	

	    append ptext($realval) "$buffer"

	    incr linecount
	}

	lappend var(topiclist) $realval
    }

    close $fileID
}

proc makePasteWin { } {

    global textwin

    toplevel .text
    wm title .text "Exportiertes Makro"
    wm transient .text

    set textwin [ \
	    text .text.text \
	    -width 50 \
	    -height 15 \
	    -exportselection true \
	    -state disabled \
	    -relief raised -bd 2 \
	    ]
    pack $textwin \
	    -side top -anchor w \
	    -fill x -expand yes

    button .text.quit \
	    -text "Schlieen" \
	    -command { wm withdraw .text }

    pack .text.quit \
	    -side top -fill x -expand yes

    bind .text <Any-Return>    ".text.quit invoke"
    bind .text <Any-Key-space> ".text.quit invoke"
    bind .text <Button-1>  "$textwin tag add sel 1.0 insert"

    wm withdraw .text
}

proc setSel { topic { what "" } } {

    global textwin var lastsel ptext 

    #
    # take selection
    #
    selection clear .

    if { $what == "clear" } {

	$textwin tag remove sel 1.0 end
	return
    }

    #
    # select everything in text except trailing newline
    #
    $textwin tag add sel 1.0 insert

    #
    # set notification for selection changes
    #
    selection own \
	    -command { 
	#
	# check if selection is taken by another applikation
	#
	if { [ selection own ] == {} } {
	    
	    set var($lastsel) 0
	    focus .main.topics.$var($lastsel,col).$var($lastsel,row) 
	    setSel $lastsel clear
	    $textwin tag remove sel 1.0 end
	} 	
    } $textwin
}

proc paste { topic { what "" } } {

    global textwin var lastsel ptext

    if { ! [ winfo exists .text ] } {

	makePasteWin
    } 

    if { $what == "show" } {

	wm deiconify .text
    }

    delPasteText

    #
    # enable to permit insertion of text
    #
    $textwin configure -state normal

    #
    # insert new text
    #
    $textwin insert end $ptext($topic)

    #
    # no further operations to the widget permitted
    #
    $textwin configure -state disabled

    setSel $topic

    focus .main.topics.$var($topic,col).$var($topic,row) 

    #
    # if nothing is focused
    #
    if { $lastsel != "" } {
	set var($lastsel) 0
    }

    #
    # set checkbox value
    #
    set var($topic)    1
    set lastsel       $topic

}

proc delPasteText { } {

    global textwin var lastsel ptext

    #
    # enable to permit insertion of text
    #
    $textwin configure -state normal

    #
    # remove former text
    #
    $textwin delete 1.0 end

    #
    # no further operations to the widget permitted
    #
    $textwin configure -state disabled
}

proc makeHelpWin {} {

    if [ winfo exists .help ] {
	
	wm deiconify .help
	return
    } 

    toplevel .help
    wm title .help "xbuch-Kurzanleitung"

    message .help.text \
	    -width 500 \
	    -font "-adobe-helvetica-medium-r-normal--11-80-100-100-p-60-iso8859-1" \
	    -text \
"Kurzanleitung zu xbuch

Tasten:

\tPfeile + Tab:\tBewegung zum nchsten bzw. vorhergehenden Knopf
\tLeertaste:\t\tSelektieren des Makros, auch zum Export
\tDoppel-Leertaste:\tSelektieren und anzeigen
\tReturn:\t\tSelektieren und anzeigen

Maus:

\tLinks-Click:\tSelektieren des Makros, auch zum Export
\tLinks-Doppelclick:\tSelektieren und anzeigen
\tRechts-Click:\tSelektieren und anzeigen

Anzeigefenster:

\tLinks-Click:\tNeu-Selektieren des Makros
"


    button .help.quit \
	    -text "Schlieen" \
	    -command { wm withdraw .help }

    pack .help.text \
	    -side top \
	    -expand yes -fill x

    pack .help.quit \
	    -side top -fill x -expand yes
}

set lastsel ""

frame .main
frame .main.topics \
	-relief raised -bd 2

readtemplate

set var(topiclist) [ lsort $var(topiclist) ]

set row  0
set col  0

foreach topic $var(topiclist) {

    if { $row == 0 } {

	frame .main.topics.$col 
	pack .main.topics.$col \
		-side left -anchor nw \
		-expand yes
    }

    set var($topic) 0

    set var($topic,col) $col
    set var($topic,row) $row

    checkbutton .main.topics.$col.$row \
	    -text "$pname($topic)" \
	    -command "paste $topic" \
	    -variable var($topic) \
	    -font "-adobe-helvetica-medium-r-normal--11-80-100-100-p-60-iso8859-1" 

    pack .main.topics.$col.$row \
	    -side top -anchor w \
	    -padx 0m -pady 0m \
	    -ipadx 0m -ipady 0m \
	    -expand yes

    bind .main.topics.$col.$row <Key-Return> "paste $topic show"
    bind .main.topics.$col.$row <Double-Key-space> "paste $topic show"
    bind .main.topics.$col.$row <Double-Button-1> "paste $topic show"
    bind .main.topics.$col.$row <Button-3> "paste $topic show"

    incr row

    if { $row == $maxrow } {

	incr col
	set row 0
    }
}

set maxcol $col

bind Checkbutton <Any-Key-Up>    { focus [ tk_focusPrev %W  ] }
bind Checkbutton <Any-Key-Down>  { focus [ tk_focusNext %W  ] }
bind Checkbutton <Any-Key-Left>  { focus [ tk_focusLeft %W  ] }
bind Checkbutton <Any-Key-Right> { focus [ tk_focusRight %W ] }

bind Button <Any-Key-Up>    { focus [ tk_focusPrev %W ] }
bind Button <Any-Key-Down>  { focus [ tk_focusNext %W ] }
bind Button <Any-Key-Left>  { focus [ tk_focusPrev %W ] }
bind Button <Any-Key-Right> { focus [ tk_focusNext %W ] }

proc tk_focusLeft { w } { 

    global maxcol

    set dummy {}
    set row   {}
    set col   {}
    regexp {\.main\.topics\.([0-9]+)\.([0-9]+)} $w dummy col row

    incr col -1
    
    if { $col < 0 } {
	
	set col $maxcol
    }
    
    set newwin ".main.topics.$col.$row"
    
    while { ! [ winfo exists $newwin ] } {

	incr col -1
	
	if { $col < 0 } {
	    
	    set col $maxcol
	}
	
	set newwin ".main.topics.$col.$row"
    }

    return $newwin
}

proc tk_focusRight { w } { 

    global maxcol

    set dummy {}
    set row   {}
    set col   {}
    regexp {\.main\.topics\.([0-9]+)\.([0-9]+)} $w dummy col row

    incr col 1
    
    if { $col > $maxcol } {
	
	set col 0
    }
    
    set newwin ".main.topics.$col.$row"
    
    while { ! [ winfo exists $newwin ] } {

	incr col 1
	
	if { $col > $maxcol } {
	    
	    set col 0
	}
	
	set newwin ".main.topics.$col.$row"
    }

    return $newwin
}

frame .main.btns \
	-relief raised -bd 2

button .main.btns.help \
	-text "Hilfe" \
	-command { makeHelpWin }

button .main.btns.template \
	-text "Vorlage ansehen" \
	-command { exec xless $textemplate & }

button .main.btns.anleitung \
	-text "Anleitung lesen" \
	-command { exec xdvi $anleitung & }

button .main.btns.quit \
	-text "Beenden" \
	-command { exit 0 }

.main.btns.help      configure -underline 0
.main.btns.anleitung configure -underline 0
.main.btns.template  configure -underline 0
.main.btns.quit      configure -underline 0

pack .main.topics \
	-side top \
	-fill x

pack .main.btns.help .main.btns.template .main.btns.anleitung .main.btns.quit \
	-side left \
	-fill x -expand yes \
	-padx 1m -pady 1m

pack .main.btns \
	-side top \
	-fill x

pack .main \
	-side top \
	-fill x
	
bind all q ".main.btns.quit      invoke"
bind all h ".main.btns.help      invoke"
bind all a ".main.btns.anleitung invoke"
bind all v ".main.btns.template  invoke"

wm title . "S.u.S.E.-Buch-Makros"

if { ! [ file exists "$anleitung" ] } {

    .main.btns.help configure -state disabled 
}
if { ! [ file exists "$textemplate" ] } {

    .main.btns.template configure -state disabled 
}

set topic [lindex $var(topiclist) 0]
focus .main.topics.0.0 

