http://qs321.pair.com?node_id=505924
Category: Gui programming
Author/Contact Info nglimsdale@gmail.com
Description: This is a simple text editor I wrote in Perl using Tk and Tk::DialogBox

#! /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)->pa
+ck (-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', -comm
+and => \&SaveAs);
$file_menu->command(-label => "Exit",-accelerator => 'Ctl+Q', -command
+ => \&Exit);

my $text = $main->Text(-background => $background, -foreground => $for
+eground, -font => $font)->pack(-fill => "both", -expand => 1);

my $fg = $main->Button(-text => 'Text Color', -command => \&FgColorCyc
+le)->pack(-side => 'left');
my $bg = $main->Button(-text => 'Bg Color', -command => \&BgColorCycle
+)->pack(-side => 'left');
my $font_button = $main->Button(-text => 'Font', -command => \&FontCha
+nge)->pack(-side => 'left');
my $find_entry = $main->Entry(-width => "20", -background => 'white')-
+>pack(-side => 'left');
my $search_button = $main->Button(-text => 'Find', -command => \&FindT
+ext)->pack(-side => 'left');

$main->bind('<Control-o>', [\&Open]);
$main->bind('<Control-s>', [\&Save]);
$main->bind('<Control-n>', [\&New]);
$main->bind('<Control-q>', [\&Exit]);
$main->bind('<Control-w>', [\&SaveAs]);
my $exit_dialog = $main->DialogBox( -title => "Exit", -buttons => [ "Y
+es", "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 (<FILE_O>) {
  $text->insert('end', $line);
 }
}

MainLoop;

sub SaveAs {
 $save = $main->getSaveFile(-filetypes => $types, -initialfile => $fil
+e);
 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 (<FILE_O>) {
  $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);
}