Commits

Herbert Breunung committed 5c7ac4f

copy over from sed to med

Comments (0)

Files changed (1)

 use warnings;
 
 package MediumEdit;
-use Wx qw/ :everything /;
-use base qw(Wx::App);
-use Kephra::App::Editor;
+our $file = __FILE__;
+our $encoding;
 
 sub OnInit {
-	my $app   = shift;
-	my $frame = Wx::Frame->new( undef, wxDEFAULT, 'Servus Welt');
-	my $ed = Kephra::App::Editor->new($frame, -1);
-	Wx::Window::SetFocus( $ed );
+        my $app   = shift;
+        my $frame = Wx::Frame->new( undef, -1, __PACKAGE__ . 'Kephra xp testbed', [-1,-1], [1000,800]);
+        my $ed = Kephra::App::Editor->new($frame, -1);
+        Wx::Window::SetFocus( $ed );
 
-	$frame->Show(1);
-	$app->SetTopWindow($frame);
-	1;
+        Wx::Event::EVT_KEY_DOWN($ed , sub {
+                my ($ed, $event) = @_;
+                my $code = $event->GetUnicodeKey;
+                if ($code == 79 and $event->ControlDown){
+                	$file = get_file_open();
+                	$ed->SetText( read_file($file) );
+                }
+                elsif ($code == 81 and $event->ControlDown){ $frame->Close }
+                elsif ($code == 83 and $event->ControlDown){
+                	write_file( $file, $ed->GetText() );
+                } 
+                else { $event->Skip }
+        });
+		$ed->SetText( read_file($file) );
+        
+        $frame->Center();
+        $frame->Show(1);
+        $app->SetTopWindow($frame);
+        1;
 }
 
+sub get_file_open {
+	my $title  = shift // 'Open File ...';
+	my $dir    = shift // '.';
+	my $filter = shift // '(*)|*';
+	my $parent = shift // undef;
+	Wx::FileSelector( $title, $dir, '', '', $filter, &Wx::wxFD_OPEN, $parent);
+}
+
+sub read_file {
+	my $file = shift;
+	return unless $file and -r $file;
+	CORE::open my $FH, '<', $file;
+	binmode($FH);
+	my $content = do { local $/; <$FH> };
+	if ($content) {
+		my $guess = Encode::Guess::guess_encoding( $content, qw/utf-8 iso8859-1 latin1/ );
+		if ( ref($guess) and ref($guess) =~ m/^Encode::/ ) {
+			$encoding = $guess->name;
+		} elsif ( $guess =~ m/utf8/ ) {
+			$encoding = 'utf-8';
+		} elsif ( $guess =~ m/or/ ) {
+			my @suggest_encodings = split /\sor\s/, $guess;
+			$encoding = $suggest_encodings[0];
+		} else { $encoding = 'utf-8' }
+		return Encode::decode( $encoding,  $content );		
+	}
+}
+
+sub write_file {
+	my ($file, $content) = @_;
+	return unless $file and -w $file;
+	CORE::open my $FH, '> :raw :encoding('.$encoding.')', $file;
+	print $FH $content;
+	print $file , $encoding;
+}
 
 package main;
 MediumEdit->new->MainLoop;