Source

Kephra / lib / med.pl

#!usr/bin/perl
use v5.12;
use warnings;
BEGIN { unshift @INC, '.' }

MultiEdit->new->MainLoop;

package MultiEdit;
use base qw(Wx::App);

sub OnInit {
	my $app   = shift;
	my $frame = Kephra::App::Window->new();
	$frame->Center();
	$frame->Show(1);
	$app->SetTopWindow($frame);
	1;
}

package Kephra::App::Window;
use base qw(Wx::Frame);
use Kephra::API qw(warning);
use Kephra::App::Dialog;
use Kephra::App::Editor;
use Kephra::IO::LocalFile;

our ($file, $encoding, $content, $ed);

sub new {
	my($class, $parent) = @_;
	my $self = $class->SUPER::new( undef, -1, '', [-1,-1], [1000,800] );
	$self->CreateStatusBar(3);
	$self->SetStatusWidths(100, 50, -1);
	$ed = Kephra::App::Editor->new($self, -1);
	Wx::Window::SetFocus( $ed );

	Wx::Event::EVT_KEY_DOWN($ed , sub {
		my ($ed, $event) = @_;
		given (Kephra::API::KeyMap::definition_from_event($event)){
			when('ctrl+o'){ open_file( Kephra::App::Dialog::get_file_open() ) }
			when('ctrl+q'){ $self->Close }
			when('ctrl+s'){
				Kephra::IO::LocalFile::write( $file, $encoding, $ed->GetText() );
				$ed->SetSavePoint;
			} 
			default       { $event->Skip }
		}
	});
	Wx::Event::EVT_STC_UPDATEUI($ed, -1, sub {
		$self->SetStatusText( $ed->GetCurrentPos, 0);	
	});
	
	open_file(__FILE__);
	return $self;
}

sub open_file {
	my $candidate = shift;
	return unless $candidate and -r $candidate;
	($content, $encoding) = Kephra::IO::LocalFile::read( $file = $candidate );
	$ed->SetText( $content );
	$ed->EmptyUndoBuffer;
	$ed->SetSavePoint;
	$ed->GetParent->SetStatusText( $encoding, 1);
}

sub set_title {
	my ($self, $status) = @_;
	my $title = 'Multi Edit - Kephra xp testbed step 3 - ' . $file;
	$title .= ' *' if $status;
	$self->SetTitle($title);
}