fixed tests
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / XEventQueue.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-2006 Novell, Inc.
21 //
22 // System.Windows.Forms.XEventQueue
23 //
24 // Authors:
25 //  Jackson Harper (jackson@ximian.com)
26 //  Peter Dennis Bartok (pbartok@novell.com)
27 //
28
29 using System;
30 using System.Threading;
31 using System.Collections;
32
33 namespace System.Windows.Forms {
34
35         internal class XEventQueue {
36
37                 private XQueue          xqueue;
38                 private XQueue          lqueue; // Events inserted from threads other then the main X thread
39                 private PaintQueue      paint;  // Paint-only queue
40                 internal ArrayList      timer_list;
41                 private Thread          thread;
42                 private bool            quit_posted;
43                 private bool            dispatch_idle;
44
45                 private static readonly int InitialXEventSize = 100;
46                 private static readonly int InitialLXEventSize = 10;
47                 private static readonly int InitialPaintSize = 50;
48
49                 public XEventQueue (Thread thread) {
50                         xqueue = new XQueue (InitialXEventSize);
51                         lqueue = new XQueue (InitialLXEventSize);
52                         paint = new PaintQueue(InitialPaintSize);
53                         timer_list = new ArrayList ();
54                         this.thread = thread;
55                         this.quit_posted = false;
56                         this.dispatch_idle = true;
57                 }
58
59                 public int Count {
60                         get {
61                                 lock (lqueue) {
62                                         return xqueue.Count + lqueue.Count;
63                                 }
64                         }
65                 }
66
67                 public PaintQueue Paint {
68                         get {
69                                 return paint;
70                         }
71                 }
72
73                 public Thread Thread {
74                         get {
75                                 return thread;
76                         }
77                 }
78
79                 public void Enqueue (XEvent xevent)
80                 {
81                         if (Thread.CurrentThread != thread) {
82                                 Console.WriteLine ("Hwnd.Queue.Enqueue called from a different thread without locking.");
83                                 Console.WriteLine (Environment.StackTrace);
84                         }
85
86                         xqueue.Enqueue (xevent);
87                 }
88
89                 public void EnqueueLocked (XEvent xevent)
90                 {
91                         lock (lqueue) {
92                                 lqueue.Enqueue (xevent);
93                         }
94                 }
95
96                 public XEvent Dequeue ()
97                 {
98                         if (Thread.CurrentThread != thread) {
99                                 Console.WriteLine ("Hwnd.Queue.Dequeue called from a different thread without locking.");
100                                 Console.WriteLine (Environment.StackTrace);
101                         }
102
103                         if (xqueue.Count == 0) {
104                                 lock (lqueue) {
105                                         return lqueue.Dequeue ();
106                                 }
107                         }
108                         return xqueue.Dequeue ();
109                 }
110
111                 public XEvent Peek()
112                 {
113                         if (Thread.CurrentThread != thread) {
114                                 Console.WriteLine ("Hwnd.Queue.Peek called from a different thread without locking.");
115                                 Console.WriteLine (Environment.StackTrace);
116                         }
117
118                         if (xqueue.Count == 0) {
119                                 lock (lqueue) {
120                                         return lqueue.Peek ();
121                                 }
122                         }                               
123                         return xqueue.Peek();
124                 }
125
126                 public bool DispatchIdle {
127                         get {
128                                 return dispatch_idle;
129                         }
130                         set {
131                                 dispatch_idle = value;
132                         }
133                 }
134
135                 public bool PostQuitState {
136                         get {
137                                 return quit_posted;
138                         }
139
140                         set {
141                                 quit_posted = value;
142                         }
143                 }
144
145                 public class PaintQueue {
146
147                         private ArrayList       hwnds;
148                         private XEvent          xevent;
149                         
150                         public PaintQueue (int size) {
151                                 hwnds = new ArrayList(size);
152                                 xevent = new XEvent();
153                                 xevent.AnyEvent.type = XEventName.Expose;
154                         }
155
156                         public int Count {
157                                 get { return hwnds.Count; }
158                         }
159
160                         public void Enqueue (Hwnd hwnd) {
161                                 hwnds.Add(hwnd);
162                         }
163
164                         public void Remove(Hwnd hwnd) {
165                                 if (!hwnd.expose_pending && !hwnd.nc_expose_pending) {
166                                         hwnds.Remove(hwnd);
167                                 }
168                         }
169
170                         public XEvent Dequeue () {
171                                 Hwnd            hwnd;
172                                 IEnumerator     next;
173
174                                 if (hwnds.Count == 0) {
175                                         xevent.ExposeEvent.window = IntPtr.Zero;
176                                         return xevent;
177                                 }
178
179                                 next = hwnds.GetEnumerator();
180                                 next.MoveNext();
181                                 hwnd = (Hwnd)next.Current;
182
183                                 // We only remove the event from the queue if we have one expose left since
184                                 // a single 'entry in our queue may be for both NC and Client exposed
185                                 if ( !(hwnd.nc_expose_pending && hwnd.expose_pending)) {
186                                         hwnds.Remove(hwnd);
187                                 }
188                                 if (hwnd.expose_pending) {
189                                         xevent.ExposeEvent.window = hwnd.client_window;
190 #if not
191                                         xevent.ExposeEvent.x = hwnd.invalid.X;
192                                         xevent.ExposeEvent.y = hwnd.invalid.Y;
193                                         xevent.ExposeEvent.width = hwnd.invalid.Width;
194                                         xevent.ExposeEvent.height = hwnd.invalid.Height;
195 #endif
196                                         return xevent;
197                                 } else {
198                                         xevent.ExposeEvent.window = hwnd.whole_window;
199                                         xevent.ExposeEvent.x = hwnd.nc_invalid.X;
200                                         xevent.ExposeEvent.y = hwnd.nc_invalid.Y;
201                                         xevent.ExposeEvent.width = hwnd.nc_invalid.Width;
202                                         xevent.ExposeEvent.height = hwnd.nc_invalid.Height;
203                                         return xevent;
204                                 }
205                         }
206                 }
207
208                 private class XQueue {
209
210                         private XEvent [] xevents;
211                         private int head;
212                         private int tail;
213                         private int size;
214                         
215                         public XQueue (int size)
216                         {
217                                 xevents = new XEvent [size];
218                         }
219
220                         public int Count {
221                                 get { return size; }
222                         }
223
224                         public void Enqueue (XEvent xevent)
225                         {
226                                 if (size == xevents.Length)
227                                         Grow ();
228                                 
229                                 xevents [tail] = xevent;
230                                 tail = (tail + 1) % xevents.Length;
231                                 size++;
232                         }
233
234                         public XEvent Dequeue ()
235                         {
236                                 if (size < 1)
237                                         throw new Exception ("Attempt to dequeue empty queue.");
238                                 XEvent res = xevents [head];
239                                 head = (head + 1) % xevents.Length;
240                                 size--;
241                                 return res;
242                         }
243
244                         public XEvent Peek() {
245                                 if (size < 1) {
246                                         throw new Exception ("Attempt to peek at empty queue");
247                                 }
248                                 return xevents[head];
249                         }
250
251                         private void Grow ()
252                         {
253                                 int newcap = (xevents.Length * 2);
254                                 XEvent [] na = new XEvent [newcap];
255                                 xevents.CopyTo (na, 0);
256                                 xevents = na;
257                                 head = 0;
258                                 tail = head + size;
259                         }
260                 }
261         }
262 }
263