#!/usr/bin/perl
use warnings;
use strict;
use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

my $window = Gtk2::Window->new('toplevel');
$window->set_title('Select Region');
$window ->signal_connect( 'destroy' => \&delete_event );
$window->set_border_width(10);
$window->set_default_size(300,200);

my $vbox = Gtk2::VBox->new( FALSE, 6 );
$vbox->set_size_request(0,0);

$window->add($vbox);
$vbox->set_border_width(2);

my $hbox= Gtk2::HBox->new( FALSE, 6 );
$vbox->pack_end($hbox,FALSE,FALSE,0);
$hbox->set_border_width(2);

$vbox->pack_end (Gtk2::HSeparator->new, FALSE, FALSE, 0);

my $button = Gtk2::Button->new_from_stock('gtk-quit');
$hbox->pack_end( $button, FALSE, FALSE, 0 );
$button->signal_connect( clicked => \&delete_event );

my $button1 = Gtk2::Button->new_from_stock('Select Area');
$vbox->pack_start( $button1, FALSE, FALSE, 0 );
$button1->signal_connect( clicked => \&grab );

$window->show_all();
Gtk2->main;
#####################################
sub delete_event {
	Gtk2->main_quit;
	return FALSE;
}  

sub grab{

my $root = Gtk2::Gdk->get_default_root_window;
my $disp = Gtk2::Gdk::Display->get_default;

my $white = Gtk2::Gdk::Color->new (65535, 65535, 65535);

my $gc = Gtk2::Gdk::GC->new ($root, undef);
$gc->set_line_attributes (3, 'solid', 'round', 'round');
$gc->set_rgb_bg_color($white);
$gc->set_rgb_fg_color($white);
$gc->set_subwindow ('include-inferiors');
$gc->set_function ('xor');
	       
print $root." - ID: ".$root->get_xid."\n";
print $root->get_events."\n";

Gtk2::Gdk->pointer_grab ($root, 1, [qw/
		       button-press-mask
		       button-motion-mask
		       button-release-mask/], undef, Gtk2::Gdk::Cursor->new ('crosshair'), 0);

my $done = 0;
my $counter_outer = 0;
my $counter_inner = 0;
my $rx = 0;
my $ry = 0; 
my $rw = 0;
my $rh = 0; 
my $btn_pressed = 0;
my $rect_x = 0; 
my $rect_y = 0; 
my $rect_w = 0; 
my $rect_h = 0;
my $rectangle = undef;

	if (Gtk2::Gdk->pointer_is_grabbed){
		print "GRABBED!!\n";

		while(1){
			while(!$done && Gtk2::Gdk->events_pending){
				print "inner iteration\n";
				my $event = $disp->get_event;

				if($event->type eq 'button-release'){
					$done = 1;
					print "Type: ".$event->type."\n" if defined $event;				
				}elsif($event->type eq 'button-press'){
					$btn_pressed = 1;
          			$rx = $event->x;
          			$ry = $event->y;
					print "Type: ".$event->type."\n" if defined $event;				
				}elsif($event->type eq 'motion-notify'){
					print "Type: ".$event->type."\n" if defined $event;	
					if($btn_pressed){
						if ($rect_w) {
						  print "Trying to clear a rectangle ($rect_x, $rect_y, $rect_w, $rect_h)\n";						
							$root->draw_rectangle($gc, 0, $rect_x, $rect_y, $rect_w, $rect_h);
					  	}
						$rect_x = $rx;
						$rect_y = $ry;
						$rect_w = $event->x - $rect_x;
						$rect_h = $event->y - $rect_y;											
		
						if ($rect_w < 0) {
						  $rect_x += $rect_w;
						  $rect_w = 0 - $rect_w;
						}
						if ($rect_h < 0) {
						  $rect_y += $rect_h;
						  $rect_h = 0 - $rect_h;
						}

						print "Trying to draw a rectangle ($rect_x, $rect_y, $rect_w, $rect_h)\n";
						$root->draw_rectangle($gc, 0, $rect_x, $rect_y, $rect_w, $rect_h);
						#Gtk2::Gdk->flush;						
					}	

				}
	
				if (Gtk2::Gdk->pointer_is_grabbed){
					print "Still GRABBED!!\n";
				}else{
					print "Broken GRABBED!!\n";
				}	


			}
			last if $done;
		}
		 
	}else{
		print "UNGRABBED!!\n";
	}


	$root->draw_rectangle($gc, 0, $rect_x, $rect_y, $rect_w, $rect_h);
	Gtk2::Gdk->pointer_ungrab (0);

	return 1;
}



