#! /usr/bin/perl use warnings; use strict; use Tk; use Tk::DialogBox; my $file; my $save; my $copy; my @colors = qw( black brown red orange yellow green blue purple gray white); my @fonts = qw(courier times helvetica ariel system); my $fi = 0; my $bi = 9; my $fonti = 1; my $background = $colors[$bi]; my $foreground = $colors [$fi]; my $font = $fonts[$fonti]; my $main = new MainWindow; $main->minsize( qw(300 300)); $main->title("Text Editor"); my $menubar = $main->Frame(-relief => "raised", -borderwidth => 2)->pack (-anchor => "nw", -fill => "x"); my $file_menu = $menubar->Menubutton(-text => "File")->pack(-side => "left"); $file_menu->command(-label => "New",-accelerator => 'Ctl+N', -command => \&New); $file_menu->command(-label => "Open",-accelerator => 'Ctl+O', -command => \&Open); $file_menu->command(-label => "Save",-accelerator => 'Ctl+S', -command => \&Save); $file_menu->command(-label => "Save As",-accelerator => 'Ctl+W', -command => \&SaveAs); $file_menu->command(-label => "Exit",-accelerator => 'Ctl+Q', -command => \&Exit); my $text = $main->Text(-background => $background, -foreground => $foreground, -font => $font)->pack(-fill => "both", -expand => 1); my $fg = $main->Button(-text => 'Text Color', -command => \&FgColorCycle)->pack(-side => 'left'); my $bg = $main->Button(-text => 'Bg Color', -command => \&BgColorCycle)->pack(-side => 'left'); my $font_button = $main->Button(-text => 'Font', -command => \&FontChange)->pack(-side => 'left'); my $find_entry = $main->Entry(-width => "20", -background => 'white')->pack(-side => 'left'); my $search_button = $main->Button(-text => 'Find', -command => \&FindText)->pack(-side => 'left'); $main->bind('', [\&Open]); $main->bind('', [\&Save]); $main->bind('', [\&New]); $main->bind('', [\&Exit]); $main->bind('', [\&SaveAs]); my $exit_dialog = $main->DialogBox( -title => "Exit", -buttons => [ "Yes", "No" ] ); $exit_dialog->add("Label", -text => "Exit?")->pack(); my $types = [ ['Perl files', '.pl'], ['All Files', '*'],]; if ($file = shift) { open(FILE_O, "< $file"); foreach my $line () { $text->insert('end', $line); } } MainLoop; sub SaveAs { $save = $main->getSaveFile(-filetypes => $types, -initialfile => $file); open(FILE_S, "> $save"); my $content = $text->get('1.0', 'end'); print FILE_S $content; close(FILE_S); } sub Open { $text->delete('1.0', 'end'); my $open = $main->getOpenFile(-filetypes => $types); open(FILE_O, "< $open"); foreach my $line () { $text->insert('end', $line); } } sub Exit { my $button = $exit_dialog->Show(); if ($button eq "Yes") { exit; } } sub New { $text->delete('1.0', 'end'); } sub Save { if ($save) { open(SAVE, "> $save"); my $content = $text->get('1.0', 'end'); print SAVE $content; close(SAVE); } elsif ($file) { open(SAVE, "> $file"); my $content = $text->get('1.0', 'end'); print SAVE $content; close(SAVE); } else { &SaveAs; } } sub FgColorCycle { $fi++; $fi = 0 if $fi > 9; $foreground = $colors[$fi]; $text->configure(-foreground => $foreground); } sub BgColorCycle { $bi++; $bi = 0 if $bi > 9; $background = $colors[$bi]; $text->configure(-background => $background); } sub FontChange { $fonti++; $fonti = 0 if $fonti > 4; $font = $fonts[$fonti]; $text->configure(-font => $font); } sub FindText { my $pattern = $find_entry->get; $text->FindNext(-forward, -regexp, -nocase, $pattern); }