PageRenderTime 22ms CodeModel.GetById 15ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/SparkleShare/SparkleSpinner.cs

http://github.com/hbons/SparkleShare
C# | 111 lines | 67 code | 28 blank | 16 comment | 7 complexity | dce5104e53d5e17db9c295bea0f8cd43 MD5 | raw file
  1//   SparkleShare, a collaboration and sharing tool.
  2//   Copyright (C) 2010  Hylke Bons <hylkebons@gmail.com>
  3//
  4//   This program is free software: you can redistribute it and/or modify
  5//   it under the terms of the GNU General Public License as published by
  6//   the Free Software Foundation, either version 3 of the License, or
  7//   (at your option) any later version.
  8//
  9//   This program is distributed in the hope that it will be useful,
 10//   but WITHOUT ANY WARRANTY; without even the implied warranty of
 11//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 12//   GNU General Public License for more details.
 13//
 14//   You should have received a copy of the GNU General Public License
 15//   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 16
 17
 18using System.Timers;
 19using Gtk;
 20
 21namespace SparkleShare {
 22        
 23    // This is a close implementation of GtkSpinner
 24    public class SparkleSpinner : Image {
 25
 26        public bool Active;
 27
 28        private Gdk.Pixbuf [] Images;
 29        private Timer Timer;
 30        private int CycleDuration;
 31        private int CurrentStep;
 32        private int NumSteps;
 33        private int Size;
 34
 35
 36        public SparkleSpinner (int size) : base ()
 37        {
 38            Size = size;
 39
 40            CycleDuration = 600;
 41            CurrentStep = 0;
 42
 43            Gdk.Pixbuf spinner_gallery = SparkleUIHelpers.GetIcon ("process-working", Size);
 44
 45            int frames_in_width  = spinner_gallery.Width / Size;
 46            int frames_in_height = spinner_gallery.Height / Size;
 47
 48            NumSteps = frames_in_width * frames_in_height;
 49            Images   = new Gdk.Pixbuf [NumSteps - 1];
 50
 51            int i = 0;
 52
 53            for (int y = 0; y < frames_in_height; y++) {
 54                for (int x = 0; x < frames_in_width; x++) {
 55                    if (!(y == 0 && x == 0)) {
 56                        Images [i] = new Gdk.Pixbuf (spinner_gallery, x * Size, y * Size, Size, Size);
 57                        i++;
 58                    }
 59                }
 60            }
 61
 62            Timer = new Timer () {
 63                Interval = CycleDuration / NumSteps
 64            };
 65
 66            Timer.Elapsed += delegate {
 67                NextImage ();
 68            };
 69
 70            Start ();
 71        }
 72
 73
 74        private void NextImage ()
 75        {
 76            if (CurrentStep < NumSteps - 2)
 77                CurrentStep++;
 78            else
 79                CurrentStep = 0;
 80
 81            Application.Invoke (delegate { SetImage (); });
 82        }
 83
 84
 85        private void SetImage ()
 86        {
 87            Pixbuf = Images [CurrentStep];
 88        }
 89
 90
 91        public bool IsActive ()
 92        {
 93            return Active;
 94        }
 95
 96
 97        public void Start ()
 98        {
 99            CurrentStep = 0;
100            Active = true;
101            Timer.Start ();
102        }
103
104
105        public void Stop ()
106        {
107            Active = false;
108            Timer.Stop ();
109        }
110    }
111}