aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-spinner.c
blob: 56990d71a14a0557a2890fa8f6ff08e6684ba056 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
/*
 * Copyright © 2000 Eazel, Inc.
 * Copyright © 2002-2004 Marco Pesenti Gritti
 * Copyright © 2004, 2006 Christian Persch
 *
 * Nautilus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Nautilus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author: Andy Hertzfeld <andy@eazel.com>
 *
 * Ephy port by Marco Pesenti Gritti <marco@it.gnome.org>
 *
 * $Id: e-spinner.c 12639 2006-12-12 17:06:55Z chpe $
 */

/*
 * From Nautilus ephy-spinner.c
 */

#include "config.h"

#include "e-spinner.h"

#define E_TYPE_SPINNER      (e_spinner_get_type ())
#define E_SPINNER(o)            (G_TYPE_CHECK_INSTANCE_CAST ((o), E_TYPE_SPINNER, ESpinner))
#define E_SPINNER_CLASS(k)      (G_TYPE_CHECK_CLASS_CAST((k), E_TYPE_SPINNER, ESpinnerClass))
#define E_IS_SPINNER(o)     (G_TYPE_CHECK_INSTANCE_TYPE ((o), E_TYPE_SPINNER))
#define E_IS_SPINNER_CLASS(k)   (G_TYPE_CHECK_CLASS_TYPE ((k), E_TYPE_SPINNER))
#define E_SPINNER_GET_CLASS(o)  (G_TYPE_INSTANCE_GET_CLASS ((o), E_TYPE_SPINNER, ESpinnerClass))

typedef struct _ESpinner        ESpinner;
typedef struct _ESpinnerClass   ESpinnerClass;
typedef struct _ESpinnerDetails ESpinnerDetails;

struct _ESpinner
{
    GtkWidget parent;

    /*< private >*/
    ESpinnerDetails *details;
};

struct _ESpinnerClass
{
    GtkWidgetClass parent_class;
};

#define LOG(msg, args...)
#define START_PROFILER(name)
#define STOP_PROFILER(name)

#include "e-util/e-icon-factory.h"

/* Spinner cache implementation */

#define E_TYPE_SPINNER_CACHE            (e_spinner_cache_get_type())
#define E_SPINNER_CACHE(object)     (G_TYPE_CHECK_INSTANCE_CAST((object), E_TYPE_SPINNER_CACHE, ESpinnerCache))
#define E_SPINNER_CACHE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass), E_TYPE_SPINNER_CACHE, ESpinnerCacheClass))
#define E_IS_SPINNER_CACHE(object)      (G_TYPE_CHECK_INSTANCE_TYPE((object), E_TYPE_SPINNER_CACHE))
#define E_IS_SPINNER_CACHE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), E_TYPE_SPINNER_CACHE))
#define E_SPINNER_CACHE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj), E_TYPE_SPINNER_CACHE, ESpinnerCacheClass))

typedef struct _ESpinnerCache   ESpinnerCache;
typedef struct _ESpinnerCacheClass  ESpinnerCacheClass;
typedef struct _ESpinnerCachePrivate    ESpinnerCachePrivate;

struct _ESpinnerCacheClass
{
    GObjectClass parent_class;
};

struct _ESpinnerCache
{
    GObject parent_object;

    /*< private >*/
    ESpinnerCachePrivate *priv;
};

#define E_SPINNER_CACHE_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), E_TYPE_SPINNER_CACHE, ESpinnerCachePrivate))

struct _ESpinnerCachePrivate
{
    /* Hash table of GdkScreen -> ESpinnerCacheData */
    GHashTable *hash;
};

typedef struct
{
    guint ref_count;
    GtkIconSize size;
    int width;
    int height;
    GdkPixbuf **animation_pixbufs;
    guint n_animation_pixbufs;
} ESpinnerImages;

