/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. using System.Timers;
  17. using Gtk;
  18. namespace SparkleShare {
  19. // This is a close implementation of GtkSpinner
  20. public class SparkleSpinner : Image {
  21. public bool Active;
  22. private Gdk.Pixbuf [] Images;
  23. private Timer Timer;
  24. private int CycleDuration;
  25. private int CurrentStep;
  26. private int NumSteps;
  27. private int Size;
  28. public SparkleSpinner (int size) : base ()
  29. {
  30. Size = size;
  31. CycleDuration = 600;
  32. CurrentStep = 0;
  33. Gdk.Pixbuf spinner_gallery = SparkleUIHelpers.GetIcon ("process-working", Size);
  34. int frames_in_width = spinner_gallery.Width / Size;
  35. int frames_in_height = spinner_gallery.Height / Size;
  36. NumSteps = frames_in_width * frames_in_height;
  37. Images = new Gdk.Pixbuf [NumSteps - 1];
  38. int i = 0;
  39. for (int y = 0; y < frames_in_height; y++) {
  40. for (int x = 0; x < frames_in_width; x++) {
  41. if (!(y == 0 && x == 0)) {
  42. Images [i] = new Gdk.Pixbuf (spinner_gallery, x * Size, y * Size, Size, Size);
  43. i++;
  44. }
  45. }
  46. }
  47. Timer = new Timer () {
  48. Interval = CycleDuration / NumSteps
  49. };
  50. Timer.Elapsed += delegate {
  51. NextImage ();
  52. };
  53. Start ();
  54. }
  55. private void NextImage ()
  56. {
  57. if (CurrentStep < NumSteps - 2)
  58. CurrentStep++;
  59. else
  60. CurrentStep = 0;
  61. Application.Invoke (delegate { SetImage (); });
  62. }
  63. private void SetImage ()
  64. {
  65. Pixbuf = Images [CurrentStep];
  66. }
  67. public bool IsActive ()
  68. {
  69. return Active;
  70. }
  71. public void Start ()
  72. {
  73. CurrentStep = 0;
  74. Active = true;
  75. Timer.Start ();
  76. }
  77. public void Stop ()
  78. {
  79. Active = false;
  80. Timer.Stop ();
  81. }
  82. }
  83. }