Goo canvas runaway zoom, and adjustment warning



Hi,

I've been having trouble with a runaway zoom in the script below.
It works fine if you click with a quick release on the zoom control,
but if you hold the mouse button down to change the scroll, a runaway
zoom is initiated.

Also, it may be related, but otherwise seems harmless, I get
this warning. I googled for it, but couldn't find a sensible answer.

GtkSpinButton: setting an adjustment with non-zero page size is deprecated
at ./basic-canvas-scaled line 70

I really want to know how to control that runaway zoom.

Thanks in advance.
zentara

#!/usr/bin/perl 
use strict;
use warnings;
use Goo::Canvas;
use Gtk2 '-init';
use Glib qw(TRUE FALSE);

my $zoomx = 0; my $zoomy = 0;
my $zoom0 = 1;

my $window = Gtk2::Window->new('toplevel');
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });
$window->set_default_size(640, 600);

my $vbox = Gtk2::VBox->new;
$vbox->set_border_width(4);
$vbox->show;

my $hbox = Gtk2::HBox->new(FALSE, 4);
$vbox->pack_start($hbox, FALSE, FALSE, 0);
$hbox->show;
$window->add($vbox);

my $swin = Gtk2::ScrolledWindow->new;
$swin->set_shadow_type('in');
$vbox->pack_start($swin, 1, 1, 0); 


my $cwidth = 1000;
my $cheight = 1000;
my $canvas = Goo::Canvas->new();
$canvas->set_size_request(600, 450); # size on screen
$canvas->set_bounds(0, 0, $cwidth, $cheight); # scrollregion
$swin->add($canvas);
my $root = $canvas->get_root_item();
$canvas->signal_connect('button-press-event',
                      \&on_can_button_press);

# write a text grid to watch zoom

my $x1 = 0; my $y = 20;my $sp = 30;

for(my $x = 0; $x<=2000; $x += 1){
  $x1 += 50;
  if($x1 > 980){$y+=20; $x1= 50} 
  if($y > 980){ last} 
  
  my $text = Goo::Canvas::Text->new(
    $root, $x , $x1 , $y , 20, 
    'center',
    'font' => 'Sans 12',
    'wrap' => 'word',
 );
   
    print $canvas->get_default_line_width,"\n";
   print  $text->get('width'),"\n";

 }


# Zoom
my $z = Gtk2::Label->new("Zoom:");
$hbox->pack_start($z, FALSE, FALSE, 0);
$z->show;

my $adj = Gtk2::Adjustment->new(1, 0.05, 100, 0.05, 0.5, 0.5);
my $sb = Gtk2::SpinButton->new($adj, 0 , 2 );
$adj->signal_connect("value-changed", \&zoom_changed, $canvas);
$hbox->pack_start($sb, FALSE, FALSE, 10);
$sb->show;

# Center scroll or 0,0 scroll

my $button1 = Gtk2::RadioButton->new(undef,"00 Zoom");
$button1->signal_connect( "toggled" => \&sighandler); 
$hbox->pack_start($button1, 0, 0, 0);
$button1->set_active(TRUE);
$button1->show;

my @group = $button1->get_group;
my $button2 = Gtk2::RadioButton->new_with_label(@group,"Center Zoom");
$button2->signal_connect( "toggled" => \&sighandler); 
$hbox->pack_start($button2, 0, 0, 0);
$button2->show;
    
# Create PDF                                                                          
my $bpdf = Gtk2::Button->new_with_label('Write PDF');                                       
$hbox->pack_start($bpdf, FALSE, FALSE, 0);                                               
$bpdf->show;                                                                             
$bpdf->signal_connect("clicked", \&write_pdf_clicked, $canvas);         


$window->show_all();
Gtk2->main;

sub on_can_button_press {
     my ( $widget, $event ) = @_;
#     print $widget ,' ',$event->type,"\n";
      my ($x,$y) = ($event->x,$event->y);
      print "$x  $y\n";
    return TRUE;
}

sub write_pdf_clicked {
    my ($but, $canvas) = @_;
    print "Write PDF...\n";

    my $scale = $adj->get_value;
    print "scale->$scale\n"; 
    
    my $surface = Cairo::PdfSurface->create("$0-$scale.pdf", 
                    $scale*$cwidth, $scale*$cheight);
    
    my $cr = Cairo::Context->create($surface);

    # needed to save scaled version
    $cr->scale($scale, $scale);

    $canvas->render($cr, undef, 1);
    $cr->show_page;
    print "done\n";
    return TRUE;
}


sub zoom_changed {
    my ($adj, $canvas) = @_;
    $canvas->set_scale($adj->get_value);
  
    if($zoom0){
    # zoom from 0,0 ...like Gnome2::Canvas's set_center_scroll_region (FALSE);
    $zoomx = 0; $zoomy= 0;
    }else{
    
     my $ha1  = $swin->get_hadjustment;
     my $va1  = $swin->get_vadjustment;

     print "lower-> ".$ha1->lower."    ".$va1->lower,"\n";
     print "upper-> ".$ha1->upper."    ".$va1->upper,"\n";
    print "page size-> ".$ha1->page_size."    ".$va1->page_size,"\n";

    my $x =  $ha1->page_size/2 ;
    my $y =  $va1->page_size/2;

    print "\t\tx y -> $x $y\n";
 
    $zoomx = $x ; #* $adj->get_value;
    $zoomy = $y ; #* $adj->get_value;
  
    }
    
    print "$zoomx $zoomy\n";
    $canvas->scroll_to ($zoomx,$zoomy);   
}


sub sighandler {
    my ($button) = @_;
    if ($button->get_active())
       {
         print $button->get_label," is active\n";
         if($button->get_label eq '00 Zoom'){$zoom0 = 1}else{$zoom0 = 0}    
       }
}
__END__




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]