#define LAST_ICON_SIZE          GTK_ICON_SIZE_DIALOG + 1
#define SPINNER_ICON_NAME       "process-working"
#define SPINNER_FALLBACK_ICON_NAME  "gnome-spinner"
#define E_SPINNER_IMAGES_INVALID    ((ESpinnerImages *) 0x1)

typedef struct
{
    GdkScreen *screen;
    GtkIconTheme *icon_theme;
    ESpinnerImages *images[LAST_ICON_SIZE];
} ESpinnerCacheData;

static void e_spinner_cache_class_init (ESpinnerCacheClass *klass);
static void e_spinner_cache_init      (ESpinnerCache *cache);

static GObjectClass *e_spinner_cache_parent_class;
static gpointer spinner_cache = NULL;

static GType
e_spinner_cache_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0))
    {
        const GTypeInfo our_info =
        {
            sizeof (ESpinnerCacheClass),
            NULL,
            NULL,
            (GClassInitFunc) e_spinner_cache_class_init,
            NULL,
            NULL,
            sizeof (ESpinnerCache),
            0,
            (GInstanceInitFunc) e_spinner_cache_init
        };

        type = g_type_register_static (G_TYPE_OBJECT,
                           "ESpinnerCache",
                           &our_info, 0);
    }

    return type;
}

static ESpinnerImages *
e_spinner_images_ref (ESpinnerImages *images)
{
    g_return_val_if_fail (images != NULL, NULL);

    images->ref_count++;

    return images;
}

static void
e_spinner_images_unref (ESpinnerImages *images)
{
    g_return_if_fail (images != NULL);

    images->ref_count--;
    if (images->ref_count == 0)
    {
        guint i;

        LOG ("Freeing spinner images %p for size %d", images, images->size);

        for (i = 0; i < images->n_animation_pixbufs; ++i)
        {
            g_object_unref (images->animation_pixbufs[i]);
        }
        g_free (images->animation_pixbufs);

        g_free (images);
    }
}

static void
e_spinner_cache_data_unload (ESpinnerCacheData *data)
{
    GtkIconSize size;
    ESpinnerImages *images;

    g_return_if_fail (data != NULL);

    LOG ("ESpinnerDataCache unload for screen %p", data->screen);

    for (size = GTK_ICON_SIZE_INVALID; size < LAST_ICON_SIZE; ++size)
    {
        images = data->images[size];
        data->images[size] = NULL;

        if (images != NULL && images != E_SPINNER_IMAGES_INVALID)
        {
            e_spinner_images_unref (images);
        }
    }
}

static GdkPixbuf *
extract_frame (GdkPixbuf *grid_pixbuf,
           int x,
           int y,
           int size)
{
    GdkPixbuf *pixbuf;

    if (x + size > gdk_pixbuf_get_width (grid_pixbuf) ||
        y + size > gdk_pixbuf_get_height (grid_pixbuf))
    {
        return NULL;
    }

    pixbuf = gdk_pixbuf_new_subpixbuf (grid_pixbuf,
                       x, y,
                       size, size);
    g_return_val_if_fail (pixbuf != NULL, NULL);

    return pixbuf;
}

static GdkPixbuf *
scale_to_size (GdkPixbuf *pixbuf,
           int dw,
           int dh)
{
    GdkPixbuf *result;
    int pw, ph;

    g_return_val_if_fail (pixbuf != NULL, NULL);

    pw = gdk_pixbuf_get_width (pixbuf);
    ph = gdk_pixbuf_get_height (pixbuf);

    if (pw != dw || ph != dh)
    {
        result = e_icon_factory_pixbuf_scale (pixbuf, dw, dh);
        g_object_unref (pixbuf);
        return result;
    }

    return pixbuf;
}

static ESpinnerImages *
e_spinner_images_load (GdkScreen *screen,
              GtkIconTheme *icon_theme,
              GtkIconSize icon_size)
{
    ESpinnerImages *images;
    GdkPixbuf *icon_pixbuf, *pixbuf;
    GtkIconInfo *icon_info = NULL;
    int grid_width, grid_height, x, y, requested_size, size, isw, ish, n;
    const char *icon;
    GSList *list = NULL, *l;

    LOG ("ESpinnerCacheData loading for screen %p at size %d", screen, icon_size);

    START_PROFILER ("loading spinner animation")

    if (!gtk_icon_size_lookup_for_settings (gtk_settings_get_for_screen (screen),
                        icon_size, &isw, &ish)) goto loser;

    requested_size = MAX (ish, isw);

    /* Load the animation. The 'rest icon' is the 0th frame */
    icon_info = gtk_icon_theme_lookup_icon (icon_theme,
                        SPINNER_ICON_NAME,
                            requested_size, 0);
    if (icon_info == NULL)
    {
        g_warning ("Throbber animation not found");

        /* If the icon naming spec compliant name wasn't found, try the old name */
        icon_info = gtk_icon_theme_lookup_icon (icon_theme,
                            SPINNER_FALLBACK_ICON_NAME,
                            requested_size, 0);
        if (icon_info == NULL)
        {
            g_warning ("Throbber fallback animation not found either");
            goto loser;
        }
    }
    if (icon_info == NULL) {
        g_warning ("icon_info is NULL");
        goto loser;
    }

    size = gtk_icon_info_get_base_size (icon_info);
    icon = gtk_icon_info_get_filename (icon_info);
    if (icon == NULL) goto loser;

    icon_pixbuf = gdk_pixbuf_new_from_file (icon, NULL);
    gtk_icon_info_free (icon_info);
    icon_info = NULL;

    if (icon_pixbuf == NULL)
    {
        g_warning ("Could not load the spinner file");
        goto loser;
    }

    grid_width = gdk_pixbuf_get_width (icon_pixbuf);
    grid_height = gdk_pixbuf_get_height (icon_pixbuf);

    n = 0;
    for (y = 0; y < grid_height; y += size)
    {
        for (x = 0; x < grid_width ; x += size)
        {
            pixbuf = extract_frame (icon_pixbuf, x, y, size);

            if (pixbuf)
            {
                list = g_slist_prepend (list, pixbuf);
                ++n;
            }
            else
            {
                g_warning ("Cannot extract frame (%d, %d) from the grid\n", x, y);
            }
        }
    }

    g_object_unref (icon_pixbuf);

    if (list == NULL || n <= 0) goto loser;

    if (size > requested_size)
    {
        for (l = list; l != NULL; l = l->next)
        {
            l->data = scale_to_size (l->data, isw, ish);
        }
    }

    /* Now we've successfully got all the data */
    images = g_new (ESpinnerImages, 1);
    images->ref_count = 1;

    images->size = icon_size;
    images->width = images->height = requested_size;

    images->n_animation_pixbufs = n;
    images->animation_pixbufs = g_new (GdkPixbuf *, n);

    for (l = list; l != NULL; l = l->next)
    {
        if (l->data)
            images->animation_pixbufs[--n] = l->data;
    }
    if (n != 0) {
        g_warning ("n != 0 \n");
    }

    g_slist_free (list);

    STOP_PROFILER ("loading spinner animation")

    return images;

loser:
    if (icon_info)
    {
        gtk_icon_info_free (icon_info);
    }
    g_slist_foreach (list, (GFunc) g_object_unref, NULL);

    STOP_PROFILER ("loading spinner animation")

    return NULL;
}

static ESpinnerCacheData *
e_spinner_cache_data_new (GdkScreen *screen)
{
    ESpinnerCacheData *data;

    data = g_new0 (ESpinnerCacheData, 1);

    data->screen = screen;
    data->icon_theme = gtk_icon_theme_get_for_screen (screen);
    g_signal_connect_swapped (data->icon_theme, "changed",
                  G_CALLBACK (e_spinner_cache_data_unload),
                  data);

    return data;
}

