merge -r 54590:54591
[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 \r
42         {  \r
43                 private int frameCount;\r
44                 private int activeFrameCount = 0;\r
45                 private Thread thread;\r
46       \r
47                 //Constructor.\r
48                 //\r
49                 public AnimateEventArgs(Image img)\r
50                 {\r
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))\r
55                                         this.frameCount = img.GetFrameCount (FrameDimension.Time);
56                         }                       \r
57                 }\r
58       \r
59                 public int FrameCount {     \r
60                         get { \r
61                                 return frameCount;\r
62                         }      \r
63                 }\r
64       \r
65                 public int ActiveFrameCount {\r
66                         get {\r
67                                 return activeFrameCount;\r
68                         }\r
69 \r
70                         set {\r
71                                 activeFrameCount = value;\r
72                         }\r
73                 }\r
74 \r
75                 public Thread RunThread{\r
76                         get {\r
77                                 return thread;\r
78                         }\r
79 \r
80                         set {\r
81                                 thread = value;\r
82                         }\r
83                 }\r
84         }\r
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                                 WorkerThread WT = new WorkerThread(onFrameChangeHandler, evtArgs);
110                                 ThreadStart TS = new ThreadStart(WT.LoopHandler);       
111                                 Thread thread = new Thread(TS);
112                                 thread.IsBackground = true;
113                                 evtArgs.RunThread = thread;
114                                 ht.Add (img, evtArgs);
115                                 
116                                 thread.Start();                         
117                         }
118                 }
119
120                 public static bool CanAnimate (Image img)
121                 {
122                         //An image can animate if it has multiple frame in
123                         //time based FrameDimension else return false
124                         //Doubt what if the image has multiple frame in page
125                         //based FrameDimension
126                         if (img == null)
127                                 return false;
128
129                         //Need to check whether i can do this without iterating
130                         //within the FrameDimensionsList, ie just call GetFrameCount
131                         //with parameter FrameDimension.Time
132                         Guid[] dimensionList = img.FrameDimensionsList;
133                         int length = dimensionList.Length;
134                         int frameCount;
135                         for (int i=0; i<length; i++) \r
136                         {
137                                 if (dimensionList [i].Equals(FrameDimension.Time.Guid)) \r
138                                 {
139                                         frameCount = img.GetFrameCount (FrameDimension.Time);
140                                         if (frameCount > 1)
141                                                 return true;
142                                 }
143                         }                       
144
145                         return false;           \r
146                 }
147
148                 public static void StopAnimate (Image img, EventHandler onFrameChangeHandler)
149                 {
150                         if (img == null)
151                                 throw new System.NullReferenceException ("Object reference not set to an instance of an object.");                      
152
153                         if (ht.ContainsKey (img)) {
154                                 AnimateEventArgs evtArgs = (AnimateEventArgs) ht [img];
155                                 evtArgs.RunThread.Abort ();
156                                 ht.Remove (img);
157                         }                               
158                 }
159
160                 public static void UpdateFrames ()
161                 {
162                         foreach (Image img in ht.Keys) {
163                                 UpdateFrames (img);
164                         }
165                 }
166                 
167                 public static void UpdateFrames (Image img)
168                 {
169                         if (img == null)
170                                 throw new System.NullReferenceException ("Object reference not set to an instance of an object.");
171
172                         if (ht.ContainsKey (img)){
173                                 //Need a way to get the delay during animation
174                                 AnimateEventArgs evtArgs = (AnimateEventArgs) ht [img];
175                                 if (evtArgs.ActiveFrameCount < evtArgs.FrameCount-1){
176                                         evtArgs.ActiveFrameCount ++;
177                                         img.SelectActiveFrame (FrameDimension.Time, evtArgs.ActiveFrameCount);
178                                 } \r
179                                 else
180                                         evtArgs.ActiveFrameCount = 0;
181                                 ht [img] = evtArgs;
182                         }                       
183                 }
184         }
185
186         class WorkerThread
187         {
188                 private EventHandler frameChangeHandler;
189                 private AnimateEventArgs animateEventArgs;
190                                 
191                 public WorkerThread(EventHandler frmChgHandler, AnimateEventArgs aniEvtArgs)
192                 {
193                         frameChangeHandler = frmChgHandler;
194                         animateEventArgs = aniEvtArgs;
195                 }
196     
197                 public void LoopHandler()
198                 {
199                         try
200                         {
201                                 while (true) {
202                                         //Need a way to get the delay during animation
203                                         Thread.Sleep (100);
204                                         frameChangeHandler (null, animateEventArgs);\r
205                                 }                               
206                         }
207                         catch(ThreadAbortException)
208                         { 
209                                 //lets not bother ourselves with tae
210                                 //it will be thrown anyway
211                         }
212                         catch(Exception er)
213                         {
214                                 throw er;
215                         }
216                 }
217         }\r
218 }