Re: Very strange issue with multiple drawing areas



Hi Daniel,

OK, I am new to Perl and trying to learn a few things myself. I know GTK and C can get grumpy if you don't send your variables in your signal callbacks correctly. So maybe check that the variables in your callback are what you expect with a little test code. Print to screen or use cairo to draw differently with different variables. Then try to track down the problem. And have a little fun drawing.

Eric

#!/usr/bin/perl
use strict;
use diagnostics;
use warnings;
use Gtk3 '-init';
use Glib qw(TRUE FALSE);

my $window = Gtk3::Window->new();
$window->signal_connect('delete_event' => sub { Gtk3->main_quit; });
$window->set_default_size(400, 200);
$window->set_border_width(10);
$window->set_title("Graphs");

my $test1 = 1;
my $da1 = Gtk3::DrawingArea->new();
$da1->set_name("Graph1");
$da1->signal_connect('draw' => \&draw_graph, $test1);

my $test2 = 2;
my $da2 = Gtk3::DrawingArea->new();
$da2->set_name("Graph2");
$da2->signal_connect('draw' => \&draw_graph, $test2);

my $hBox = Gtk3::Box->new('horizontal', 10);
$hBox->pack_start($da1, TRUE, TRUE, 0);
$hBox->pack_start($da2, TRUE, TRUE, 0);

$window->add($hBox);

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

sub draw_graph
{
  my ($widget, $cr, $data) = @_;
  print $widget->get_name() . "\n";
  my $width = $widget->get_allocated_width();
  my $height = $widget->get_allocated_height();

  #Paint background.
  if ($data == 1)
    {
      #Graph 1.
      $cr->set_source_rgb(0, 1, 0);
      $cr->paint;
    }
  else
    {
      #Graph 2.
      $cr->set_source_rgb(0, 1, 1);
      $cr->paint;
    }

  #Put some graph lines in.
  $cr->set_source_rgb(0, 0, 0);
  $cr->set_line_width(5);
  $cr->move_to($width / 10, $height / 2);
  $cr->line_to(9 * $width / 10, $height / 2);
  $cr->stroke();
  $cr->move_to($width / 2, $height / 10);
  $cr->line_to($width / 2, 9 * $height / 10);
  $cr->stroke();

  #The name of the graph.
  $cr->move_to(6 * $width / 10, 9 * $height / 10);
  $cr->show_text ($widget->get_name ());

  return FALSE;
}
 





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