External/Aga.Controls/Tree/NodeControls/ExpandingIcon.cs
author moel.mich
Sun, 27 May 2012 16:08:54 +0000
changeset 346 f652ab1e06e2
permissions -rw-r--r--
Added the small modifications to the Aga.Controls required by the Open Hardware Monitor.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 using System.Drawing;
     5 using System.Threading;
     6 using System.Windows.Forms;
     7 
     8 namespace Aga.Controls.Tree.NodeControls
     9 {
    10 	/// <summary>
    11 	/// Displays an animated icon for those nodes, who are in expanding state. 
    12 	/// Parent TreeView must have AsyncExpanding property set to true.
    13 	/// </summary>
    14 	public class ExpandingIcon: NodeControl
    15 	{
    16 		private static GifDecoder _gif = ResourceHelper.LoadingIcon;
    17 		private static int _index = 0;
    18 		private static volatile Thread _animatingThread;
    19         private static object _lock = new object();
    20 
    21 		public override Size MeasureSize(TreeNodeAdv node, DrawContext context)
    22 		{
    23 			return ResourceHelper.LoadingIcon.FrameSize;
    24 		}
    25 
    26 		protected override void OnIsVisibleValueNeeded(NodeControlValueEventArgs args)
    27 		{
    28 			args.Value = args.Node.IsExpandingNow;
    29 			base.OnIsVisibleValueNeeded(args);
    30 		}
    31 
    32 		public override void Draw(TreeNodeAdv node, DrawContext context)
    33 		{
    34 			Rectangle rect = GetBounds(node, context);
    35 			Image img = _gif.GetFrame(_index).Image;
    36 			context.Graphics.DrawImage(img, rect.Location);
    37 		}
    38 
    39 		public static void Start()
    40 		{
    41             lock (_lock)
    42             {
    43                 if (_animatingThread == null)
    44                 {
    45                     _index = 0;
    46                     _animatingThread = new Thread(new ThreadStart(IterateIcons));
    47                     _animatingThread.IsBackground = true;
    48                     _animatingThread.Priority = ThreadPriority.Lowest;
    49                     _animatingThread.Start();
    50                 }
    51             }
    52 		}
    53 
    54         public static void Stop()
    55         {
    56             lock (_lock)
    57             {
    58                 _index = 0;
    59                 _animatingThread = null;
    60             }
    61         }
    62 
    63 		private static void IterateIcons()
    64 		{
    65             while (_animatingThread != null)
    66 			{
    67 				if (_index < _gif.FrameCount - 1)
    68 					_index++;
    69 				else
    70 					_index = 0;
    71 
    72 				if (IconChanged != null)
    73 					IconChanged(null, EventArgs.Empty);
    74 
    75 				int delay = _gif.GetFrame(_index).Delay;
    76 				Thread.Sleep(delay);
    77 			}
    78             System.Diagnostics.Debug.WriteLine("IterateIcons Stopped");
    79 		}
    80 
    81 		public static event EventHandler IconChanged;
    82 	}
    83 }