]>
Commit | Line | Data |
---|---|---|
c978d361 | 1 | ///////////////////////////////////////////////////////////////////////////// |
521bf4ff | 2 | // Name: src/x11/reparent.cpp |
c978d361 JS |
3 | // Purpose: wxWindow |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 2002-03-09 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
c978d361 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
521bf4ff WS |
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 | ||
c978d361 JS |
19 | // ============================================================================ |
20 | // declarations | |
21 | // ============================================================================ | |
22 | ||
23 | // ---------------------------------------------------------------------------- | |
24 | // headers | |
25 | // ---------------------------------------------------------------------------- | |
26 | ||
15d5a947 JS |
27 | #if !wxUSE_NANOX |
28 | ||
2b5f62a0 | 29 | #include "wx/x11/reparent.h" |
e4db172a WS |
30 | |
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/log.h" | |
670f9935 | 33 | #include "wx/app.h" |
c0badb70 | 34 | #include "wx/timer.h" |
e4db172a WS |
35 | #endif |
36 | ||
c978d361 | 37 | #include "wx/evtloop.h" |
c978d361 JS |
38 | |
39 | #include "wx/x11/private.h" | |
40 | #include "X11/Xatom.h" | |
41 | ||
c2ca375c VZ |
42 | #include "wx/generic/private/timer.h" |
43 | ||
c978d361 JS |
44 | /* |
45 | * wxAdoptedWindow | |
46 | */ | |
47 | ||
48 | wxAdoptedWindow::wxAdoptedWindow() | |
49 | { | |
50 | } | |
51 | ||
52 | wxAdoptedWindow::wxAdoptedWindow(WXWindow window) | |
53 | { | |
ab6b6b15 | 54 | m_mainWindow = window; |
c978d361 JS |
55 | } |
56 | ||
57 | wxAdoptedWindow::~wxAdoptedWindow() | |
58 | { | |
59 | } | |
174046a3 | 60 | |
c978d361 JS |
61 | /* |
62 | * wxReparenter | |
63 | */ | |
64 | ||
65 | static bool Xerror; | |
66 | static Atom WM_STATE = 0; | |
670f9935 | 67 | bool wxReparenter::sm_done = false; |
c978d361 JS |
68 | wxAdoptedWindow* wxReparenter::sm_toReparent = NULL; |
69 | wxWindow* wxReparenter::sm_newParent = NULL; | |
70 | wxString wxReparenter::sm_name; | |
670f9935 | 71 | bool wxReparenter::sm_exactMatch = false; |
c978d361 | 72 | |
174046a3 | 73 | static int ErrorHandler(Display* dpy, XErrorEvent* event) |
c978d361 | 74 | { |
174046a3 JS |
75 | Xerror = True; |
76 | return False; | |
c978d361 JS |
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); | |
ab6b6b15 RR |
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) | |
c978d361 JS |
100 | { |
101 | XSetErrorHandler(old); | |
521bf4ff | 102 | return true; |
c978d361 JS |
103 | } |
104 | ||
105 | if (numchildren > 0) | |
106 | { | |
046ba576 JS |
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); | |
c978d361 JS |
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 | { | |
ab6b6b15 RR |
120 | XGetWindowAttributes( wxGlobalDisplay(), |
121 | children[each], &xwa); | |
c978d361 JS |
122 | fprintf(stderr, |
123 | "Reparenting child at offset %d and position %d, %d.\n", | |
124 | parentOffset, parentOffset+xwa.x, parentOffset+xwa.y); | |
ab6b6b15 RR |
125 | XReparentWindow( wxGlobalDisplay(), |
126 | children[each], (Window) newParent->GetMainWindow(), | |
127 | xwa.x, xwa.y); | |
c978d361 JS |
128 | } |
129 | } | |
130 | ||
131 | XSetErrorHandler(old); | |
521bf4ff | 132 | return true; |
c978d361 JS |
133 | } |
134 | ||
135 | // Wait for an appropriate window to be created. | |
670f9935 | 136 | // If exactMatch is false, a substring match is OK. |
c978d361 JS |
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; | |
521bf4ff | 146 | |
ab6b6b15 | 147 | Display* display = wxGlobalDisplay(); |
c978d361 JS |
148 | XSelectInput(display, |
149 | RootWindowOfScreen(DefaultScreenOfDisplay(display)), | |
150 | SubstructureNotifyMask); | |
151 | ||
152 | if (!WM_STATE) | |
153 | WM_STATE = XInternAtom(display, "WM_STATE", False); | |
154 | ||
155 | #ifdef __WXDEBUG__ | |
521bf4ff | 156 | if (!windowName.empty()) |
c978d361 JS |
157 | wxLogDebug(_T("Waiting for window %s"), windowName.c_str()); |
158 | #endif | |
521bf4ff | 159 | |
670f9935 | 160 | sm_done = false; |
c978d361 JS |
161 | |
162 | wxEventLoop eventLoop; | |
163 | while (!sm_done) | |
164 | { | |
165 | if (eventLoop.Pending()) | |
166 | { | |
167 | XEvent xevent; | |
168 | XNextEvent(display, & xevent); | |
169 | if (!wxTheApp->ProcessXEvent((WXEvent*) & xevent)) | |
170 | { | |
171 | // Do the local event processing | |
172 | ProcessXEvent((WXEvent*) & xevent); | |
173 | } | |
174 | } | |
175 | else | |
176 | { | |
177 | #if wxUSE_TIMER | |
c2ca375c | 178 | wxGenericTimerImpl::NotifyTimers(); |
a0749355 | 179 | wxTheApp->ProcessIdle(); |
c978d361 JS |
180 | #endif |
181 | } | |
182 | } | |
521bf4ff | 183 | return true; |
c978d361 JS |
184 | } |
185 | ||
186 | bool wxReparenter::ProcessXEvent(WXEvent* event) | |
187 | { | |
188 | XEvent* xevent = (XEvent*) event; | |
189 | Window client; | |
190 | ||
191 | if (!sm_done) | |
192 | { | |
193 | if (xevent->type == MapNotify) | |
194 | { | |
195 | wxLogDebug(_T("Window was mapped")); | |
196 | } | |
521bf4ff | 197 | |
c978d361 JS |
198 | if (xevent->type == MapNotify && !xevent->xmap.override_redirect && |
199 | (client = (Window) FindAClientWindow((WXWindow) xevent->xmap.window, sm_name))) | |
200 | { | |
201 | wxLogDebug(_T("Found a client window, about to reparent")); | |
202 | wxASSERT(sm_toReparent->GetParent() == NULL); | |
521bf4ff | 203 | |
c978d361 JS |
204 | sm_toReparent->SetHandle((WXWindow) client); |
205 | sm_newParent->AddChild(sm_toReparent); | |
206 | sm_done = Reparent(sm_newParent, sm_toReparent); | |
207 | return sm_done; | |
208 | } else if (xevent->type == MapNotify && | |
209 | xevent->xmap.override_redirect && | |
210 | xevent->xmap.window) | |
211 | { | |
212 | wxLogDebug(_T("Found an override redirect window, about to reparent")); | |
213 | sm_toReparent->SetHandle((WXWindow) xevent->xmap.window); | |
214 | sm_newParent->AddChild(sm_toReparent); | |
215 | wxASSERT(sm_toReparent->GetParent() == NULL); | |
521bf4ff | 216 | |
c978d361 JS |
217 | sm_done = Reparent(sm_newParent, sm_toReparent); |
218 | return sm_done; | |
219 | } | |
220 | } | |
670f9935 | 221 | return false; |
c978d361 JS |
222 | } |
223 | ||
224 | WXWindow wxReparenter::FindAClientWindow(WXWindow window, const wxString& name) | |
225 | { | |
174046a3 JS |
226 | int rvalue, i; |
227 | Atom actualtype; | |
228 | int actualformat; | |
229 | unsigned long nitems, bytesafter; | |
230 | unsigned char *propreturn; | |
231 | Window *children; | |
232 | unsigned int numchildren; | |
233 | Window returnroot, returnparent; | |
234 | Window result = 0; | |
235 | XErrorHandler old; | |
236 | char *clientName; | |
521bf4ff | 237 | |
174046a3 JS |
238 | Xerror = False; |
239 | old = XSetErrorHandler(ErrorHandler); | |
240 | rvalue = XGetWindowProperty((Display*) wxGetDisplay(), | |
241 | (Window) window, WM_STATE, | |
242 | 0, 1, False, | |
243 | AnyPropertyType, &actualtype, &actualformat, | |
244 | &nitems, &bytesafter, &propreturn); | |
c978d361 | 245 | |
c978d361 | 246 | XSetErrorHandler(old); |
c978d361 | 247 | |
174046a3 JS |
248 | if (!Xerror && rvalue == Success && actualtype != None) |
249 | { | |
250 | if (rvalue == Success) | |
251 | { | |
252 | XFree((char *) propreturn); | |
253 | } | |
254 | XFetchName((Display*) wxGetDisplay(), (Window) window, &clientName); | |
521bf4ff | 255 | |
174046a3 JS |
256 | wxString str1(name); |
257 | wxString str2 = wxString::FromAscii(clientName); | |
258 | str1.Lower(); | |
259 | str2.Lower(); | |
521bf4ff | 260 | |
174046a3 JS |
261 | bool matches; |
262 | if (sm_exactMatch) | |
263 | matches = (name == wxString::FromAscii(clientName)); | |
264 | else | |
265 | matches = (str1.Contains(str2) || str2.Contains(str1)); | |
521bf4ff | 266 | |
174046a3 | 267 | XFree(clientName); |
521bf4ff | 268 | |
174046a3 JS |
269 | if (matches) |
270 | return (WXWindow) window; | |
271 | else | |
272 | return NULL; | |
273 | } | |
521bf4ff | 274 | |
174046a3 JS |
275 | old = XSetErrorHandler(ErrorHandler); |
276 | if (!XQueryTree((Display*) wxGetDisplay(), (Window) window, &returnroot, &returnparent, | |
277 | &children, &numchildren) || Xerror) | |
278 | { | |
279 | XSetErrorHandler(old); | |
280 | return NULL; | |
281 | } | |
282 | XSetErrorHandler(old); | |
521bf4ff | 283 | |
174046a3 JS |
284 | result = 0; |
285 | for (i=0; i<(int)numchildren && !result ;i++) { | |
286 | result = (Window) FindAClientWindow((WXWindow) children[i], name); | |
287 | } | |
288 | if (numchildren) { | |
289 | XFree((char *) children); | |
290 | } return (WXWindow) result; | |
c978d361 JS |
291 | } |
292 | ||
e4db172a | 293 | #endif // !wxUSE_NANOX |