#!/usr/bin/perl -w
use
lib
"$FindBin::RealBin/../lib"
;
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;
}
open
STDERR,
">/dev/null"
;
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
}
]
}
);
my
$cui
= new Curses::UI (
-clear_on_exit
=> 1
);
my
$menu
=
$cui
->add(
'menu'
,
'Menubar'
,
-menu
=> \
@menu
,
);
my
$screen
=
$cui
->add(
'screen'
,
'Window'
,
-padtop
=> 1,
-border
=> 0,
-ipad
=> 0,
);
my
$editor
=
$screen
->add(
'editor'
,
'TextEditor'
,
-border
=> 1,
-padtop
=> 0,
-padbottom
=> 3,
-showlines
=> 0,
-sbborder
=> 0,
-vscrollbar
=> 1,
-hscrollbar
=> 1,
-showhardreturns
=> 0,
-wrapping
=> 0,
-text
=>
$text
,
);
$editor
->clear_binding(
'loose-focus'
);
$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"
,
);
sub
open_dialog()
{
my
$file
=
$cui
->loadfilebrowser(
-file
=>
$currentfile
,
);
if
(
defined
$file
)
{
if
(
open
F,
"<$file"
) {
my
$text
=
""
;
while
(<F>) {
$text
.=
$_
}
close
F;
$editor
->text(
$text
);
$editor
->cursor_to_home;
$currentfile
=
$file
;
}
else
{
$cui
->error(
-message
=>
"Can't read file \"$file\":\n$!"
);
}
}
}
sub
save_dialog()
{
my
$file
=
$cui
->savefilebrowser(
-file
=>
$currentfile
,
);
return
unless
defined
$file
;
if
(
open
F,
">$file"
) {
print
F
$editor
->text;
if
(
close
F) {
$cui
->dialog(
-message
=>
"File \"$file\"\nsuccessfully saved"
);
$currentfile
=
$file
;
}
else
{
$cui
->error(
-message
=>
"Error on closing file \"$file\":\n$!"
);
}
}
else
{
$cui
->error(
-message
=>
"Can't write to $file:\n$!"
);
}
}
sub
about_dialog()
{
$cui
->dialog(
-title
=>
'About editor'
,
-message
=>
"Program : Curses::UI Editor\n"
.
"Author : Maurice Makaay\n"
.
"\n"
.
"The sole purpose of this editor\n"
.
"is the demonstration of my perl\n"
.
"Curses::UI widget set."
);
}
sub
exit_dialog()
{
my
$return
=
$cui
->dialog(
-title
=>
"Are you sure???"
,
-buttons
=> [
'yes'
,
'no'
],
-message
=>
"Do you really want to quit?"
);
exit
(0)
if
$return
;
}
$cui
->set_binding(\
&exit_dialog
,
"\cQ"
,
"\cC"
);
$cui
->set_binding(\
&save_dialog
,
"\cS"
);
$cui
->set_binding(\
&open_dialog
,
"\cO"
);
$cui
->set_binding(
sub
{
shift
()->getobj(
'menu'
)->focus},
"\cX"
, KEY_F(10));
$cui
->set_binding(
sub
{
my
$cui
=
shift
;
$cui
->layout;
$cui
->draw;
},
"\cL"
);
$editor
->focus;
$cui
->mainloop;