#!/usr/bin/perl -w
use strict;

# We do not want STDERR to clutter our screen.
open STDERR, ">/dev/null";

use FindBin;
use lib "$FindBin::RealBin/../lib";
use Curses::UI;
my $cui = new Curses::UI ( 
	-clear_on_exit => 1 
);

$cui->dialog(
	"In this demo you can see what the intellidraw() method\n"
      . "does for your application. In a moment you wil see three\n"
      . "windows, which are overlapping each other. Using <TAB>\n"
      . "you can switch between them. Using CTRL+Q you can exit\n"
      . "the demo.\n"
      . "\n"
      . "Each window has an entry on it. If the content of this\n"
      . "entry is changed, all titles of all windows and all entries\n"
      . "on all windows will be set accordingly. But only the title\n"
      . "(and the entry, but you can't see that) of the topmost\n"
      . "window is redrawn (if the title and the entry of other\n"
      . "windows were redraw, they would have cluttered the screen).\n"
      . "\n"
      . "So the lesson is: you can set parameters for widgets which\n"
      . "are on any window and the widget will figure out if it\n"
      . "should redraw or not by itself."
);

my $win1 = $cui->add(
	'win1', 'Window',
	-x => 2, -y => 2,
	-width => 30, -height => 10,
	-border => 1,
	-title => 'Window 1'
);

my $win2 = $cui->add(
	'win2', 'Window',
	-x => 6, -y => 6,
	-width => 30, -height => 10,
	-border => 1,
	-title => 'Window 2'
);

my $win3 = $cui->add(
	'win3', 'Window',
	-x => 10, -y => 10,
	-width => 30, -height => 10,
	-border => 1,
	-title => 'Window 3'
);

for my $n (1..3)
{
	my $win = $cui->getobj("win$n");
	my $entry = $win->add(
		'entry', 'TextEntry', 
		-x => 1, -y => 1, 
		-width => 26,		
		-sbborder => 1,
		-onChange => \&onchange_event
	);
	$entry->set_routine('return', 'LEAVE_CONTAINER');
	$win->returnkeys("\cQ", "\cN");
}
	
sub onchange_event
{
	my $entry = shift;
	my $cui = $entry->root;

	for my $n (1..3)
	{
		my $win = $cui->getobj("win$n");
		$win->title($entry->get);
		$win->getobj('entry')->text($entry->get);
	}
}


FOREVER: for(;;)
{
	foreach my $id ('win1','win2','win3')
	{
		my $win = $cui->getobj($id);

		$win->raise;
		$cui->draw;
		my ($return, $key) = $win->focus;

		last FOREVER if $key eq "\cQ";
	}
}



