#!/usr/bin/perl -w
# $RCSfile: textundo.pl $ $Revision: 1.1 $ $Date: 1999/11/20 07:46:01 $
use Tk;
use Tk::TextUndo;
# 定数
$filetypes = [["Text file", '.txt'],
	      ["All files",  '*'  ]];
#
$mw = MainWindow->new;
$mw->title("TextUndo");
# Scrolledでテキストアンドゥを定義
$txt = $mw->Scrolled('TextUndo',
		     -background => 'white',
		     -width      => 80,
		     -height     => 25,
		     -wrap       => 'word',
		     -scrollbars => 'e');
$txt->pack(-side   => 'left',
	   -fill   => 'both',
	   -expand => 'yes');
# メニューバーの定義
$menu = $mw->Menu();
# Toplevel $mw のオプション -menu を定義。
$mw->configure(-menu => $menu);
# メニューバーのエントリ
$menu->add('cascade',
	   -label     => "File",
	   -underline => 0);
$menu->add('cascade',
	   -label     => "Edit",
	   -underline => 0);
# Fileのプルダウンメニュー
$item1 = $menu->Menu();
$menu->entryconfigure("File", -menu => $item1);
#
$item1->add('command',
	    -label     => "Load",
	    -underline => 0,
	    -command   => \&LoadFile);
$item1->add('command',
	    -label     => "Save",
	    -underline => 0,
	    -command   => \&SaveFile);
$item1->add('command',
	    -label     => "Save As...",
	    -underline => 5,
	    -command   => \&SaveAsFile);
$item1->separator;
$item1->add('command',
	    -label     => "Exit",
	    -underline => 1,
	    -command   => sub { exit; });
# Editのプルダウンメニュー
$item2 = $menu->Menu();
$menu->entryconfigure("Edit", -menu => $item2);
#
$item2->add('command',
	    -label     => "Undo",
	    -underline => 0,
	    -command   => [$txt, 'undo']);
$item2->add('command',
	    -label     => "Redo",
	    -underline => 0,
	    -command   => [$txt, 'redo']);
$item2->separator;
$item2->add('command',
	    -label     => "Copy",
	    -underline => 0,
	    -command   => [$txt, 'clipboardCopy']);
$item2->add('command',
	    -label     => "Cut",
	    -underline => 2,
	    -command   => [$txt, 'clipboardCut']);
$item2->add('command',
	    -label     => "Paste",
	    -underline => 0,
	    -command   => [$txt, 'clipboardPaste']);
#
MainLoop;
# ファイルの読み込み
sub LoadFile {
    my $filename = $mw->getOpenFile(-filetypes        => $filetypes,
				    -defaultextension => '.txt');
    if (defined $filename) {
	$txt->Load($filename);
    }
}
# ファイルの保存
sub SaveFile {
    my $filename = $txt->FileName();
    if (defined $filename) {
	$txt->Save($filename);
    } else {
	&SaveAsFile;
    }
}
# 名前をつけて保存
sub SaveAsFile {
    my $filename = $mw->getSaveFile(-filetypes        => $filetypes,
				    -defaultextension => '.txt');
    if (defined $filename) {
	$txt->Save($filename);
    }
}
# ---
# textundo.pl
