File Coverage

blib/lib/App/GUI/Notepad/Frame.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package App::GUI::Notepad::Frame;
2              
3 1     1   2086 use strict;
  1         2  
  1         35  
4 1     1   5 use File::Spec ();
  1         1  
  1         16  
5 1     1   5 use base qw/Wx::Frame/;
  1         1  
  1         746  
6             use Wx qw/:allclasses wxTE_MULTILINE wxID_OK wxSAVE wxOK wxCENTRE wxFONTENCODING_SYSTEM wxMODERN wxNORMAL wxNullColour wxSWISS wxTE_RICH/;
7             use Wx::Event qw/EVT_MENU/;
8              
9              
10             use App::GUI::Notepad::MenuBar;
11              
12             use vars qw{$VERSION};
13             BEGIN {
14             $VERSION = '0.01';
15             }
16              
17             # This is the constructor for the object.
18             # It creates:
19             # - the menubar
20             # - the textctrl which is where the text that is being edited is
21             # is stored
22             # - the status bar where non modal messages can be sent to the
23             # user
24             # It associates actions to the various menu options, sets the application
25             # icon and sets the font for displaying the file to a fixed width font.
26              
27             sub new {
28             my ($class) = shift;
29             my ($title, $position, $size) = @_;
30             my ($this) = $class->SUPER::new( undef, -1, $title, $position, $size );
31             $this->SetIcon( Wx::GetWxPerlIcon() );
32              
33             $this->{menubar} = App::GUI::Notepad::MenuBar->new();
34             $this->SetMenuBar( $this->{menubar}->menubar() );
35              
36             # Associate the various menu options with subroutines that are the
37             # actions to be carried out when the user clicks on the menu item.
38             #
39             # I wanted to do this in the MenuBar object but this function
40             # EVT_MENU need a reference to the frame and so they are called
41             # here
42              
43             EVT_MENU( $this, $this->{menubar}->{ID_NEW}, \&_menu_new );
44             EVT_MENU( $this, $this->{menubar}->{ID_OPEN}, \&_menu_open );
45             EVT_MENU( $this, $this->{menubar}->{ID_SAVE}, \&_menu_save );
46             EVT_MENU( $this, $this->{menubar}->{ID_SAVEAS}, \&_menu_saveas );
47             EVT_MENU( $this, $this->{menubar}->{ID_CLOSE}, \&_menu_close );
48             EVT_MENU( $this, $this->{menubar}->{ID_EXIT}, \&_menu_exit );
49             EVT_MENU( $this, $this->{menubar}->{ID_UNDO}, \&_menu_undo );
50             EVT_MENU( $this, $this->{menubar}->{ID_REDO}, \&_menu_redo );
51             EVT_MENU( $this, $this->{menubar}->{ID_CUT}, \&_menu_cut );
52             EVT_MENU( $this, $this->{menubar}->{ID_COPY}, \&_menu_copy );
53             EVT_MENU( $this, $this->{menubar}->{ID_PASTE}, \&_menu_paste );
54             EVT_MENU( $this, $this->{menubar}->{ID_ABOUT}, \&_menu_about );
55              
56             # Create the main text control
57              
58             $this->{textctrl} = Wx::TextCtrl->new(
59             $this,
60             -1,
61             "",
62             [ 0, 0 ],
63             [ 100, 100 ],
64             wxTE_MULTILINE | wxTE_RICH,
65             );
66              
67             # Set the font of the new text control to a fixed width font Courier New
68             # This font was chosen because of it's availablity on different platforms.
69              
70             my $font = Wx::Font->new(10, wxMODERN, wxNORMAL, wxNORMAL, 0, "Courier New");
71              
72             my $black = Wx::Colour->new(255,255,255);
73             my $white = Wx::Colour->new(0,0,0);
74             my $textattr = Wx::TextAttr->new($white, $black, $font);
75              
76             $this->{textctrl}->SetDefaultStyle($textattr);
77              
78              
79             #Create the statusbar
80              
81             $this->CreateStatusBar(2);
82              
83             return $this;
84             }
85              
86              
87             # This sub is called when the user clicks on menu File item New
88             # The textctrl and filename is cleared
89              
90             sub _menu_new {
91             my ($this) = @_;
92             $this->SetTitle("Perlpad");
93             $this->{textctrl}->SetValue("");
94             $this->{filename} = "";
95             return 1;
96             }
97              
98              
99             # This sub is called when the user clicks on menu File item Open
100             # The dialog that allows the user to choose a new file is displayed
101             # The text control is then instructed to load the contents of the file
102             # The title of the frame is altered to show the file's name
103              
104             sub _menu_open {
105             my ($this) = @_;
106             my $opendialog = Wx::FileDialog->new($this, "Choose a file to open",
107             "", "", "*.*", 0, [0,0]);
108             my $result = $opendialog->ShowModal();
109             if ($result == wxID_OK) {
110             $this->{filename} = File::Spec->catfile(
111             $opendialog->GetDirectory(), $opendialog->GetFilename()
112             );
113             $this->{textctrl}->LoadFile( $this->{filename} );
114              
115             $this->SetTitle( "Perlpad - " . $this->{filename} );
116             }
117             }
118              
119             # This sub is called when the user clicks on menu File item Exit
120             # It causes the editor to exit.
121             # TODO: Check if file was changed and ask if it needs to be saved.
122              
123             sub _menu_exit {
124             exit(0);
125             }
126              
127              
128             # This sub is called when the user clicks on menu File item Save
129             # The text control is instructed to save the file to the filename
130             # that we have as the name of the currently opened file.
131              
132             sub _menu_save {
133             my ($this) = @_;
134             $this->{textctrl}->SaveFile( $this->{filename} );
135             return 1;
136             }
137              
138             # This sub is called when the user clicks on menu File item Save As
139             # The Save As dialog is shown to allow the user to select a new filename
140             # A new path is constructed with File::Spec to allow for platform independance
141             # The text control is instructed to save the file to the filename
142             # The new filename is stored as the opened file's name
143              
144             sub _menu_saveas {
145             my ($this) = @_;
146             my $saveasdialog = Wx::FileDialog->new($this, "Choose a file name and " .
147             "location to save to",
148             "", "", "*.*",
149             wxSAVE, [0,0]);
150             my $result = $saveasdialog->ShowModal();
151             if ($result == wxID_OK) {
152             $this->{textctrl}->SaveFile(File::Spec->catfile(
153             $saveasdialog->GetDirectory(),
154             $saveasdialog->GetFilename()
155             )
156             );
157             $this->{filename} = File::Spec->catfile($saveasdialog->GetDirectory(),
158             $saveasdialog->GetFilename());
159             $this->SetTitle("Perlpad - " . $this->{filename});
160             }
161             }
162              
163              
164              
165              
166             # This sub is called when the user clicks on menu File item Close
167             # Does the same thing as File..New
168             # Clears on the text in the text control and changes the filename
169             # of the curently opened file to ''
170             # TODO: Check if file was changed and ask if it needs to be saved.
171             sub _menu_close{
172             my ($this) = @_;
173             $this->SetTitle("Perlpad");
174             $this->{textctrl}->SetValue("");
175             $this->{filename} = "";
176              
177             }
178              
179             # This sub is called when the user clicks on menu Edit item Undo
180             # Causes text control to undo the last edit
181             sub _menu_undo {
182             my ($this) = @_;
183             $this->{textctrl}->Undo();
184              
185             }
186              
187             # This sub is called when the user clicks on menu Edit item Redo
188             # Causes text control to redo the last edit ie Undo the undo. :-)
189              
190             sub _menu_redo {
191             my ($this) = @_;
192             $this->{textctrl}->Redo();
193             }
194              
195             # This sub is called when the user clicks on menu Edit item Cut
196             # Causes text control to redo the last edit ie Undo the undo. :-)
197              
198             sub _menu_cut {
199             my ($this) = @_;
200             $this->{textctrl}->Cut();
201             print "Cut\n";
202             #TODO: Put a message in the status "Cut text placed in clipboard"?
203             }
204              
205             # This sub is called when the user clicks on menu Edit item Copy
206              
207             sub _menu_copy{
208             my ($this) = @_;
209             $this->{textctrl}->Copy();
210             print "Copy\n";
211             #TODO: Put a message in the status "Copied text placed in clipboard"?
212             }
213              
214             # This sub is called when the user clicks on menu Edit item Paste
215              
216             sub _menu_paste{
217             my ($this) = @_;
218             $this->{textctrl}->Paste();
219             print "Paste\n";
220             #TODO: Put a message in the status "Copied text placed in clipboard"?
221             }
222              
223             # This sub is called when the user clicks on menu Help item About
224             # Displays information about the application in a modal dialog
225              
226             sub _menu_about{
227             my ($this) = @_;
228              
229             my $dialogtext = "Copyright 2005 by \n" .
230             "\tBen Marsh \n" .
231             "\tAdam Kennedy \n" .
232             "All rights reserved. You can redistribute and/or modify this \n" .
233             "bundle under the same terms as Perl itself\n\n" .
234             "See http://www.perl.com/perl/misc/Artistic.html";
235              
236             Wx::MessageBox($dialogtext, "About perlpad", wxOK|wxCENTRE, $this);
237             }
238              
239             1;