Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Existing Project Development (http://www.programmingforums.org/forum51.html)
-   -   [perl] graphical text editor (http://www.programmingforums.org/showthread.php?t=10975)

glimmy Aug 6th, 2006 11:47 PM

[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. :D

:

  1. #! /usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Tk;
  5. use Tk::DialogBox;
  6. my $file;
  7. my $save;
  8. my $copy;
  9.  
  10.  
  11. my $background = 'white';
  12. my $foreground'black';
  13. my $font = "system, 10";
  14.  
  15. if ($^O eq 'MSWin32') {
  16.         require Win32::Console;
  17.         Win32::Console::Free();
  18. } else {
  19.         fork && exit;
  20. }
  21.  
  22. my $main = new MainWindow;
  23. $main->title("Glimsdale's Text Editor: $file");
  24.  
  25. my $menubar = $main->Frame(-relief => "raised", -borderwidth => 2)->pack (-anchor => "nw", -fill  => "x");
  26. my $file_menu = $menubar->Menubutton(-text => "File")->pack(-side => "left");
  27.  
  28. $file_menu->command(-label => "New", -command => \&New);
  29. $file_menu->command(-label => "Open", -command => \&Open);
  30. $file_menu->command(-label => "Save", -command => \&Save);
  31. $file_menu->command(-label => "Save As", -command => \&SaveAs);
  32. $file_menu->command(-label => "Exit", -command => \&Exit);
  33.  
  34. my $text = $main->Text(-background => $background, -foreground => $foreground, -font => $font)->pack(-fill => "both", -expand => 1);
  35.  
  36. my $sb = $main->Button(-text => 'Save', -command => \&Save)->pack(-side => 'left');
  37. my $ob = $main->Button(-text => 'Open', -command => \&Open)->pack(-side => 'left');
  38. my $nb = $main->Button(-text => 'New', -command => \&New)->pack(-side => 'left');
  39. my $find_entry = $main->Entry(-width => "20", -background => 'white')->pack(-side => 'left');
  40. my $search_button = $main->Button(-text => 'Find', -command => \&FindText)->pack(-side => 'left');
  41.  
  42. my $exit_dialog = $main->DialogBox( -title => "Exit", -buttons => [ "Yes", "No" ] );
  43. $exit_dialog->add("Label", -text => "Exit?")->pack();
  44.  
  45. my $about_dialog = $main->DialogBox( -title => "Exit" , -buttons => [ "Ok" ]);
  46. $about_dialog->add("Label", -text => "Glimsdale's Text Editor\nv.06")->pack();
  47.  
  48. my $types = [['All Files''*'], ['Perl files', '.pl'], ['Text Files', '.txt']];
  49.  
  50. if ($file = shift) {
  51. $main->title("Glimsdale's Text Editor: $file");
  52. open(FILE_O, "< $file");
  53. foreach my $line (<FILE_O>) {
  54.   $text->insert('end', $line);
  55. }
  56. }
  57.  
  58. MainLoop;
  59.  
  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. } else {
  68.   return;
  69. }
  70. }
  71.  
  72. sub Open {
  73. my $open = $main->getOpenFile(-filetypes => $types);
  74. if (defined $open) {
  75.   $file = $open;
  76.   undef($save) if defined($save);
  77.   $main->title("Glimsdale's Text Editor: $file");
  78.   $text->delete('1.0', 'end');
  79.   open(FILE_O, "< $open");
  80.   foreach my $line (<FILE_O>) {
  81.   $text->insert('end', $line);
  82.   }
  83.   } else {
  84.   return;
  85. }
  86. }
  87.  
  88. sub Exit {
  89. my $button = $exit_dialog->Show();
  90. if ($button eq "Yes") {
  91.   exit;
  92. }
  93. }
  94.  
  95. sub New {
  96. $text->delete('1.0', 'end');
  97. undef($file) if defined($file);
  98. undef($save) if defined($save);
  99. $main->title("Glimsdale's Text Editor: $file");
  100. }
  101. sub Save {
  102. if ($save) {
  103.   open(SAVE, "> $save");
  104.   my $content = $text->get('1.0', 'end');
  105.   print SAVE $content;
  106.   close(SAVE);
  107. }
  108. elsif ($file) {
  109.   open(SAVE, "> $file");
  110.   my $content = $text->get('1.0', 'end');
  111.   print SAVE $content;
  112.   close(SAVE);
  113. } else {
  114.   &SaveAs;
  115. }
  116. }
  117.  
  118. sub FindText {
  119. my $pattern = $find_entry->get;
  120. $text->FindNext(-forward, -regexp, -nocase, $pattern);
  121. }



All times are GMT -5. The time now is 1:32 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC