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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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
25 // Jackson Harper (jackson@ximian.com)
26 // Peter Dennis Bartok (pbartok@novell.com)
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 private Thread thread;
42 private static readonly int InitialXEventSize = 100;
43 private static readonly int InitialLXEventSize = 10;
44 private static readonly int InitialPaintSize = 50;
46 public XEventQueue (Thread thread) {
47 xqueue = new XQueue (InitialXEventSize);
48 lqueue = new XQueue (InitialLXEventSize);
49 paint = new PaintQueue(InitialPaintSize);
56 return xqueue.Count + lqueue.Count;
61 public PaintQueue Paint {
67 public Thread Thread {
73 public void Enqueue (XEvent xevent)
75 xqueue.Enqueue (xevent);
78 public void EnqueueLocked (XEvent xevent)
81 lqueue.Enqueue (xevent);
85 public XEvent Dequeue ()
87 if (xqueue.Count == 0) {
89 return lqueue.Dequeue ();
92 return xqueue.Dequeue ();
95 public class PaintQueue {
97 private ArrayList hwnds;
98 private XEvent xevent;
100 public PaintQueue (int size) {
101 hwnds = new ArrayList(size);
102 xevent = new XEvent();
103 xevent.AnyEvent.type = XEventName.Expose;
107 get { return hwnds.Count; }
110 public void Enqueue (Hwnd hwnd) {
114 public void Remove(Hwnd hwnd) {
115 if (!hwnd.expose_pending && !hwnd.nc_expose_pending) {
120 public XEvent Dequeue () {
124 if (hwnds.Count == 0) {
125 xevent.ExposeEvent.window = IntPtr.Zero;
129 next = hwnds.GetEnumerator();
131 hwnd = (Hwnd)next.Current;
133 // We only remove the event from the queue if we have one expose left since
134 // a single 'entry in our queue may be for both NC and Client exposed
135 if ( !(hwnd.nc_expose_pending && hwnd.expose_pending)) {
138 if (hwnd.expose_pending) {
139 xevent.ExposeEvent.window = hwnd.client_window;
141 xevent.ExposeEvent.x = hwnd.invalid.X;
142 xevent.ExposeEvent.y = hwnd.invalid.Y;
143 xevent.ExposeEvent.width = hwnd.invalid.Width;
144 xevent.ExposeEvent.height = hwnd.invalid.Height;
148 xevent.ExposeEvent.window = hwnd.whole_window;
149 xevent.ExposeEvent.x = hwnd.nc_invalid.X;
150 xevent.ExposeEvent.y = hwnd.nc_invalid.Y;
151 xevent.ExposeEvent.width = hwnd.nc_invalid.Width;
152 xevent.ExposeEvent.height = hwnd.nc_invalid.Height;
158 private class XQueue {
160 private XEvent [] xevents;
165 public XQueue (int size)
167 xevents = new XEvent [size];
174 public void Enqueue (XEvent xevent)
176 if (size == xevents.Length)
179 xevents [tail] = xevent;
180 tail = (tail + 1) % xevents.Length;
184 public XEvent Dequeue ()
187 throw new Exception ("Attempt to dequeue empty queue.");
188 XEvent res = xevents [head];
189 head = (head + 1) % xevents.Length;
196 int newcap = (xevents.Length * 2);
197 XEvent [] na = new XEvent [newcap];
198 xevents.CopyTo (na, 0);