Re: subclassing Gnome2::Canvas::Text objects

James Muir said:
Hi Everybody,

I see in the FAQ that subclassing Gtk2 widgets is pretty straight
forward, but I want to subclass Gnome2::Canvas::Text. I've created a
subclass called EllipsisText that simply overrides the "text" method,
appending an ellipsis (...) to the text argument.

However when I try something like this:

local $ellipsisText = Gnome2::Canvas::Item->new($root, 'EllipsisText',
x=>10, y=>10, fill_color=>'black',

            anchor=>'GTK_ANCHOR_NW', text=$text);

I am met with the following error:

"EllipsisText is not registered with gperl as an object type blah blah blah"

Is what I am trying to do possible?

You have to use Glib::Object::Subclass to do this type of subclassing, since
that registers a new GType.  Gnome2::Canvas::Item::new() is a factory that
keys off of GTypes, so if you don't create a new GType, it doesn't know what
to do.

Also, there's no "text" method; it's handled purely as a property.  We don't
support property overrides or redirects (at least, i've never tried), so your
best bet is to create a new property in your custom class, and implement your
custom stuff there:

  package EllipsisText;
  use Glib ':constants';
  use Glib::Object::Subclass
      properties => [
              pspec => Glib::ParamSpec->string ('ellipsis-text',
                                                'Ellipsis Text',
                                                'Text, ellipsized as needed',
              # implement this property as preprocessing of the actual
              # 'text' property of the parent class.
              get => sub { $_[0]->get ('text') },
              set => sub {
                  my ($object, $newval) = @_;
                  $object->set (text => ellipsize_text ($newval));

You could even have another property that specifies the maximum length, etc.

muppet <scott at asofyet dot org>

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