#!/usr/bin/perl -w

# This module uses Gtk-Perl to display the flash window when page is changed.
# It understands 2 dynamical commands:
#     SendToModule /path/to/module-gtkflash format 'x = %d, y = %d'
#     SendToModule /path/to/module-gtkflash stop

use lib `fvwm-perllib dir`;
use FVWM::Module::Gtk;
use General::Parse;  # for getTokens()
use Gtk; 
init Gtk;

my $format = "(%d, %d)";
my $module = new FVWM::Module::Gtk(Mask => M_NEW_PAGE | M_STRING);

$module->addHandler(M_STRING, sub {
	my ($module, $event) = @_;
	my $line = $event->_text;
	my ($action, @args) = getTokens($line);

	if ($action eq "stop") {
		$module->terminate;
	} elsif ($action eq "format") {
		$format = $args[0];
	}
});

my $lastWindow = undef;
$module->addHandler(M_NEW_PAGE, sub {
	my ($module, $event) = @_;

	my $width  = $event->_vp_width; 
	my $height = $event->_vp_height;

	if (!$width || !$height) {
		# this may happen when doing DeskTopSize 1x1 on page 2 2
		return;
	}
	my $page_nx = int($event->_vp_x / $width); 
	my $page_ny = int($event->_vp_y / $height);

	# actually show the flash
	my ($w, $h) = (100, 50);
	my $string = sprintf($format, $page_nx, $page_ny);

	my $window = new Gtk::Window('toplevel');
	$window->set_title("FlashWindow");
	$window->set_border_width(5);
	$window->set_usize($w, $h);
	$window->set_uposition(($width - $w) / 2, ($height - $h) / 2);

	my $frame = new Gtk::Frame();
	$window->add($frame);
	$frame->set_shadow_type('etched_out');

	my $label = new Gtk::Label($string);
	$frame->add($label);
	$window->show_all;

	$lastWindow->destroy() if $lastWindow;
	$lastWindow = $window;

	# alarm is not called in perl-5.8.0, but called in earlier perls!..
	# probably some problem with Gtk-Perl and perl-5.8.0
	$SIG{ALRM} = sub {
		$lastWindow->destroy();
		$lastWindow = undef;
	};
	alarm(1);
});

$module->send("Style FlashWindow UsePPosition, NoTitle, NoHandles, BorderWidth 10, StaysOnTop, WindowListSkip, NeverFocus");

$module->eventLoop;
