|
Programmer
Join Date: May 2005
Location: Minnesota
Posts: 42
Rep Power: 0 
|
[perl] graphical text editor
This is a perl graphical text editor I have been working on. It uses the Tk has very simple functionality so far including a very basic find feature, and runs fine on both windows and linux (and probably other OS's that run perl).
I am hoping to incorporate more features without losing its portability and any constructive criticism would be helpful.
#! /usr/bin/perl use warnings; use strict; use Tk; use Tk::DialogBox; my $file; my $save; my $copy; my $background = 'white'; my $foreground = 'black'; my $font = "system, 10"; if ($^O eq 'MSWin32') { require Win32::Console; Win32::Console::Free(); } else { fork && exit; } my $main = new MainWindow; $main->title("Glimsdale's Text Editor: $file"); 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", -command => \&New); $file_menu->command(-label => "Open", -command => \&Open); $file_menu->command(-label => "Save", -command => \&Save); $file_menu->command(-label => "Save As", -command => \&SaveAs); $file_menu->command(-label => "Exit", -command => \&Exit); my $text = $main->Text(-background => $background, -foreground => $foreground, -font => $font)->pack(-fill => "both", -expand => 1); my $sb = $main->Button(-text => 'Save', -command => \&Save)->pack(-side => 'left'); my $ob = $main->Button(-text => 'Open', -command => \&Open)->pack(-side => 'left'); my $nb = $main->Button(-text => 'New', -command => \&New)->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'); my $exit_dialog = $main->DialogBox( -title => "Exit", -buttons => [ "Yes", "No" ] ); $exit_dialog->add("Label", -text => "Exit?")->pack(); my $about_dialog = $main->DialogBox( -title => "Exit" , -buttons => [ "Ok" ]); $about_dialog->add("Label", -text => "Glimsdale's Text Editor\nv.06")->pack(); my $types = [['All Files', '*'], ['Perl files', '.pl'], ['Text Files', '.txt']]; if ($file = shift) { $main->title("Glimsdale's Text Editor: $file"); open(FILE_O, "< $file"); foreach my $line (<FILE_O>) { $text->insert('end', $line); } } MainLoop; sub SaveAs { $save = $main->getSaveFile(-filetypes => $types, -initialfile => $file); if (defined $save) { open(FILE_S, "> $save"); my $content = $text->get('1.0', 'end'); print FILE_S $content; close(FILE_S); } else { return; } } sub Open { my $open = $main->getOpenFile(-filetypes => $types); if (defined $open) { $file = $open; undef($save) if defined($save); $main->title("Glimsdale's Text Editor: $file"); $text->delete('1.0', 'end'); open(FILE_O, "< $open"); foreach my $line (<FILE_O>) { $text->insert('end', $line); } } else { return; } } sub Exit { my $button = $exit_dialog->Show(); if ($button eq "Yes") { exit; } } sub New { $text->delete('1.0', 'end'); undef($file) if defined($file); undef($save) if defined($save); $main->title("Glimsdale's Text Editor: $file"); } 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 FindText { my $pattern = $find_entry->get; $text->FindNext(-forward, -regexp, -nocase, $pattern); }
|