static void
e_spinner_cache_data_free (ESpinnerCacheData *data)
{
    g_return_if_fail (data != NULL);
    g_return_if_fail (data->icon_theme != NULL);

    g_signal_handlers_disconnect_by_func
        (data->icon_theme,
         G_CALLBACK (e_spinner_cache_data_unload), data);

    e_spinner_cache_data_unload (data);

    g_free (data);
}

static ESpinnerImages *
e_spinner_cache_get_images (ESpinnerCache *cache,
                   GdkScreen *screen,
                   GtkIconSize icon_size)
{
    ESpinnerCachePrivate *priv = cache->priv;
    ESpinnerCacheData *data;
    ESpinnerImages *images;

    LOG ("Getting animation images for screen %p at size %d", screen, icon_size);

    g_return_val_if_fail (icon_size >= 0 && icon_size < LAST_ICON_SIZE, NULL);

    /* Backward compat: "invalid" meant "native" size which doesn't exist anymore */
    if (icon_size == GTK_ICON_SIZE_INVALID)
    {
        icon_size = GTK_ICON_SIZE_DIALOG;
    }

    data = g_hash_table_lookup (priv->hash, screen);
    if (data == NULL)
    {
        data = e_spinner_cache_data_new (screen);
        /* FIXME: think about what happens when the screen's display is closed later on */
        g_hash_table_insert (priv->hash, screen, data);
    }

    images = data->images[icon_size];
    if (images == E_SPINNER_IMAGES_INVALID)
    {
        /* Load failed, but don't try endlessly again! */
        return NULL;
    }

    if (images != NULL)
    {
        /* Return cached data */
        return e_spinner_images_ref (images);
    }

    images = e_spinner_images_load (screen, data->icon_theme, icon_size);

    if (images == NULL)
    {
        /* Mark as failed-to-load */
        data->images[icon_size] = E_SPINNER_IMAGES_INVALID;

        return NULL;
    }

    data->images[icon_size] = images;

    return e_spinner_images_ref (images);
}

static void
e_spinner_cache_init (ESpinnerCache *cache)
{
    ESpinnerCachePrivate *priv;

    priv = cache->priv = E_SPINNER_CACHE_GET_PRIVATE (cache);

    LOG ("ESpinnerCache initialising");

    priv->hash = g_hash_table_new_full (g_direct_hash, g_direct_equal,
                        NULL,
                        (GDestroyNotify) e_spinner_cache_data_free);
}

static void
e_spinner_cache_finalize (GObject *object)
{
    ESpinnerCache *cache = E_SPINNER_CACHE (object);
    ESpinnerCachePrivate *priv = cache->priv;

    g_hash_table_destroy (priv->hash);

    LOG ("ESpinnerCache finalised");

    G_OBJECT_CLASS (e_spinner_cache_parent_class)->finalize (object);
}

static void
e_spinner_cache_class_init (ESpinnerCacheClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    e_spinner_cache_parent_class = g_type_class_peek_parent (klass);

    object_class->finalize = e_spinner_cache_finalize;

    g_type_class_add_private (object_class, sizeof (ESpinnerCachePrivate));
}

static ESpinnerCache *
e_spinner_cache_ref (void)
{
    if (G_UNLIKELY (spinner_cache == NULL))
    {
        spinner_cache = g_object_new (E_TYPE_SPINNER_CACHE, NULL);
        g_object_add_weak_pointer (
                        G_OBJECT (spinner_cache), &spinner_cache);
    }

    return g_object_ref_sink (spinner_cache);
}

/* Spinner implementation */

#define SPINNER_TIMEOUT 125 /* ms */

#define E_SPINNER_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), E_TYPE_SPINNER, ESpinnerDetails))

struct _ESpinnerDetails
{
    GtkIconTheme *icon_theme;
    ESpinnerCache *cache;
    GtkIconSize size;
    ESpinnerImages *images;
    guint current_image;
    guint timeout;
    guint timer_task;
    guint spinning : 1;
    guint need_load : 1;
};

static void e_spinner_class_init (ESpinnerClass *class);
static void e_spinner_init      (ESpinner *spinner);

static GObjectClass *parent_class;

static GType
e_spinner_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0))
    {
        const GTypeInfo our_info =
        {
            sizeof (ESpinnerClass),
            NULL, /* base_init */
            NULL, /* base_finalize */
            (GClassInitFunc) e_spinner_class_init,
            NULL,
            NULL, /* class_data */
            sizeof (ESpinner),
            0, /* n_preallocs */
            (GInstanceInitFunc) e_spinner_init
        };

        type = g_type_register_static (GTK_TYPE_WIDGET,
                           "ESpinner",
                           &our_info, 0);
    }

    return type;
}

static gboolean
e_spinner_load_images (ESpinner *spinner)
{
    ESpinnerDetails *details = spinner->details;

    if (details->need_load)
    {
        START_PROFILER ("e_spinner_load_images")

        details->images =
            e_spinner_cache_get_images
                (details->cache,
                 gtk_widget_get_screen (GTK_WIDGET (spinner)),
                 details->size);

        STOP_PROFILER ("e_spinner_load_images")

        details->current_image = 0; /* 'rest' icon */
        details->need_load = FALSE;
    }

    return details->images != NULL;
}

static void
e_spinner_unload_images (ESpinner *spinner)
{
    ESpinnerDetails *details = spinner->details;

    if (details->images != NULL)
    {
        e_spinner_images_unref (details->images);
        details->images = NULL;
    }

    details->current_image = 0;
    details->need_load = TRUE;
}

static void
icon_theme_changed_cb (GtkIconTheme *icon_theme,
               ESpinner *spinner)
{
    e_spinner_unload_images (spinner);
    gtk_widget_queue_resize (GTK_WIDGET (spinner));
}

static void
e_spinner_init (ESpinner *spinner)
{
    ESpinnerDetails *details;

    details = spinner->details = E_SPINNER_GET_PRIVATE (spinner);

    GTK_WIDGET_SET_FLAGS (GTK_WIDGET (spinner), GTK_NO_WINDOW);

    details->cache = e_spinner_cache_ref ();
    details->size = GTK_ICON_SIZE_DIALOG;
    details->spinning = FALSE;
    details->timeout = SPINNER_TIMEOUT;
    details->need_load = TRUE;
}

static int
e_spinner_expose (GtkWidget *widget,
             GdkEventExpose *event)
{
    ESpinner *spinner = E_SPINNER (widget);
    ESpinnerDetails *details = spinner->details;
    ESpinnerImages *images;
    GdkPixbuf *pixbuf;
    GdkGC *gc;
    int x_offset, y_offset, width, height;
    GdkRectangle pix_area, dest;

    if (!GTK_WIDGET_DRAWABLE (spinner))
    {
        return FALSE;
    }

    if (details->need_load &&
        !e_spinner_load_images (spinner))
    {
        return FALSE;
    }

    images = details->images;
    if (images == NULL)
    {
        return FALSE;
    }

    /* Otherwise |images| will be NULL anyway */
    g_return_val_if_fail (images->n_animation_pixbufs > 0, FALSE);

    g_return_val_if_fail (details->current_image >= 0 &&
          details->current_image < images->n_animation_pixbufs, FALSE);

    pixbuf = images->animation_pixbufs[details->current_image];

    g_return_val_if_fail (pixbuf != NULL, FALSE);

    width = gdk_pixbuf_get_width (pixbuf);
    height = gdk_pixbuf_get_height (pixbuf);

    /* Compute the offsets for the image centered on our allocation */
    x_offset = (widget->allocation.width - width) / 2;
    y_offset = (widget->allocation.height - height) / 2;

    pix_area.x = x_offset + widget->allocation.x;
    pix_area.y = y_offset + widget->allocation.y;
    pix_area.width = width;
    pix_area.height = height;

    if (!gdk_rectangle_intersect (&event->area, &pix_area, &dest))
    {
        return FALSE;
    }

    gc = gdk_gc_new (widget->window);
    gdk_draw_pixbuf (widget->window, gc, pixbuf,
             dest.x - x_offset - widget->allocation.x,
             dest.y - y_offset - widget->allocation.y,
             dest.x, dest.y,
             dest.width, dest.height,
             GDK_RGB_DITHER_MAX, 0, 0);
    g_object_unref (gc);

    return FALSE;
}

static gboolean
bump_spinner_frame_cb (ESpinner *spinner)
{
    ESpinnerDetails *details = spinner->details;

    /* This can happen when we've unloaded the images on a theme
     * change, but haven't been in the queued size request yet.
     * Just skip this update.
     */
    if (details->images == NULL) return TRUE;

    details->current_image++;
    if (details->current_image >= details->images->n_animation_pixbufs)
    {
        /* the 0th frame is the 'rest' icon */
        details->current_image = MIN (1, details->images->n_animation_pixbufs);
    }

    gtk_widget_queue_draw (GTK_WIDGET (spinner));

    /* run again */
    return TRUE;
}

static void
e_spinner_start (ESpinner *spinner)
{
    ESpinnerDetails *details = spinner->details;

    details->spinning = TRUE;

    if (GTK_WIDGET_MAPPED (GTK_WIDGET (spinner)) &&
        details->timer_task == 0 &&
        e_spinner_load_images (spinner))
    {
        /* the 0th frame is the 'rest' icon */
        details->current_image = MIN (1, details->images->n_animation_pixbufs);

        details->timer_task =
            g_timeout_add_full (G_PRIORITY_LOW,
                                details->timeout,
                            (GSourceFunc) bump_spinner_frame_cb,
                            spinner,
                            NULL);
    }
}

static void
e_spinner_remove_update_callback (ESpinner *spinner)
{
    ESpinnerDetails *details = spinner->details;

    if (details->timer_task != 0)
    {
        g_source_remove (details->timer_task);
        details->timer_task = 0;
    }
}

static void
e_spinner_set_size (ESpinner *spinner,
               GtkIconSize size)
{
    if (size == GTK_ICON_SIZE_INVALID)
    {
        size = GTK_ICON_SIZE_DIALOG;
    }

    if (size != spinner->details->size)
    {
        e_spinner_unload_images (spinner);

        spinner->details->size = size;

        gtk_widget_queue_resize (GTK_WIDGET (spinner));
    }
}

#if 0

static void
e_spinner_stop (ESpinner *spinner)
{
    ESpinnerDetails *details = spinner->details;

    details->spinning = FALSE;
    details->current_image = 0;

    if (details->timer_task != 0)
    {
        e_spinner_remove_update_callback (spinner);

        if (GTK_WIDGET_MAPPED (GTK_WIDGET (spinner)))
        {
            gtk_widget_queue_draw (GTK_WIDGET (spinner));
        }
    }
}


/*
 * e_spinner_set_timeout:
 * @spinner: a #ESpinner
 * @timeout: time delay between updates to the spinner.
 *
 * Sets the timeout delay for spinner updates.
 **/
void
e_spinner_set_timeout (ESpinner *spinner,
              guint timeout)
{
    ESpinnerDetails *details = spinner->details;

    if (timeout != details->timeout)
    {
        e_spinner_stop (spinner);

        details->timeout = timeout;

        if (details->spinning)
        {
            e_spinner_start (spinner);
        }
    }
}
#endif

