2004-04-13 Andreas Nahr <ClassDevelopment@A-SoftTech.com>
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / Timer.cs
index 977379ea0174b96f0a312317530dff33f6be41de..06854e917c028352c90dfa6eeb8e728cda711315 100644 (file)
-//\r
-// System.Windows.Forms.Timer\r
-//\r
-// Author:\r
-//   stubbed out by Jackson Harper (jackson@latitudegeo.com)\r
-//\r
-// (C) 2002 Ximian, Inc\r
-//\r
-\r
-namespace System.Windows.Forms {\r
-\r
-       // <summary>\r
-       //      This is only a template.  Nothing is implemented yet.\r
-       //\r
-       // </summary>\r
-using System.ComponentModel;\r
-    public class Timer : Component {\r
-\r
-               //\r
-               //  --- Public Constructors\r
-               //\r
-               [MonoTODO]\r
-               public Timer()\r
-               {\r
-                       throw new NotImplementedException ();\r
-               }\r
-               [MonoTODO]\r
-               public Timer(IContainer container)\r
-               {\r
-                       throw new NotImplementedException ();\r
-               }\r
-               //\r
-               // --- Public Properties\r
-               //\r
-               [MonoTODO]\r
-               public virtual bool Enabled {\r
-                       get {\r
-                               throw new NotImplementedException ();\r
-                       }\r
-                       set {\r
-                               throw new NotImplementedException ();\r
-                       }\r
-               }\r
-               [MonoTODO]\r
-               public int Interval {\r
-                       get {\r
-                               throw new NotImplementedException ();\r
-                       }\r
-                       set {\r
-                               throw new NotImplementedException ();\r
-                       }\r
-               }\r
-               //\r
-               // --- Public Methods\r
-               //\r
-               [MonoTODO]\r
-               public void Start() \r
-               {\r
-                       throw new NotImplementedException ();\r
-               }\r
-               [MonoTODO]\r
-               public void Stop() \r
-               {\r
-                       throw new NotImplementedException ();\r
-               }\r
-               [MonoTODO]\r
-               public override string ToString() \r
-               {\r
-                       throw new NotImplementedException ();\r
-               }\r
-               //\r
-               // --- Public Events\r
-               //\r
-               [MonoTODO]\r
-               public event EventHandler Tick;\r
-               //\r
-               // --- Protected Methods\r
-               //\r
-               [MonoTODO]\r
-               protected override void Dispose(bool disposing) \r
-               {\r
-                       throw new NotImplementedException ();\r
-               }\r
-               [MonoTODO]\r
-               protected virtual void OnTick(EventArgs e) \r
-               {\r
-                       throw new NotImplementedException ();\r
-               }\r
-       }\r
-}\r
+//
+// System.Windows.Forms.Timer
+//
+// Author:
+//     stubbed out by Jackson Harper (jackson@latitudegeo.com)
+//     Dennis Hayes (dennish@raytek.com)
+//     Aleksey Ryabchuk (ryabchuk@yahoo.com)
+//
+// (C) 2002/3 Ximian, Inc
+//
+
+using System.ComponentModel;
+using System.Runtime.InteropServices;
+using System.Collections;
+
+namespace System.Windows.Forms {
+
+       // <summary>
+       //      Represents a timer that raises an event at user-defined intervals.
+       // </summary>
+       public class Timer : Component {
+
+               private bool enabled = false;
+               private int  interval = 100;
+               private uint timerid = 0;
+               private GCHandle timerHandle;
+               private Win32.TimerProc proc;
+
+               public Timer(){
+               }
+
+               public Timer( IContainer container ) {
+                       container.Add ( this );
+               }
+
+               public virtual bool Enabled {
+                       get { 
+                               return enabled;
+                       }
+                       set { 
+                               enabled = value;
+                               if ( enabled ) {
+                                       if ( timerid != 0 )
+                                               Win32.KillTimer ( IntPtr.Zero , timerid );
+
+                                       if ( !timerHandle.IsAllocated )
+                                               timerHandle = GCHandle.Alloc( this );
+
+                                       if ( proc == null )
+                                               proc = new Win32.TimerProc( this.TimeProc );
+                                       
+                                       timerid = Win32.SetTimer( IntPtr.Zero,  0, (uint)Interval, proc );
+                               }
+                               else {
+                                       if ( timerid != 0 )
+                                               Win32.KillTimer ( IntPtr.Zero , timerid );
+
+                                       timerid = 0;
+
+                                       if ( timerHandle.IsAllocated )
+                                               timerHandle.Free();
+                               }
+                       }
+               }
+
+               public int Interval {
+                       get {
+                               return interval;
+                       }
+                       set {
+                               if ( value <= 0 )
+                                       throw new ArgumentException (
+                                       string.Format (" '{0}' is not a valid value for Interval. Interval must be greater than 0.",
+                                                       value ) );
+                               interval = value;
+                               if ( Enabled )
+                                       Enabled = true; // restart
+                       }
+               }
+
+               public void Start() {
+                       Enabled = true;
+               }
+
+               public void Stop() {
+                       Enabled = false;
+               }
+
+               public override string ToString() 
+               {
+                       return "[" + GetType().FullName.ToString() + "], Interval: " + Interval;
+               }
+
+               public event EventHandler Tick;
+
+               protected virtual void OnTick(EventArgs e) 
+               {
+                       if ( Tick != null )
+                               Tick ( this, e );
+               }
+
+               private void TimeProc( IntPtr hwnd, uint uMsg, uint idEvent, int dwTime )
+               {
+                       OnTick ( EventArgs.Empty );
+               }  
+               
+               protected override void Dispose( bool disposing ) {
+                       Enabled = false;
+                       base.Dispose ( disposing );
+               }
+       }
+}