Curses-UI-POE

 view release on metacpan or  search on metacpan

examples/editor  view on Meta::CPAN

#!/usr/bin/perl -w

# ----------------------------------------------------------------------
# Script: editor
#
# (c) 2001-2002 by Maurice Makaay. All rights reserved.
# This file is part of Curses::UI::POE. Curses::UI::POE is free software.
# You can redistribute it and/or modify it under the same terms
# as perl itself.
#
# e-mail: maurice@gitaar.net
# ----------------------------------------------------------------------

use strict;
use Curses;
use Cwd;

# Use the libraries from the distribution, instead of 
# system wide libraries.
use FindBin;
use lib "$FindBin::RealBin/../lib";

# Load an initial file if an argument given on the command line.
# If the file can't be found, assume that this is a new file.
my $text = "";
my $currentfile = shift;
if (defined $currentfile and -f $currentfile) 
{
	open F, "<$currentfile" or die "Can't read $currentfile: $!\n";
	while (<F>) { $text .= $_ }
	$currentfile = $currentfile;
	close F;
}

# We don't want STDERR output to clutter the screen.
#
# Hint: If you need STDERR, write it out to a file and put 
# a tail on that file to see the STDERR output. Example:
#open STDERR, ">>/tmp/editor_errors.$$";
open STDERR, ">/dev/null";

# ----------------------------------------------------------------------
# Menu definition
# ----------------------------------------------------------------------

my @menu = (
  { -label => 'File', 
    -submenu => [
      { -label => 'Open file ^O', -value => \&open_dialog  },
      { -label => 'Save file ^S', -value => \&save_dialog  },
      { -label => 'Exit      ^Q', -value => \&exit_dialog  }
    ]
  },
  { -label => 'Help', 
    -submenu => [
      { -label => 'About editor', -value => \&about_dialog }
    ]
  } 
);

# ----------------------------------------------------------------------
# Create widgets
# ----------------------------------------------------------------------

# Create the root. Everything else will be built up from here.
use Curses::UI::POE;
my $cui = new Curses::UI::POE ( 
	-clear_on_exit => 1 
);

# Add the menu to the root.
my $menu = $cui->add(
	'menu','Menubar', 
	-menu => \@menu,
);

# Create the screen for the editor.
my $screen = $cui->add(
	'screen', 'Window',
	-padtop          => 1, # leave space for the menu
	-border		 => 0,
	-ipad		 => 0,
);

# We add the editor widget to this screen.
my $editor = $screen->add(
	'editor', 'TextEditor',
	-border 	 => 1,
	-padtop		 => 0,	
	-padbottom 	 => 3,
	-showlines	 => 0,
	-sbborder	 => 0,
	-vscrollbar	 => 1,
	-hscrollbar	 => 1,
	-showhardreturns => 0,
	-wrapping        => 0, # wrapping slows down the editor :-(
	-text		 => $text,
);

# There is no need for the editor widget to loose focus, so
# the "loose-focus" binding is disabled here. This also enables the
# use of the "TAB" key in the editor, which is nice to have.
$editor->clear_binding('loose-focus');

# Help information for the user. 
$screen->add(
	'help', 'Label',
	-y 	 	 => -2,
	-width		 => -1,
	-reverse 	 => 1,
	-paddingspaces   => 1,
	-text 	 	 => 
	      " ^Q Quit from the program ^S save file"
	    . " ^W toggle wrapping\n"
	    . " ^X Open the menu         ^O open file"
	    . " ^R toggle hard returns viewing",
);

# ----------------------------------------------------------------------
# Callback routines
# ----------------------------------------------------------------------
	
sub open_dialog()
{
	my $file = $cui->loadfilebrowser( 
		-file         => $currentfile,
	);

	if (defined $file) 
	{
		if (open F, "<$file") {
		    my $text = "";
		    while (<F>) { $text .= $_ }



( run in 0.580 second using v1.01-cache-2.11-cpan-39bf76dae61 )