New test.
[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                         xqueue.Enqueue (xevent);
82                 }
83
84                 public void EnqueueLocked (XEvent xevent)
85                 {
86                         lock (lqueue) {
87                                 lqueue.Enqueue (xevent);
88                         }
89                 }
90
91                 public XEvent Dequeue ()
92                 {
93                         if (xqueue.Count == 0) {
94                                 lock (lqueue) {
95                                         return lqueue.Dequeue ();
96                                 }
97                         }
98                         return xqueue.Dequeue ();
99                 }
100
101                 public XEvent Peek()
102                 {
103                         if (xqueue.Count == 0) {
104                                 lock (lqueue) {
105                                         return lqueue.Peek ();
106                                 }
107                         }                               
108                         return xqueue.Peek();
109                 }
110
111                 public bool DispatchIdle {
112                         get {
113                                 return dispatch_idle;
114                         }
115                         set {
116                                 dispatch_idle = value;
117                         }
118                 }
119
120                 public bool PostQuitState {
121                         get {
122                                 return quit_posted;
123                         }
124
125                         set {
126                                 quit_posted = value;
127                         }
128                 }
129
130                 public class PaintQueue {
131
132                         private ArrayList       hwnds;
133                         private XEvent          xevent;
134                         
135                         public PaintQueue (int size) {
136                                 hwnds = new ArrayList(size);
137                                 xevent = new XEvent();
138                                 xevent.AnyEvent.type = XEventName.Expose;
139                         }
140
141                         public int Count {
142                                 get { return hwnds.Count; }
143                         }
144
145                         public void Enqueue (Hwnd hwnd) {
146                                 hwnds.Add(hwnd);
147                         }
148
149                         public void Remove(Hwnd hwnd) {
150                                 if (!hwnd.expose_pending && !hwnd.nc_expose_pending) {
151                                         hwnds.Remove(hwnd);
152                                 }
153                         }
154
155                         public XEvent Dequeue () {
156                                 Hwnd            hwnd;
157                                 IEnumerator     next;
158
159                                 if (hwnds.Count == 0) {
160                                         xevent.ExposeEvent.window = IntPtr.Zero;
161                                         return xevent;
162                                 }
163
164                                 next = hwnds.GetEnumerator();
165                                 next.MoveNext();
166                                 hwnd = (Hwnd)next.Current;
167
168                                 // We only remove the event from the queue if we have one expose left since
169                                 // a single 'entry in our queue may be for both NC and Client exposed
170                                 if ( !(hwnd.nc_expose_pending && hwnd.expose_pending)) {
171                                         hwnds.Remove(hwnd);
172                                 }
173                                 if (hwnd.expose_pending) {
174                                         xevent.ExposeEvent.window = hwnd.client_window;
175 #if not
176                                         xevent.ExposeEvent.x = hwnd.invalid.X;
177                                         xevent.ExposeEvent.y = hwnd.invalid.Y;
178                                         xevent.ExposeEvent.width = hwnd.invalid.Width;
179                                         xevent.ExposeEvent.height = hwnd.invalid.Height;
180 #endif
181                                         return xevent;
182                                 } else {
183                                         xevent.ExposeEvent.window = hwnd.whole_window;
184                                         xevent.ExposeEvent.x = hwnd.nc_invalid.X;
185                                         xevent.ExposeEvent.y = hwnd.nc_invalid.Y;
186                                         xevent.ExposeEvent.width = hwnd.nc_invalid.Width;
187                                         xevent.ExposeEvent.height = hwnd.nc_invalid.Height;
188                                         return xevent;
189                                 }
190                         }
191                 }
192
193                 private class XQueue {
194
195                         private XEvent [] xevents;
196                         private int head;
197                         private int tail;
198                         private int size;
199                         
200                         public XQueue (int size)
201                         {
202                                 xevents = new XEvent [size];
203                         }
204
205                         public int Count {
206                                 get { return size; }
207                         }
208
209                         public void Enqueue (XEvent xevent)
210                         {
211                                 if (size == xevents.Length)
212                                         Grow ();
213                                 
214                                 xevents [tail] = xevent;
215                                 tail = (tail + 1) % xevents.Length;
216                                 size++;
217                         }
218
219                         public XEvent Dequeue ()
220                         {
221                                 if (size < 1)
222                                         throw new Exception ("Attempt to dequeue empty queue.");
223                                 XEvent res = xevents [head];
224                                 head = (head + 1) % xevents.Length;
225                                 size--;
226                                 return res;
227                         }
228
229                         public XEvent Peek() {
230                                 if (size < 1) {
231                                         throw new Exception ("Attempt to peek at empty queue");
232                                 }
233                                 return xevents[head];
234                         }
235
236                         private void Grow ()
237                         {
238                                 int newcap = (xevents.Length * 2);
239                                 XEvent [] na = new XEvent [newcap];
240                                 xevents.CopyTo (na, 0);
241                                 xevents = na;
242                                 head = 0;
243                                 tail = head + size;
244                         }
245                 }
246         }
247 }
248