#!/usr/bin/wish # einfacher Editor mit Undo/Redo package require Tk # GUI erzeugen text .text -bg white -undo yes -wrap word \ -yscrollcommand ".vscroll set" \ -xscrollcommand ".hscroll set" scrollbar .hscroll -orient horizontal -command ".text xview" scrollbar .vscroll -orient vertical -command ".text yview" label .status -font 8 -textvariable ::currentFile grid .text .vscroll -sticky nesw grid .hscroll -sticky ew grid .status -sticky ew grid rowconfigure . 0 -weight 1 grid rowconfigure . 1 -weight 0 grid rowconfigure . 2 -weight 0 grid columnconfigure . 0 -weight 1 grid columnconfigure . 1 -weight 0 # Menü erzeugen menu .menu . configure -menu .menu menu .menu.file -tearoff 0 .menu add cascade -menu .menu.file -label "Datei" .menu.file add command -label "Öffnen" -command openFile .menu.file add command -label "Speichern unter..." -command saveFileAs .menu.file add separator .menu.file add command -label "Beenden" -command {exit 0} menu .menu.edit -tearoff 0 .menu add cascade -menu .menu.edit -label "Edit " .menu.edit add command -label "Rückgängig machen " \ -command {.text edit undo} .menu.edit add command -label "Wiederherstellen " \ -command {.text edit redo} .menu.file add separator .menu.edit add command -label "Euro-Symbol einfügen" \ -command {.text insert "insert" \u20ac} .menu.edit add command -label "Zeile löschen" \ -command {.text delete "insert linestart" "insert lineend"} .menu.edit add command -label "Zum Zeilenanfang" \ -command {.text mark set insert "insert linestart"} set dateitypen { {"Textdateien" {.txt} } {"Tcl-Skripte" {.tcl} } {"C-Dateien" {.c .h} } {"Alle Dateien" *} } proc openFile {} { while {true} { set fileName [tk_getOpenFile -filetypes $::dateitypen] if {[string length $fileName] == 0} { break } elseif {[file exists $fileName] && [file readable $fileName]} { set fd [open $fileName r] .text delete 1.0 end .text insert 1.0 [read $fd] close $fd set ::currentFile $fileName break } else { tk_messageBox -icon error -title "Lesefehler" \ -message "Datei «$fileName» kann nicht gelesen werden" } } } proc saveFileAs {} { while {true} { set fileName [tk_getSaveFile -filetypes $::dateitypen \ -initialfile [file tail $::currentFile]] if {[string length $fileName] == 0} { break } elseif {[catch { set fd [open $fileName w] puts -nonewline $fd [.text get 1.0 "end -1c"] close $fd } err]} { set antwort [tk_messageBox -type retrycancel \ -icon error -title "Fehler" \ -message "Beim Abspeichern trat ein Fehler auf:\n$err"] if {[string match "cancel" $antwort]} { break } set $fileName "" } else { set ::currentFile $fileName break } } }