#! /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); }

In reply to simple text editor by cosecant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.