Very strange issue with multiple drawing areas

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Very strange issue with multiple drawing areas

Daniel Kasak-4
Hi all.

I'm using a DrawingArea to render a graph. I then thought it would be
great to have multiple graphs, side-by-side, for different dates. So I
now create drawing areas in a loop, one for each day selected. However
*most* things are rendering to the 1st drawing area, and I've spent a
day looking at my code, and don't think I have a bug anywhere.

Each drawing area is added to a box:

$self->{builder}->get_object( "graph_box" )->pack_start(
$drawing_area, 1, 1, 0 );

The 'draw' signal is being set:

$drawing_area->signal_connect( draw => sub { $self->render_graph( @_,
$date ) } );

This is being fired for all drawing areas connected, and I get the
correct $date being passed in. But all the graphing occurs in 1
drawing area. Interestingly, I have another method for rendering text,
and *this* is correctly rendering on each drawing area. But both this
method, and the method rendering the graphs use the $cairo_context
being passed into render_graph, eg:

sub render_graph {

    my ( $self, $widget, $cairo_context, $date ) = @_;

    ...

}

Lastly, while I see $widget changes for each drawing area,
$cairo_context is actually the same *every* time. I would have
expected this to change. Why isn't it?

I can't see how I could have possibly mixed things up, as the
$cairo_context is automatically passed in on the 'draw' event for each
drawing area. I'm not doing anything weird like storing cairo contexts
in global variables. I could post the code if someone is interested,
but it's pretty big these days, and requires a database to pull data
from.

Can anyone think of a reason why I'm seeing these issues? Is there
some limitation on only having 1 drawing area?

Dan
_______________________________________________
gtk-perl-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: Very strange issue with multiple drawing areas

Gtk+ - Perl mailing list
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;
}
 




_______________________________________________
gtk-perl-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-perl-list