#!/usr/bin/perl -w
######################################################################
# editor.pl
######################################################################
# Perl Power! - Michael Schilli 1998
######################################################################

use Tk;
use Fs;
use strict;

my $file;

my $top = MainWindow->new;

# define two file selectors
my $fsload = Fs->new($top, \&loadcallback);
my $fssave  = Fs->new($top, \&savecallback);

# specify start directory
chop(my $startdir = `pwd`);

# build menus
my $menu = $top->Frame(-relief => 'raised', -bd => 2);

my $menu_file    = $menu->Menubutton(-text => "File");
my $menu_options = $menu->Menubutton(-text => "Options");

$menu_file->command(-label => 'Load', 
                    -command => sub { $fsload->start($startdir)});
$menu_file->command(-label => 'Save', 
                  -command => sub { savecallback($file) });
$menu_file->command(-label => 'Save As', 
                    -command => sub { $fssave->start($startdir)});
$menu_file->command(-label => 'Quit', 
                    -command => sub { destroy $top });

# Option meu radio buttons
my $wrap_mode = "none";

$menu_options->radiobutton(-label => "No wrap", 
                           -variable => \$wrap_mode, 
                           -value => "none",
			   -command => \&set_wrap);

$menu_options->radiobutton(-label => "Char wrap", 
                           -variable => \$wrap_mode, 
                           -value => "char",
			   -command => \&set_wrap);

$menu_options->radiobutton(-label => "Word wrap", 
                           -variable => \$wrap_mode, 
                           -value => "word",
			   -command => \&set_wrap);

# editable text field with scrollbar
my $text = $top->Text(-borderwidth => 2, -setgrid => 1);
my $scrollbar = $top->Scrollbar(-command => [yview => $text]);
$text->configure(-yscrollcommand => [set => $scrollbar]);

# pack all
$menu->pack(-side => 'top', -fill => 'x');
$scrollbar->pack(-side => 'right', -fill => 'both');
$text->pack(-side => 'left', -fill => 'both', -expand => 'yes');
$menu_file->pack(-side, 'left');
$menu_options->pack(-side, 'left');

MainLoop;

######################################################################
# Load file into the text widget: loadcallback($file);
######################################################################
sub loadcallback {
  $file = shift;

  return unless defined $file;

  open(FILE, $file) || return 0;

  $text->delete("1.0", "end");

  while(<FILE>)
  {  $text->insert("end", $_);
  }
  close(FILE);
  set_wrap();
}

######################################################################
# Save text of the text widgets: savecallback($file);
######################################################################
sub savecallback {
  $file = shift;

  return unless defined $file;

  open(FILE, ">$file") || return 0;
  my @lines = $text->get("1.0", "end");
  print FILE @lines;
  close(FILE);
}

######################################################################
# Set wrap mode (callback)
######################################################################
sub set_wrap {
    $text->configure(-wrap, $wrap_mode);
}

