Re: widget name (was Re: Remove spaces in dialog window)

Beast said:
muppet wrote:

This is very dangerous.  A plain, unmagical, blessed perl hash cannot be
used as a Gtk2::Widget, because there is no actual C GObject backing it.

Either use Glib::Object::Subclass to subclass Gtk2::Dialog (or any
widget), or rebless the actual widget like this:

    my $self = $class->SUPER::new(...);
    bless $self, $class;

But I think I mistakenly your point (I though its dangerous to bless
hash, so I change it to scalar variable).

Hash versus scalar is not the point; you can't bless non-scalars, because you
can't bless non-references.

I should use :

  my $self = {};
  $self->{win} = $class->SUPER::new($title, $parent, "modal");
  bless($self, $class);

Instead of:

  my $self = {};
  bless($self, $class);
  $self->{win} = $class->SUPER::new($title, $parent, "modal");

Is that correct?


You should use

  $self = $class->SUPER::new (...);
  bless $self, $class;

That way, $self, which will be initially created as a Gtk2::Dialog by
SUPER::new(), will be reblessed as a $class.  Since you've propertly set up
@ISA = 'Gtk2::Dialog', this means that

  $thing = Properties->new ();
  isa_ok ($thing, 'Properties');
  isa_ok ($thing, 'Gtk2::Dialog');

  # and now you can use any Gtk2::Dialog method on $thing.

Conversely, if you use the ->{win} technique:

  $thing = bless {}, $class;
  $thing->{win} = some widget instance;
  $thing->run;  # BOOM!  this won't work, because $thing isn't
                # actually a Gtk2::Dialog
  $thing->{win}->run;  # this is what you have to do.

because although you've blessed $thing as a Properties and set up the
inheritance, when the bindings try to extract the underlying widget reference
from the blessed scalar, they won't be able to find it.

All you really need is a working understanding, but here's the technical
reason:  the bindings create a hash reference to represent each GObject; the
hash reference is blessed into the corresponding package, and the actual C
widget pointer is attached to a MAGIC vtable (see the perlguts manpage).  That
means that the widget pointer is attached to the reference in a way that you
can't see (or break) at the perl level.

Have a look at the article about Subclassing Widgets in Perl:

muppet <scott at asofyet dot org>

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