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.
23 #include "ui-spinner.h"
26 #include "ui-fileops.h"
29 #define SPINNER_FRAMES 19
33 *-----------------------------------------------------------------------------
35 *-----------------------------------------------------------------------------
38 typedef struct _SpinnerData SpinnerData;
41 GList *list; /* list of pixbufs */
43 guint timer_id; /* event source id */
46 static void spinner_set_frame(SpinnerData *sp, guint frame)
50 pb = static_cast<GdkPixbuf *>(g_list_nth_data(sp->list, frame));
51 if (pb) gtk_image_set_from_pixbuf(GTK_IMAGE(sp->image), pb);
56 static void spinner_increment_frame(SpinnerData *sp)
59 if (sp->frame >= g_list_length(sp->list)) sp->frame = 1;
60 spinner_set_frame(sp, sp->frame);
63 static gboolean spinner_loop_cb(gpointer data)
65 SpinnerData *sp = static_cast<SpinnerData *>(data);
67 if (sp->list) spinner_increment_frame(sp);
72 static void spinner_set_timeout(SpinnerData *sp, gint interval)
78 g_source_remove(sp->timer_id);
84 sp->timer_id = g_timeout_add(interval, spinner_loop_cb, sp);
86 else if (interval < 0)
88 spinner_set_frame(sp, 0);
91 gtk_widget_set_sensitive(sp->image, (interval >= 0));
94 static void spinner_destroy_cb(GtkWidget *UNUSED(widget), gpointer data)
96 SpinnerData *sp = static_cast<SpinnerData *>(data);
99 spinner_set_timeout(sp, 0);
104 GdkPixbuf *pb = static_cast<GdkPixbuf *>(work->data);
109 g_list_free(sp->list);
113 GtkWidget *spinner_new(const gchar *path, gint interval)
117 sp = g_new0(SpinnerData, 1);
126 pathl = path_from_utf8(path);
129 buf = g_strdup_printf("%s%02d.png", pathl, n);
130 while ((pb = gdk_pixbuf_new_from_file(buf, NULL)))
132 sp->list = g_list_append(sp->list, pb);
136 buf = g_strdup_printf("%s%02d.png", pathl, n);
149 pb = gdk_pixbuf_new_from_inline(-1, icon_spinner, FALSE, NULL);
150 w = gdk_pixbuf_get_width(pb);
151 h = gdk_pixbuf_get_height(pb) / SPINNER_FRAMES;
152 for (n = 0; n < SPINNER_FRAMES; n++)
154 sp->list = g_list_append(sp->list,
155 gdk_pixbuf_new_subpixbuf(pb, 0, n * h, w, h));
157 /* pb pixels is inline static, so the subpixbufs in sp->list will be ok */
165 pb = static_cast<GdkPixbuf *>(sp->list->data);
166 sp->image = gtk_image_new_from_pixbuf(pb);
170 sp->image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_DIALOG);
173 g_object_set_data(G_OBJECT(sp->image), "spinner", sp);
175 g_signal_connect(G_OBJECT(sp->image), "destroy",
176 G_CALLBACK(spinner_destroy_cb), sp);
178 spinner_set_timeout(sp, interval);
183 void spinner_set_interval(GtkWidget *spinner, gint interval)
187 sp = static_cast<SpinnerData *>(g_object_get_data(G_OBJECT(spinner), "spinner"));
189 spinner_set_timeout(sp, interval);
192 //void spinner_step(GtkWidget *spinner, gboolean reset)
196 //sp = g_object_get_data(G_OBJECT(spinner), "spinner");
199 //log_printf("spinner warning: attempt to step with timer set\n");
205 //spinner_set_frame(sp, 0);
209 //spinner_increment_frame(sp);
212 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */