blob: f1d74da501d4868ca5a8912e6c348ae67e487007 [file] [log] [blame]
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001/*
2 * Server-side window handling
3 *
4 * Copyright (C) 2001 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexandre Julliard1a66d222001-08-28 18:44:52 +000019 */
20
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000021#include "config.h"
22#include "wine/port.h"
23
Alexandre Julliard1a66d222001-08-28 18:44:52 +000024#include <assert.h>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000025#include <stdarg.h>
Alexandre Julliard1a66d222001-08-28 18:44:52 +000026
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000027#include "windef.h"
Alexandre Julliard47f98212001-11-14 21:28:36 +000028#include "winbase.h"
29#include "wingdi.h"
30#include "winuser.h"
31
Alexandre Julliard1a66d222001-08-28 18:44:52 +000032#include "object.h"
33#include "request.h"
34#include "thread.h"
35#include "process.h"
36#include "user.h"
Alexandre Julliard805bdc52001-11-13 22:23:48 +000037#include "unicode.h"
Alexandre Julliard1a66d222001-08-28 18:44:52 +000038
Alexandre Julliard7a2017d2001-10-12 19:10:26 +000039/* a window property */
40struct property
41{
42 unsigned short type; /* property type (see below) */
43 atom_t atom; /* property atom */
Alexandre Julliard51885742002-05-30 20:12:58 +000044 obj_handle_t handle; /* property handle (user-defined storage) */
Alexandre Julliard7a2017d2001-10-12 19:10:26 +000045};
46
47enum property_type
48{
49 PROP_TYPE_FREE, /* free entry */
50 PROP_TYPE_STRING, /* atom that was originally a string */
51 PROP_TYPE_ATOM /* plain atom */
52};
53
54
Alexandre Julliard1a66d222001-08-28 18:44:52 +000055struct window
56{
Alexandre Julliard844ceb92001-10-09 23:26:40 +000057 struct window *parent; /* parent window */
Alexandre Julliard7dafa612002-09-25 00:21:56 +000058 user_handle_t owner; /* owner of this window */
Alexandre Julliard844ceb92001-10-09 23:26:40 +000059 struct window *first_child; /* first child in Z-order */
60 struct window *last_child; /* last child in Z-order */
61 struct window *first_unlinked; /* first child not linked in the Z-order list */
62 struct window *next; /* next window in Z-order */
63 struct window *prev; /* prev window in Z-order */
64 user_handle_t handle; /* full handle for this window */
65 struct thread *thread; /* thread owning the window */
Alexandre Julliard7a2017d2001-10-12 19:10:26 +000066 atom_t atom; /* class atom */
Alexandre Julliard5030bda2002-10-11 23:41:06 +000067 user_handle_t last_active; /* last active popup */
Alexandre Julliard0d509652001-10-16 21:55:37 +000068 rectangle_t window_rect; /* window rectangle */
69 rectangle_t client_rect; /* client rectangle */
Alexandre Julliardddc33172001-10-22 19:08:33 +000070 unsigned int style; /* window style */
71 unsigned int ex_style; /* window extended style */
72 unsigned int id; /* window id */
73 void* instance; /* creator instance */
74 void* user_data; /* user-specific data */
Alexandre Julliard805bdc52001-11-13 22:23:48 +000075 WCHAR *text; /* window caption text */
76 int paint_count; /* count of pending paints for this window */
Alexandre Julliard7a2017d2001-10-12 19:10:26 +000077 int prop_inuse; /* number of in-use window properties */
78 int prop_alloc; /* number of allocated window properties */
79 struct property *properties; /* window properties array */
Alexandre Julliard1a66d222001-08-28 18:44:52 +000080};
81
82static struct window *top_window; /* top-level (desktop) window */
83
Alexandre Julliarda09da0c2001-09-21 21:08:40 +000084
85/* retrieve a pointer to a window from its handle */
86inline static struct window *get_window( user_handle_t handle )
87{
88 struct window *ret = get_user_object( handle, USER_WINDOW );
89 if (!ret) set_error( STATUS_INVALID_HANDLE );
90 return ret;
91}
92
Alexandre Julliard844ceb92001-10-09 23:26:40 +000093/* unlink a window from the tree */
94static void unlink_window( struct window *win )
95{
96 struct window *parent = win->parent;
97
98 assert( parent );
99
100 if (win->next) win->next->prev = win->prev;
101 else if (parent->last_child == win) parent->last_child = win->prev;
102
103 if (win->prev) win->prev->next = win->next;
104 else if (parent->first_child == win) parent->first_child = win->next;
105 else if (parent->first_unlinked == win) parent->first_unlinked = win->next;
106}
107
108
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000109/* link a window into the tree (or unlink it if the new parent is NULL) */
110static void link_window( struct window *win, struct window *parent, struct window *previous )
111{
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000112 unlink_window( win ); /* unlink it from the previous location */
113
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000114 if (parent)
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000115 {
Alexandre Julliard7dafa612002-09-25 00:21:56 +0000116 win->parent = parent;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000117 if ((win->prev = previous))
118 {
119 if ((win->next = previous->next)) win->next->prev = win;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000120 else if (win->parent->last_child == previous) win->parent->last_child = win;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000121 win->prev->next = win;
122 }
123 else
124 {
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000125 if ((win->next = parent->first_child)) win->next->prev = win;
126 else win->parent->last_child = win;
127 parent->first_child = win;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000128 }
129 }
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000130 else /* move it to parent unlinked list */
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000131 {
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000132 parent = win->parent;
133 if ((win->next = parent->first_unlinked)) win->next->prev = win;
134 win->prev = NULL;
135 parent->first_unlinked = win;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000136 }
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000137}
138
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000139/* set a window property */
Alexandre Julliard51885742002-05-30 20:12:58 +0000140static void set_property( struct window *win, atom_t atom, obj_handle_t handle,
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000141 enum property_type type )
142{
143 int i, free = -1;
144 struct property *new_props;
145
146 /* check if it exists already */
147 for (i = 0; i < win->prop_inuse; i++)
148 {
149 if (win->properties[i].type == PROP_TYPE_FREE)
150 {
151 free = i;
152 continue;
153 }
154 if (win->properties[i].atom == atom)
155 {
156 win->properties[i].type = type;
157 win->properties[i].handle = handle;
158 return;
159 }
160 }
161
162 /* need to add an entry */
163 if (!grab_global_atom( atom )) return;
164 if (free == -1)
165 {
166 /* no free entry */
167 if (win->prop_inuse >= win->prop_alloc)
168 {
169 /* need to grow the array */
170 if (!(new_props = realloc( win->properties,
171 sizeof(*new_props) * (win->prop_alloc + 16) )))
172 {
173 set_error( STATUS_NO_MEMORY );
174 release_global_atom( atom );
175 return;
176 }
177 win->prop_alloc += 16;
178 win->properties = new_props;
179 }
180 free = win->prop_inuse++;
181 }
182 win->properties[free].atom = atom;
183 win->properties[free].type = type;
184 win->properties[free].handle = handle;
185}
186
187/* remove a window property */
Alexandre Julliard51885742002-05-30 20:12:58 +0000188static obj_handle_t remove_property( struct window *win, atom_t atom )
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000189{
190 int i;
191
192 for (i = 0; i < win->prop_inuse; i++)
193 {
194 if (win->properties[i].type == PROP_TYPE_FREE) continue;
195 if (win->properties[i].atom == atom)
196 {
197 release_global_atom( atom );
198 win->properties[i].type = PROP_TYPE_FREE;
199 return win->properties[i].handle;
200 }
201 }
202 /* FIXME: last error? */
203 return 0;
204}
205
206/* find a window property */
Alexandre Julliard51885742002-05-30 20:12:58 +0000207static obj_handle_t get_property( struct window *win, atom_t atom )
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000208{
209 int i;
210
211 for (i = 0; i < win->prop_inuse; i++)
212 {
213 if (win->properties[i].type == PROP_TYPE_FREE) continue;
214 if (win->properties[i].atom == atom) return win->properties[i].handle;
215 }
216 /* FIXME: last error? */
217 return 0;
218}
219
220/* destroy all properties of a window */
221inline static void destroy_properties( struct window *win )
222{
223 int i;
224
225 if (!win->properties) return;
226 for (i = 0; i < win->prop_inuse; i++)
227 {
228 if (win->properties[i].type == PROP_TYPE_FREE) continue;
229 release_global_atom( win->properties[i].atom );
230 }
231 free( win->properties );
232}
233
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000234/* destroy a window */
235static void destroy_window( struct window *win )
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000236{
237 assert( win != top_window );
238
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000239 /* destroy all children */
240 while (win->first_child) destroy_window( win->first_child );
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000241 while (win->first_unlinked) destroy_window( win->first_unlinked );
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000242
Alexandre Julliarde806c972001-11-24 03:44:47 +0000243 if (win->thread->queue)
244 {
245 if (win->paint_count) inc_queue_paint_count( win->thread, -win->paint_count );
246 queue_cleanup_window( win->thread, win->handle );
247 }
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000248 free_user_handle( win->handle );
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000249 destroy_properties( win );
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000250 unlink_window( win );
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000251 if (win->text) free( win->text );
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000252 memset( win, 0x55, sizeof(*win) );
253 free( win );
254}
255
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000256/* create a new window structure (note: the window is not linked in the window tree) */
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000257static struct window *create_window( struct window *parent, struct window *owner, atom_t atom )
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000258{
259 struct window *win = mem_alloc( sizeof(*win) );
260 if (!win) return NULL;
261
262 if (!(win->handle = alloc_user_handle( win, USER_WINDOW )))
263 {
264 free( win );
265 return NULL;
266 }
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000267 win->parent = parent;
Alexandre Julliard7dafa612002-09-25 00:21:56 +0000268 win->owner = owner ? owner->handle : 0;
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000269 win->first_child = NULL;
270 win->last_child = NULL;
271 win->first_unlinked = NULL;
272 win->thread = current;
273 win->atom = atom;
Alexandre Julliard5030bda2002-10-11 23:41:06 +0000274 win->last_active = win->handle;
Alexandre Julliardddc33172001-10-22 19:08:33 +0000275 win->style = 0;
276 win->ex_style = 0;
277 win->id = 0;
278 win->instance = NULL;
279 win->user_data = NULL;
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000280 win->text = NULL;
281 win->paint_count = 0;
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000282 win->prop_inuse = 0;
283 win->prop_alloc = 0;
284 win->properties = NULL;
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000285
286 if (parent) /* put it on parent unlinked list */
287 {
288 if ((win->next = parent->first_unlinked)) win->next->prev = win;
289 win->prev = NULL;
290 parent->first_unlinked = win;
291 }
292 else win->next = win->prev = NULL;
293
Alexandre Julliardab5063b2002-10-11 18:50:15 +0000294 /* if parent belongs to a different thread, attach the two threads */
295 if (parent && parent->thread && parent->thread != current)
296 attach_thread_input( current, parent->thread );
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000297 return win;
298}
299
300/* destroy all windows belonging to a given thread */
301void destroy_thread_windows( struct thread *thread )
302{
303 user_handle_t handle = 0;
304 struct window *win;
305
306 while ((win = next_user_handle( &handle, USER_WINDOW )))
307 {
308 if (win->thread != thread) continue;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000309 destroy_window( win );
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000310 }
311}
312
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000313/* check whether child is a descendant of parent */
314int is_child_window( user_handle_t parent, user_handle_t child )
315{
316 struct window *child_ptr = get_user_object( child, USER_WINDOW );
317 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
318
319 if (!child_ptr || !parent_ptr) return 0;
320 while (child_ptr->parent)
321 {
322 if (child_ptr->parent == parent_ptr) return 1;
323 child_ptr = child_ptr->parent;
324 }
325 return 0;
326}
327
Alexandre Julliard5030bda2002-10-11 23:41:06 +0000328/* check whether window is a top-level window */
329int is_top_level_window( user_handle_t window )
330{
331 struct window *win = get_user_object( window, USER_WINDOW );
332 return (win && win->parent == top_window);
333}
334
335/* make a window active if possible */
336int make_window_active( user_handle_t window )
337{
338 struct window *owner, *win = get_window( window );
339
340 if (!win) return 0;
341
342 /* set last active for window and its owner */
343 win->last_active = win->handle;
344 if ((owner = get_user_object( win->owner, USER_WINDOW ))) owner->last_active = win->handle;
345 return 1;
346}
347
Alexandre Julliard242e3952003-01-08 00:27:58 +0000348/* find child of 'parent' that contains the given point (in parent-relative coords) */
349static struct window *child_window_from_point( struct window *parent, int x, int y )
350{
Alexandre Julliard3783e492003-01-08 19:55:08 +0000351 struct window *ptr;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000352
Alexandre Julliard3783e492003-01-08 19:55:08 +0000353 for (ptr = parent->first_child; ptr; ptr = ptr->next)
Alexandre Julliard242e3952003-01-08 00:27:58 +0000354 {
355 if (!(ptr->style & WS_VISIBLE)) continue; /* not visible -> skip */
356 if ((ptr->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
357 continue; /* disabled child -> skip */
358 if ((ptr->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
359 continue; /* transparent -> skip */
360 if (x < ptr->window_rect.left || x >= ptr->window_rect.right ||
361 y < ptr->window_rect.top || y >= ptr->window_rect.bottom)
362 continue; /* not in window -> skip */
363
364 /* FIXME: check window region here */
365
366 /* if window is minimized or disabled, return at once */
367 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
368
369 /* if point is not in client area, return at once */
370 if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
371 y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
372 return ptr;
373
374 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
375 }
376 return parent; /* not found any child */
377}
378
379/* find window containing point (in absolute coords) */
380user_handle_t window_from_point( int x, int y )
381{
382 struct window *ret;
383
384 if (!top_window) return 0;
385 ret = child_window_from_point( top_window, x, y );
386 return ret->handle;
387}
Alexandre Julliard5030bda2002-10-11 23:41:06 +0000388
Alexandre Julliard81f2a732002-03-23 20:43:52 +0000389/* return the thread owning a window */
390struct thread *get_window_thread( user_handle_t handle )
391{
392 struct window *win = get_user_object( handle, USER_WINDOW );
393 if (!win || !win->thread) return NULL;
394 return (struct thread *)grab_object( win->thread );
395}
Alexandre Julliard47f98212001-11-14 21:28:36 +0000396
397/* find a child of the specified window that needs repainting */
398static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
399{
400 struct window *ptr, *ret = NULL;
401
402 for (ptr = parent->first_child; ptr && !ret; ptr = ptr->next)
403 {
404 if (!(ptr->style & WS_VISIBLE)) continue;
405 if (ptr->paint_count && ptr->thread == thread)
406 ret = ptr;
407 else /* explore its children */
408 ret = find_child_to_repaint( ptr, thread );
409 }
410
411 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
412 {
413 /* transparent window, check for non-transparent sibling to paint first */
414 for (ptr = ret->next; ptr; ptr = ptr->next)
415 {
416 if (!(ptr->style & WS_VISIBLE)) continue;
417 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
418 if (ptr->paint_count && ptr->thread == thread) return ptr;
419 }
420 }
421 return ret;
422}
423
424
425/* find a window that needs repainting */
426user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
427{
428 struct window *win = parent ? get_window( parent ) : top_window;
429
430 if (!win || !(win->style & WS_VISIBLE)) return 0;
431 if (!win->paint_count || win->thread != thread)
432 win = find_child_to_repaint( win, thread );
433 return win ? win->handle : 0;
434}
435
436
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000437/* create a window */
438DECL_HANDLER(create_window)
439{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000440 reply->handle = 0;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000441 if (!req->parent) /* return desktop window */
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000442 {
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000443 if (!top_window)
444 {
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000445 if (!(top_window = create_window( NULL, NULL, req->atom ))) return;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000446 top_window->thread = NULL; /* no thread owns the desktop */
Alexandre Julliard7dafa612002-09-25 00:21:56 +0000447 top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000448 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000449 reply->handle = top_window->handle;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000450 }
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000451 else
452 {
453 struct window *win, *parent, *owner = NULL;
454
455 if (!(parent = get_window( req->parent ))) return;
456 if (req->owner && !(owner = get_window( req->owner ))) return;
Alexandre Julliard4a9c8392001-11-06 17:54:15 +0000457 if (owner == top_window) owner = NULL;
Alexandre Julliard7dafa612002-09-25 00:21:56 +0000458 else if (owner && parent != top_window)
Alexandre Julliardddc33172001-10-22 19:08:33 +0000459 {
Alexandre Julliard7dafa612002-09-25 00:21:56 +0000460 /* an owned window must be created as top-level */
Alexandre Julliardddc33172001-10-22 19:08:33 +0000461 set_error( STATUS_ACCESS_DENIED );
462 return;
463 }
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000464 if (!(win = create_window( parent, owner, req->atom ))) return;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000465 reply->handle = win->handle;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000466 }
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000467}
468
469
470/* link a window into the tree */
471DECL_HANDLER(link_window)
472{
473 struct window *win, *parent = NULL, *previous = NULL;
474
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000475 if (!(win = get_window( req->handle ))) return;
476 if (req->parent && !(parent = get_window( req->parent ))) return;
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000477
478 if (win == top_window)
479 {
480 set_error( STATUS_INVALID_PARAMETER );
481 return;
482 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000483 reply->full_parent = parent ? parent->handle : 0;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000484 if (parent && req->previous)
485 {
Alexandre Julliard5af055d2001-09-24 01:13:48 +0000486 if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000487 {
Alexandre Julliard5af055d2001-09-24 01:13:48 +0000488 previous = parent->last_child;
489 if (previous == win) return; /* nothing to do */
490 }
491 else
492 {
493 if (!(previous = get_window( req->previous ))) return;
494 /* previous must be a child of parent, and not win itself */
495 if (previous->parent != parent || previous == win)
496 {
497 set_error( STATUS_INVALID_PARAMETER );
498 return;
499 }
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000500 }
501 }
502 link_window( win, parent, previous );
503}
504
505
506/* destroy a window */
507DECL_HANDLER(destroy_window)
508{
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000509 struct window *win = get_window( req->handle );
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000510 if (win)
511 {
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000512 if (win != top_window) destroy_window( win );
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000513 else set_error( STATUS_ACCESS_DENIED );
514 }
515}
516
517
Alexandre Julliardddc33172001-10-22 19:08:33 +0000518/* set a window owner */
519DECL_HANDLER(set_window_owner)
520{
521 struct window *win = get_window( req->handle );
Alexandre Julliard7dafa612002-09-25 00:21:56 +0000522 struct window *owner = NULL;
Alexandre Julliardddc33172001-10-22 19:08:33 +0000523
Alexandre Julliard7dafa612002-09-25 00:21:56 +0000524 if (!win) return;
525 if (req->owner && !(owner = get_window( req->owner ))) return;
526 if (win == top_window)
Alexandre Julliardddc33172001-10-22 19:08:33 +0000527 {
Alexandre Julliardddc33172001-10-22 19:08:33 +0000528 set_error( STATUS_ACCESS_DENIED );
529 return;
530 }
Alexandre Julliard7dafa612002-09-25 00:21:56 +0000531 reply->prev_owner = win->owner;
532 reply->full_owner = win->owner = owner ? owner->handle : 0;
Alexandre Julliardddc33172001-10-22 19:08:33 +0000533}
534
535
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000536/* get information from a window handle */
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000537DECL_HANDLER(get_window_info)
538{
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000539 struct window *win = get_window( req->handle );
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000540
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000541 reply->full_handle = 0;
542 reply->tid = reply->pid = 0;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000543 if (win)
544 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000545 reply->full_handle = win->handle;
Alexandre Julliard5030bda2002-10-11 23:41:06 +0000546 reply->last_active = win->handle;
547 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000548 if (win->thread)
549 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000550 reply->tid = get_thread_id( win->thread );
551 reply->pid = get_process_id( win->thread->process );
552 reply->atom = win->atom;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000553 }
554 }
555}
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000556
557
Alexandre Julliardddc33172001-10-22 19:08:33 +0000558/* set some information in a window */
559DECL_HANDLER(set_window_info)
560{
561 struct window *win = get_window( req->handle );
Alexandre Julliard7dafa612002-09-25 00:21:56 +0000562
Alexandre Julliardddc33172001-10-22 19:08:33 +0000563 if (!win) return;
Alexandre Julliard7dafa612002-09-25 00:21:56 +0000564 if (req->flags && win == top_window)
565 {
566 set_error( STATUS_ACCESS_DENIED );
567 return;
568 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000569 reply->old_style = win->style;
570 reply->old_ex_style = win->ex_style;
571 reply->old_id = win->id;
572 reply->old_instance = win->instance;
573 reply->old_user_data = win->user_data;
Alexandre Julliardddc33172001-10-22 19:08:33 +0000574 if (req->flags & SET_WIN_STYLE) win->style = req->style;
575 if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
576 if (req->flags & SET_WIN_ID) win->id = req->id;
577 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
578 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
579}
580
581
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000582/* get a list of the window parents, up to the root of the tree */
583DECL_HANDLER(get_window_parents)
584{
585 struct window *ptr, *win = get_window( req->handle );
586 int total = 0;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000587 user_handle_t *data;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000588 size_t len;
589
590 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
591
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000592 reply->count = total;
593 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
594 if (len && ((data = set_reply_data_size( len ))))
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000595 {
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000596 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
597 *data++ = ptr->handle;
598 }
599}
600
601
602/* get a list of the window children */
603DECL_HANDLER(get_window_children)
604{
605 struct window *ptr, *parent = get_window( req->parent );
606 int total = 0;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000607 user_handle_t *data;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000608 size_t len;
609
610 if (parent)
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000611 for (ptr = parent->first_child, total = 0; ptr; ptr = ptr->next)
612 {
613 if (req->atom && ptr->atom != req->atom) continue;
614 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
615 total++;
616 }
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000617
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000618 reply->count = total;
619 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
620 if (len && ((data = set_reply_data_size( len ))))
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000621 {
Uwe Bonnes4f7d8932002-04-11 17:32:10 +0000622 for (ptr = parent->first_child; ptr && len; ptr = ptr->next)
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000623 {
624 if (req->atom && ptr->atom != req->atom) continue;
625 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000626 *data++ = ptr->handle;
Uwe Bonnes4f7d8932002-04-11 17:32:10 +0000627 len -= sizeof(*data);
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000628 }
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000629 }
630}
631
632
633/* get window tree information from a window handle */
634DECL_HANDLER(get_window_tree)
635{
636 struct window *win = get_window( req->handle );
637
638 if (!win) return;
639
640 if (win->parent)
641 {
642 struct window *parent = win->parent;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000643 reply->parent = parent->handle;
Alexandre Julliard7dafa612002-09-25 00:21:56 +0000644 reply->owner = win->owner;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000645 reply->next_sibling = win->next ? win->next->handle : 0;
646 reply->prev_sibling = win->prev ? win->prev->handle : 0;
647 reply->first_sibling = parent->first_child ? parent->first_child->handle : 0;
648 reply->last_sibling = parent->last_child ? parent->last_child->handle : 0;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000649 }
650 else
651 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000652 reply->parent = 0;
653 reply->owner = 0;
654 reply->next_sibling = 0;
655 reply->prev_sibling = 0;
656 reply->first_sibling = 0;
657 reply->last_sibling = 0;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000658 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000659 reply->first_child = win->first_child ? win->first_child->handle : 0;
660 reply->last_child = win->last_child ? win->last_child->handle : 0;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000661}
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000662
663
Alexandre Julliard0d509652001-10-16 21:55:37 +0000664/* set the window and client rectangles of a window */
665DECL_HANDLER(set_window_rectangles)
666{
667 struct window *win = get_window( req->handle );
668
669 if (win)
670 {
671 win->window_rect = req->window;
672 win->client_rect = req->client;
673 }
674}
675
676
677/* get the window and client rectangles of a window */
678DECL_HANDLER(get_window_rectangles)
679{
680 struct window *win = get_window( req->handle );
681
682 if (win)
683 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000684 reply->window = win->window_rect;
685 reply->client = win->client_rect;
Alexandre Julliard0d509652001-10-16 21:55:37 +0000686 }
687}
688
689
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000690/* get the window text */
691DECL_HANDLER(get_window_text)
692{
693 struct window *win = get_window( req->handle );
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000694
695 if (win && win->text)
696 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000697 size_t len = strlenW( win->text ) * sizeof(WCHAR);
698 if (len > get_reply_max_size()) len = get_reply_max_size();
699 set_reply_data( win->text, len );
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000700 }
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000701}
702
703
704/* set the window text */
705DECL_HANDLER(set_window_text)
706{
707 struct window *win = get_window( req->handle );
708
709 if (win)
710 {
711 WCHAR *text = NULL;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000712 size_t len = get_req_data_size() / sizeof(WCHAR);
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000713 if (len)
714 {
715 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000716 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000717 text[len] = 0;
718 }
719 if (win->text) free( win->text );
720 win->text = text;
721 }
722}
723
724
725/* increment the window paint count */
726DECL_HANDLER(inc_window_paint_count)
727{
728 struct window *win = get_window( req->handle );
729
Alexandre Julliard47f98212001-11-14 21:28:36 +0000730 if (win && win->thread)
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000731 {
732 int old = win->paint_count;
733 if ((win->paint_count += req->incr) < 0) win->paint_count = 0;
734 inc_queue_paint_count( win->thread, win->paint_count - old );
735 }
736}
737
738
Alexandre Julliard0d509652001-10-16 21:55:37 +0000739/* get the coordinates offset between two windows */
740DECL_HANDLER(get_windows_offset)
741{
742 struct window *win;
743
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000744 reply->x = reply->y = 0;
Alexandre Julliard0d509652001-10-16 21:55:37 +0000745 if (req->from)
746 {
747 if (!(win = get_window( req->from ))) return;
748 while (win)
749 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000750 reply->x += win->client_rect.left;
751 reply->y += win->client_rect.top;
Alexandre Julliard0d509652001-10-16 21:55:37 +0000752 win = win->parent;
753 }
754 }
755 if (req->to)
756 {
757 if (!(win = get_window( req->to ))) return;
758 while (win)
759 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000760 reply->x -= win->client_rect.left;
761 reply->y -= win->client_rect.top;
Alexandre Julliard0d509652001-10-16 21:55:37 +0000762 win = win->parent;
763 }
764 }
765}
766
767
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000768/* set a window property */
769DECL_HANDLER(set_window_property)
770{
771 struct window *win = get_window( req->window );
772
773 if (win) set_property( win, req->atom, req->handle,
774 req->string ? PROP_TYPE_STRING : PROP_TYPE_ATOM );
775}
776
777
778/* remove a window property */
779DECL_HANDLER(remove_window_property)
780{
781 struct window *win = get_window( req->window );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000782 reply->handle = 0;
783 if (win) reply->handle = remove_property( win, req->atom );
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000784}
785
786
787/* get a window property */
788DECL_HANDLER(get_window_property)
789{
790 struct window *win = get_window( req->window );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000791 reply->handle = 0;
792 if (win) reply->handle = get_property( win, req->atom );
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000793}
794
795
796/* get the list of properties of a window */
797DECL_HANDLER(get_window_properties)
798{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000799 property_data_t *data;
800 int i, count, max = get_reply_max_size() / sizeof(*data);
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000801 struct window *win = get_window( req->window );
802
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000803 reply->total = 0;
804 if (!win) return;
805
806 for (i = count = 0; i < win->prop_inuse; i++)
807 if (win->properties[i].type != PROP_TYPE_FREE) count++;
808 reply->total = count;
809
810 if (count > max) count = max;
811 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
812
813 for (i = 0; i < win->prop_inuse && count; i++)
814 {
815 if (win->properties[i].type == PROP_TYPE_FREE) continue;
816 data->atom = win->properties[i].atom;
817 data->string = (win->properties[i].type == PROP_TYPE_STRING);
818 data->handle = win->properties[i].handle;
819 data++;
820 count--;
821 }
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000822}