Remove commented out code.
[geeqie.git] / src / ui_spinner.c
1 /*
2  * (SLIK) SimpLIstic sKin functions
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2012 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
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!
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #  include "config.h"
15 #endif
16 #include "intl.h"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include <gtk/gtk.h>
23
24 #include "main.h"
25 #include "ui_spinner.h"
26
27 #include "ui_icons.h"
28 #include "ui_fileops.h"
29
30
31 #define SPINNER_FRAMES 19
32
33
34 /*
35  *-----------------------------------------------------------------------------
36  * spinner utility
37  *-----------------------------------------------------------------------------
38  */
39
40 typedef struct _SpinnerData SpinnerData;
41 struct _SpinnerData {
42         GtkWidget *image;
43         GList *list;            /* list of pixbufs */
44         guint frame;
45         guint timer_id; /* event source id */
46 };
47
48 static void spinner_set_frame(SpinnerData *sp, guint frame)
49 {
50         GdkPixbuf *pb;
51
52         pb = g_list_nth_data(sp->list, frame);
53         if (pb) gtk_image_set_from_pixbuf(GTK_IMAGE(sp->image), pb);
54
55         sp->frame = frame;
56 }
57
58 static void spinner_increment_frame(SpinnerData *sp)
59 {
60         sp->frame++;
61         if (sp->frame >= g_list_length(sp->list)) sp->frame = 1;
62         spinner_set_frame(sp, sp->frame);
63 }
64
65 static gboolean spinner_loop_cb(gpointer data)
66 {
67         SpinnerData *sp = data;
68
69         if (sp->list) spinner_increment_frame(sp);
70
71         return TRUE;
72 }
73
74 static void spinner_set_timeout(SpinnerData *sp, gint interval)
75 {
76         if (!sp) return;
77
78         if (sp->timer_id)
79                 {
80                 g_source_remove(sp->timer_id);
81                 sp->timer_id = 0;
82                 }
83
84         if (interval > 0)
85                 {
86                 sp->timer_id = g_timeout_add(interval, spinner_loop_cb, sp);
87                 }
88         else if (interval < 0)
89                 {
90                 spinner_set_frame(sp, 0);
91                 }
92
93         gtk_widget_set_sensitive(sp->image, (interval >= 0));
94 }
95
96 static void spinner_destroy_cb(GtkWidget *widget, gpointer data)
97 {
98         SpinnerData *sp = data;
99         GList *work;
100
101         spinner_set_timeout(sp, 0);
102
103         work = sp->list;
104         while (work)
105                 {
106                 GdkPixbuf *pb = work->data;
107                 work = work->next;
108
109                 g_object_unref(pb);
110                 }
111         g_list_free(sp->list);
112         g_free(sp);
113 }
114
115 GtkWidget *spinner_new(const gchar *path, gint interval)
116 {
117         SpinnerData *sp;
118
119         sp = g_new0(SpinnerData, 1);
120
121         if (path)
122                 {
123                 gchar *pathl;
124                 GdkPixbuf *pb;
125                 gint n;
126                 gchar *buf;
127
128                 pathl = path_from_utf8(path);
129
130                 n = 0;
131                 buf = g_strdup_printf("%s%02d.png", pathl, n);
132                 while ((pb = gdk_pixbuf_new_from_file(buf, NULL)))
133                         {
134                         sp->list = g_list_append(sp->list, pb);
135
136                         n++;
137                         g_free(buf);
138                         buf = g_strdup_printf("%s%02d.png", pathl, n);
139                         }
140                 g_free(buf);
141
142                 g_free(pathl);
143                 }
144
145         if (!sp->list)
146                 {
147                 GdkPixbuf *pb;
148                 gint n;
149                 gint w, h;
150
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++)
155                         {
156                         sp->list = g_list_append(sp->list,
157                                                  gdk_pixbuf_new_subpixbuf(pb, 0, n * h, w, h));
158                         }
159                 /* pb pixels is inline static, so the subpixbufs in sp->list will be ok */
160                 g_object_unref(pb);
161                 }
162
163         if (sp->list)
164                 {
165                 GdkPixbuf *pb;
166
167                 pb = sp->list->data;
168                 sp->image = gtk_image_new_from_pixbuf(pb);
169                 }
170         else
171                 {
172                 sp->image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_DIALOG);
173                 }
174
175         g_object_set_data(G_OBJECT(sp->image), "spinner", sp);
176
177         g_signal_connect(G_OBJECT(sp->image), "destroy",
178                          G_CALLBACK(spinner_destroy_cb), sp);
179
180         spinner_set_timeout(sp, interval);
181
182         return sp->image;
183 }
184
185 void spinner_set_interval(GtkWidget *spinner, gint interval)
186 {
187         SpinnerData *sp;
188
189         sp = g_object_get_data(G_OBJECT(spinner), "spinner");
190
191         spinner_set_timeout(sp, interval);
192 }
193
194 void spinner_step(GtkWidget *spinner, gboolean reset)
195 {
196         SpinnerData *sp;
197
198         sp = g_object_get_data(G_OBJECT(spinner), "spinner");
199         if (sp->timer_id)
200                 {
201                 log_printf("spinner warning: attempt to step with timer set\n");
202                 return;
203                 }
204
205         if (reset)
206                 {
207                 spinner_set_frame(sp, 0);
208                 }
209         else
210                 {
211                 spinner_increment_frame(sp);
212                 }
213 }
214 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */