#!/usr/bin/wish -f
#
# $Id: xbuch2,v 1.6 1999/12/01 15:42:56 ke Exp $
# $Locker:  $  
# 
# $Log: xbuch2,v $
# Revision 1.6  1999/12/01 15:42:56  ke
# path adjustments
#
# Revision 1.5  1999/02/25 21:11:33  wey
# Pfad angepasst auf /home/alex/lxbuch/src/susebuch
#
# Revision 1.4  1997/10/09 08:45:05  ke
# #! auf wish umgestellt
#
# Revision 1.3  1997/03/28 16:11:47  maddin
# *** empty log message ***
#
# Revision 1.2  1997/03/26 17:18:02  maddin
# neu: mehr bersicht; mit submens
#
# 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 debugmode off

######################################################################
#
# debug
#
######################################################################

proc debug { what } {

    global debugmode

    upvar $what var

    if { $debugmode == "on" } {

	puts "$what: $var"
    }
}

set focusList {}
proc wanderFocus { item way } {

    global focusList

    set i [ lsearch -exact $focusList [ focus ] ]

    switch -- $way {

	next {

	    incr i
	    if { $i == [ llength $focusList ] } {
		
		set i 0
	    }
	}

	prev {

	    if { $i == 0 } {
		
		set i [ llength $focusList ]
	    }

	    incr i -1
	}
    }

    focus [ lindex $focusList $i ]
}

proc newFocus { w } {

    global focusList 

    lappend focusList $w
}

######################################################################
#
# optionmenu
#
######################################################################

proc optionmenu args {

    set w [ lindex $args 0 ]
    set args [ lrange $args 1 end ]

    menubutton $w -indicatoron 1 -menu $w.menu \
	    -padx 4p -pady 4p -highlightthickness 2

    menu $w.menu -tearoff 0

    while { $args != {} } {

	set opt    [ lindex $args 0 ]
	set optval [ lindex $args 1 ]
	set args   [ lrange $args 2 end ]

	switch -regexp -- "$opt" {
	    
	    -textvariable {

		if { ! [ info exists $optval ] } {
		    set $optval {}
		}
		$w configure -textvariable $optval
	    }

	    -(justify|width|border|bd|relief|anchor) {

		$w configure $opt $optval
	    }

	    default {

		puts "error: unknown option $opt"
		return -1
	    }
	}
    }

    newFocus $w
}

######################################################################
#
# readtemplate
#
######################################################################

proc readtemplate {} {

    global template var ptext pname

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

	puts "open error"
	return
    }

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

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

	    continue
	}

	debug rawvalue

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

	    set submenu "Sonstige"
	    set value $rawvalue
	}

	debug value

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

	#
	# this is the key!
	#
	set realval [ string tolower $realval ]

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

	    append realval "_"
	}

	#
	# text to display in the menu
	#
	set var($realval,label)   $value

	#
	# submenu to which this entry belongs
	#
	set var($realval,menu)    $submenu

	#
	# this is the text to be pasted 
	#
	set var($realval,text)    {}

	set linecount 0

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

	    if { $linecount > 0 } {
		append var($realval,text) "\n"
	    }	

	    append var($realval,text) "$buffer"

	    incr linecount
	}

	#
	# check if this menu is already in menulist
	#
	while { [ lsearch -exact $var(menulist) $submenu ] == -1 } {

	    #
	    # put it in the menulist
	    #
	    lappend var(menulist) $submenu
	}

	#
	# a new topic at least
	#
	lappend var(topiclist) $realval

	#
	# the submenu the topic belongs to
	#
	lappend var($submenu,list)  $realval
    }

    close $fileID
}

######################################################################
#
# makePasteWin 
#
######################################################################

proc makePasteWin { } {

    global textwin

    if [ winfo exists .text ] {

	wm deiconify .text
	return
    } 

    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
}

######################################################################
#
# setSel 
#
######################################################################

proc setSel { topic { what "" } } {

    global textwin var

    #
    # 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($var(lastmenu),check) 0
	    setSel $var(lastmenu) clear
	    $textwin tag remove sel 1.0 end
	} 	
    } $textwin
}

