Blob Blame History Raw
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>3. Knowing when an actor's position or size changes</title><link rel="stylesheet" type="text/css" href="style.css"><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"><link rel="home" href="index.html" title="The Clutter Cookbook"><link rel="up" href="actors.html" title="Chapter 2. Actors"><link rel="prev" href="actors-composite.html" title="2. Implementing a simple custom actor"><link rel="next" href="actors-paint-wrappers.html" title="4. Overriding the paint sequence"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">3. Knowing when an actor's position or size changes</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="actors-composite.html">Prev</a> </td><th width="60%" align="center">Chapter 2. Actors</th><td width="20%" align="right"> <a accesskey="n" href="actors-paint-wrappers.html">Next</a></td></tr></table><hr></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="actors-allocation-notify"></a>3. Knowing when an actor's position or size changes</h2></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="idm140200513279152"></a>3.1. Problem</h3></div></div></div><p>You want to know when the position or the size, or
      both, of an actor change, for instance to update an unrelated
      actor or some internal state.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="idm140200513277664"></a>3.2. Solution</h3></div></div></div><p>You can use the <span class="emphasis"><em>notify</em></span> signal,
      detailed with the coordinate or the dimension you want
      to know has changed:</p><div class="informalexample"><pre class="programlisting">g_signal_connect (actor, "notify::x",
                  G_CALLBACK (on_x_changed),
                  NULL);
g_signal_connect (actor, "notify::height",
                  G_CALLBACK (on_height_changed),
                  NULL);
g_signal_connect (actor, "notify::depth",
                  G_CALLBACK (on_depth_changed),
                  NULL);</pre></div><p>If you want to know if any of the coordinates or dimensions of
      an actor have been changed, except for depth, you can use the
      <span class="emphasis"><em>allocation-changed</em></span> signal:</p><div class="informalexample"><pre class="programlisting">g_signal_connect (actor, "allocation-changed",
                  G_CALLBACK (on_allocation_changed),
                  NULL);</pre></div><p>The signature for the handler of the "notify" signal is:</p><div class="informalexample"><pre class="programlisting">void
on_notify (GObject    *gobject,
           GParamSpec *pspec,
           gpointer    user_data);</pre></div><p>While the signature for the handler of the "allocation-changed"
      signal is:</p><div class="informalexample"><pre class="programlisting">void
on_allocation_changed (ClutterActor           *actor,
                       const ClutterActorBox  *allocation,
                       ClutterAllocationFlags  flags,
                       gpointer                user_data);</pre></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="idm140200513268416"></a>3.3. Discussion</h3></div></div></div><p>Any change the position and size of an actor will cause a
      change in the allocation of the actor itself. This will update the
      values of the <span class="property">x</span>, <span class="property">y</span>,
      <span class="property">width</span> and <span class="property">height</span>
      properties as well.</p><p>The first technique allows a greater deal of granularity,
      allowing you to know what exactly changed. Inside the callback
      for the signal you can query the value of the property:</p><div class="informalexample"><pre class="programlisting">void
on_x_changed (GObject    *gobject,
              GParamSpec *pspec,
              gpointer    user_data)
{
  gint x_value = 0;

  /* Round the X coordinate to the nearest pixel */
  x_value = floorf (clutter_actor_get_x (CLUTTER_ACTOR (gobject))) + 0.5;

  g_print ("The new X coordinate is '%d' pixels\n", x_value);
}</pre></div><p>The second technique is more indicated if you want to
      get notification that any of the positional or dimensional
      attributes changed, except for the depth:</p><div class="informalexample"><pre class="programlisting">void
on_allocation_changed (ClutterActor           *actor,
                       const ClutterActorBox  *allocation,
                       ClutterAllocationFlags  flags,
                       gpointer                user_data)
{
  g_print ("The bounding box is now: (%.2f, %.2f) (%.2f x %.2f)\n",
           clutter_actor_box_get_x (allocation),
           clutter_actor_box_get_y (allocation),
           clutter_actor_box_get_width (allocation),
           clutter_actor_box_get_height (allocation));
}</pre></div><p>All actors will update these properties when their size
      or position change.</p><p>Note that the stage, on the other hand, will not notify on
      position changes, so it is not possible to use the
      <span class="property">x</span> and <span class="property">y</span>
      properties to know that the platform-specific window embedding the
      stage has been moved — if the platform supports a windowing
      system. In order to achieve that you will have to use backend-specific
      API to extract the surface used by the stage and then platform-specific
      API to retrieve its coordinates.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="actors-composite.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="actors.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="actors-paint-wrappers.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">2. Implementing a simple custom actor </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 4. Overriding the paint sequence</td></tr></table></div></body></html>