Emmanuele Bassi > Clutter-0.820 > xs/ClutterContainer.xs

Download:
Clutter-0.820.tar.gz

Annotate this POD

CPAN RT

New  3
Open  2
View/Report Bugs
Source   Latest Release: Clutter-1.002

DESCRIPTION ^

Clutter::Container is an interface for Clutter::Actors that provides a common API for adding, removing and iterating over children.

By implementing the Clutter::Container interface with the usual Glib::Object subclassing methods it is possible to use the Clutter::Container API for managing children.

CHILD PROPERTIES ^

Clutter::Container allows the management of properties that exist only when an actor is contained inside a container.

For instance, it is possible to add a property like padding to a child actor when inside a container; such property might only be useful when adding any actor to a specific container implementation, and might be desirable not to have it inside the actor class itself.

In order to create child properties for a container it is needed to subclass the Clutter::ChildMeta object class using the usual Glib::Object mechanisms, like:

  package My::ChildMeta;

  use Glib::Object::Subclass
      'Clutter::ChildMeta',
      properties => [
        ...
      ];

The Clutter::ChildMeta class is the class that stores and handles the child properties. A ChildMeta instance also stores references to the container that handles it, and to the actor it is wrapping.

The container implementation should set the package name of the ChildMeta subclass it will use; the most convenient place is inside the INIT_INSTANCE sub:

  sub INIT_INSTANCE {
    my ($self) = @_;

    # instance set up

    $self->set_child_meta_type('My::ChildMeta');
  }

This is enough to start using the Clutter::Container::child_set() and Clutter::Container::child_get() methods to store and retrieve values for the properties specified inside the ChildMeta subclass, like:

  $container->child_set($child, 'padding', [ 0, 10, 0, 10 ]);

When the child is removed from the container, the ChildMeta instance bound to it will simply disappear.

It is also possible to exercise a higher degree of control on the creation and destruction of the ChildMeta instances by using the CREATE_CHILD_META, GET_CHILD_META and DESTROY_CHILD_META functions.

CREATING A CUSTOM CONTAINER ACTOR ^

  package MyContainer;
  use Clutter;
  use Glib::Object::Subclass
      'Clutter::Actor',
      interfaces => [ qw( Clutter::Container ) ];

Virtual Methods

In order to create a Clutter::Container actor, an implementation of the following methods is required:

ADD ($container, $actor)
o $container (Clutter::Container)
o $actor (Clutter::Actor)

Called to add actor to container. The implementation should queue a relayout of container and emit the ::actor-added signal once the actor has been added.

For instance:

  push @{$self->{children}}, $actor;
  $actor->set_parent($self);

  $self->queue_relayout();
  $self->signal_emit('actor-added', $actor);
REMOVE ($container, $actor)
o $container (Clutter::Container)
o $actor (Clutter::Actor)

Called to remove actor from container. The implementation should emit the ::actor-removed signal once the actor has been removed.

RAISE ($container, $child, $sibling)
o $container (Clutter::Container)
o $child (Clutter::Actor)
o $sibling (Clutter::Actor)

Called when raising child above sibling. If sibling is undefined, then child should be raised above every other child of container.

LOWER ($container, $child, $sibling)
o $container (Clutter::Container)
o $child (Clutter::Actor)
o $sibling (Clutter::Actor)

Called when lowering child below sibling. If sibling is undefined, then child should be lowered below every other child of container.

SORT_DEPTH_ORDER ($container)
o $container (Clutter::Container)

Called when resorting the list of children depending on their depth.

FOREACH ($container, $function, $data)
o $container (Clutter::Container)
o $function (code reference)
o $data (scalar) data to pass to the function

Called when iterating over every child of container. For each child the function must be called with the actor and the passed data, for instance:

  foreach my $child (@{$container->{children}}) {
    &$function ($child, $data);
  }

This function will also be called by the get_children method.

CREATE_CHILD_META ($container, $actor)
o $container (Clutter::Container)
o $actor (Clutter::Actor)

Called when creating a new Clutter::ChildMeta instance wrapping actor inside container. This function should be overridden only if the metadata class used to implement child properties needs special code or needs to be stored inside a different data structure within container.

For instance:

  sub CREATE_CHILD_META {
      my ($self, $child) = @_;

      my $meta = My::ChildMeta->new(
          container => $self,
          actor     => $child,
      );

      # store the ChildMeta into a hash using the
      # actor itself as the key
      $self->{meta}->{$child} = $meta;
  }

When overriding the CREATE_CHILD_META, the GET_CHILD_META and the DESTROY_CHILD_META methods should be overridden as well.

This function is called before Clutter::Container::ADD_ACTOR.

DESTROY_CHILD_META ($container, $actor)
o $container (Clutter::Container)
o $actor (Clutter::Actor)

Called when destroying a Clutter::ChildMeta instance for actor.

For instance:

  sub DESTROY_CHILD_META {
      my ($self, $child) = @_;

      delete $self->{meta}->{$child};
  }

This function is called before Clutter::Actor::REMOVE_ACTOR

childmeta = GET_CHILD_META ($container, $actor)
o $container (Clutter::Container)
o $actor (Clutter::Actor)

Called when retrieving a Clutter::ChildMeta instance for actor.

For instance:

  sub GET_CHILD_META {
      my ($self, $child) = @_;

      return $self->{meta}->{$child};
  }
syntax highlighting: