~ubuntu-branches/ubuntu/lucid/beagle/lucid

« back to all changes in this revision

Viewing changes to search/Beagle.Search/Spinner.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-05-04 00:31:32 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20080504003132-2tkm5o8moo5952ri
Tags: 0.3.7-2ubuntu1
 * Merge from Debian unstable. (LP: #225746) Remaining Ubuntu changes:
  - debian/control:
    + Rename ice{weasel,dove}-beagle to {mozilla,thunderbird}-beagle and
      and update the dependencies accordingly.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Spinner.cs
 
3
//
 
4
// Copyright (C) 2008 Lukas Lipka <lukaslipka@gmail.com>
 
5
 
 
6
using System;
 
7
 
 
8
using Gtk;
 
9
using Gdk;
 
10
 
 
11
namespace Beagle.Search {
 
12
 
 
13
        public class Spinner : Gtk.Image {
 
14
 
 
15
                private IconTheme theme;
 
16
 
 
17
                private Pixbuf idle_pixbuf;
 
18
                private Pixbuf[] frames;
 
19
 
 
20
                private int current_frame;
 
21
                private uint timeout_id;
 
22
 
 
23
                private const int target_size = 24;
 
24
                private const int refresh_rate = 125;
 
25
 
 
26
                public Spinner ()
 
27
                {
 
28
                }
 
29
 
 
30
                ~Spinner ()
 
31
                {
 
32
                        Stop ();
 
33
                }
 
34
 
 
35
                public void Start ()
 
36
                {
 
37
                        if (!IsRealized)
 
38
                                return;
 
39
 
 
40
                        if (frames == null || frames.Length == 0)
 
41
                                return;
 
42
 
 
43
                        if (timeout_id > 0)
 
44
                                return;
 
45
 
 
46
                        timeout_id = GLib.Timeout.Add (refresh_rate, TimeoutHandler);
 
47
                }
 
48
 
 
49
                public void Stop ()
 
50
                {
 
51
                        if (timeout_id == 0)
 
52
                                return;
 
53
 
 
54
                        GLib.Source.Remove (timeout_id);
 
55
                        timeout_id = 0;
 
56
                        
 
57
                        Pixbuf = idle_pixbuf;
 
58
                }
 
59
 
 
60
                private bool TimeoutHandler ()
 
61
                {
 
62
                        Pixbuf = frames [current_frame];
 
63
                        current_frame = (current_frame + 1) % frames.Length;
 
64
 
 
65
                        return true;
 
66
                }
 
67
 
 
68
                protected override void OnRealized ()
 
69
                {
 
70
                        base.OnRealized ();
 
71
 
 
72
                        theme = Gtk.IconTheme.GetForScreen (Screen);
 
73
                        theme.Changed += ThemeChanged;
 
74
 
 
75
                        LoadImages ();
 
76
                }
 
77
 
 
78
                private void ThemeChanged (object o, EventArgs args)
 
79
                {
 
80
                        LoadImages ();
 
81
                }
 
82
 
 
83
                private void LoadImages ()
 
84
                {
 
85
                        int icon_size = target_size;
 
86
 
 
87
                        // Find available spinner sizes and use the appropriate one
 
88
 
 
89
                        foreach (int size in theme.GetIconSizes ("gnome-spinner-rest")) {
 
90
                                if (size >= target_size) {
 
91
                                        icon_size = size;
 
92
                                        break;
 
93
                                }
 
94
                        }
 
95
 
 
96
                        try {
 
97
                                idle_pixbuf = theme.LoadIcon ("gnome-spinner-rest", icon_size, 0);
 
98
                        } catch {
 
99
                                Console.Error.WriteLine ("Could not load spinner image");
 
100
                                frames = null;
 
101
                                Pixbuf = null;
 
102
                                return;
 
103
                        }
 
104
 
 
105
                        Gdk.Pixbuf frames_pixbuf = null;
 
106
 
 
107
                        try {
 
108
                                frames_pixbuf = theme.LoadIcon ("gnome-spinner", icon_size, 0);
 
109
                        } catch {
 
110
                                Console.Error.WriteLine ("Could not load spinner image");
 
111
                                frames = null;
 
112
                                Pixbuf = idle_pixbuf;
 
113
                                return;
 
114
                        }
 
115
 
 
116
                        int frame_width = idle_pixbuf.Width, frame_height = idle_pixbuf.Height;
 
117
                        int width = frames_pixbuf.Width, height = frames_pixbuf.Height;
 
118
 
 
119
                        if (width % frame_width != 0 || height % frame_height != 0) {
 
120
                                Console.Error.WriteLine ("Spinner images are wrong size");
 
121
                                frames = null;
 
122
                                Pixbuf = idle_pixbuf;
 
123
                                return;
 
124
                        }
 
125
 
 
126
                        int rows = height / frame_height, cols = width / frame_width;
 
127
 
 
128
                        frames = new Pixbuf [rows * cols];
 
129
 
 
130
                        for (int y = 0, n = 0; y < rows; y++) {
 
131
                                for (int x = 0; x < cols; x++) {
 
132
                                        frames [n++] = new Pixbuf (frames_pixbuf,
 
133
                                                                   x * frame_width,
 
134
                                                                   y * frame_height,
 
135
                                                                   frame_width,
 
136
                                                                   frame_height);
 
137
                                }
 
138
                        }
 
139
 
 
140
                        current_frame = 0;
 
141
 
 
142
                        if (timeout_id != 0)
 
143
                                Pixbuf = frames [current_frame];
 
144
                        else
 
145
                                Pixbuf = idle_pixbuf;
 
146
                }
 
147
        }
 
148
}