#!/usr/bin/wish -f
# menu.tcl --
#
# This demonstration script creates a window with a bunch of menus
# and cascaded menus using menubars.
#
# RCS: @(#) $Id: menu.tcl,v 1.2 1998/09/14 18:23:29 stanton Exp $

set font "Arial"

global .w

set w .menu
catch {destroy $w}
toplevel $w
wm title $w "Latein lernen"
wm iconname $w "menu"
# positionWindow $w

label $w.question -font $font -wraplength 4i -justify center -width 50 -height 10
$w.question configure -text "Frage" -background "yellow" -foreground "red"
label $w.answer -font $font -wraplength 4i -justify center  -width 50 -height 10
$w.answer configure -text "Antwort" -foreground "yellow" -background "orange"

pack $w.question -side top
pack $w.answer -side bottom

set menustatus "    "
global counter
frame $w.statusBar
label $w.statusBar.label -background "green" -foreground "orangered" -textvariable menustatus -relief sunken -bd 1 -font "Helvetica 10" -anchor w
label $w.statusBar.counter -foreground "blue" -background "orange" -textvariable counter -relief raised -bd 1 -font "Helvetica 10" -anchor e
pack $w.statusBar.label -side left -padx 2 -expand yes -fill both
pack $w.statusBar.counter -side right -padx 2 -expand no -fill both
pack $w.statusBar -side bottom -fill x -pady 2

frame $w.buttons -background "ghostwhite"
pack $w.buttons -side bottom -fill x -pady 2m
button $w.buttons.gewusst -text "Gewusst!" -command "gewusst" -background "orangered" -bitmap "info"
button $w.buttons.nachgedacht -text "Nachgedacht!" -command "nachgedacht" -background "ghostwhite" -bitmap "questhead"
button $w.buttons.verloren -text "Verloren!" -command "verloren" -background "#FF6733" -bitmap "error"
pack $w.buttons.gewusst $w.buttons.nachgedacht $w.buttons.verloren -side right -expand 1

menu $w.menu -tearoff 0 -background "#FF8800" -foreground "orangered"

set m $w.menu.file
menu $m -tearoff 0
$w.menu add cascade -label "Latein" -menu $m -underline 0
$m add command -label "ffnen ..." -command datei_oeffnen
$m add separator
$m add command -label "beenden ..." -command exit

$w configure -menu $w.menu -background "orangered"

bind Menu <<MenuSelect>> {
    global $menustatus
    if {[catch {%W entrycget active -label} label]} {
	set label "    "
    }
    set menustatus $label
    # set counter "Hallo"
    # pack $w.statusBar.counter
    update idletasks
}


# Sam Mr  2 20:18:27 MET 2002: alles heute, jetzt von GM wieder da:

proc datei_oeffnen { } {
  global fId
  global w
  global counter
  set name [ fileDialog $w name open ]
  # fileDialog $w name open
  set fId [ open $name r ]
  set counter [ exec "/bin/bash" "-c" "cat lektion71-u1-1.txt | wc -l" ]
  displayer_voc
}

proc displayer_voc { } {
  global fId
  global w
  global counter
  if { $counter == 0 } {
    tk_messageBox -icon info -message "Congratulations!" -type ok -parent $w
  } else {
    set c ""
    set cc 0
    set q ""
    global a
    set a ""
    while {$cc != 2} {
      set c [ read $fId 1 ]
      if {$c != "-"} { append q $c }
      if {$c == "-"} { set cc [ expr $cc + 1 ] }
    }
    set c [ read $fId 1 ]
    set c [ read $fId 1 ]
    while {$c != "-" && $c != "\n" } {
      set c [ read $fId 1 ]
      append a $c
    }
    $w.question configure -text $q
    $w.answer configure -text ""
    puts -nonewline $q
    puts -nonewline ": "
  }
}

proc gewusst { } {
  puts "Gewusst!"
  displayer_voc
}

proc verloren { } {
  puts "Verloren!"
  displayer_voc
}

proc nachgedacht { } {
  global a
  global w
  global counter
  $w.answer configure -text $a
  set counter [ expr $counter - 1 ]
}

proc fileDialog {w ent operation} {
    #   Type names		Extension(s)	Mac File Type(s)
    #
    #---------------------------------------------------------
    set types {
	{"Text files"		{.txt .doc}	}
	{"Text files"		{}		TEXT}
	{"Tcl Scripts"		{.tcl}		TEXT}
	{"C Source Files"	{.c .h}		}
	{"All Source Files"	{.tcl .c .h}	}
	{"Image Files"		{.gif}		}
	{"Image Files"		{.jpeg .jpg}	}
	{"Image Files"		""		{GIFF JPEG}}
	{"All files"		*}
    }
    if {$operation == "open"} {
	set file [tk_getOpenFile -filetypes $types -parent $w]
    } else {
	set file [tk_getSaveFile -filetypes $types -parent $w \
	    -initialfile Untitled -defaultextension .txt]
    }
    if [string compare $file ""] {
        return $file
#	$ent delete 0 end
#	$ent insert 0 $file
#	$ent xview end
    }
}

