int main (int argc, char *argv[]) { ClutterActor *stage; ClutterActor *button; ClutterConstraint *align_x_constraint; ClutterConstraint *align_y_constraint; if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS) return 1; stage = clutter_stage_new (); clutter_actor_set_size (stage, 400, 400); clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color); g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL); button = cb_button_new (); cb_button_set_text (CB_BUTTON (button), "hello"); /* the following is equivalent to the two lines above: * * button = g_object_new (CB_TYPE_BUTTON, * "text", "winkle", * NULL); * * because we defined a set_property function, which can accept * a PROP_TEXT parameter, GObject can create a button and set one * or more properties with a single call to g_object_new() */ /* note that the size of the button is left to Clutter's size requisition */ cb_button_set_text_color (CB_BUTTON (button), &white_color); cb_button_set_background_color (CB_BUTTON (button), &yellow_color); g_signal_connect (button, "clicked", G_CALLBACK (clicked), NULL); align_x_constraint = clutter_align_constraint_new (stage, CLUTTER_ALIGN_X_AXIS, 0.5); align_y_constraint = clutter_align_constraint_new (stage, CLUTTER_ALIGN_Y_AXIS, 0.5); clutter_actor_add_constraint (button, align_x_constraint); clutter_actor_add_constraint (button, align_y_constraint); clutter_actor_add_child (stage, button); clutter_actor_show (stage); clutter_main (); return EXIT_SUCCESS; }
/* paint function implementation: just calls paint() on the ClutterBox */ static void cb_button_paint (ClutterActor *actor) { CbButtonPrivate *priv = CB_BUTTON (actor)->priv; clutter_actor_paint (priv->child); }
/* use the actor's allocation for the ClutterBox */ static void cb_button_allocate (ClutterActor *actor, const ClutterActorBox *box, ClutterAllocationFlags flags) { CbButtonPrivate *priv = CB_BUTTON (actor)->priv; ClutterActorBox child_box = { 0, }; /* set the allocation for the whole button */ CLUTTER_ACTOR_CLASS (cb_button_parent_class)->allocate (actor, box, flags); /* make the child (the ClutterBox) fill the parent; * note that this allocation box is relative to the * coordinates of the whole button actor, so we can't just * use the box passed into this function; instead, it * is adjusted to span the whole of the actor, from its * top-left corner (0,0) to its bottom-right corner * (width,height) */ child_box.x1 = 0.0; child_box.y1 = 0.0; child_box.x2 = clutter_actor_box_get_width (box); child_box.y2 = clutter_actor_box_get_height (box); clutter_actor_allocate (priv->child, &child_box, flags); }
/* from http://mail.gnome.org/archives/gtk-devel-list/2004-July/msg00158.html: * * "The finalize method finishes releasing the remaining * resources just before the object itself will be freed from memory, and * therefore it will only be called once. The two step process helps break * cyclic references. Both dispose and finalize must chain up to their * parent objects by calling their parent's respective methods *after* they * have disposed or finalized their own members." */ static void cb_button_finalize (GObject *gobject) { CbButtonPrivate *priv = CB_BUTTON (gobject)->priv; g_free (priv->text); /* call the parent class' finalize() method */ G_OBJECT_CLASS (cb_button_parent_class)->finalize (gobject); }
static void cb_button_get_preferred_width (ClutterActor *self, gfloat for_height, gfloat *min_width_p, gfloat *natural_width_p) { CbButtonPrivate *priv = CB_BUTTON (self)->priv; clutter_actor_get_preferred_width (priv->child, for_height, min_width_p, natural_width_p); *min_width_p += 20.0; *natural_width_p += 20.0; }
/* enables objects to be uniformly treated as GObjects; * also exposes properties so they become scriptable, e.g. * through ClutterScript */ static void cb_button_set_property (GObject *gobject, guint prop_id, const GValue *value, GParamSpec *pspec) { CbButton *button = CB_BUTTON (gobject); switch (prop_id) { case PROP_TEXT: cb_button_set_text (button, g_value_get_string (value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec); break; } }
/* enables objects to be uniformly treated as GObjects */ static void cb_button_get_property (GObject *gobject, guint prop_id, GValue *value, GParamSpec *pspec) { CbButtonPrivate *priv = CB_BUTTON (gobject)->priv; switch (prop_id) { case PROP_TEXT: g_value_set_string (value, priv->text); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec); break; } }
/* composite actors should implement destroy(), and inside their * implementation destroy any actors they are composed from; * in this case, we just destroy the child ClutterBox */ static void cb_button_destroy (ClutterActor *self) { CbButtonPrivate *priv = CB_BUTTON (self)->priv; /* we just destroy the child, and let the child * deal with destroying _its_ children; note that we have a guard * here in case the child has already been destroyed */ if (priv->child) { clutter_actor_destroy (priv->child); priv->child = NULL; } /* chain up to destroy() on the parent ClutterActorClass; * note that we check the parent class has a destroy() implementation * before calling it */ if (CLUTTER_ACTOR_CLASS (cb_button_parent_class)->destroy) CLUTTER_ACTOR_CLASS (cb_button_parent_class)->destroy (self); }