2007-03-19 Chris Toshok <toshok@ximian.com>
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / XEventQueue.cs
blob11391af1a7279093caeaea71e177f8d3447a5adb
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.
20 // Copyright (c) 2004-2006 Novell, Inc.
22 // System.Windows.Forms.XEventQueue
24 // Authors:
25 // Jackson Harper (jackson@ximian.com)
26 // Peter Dennis Bartok (pbartok@novell.com)
29 using System;
30 using System.Threading;
31 using System.Collections;
33 namespace System.Windows.Forms {
35 internal class XEventQueue {
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 dispatch_idle;
44 private static readonly int InitialXEventSize = 100;
45 private static readonly int InitialLXEventSize = 10;
46 private static readonly int InitialPaintSize = 50;
48 public XEventQueue (Thread thread) {
49 xqueue = new XQueue (InitialXEventSize);
50 lqueue = new XQueue (InitialLXEventSize);
51 paint = new PaintQueue(InitialPaintSize);
52 timer_list = new ArrayList ();
53 this.thread = thread;
54 this.dispatch_idle = true;
57 public int Count {
58 get {
59 lock (lqueue) {
60 return xqueue.Count + lqueue.Count;
65 public PaintQueue Paint {
66 get {
67 return paint;
71 public Thread Thread {
72 get {
73 return thread;
77 public void Enqueue (XEvent xevent)
79 if (Thread.CurrentThread != thread) {
80 Console.WriteLine ("Hwnd.Queue.Enqueue called from a different thread without locking.");
81 Console.WriteLine (Environment.StackTrace);
84 xqueue.Enqueue (xevent);
87 public void EnqueueLocked (XEvent xevent)
89 lock (lqueue) {
90 lqueue.Enqueue (xevent);
94 public XEvent Dequeue ()
96 if (Thread.CurrentThread != thread) {
97 Console.WriteLine ("Hwnd.Queue.Dequeue called from a different thread without locking.");
98 Console.WriteLine (Environment.StackTrace);
101 if (xqueue.Count == 0) {
102 lock (lqueue) {
103 return lqueue.Dequeue ();
106 return xqueue.Dequeue ();
109 public XEvent Peek()
111 if (Thread.CurrentThread != thread) {
112 Console.WriteLine ("Hwnd.Queue.Peek called from a different thread without locking.");
113 Console.WriteLine (Environment.StackTrace);
116 if (xqueue.Count == 0) {
117 lock (lqueue) {
118 return lqueue.Peek ();
121 return xqueue.Peek();
124 public bool DispatchIdle {
125 get {
126 return dispatch_idle;
128 set {
129 dispatch_idle = value;
133 public class PaintQueue {
135 private ArrayList hwnds;
136 private XEvent xevent;
138 public PaintQueue (int size) {
139 hwnds = new ArrayList(size);
140 xevent = new XEvent();
141 xevent.AnyEvent.type = XEventName.Expose;
144 public int Count {
145 get { return hwnds.Count; }
148 public void Enqueue (Hwnd hwnd) {
149 hwnds.Add(hwnd);
152 public void Remove(Hwnd hwnd) {
153 if (!hwnd.expose_pending && !hwnd.nc_expose_pending) {
154 hwnds.Remove(hwnd);
158 public XEvent Dequeue () {
159 Hwnd hwnd;
160 IEnumerator next;
162 if (hwnds.Count == 0) {
163 xevent.ExposeEvent.window = IntPtr.Zero;
164 return xevent;
167 next = hwnds.GetEnumerator();
168 next.MoveNext();
169 hwnd = (Hwnd)next.Current;
171 // We only remove the event from the queue if we have one expose left since
172 // a single 'entry in our queue may be for both NC and Client exposed
173 if ( !(hwnd.nc_expose_pending && hwnd.expose_pending)) {
174 hwnds.Remove(hwnd);
176 if (hwnd.expose_pending) {
177 xevent.ExposeEvent.window = hwnd.client_window;
178 #if not
179 xevent.ExposeEvent.x = hwnd.invalid.X;
180 xevent.ExposeEvent.y = hwnd.invalid.Y;
181 xevent.ExposeEvent.width = hwnd.invalid.Width;
182 xevent.ExposeEvent.height = hwnd.invalid.Height;
183 #endif
184 return xevent;
185 } else {
186 xevent.ExposeEvent.window = hwnd.whole_window;
187 xevent.ExposeEvent.x = hwnd.nc_invalid.X;
188 xevent.ExposeEvent.y = hwnd.nc_invalid.Y;
189 xevent.ExposeEvent.width = hwnd.nc_invalid.Width;
190 xevent.ExposeEvent.height = hwnd.nc_invalid.Height;
191 return xevent;
196 private class XQueue {
198 private XEvent [] xevents;
199 private int head;
200 private int tail;
201 private int size;
203 public XQueue (int size)
205 xevents = new XEvent [size];
208 public int Count {
209 get { return size; }
212 public void Enqueue (XEvent xevent)
214 if (size == xevents.Length)
215 Grow ();
217 xevents [tail] = xevent;
218 tail = (tail + 1) % xevents.Length;
219 size++;
222 public XEvent Dequeue ()
224 if (size < 1)
225 throw new Exception ("Attempt to dequeue empty queue.");
226 XEvent res = xevents [head];
227 head = (head + 1) % xevents.Length;
228 size--;
229 return res;
232 public XEvent Peek() {
233 if (size < 1) {
234 throw new Exception ("Attempt to peek at empty queue");
236 return xevents[head];
239 private void Grow ()
241 int newcap = (xevents.Length * 2);
242 XEvent [] na = new XEvent [newcap];
243 xevents.CopyTo (na, 0);
244 xevents = na;
245 head = 0;
246 tail = head + size;