#!/usr/bin/perl

use strict;
use warnings;

use Gtk2 -init;
use Cairo;

my $area = Gtk2::DrawingArea->new();
my $cairo;
my $xx=0;
my $pixmap;
my ($pixw, $pixh, $pixd) = (-1, -1, -1);
sub draw() {
	my $win = $area->window;
	my $alloc = $area->allocation;
	my $w = $alloc->width;
	my $h = $alloc->height;
	my $d = $win->get_depth;
	# print "Area $w x $h x $d, xx=$xx\n";

	if (!defined($pixmap) || $pixw != $w || $pixh != $h || $pixd != $d) {
		$pixmap = Gtk2::Gdk::Pixmap->new($win, $w, $h, $d);
		($pixw, $pixh, $pixd) = ($w, $h, $d);
		$cairo = Gtk2::Gdk::Cairo::Context->create($pixmap);
	}

	$cairo->rectangle(0, 0, $w, $h);
	$cairo->set_source_rgb(0, 0, 0);
	$cairo->fill;

	$cairo->rectangle($xx, 10, 40, 40);
	$cairo->set_source_rgb(0, 0, 1);
	$cairo->fill;

	#my $gc = Gtk2::Gdk::GC->new($pixmap);
	#$gc->set_rgb_background(0x000000);
	#$gc->set_rgb_foreground(0x00ff00);
	#$pixmap->draw_line($gc, $xx, 0, $w-1, $h-1);

	my $wgc = Gtk2::Gdk::GC->new($win);
	$win->draw_drawable($wgc, $pixmap, 0, 0, 0, 0, $w, $h);

	$xx+=1;
}

my $timer;
$area->signal_connect("expose-event" => sub {
	draw();
	if (!defined $timer) {
		$timer = Glib::Timeout->add(10, sub { draw(); return 1; });
	}
});
$area->set_flags('can-focus');
# $area->add_events('key-press-mask');
$area->signal_connect('key-press-event' => sub {
	my ($w, $evt) = @_;
	my $k = Gtk2::Gdk::keyval_name(0, $evt->keyval);
	print "Pressed key $k\n";
	if ($k eq "Escape") {
		Gtk2->main_quit;
	}
});

my $window = Gtk2::Window->new ('toplevel');
$window->signal_connect ("delete-event" => sub { Gtk2->main_quit });
$window->set_title("Brum");
$window->set_wmclass("anim", "Anim");
$window->set_default_size(640, 480);
$window->add ($area);
$window->show_all;
$window->fullscreen;

Gtk2->main;
