#!/usr/bin/wish8.3
# 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

array set daten { }

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" -padx .3c -pady .3c
button $w.buttons.nachgedacht -text "Nachgedacht!" -command "nachgedacht" -background "ghostwhite" -bitmap "questhead" -padx .3c -pady .3c
button $w.buttons.verloren -text "Verloren!" -command "verloren" -background "#FF6733" -bitmap "error" -padx .3c -pady .3c
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 w
  global daten
  set name [ fileDialog $w name open ]
  #exec "/bin/cp" $name "/tmp/latein-lernen.txt"
  # fileDialog $w name open
  global counter
  set fId [ open $name r ]
  set c ""
  set n "0"
  set nul [ exec "/bin/bash" "-c" "cat $name | wc -l" ]
  while { $n < $nul } {
    set c ""
    puts "aaa"
    while { $c != "\n" } {
      set c [ read $fId 1 ]
      append daten($n) $c
      puts $c
      #tk_messageBox -icon info -message "Congratulations!" -type ok -parent $w
    }
    set n [ expr $n + 1 ]
  }
  set counter [ expr [ array size daten ] - 1 ]
  # $n
  displayer_voc
}

proc displayer_voc { } {
  global w
  global counter
  global daten
  if { $counter == -1 } {
    tk_messageBox -icon info -message "Congratulations!" -type ok -parent $w
  } else {
#    set c ""
#    set cc 0
    global q
    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
#    }
    set q [ string range $daten([ expr [ array size daten ] - 1]) 0 [ expr [ string first "--" $daten([ expr [ array size daten ] - 1])] - 1 ] ]
    set a [ string range $daten([ expr [ array size daten ] - 1]) [ expr [ string first "--" $daten([ expr [ array size daten ] - 1])] - 1 + 3 ] end ]
    $w.question configure -text $q
    $w.answer configure -text ""
    puts -nonewline $q
    puts -nonewline ": "
  }
}

proc gewusst { } {
  global counter
  global daten
  global w
  puts "Gewusst!"
  array unset daten [ expr [ array size daten ] - 1 ]
  set counter [ expr [ array size daten ] - 1 ]
  displayer_voc
}

proc verloren { } {
#  global a
#  global q
#  set i ""
#  append i $q "--" $a
#  global fId
#  puts $fId $i
  puts "Verloren!"
  global daten
  global counter
  set counter [ expr [ array size daten ] - 1 ]
  set l $daten(1)
  set daten(1) $daten($counter)
  set daten($counter) $l
  displayer_voc
}

proc nachgedacht { } {
  global a
  global w
  $w.answer configure -text $a
}

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
    }
}

