* TextBoxBase.cs: Take HideSelection into account when
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / XEventQueue.cs
blob29812fd1e56ebf14615e7f5e70c7c614a71f9ac1
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 quit_posted;
43 private bool dispatch_idle;
45 private static readonly int InitialXEventSize = 100;
46 private static readonly int InitialLXEventSize = 10;
47 private static readonly int InitialPaintSize = 50;
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;
59 public int Count {
60 get {
61 lock (lqueue) {
62 return xqueue.Count + lqueue.Count;
67 public PaintQueue Paint {
68 get {
69 return paint;
73 public Thread Thread {
74 get {
75 return thread;
79 public void Enqueue (XEvent xevent)
81 if (Thread.CurrentThread != thread) {
82 Console.WriteLine ("Hwnd.Queue.Enqueue called from a different thread without locking.");
83 Console.WriteLine (Environment.StackTrace);
86 xqueue.Enqueue (xevent);
89 public void EnqueueLocked (XEvent xevent)
91 lock (lqueue) {
92 lqueue.Enqueue (xevent);
96 public XEvent Dequeue ()
98 if (Thread.CurrentThread != thread) {
99 Console.WriteLine ("Hwnd.Queue.Dequeue called from a different thread without locking.");
100 Console.WriteLine (Environment.StackTrace);
103 if (xqueue.Count == 0) {
104 lock (lqueue) {
105 return lqueue.Dequeue ();
108 return xqueue.Dequeue ();
111 public XEvent Peek()
113 if (Thread.CurrentThread != thread) {
114 Console.WriteLine ("Hwnd.Queue.Peek called from a different thread without locking.");
115 Console.WriteLine (Environment.StackTrace);
118 if (xqueue.Count == 0) {
119 lock (lqueue) {
120 return lqueue.Peek ();
123 return xqueue.Peek();
126 public bool DispatchIdle {
127 get {
128 return dispatch_idle;
130 set {
131 dispatch_idle = value;
135 public bool PostQuitState {
136 get {
137 return quit_posted;
140 set {
141 quit_posted = value;
145 public class PaintQueue {
147 private ArrayList hwnds;
148 private XEvent xevent;
150 public PaintQueue (int size) {
151 hwnds = new ArrayList(size);
152 xevent = new XEvent();
153 xevent.AnyEvent.type = XEventName.Expose;
156 public int Count {
157 get { return hwnds.Count; }
160 public void Enqueue (Hwnd hwnd) {
161 hwnds.Add(hwnd);
164 public void Remove(Hwnd hwnd) {
165 if (!hwnd.expose_pending && !hwnd.nc_expose_pending) {
166 hwnds.Remove(hwnd);
170 public XEvent Dequeue () {
171 Hwnd hwnd;
172 IEnumerator next;
174 if (hwnds.Count == 0) {
175 xevent.ExposeEvent.window = IntPtr.Zero;
176 return xevent;
179 next = hwnds.GetEnumerator();
180 next.MoveNext();
181 hwnd = (Hwnd)next.Current;
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);
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;
208 private class XQueue {
210 private XEvent [] xevents;
211 private int head;
212 private int tail;
213 private int size;
215 public XQueue (int size)
217 xevents = new XEvent [size];
220 public int Count {
221 get { return size; }
224 public void Enqueue (XEvent xevent)
226 if (size == xevents.Length)
227 Grow ();
229 xevents [tail] = xevent;
230 tail = (tail + 1) % xevents.Length;
231 size++;
234 public XEvent Dequeue ()
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;
244 public XEvent Peek() {
245 if (size < 1) {
246 throw new Exception ("Attempt to peek at empty queue");
248 return xevents[head];
251 private void Grow ()
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;