* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Drawing / System.Drawing / ImageAnimator.cs
1 //
2 // System.Drawing.ImageAnimator.cs
3 //
4 // Author:
5 //   Dennis Hayes (dennish@Raytek.com)
6 //   Sanjay Gupta (gsanjay@novell.com)
7 //
8 // (C) 2002 Ximian, Inc
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 using System;
34 using System.Drawing.Imaging;
35 using System.Threading;
36 using System.Collections;
37
38 namespace System.Drawing
39 {
40         //AnimateEventArgs class
41         class AnimateEventArgs : EventArgs 
42         {  
43                 private int frameCount;
44                 private int activeFrameCount = 0;
45                 private Thread thread;
46       
47                 //Constructor.
48                 //
49                 public AnimateEventArgs(Image img)
50                 {
51                         Guid[] dimensionList = img.FrameDimensionsList;
52                         int length = dimensionList.Length;
53                         for (int i=0; i<length; i++) {
54                                 if (dimensionList [i].Equals(FrameDimension.Time.Guid))
55                                         this.frameCount = img.GetFrameCount (FrameDimension.Time);
56                         }                       
57                 }
58       
59                 public int FrameCount {     
60                         get { 
61                                 return frameCount;
62                         }      
63                 }
64       
65                 public int ActiveFrameCount {
66                         get {
67                                 return activeFrameCount;
68                         }
69
70                         set {
71                                 activeFrameCount = value;
72                         }
73                 }
74
75                 public Thread RunThread{
76                         get {
77                                 return thread;
78                         }
79
80                         set {
81                                 thread = value;
82                         }
83                 }
84         }
85
86         /// <summary>
87         /// Summary description for ImageAnimator.
88         /// </summary>
89         /// 
90         [MonoTODO]
91         public sealed class ImageAnimator
92         {
93                 static Hashtable ht = new Hashtable (); 
94                 
95                 private ImageAnimator ()
96                 {
97                         //
98                         // TODO: Add constructor logic here
99                         //
100                 }
101
102                 public static void Animate (Image img, EventHandler onFrameChangeHandler)
103                 {
104                         if (img == null)
105                                 throw new System.NullReferenceException ("Object reference not set to an instance of an object.");
106                         
107                         if (!ht.ContainsKey (img)) {
108                                 AnimateEventArgs evtArgs = new AnimateEventArgs (img);
109                                 int delay;
110                                 try {
111                                         PropertyItem item = img.GetPropertyItem (0x5100); // FrameDelay in libgdiplus
112                                         // Time is in 1/100th of a second
113                                         delay = (item.Value [0] + item.Value [1] * 256) * 10;
114                                 } catch {
115                                         delay = 200;
116                                 }
117
118                                 WorkerThread WT = new WorkerThread (onFrameChangeHandler, evtArgs, delay);
119                                 ThreadStart TS = new ThreadStart(WT.LoopHandler);       
120                                 Thread thread = new Thread(TS);
121                                 thread.IsBackground = true;
122                                 evtArgs.RunThread = thread;
123                                 ht.Add (img, evtArgs);
124                                 
125                                 thread.Start();                         
126                         }
127                 }
128
129                 public static bool CanAnimate (Image img)
130                 {
131                         //An image can animate if it has multiple frame in
132                         //time based FrameDimension else return false
133                         //Doubt what if the image has multiple frame in page
134                         //based FrameDimension
135                         if (img == null)
136                                 return false;
137
138                         //Need to check whether i can do this without iterating
139                         //within the FrameDimensionsList, ie just call GetFrameCount
140                         //with parameter FrameDimension.Time
141                         Guid[] dimensionList = img.FrameDimensionsList;
142                         int length = dimensionList.Length;
143                         int frameCount;
144                         for (int i=0; i<length; i++) 
145                         {
146                                 if (dimensionList [i].Equals(FrameDimension.Time.Guid)) 
147                                 {
148                                         frameCount = img.GetFrameCount (FrameDimension.Time);
149                                         if (frameCount > 1)
150                                                 return true;
151                                 }
152                         }                       
153
154                         return false;           
155                 }
156
157                 public static void StopAnimate (Image img, EventHandler onFrameChangeHandler)
158                 {
159                         if (img == null)
160                                 throw new System.NullReferenceException ("Object reference not set to an instance of an object.");                      
161
162                         if (ht.ContainsKey (img)) {
163                                 AnimateEventArgs evtArgs = (AnimateEventArgs) ht [img];
164                                 evtArgs.RunThread.Abort ();
165                                 ht.Remove (img);
166                         }                               
167                 }
168
169                 public static void UpdateFrames ()
170                 {
171                         foreach (Image img in ht.Keys) {
172                                 UpdateFrames (img);
173                         }
174                 }
175                 
176                 public static void UpdateFrames (Image img)
177                 {
178                         if (img == null)
179                                 throw new System.NullReferenceException ("Object reference not set to an instance of an object.");
180
181                         if (ht.ContainsKey (img)){
182                                 //Need a way to get the delay during animation
183                                 AnimateEventArgs evtArgs = (AnimateEventArgs) ht [img];
184                                 if (evtArgs.ActiveFrameCount < evtArgs.FrameCount-1){
185                                         evtArgs.ActiveFrameCount ++;
186                                         img.SelectActiveFrame (FrameDimension.Time, evtArgs.ActiveFrameCount);
187                                 } 
188                                 else
189                                         evtArgs.ActiveFrameCount = 0;
190                                 ht [img] = evtArgs;
191                         }                       
192                 }
193         }
194
195         class WorkerThread
196         {
197                 EventHandler frameChangeHandler;
198                 AnimateEventArgs animateEventArgs;
199                 int delay;
200                                 
201                 public WorkerThread (EventHandler frmChgHandler, AnimateEventArgs aniEvtArgs, int delay)
202                 {
203                         frameChangeHandler = frmChgHandler;
204                         animateEventArgs = aniEvtArgs;
205                         this.delay = delay;
206                 }
207     
208                 public void LoopHandler()
209                 {
210                         try {
211                                 while (true) {
212                                         Thread.Sleep (delay);
213                                         frameChangeHandler (null, animateEventArgs);
214                                 }                               
215                         } catch (ThreadAbortException) { 
216                                 Thread.ResetAbort (); // we're going to finish anyway
217                         } catch (Exception er) {
218                                 throw er;
219                         }
220                 }
221         }
222 }
223