oldtechaa
oldtechaa

Reputation: 1524

undef is not of type Gtk2::Gdk::Drawable - gtk2-perl custom widget - calling window method of a GtkWidget

I'm trying to make a custom Perl Gtk2 widget. When I try to access the window property of the GtkDrawingArea, it returns undef rather than a GdkDrawable object. According to the Gtk2-Perl documentation this method can return either the window object or undef, but it isn't clear under what conditions it returns undef. Could someone please clarify this?

#!/usr/bin/perl

use strict;
use warnings;

package Gtk2::MIDIPlot;

use Gtk2;
use base 'Gtk2::DrawingArea';
use Cairo;

sub new {
  my $class = shift;
  my $this = bless Gtk2::DrawingArea->new(), $class;

  $this->set_size_request(14400, 768);

  return $this;
}

sub draw {
  my $drawArea = shift;

  my $thisCairoSurface = Gtk2::Gdk::Cairo::Context->create($drawArea->window);
}

package main;

use Gtk2 -init;

my $window = Gtk2::Window->new();

my $mainWidgetScroll = Gtk2::ScrolledWindow->new();
my $mainWidget = Gtk2::MIDIPlot->new();
$mainWidget->draw($mainWidget);
$mainWidgetScroll->add_with_viewport($mainWidget);
$mainWindow->add($mainWidgetScroll);

$window->signal_connect(destroy => sub{Gtk2->main_quit()});
$window->show_all();
Gtk2->main();

0;

Upvotes: 2

Views: 147

Answers (1)

oldtechaa
oldtechaa

Reputation: 1524

Looking at this example I discovered I need to wait for the expose event of the widget before being able to draw on it, as the window has not been created until that event. Also, the code needs to fit better with the GTK style of setting things up, then executing actions in callbacks.

#!/usr/bin/perl

use strict;
use warnings;

package Gtk2::MIDIPlot;

use Gtk2;
use base 'Gtk2::DrawingArea';
use Cairo;

sub new {
  my $class = shift;
  my $this = bless Gtk2::DrawingArea->new(), $class;

  $this->signal_connect(expose_event => 'Gtk2::MIDIPlot::draw');

  return $this;
}

sub draw {
  my $drawArea = shift;

  $drawArea->set_size_request(14400, 768);
  my $thisCairoSurface = Gtk2::Gdk::Cairo::Context->create($drawArea->get_window());
}

Upvotes: 1

Related Questions