cookbook: Begin porting examples to the new API

Start dropping the usage of deprecated classes and API.
This commit is contained in:
Emmanuele Bassi 2012-01-24 15:01:00 +00:00
parent 75549456fd
commit 1c01554e6a
7 changed files with 68 additions and 60 deletions

View File

@ -70,7 +70,7 @@ main (int argc,
clutter_actor_add_constraint (button, align_x_constraint); clutter_actor_add_constraint (button, align_x_constraint);
clutter_actor_add_constraint (button, align_y_constraint); clutter_actor_add_constraint (button, align_y_constraint);
clutter_container_add_actor (CLUTTER_CONTAINER (stage), button); clutter_actor_add_child (stage, button);
clutter_actor_show (stage); clutter_actor_show (stage);

View File

@ -18,10 +18,8 @@ static const ClutterColor blue_color = { 0x00, 0x00, 0xff, 0xff };
*/ */
static void static void
add_keys_for_actor (ClutterActor *actor, add_keys_for_actor (ClutterActor *actor,
gpointer user_data) ClutterAnimator *animator)
{ {
ClutterAnimator *animator = CLUTTER_ANIMATOR (user_data);
gfloat x, end_x; gfloat x, end_x;
x = clutter_actor_get_x (actor); x = clutter_actor_get_x (actor);
@ -41,7 +39,8 @@ move_actors (ClutterActor *actor,
ClutterEvent *event, ClutterEvent *event,
gpointer user_data) gpointer user_data)
{ {
State *state = (State *) user_data; State *state = user_data;
ClutterActor *child;
/* do nothing if the animator is already running */ /* do nothing if the animator is already running */
if (clutter_timeline_is_playing (clutter_animator_get_timeline (state->animator))) if (clutter_timeline_is_playing (clutter_animator_get_timeline (state->animator)))
@ -51,9 +50,12 @@ move_actors (ClutterActor *actor,
clutter_animator_remove_key (state->animator, NULL, NULL, -1); clutter_animator_remove_key (state->animator, NULL, NULL, -1);
/* add keys for all actors in the group */ /* add keys for all actors in the group */
clutter_container_foreach (CLUTTER_CONTAINER (state->group), for (child = clutter_actor_get_first_child (state->group);
add_keys_for_actor, child != NULL;
state->animator); child = clutter_actor_get_next_sibling (child))
{
add_keys_for_actor (child, state->animator);
}
/* start the animation */ /* start the animation */
clutter_animator_start (state->animator); clutter_animator_start (state->animator);
@ -68,6 +70,7 @@ main (int argc,
ClutterActor *red; ClutterActor *red;
ClutterActor *green; ClutterActor *green;
ClutterActor *blue; ClutterActor *blue;
State *state = g_new0 (State, 1); State *state = g_new0 (State, 1);
/* seed random number generator */ /* seed random number generator */
@ -87,32 +90,32 @@ if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
G_CALLBACK (clutter_main_quit), G_CALLBACK (clutter_main_quit),
NULL); NULL);
state->group = clutter_group_new (); state->group = clutter_actor_new ();
clutter_actor_add_child (state->stage, state->group);
red = clutter_rectangle_new_with_color (&red_color); red = clutter_actor_new ();
clutter_actor_set_background_color (red, &red_color);
clutter_actor_set_size (red, 50, 50); clutter_actor_set_size (red, 50, 50);
clutter_actor_set_position (red, 50, 50); clutter_actor_set_position (red, 50, 50);
clutter_actor_add_child (state->group, red);
green = clutter_rectangle_new_with_color (&green_color); green = clutter_actor_new ();
clutter_actor_set_background_color (green, &green_color);
clutter_actor_set_size (green, 50, 50); clutter_actor_set_size (green, 50, 50);
clutter_actor_set_position (green, 50, 150); clutter_actor_set_position (green, 50, 150);
clutter_actor_add_child (state->group, green);
blue = clutter_rectangle_new_with_color (&blue_color); blue = clutter_actor_new ();
clutter_actor_set_background_color (blue, &blue_color);
clutter_actor_set_size (blue, 50, 50); clutter_actor_set_size (blue, 50, 50);
clutter_actor_set_position (blue, 50, 250); clutter_actor_set_position (blue, 50, 250);
clutter_actor_add_child (state->group, blue);
g_signal_connect (state->stage, g_signal_connect (state->stage,
"key-press-event", "key-press-event",
G_CALLBACK (move_actors), G_CALLBACK (move_actors),
state); state);
clutter_container_add (CLUTTER_CONTAINER (state->group),
red,
green,
blue,
NULL);
clutter_container_add_actor (CLUTTER_CONTAINER (state->stage), state->group);
clutter_actor_show (state->stage); clutter_actor_show (state->stage);
clutter_main (); clutter_main ();

View File

@ -326,8 +326,7 @@ cb_button_init (CbButton *self)
priv->child = clutter_box_new (layout); priv->child = clutter_box_new (layout);
/* set the parent of the ClutterBox to this instance */ /* set the parent of the ClutterBox to this instance */
clutter_actor_set_parent (priv->child, clutter_actor_add_child (CLUTTER_ACTOR (self), priv->child);
CLUTTER_ACTOR (self));
/* add text label to the button; see the ClutterText API docs /* add text label to the button; see the ClutterText API docs
* for more information about available properties * for more information about available properties
@ -337,8 +336,7 @@ cb_button_init (CbButton *self)
"ellipsize", PANGO_ELLIPSIZE_END, "ellipsize", PANGO_ELLIPSIZE_END,
NULL); NULL);
clutter_container_add_actor (CLUTTER_CONTAINER (priv->child), clutter_actor_add_child (priv->child, priv->label);
priv->label);
/* add a ClutterClickAction on this actor, so we can proxy its /* add a ClutterClickAction on this actor, so we can proxy its
* "clicked" signal into a signal from this actor * "clicked" signal into a signal from this actor

View File

@ -2,15 +2,15 @@
#include <clutter/clutter.h> #include <clutter/clutter.h>
static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff }; static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff };
static const ClutterColor red_color = { 0xff, 0x00, 0x00, 0xff };
static const ClutterColor blue_color = { 0x00, 0x00, 0xff, 0xff };
void void
clicked_cb (ClutterClickAction *action, clicked_cb (ClutterClickAction *action,
ClutterActor *actor, ClutterActor *actor,
gpointer user_data) gpointer user_data)
{ {
g_debug ("Button %d clicked", clutter_click_action_get_button (action)); g_print ("Pointer button %d clicked on actor %s\n",
clutter_click_action_get_button (action),
clutter_actor_get_name (actor));
} }
int int
@ -31,15 +31,21 @@ main (int argc,
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color); clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL); g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
actor1 = clutter_rectangle_new_with_color (&red_color); actor1 = clutter_actor_new ();
clutter_actor_set_name (actor1, "Red Button");
clutter_actor_set_background_color (actor1, CLUTTER_COLOR_Red);
clutter_actor_set_size (actor1, 100, 100); clutter_actor_set_size (actor1, 100, 100);
clutter_actor_set_reactive (actor1, TRUE); clutter_actor_set_reactive (actor1, TRUE);
clutter_actor_set_position (actor1, 50, 150); clutter_actor_set_position (actor1, 50, 150);
clutter_actor_add_child (stage, actor1);
actor2 = clutter_rectangle_new_with_color (&blue_color); actor2 = clutter_actor_new ();
clutter_actor_set_name (actor2, "Blue Button");
clutter_actor_set_background_color (actor2, CLUTTER_COLOR_Blue);
clutter_actor_set_size (actor2, 100, 100); clutter_actor_set_size (actor2, 100, 100);
clutter_actor_set_position (actor2, 250, 150); clutter_actor_set_position (actor2, 250, 150);
clutter_actor_set_reactive (actor2, TRUE); clutter_actor_set_reactive (actor2, TRUE);
clutter_actor_add_child (stage, actor2);
action1 = clutter_click_action_new (); action1 = clutter_click_action_new ();
clutter_actor_add_action (actor1, action1); clutter_actor_add_action (actor1, action1);
@ -47,8 +53,6 @@ main (int argc,
action2 = clutter_click_action_new (); action2 = clutter_click_action_new ();
clutter_actor_add_action (actor2, action2); clutter_actor_add_action (actor2, action2);
clutter_container_add (CLUTTER_CONTAINER (stage), actor1, actor2, NULL);
g_signal_connect (action1, g_signal_connect (action1,
"clicked", "clicked",
G_CALLBACK (clicked_cb), G_CALLBACK (clicked_cb),

View File

@ -96,7 +96,7 @@ main (int argc, char *argv[])
NULL); NULL);
/* the viewport which the box is scrolled within */ /* the viewport which the box is scrolled within */
viewport = clutter_group_new (); viewport = clutter_actor_new ();
/* viewport is shorter than the stage */ /* viewport is shorter than the stage */
clutter_actor_set_size (viewport, STAGE_WIDTH, STAGE_HEIGHT * 0.5); clutter_actor_set_size (viewport, STAGE_WIDTH, STAGE_HEIGHT * 0.5);
@ -111,10 +111,10 @@ main (int argc, char *argv[])
clutter_actor_set_clip_to_allocation (viewport, TRUE); clutter_actor_set_clip_to_allocation (viewport, TRUE);
/* put the texture inside the viewport */ /* put the texture inside the viewport */
clutter_container_add_actor (CLUTTER_CONTAINER (viewport), texture); clutter_actor_add_child (viewport, texture);
/* add the viewport to the stage */ /* add the viewport to the stage */
clutter_container_add_actor (CLUTTER_CONTAINER (stage), viewport); clutter_actor_add_child (stage, viewport);
g_signal_connect (viewport, g_signal_connect (viewport,
"scroll-event", "scroll-event",

View File

@ -4,8 +4,6 @@
#define OVERLAY_FACTOR 1.1 #define OVERLAY_FACTOR 1.1
static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff }; static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff };
static const ClutterColor red = { 0xff, 0x00, 0x00, 0xff };
static const ClutterColor blue = { 0x00, 0x00, 0xff, 0x66 };
void void
allocation_changed_cb (ClutterActor *actor, allocation_changed_cb (ClutterActor *actor,
@ -43,18 +41,22 @@ main (int argc, char *argv[])
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color); clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL); g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
actor = clutter_rectangle_new_with_color (&red); actor = clutter_actor_new ();
clutter_actor_set_background_color (actor, CLUTTER_COLOR_Red);
clutter_actor_set_size (actor, 100, 100); clutter_actor_set_size (actor, 100, 100);
clutter_actor_set_position (actor, 150, 150); clutter_actor_set_position (actor, 150, 150);
clutter_actor_add_child (stage, actor);
overlay = clutter_rectangle_new_with_color (&blue); overlay = clutter_actor_new ();
clutter_actor_set_background_color (overlay, CLUTTER_COLOR_Blue);
clutter_actor_set_opacity (overlay, 128);
g_signal_connect (actor, g_signal_connect (actor,
"allocation-changed", "allocation-changed",
G_CALLBACK (allocation_changed_cb), G_CALLBACK (allocation_changed_cb),
overlay); overlay);
clutter_container_add (CLUTTER_CONTAINER (stage), actor, overlay, NULL); clutter_actor_add_child (stage, overlay);
clutter_actor_animate (actor, CLUTTER_LINEAR, 2000, clutter_actor_animate (actor, CLUTTER_LINEAR, 2000,
"width", 300.0, "width", 300.0,

View File

@ -35,18 +35,20 @@ main (int argc,
/* put 5px of spacing between actors */ /* put 5px of spacing between actors */
clutter_box_layout_set_spacing (CLUTTER_BOX_LAYOUT (box_layout), 5); clutter_box_layout_set_spacing (CLUTTER_BOX_LAYOUT (box_layout), 5);
/* actors are packed into this box; we set its width, but /* actors are packed into this actor; we set its width, but
* allow its height to be determined by the children it contains * allow its height to be determined by the children it contains
*/ */
box = clutter_box_new (box_layout); box = clutter_actor_new ();
clutter_box_set_color (CLUTTER_BOX (box), &box_color); clutter_actor_set_layout_manager (box, box_layout);
clutter_actor_set_background_color (box, CLUTTER_COLOR_White);
clutter_actor_set_position (box, 100, 50); clutter_actor_set_position (box, 100, 50);
clutter_actor_set_width (box, 200); clutter_actor_set_width (box, 200);
/* pack an actor into the layout and set all layout properties on it /* pack an actor into the layout and set all layout properties on it
* at the same time * at the same time
*/ */
yellow = clutter_rectangle_new_with_color (&yellow_color); yellow = clutter_actor_new ();
clutter_actor_set_background_color (yellow, CLUTTER_COLOR_Yellow);
clutter_actor_set_size (yellow, 100, 100); clutter_actor_set_size (yellow, 100, 100);
clutter_box_layout_pack (CLUTTER_BOX_LAYOUT (box_layout), clutter_box_layout_pack (CLUTTER_BOX_LAYOUT (box_layout),
@ -57,28 +59,27 @@ main (int argc,
CLUTTER_BOX_ALIGNMENT_START, /* x-align */ CLUTTER_BOX_ALIGNMENT_START, /* x-align */
CLUTTER_BOX_ALIGNMENT_START); /* y-align */ CLUTTER_BOX_ALIGNMENT_START); /* y-align */
/* pack an actor into the box and set layout properties at the
* same time; note this is more concise if you mostly want to
* use the default properties for the layout
*/
red = clutter_rectangle_new_with_color (&red_color);
clutter_actor_set_size (red, 100, 100);
clutter_box_pack (CLUTTER_BOX (box),
red,
"x-fill", TRUE,
NULL);
/* add an actor to the box as a container and set layout properties /* add an actor to the box as a container and set layout properties
* afterwards; the latter is useful if you want to change properties on * afterwards; the latter is useful if you want to change properties on
* actors already inside a layout, but note that you have to * actors already inside a layout, but note that you have to
* pass the function both the layout AND the container * pass the function both the layout AND the container
*/ */
blue = clutter_rectangle_new_with_color (&blue_color); red = clutter_actor_new ();
clutter_actor_set_background_color (red, CLUTTER_COLOR_Red);
clutter_actor_set_size (red, 100, 100);
clutter_actor_add_child (box, red);
clutter_layout_manager_child_set (box_layout,
CLUTTER_CONTAINER (box),
red,
"x-fill", TRUE,
NULL);
blue = clutter_actor_new ();
clutter_actor_set_background_color (blue, CLUTTER_COLOR_Blue);
clutter_actor_set_size (blue, 100, 100); clutter_actor_set_size (blue, 100, 100);
clutter_container_add_actor (CLUTTER_CONTAINER (box), blue); clutter_actor_add_child (box, blue);
clutter_layout_manager_child_set (box_layout, clutter_layout_manager_child_set (box_layout,
CLUTTER_CONTAINER (box), CLUTTER_CONTAINER (box),
blue, blue,
@ -86,7 +87,7 @@ main (int argc,
NULL); NULL);
/* put the box on the stage */ /* put the box on the stage */
clutter_container_add_actor (CLUTTER_CONTAINER (stage), box); clutter_actor_add_child (stage, box);
clutter_actor_show (stage); clutter_actor_show (stage);