merge -r 61110:61111
[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                 private Thread          thread;
41
42                 private static readonly int InitialXEventSize = 100;
43                 private static readonly int InitialLXEventSize = 10;
44                 private static readonly int InitialPaintSize = 50;
45
46                 public XEventQueue (Thread thread) {
47                         xqueue = new XQueue (InitialXEventSize);
48                         lqueue = new XQueue (InitialLXEventSize);
49                         paint = new PaintQueue(InitialPaintSize);
50                         this.thread = thread;
51                 }
52
53                 public int Count {
54                         get {
55                                 lock (lqueue) {
56                                         return xqueue.Count + lqueue.Count;
57                                 }
58                         }
59                 }
60
61                 public PaintQueue Paint {
62                         get {
63                                 return paint;
64                         }
65                 }
66
67                 public Thread Thread {
68                         get {
69                                 return thread;
70                         }
71                 }
72
73                 public void Enqueue (XEvent xevent)
74                 {
75                         xqueue.Enqueue (xevent);
76                 }
77
78                 public void EnqueueLocked (XEvent xevent)
79                 {
80                         lock (lqueue) {
81                                 lqueue.Enqueue (xevent);
82                         }
83                 }
84
85                 public XEvent Dequeue ()
86                 {
87                         if (xqueue.Count == 0) {
88                                 lock (lqueue) {
89                                         return lqueue.Dequeue ();
90                                 }
91                         }
92                         return xqueue.Dequeue ();
93                 }
94
95                 public class PaintQueue {
96
97                         private ArrayList       hwnds;
98                         private XEvent          xevent;
99                         
100                         public PaintQueue (int size) {
101                                 hwnds = new ArrayList(size);
102                                 xevent = new XEvent();
103                                 xevent.AnyEvent.type = XEventName.Expose;
104                         }
105
106                         public int Count {
107                                 get { return hwnds.Count; }
108                         }
109
110                         public void Enqueue (Hwnd hwnd) {
111                                 hwnds.Add(hwnd);
112                         }
113
114                         public void Remove(Hwnd hwnd) {
115                                 if (!hwnd.expose_pending && !hwnd.nc_expose_pending) {
116                                         hwnds.Remove(hwnd);
117                                 }
118                         }
119
120                         public XEvent Dequeue () {
121                                 Hwnd            hwnd;
122                                 IEnumerator     next;
123
124                                 if (hwnds.Count == 0) {
125                                         xevent.ExposeEvent.window = IntPtr.Zero;
126                                         return xevent;
127                                 }
128
129                                 next = hwnds.GetEnumerator();
130                                 next.MoveNext();
131                                 hwnd = (Hwnd)next.Current;
132
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)) {
136                                         hwnds.Remove(hwnd);
137                                 }
138                                 if (hwnd.expose_pending) {
139                                         xevent.ExposeEvent.window = hwnd.client_window;
140 #if not
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;
145 #endif
146                                         return xevent;
147                                 } else {
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;
153                                         return xevent;
154                                 }
155                         }
156                 }
157
158                 private class XQueue {
159
160                         private XEvent [] xevents;
161                         private int head;
162                         private int tail;
163                         private int size;
164                         
165                         public XQueue (int size)
166                         {
167                                 xevents = new XEvent [size];
168                         }
169
170                         public int Count {
171                                 get { return size; }
172                         }
173
174                         public void Enqueue (XEvent xevent)
175                         {
176                                 if (size == xevents.Length)
177                                         Grow ();
178                                 
179                                 xevents [tail] = xevent;
180                                 tail = (tail + 1) % xevents.Length;
181                                 size++;
182                         }
183
184                         public XEvent Dequeue ()
185                         {
186                                 if (size < 1)
187                                         throw new Exception ("Attempt to dequeue empty queue.");
188                                 XEvent res = xevents [head];
189                                 head = (head + 1) % xevents.Length;
190                                 size--;
191                                 return res;
192                         }
193
194                         private void Grow ()
195                         {
196                                 int newcap = (xevents.Length * 2);
197                                 XEvent [] na = new XEvent [newcap];
198                                 xevents.CopyTo (na, 0);
199                                 xevents = na;
200                                 head = 0;
201                                 tail = head + size;
202                         }
203                 }
204         }
205 }
206