* TreeView.cs: Don't draw the selected node when we lose
[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 bool Enabled {
55                         get {
56                                 return enabled;
57                         }
58                         set {
59                                 if (value != enabled) {
60                                         enabled = value;
61                                         if (value) {
62                                                 XplatUI.SetTimer (this);
63                                         } else {
64                                                 XplatUI.KillTimer (this);
65                                         }
66                                 }
67                         }
68                 }
69
70                 [DefaultValue (100)]
71                 public int Interval {
72                         get {
73                                 return interval;
74                         }
75                         set {
76                                 if (interval == value) {
77                                         return;
78                                 }
79                                 
80                                 interval = value;
81                                                                 
82                                 // Use AddTicks so we get some rounding
83                                 expires = DateTime.Now.AddMilliseconds (interval > Minimum ? interval : Minimum);
84                                                                         
85                                 if (enabled == true) {                          
86                                         XplatUI.KillTimer (this);
87                                         XplatUI.SetTimer (this);
88                                 }
89                         }
90                 }
91
92                 public void Start ()
93                 {
94                         Enabled = true;
95                 }
96
97                 public void Stop ()
98                 {
99                         Enabled = false;
100                 }
101
102                 internal DateTime Expires {
103                         get {
104                                 return expires;
105                         }
106                 }
107
108                 public event EventHandler Tick;
109
110                 public override string ToString ()
111                 {
112                         return base.ToString () + ", Interval: " + Interval;
113                 }
114
115                 internal void Update (DateTime update)
116                 {
117                         expires = update.AddMilliseconds (interval > Minimum ? interval : Minimum);
118                 }
119
120                 internal void FireTick ()
121                 {
122                         OnTick (EventArgs.Empty);
123                 }
124
125
126                 protected virtual void OnTick (EventArgs e)
127                 {
128                         if (Tick != null)
129                                 Tick (this, e);
130                 }
131
132                 protected override void Dispose (bool disposing)
133                 {
134                         base.Dispose (disposing);
135                         Enabled = false;
136                 }
137
138                 private bool has_last_fire = false;
139                 private DateTime last_fire;
140
141                 internal void TickHandler (object sender, EventArgs e)
142                 {
143                         OnTick (e);
144                 }
145         }
146 }
147