Mercurial > hg > forks > geeqie
annotate src/ui_spinner.c @ 2916:ae6cdcd69d9f default tip
Merge with upstream/master.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 14 May 2019 11:46:50 +0300 |
parents | 95507e596256 |
children |
rev | line source |
---|---|
9 | 1 /* |
2350
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
2 * Copyright (C) 2004 John Ellis |
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
3 * Copyright (C) 2008 - 2016 The Geeqie Team |
9 | 4 * |
5 * Author: John Ellis | |
6 * | |
2350
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
7 * This program is free software; you can redistribute it and/or modify |
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
8 * it under the terms of the GNU General Public License as published by |
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
9 * the Free Software Foundation; either version 2 of the License, or |
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
10 * (at your option) any later version. |
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
11 * |
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
12 * This program is distributed in the hope that it will be useful, |
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
15 * GNU General Public License for more details. |
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
16 * |
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
17 * You should have received a copy of the GNU General Public License along |
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
18 * with this program; if not, write to the Free Software Foundation, Inc., |
95507e596256
Update copyright in all files
Klaus Ethgen <Klaus@Ethgen.de>
parents:
2021
diff
changeset
|
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
9 | 20 */ |
21 | |
22 #ifdef HAVE_CONFIG_H | |
23 # include "config.h" | |
24 #endif | |
25 #include "intl.h" | |
442 | 26 |
9 | 27 #include <stdio.h> |
28 #include <stdlib.h> | |
29 #include <string.h> | |
442 | 30 |
9 | 31 #include <gtk/gtk.h> |
32 | |
673
c9c2bc042e10
Do not use printf() directly but use new wrapper function log_printf() instead.
Laurent Monin <geeqie@norz.org>
parents:
513
diff
changeset
|
33 #include "main.h" |
9 | 34 #include "ui_spinner.h" |
35 | |
36 #include "ui_icons.h" | |
37 #include "ui_fileops.h" | |
38 | |
39 | |
40 #define SPINNER_FRAMES 19 | |
41 | |
42 | |
43 /* | |
44 *----------------------------------------------------------------------------- | |
45 * spinner utility | |
46 *----------------------------------------------------------------------------- | |
47 */ | |
48 | |
49 typedef struct _SpinnerData SpinnerData; | |
50 struct _SpinnerData { | |
51 GtkWidget *image; | |
52 GList *list; /* list of pixbufs */ | |
736
31d858f3272d
Fix signed vs unsigned warnings.
Laurent Monin <geeqie@norz.org>
parents:
673
diff
changeset
|
53 guint frame; |
1523
e52aabe8fc7a
Fix up event source ids type: gint -> guint.
Laurent Monin <geeqie@norz.org>
parents:
1448
diff
changeset
|
54 guint timer_id; /* event source id */ |
9 | 55 }; |
56 | |
736
31d858f3272d
Fix signed vs unsigned warnings.
Laurent Monin <geeqie@norz.org>
parents:
673
diff
changeset
|
57 static void spinner_set_frame(SpinnerData *sp, guint frame) |
9 | 58 { |
59 GdkPixbuf *pb; | |
60 | |
61 pb = g_list_nth_data(sp->list, frame); | |
62 if (pb) gtk_image_set_from_pixbuf(GTK_IMAGE(sp->image), pb); | |
63 | |
64 sp->frame = frame; | |
65 } | |
66 | |
67 static void spinner_increment_frame(SpinnerData *sp) | |
68 { | |
69 sp->frame++; | |
70 if (sp->frame >= g_list_length(sp->list)) sp->frame = 1; | |
71 spinner_set_frame(sp, sp->frame); | |
72 } | |
73 | |
74 static gboolean spinner_loop_cb(gpointer data) | |
75 { | |
76 SpinnerData *sp = data; | |
77 | |
78 if (sp->list) spinner_increment_frame(sp); | |
79 | |
80 return TRUE; | |
81 } | |
82 | |
83 static void spinner_set_timeout(SpinnerData *sp, gint interval) | |
84 { | |
85 if (!sp) return; | |
86 | |
1523
e52aabe8fc7a
Fix up event source ids type: gint -> guint.
Laurent Monin <geeqie@norz.org>
parents:
1448
diff
changeset
|
87 if (sp->timer_id) |
9 | 88 { |
89 g_source_remove(sp->timer_id); | |
1523
e52aabe8fc7a
Fix up event source ids type: gint -> guint.
Laurent Monin <geeqie@norz.org>
parents:
1448
diff
changeset
|
90 sp->timer_id = 0; |
9 | 91 } |
92 | |
93 if (interval > 0) | |
94 { | |
95 sp->timer_id = g_timeout_add(interval, spinner_loop_cb, sp); | |
96 } | |
97 else if (interval < 0) | |
98 { | |
99 spinner_set_frame(sp, 0); | |
100 } | |
101 | |
102 gtk_widget_set_sensitive(sp->image, (interval >= 0)); | |
103 } | |
104 | |
105 static void spinner_destroy_cb(GtkWidget *widget, gpointer data) | |
106 { | |
107 SpinnerData *sp = data; | |
108 GList *work; | |
109 | |
110 spinner_set_timeout(sp, 0); | |
111 | |
112 work = sp->list; | |
113 while (work) | |
114 { | |
115 GdkPixbuf *pb = work->data; | |
116 work = work->next; | |
117 | |
118 g_object_unref(pb); | |
119 } | |
120 g_list_free(sp->list); | |
121 g_free(sp); | |
122 } | |
123 | |
124 GtkWidget *spinner_new(const gchar *path, gint interval) | |
125 { | |
126 SpinnerData *sp; | |
127 | |
128 sp = g_new0(SpinnerData, 1); | |
129 | |
130 if (path) | |
131 { | |
132 gchar *pathl; | |
133 GdkPixbuf *pb; | |
134 gint n; | |
135 gchar *buf; | |
136 | |
137 pathl = path_from_utf8(path); | |
138 | |
139 n = 0; | |
140 buf = g_strdup_printf("%s%02d.png", pathl, n); | |
141 while ((pb = gdk_pixbuf_new_from_file(buf, NULL))) | |
142 { | |
143 sp->list = g_list_append(sp->list, pb); | |
144 | |
145 n++; | |
146 g_free(buf); | |
147 buf = g_strdup_printf("%s%02d.png", pathl, n); | |
148 } | |
149 g_free(buf); | |
150 | |
151 g_free(pathl); | |
152 } | |
153 | |
154 if (!sp->list) | |
155 { | |
156 GdkPixbuf *pb; | |
157 gint n; | |
158 gint w, h; | |
159 | |
513
6743a5928e09
Remove whitespace between function name and first parenthesis for the sake of consistency. (pass 2)
Laurent Monin <geeqie@norz.org>
parents:
475
diff
changeset
|
160 pb = gdk_pixbuf_new_from_inline(-1, icon_spinner, FALSE, NULL); |
9 | 161 w = gdk_pixbuf_get_width(pb); |
162 h = gdk_pixbuf_get_height(pb) / SPINNER_FRAMES; | |
163 for (n = 0; n < SPINNER_FRAMES; n++) | |
164 { | |
165 sp->list = g_list_append(sp->list, | |
166 gdk_pixbuf_new_subpixbuf(pb, 0, n * h, w, h)); | |
167 } | |
168 /* pb pixels is inline static, so the subpixbufs in sp->list will be ok */ | |
169 g_object_unref(pb); | |
170 } | |
171 | |
172 if (sp->list) | |
173 { | |
174 GdkPixbuf *pb; | |
175 | |
176 pb = sp->list->data; | |
177 sp->image = gtk_image_new_from_pixbuf(pb); | |
178 } | |
179 else | |
180 { | |
181 sp->image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_DIALOG); | |
182 } | |
183 | |
184 g_object_set_data(G_OBJECT(sp->image), "spinner", sp); | |
185 | |
186 g_signal_connect(G_OBJECT(sp->image), "destroy", | |
187 G_CALLBACK(spinner_destroy_cb), sp); | |
188 | |
189 spinner_set_timeout(sp, interval); | |
190 | |
191 return sp->image; | |
192 } | |
193 | |
194 void spinner_set_interval(GtkWidget *spinner, gint interval) | |
195 { | |
196 SpinnerData *sp; | |
197 | |
198 sp = g_object_get_data(G_OBJECT(spinner), "spinner"); | |
199 | |
200 spinner_set_timeout(sp, interval); | |
201 } | |
202 | |
1448 | 203 void spinner_step(GtkWidget *spinner, gboolean reset) |
9 | 204 { |
205 SpinnerData *sp; | |
206 | |
207 sp = g_object_get_data(G_OBJECT(spinner), "spinner"); | |
1523
e52aabe8fc7a
Fix up event source ids type: gint -> guint.
Laurent Monin <geeqie@norz.org>
parents:
1448
diff
changeset
|
208 if (sp->timer_id) |
9 | 209 { |
673
c9c2bc042e10
Do not use printf() directly but use new wrapper function log_printf() instead.
Laurent Monin <geeqie@norz.org>
parents:
513
diff
changeset
|
210 log_printf("spinner warning: attempt to step with timer set\n"); |
9 | 211 return; |
212 } | |
213 | |
214 if (reset) | |
215 { | |
216 spinner_set_frame(sp, 0); | |
217 } | |
218 else | |
219 { | |
220 spinner_increment_frame(sp); | |
221 } | |
222 } | |
1055
4bbdfab3c89a
Adding a vim modeline to all files - patch by Klaus Ethgen
Vladimir Nadvornik <nadvornik@suse.cz>
parents:
736
diff
changeset
|
223 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ |