Mercurial > geeqie
annotate src/ui_spinner.c @ 1811:f405ec9b696b default tip
Some small logic mistakes
Use boolean operators for booleans and bitwise otherwise only.
| author | mow |
|---|---|
| date | Mon, 10 May 2010 11:33:13 +0000 |
| parents | 956aab097ea7 |
| children |
| rev | line source |
|---|---|
| 9 | 1 /* |
| 2 * (SLIK) SimpLIstic sKin functions | |
| 3 * (C) 2004 John Ellis | |
| 1802 | 4 * Copyright (C) 2008 - 2010 The Geeqie Team |
| 9 | 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" | |
| 442 | 17 |
| 9 | 18 #include <stdio.h> |
| 19 #include <stdlib.h> | |
| 20 #include <string.h> | |
| 442 | 21 |
| 9 | 22 #include <gtk/gtk.h> |
| 23 | |
|
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
513
diff
changeset
|
24 #include "main.h" |
| 9 | 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 */ | |
| 736 | 44 guint frame; |
| 1523 | 45 guint timer_id; /* event source id */ |
| 9 | 46 }; |
| 47 | |
| 736 | 48 static void spinner_set_frame(SpinnerData *sp, guint frame) |
| 9 | 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 | |
| 1523 | 78 if (sp->timer_id) |
| 9 | 79 { |
| 80 g_source_remove(sp->timer_id); | |
| 1523 | 81 sp->timer_id = 0; |
| 9 | 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 | |
|
513
985fdfebd89e
Remove whitespace between function name and first parenthesis for the sake of consistency. (pass 2)
zas_
parents:
475
diff
changeset
|
151 pb = gdk_pixbuf_new_from_inline(-1, icon_spinner, FALSE, NULL); |
| 9 | 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 | |
| 1448 | 194 void spinner_step(GtkWidget *spinner, gboolean reset) |
| 9 | 195 { |
| 196 SpinnerData *sp; | |
| 197 | |
| 198 sp = g_object_get_data(G_OBJECT(spinner), "spinner"); | |
| 1523 | 199 if (sp->timer_id) |
| 9 | 200 { |
|
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
513
diff
changeset
|
201 log_printf("spinner warning: attempt to step with timer set\n"); |
| 9 | 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 } | |
|
1055
1646720364cf
Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents:
736
diff
changeset
|
214 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ |
