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