2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / HandleData.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
27
28 using System;
29 using System.Drawing;
30 using System.Collections;
31
32 namespace System.Windows.Forms {
33
34         internal class HandleData : IDisposable {
35
36                 private Queue           message_queue;
37                 private Rectangle       invalid = Rectangle.Empty;
38                 private Object          dc;
39                 private bool            has_expose;
40                 private bool            is_visible;
41
42                 #region Constructors and destructors
43                 public HandleData ()
44                 {
45                         is_visible = true;
46                 }
47
48                 public void Dispose () {
49                         DeviceContext = null;
50                 }
51                 #endregion
52
53                 #region Paint area handling
54                 public Object DeviceContext {
55                         get { return dc; }
56                         set {
57                                 if (value is Graphics) {
58                                         if (dc != null) {
59                                                 ((Graphics)dc).Dispose ();
60                                         }
61                                 }
62                                 dc = value;
63                         }
64                 }
65
66                 public bool HasExpose {
67                         get {
68                                 return has_expose;
69                         }
70                         set {
71                                 has_expose = value;
72                         }
73                 }
74
75                 public bool IsVisible {
76                         get {
77                                 return is_visible;
78                         }
79                         set {
80                                 is_visible = value;
81                         }
82                 }
83
84                 public Rectangle InvalidArea {
85                         get {
86                                 return invalid;
87                         }
88                 }
89
90                 public void ScrollInvalidArea (int x, int y) {
91                         if (invalid == Rectangle.Empty) {
92                                 return;
93                         }
94
95                         invalid.X += x;
96                         invalid.Y += y;
97
98                         if (invalid.X < 0) {
99                                 invalid.Width += invalid.X;
100                                 invalid.X =0;
101                         }
102
103                         if (invalid.Y < 0) {
104                                 invalid.Height += invalid.Y;
105                                 invalid.Y =0;
106                         }
107                 }
108
109                 public void AddToInvalidArea (int x, int y, int width, int height)
110                 {
111                         if (invalid == Rectangle.Empty) {
112                                 invalid = new Rectangle (x, y, width, height);
113                                 return;
114                         }
115                         invalid = Rectangle.Union (invalid, new Rectangle (x, y, width, height));
116                 }
117
118                 public void AddToInvalidArea (Rectangle r)
119                 {
120                         if (invalid == Rectangle.Empty) {
121                                 invalid = r;
122                                 return;
123                         }
124                         invalid = Rectangle.Union (invalid, r);
125                 }
126
127                 public void ClearInvalidArea ()
128                 {
129                         invalid = Rectangle.Empty;
130                         has_expose = false;
131                 }
132                 #endregion      // Paint area methods
133
134                 #region Message storage and retrieval
135                 public bool MessageWaiting {
136                         get {
137                                 if ((message_queue == null) || (message_queue.Count == 0)) {
138                                         return false;
139                                 }
140                                 return true;
141                         }
142                 }
143
144                 public bool GetMessage(ref MSG msg) {
145                         MSG     message;
146
147                         if ((message_queue == null) || (message_queue.Count == 0)) {
148                                 return false;
149                         }
150
151                         message = (MSG)message_queue.Dequeue();
152                         msg = message;
153
154                         return true;
155                 }
156
157                 public bool StoreMessage(ref MSG msg) {
158                         MSG message = new MSG();
159
160                         if (message_queue == null) {
161                                 message_queue = new Queue();
162                         }
163
164                         message = msg;
165                         message_queue.Enqueue(message);
166
167                         return true;
168                 }
169                 #endregion      // Message storage and retrieval
170         }
171 }
172