2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 #include "ui_spinner.h"
37 #include "ui_fileops.h"
40 #define SPINNER_FRAMES 19
44 *-----------------------------------------------------------------------------
46 *-----------------------------------------------------------------------------
49 typedef struct _SpinnerData SpinnerData;
52 GList *list; /* list of pixbufs */
54 guint timer_id; /* event source id */
57 static void spinner_set_frame(SpinnerData *sp, guint frame)
61 pb = g_list_nth_data(sp->list, frame);
62 if (pb) gtk_image_set_from_pixbuf(GTK_IMAGE(sp->image), pb);
67 static void spinner_increment_frame(SpinnerData *sp)
70 if (sp->frame >= g_list_length(sp->list)) sp->frame = 1;
71 spinner_set_frame(sp, sp->frame);
74 static gboolean spinner_loop_cb(gpointer data)
76 SpinnerData *sp = data;
78 if (sp->list) spinner_increment_frame(sp);
83 static void spinner_set_timeout(SpinnerData *sp, gint interval)
89 g_source_remove(sp->timer_id);
95 sp->timer_id = g_timeout_add(interval, spinner_loop_cb, sp);
97 else if (interval < 0)
99 spinner_set_frame(sp, 0);
102 gtk_widget_set_sensitive(sp->image, (interval >= 0));
105 static void spinner_destroy_cb(GtkWidget *widget, gpointer data)
107 SpinnerData *sp = data;
110 spinner_set_timeout(sp, 0);
115 GdkPixbuf *pb = work->data;
120 g_list_free(sp->list);
124 GtkWidget *spinner_new(const gchar *path, gint interval)
128 sp = g_new0(SpinnerData, 1);
137 pathl = path_from_utf8(path);
140 buf = g_strdup_printf("%s%02d.png", pathl, n);
141 while ((pb = gdk_pixbuf_new_from_file(buf, NULL)))
143 sp->list = g_list_append(sp->list, pb);
147 buf = g_strdup_printf("%s%02d.png", pathl, n);
160 pb = gdk_pixbuf_new_from_inline(-1, icon_spinner, FALSE, NULL);
161 w = gdk_pixbuf_get_width(pb);
162 h = gdk_pixbuf_get_height(pb) / SPINNER_FRAMES;
163 for (n = 0; n < SPINNER_FRAMES; n++)
165 sp->list = g_list_append(sp->list,
166 gdk_pixbuf_new_subpixbuf(pb, 0, n * h, w, h));
168 /* pb pixels is inline static, so the subpixbufs in sp->list will be ok */
177 sp->image = gtk_image_new_from_pixbuf(pb);
181 sp->image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_DIALOG);
184 g_object_set_data(G_OBJECT(sp->image), "spinner", sp);
186 g_signal_connect(G_OBJECT(sp->image), "destroy",
187 G_CALLBACK(spinner_destroy_cb), sp);
189 spinner_set_timeout(sp, interval);
194 void spinner_set_interval(GtkWidget *spinner, gint interval)
198 sp = g_object_get_data(G_OBJECT(spinner), "spinner");
200 spinner_set_timeout(sp, interval);
203 void spinner_step(GtkWidget *spinner, gboolean reset)
207 sp = g_object_get_data(G_OBJECT(spinner), "spinner");
210 log_printf("spinner warning: attempt to step with timer set\n");
216 spinner_set_frame(sp, 0);
220 spinner_increment_frame(sp);
223 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */