Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

simple text editor

by cosecant (Monk)
on Nov 05, 2005 at 01:10 UTC ( #505924=sourcecode: print w/replies, xml ) Need Help??
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);
}
Replies are listed 'Best First'.
Re: simple text editor
by ghenry (Vicar) on Nov 05, 2005 at 07:46 UTC

    I like it!

    A little bug. If I select "Save-As" or "Open", but then cancel, I get

    Use of uninitialized value in concatenation (.) or string at ./tkedito +r line 62.print() on closed filehandle FILE_S at ./tkeditor line 64.

    Some kind of if defined should fix this. For example:

    60 sub SaveAs { 61 $save = $main->getSaveFile(-filetypes => $types, -initialfile + => $file); 62 if ( defined $save ) { 63 open(FILE_S, "> $save"); 64 my $content = $text->get('1.0', 'end'); 65 print FILE_S $content; 66 close(FILE_S); 67 } 68 else { 69 return; 70 } 71 }
    and
    Use of uninitialized value in concatenation (.) or string at ./tkedito +r line 76. readline() on closed filehandle FILE_O at ./tkeditor line 77.
    with the same kind of fix.

    But you already knew that :-)

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!
Re: simple text editor
by jdporter (Canon) on Nov 06, 2005 at 20:28 UTC

    Cool! That has inspired me to write a "minimal" Tk editor. This punts on everything from file reading/writing to appearance control.

    =pod This program reads text from "stdin" and writes to "stdout" on exit. It can be used to do in-place editing of a file by executing with perl + -i.bak If you exit via 'OK' or 'Cancel', the process exit code will be 0 or 1 +, respectively. If you exit with 'Cancel' and are NOT running with perl -i, the text w +ill not be printed to "stdout". Examples: % foo | perl stream_editor_tk | bar # foo produces text; # stream_editor_tk is this program; # bar consumes text. % perl -i.bak stream_editor_tk some_doc.txt # edit a file in place. =cut use Tk; use strict; use warnings; my $orig = $_ = do { local $/; <> }; my $code = 1; my $mw = new MainWindow( -title => 'Edit' ); my $fr = $mw->Frame( -border => 2, -relief => 'raised' )->pack( -side +=> 'bottom', -fill => 'both', ); my $text = $mw->Scrolled( 'Text', -wrap => 'none' )->pack( -side => 'b +ottom', -fill => 'both', -expand => 1 ); $fr->Button( -text => 'Reset', -command => sub { $text->delete( '1.0', + 'end' ); $text->insert( end => $_ ) } )->place( -relx => 0.25, -anch +or => 'n', ); my $ok = $fr->Button( -text => 'OK', -command => sub { print $text->ge +t( '1.0', 'end' ); $code = 0; $mw->destroy } )->place( -relx => 0.5, +-anchor => 'n', ); $fr->Button( -text => 'Cancel', -command => sub { defined $^I and prin +t $orig; $mw->destroy } )->place( -relx => 0.75, -anchor => 'n', ); $mw->after( 100, sub { $fr->configure( -height => $ok->height + 5 ); } + ); $text->insert( end => $_ ); MainLoop; exit $code;
    We're building the house of the future together.
      That has inspired me to write a "minimal" Tk editor.

      You can probably get somewhat closer to "minimal" (depending on your def'n) using Tk::TextUndo. Something like this will get you most of the way there:

      perl -MTk -MTk::TextUndo -e "(tkinit)->Scrolled('TextUndo')->pack;Main +Loop"
      Right click for a menu. Some menu items require implementation.

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: simple text editor
by tcc (Novice) on Nov 06, 2005 at 09:33 UTC
    Nice text editor! I love it. I dont know if its me but the text is a little small other wise its great.
Re: simple text editor
by cosecant (Monk) on Nov 05, 2005 at 20:40 UTC
    There is also a problem when I hit Ctl-s; it prints some unreadable character in windows and on linux it prints  If anyone knows a way to fix that I would be very appreciative.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://505924]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2020-09-23 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (132 votes). Check out past polls.

    Notices?