| 1 | 1 | ▶ | use v5.12; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | package Kephra::App::Window; | ||
| 5 | 1 | ▶ | use base qw(Wx::Frame); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | use Kephra::App::Dialog; | ||
| 8 | use Kephra::App::Editor; | ||
| 9 | use Kephra::App::SearchBar; | ||
| 10 | use Kephra::App::ReplaceBar; | ||
| 11 | use Kephra::App::Window::Menu; | ||
| 12 | use Kephra::IO::LocalFile; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my ($class, $parent) = @_; | ||
| 16 | my $self = $class->SUPER::new( undef, -1, '', [-1,-1], [1200,1000] ); | ||
| 17 | $self->CreateStatusBar(3); | ||
| 18 | $self->SetStatusWidths(100, 150, -1); | ||
| 19 | $self->SetStatusBarPane(2); | ||
| 20 | |||
| 21 | $self->{'app'} = $parent; | ||
| 22 | $self->{'editor'} = Kephra::App::Editor->new($self, -1); | ||
| 23 | $self->{'searchbar'} = Kephra::App::SearchBar->new($self, -1); | ||
| 24 | $self->{'replacebar'} = Kephra::App::ReplaceBar->new($self, -1); | ||
| 25 | Kephra::App::Window::Menu::mount( $self ); | ||
| 26 | |||
| 27 | my $sizer = Wx::BoxSizer->new( &Wx::wxVERTICAL ); | ||
| 28 | $sizer->Add( $self->{'editor'}, 1, &Wx::wxEXPAND, 0); | ||
| 29 | $sizer->Add( $self->{'searchbar'}, 0, &Wx::wxGROW, 0); | ||
| 30 | $sizer->Add( $self->{'replacebar'}, 0, &Wx::wxGROW, 0); | ||
| 31 | |||
| 32 | $self->SetSizer($sizer); | ||
| 33 | |||
| 34 | Wx::Window::SetFocus( $self->{'editor'} ); | ||
| 35 | Wx::Event::EVT_KEY_DOWN( $self, sub { | ||
| 36 | my ($self, $event) = @_; | ||
| 37 | my $code = $event->GetKeyCode ; | ||
| 38 | if ($code == &Wx::WXK_F11) { $self->ShowFullScreen( not $self->IsFullScreen ) } | ||
| 39 | else { $event->Skip } | ||
| 40 | }); | ||
| 41 | Wx::Event::EVT_CLOSE( $self, sub { | ||
| 42 | my ($self, $event) = @_; | ||
| 43 | if ($self->{'editor'}->GetModify() and not exists $self->{'dontask'}){ | ||
| 44 | my $ret = Kephra::App::Dialog::yes_no_cancel( "\n".' save file ? '); | ||
| 45 | return if $ret == &Wx::wxCANCEL; | ||
| 46 | $self->save_file if $ret == &Wx::wxYES; | ||
| 47 | } | ||
| 48 | $event->Skip(1); | ||
| 49 | }); | ||
| 50 | Wx::Event::EVT_CLOSE( $self, sub { | ||
| 51 | $self->config->set_value( $self->{'file'}, 'file', 'open'); | ||
| 52 | $self->{'editor'}->save_config( $self->config ); | ||
| 53 | $self->{'searchbar'}->save_config( $self->config ); | ||
| 54 | $_[1]->Skip(1); | ||
| 55 | }); | ||
| 56 | |||
| 57 | # recreate starting state | ||
| 58 | $self->set_title(); | ||
| 59 | $self->{'searchbar'}->show(1); | ||
| 60 | $self->{'replacebar'}->show(0); | ||
| 61 | |||
| 62 | $self->read_file( $self->config->get_value('file', 'open') ); # open the last opened file | ||
| 63 | $self->{'editor'}->apply_config( $self->config ); | ||
| 64 | $self->{'searchbar'}->apply_config( $self->config ); | ||
| 65 | |||
| 66 | return $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub config {$_[0]{'app'}{'config'}} | ||
| 70 | |||
| 71 | sub new_file { | ||
| 72 | my $self = shift; | ||
| 73 | Kephra::App::Window::Menu::update_recent_files_menu( $self, $self->{'file'} ); | ||
| 74 | $self->{'file'} = ''; | ||
| 75 | $self->{'editor'}->new_text( '' ); | ||
| 76 | $self->{'encoding'} = 'utf-8'; | ||
| 77 | $self->set_title(); | ||
| 78 | $self->SetStatusText( $self->{'encoding'}, 1); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub open_file { | ||
| 82 | my ($self) = @_; | ||
| 83 | my $dir = Kephra::IO::LocalFile::dir_from_path( $self->{'file'} ); | ||
| 84 | my $file = Kephra::App::Dialog::get_file_open( $dir ); | ||
| 85 | return unless $file; | ||
| 86 | $self->read_file( $file ); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub reopen_file { $_[0]->read_file( $_[0]->{'file'}, 1) } | ||
| 90 | |||
| 91 | sub read_file { | ||
| 92 | my ($self, $file, $soft) = @_; | ||
| 93 | return unless defined $file and -r $file; | ||
| 94 | Kephra::App::Window::Menu::update_recent_files_menu( $self, $self->{'file'} ); | ||
| 95 | my ($content, $encoding) = Kephra::IO::LocalFile::read( $file ); | ||
| 96 | $self->{'encoding'} = $encoding; | ||
| 97 | $self->{'editor'}->new_text( $content, $soft ); | ||
| 98 | $self->{'file'} = $file; | ||
| 99 | $self->set_title(); | ||
| 100 | $self->SetStatusText( $self->{'encoding'}, 1); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub save_file { | ||
| 104 | my $self = shift; | ||
| 105 | unless (exists $self->{'file'} and -r $self->{'file'}){ | ||
| 106 | my $file = Kephra::App::Dialog::get_file_save( ); | ||
| 107 | return if $file eq &Wx::wxID_CANCEL; | ||
| 108 | $self->{'file'} = $file; | ||
| 109 | } | ||
| 110 | Kephra::IO::LocalFile::write( $self->{'file'}, $self->{'encoding'}, $self->{'editor'}->GetText() ); | ||
| 111 | $self->{'editor'}->SetSavePoint; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub save_as_file { | ||
| 115 | my $self = shift; | ||
| 116 | my $dir = Kephra::IO::LocalFile::dir_from_path( $self->{'file'} ); | ||
| 117 | my $file = Kephra::App::Dialog::get_file_save( $dir ); | ||
| 118 | return unless $file; | ||
| 119 | $self->{'file'} = $file; | ||
| 120 | Kephra::IO::LocalFile::write( $self->{'file'}, $self->{'encoding'}, $self->{'editor'}->GetText() ); | ||
| 121 | $self->{'editor'}->SetSavePoint; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub save_under_file { | ||
| 125 | my $self = shift; | ||
| 126 | my $dir = Kephra::IO::LocalFile::dir_from_path( $self->{'file'} ); | ||
| 127 | my $file = Kephra::App::Dialog::get_file_save( $dir ); | ||
| 128 | return unless $file; | ||
| 129 | Kephra::IO::LocalFile::write( $file, $self->{'encoding'}, $self->{'editor'}->GetText() ); | ||
| 130 | $self->{'editor'}->SetSavePoint; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub set_title { | ||
| 134 | my ($self) = @_; | ||
| 135 | my $title .= $self->{'file'} ? $self->{'file'} : '<unnamed>'; | ||
| 136 | $title .= " - Kephra"; | ||
| 137 | $title = '* '.$title if $self->{'editor'}->GetModify(); | ||
| 138 | $self->SetTitle( $title ); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub toggle_full_screen { | ||
| 142 | my ($self) = @_; | ||
| 143 | $self->ShowFullScreen( not $self->IsFullScreen ); | ||
| 144 | $self->GetMenuBar->Check(16410, $self->IsFullScreen); | ||
| 145 | } | ||
| 146 | |||
| 147 | 1; | ||
| 148 | |||