#!/usr/bin/perl -w

use warnings;
use strict;
use Cairo;
use Glib qw/TRUE FALSE/;
use Gtk2;

## set $case=0 to produces immediate crash on opensuse 13.1 x86_64
## set $case=1 to produces crash after a few clicks on drawing area on opensuse 13.1 x86_64
## both $case=0 and $case=1 produce no crash on opensuse 13.1 i586, nor on windows.
my $case=0;

my $window = undef;		
my $cr = undef;			
my $drawing_area = undef;	
my $ScrolledWindow = undef;	
my $surface = undef;

$surface = Cairo::ImageSurface->create('rgb24',200,200);
$cr = Cairo::Context->create( $surface );
$cr->rectangle (0,0,200,200);
$cr->set_source_rgb (1,1,1);
$cr->fill;
$cr->show_page;

Gtk2->init;

$window = Gtk2::Window->new ('toplevel');
$window->set_size_request(200,200);
$window->signal_connect ("destroy", sub { Gtk2->main_quit });

$drawing_area = Gtk2::DrawingArea->new;

if($case==0){
  $drawing_area->set_size_request (200,200);
  $window->add ($drawing_area);
}else{
  my $hbox=createmaindrawingarea();
  $window->add($hbox);
}

$drawing_area->signal_connect (expose_event => \&expose_event);

$window->show_all;

Gtk2->main;

sub expose_event {
  my $widget = shift; # GtkWidget *widget
  $cr = Gtk2::Gdk::Cairo::Context->create( $widget->window );
  $cr->set_source_surface( $surface, 0, 0 );  
  $cr->paint;
  return FALSE;
}


sub createmaindrawingarea {

my $hbox = Gtk2::HBox->new (FALSE, 0);
$hbox->set_border_width(5);

my $vp = Gtk2::Viewport->new( undef, undef );
$ScrolledWindow = Gtk2::ScrolledWindow->new(undef, undef );
$ScrolledWindow->set_policy( 'automatic', 'automatic' );
$ScrolledWindow->add( $vp );

$drawing_area = Gtk2::DrawingArea->new;
$drawing_area->set_size_request (2000,2000);

$vp->add($drawing_area);
$hbox->pack_start ($ScrolledWindow, TRUE, TRUE, 0);

$drawing_area->signal_connect (expose_event => \&expose_event);
$drawing_area->signal_connect (button_press_event => \&button_press_event);
$drawing_area->set_events ([qw/button-press-mask/]);
$drawing_area->set_extension_events("cursor");

return $hbox;

}

sub button_press_event {
  $drawing_area->queue_draw();
}
