#! /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);
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.