/* First, add the boilerplate for the object itself. *Thispartwouldnormallygointheheader.
*/ #define GTK_TYPE_NUCLEAR_ANIMATION (gtk_nuclear_animation_get_type ())
G_DECLARE_FINAL_TYPE (GtkNuclearAnimation, gtk_nuclear_animation, GTK, NUCLEAR_ANIMATION, GObject)
/* Do a full rotation in 5 seconds. *Wewillregisterthetimeoutfordoingasinglestepto *beexecutedevery10ms,whichmeansafter1000steps *10swillhaveelapsed.
*/ #define MAX_PROGRESS 500
/* Declare the struct. */ struct _GtkNuclearAnimation
{
GObject parent_instance;
gboolean draw_background;
/* This variable stores the progress of our animation. *WejustcountupwardsuntilwehitMAX_PROGRESSand *thenstartfromscratch.
*/ int progress;
/* This variable holds the ID of the timer that updates *ourprogressvariable. *Weneedtokeeptrackofitsothatwecanremoveit *again.
*/
guint source_id;
};
/* For non-static paintables, this function needs to be implemented. *Itmustreturnastaticpaintablewiththesamecontents *asthisonecurrentlyhas. * *Luckilyweaddedtherotationpropertytothenuclearicon *objectpreviously,sowecanjustreturnaninstanceofthatone.
*/ return gtk_nuclear_icon_new (360 * nuclear->progress / MAX_PROGRESS);
}
static GdkPaintableFlags
gtk_nuclear_animation_get_flags (GdkPaintable *paintable)
{ /* This time, we cannot set the static contents flag because our animation *changesthecontents. *However,oursizestilldoesn'tchange,soreportthatflag.
*/ return GDK_PAINTABLE_STATIC_SIZE;
}
/* When defining the GType, we need to implement the GdkPaintable interface */
G_DEFINE_TYPE_WITH_CODE (GtkNuclearAnimation, gtk_nuclear_animation, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE,
gtk_nuclear_animation_paintable_init))
/* This time, we need to implement the finalize function,
*/ staticvoid
gtk_nuclear_animation_finalize (GObject *object)
{
GtkNuclearAnimation *nuclear = GTK_NUCLEAR_ANIMATION (object);
/* Remove the timeout we registered when constructing *theobject.
*/
g_source_remove (nuclear->source_id);
/* Don't forget to chain up to the parent class' implementation *ofthefinalizefunction.
*/
G_OBJECT_CLASS (gtk_nuclear_animation_parent_class)->finalize (object);
}
/* In the class declaration, we need to add our finalize function.
*/ staticvoid
gtk_nuclear_animation_class_init (GtkNuclearAnimationClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
/* Add 1 to the progress and reset it when we've reached *themaximumvalue. *Theanimationwillrotateby360degreesatMAX_PROGRESS *soitwillbeidenticaltotheoriginalunrotatedone.
*/
nuclear->progress = (nuclear->progress + 1) % MAX_PROGRESS;
/* Now we need to tell all listeners that we've changed out contents *sothattheycanredrawthispaintable.
*/
gdk_paintable_invalidate_contents (GDK_PAINTABLE (nuclear));
/* We want this timeout function to be called repeatedly, *sowereturnthisvaluehere. *Ifthiswasasingle-shottimeout,wecouldalso *returnG_SOURCE_REMOVEheretogetridofit.
*/ return G_SOURCE_CONTINUE;
}
staticvoid
gtk_nuclear_animation_init (GtkNuclearAnimation *nuclear)
{ /* Add a timer here that constantly updates our animations. *Wewanttoupdateitoftenenoughtoguaranteeasmoothanimation. * *Ideally,we'dattachtotheframeclock,butbecausewedo *nothaveitavailablehere,wejustusearegulartimeout *thathopefullytriggersoftenenoughtobesmooth.
*/
nuclear->source_id = g_timeout_add (10,
gtk_nuclear_animation_step,
nuclear);
}
/* And finally, we add the simple constructor we declared in the header. */
GdkPaintable *
gtk_nuclear_animation_new (gboolean draw_background)
{
GtkNuclearAnimation *nuclear;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.