]> git.saurik.com Git - wxWidgets.git/blob - src/x11/reparent.cpp
Improve wxCollapsiblePane appearance under OS X.
[wxWidgets.git] / src / x11 / reparent.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/reparent.cpp
3 // Purpose: wxWindow
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2002-03-09
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if defined(__BORLANDC__)
16 #pragma hdrstop
17 #endif
18
19 // ============================================================================
20 // declarations
21 // ============================================================================
22
23 // ----------------------------------------------------------------------------
24 // headers
25 // ----------------------------------------------------------------------------
26
27 #if !wxUSE_NANOX
28
29 #include "wx/x11/reparent.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/log.h"
33 #include "wx/app.h"
34 #include "wx/timer.h"
35 #endif
36
37 #include "wx/evtloop.h"
38
39 #include "wx/x11/private.h"
40 #include "X11/Xatom.h"
41
42 #include "wx/generic/private/timer.h"
43
44 /*
45 * wxAdoptedWindow
46 */
47
48 wxAdoptedWindow::wxAdoptedWindow()
49 {
50 }
51
52 wxAdoptedWindow::wxAdoptedWindow(WXWindow window)
53 {
54 m_mainWindow = window;
55 }
56
57 wxAdoptedWindow::~wxAdoptedWindow()
58 {
59 }
60
61 /*
62 * wxReparenter
63 */
64
65 static bool Xerror;
66 static Atom WM_STATE = 0;
67 bool wxReparenter::sm_done = false;
68 wxAdoptedWindow* wxReparenter::sm_toReparent = NULL;
69 wxWindow* wxReparenter::sm_newParent = NULL;
70 wxString wxReparenter::sm_name;
71 bool wxReparenter::sm_exactMatch = false;
72
73 static int ErrorHandler(Display* WXUNUSED(dpy), XErrorEvent* WXUNUSED(event))
74 {
75 Xerror = True;
76 return False;
77 }
78
79 // We assume that toReparent has had its X window set
80 // appropriately.
81 bool wxReparenter::Reparent(wxWindow* newParent, wxAdoptedWindow* toReparent)
82 {
83 XWindowAttributes xwa;
84 Window *children;
85 unsigned int numchildren, each;
86 Window returnroot, returnparent;
87 XErrorHandler old;
88 int parentOffset = 0;
89
90 old = XSetErrorHandler(ErrorHandler);
91 XReparentWindow( wxGlobalDisplay(),
92 (Window) toReparent->GetMainWindow(),
93 (Window) newParent->GetMainWindow(),
94 0, 0);
95
96 if (!XQueryTree( wxGlobalDisplay(),
97 (Window) toReparent->GetMainWindow(),
98 &returnroot, &returnparent,
99 &children, &numchildren) || Xerror)
100 {
101 XSetErrorHandler(old);
102 return true;
103 }
104
105 if (numchildren > 0)
106 {
107 // TEST: see if we can get away with reparenting just
108 // first one
109 if (numchildren > 1)
110 {
111 wxLogDebug(wxT("Found %d, but only reparenting 1 child."), numchildren);
112 numchildren = 1;
113 }
114 wxLogDebug(wxT("Reparenting %d children."), numchildren);
115 /* Stacking order is preserved since XQueryTree returns its children in
116 bottommost to topmost order
117 */
118 for (each=0; each<numchildren; each++)
119 {
120 XGetWindowAttributes( wxGlobalDisplay(),
121 children[each], &xwa);
122 fprintf(stderr,
123 "Reparenting child at offset %d and position %d, %d.\n",
124 parentOffset, parentOffset+xwa.x, parentOffset+xwa.y);
125 XReparentWindow( wxGlobalDisplay(),
126 children[each], (Window) newParent->GetMainWindow(),
127 xwa.x, xwa.y);
128 }
129 }
130
131 XSetErrorHandler(old);
132 return true;
133 }
134
135 // Wait for an appropriate window to be created.
136 // If exactMatch is false, a substring match is OK.
137 // If windowName is empty, then wait for the next overrideRedirect window.
138 bool wxReparenter::WaitAndReparent(wxWindow* newParent, wxAdoptedWindow* toReparent,
139 const wxString& windowName,
140 bool exactMatch)
141 {
142 sm_newParent = newParent;
143 sm_toReparent = toReparent;
144 sm_exactMatch = exactMatch;
145 sm_name = windowName;
146
147 Display* display = wxGlobalDisplay();
148 XSelectInput(display,
149 RootWindowOfScreen(DefaultScreenOfDisplay(display)),
150 SubstructureNotifyMask);
151
152 if (!WM_STATE)
153 WM_STATE = XInternAtom(display, "WM_STATE", False);
154
155 sm_done = false;
156
157 wxEventLoop eventLoop;
158 while (!sm_done)
159 {
160 if (eventLoop.Pending())
161 {
162 XEvent xevent;
163 XNextEvent(display, & xevent);
164 if (!wxTheApp->ProcessXEvent((WXEvent*) & xevent))
165 {
166 // Do the local event processing
167 ProcessXEvent((WXEvent*) & xevent);
168 }
169 }
170 else
171 {
172 #if wxUSE_TIMER
173 wxGenericTimerImpl::NotifyTimers();
174 wxTheApp->ProcessIdle();
175 #endif
176 }
177 }
178 return true;
179 }
180
181 bool wxReparenter::ProcessXEvent(WXEvent* event)
182 {
183 XEvent* xevent = (XEvent*) event;
184 Window client;
185
186 if (!sm_done)
187 {
188 if (xevent->type == MapNotify)
189 {
190 wxLogDebug(wxT("Window was mapped"));
191 }
192
193 if (xevent->type == MapNotify && !xevent->xmap.override_redirect &&
194 (client = (Window) FindAClientWindow((WXWindow) xevent->xmap.window, sm_name)))
195 {
196 wxLogDebug(wxT("Found a client window, about to reparent"));
197 wxASSERT(sm_toReparent->GetParent() == NULL);
198
199 sm_toReparent->SetHandle((WXWindow) client);
200 sm_newParent->AddChild(sm_toReparent);
201 sm_done = Reparent(sm_newParent, sm_toReparent);
202 return sm_done;
203 } else if (xevent->type == MapNotify &&
204 xevent->xmap.override_redirect &&
205 xevent->xmap.window)
206 {
207 wxLogDebug(wxT("Found an override redirect window, about to reparent"));
208 sm_toReparent->SetHandle((WXWindow) xevent->xmap.window);
209 sm_newParent->AddChild(sm_toReparent);
210 wxASSERT(sm_toReparent->GetParent() == NULL);
211
212 sm_done = Reparent(sm_newParent, sm_toReparent);
213 return sm_done;
214 }
215 }
216 return false;
217 }
218
219 WXWindow wxReparenter::FindAClientWindow(WXWindow window, const wxString& name)
220 {
221 int rvalue, i;
222 Atom actualtype;
223 int actualformat;
224 unsigned long nitems, bytesafter;
225 unsigned char *propreturn;
226 Window *children;
227 unsigned int numchildren;
228 Window returnroot, returnparent;
229 Window result = 0;
230 XErrorHandler old;
231 char *clientName;
232
233 Xerror = False;
234 old = XSetErrorHandler(ErrorHandler);
235 rvalue = XGetWindowProperty((Display*) wxGetDisplay(),
236 (Window) window, WM_STATE,
237 0, 1, False,
238 AnyPropertyType, &actualtype, &actualformat,
239 &nitems, &bytesafter, &propreturn);
240
241 XSetErrorHandler(old);
242
243 if (!Xerror && rvalue == Success && actualtype != None)
244 {
245 if (rvalue == Success)
246 {
247 XFree((char *) propreturn);
248 }
249 XFetchName((Display*) wxGetDisplay(), (Window) window, &clientName);
250
251 wxString str1(name);
252 wxString str2 = wxString::FromAscii(clientName);
253 str1.Lower();
254 str2.Lower();
255
256 bool matches;
257 if (sm_exactMatch)
258 matches = (name == wxString::FromAscii(clientName));
259 else
260 matches = (str1.Contains(str2) || str2.Contains(str1));
261
262 XFree(clientName);
263
264 if (matches)
265 return (WXWindow) window;
266 else
267 return NULL;
268 }
269
270 old = XSetErrorHandler(ErrorHandler);
271 if (!XQueryTree((Display*) wxGetDisplay(), (Window) window, &returnroot, &returnparent,
272 &children, &numchildren) || Xerror)
273 {
274 XSetErrorHandler(old);
275 return NULL;
276 }
277 XSetErrorHandler(old);
278
279 result = 0;
280 for (i=0; i<(int)numchildren && !result ;i++) {
281 result = (Window) FindAClientWindow((WXWindow) children[i], name);
282 }
283 if (numchildren) {
284 XFree((char *) children);
285 } return (WXWindow) result;
286 }
287
288 #endif // !wxUSE_NANOX