######################################################################
#
# paste 
#
######################################################################

proc paste { topic { what "" } } {

    global textwin var

    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 $var($topic,text)

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

    setSel $topic

    set submenu $var($topic,menu)

    #
    # focus this one
    #
    focus $var($submenu,menubtn)

    #
    # set the value of the last checkbutton 
    #
    catch { set var($var(lastmenu),check) 0 }
    
    #
    # set new value
    #
    set var($submenu,check) 1

    #
    # this one is now the last menu
    #
    set var(lastmenu)       $submenu
}

######################################################################
#
# delPasteText 
#
######################################################################

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
}

######################################################################
#
# makeHelpWin 
#
######################################################################

proc makeHelpWin {} {

    global helpexit

    catch { set helpexit 0 }

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

    } else {

	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 ; set helpexit 1 }
	
	pack .help.text \
		-side top \
		-expand yes -fill x
	
	pack .help.quit \
		-side top -fill x -expand yes
	
    }

    bind .help <Any-Return>    ".help.quit invoke"
    bind .help <Any-Key-space> ".help.quit invoke"
    
    bind all <Any-Tab>   { break }    
    bind all <Key-Right> { break }    
    bind all <Key-Left>  { break }    
    
    tkwait visibility .help
    
    set prevgrab  [ grab current ]
    set prevfocus [ focus ]
    grab .help

    focus .help.quit

    tkwait variable helpexit

    grab release .help

    focus $prevfocus
    grab .

    bind all <Tab>       { wanderFocus %W next; break }    
    bind all <Shift-Tab> { wanderFocus %W prev; break }    
    bind all <Key-Right> { wanderFocus %W next; break }    
    bind all <Key-Left>  { wanderFocus %W prev; break }    
}

set var(lastmenu) ""

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

readtemplate

######################################################################
#
# optmenuproc 
#
######################################################################

proc optmenuproc { item } {

    global var

    #
    # for easier handling
    #
    set submenu $var($item,menu)

    #
    # set label of menu
    #
    set var($submenu,val) "$var($item,label)"

    #
    # take this item for pasting
    #
    paste $item
}

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

set num 0
foreach submenu $var(menulist) {

    #
    # make a new menu for every category
    #
    frame .main.topics.$num
    
    #
    # consists of a checkbutton
    #
    set var($submenu,val) {}
    set var($submenu,cbtn) .main.topics.$num.cbtn
    checkbutton $var($submenu,cbtn) \
	    -text "$submenu" \
	    -width 13  -anchor w \
	    -variable var($submenu,check)
    $var($submenu,cbtn) configure -state disabled

    pack $var($submenu,cbtn) \
	    -side left -anchor w

    #
    # and an optionmenu
    #
    set var($submenu,menubtn) .main.topics.$num.menu
    optionmenu $var($submenu,menubtn) \
	    -relief raised -bd 2 \
	    -textvariable var($submenu,val) \
	    -width 35 \
	    -justify left \
	    -anchor w

    pack $var($submenu,menubtn)  \
	    -side left

    #
    # now make the submenu of the optionmenu
    #
    set var($submenu,list) [ lsort $var($submenu,list) ]

    foreach item $var($submenu,list) {

	$var($submenu,menubtn).menu add command \
		-label $var($item,label) \
		-command "optmenuproc $item" 
    }

    optmenuproc [ lindex $var($submenu,list) 0 ]

    pack .main.topics.$num

    incr num
}

optmenuproc [ lindex $var([ lindex $var(menulist) 0 ],list) 0 ]

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

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

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.template  configure -underline 0
.main.btns.anleitung configure -underline 0
.main.btns.quit      configure -underline 0

newFocus .main.btns.help
newFocus .main.btns.template
newFocus .main.btns.anleitung
newFocus .main.btns.quit

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"

bind all <Any-Tab>   { break }
bind all <Tab>       { wanderFocus %W next; break }    
bind all <Shift-Tab> { wanderFocus %W prev; break }    
bind all <Key-Right> { wanderFocus %W next; break }    
bind all <Key-Left>  { wanderFocus %W prev; break }    

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 
}


