External/Aga.Controls/Tree/NodeControls/ExpandingIcon.cs
author moel.mich
Mon, 02 Jul 2012 21:14:40 +0000
changeset 357 fb8dc26f65a4
permissions -rw-r--r--
Added mainboard specific configurations for the following Gigabyte mainboards: EX58-UD3R, G41M-Combo, G41MT-S2, G41MT-S2P, GA-MA770T-UD3P, GA-MA785GM-US2H, GA-MA78LM-S2H, GA-MA790X-UD3P, H55-USB3, H55N-USB3, H61M-DS2 REV 1.2, H61M-USB3-B3 REV 2.0, H67A-USB3-B3, P55A-UD3, P67A-UD3-B3, P67A-UD3R-B3, Z68A-D3H-B3, Z68AP-D3, Z68X-UD3H-B3.
     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 }