Re: Drawing Area issues ( continued, with example )




You might be able to just use one drawing area. Then you don't have to worry about creating and destroying drawing areas, surfaces and contexts. When you want to redraw, just "paint" the drawing area background and start drawing. This approach works well for most drawings.

Test out the following and see if it is close. Just a grid in one or four panes with one drawing area.

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(800, 400);
$window->set_border_width(10);
$window->set_title("Graphs");

#Number of graphs. Either 1 or 4. Changed by the two buttons.
my $graphs = 1;

my $da = Gtk3::DrawingArea->new();
$da->set_vexpand(TRUE);
$da->set_hexpand(TRUE);
$da->signal_connect('draw' => \&draw_graph);

my $single_button = Gtk3::Button->new( 'graph 1 date' );
$single_button->signal_connect('clicked' => \&single_button_clicked, $da);

my $multiple_button = Gtk3::Button->new( 'graph 4 dates' );
$multiple_button->signal_connect('clicked' => \&multiple_button_clicked, $da);

my $grid1 = Gtk3::Grid->new();
$grid1->set_row_spacing(10);
$grid1->attach($da, 0, 0, 4, 4);
$grid1->attach($single_button, 1, 5, 1, 1);
$grid1->attach($multiple_button, 2, 5, 1, 1);

$window->add($grid1);

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

sub draw_graph
{
  my ($widget, $cr, $data) = @_;
  my $width = $widget->get_allocated_width();
  my $height = $widget->get_allocated_height();
  #Some drawing variables.
  my $x = 0;
  my $y = 0;
  my $graph_width = $width / $graphs;
  my $vertical_width = $graph_width / 24;
  my $horizontal_width = $height / 4;
 
  #Paint background.
  $cr->set_source_rgb(0, 0, 0);
  $cr->paint;

  #Vertical lines.
  $cr->set_source_rgb(0, 0, 1);
  $cr->set_line_width(1);
  for (my $i=0; $i < $graphs; $i++)
    {
      for (my $j = 0; $j < 24; $j++)
        {
          $x = $i * $graph_width + $j * $vertical_width;
          $cr->move_to($x, 0);
          $cr->line_to($x, $height);
          $cr->stroke();
        }
    }

  #24 numbers.
  $cr->set_source_rgb(1, 1, 1);
  for (my $i=0; $i < $graphs; $i++)
    {
      for (my $j = 0; $j < 24; $j++)
        {
          $x = $i * $graph_width + $j * $vertical_width;
          $cr->move_to($x + 5, $height - 10);
          $cr->show_text ($j);
        }
    }

  #Horizontal lines.
  $cr->set_source_rgb(0, 0, 1);
  for (my $i=0; $i < $graphs; $i++)
    {
      for (my $j = 1; $j < 4; $j++)
        {
          $y = $j * $horizontal_width;
          $cr->move_to(0, $y);
          $cr->line_to($width, $y);
          $cr->stroke();
        }
    }

  #4 numbers.
  $cr->set_source_rgb(1, 1, 1);
  for (my $i=0; $i < $graphs; $i++)
    {
      for (my $j = 0; $j < 4; $j++)
        {
          $y = $j * $horizontal_width;
          $cr->move_to(5, $y + 20);
          $cr->show_text (2000 - $j * 500);
        }
    }


  #Draw graph blocks.
  $cr->set_source_rgb(0, 1, 1);
  $cr->set_line_width(10);
  $cr->rectangle(0, 0, $width, $height);
  $cr->stroke();
  $cr->set_line_width(5);
  for (my $i=1; $i < $graphs; $i++)
    {
      $x = $i * $graph_width;
      $cr->move_to($x, 0);
      $cr->line_to($x, $height);
      $cr->stroke();
    }

  return FALSE;
}
sub single_button_clicked
{
  my ($widget, $da) = @_;
  $graphs = 1;
  $da->queue_draw();
}
sub multiple_button_clicked
{
  my ($widget, $da) = @_;
  $graphs = 4;
  $da->queue_draw();
}




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