2 * (SLIK) SimpLIstic sKin functions
4 * Copyright (C) 2008 - 2012 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
25 #include "ui_spinner.h"
28 #include "ui_fileops.h"
31 #define SPINNER_FRAMES 19
35 *-----------------------------------------------------------------------------
37 *-----------------------------------------------------------------------------
40 typedef struct _SpinnerData SpinnerData;
43 GList *list; /* list of pixbufs */
45 guint timer_id; /* event source id */
48 static void spinner_set_frame(SpinnerData *sp, guint frame)
52 pb = g_list_nth_data(sp->list, frame);
53 if (pb) gtk_image_set_from_pixbuf(GTK_IMAGE(sp->image), pb);
58 static void spinner_increment_frame(SpinnerData *sp)
61 if (sp->frame >= g_list_length(sp->list)) sp->frame = 1;
62 spinner_set_frame(sp, sp->frame);
65 static gboolean spinner_loop_cb(gpointer data)
67 SpinnerData *sp = data;
69 if (sp->list) spinner_increment_frame(sp);
74 static void spinner_set_timeout(SpinnerData *sp, gint interval)
80 g_source_remove(sp->timer_id);
86 sp->timer_id = g_timeout_add(interval, spinner_loop_cb, sp);
88 else if (interval < 0)
90 spinner_set_frame(sp, 0);
93 gtk_widget_set_sensitive(sp->image, (interval >= 0));
96 static void spinner_destroy_cb(GtkWidget *widget, gpointer data)
98 SpinnerData *sp = data;
101 spinner_set_timeout(sp, 0);
106 GdkPixbuf *pb = work->data;
111 g_list_free(sp->list);
115 GtkWidget *spinner_new(const gchar *path, gint interval)
119 sp = g_new0(SpinnerData, 1);
128 pathl = path_from_utf8(path);
131 buf = g_strdup_printf("%s%02d.png", pathl, n);
132 while ((pb = gdk_pixbuf_new_from_file(buf, NULL)))
134 sp->list = g_list_append(sp->list, pb);
138 buf = g_strdup_printf("%s%02d.png", pathl, n);
151 pb = gdk_pixbuf_new_from_inline(-1, icon_spinner, FALSE, NULL);
152 w = gdk_pixbuf_get_width(pb);
153 h = gdk_pixbuf_get_height(pb) / SPINNER_FRAMES;
154 for (n = 0; n < SPINNER_FRAMES; n++)
156 sp->list = g_list_append(sp->list,
157 gdk_pixbuf_new_subpixbuf(pb, 0, n * h, w, h));
159 /* pb pixels is inline static, so the subpixbufs in sp->list will be ok */
168 sp->image = gtk_image_new_from_pixbuf(pb);
172 sp->image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_DIALOG);
175 g_object_set_data(G_OBJECT(sp->image), "spinner", sp);
177 g_signal_connect(G_OBJECT(sp->image), "destroy",
178 G_CALLBACK(spinner_destroy_cb), sp);
180 spinner_set_timeout(sp, interval);
185 void spinner_set_interval(GtkWidget *spinner, gint interval)
189 sp = g_object_get_data(G_OBJECT(spinner), "spinner");
191 spinner_set_timeout(sp, interval);
194 void spinner_step(GtkWidget *spinner, gboolean reset)
198 sp = g_object_get_data(G_OBJECT(spinner), "spinner");
201 log_printf("spinner warning: attempt to step with timer set\n");
207 spinner_set_frame(sp, 0);
211 spinner_increment_frame(sp);
214 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */