Add methods and messages to retrieve specific information from beagle-info. Clients...
[beagle.git] / search / Spinner.cs
blob978e1028a8b6c34e40a0c1459262726118282982
1 using System;
2 using Gtk;
3 using Gdk;
5 namespace Search {
7 public class Spinner : Gtk.Image {
9 private Pixbuf idlePixbuf;
10 private Pixbuf [] frames;
11 private int currentFrame;
12 private uint timeoutId;
14 private const int targetSize = 24;
15 private const int refreshRate = 125;
17 ~Spinner ()
19 Stop ();
22 Gtk.IconTheme theme;
24 protected override void OnRealized ()
26 base.OnRealized ();
28 theme = Gtk.IconTheme.GetForScreen (Screen);
29 theme.Changed += ThemeChanged;
30 LoadImages ();
33 private void ThemeChanged (object obj, EventArgs args)
35 LoadImages ();
38 private void LoadImages ()
40 int iconSize = targetSize;
42 #if false
43 // This code requires gtk-sharp 2.6, which we don't (yet) require
44 foreach (int size in theme.GetIconSizes ("gnome-spinner-rest")) {
45 if (size >= targetSize) {
46 iconSize = size;
47 break;
50 #endif
52 try {
53 idlePixbuf = theme.LoadIcon ("gnome-spinner-rest", iconSize, 0);
54 } catch {
55 Console.Error.WriteLine ("Could not load spinner image");
56 frames = null;
57 Pixbuf = null;
58 return;
61 Gdk.Pixbuf framesPixbuf;
62 try {
63 framesPixbuf = theme.LoadIcon ("gnome-spinner", iconSize, 0);
64 } catch {
65 Console.Error.WriteLine ("Could not load spinner image");
66 frames = null;
67 Pixbuf = idlePixbuf;
68 return;
71 int frameWidth = idlePixbuf.Width, frameHeight = idlePixbuf.Height;
72 int width = framesPixbuf.Width, height = framesPixbuf.Height;
73 if (width % frameWidth != 0 || height % frameHeight != 0) {
74 Console.Error.WriteLine ("Spinner image is wrong size");
75 frames = null;
76 Pixbuf = idlePixbuf;
77 return;
80 int rows = height / frameHeight, cols = width / frameWidth;
82 frames = new Pixbuf[rows * cols];
84 for (int y = 0, n = 0; y < rows; y++) {
85 for (int x = 0; x < cols; x++, n++) {
86 frames[n] = new Pixbuf (framesPixbuf,
87 x * frameWidth,
88 y * frameHeight,
89 frameWidth,
90 frameHeight);
94 currentFrame = 0;
95 if (timeoutId != 0)
96 Pixbuf = frames[currentFrame];
97 else
98 Pixbuf = idlePixbuf;
101 public void Start ()
103 if (!IsRealized)
104 return;
105 if (frames == null || frames.Length == 0)
106 return;
107 if (timeoutId != 0)
108 return;
110 timeoutId = GLib.Timeout.Add (refreshRate, TimeoutHandler);
113 public void Stop ()
115 if (timeoutId == 0)
116 return;
118 GLib.Source.Remove (timeoutId);
119 timeoutId = 0;
120 Pixbuf = idlePixbuf;
123 private bool TimeoutHandler ()
125 Pixbuf = frames[currentFrame];
126 currentFrame = (currentFrame + 1) % frames.Length;
127 return true;