2006-03-21 Peter Dennis Bartok <pbartok@novell.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Timer.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004 Novell, Inc.
21 //
22 // Authors:
23 //      Jackson Harper (jackson@ximian.com)
24
25
26 using System;
27 using System.Threading;
28 using System.ComponentModel;
29
30 namespace System.Windows.Forms {
31         [DefaultProperty("Interval")]
32         [DefaultEvent("Tick")]
33         [ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Allow)]
34         public class Timer : Component {
35
36                 private bool enabled;
37                 private IContainer container;
38                 private int interval = 100;
39                 private DateTime expires;
40
41                 internal static readonly int Minimum = 15;
42
43                 public Timer ()
44                 {
45                         enabled = false;
46                 }
47
48                 public Timer (IContainer container) : this ()
49                 {
50                         container.Add (this);
51                 }
52
53                 [DefaultValue (false)]
54                 public virtual bool Enabled {
55                         get {
56                                 return enabled;
57                         }
58                         set {
59                                 if (value != enabled) {
60                                         enabled = value;
61                                         if (value) {
62                                                 // Use AddTicks so we get some rounding
63                                                 expires = DateTime.Now.AddMilliseconds (interval > Minimum ? interval : Minimum);
64                                                 XplatUI.SetTimer (this);
65                                         } else {
66                                                 XplatUI.KillTimer (this);
67                                         }
68                                 }
69                         }
70                 }
71
72                 [DefaultValue (100)]
73                 public int Interval {
74                         get {
75                                 return interval;
76                         }
77                         set {
78                                 if (interval == value) {
79                                         return;
80                                 }
81                                 
82                                 interval = value;
83                                                                 
84                                 // Use AddTicks so we get some rounding
85                                 expires = DateTime.Now.AddMilliseconds (interval > Minimum ? interval : Minimum);
86                                                                         
87                                 if (enabled == true) {                          
88                                         XplatUI.KillTimer (this);
89                                         XplatUI.SetTimer (this);
90                                 }
91                         }
92                 }
93
94                 public void Start ()
95                 {
96                         Enabled = true;
97                 }
98
99                 public void Stop ()
100                 {
101                         Enabled = false;
102                 }
103
104                 internal DateTime Expires {
105                         get {
106                                 return expires;
107                         }
108                 }
109
110                 public event EventHandler Tick;
111
112                 public override string ToString ()
113                 {
114                         return base.ToString () + ", Interval: " + Interval;
115                 }
116
117                 internal void Update (DateTime update)
118                 {
119                         expires = update.AddMilliseconds (interval > Minimum ? interval : Minimum);
120                 }
121
122                 internal void FireTick ()
123                 {
124                         OnTick (EventArgs.Empty);
125                 }
126
127
128                 protected virtual void OnTick (EventArgs e)
129                 {
130                         if (Tick != null)
131                                 Tick (this, e);
132                 }
133
134                 protected override void Dispose (bool disposing)
135                 {
136                         base.Dispose (disposing);
137                         Enabled = false;
138                 }
139
140                 private bool has_last_fire = false;
141                 private DateTime last_fire;
142
143                 internal void TickHandler (object sender, EventArgs e)
144                 {
145                         OnTick (e);
146                 }
147         }
148 }
149