+static GdkPixbuf *framebuf;
+
+void play_toggled(GtkToggleButton *button, gpointer data)
+{
+ gboolean active = gtk_toggle_button_get_active(button);
+ GtkWidget *spin;
+
+ if (active && !image) {
+ gtk_toggle_button_set_active(button, FALSE);
+ return;
+ }
+
+ spin = GTK_WIDGET(gtk_builder_get_object(builder, "framespin"));
+ gtk_widget_set_sensitive(spin, !active);
+
+ if (active) {
+ gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), 0);
+ }
+}
+
+void set_frame(GtkSpinButton *spin, gpointer data)
+{
+ unsigned frame = gtk_spin_button_get_value_as_int(spin);
+
+ if (image)
+ render_to_pixbuf(image, framebuf, frame);
+ gdk_window_invalidate_rect(canvas->window, NULL, FALSE);
+}
+
+static void redraw_image(void)
+{
+ GtkSpinButton *spin;
+
+ spin = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "framespin"));
+ set_frame(spin, NULL);
+}
+
+static void tick(void *p, double delta)
+{
+ static double elapsed = 0;
+ double seconds_per_frame = 1.0/15;
+ struct lbx_imginfo info;
+ GtkSpinButton *spin;
+ GtkToggleButton *play;
+ unsigned frame;
+
+ if (!image)
+ return;
+
+ lbximg_getinfo(image, &info);
+ spin = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "framespin"));
+ play = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "playbutton"));
+
+ frame = gtk_spin_button_get_value_as_int(spin);
+ if (!gtk_toggle_button_get_active(play))
+ return;
+
+ elapsed += delta;
+ while (elapsed > seconds_per_frame) {
+ elapsed -= seconds_per_frame;
+
+ if (++frame >= info.nframes) {
+ if (!info.looping) {
+ gtk_toggle_button_set_active(play, FALSE);
+ break;
+ }
+
+ frame = info.loopstart;
+ }
+
+ gtk_spin_button_set_value(spin, frame);
+ }
+}