Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 7th, 2006, 12:47 AM   #1
glimmy
Programmer
 
glimmy's Avatar
 
Join Date: May 2005
Location: Minnesota
Posts: 42
Rep Power: 0 glimmy is on a distinguished road
Send a message via AIM to glimmy
[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.

perl Syntax (Toggle Plain Text)
  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. }
glimmy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OnlineTextEditor.Com! Sane Show Off Your Open Source Projects 43 Jun 16th, 2006 9:55 AM
Basic Text Editor Toro Show Off Your Open Source Projects 6 Jun 15th, 2006 11:33 PM
How to detect cursor location and insert text??? syntax-error C# 3 Jun 30th, 2005 2:42 AM
Text Editor : Loading Dll's Encryption C++ 0 Feb 27th, 2005 2:57 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:04 AM.

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