static void
e_spinner_size_request (GtkWidget *widget,
               GtkRequisition *requisition)
{
    ESpinner *spinner = E_SPINNER (widget);
    ESpinnerDetails *details = spinner->details;

    if ((details->need_load &&
         !e_spinner_load_images (spinner)) ||
            details->images == NULL)
    {
        requisition->width = requisition->height = 0;
        gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (widget),
                           details->size,
                           &requisition->width,
                               &requisition->height);
        return;
    }

    requisition->width = details->images->width;
    requisition->height = details->images->height;

    /* FIXME fix this hack */
    /* allocate some extra margin so we don't butt up against toolbar edges */
    if (details->size != GTK_ICON_SIZE_MENU)
    {
        requisition->width += 2;
        requisition->height += 2;
    }
}

static void
e_spinner_map (GtkWidget *widget)
{
    ESpinner *spinner = E_SPINNER (widget);
    ESpinnerDetails *details = spinner->details;

    GTK_WIDGET_CLASS (parent_class)->map (widget);

    if (details->spinning)
    {
        e_spinner_start (spinner);
    }
}

static void
e_spinner_unmap (GtkWidget *widget)
{
    ESpinner *spinner = E_SPINNER (widget);

    e_spinner_remove_update_callback (spinner);

    GTK_WIDGET_CLASS (parent_class)->unmap (widget);
}

static void
e_spinner_dispose (GObject *object)
{
    ESpinner *spinner = E_SPINNER (object);

    g_signal_handlers_disconnect_by_func
            (spinner->details->icon_theme,
         G_CALLBACK (icon_theme_changed_cb), spinner);

    G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
e_spinner_finalize (GObject *object)
{
    ESpinner *spinner = E_SPINNER (object);

    e_spinner_remove_update_callback (spinner);
    e_spinner_unload_images (spinner);

    g_object_unref (spinner->details->cache);

    G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
e_spinner_screen_changed (GtkWidget *widget,
                 GdkScreen *old_screen)
{
    ESpinner *spinner = E_SPINNER (widget);
    ESpinnerDetails *details = spinner->details;
    GdkScreen *screen;

    if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
    {
        GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, old_screen);
    }

    screen = gtk_widget_get_screen (widget);

    /* FIXME: this seems to be happening when then spinner is destroyed!? */
    if (old_screen == screen) return;

    /* We'll get mapped again on the new screen, but not unmapped from
     * the old screen, so remove timeout here.
     */
    e_spinner_remove_update_callback (spinner);

    e_spinner_unload_images (spinner);

    if (old_screen != NULL)
    {
        g_signal_handlers_disconnect_by_func
            (gtk_icon_theme_get_for_screen (old_screen),
             G_CALLBACK (icon_theme_changed_cb), spinner);
    }

    details->icon_theme = gtk_icon_theme_get_for_screen (screen);
    g_signal_connect (details->icon_theme, "changed",
              G_CALLBACK (icon_theme_changed_cb), spinner);
}

static void
e_spinner_class_init (ESpinnerClass *class)
{
    GObjectClass *object_class =  G_OBJECT_CLASS (class);
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);

    parent_class = g_type_class_peek_parent (class);

    object_class->dispose = e_spinner_dispose;
    object_class->finalize = e_spinner_finalize;

    widget_class->expose_event = e_spinner_expose;
    widget_class->size_request = e_spinner_size_request;
    widget_class->map = e_spinner_map;
    widget_class->unmap = e_spinner_unmap;
    widget_class->screen_changed = e_spinner_screen_changed;

    g_type_class_add_private (object_class, sizeof (ESpinnerDetails));
}

GtkWidget *e_spinner_new_spinning_small_shown (void)
{
  ESpinner *image;
  image = E_SPINNER (g_object_new (E_TYPE_SPINNER, NULL));

  e_spinner_set_size (image, GTK_ICON_SIZE_SMALL_TOOLBAR);
  e_spinner_start (image);
  gtk_widget_show (GTK_WIDGET(image));

  return GTK_WIDGET (image);
}