blob: 750b2b3dc82ec9896abd18529dbe9941d53ddba7 [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 Julliard705909a2005-03-16 19:54:33 +000059 struct list children; /* list of children in Z-order */
60 struct list unlinked; /* list of children not linked in the Z-order list */
61 struct list entry; /* entry in parent's children list */
Alexandre Julliard844ceb92001-10-09 23:26:40 +000062 user_handle_t handle; /* full handle for this window */
63 struct thread *thread; /* thread owning the window */
Alexandre Julliardbfce1512003-12-10 04:08:06 +000064 struct window_class *class; /* window class */
Alexandre Julliard7a2017d2001-10-12 19:10:26 +000065 atom_t atom; /* class atom */
Alexandre Julliard5030bda2002-10-11 23:41:06 +000066 user_handle_t last_active; /* last active popup */
Alexandre Julliard4616dcb2004-07-20 22:17:38 +000067 rectangle_t window_rect; /* window rectangle (relative to parent client area) */
Alexandre Julliardf7560902005-03-17 19:10:41 +000068 rectangle_t visible_rect; /* visible part of window rect (relative to parent client area) */
Alexandre Julliard4616dcb2004-07-20 22:17:38 +000069 rectangle_t client_rect; /* client rectangle (relative to parent client area) */
70 struct region *win_region; /* region for shaped windows (relative to window rect) */
Alexandre Julliard5defa492004-12-07 17:31:53 +000071 struct region *update_region; /* update region (relative to window rect) */
Alexandre Julliardddc33172001-10-22 19:08:33 +000072 unsigned int style; /* window style */
73 unsigned int ex_style; /* window extended style */
74 unsigned int id; /* window id */
75 void* instance; /* creator instance */
76 void* user_data; /* user-specific data */
Alexandre Julliard805bdc52001-11-13 22:23:48 +000077 WCHAR *text; /* window caption text */
Alexandre Julliard5defa492004-12-07 17:31:53 +000078 unsigned int paint_flags; /* various painting flags */
Alexandre Julliard7a2017d2001-10-12 19:10:26 +000079 int prop_inuse; /* number of in-use window properties */
80 int prop_alloc; /* number of allocated window properties */
81 struct property *properties; /* window properties array */
Alexandre Julliard97903d22003-11-26 22:15:41 +000082 int nb_extra_bytes; /* number of extra bytes */
83 char extra_bytes[1]; /* extra bytes storage */
Alexandre Julliard1a66d222001-08-28 18:44:52 +000084};
85
Alexandre Julliard5defa492004-12-07 17:31:53 +000086#define PAINT_INTERNAL 0x01 /* internal WM_PAINT pending */
87#define PAINT_ERASE 0x02 /* needs WM_ERASEBKGND */
88#define PAINT_NONCLIENT 0x04 /* needs WM_NCPAINT */
89
Alexandre Julliard4616dcb2004-07-20 22:17:38 +000090/* growable array of user handles */
91struct user_handle_array
92{
93 user_handle_t *handles;
94 int count;
95 int total;
96};
97
Alexandre Julliard1a66d222001-08-28 18:44:52 +000098static struct window *top_window; /* top-level (desktop) window */
99
Alexandre Julliard8d174d32003-10-07 03:40:23 +0000100/* global window pointers */
101static struct window *shell_window;
102static struct window *shell_listview;
103static struct window *progman_window;
104static struct window *taskman_window;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000105
106/* retrieve a pointer to a window from its handle */
107inline static struct window *get_window( user_handle_t handle )
108{
109 struct window *ret = get_user_object( handle, USER_WINDOW );
110 if (!ret) set_error( STATUS_INVALID_HANDLE );
111 return ret;
112}
113
Alexandre Julliard4d32a472005-03-25 10:38:56 +0000114/* change the parent of a window (or unlink the window if the new parent is NULL) */
115static int set_parent_window( struct window *win, struct window *parent )
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000116{
Alexandre Julliard4d32a472005-03-25 10:38:56 +0000117 struct window *ptr;
118
119 /* make sure parent is not a child of window */
120 for (ptr = parent; ptr; ptr = ptr->parent)
121 {
122 if (ptr == win)
123 {
124 set_error( STATUS_INVALID_PARAMETER );
125 return 0;
126 }
127 }
128
Alexandre Julliard705909a2005-03-16 19:54:33 +0000129 list_remove( &win->entry ); /* unlink it from the previous location */
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000130
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000131 if (parent)
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000132 {
Alexandre Julliard7dafa612002-09-25 00:21:56 +0000133 win->parent = parent;
Alexandre Julliard4d32a472005-03-25 10:38:56 +0000134 list_add_head( &parent->children, &win->entry );
135
136 /* if parent belongs to a different thread, attach the two threads */
137 if (parent->thread && parent->thread != win->thread)
138 attach_thread_input( win->thread, parent->thread );
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000139 }
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000140 else /* move it to parent unlinked list */
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000141 {
Alexandre Julliard705909a2005-03-16 19:54:33 +0000142 list_add_head( &win->parent->unlinked, &win->entry );
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000143 }
Alexandre Julliard4d32a472005-03-25 10:38:56 +0000144 return 1;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000145}
146
Alexandre Julliard705909a2005-03-16 19:54:33 +0000147/* get next window in Z-order list */
148static inline struct window *get_next_window( struct window *win )
149{
150 struct list *ptr = list_next( &win->parent->children, &win->entry );
151 if (ptr == &win->parent->unlinked) ptr = NULL;
152 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
153}
154
155/* get previous window in Z-order list */
156static inline struct window *get_prev_window( struct window *win )
157{
158 struct list *ptr = list_prev( &win->parent->children, &win->entry );
159 if (ptr == &win->parent->unlinked) ptr = NULL;
160 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
161}
162
163/* get first child in Z-order list */
164static inline struct window *get_first_child( struct window *win )
165{
166 struct list *ptr = list_head( &win->children );
167 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
168}
169
170/* get last child in Z-order list */
171static inline struct window *get_last_child( struct window *win )
172{
173 struct list *ptr = list_tail( &win->children );
174 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
175}
176
Alexandre Julliard4616dcb2004-07-20 22:17:38 +0000177/* append a user handle to a handle array */
178static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
179{
180 if (array->count >= array->total)
181 {
182 int new_total = max( array->total * 2, 32 );
183 user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
184 if (!new_array)
185 {
186 free( array->handles );
187 set_error( STATUS_NO_MEMORY );
188 return 0;
189 }
190 array->handles = new_array;
191 array->total = new_total;
192 }
193 array->handles[array->count++] = handle;
194 return 1;
195}
196
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000197/* set a window property */
Alexandre Julliard51885742002-05-30 20:12:58 +0000198static void set_property( struct window *win, atom_t atom, obj_handle_t handle,
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000199 enum property_type type )
200{
201 int i, free = -1;
202 struct property *new_props;
203
204 /* check if it exists already */
205 for (i = 0; i < win->prop_inuse; i++)
206 {
207 if (win->properties[i].type == PROP_TYPE_FREE)
208 {
209 free = i;
210 continue;
211 }
212 if (win->properties[i].atom == atom)
213 {
214 win->properties[i].type = type;
215 win->properties[i].handle = handle;
216 return;
217 }
218 }
219
220 /* need to add an entry */
221 if (!grab_global_atom( atom )) return;
222 if (free == -1)
223 {
224 /* no free entry */
225 if (win->prop_inuse >= win->prop_alloc)
226 {
227 /* need to grow the array */
228 if (!(new_props = realloc( win->properties,
229 sizeof(*new_props) * (win->prop_alloc + 16) )))
230 {
231 set_error( STATUS_NO_MEMORY );
232 release_global_atom( atom );
233 return;
234 }
235 win->prop_alloc += 16;
236 win->properties = new_props;
237 }
238 free = win->prop_inuse++;
239 }
240 win->properties[free].atom = atom;
241 win->properties[free].type = type;
242 win->properties[free].handle = handle;
243}
244
245/* remove a window property */
Alexandre Julliard51885742002-05-30 20:12:58 +0000246static obj_handle_t remove_property( struct window *win, atom_t atom )
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000247{
248 int i;
249
250 for (i = 0; i < win->prop_inuse; i++)
251 {
252 if (win->properties[i].type == PROP_TYPE_FREE) continue;
253 if (win->properties[i].atom == atom)
254 {
255 release_global_atom( atom );
256 win->properties[i].type = PROP_TYPE_FREE;
257 return win->properties[i].handle;
258 }
259 }
260 /* FIXME: last error? */
261 return 0;
262}
263
264/* find a window property */
Alexandre Julliard51885742002-05-30 20:12:58 +0000265static obj_handle_t get_property( struct window *win, atom_t atom )
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000266{
267 int i;
268
269 for (i = 0; i < win->prop_inuse; i++)
270 {
271 if (win->properties[i].type == PROP_TYPE_FREE) continue;
272 if (win->properties[i].atom == atom) return win->properties[i].handle;
273 }
274 /* FIXME: last error? */
275 return 0;
276}
277
278/* destroy all properties of a window */
279inline static void destroy_properties( struct window *win )
280{
281 int i;
282
283 if (!win->properties) return;
284 for (i = 0; i < win->prop_inuse; i++)
285 {
286 if (win->properties[i].type == PROP_TYPE_FREE) continue;
287 release_global_atom( win->properties[i].atom );
288 }
289 free( win->properties );
290}
291
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000292/* destroy a window */
293static void destroy_window( struct window *win )
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000294{
295 assert( win != top_window );
296
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000297 /* destroy all children */
Alexandre Julliard705909a2005-03-16 19:54:33 +0000298 while (!list_empty(&win->children))
299 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
300 while (!list_empty(&win->unlinked))
301 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000302
Alexandre Julliarde806c972001-11-24 03:44:47 +0000303 if (win->thread->queue)
304 {
Alexandre Julliard5defa492004-12-07 17:31:53 +0000305 if (win->update_region) inc_queue_paint_count( win->thread, -1 );
306 if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( win->thread, -1 );
Alexandre Julliarde806c972001-11-24 03:44:47 +0000307 queue_cleanup_window( win->thread, win->handle );
308 }
Alexandre Julliard8d174d32003-10-07 03:40:23 +0000309 /* reset global window pointers, if the corresponding window is destroyed */
310 if (win == shell_window) shell_window = NULL;
311 if (win == shell_listview) shell_listview = NULL;
312 if (win == progman_window) progman_window = NULL;
313 if (win == taskman_window) taskman_window = NULL;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000314 free_user_handle( win->handle );
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000315 destroy_properties( win );
Alexandre Julliard705909a2005-03-16 19:54:33 +0000316 list_remove( &win->entry );
Alexandre Julliard618a7e52004-06-29 03:53:25 +0000317 if (win->win_region) free_region( win->win_region );
Alexandre Julliard5defa492004-12-07 17:31:53 +0000318 if (win->update_region) free_region( win->update_region );
Alexandre Julliardbfce1512003-12-10 04:08:06 +0000319 release_class( win->class );
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000320 if (win->text) free( win->text );
Robert Shearman04995ce2005-02-08 12:07:48 +0000321 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000322 free( win );
323}
324
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000325/* create a new window structure (note: the window is not linked in the window tree) */
Alexandre Julliardbd13ab82003-12-11 05:34:53 +0000326static struct window *create_window( struct window *parent, struct window *owner,
327 atom_t atom, void *instance )
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000328{
Alexandre Julliardbd13ab82003-12-11 05:34:53 +0000329 int extra_bytes;
Alexandre Julliardbfce1512003-12-10 04:08:06 +0000330 struct window *win;
Alexandre Julliardbd13ab82003-12-11 05:34:53 +0000331 struct window_class *class = grab_class( current->process, atom, instance, &extra_bytes );
Alexandre Julliardbfce1512003-12-10 04:08:06 +0000332
333 if (!class) return NULL;
334
335 win = mem_alloc( sizeof(*win) + extra_bytes - 1 );
336 if (!win)
337 {
338 release_class( class );
339 return NULL;
340 }
Alexandre Julliardd70a2532005-04-26 14:31:33 +0000341 if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000342
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000343 win->parent = parent;
Alexandre Julliard7dafa612002-09-25 00:21:56 +0000344 win->owner = owner ? owner->handle : 0;
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000345 win->thread = current;
Alexandre Julliardbfce1512003-12-10 04:08:06 +0000346 win->class = class;
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000347 win->atom = atom;
Alexandre Julliard5030bda2002-10-11 23:41:06 +0000348 win->last_active = win->handle;
Alexandre Julliard618a7e52004-06-29 03:53:25 +0000349 win->win_region = NULL;
Alexandre Julliard5defa492004-12-07 17:31:53 +0000350 win->update_region = NULL;
Alexandre Julliardddc33172001-10-22 19:08:33 +0000351 win->style = 0;
352 win->ex_style = 0;
353 win->id = 0;
354 win->instance = NULL;
355 win->user_data = NULL;
Alexandre Julliard805bdc52001-11-13 22:23:48 +0000356 win->text = NULL;
Alexandre Julliard5defa492004-12-07 17:31:53 +0000357 win->paint_flags = 0;
Alexandre Julliard7a2017d2001-10-12 19:10:26 +0000358 win->prop_inuse = 0;
359 win->prop_alloc = 0;
360 win->properties = NULL;
Alexandre Julliard97903d22003-11-26 22:15:41 +0000361 win->nb_extra_bytes = extra_bytes;
362 memset( win->extra_bytes, 0, extra_bytes );
Alexandre Julliard705909a2005-03-16 19:54:33 +0000363 list_init( &win->children );
364 list_init( &win->unlinked );
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000365
Alexandre Julliardd70a2532005-04-26 14:31:33 +0000366 /* if parent belongs to a different thread, attach the two threads */
367 if (parent && parent->thread && parent->thread != current)
368 {
369 if (!attach_thread_input( current, parent->thread )) goto failed;
370 }
371 else /* otherwise just make sure that the thread has a message queue */
372 {
373 if (!current->queue && !init_thread_queue( current )) goto failed;
374 }
375
Alexandre Julliard705909a2005-03-16 19:54:33 +0000376 /* put it on parent unlinked list */
377 if (parent) list_add_head( &parent->unlinked, &win->entry );
Alexandre Julliard844ceb92001-10-09 23:26:40 +0000378
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000379 return win;
Alexandre Julliardd70a2532005-04-26 14:31:33 +0000380
381failed:
382 if (win->handle) free_user_handle( win->handle );
383 release_class( class );
384 free( win );
385 return NULL;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000386}
387
388/* destroy all windows belonging to a given thread */
389void destroy_thread_windows( struct thread *thread )
390{
391 user_handle_t handle = 0;
392 struct window *win;
393
394 while ((win = next_user_handle( &handle, USER_WINDOW )))
395 {
396 if (win->thread != thread) continue;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000397 destroy_window( win );
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000398 }
399}
400
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000401/* check whether child is a descendant of parent */
402int is_child_window( user_handle_t parent, user_handle_t child )
403{
404 struct window *child_ptr = get_user_object( child, USER_WINDOW );
405 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
406
407 if (!child_ptr || !parent_ptr) return 0;
408 while (child_ptr->parent)
409 {
410 if (child_ptr->parent == parent_ptr) return 1;
411 child_ptr = child_ptr->parent;
412 }
413 return 0;
414}
415
Alexandre Julliard5030bda2002-10-11 23:41:06 +0000416/* check whether window is a top-level window */
417int is_top_level_window( user_handle_t window )
418{
419 struct window *win = get_user_object( window, USER_WINDOW );
420 return (win && win->parent == top_window);
421}
422
423/* make a window active if possible */
424int make_window_active( user_handle_t window )
425{
426 struct window *owner, *win = get_window( window );
427
428 if (!win) return 0;
429
430 /* set last active for window and its owner */
431 win->last_active = win->handle;
432 if ((owner = get_user_object( win->owner, USER_WINDOW ))) owner->last_active = win->handle;
433 return 1;
434}
435
Alexandre Julliard5defa492004-12-07 17:31:53 +0000436/* increment (or decrement) the window paint count */
437static inline void inc_window_paint_count( struct window *win, int incr )
438{
439 if (win->thread) inc_queue_paint_count( win->thread, incr );
440}
441
Alexandre Julliard085ef062004-10-27 21:54:41 +0000442/* check if window and all its ancestors are visible */
443static int is_visible( const struct window *win )
444{
445 while (win && win != top_window)
446 {
447 if (!(win->style & WS_VISIBLE)) return 0;
448 win = win->parent;
449 /* if parent is minimized children are not visible */
450 if (win && (win->style & WS_MINIMIZE)) return 0;
451 }
452 return 1;
453}
454
455/* same as is_visible but takes a window handle */
456int is_window_visible( user_handle_t window )
457{
458 struct window *win = get_user_object( window, USER_WINDOW );
459 if (!win) return 0;
460 return is_visible( win );
461}
462
Alexandre Julliard4616dcb2004-07-20 22:17:38 +0000463/* check if point is inside the window */
464static inline int is_point_in_window( struct window *win, int x, int y )
465{
466 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
467 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
468 return 0; /* disabled child */
469 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
470 return 0; /* transparent */
Alexandre Julliardf7560902005-03-17 19:10:41 +0000471 if (x < win->visible_rect.left || x >= win->visible_rect.right ||
472 y < win->visible_rect.top || y >= win->visible_rect.bottom)
Alexandre Julliard4616dcb2004-07-20 22:17:38 +0000473 return 0; /* not in window */
474 if (win->win_region &&
475 !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
476 return 0; /* not in window region */
477 return 1;
478}
479
Alexandre Julliard242e3952003-01-08 00:27:58 +0000480/* find child of 'parent' that contains the given point (in parent-relative coords) */
481static struct window *child_window_from_point( struct window *parent, int x, int y )
482{
Alexandre Julliard3783e492003-01-08 19:55:08 +0000483 struct window *ptr;
Alexandre Julliard242e3952003-01-08 00:27:58 +0000484
Alexandre Julliard705909a2005-03-16 19:54:33 +0000485 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
Alexandre Julliard242e3952003-01-08 00:27:58 +0000486 {
Alexandre Julliard4616dcb2004-07-20 22:17:38 +0000487 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
Alexandre Julliard242e3952003-01-08 00:27:58 +0000488
489 /* if window is minimized or disabled, return at once */
490 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
491
492 /* if point is not in client area, return at once */
493 if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
494 y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
495 return ptr;
496
497 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
498 }
499 return parent; /* not found any child */
500}
501
Alexandre Julliard4616dcb2004-07-20 22:17:38 +0000502/* find all children of 'parent' that contain the given point */
503static int get_window_children_from_point( struct window *parent, int x, int y,
504 struct user_handle_array *array )
505{
506 struct window *ptr;
507
Alexandre Julliard705909a2005-03-16 19:54:33 +0000508 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
Alexandre Julliard4616dcb2004-07-20 22:17:38 +0000509 {
510 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
511
512 /* if point is in client area, and window is not minimized or disabled, check children */
513 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) &&
514 x >= ptr->client_rect.left && x < ptr->client_rect.right &&
515 y >= ptr->client_rect.top && y < ptr->client_rect.bottom)
516 {
517 if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
518 y - ptr->client_rect.top, array ))
519 return 0;
520 }
521
522 /* now add window to the array */
523 if (!add_handle_to_array( array, ptr->handle )) return 0;
524 }
525 return 1;
526}
527
Alexandre Julliard242e3952003-01-08 00:27:58 +0000528/* find window containing point (in absolute coords) */
529user_handle_t window_from_point( int x, int y )
530{
531 struct window *ret;
532
533 if (!top_window) return 0;
534 ret = child_window_from_point( top_window, x, y );
535 return ret->handle;
536}
Alexandre Julliard5030bda2002-10-11 23:41:06 +0000537
Alexandre Julliard4616dcb2004-07-20 22:17:38 +0000538/* return list of all windows containing point (in absolute coords) */
539static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
540{
541 struct window *ptr;
542
543 /* make point relative to top window */
544 for (ptr = top->parent; ptr && ptr != top_window; ptr = ptr->parent)
545 {
546 x -= ptr->client_rect.left;
547 y -= ptr->client_rect.top;
548 }
549
550 if (!is_point_in_window( top, x, y )) return 1;
551
552 /* if point is in client area, and window is not minimized or disabled, check children */
553 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) &&
554 x >= top->client_rect.left && x < top->client_rect.right &&
555 y >= top->client_rect.top && y < top->client_rect.bottom)
556 {
557 if (!get_window_children_from_point( top, x - top->client_rect.left,
558 y - top->client_rect.top, array ))
559 return 0;
560 }
561 /* now add window to the array */
562 if (!add_handle_to_array( array, top->handle )) return 0;
563 return 1;
564}
565
566
Alexandre Julliard81f2a732002-03-23 20:43:52 +0000567/* return the thread owning a window */
568struct thread *get_window_thread( user_handle_t handle )
569{
570 struct window *win = get_user_object( handle, USER_WINDOW );
571 if (!win || !win->thread) return NULL;
572 return (struct thread *)grab_object( win->thread );
573}
Alexandre Julliard47f98212001-11-14 21:28:36 +0000574
Alexandre Julliard5defa492004-12-07 17:31:53 +0000575
576/* check if any area of a window needs repainting */
577static inline int win_needs_repaint( struct window *win )
578{
579 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
580}
581
582
Alexandre Julliard47f98212001-11-14 21:28:36 +0000583/* find a child of the specified window that needs repainting */
584static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
585{
586 struct window *ptr, *ret = NULL;
587
Alexandre Julliard705909a2005-03-16 19:54:33 +0000588 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
Alexandre Julliard47f98212001-11-14 21:28:36 +0000589 {
590 if (!(ptr->style & WS_VISIBLE)) continue;
Alexandre Julliard5defa492004-12-07 17:31:53 +0000591 if (ptr->thread == thread && win_needs_repaint( ptr ))
Alexandre Julliard47f98212001-11-14 21:28:36 +0000592 ret = ptr;
Alexandre Julliard5defa492004-12-07 17:31:53 +0000593 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
Alexandre Julliard47f98212001-11-14 21:28:36 +0000594 ret = find_child_to_repaint( ptr, thread );
Alexandre Julliard705909a2005-03-16 19:54:33 +0000595 if (ret) break;
Alexandre Julliard47f98212001-11-14 21:28:36 +0000596 }
597
598 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
599 {
600 /* transparent window, check for non-transparent sibling to paint first */
Alexandre Julliard705909a2005-03-16 19:54:33 +0000601 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
Alexandre Julliard47f98212001-11-14 21:28:36 +0000602 {
603 if (!(ptr->style & WS_VISIBLE)) continue;
604 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
Alexandre Julliard5defa492004-12-07 17:31:53 +0000605 if (ptr->thread != thread) continue;
606 if (win_needs_repaint( ptr )) return ptr;
Alexandre Julliard47f98212001-11-14 21:28:36 +0000607 }
608 }
609 return ret;
610}
611
612
Alexandre Julliardb9a9de62005-03-10 17:19:33 +0000613/* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
Alexandre Julliard47f98212001-11-14 21:28:36 +0000614user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
615{
Alexandre Julliard5defa492004-12-07 17:31:53 +0000616 struct window *ptr, *win = find_child_to_repaint( top_window, thread );
Alexandre Julliard47f98212001-11-14 21:28:36 +0000617
Alexandre Julliardb9a9de62005-03-10 17:19:33 +0000618 if (win && parent)
Alexandre Julliard5defa492004-12-07 17:31:53 +0000619 {
Alexandre Julliard5defa492004-12-07 17:31:53 +0000620 /* check that it is a child of the specified parent */
621 for (ptr = win; ptr; ptr = ptr->parent)
Alexandre Julliardb9a9de62005-03-10 17:19:33 +0000622 if (ptr->handle == parent) break;
Alexandre Julliard5defa492004-12-07 17:31:53 +0000623 /* otherwise don't return any window, we don't repaint a child before its parent */
Alexandre Julliardb9a9de62005-03-10 17:19:33 +0000624 if (!ptr) win = NULL;
Alexandre Julliard5defa492004-12-07 17:31:53 +0000625 }
Alexandre Julliardb9a9de62005-03-10 17:19:33 +0000626 if (!win) return 0;
627 win->paint_flags &= ~PAINT_INTERNAL;
628 return win->handle;
Alexandre Julliard47f98212001-11-14 21:28:36 +0000629}
630
631
Alexandre Julliard618a7e52004-06-29 03:53:25 +0000632/* intersect the window region with the specified region, relative to the window parent */
633static struct region *intersect_window_region( struct region *region, struct window *win )
634{
635 /* make region relative to window rect */
636 offset_region( region, -win->window_rect.left, -win->window_rect.top );
637 if (!intersect_region( region, region, win->win_region )) return NULL;
638 /* make region relative to parent again */
639 offset_region( region, win->window_rect.left, win->window_rect.top );
640 return region;
641}
642
643
Alexandre Julliardbc75f2f2005-03-31 15:36:57 +0000644/* convert coordinates from client to screen coords */
645static inline void client_to_screen( struct window *win, int *x, int *y )
646{
647 for ( ; win; win = win->parent)
648 {
649 *x += win->client_rect.left;
650 *y += win->client_rect.top;
651 }
652}
653
Alexandre Julliardeea70692005-03-30 17:11:46 +0000654/* map the region from window to screen coordinates */
655static inline void map_win_region_to_screen( struct window *win, struct region *region )
656{
657 int x = win->window_rect.left;
658 int y = win->window_rect.top;
Alexandre Julliardbc75f2f2005-03-31 15:36:57 +0000659 client_to_screen( win->parent, &x, &y );
Alexandre Julliardeea70692005-03-30 17:11:46 +0000660 offset_region( region, x, y );
661}
662
663
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000664/* clip all children of a given window out of the visible region */
665static struct region *clip_children( struct window *parent, struct window *last,
666 struct region *region, int offset_x, int offset_y )
667{
668 struct window *ptr;
669 struct region *tmp = create_empty_region();
670
671 if (!tmp) return NULL;
Alexandre Julliard705909a2005-03-16 19:54:33 +0000672 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000673 {
Alexandre Julliard705909a2005-03-16 19:54:33 +0000674 if (ptr == last) break;
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000675 if (!(ptr->style & WS_VISIBLE)) continue;
676 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
Alexandre Julliardf7560902005-03-17 19:10:41 +0000677 set_region_rect( tmp, &ptr->visible_rect );
Alexandre Julliard618a7e52004-06-29 03:53:25 +0000678 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
679 {
680 free_region( tmp );
681 return NULL;
682 }
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000683 offset_region( tmp, offset_x, offset_y );
684 if (!(region = subtract_region( region, region, tmp ))) break;
685 if (is_region_empty( region )) break;
686 }
687 free_region( tmp );
688 return region;
689}
690
691
Alexandre Julliard5054c792005-03-21 12:37:00 +0000692/* compute the intersection of two rectangles; return 0 if the result is empty */
693static inline int intersect_rect( rectangle_t *dst, const rectangle_t *src1, const rectangle_t *src2 )
694{
695 dst->left = max( src1->left, src2->left );
696 dst->top = max( src1->top, src2->top );
697 dst->right = min( src1->right, src2->right );
698 dst->bottom = min( src1->bottom, src2->bottom );
699 return (dst->left < dst->right && dst->top < dst->bottom);
700}
701
702
Alexandre Julliard548c9732005-02-22 19:41:43 +0000703/* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
704static void set_region_client_rect( struct region *region, struct window *win )
705{
706 rectangle_t rect;
707
Alexandre Julliard5054c792005-03-21 12:37:00 +0000708 intersect_rect( &rect, &win->window_rect, &win->client_rect );
Alexandre Julliard548c9732005-02-22 19:41:43 +0000709 set_region_rect( region, &rect );
710}
711
712
Alexandre Julliard4e47afb2005-03-17 14:02:06 +0000713/* get the top-level window to clip against for a given window */
714static inline struct window *get_top_clipping_window( struct window *win )
715{
716 while (win->parent && win->parent != top_window) win = win->parent;
717 return win;
718}
719
720
Alexandre Julliardeea70692005-03-30 17:11:46 +0000721/* compute the visible region of a window, in window coordinates */
Alexandre Julliardbc75f2f2005-03-31 15:36:57 +0000722static struct region *get_visible_region( struct window *win, struct window *top, unsigned int flags )
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000723{
Raphael Junqueira962a5e72005-04-11 12:54:53 +0000724 struct region *tmp = NULL, *region;
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000725 int offset_x, offset_y;
726
727 if (!(region = create_empty_region())) return NULL;
728
729 /* first check if all ancestors are visible */
730
Alexandre Julliard085ef062004-10-27 21:54:41 +0000731 if (!is_visible( win )) return region; /* empty region */
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000732
Alexandre Julliard618a7e52004-06-29 03:53:25 +0000733 /* create a region relative to the window itself */
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000734
Alexandre Julliard3365eb72004-07-06 23:54:49 +0000735 if ((flags & DCX_PARENTCLIP) && win != top && win->parent)
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000736 {
Alexandre Julliard548c9732005-02-22 19:41:43 +0000737 set_region_client_rect( region, win->parent );
738 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000739 }
740 else if (flags & DCX_WINDOW)
741 {
Alexandre Julliardf7560902005-03-17 19:10:41 +0000742 set_region_rect( region, &win->visible_rect );
Alexandre Julliard618a7e52004-06-29 03:53:25 +0000743 if (win->win_region && !intersect_window_region( region, win )) goto error;
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000744 }
745 else
746 {
Alexandre Julliard548c9732005-02-22 19:41:43 +0000747 set_region_client_rect( region, win );
Alexandre Julliard618a7e52004-06-29 03:53:25 +0000748 if (win->win_region && !intersect_window_region( region, win )) goto error;
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000749 }
Alexandre Julliardeea70692005-03-30 17:11:46 +0000750 offset_x = win->window_rect.left;
751 offset_y = win->window_rect.top;
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000752
753 /* clip children */
754
755 if (flags & DCX_CLIPCHILDREN)
756 {
Alexandre Julliardeea70692005-03-30 17:11:46 +0000757 if (!clip_children( win, NULL, region, win->client_rect.left, win->client_rect.top ))
758 goto error;
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000759 }
760
761 /* clip siblings of ancestors */
762
763 if (top && top != win && (tmp = create_empty_region()) != NULL)
764 {
Alexandre Julliard3365eb72004-07-06 23:54:49 +0000765 while (win != top && win->parent)
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000766 {
767 if (win->style & WS_CLIPSIBLINGS)
768 {
769 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
770 if (is_region_empty( region )) break;
771 }
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000772 /* clip to parent client area */
773 win = win->parent;
774 offset_x += win->client_rect.left;
775 offset_y += win->client_rect.top;
776 offset_region( region, win->client_rect.left, win->client_rect.top );
Alexandre Julliard548c9732005-02-22 19:41:43 +0000777 set_region_client_rect( tmp, win );
Raphael Junqueira962a5e72005-04-11 12:54:53 +0000778 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
779 if (!intersect_region( region, region, tmp )) goto error;
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000780 if (is_region_empty( region )) break;
781 }
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000782 free_region( tmp );
783 }
Alexandre Julliardeea70692005-03-30 17:11:46 +0000784 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000785 return region;
786
787error:
Raphael Junqueira962a5e72005-04-11 12:54:53 +0000788 if (tmp) free_region( tmp );
Alexandre Julliarde8d86b72004-06-23 20:44:58 +0000789 free_region( region );
790 return NULL;
791}
792
793
Alexandre Julliardbfce1512003-12-10 04:08:06 +0000794/* get the window class of a window */
795struct window_class* get_window_class( user_handle_t window )
796{
797 struct window *win;
798 if (!(win = get_window( window ))) return NULL;
799 return win->class;
800}
801
Alexandre Julliard5defa492004-12-07 17:31:53 +0000802/* return a copy of the specified region cropped to the window client or frame rectangle, */
803/* and converted from client to window coordinates. Helper for (in)validate_window. */
804static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
805{
Alexandre Julliard5defa492004-12-07 17:31:53 +0000806 struct region *tmp = create_empty_region();
807
808 if (!tmp) return NULL;
809
810 /* get bounding rect in client coords */
Alexandre Julliard548c9732005-02-22 19:41:43 +0000811 if (frame) set_region_rect( tmp, &win->window_rect );
812 else set_region_client_rect( tmp, win );
813 offset_region( tmp, -win->client_rect.left, -win->client_rect.top );
Alexandre Julliard5defa492004-12-07 17:31:53 +0000814
815 /* intersect specified region with bounding rect */
816 if (region && !intersect_region( tmp, region, tmp )) goto done;
817 if (is_region_empty( tmp )) goto done;
818
819 /* map it to window coords */
820 offset_region( tmp, win->client_rect.left - win->window_rect.left,
821 win->client_rect.top - win->window_rect.top );
822 return tmp;
823
824done:
825 free_region( tmp );
826 return NULL;
827}
828
829
830/* set a region as new update region for the window */
831static void set_update_region( struct window *win, struct region *region )
832{
833 if (region && !is_region_empty( region ))
834 {
835 if (!win->update_region) inc_window_paint_count( win, 1 );
836 else free_region( win->update_region );
837 win->update_region = region;
838 }
839 else
840 {
Rob Shearmanb4b7f052005-05-23 09:53:06 +0000841 if (win->update_region)
842 {
843 inc_window_paint_count( win, -1 );
844 free_region( win->update_region );
845 }
Alexandre Julliard5defa492004-12-07 17:31:53 +0000846 win->paint_flags &= ~(PAINT_ERASE | PAINT_NONCLIENT);
847 win->update_region = NULL;
848 if (region) free_region( region );
849 }
850}
851
852
853/* add a region to the update region; the passed region is freed or reused */
854static int add_update_region( struct window *win, struct region *region )
855{
856 if (win->update_region && !union_region( region, win->update_region, region ))
857 {
858 free_region( region );
859 return 0;
860 }
861 set_update_region( win, region );
862 return 1;
863}
864
865
866/* validate the non client area of a window */
867static void validate_non_client( struct window *win )
868{
869 struct region *tmp;
870 rectangle_t rect;
871
872 if (!win->update_region) return; /* nothing to do */
873
874 /* get client rect in window coords */
875 rect.left = win->client_rect.left - win->window_rect.left;
876 rect.top = win->client_rect.top - win->window_rect.top;
877 rect.right = win->client_rect.right - win->window_rect.left;
878 rect.bottom = win->client_rect.bottom - win->window_rect.top;
879
Alexandre Julliard548c9732005-02-22 19:41:43 +0000880 if ((tmp = create_empty_region()))
Alexandre Julliard5defa492004-12-07 17:31:53 +0000881 {
Alexandre Julliard548c9732005-02-22 19:41:43 +0000882 set_region_rect( tmp, &rect );
Alexandre Julliard5defa492004-12-07 17:31:53 +0000883 if (intersect_region( tmp, win->update_region, tmp ))
884 set_update_region( win, tmp );
885 else
886 free_region( tmp );
887 }
888 win->paint_flags &= ~PAINT_NONCLIENT;
889}
890
891
892/* validate a window completely so that we don't get any further paint messages for it */
893static void validate_whole_window( struct window *win )
894{
895 set_update_region( win, NULL );
896
897 if (win->paint_flags & PAINT_INTERNAL)
898 {
899 win->paint_flags &= ~PAINT_INTERNAL;
900 inc_window_paint_count( win, -1 );
901 }
902}
903
904
905/* validate the update region of a window on all parents; helper for redraw_window */
906static void validate_parents( struct window *child )
907{
908 int offset_x = 0, offset_y = 0;
909 struct window *win = child;
910 struct region *tmp = NULL;
911
912 if (!child->update_region) return;
913
914 while (win->parent && win->parent != top_window)
915 {
916 /* map to parent client coords */
917 offset_x += win->window_rect.left;
918 offset_y += win->window_rect.top;
919
920 win = win->parent;
921
922 /* and now map to window coords */
923 offset_x += win->client_rect.left - win->window_rect.left;
924 offset_y += win->client_rect.top - win->window_rect.top;
925
926 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
927 {
928 if (!tmp && !(tmp = create_empty_region())) return;
929 offset_region( child->update_region, offset_x, offset_y );
930 if (subtract_region( tmp, win->update_region, child->update_region ))
931 {
932 set_update_region( win, tmp );
933 tmp = NULL;
934 }
935 /* restore child coords */
936 offset_region( child->update_region, -offset_x, -offset_y );
937 }
938 }
939 if (tmp) free_region( tmp );
940}
941
942
943/* add/subtract a region (in client coordinates) to the update region of the window */
944static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
945{
946 struct region *tmp;
947 struct window *child;
948
949 if (flags & RDW_INVALIDATE)
950 {
951 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
952
953 if (!add_update_region( win, tmp )) return;
954
955 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
956 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
957 }
958 else if (flags & RDW_VALIDATE)
959 {
960 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
961 {
962 set_update_region( win, NULL );
963 }
964 else if (win->update_region)
965 {
966 if ((tmp = crop_region_to_win_rect( win, region, frame )))
967 {
968 if (!subtract_region( tmp, win->update_region, tmp ))
969 {
970 free_region( tmp );
971 return;
972 }
973 set_update_region( win, tmp );
974 }
975 if (flags & RDW_NOFRAME) validate_non_client( win );
976 if (flags & RDW_NOERASE) win->paint_flags &= ~PAINT_ERASE;
977 }
978 }
979
980 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
981 {
982 win->paint_flags |= PAINT_INTERNAL;
983 inc_window_paint_count( win, 1 );
984 }
985 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
986 {
987 win->paint_flags &= ~PAINT_INTERNAL;
988 inc_window_paint_count( win, -1 );
989 }
990
991 if (flags & RDW_UPDATENOW)
992 {
993 validate_parents( win );
994 flags &= ~RDW_UPDATENOW;
995 }
996
997 /* now process children recursively */
998
999 if (flags & RDW_NOCHILDREN) return;
1000 if (win->style & WS_MINIMIZE) return;
1001 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1002
1003 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1004
1005 /* map to client coordinates */
1006 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1007 win->window_rect.top - win->client_rect.top );
1008
1009 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1010
Alexandre Julliard705909a2005-03-16 19:54:33 +00001011 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
Alexandre Julliard5defa492004-12-07 17:31:53 +00001012 {
1013 if (!(child->style & WS_VISIBLE)) continue;
1014 if (!rect_in_region( tmp, &child->window_rect )) continue;
1015 offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1016 redraw_window( child, tmp, 1, flags );
1017 offset_region( tmp, child->client_rect.left, child->client_rect.top );
1018 }
1019 free_region( tmp );
1020}
1021
1022
1023/* retrieve the update flags for a window depending on the state of the update region */
1024static unsigned int get_update_flags( struct window *win, unsigned int flags )
1025{
1026 unsigned int ret = 0;
1027
1028 if (flags & UPDATE_NONCLIENT)
1029 {
1030 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1031 }
1032 if (flags & UPDATE_ERASE)
1033 {
1034 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1035 }
1036 if (flags & UPDATE_PAINT)
1037 {
1038 if (win->update_region) ret |= UPDATE_PAINT;
1039 }
1040 if (flags & UPDATE_INTERNALPAINT)
1041 {
1042 if (win->paint_flags & PAINT_INTERNAL) ret |= UPDATE_INTERNALPAINT;
1043 }
1044 return ret;
1045}
1046
1047
1048/* iterate through the children of the given window until we find one with some update flags */
1049static unsigned int get_child_update_flags( struct window *win, unsigned int flags,
1050 struct window **child )
1051{
1052 struct window *ptr;
1053 unsigned int ret = 0;
1054
Alexandre Julliard705909a2005-03-16 19:54:33 +00001055 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
Alexandre Julliard5defa492004-12-07 17:31:53 +00001056 {
1057 if (!(ptr->style & WS_VISIBLE)) continue;
1058 if ((ret = get_update_flags( ptr, flags )) != 0)
1059 {
1060 *child = ptr;
1061 break;
1062 }
1063 if (ptr->style & WS_MINIMIZE) continue;
1064
1065 /* Note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1066 * here we only want to repaint children of windows that clip them, others
1067 * need to wait for WM_PAINT to be done in the parent first.
1068 */
1069 if (!(flags & UPDATE_NOCHILDREN) &&
1070 ((flags & UPDATE_ALLCHILDREN) || (ptr->style & WS_CLIPCHILDREN)))
Alexandre Julliard705909a2005-03-16 19:54:33 +00001071 {
1072 if ((ret = get_child_update_flags( ptr, flags, child ))) break;
1073 }
Alexandre Julliard5defa492004-12-07 17:31:53 +00001074 }
1075 return ret;
1076}
1077
1078
1079/* expose a region of a window, looking for the top most parent that needs to be exposed */
1080/* the region is in window coordinates */
Alexandre Julliardbc75f2f2005-03-31 15:36:57 +00001081static void expose_window( struct window *win, struct window *top, struct region *region )
Alexandre Julliard5defa492004-12-07 17:31:53 +00001082{
1083 struct window *parent, *ptr;
1084 int offset_x, offset_y;
1085
1086 /* find the top most parent that doesn't clip either siblings or children */
1087 for (parent = ptr = win; ptr != top; ptr = ptr->parent)
1088 {
1089 if (!(ptr->style & WS_CLIPCHILDREN)) parent = ptr;
1090 if (!(ptr->style & WS_CLIPSIBLINGS)) parent = ptr->parent;
1091 }
1092 if (parent == win && parent != top && win->parent)
1093 parent = win->parent; /* always go up at least one level if possible */
1094
1095 offset_x = win->window_rect.left - win->client_rect.left;
1096 offset_y = win->window_rect.top - win->client_rect.top;
1097 for (ptr = win; ptr != parent; ptr = ptr->parent)
1098 {
1099 offset_x += ptr->client_rect.left;
1100 offset_y += ptr->client_rect.top;
1101 }
1102 offset_region( region, offset_x, offset_y );
1103 redraw_window( parent, region, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1104 offset_region( region, -offset_x, -offset_y );
1105}
1106
1107
Alexandre Julliard5defa492004-12-07 17:31:53 +00001108/* set the window and client rectangles, updating the update region if necessary */
Alexandre Julliard4e47afb2005-03-17 14:02:06 +00001109static void set_window_pos( struct window *win, struct window *previous,
Alexandre Julliardae661da2005-02-03 13:40:12 +00001110 unsigned int swp_flags, const rectangle_t *window_rect,
Alexandre Julliardf7560902005-03-17 19:10:41 +00001111 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1112 const rectangle_t *valid_rects )
Alexandre Julliard5defa492004-12-07 17:31:53 +00001113{
Alexandre Julliard4e47afb2005-03-17 14:02:06 +00001114 struct region *old_vis_rgn = NULL, *new_vis_rgn;
Alexandre Julliard5defa492004-12-07 17:31:53 +00001115 const rectangle_t old_window_rect = win->window_rect;
Alexandre Julliardf7560902005-03-17 19:10:41 +00001116 const rectangle_t old_visible_rect = win->visible_rect;
Alexandre Julliard5defa492004-12-07 17:31:53 +00001117 const rectangle_t old_client_rect = win->client_rect;
Alexandre Julliardbc75f2f2005-03-31 15:36:57 +00001118 struct window *top = get_top_clipping_window( win );
Alexandre Julliard4e47afb2005-03-17 14:02:06 +00001119 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
Alexandre Julliard5defa492004-12-07 17:31:53 +00001120
Alexandre Julliard4e47afb2005-03-17 14:02:06 +00001121 if (win->parent && !is_visible( win->parent )) visible = 0;
Alexandre Julliard5defa492004-12-07 17:31:53 +00001122
Alexandre Julliardbc75f2f2005-03-31 15:36:57 +00001123 if (visible && !(old_vis_rgn = get_visible_region( win, top, DCX_WINDOW ))) return;
Alexandre Julliard5defa492004-12-07 17:31:53 +00001124
1125 /* set the new window info before invalidating anything */
1126
Alexandre Julliardf7560902005-03-17 19:10:41 +00001127 win->window_rect = *window_rect;
1128 win->visible_rect = *visible_rect;
1129 win->client_rect = *client_rect;
Alexandre Julliard4d32a472005-03-25 10:38:56 +00001130 if (!(swp_flags & SWP_NOZORDER) && win->parent)
1131 {
1132 list_remove( &win->entry ); /* unlink it from the previous location */
1133 if (previous) list_add_after( &previous->entry, &win->entry );
1134 else list_add_head( &win->parent->children, &win->entry );
1135 }
Alexandre Julliard5defa492004-12-07 17:31:53 +00001136 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1137 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1138
Alexandre Julliard4e47afb2005-03-17 14:02:06 +00001139 /* if the window is not visible, everything is easy */
1140 if (!visible) return;
1141
Alexandre Julliardbc75f2f2005-03-31 15:36:57 +00001142 if (!(new_vis_rgn = get_visible_region( win, top, DCX_WINDOW )))
Alexandre Julliard5defa492004-12-07 17:31:53 +00001143 {
1144 free_region( old_vis_rgn );
1145 clear_error(); /* ignore error since the window info has been modified already */
1146 return;
1147 }
1148
1149 /* expose anything revealed by the change */
1150
Alexandre Julliardae661da2005-02-03 13:40:12 +00001151 if (!(swp_flags & SWP_NOREDRAW))
1152 {
1153 offset_region( old_vis_rgn, old_window_rect.left - window_rect->left,
1154 old_window_rect.top - window_rect->top );
1155 if (xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
Alexandre Julliardbc75f2f2005-03-31 15:36:57 +00001156 expose_window( win, top, new_vis_rgn );
Alexandre Julliardae661da2005-02-03 13:40:12 +00001157 }
Alexandre Julliard5defa492004-12-07 17:31:53 +00001158 free_region( old_vis_rgn );
1159
1160 if (!(win->style & WS_VISIBLE))
1161 {
1162 /* clear the update region since the window is no longer visible */
1163 validate_whole_window( win );
1164 goto done;
1165 }
1166
Alexandre Julliardae661da2005-02-03 13:40:12 +00001167 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1168
Alexandre Julliard5defa492004-12-07 17:31:53 +00001169 /* expose the whole non-client area if it changed in any way */
1170
1171 if ((swp_flags & SWP_FRAMECHANGED) ||
1172 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
Alexandre Julliardf7560902005-03-17 19:10:41 +00001173 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ) ||
Alexandre Julliard5defa492004-12-07 17:31:53 +00001174 memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
1175 {
Alexandre Julliard548c9732005-02-22 19:41:43 +00001176 struct region *tmp = create_empty_region();
Alexandre Julliard5defa492004-12-07 17:31:53 +00001177
1178 if (tmp)
1179 {
Alexandre Julliard548c9732005-02-22 19:41:43 +00001180 /* subtract the valid portion of client rect from the total region */
1181 if (!memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
1182 set_region_rect( tmp, client_rect );
1183 else if (valid_rects)
1184 set_region_rect( tmp, &valid_rects[0] );
1185
Alexandre Julliard5defa492004-12-07 17:31:53 +00001186 set_region_rect( new_vis_rgn, window_rect );
1187 if (subtract_region( tmp, new_vis_rgn, tmp ))
1188 {
Alexandre Julliardae661da2005-02-03 13:40:12 +00001189 offset_region( tmp, -client_rect->left, -client_rect->top );
1190 redraw_window( win, tmp, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
Alexandre Julliard5defa492004-12-07 17:31:53 +00001191 }
Alexandre Julliardae661da2005-02-03 13:40:12 +00001192 free_region( tmp );
Alexandre Julliard5defa492004-12-07 17:31:53 +00001193 }
1194 }
1195
Alexandre Julliard5defa492004-12-07 17:31:53 +00001196done:
1197 free_region( new_vis_rgn );
1198 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1199}
1200
Alexandre Julliardbfce1512003-12-10 04:08:06 +00001201
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001202/* create a window */
1203DECL_HANDLER(create_window)
1204{
Alexandre Julliardbd13ab82003-12-11 05:34:53 +00001205 struct window *win;
1206
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001207 reply->handle = 0;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001208 if (!req->parent) /* return desktop window */
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001209 {
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001210 if (!top_window)
1211 {
Alexandre Julliardbd13ab82003-12-11 05:34:53 +00001212 if (!(top_window = create_window( NULL, NULL, req->atom, req->instance ))) return;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001213 top_window->thread = NULL; /* no thread owns the desktop */
Alexandre Julliard7dafa612002-09-25 00:21:56 +00001214 top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001215 }
Alexandre Julliardbd13ab82003-12-11 05:34:53 +00001216 win = top_window;
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001217 }
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001218 else
1219 {
Alexandre Julliardbd13ab82003-12-11 05:34:53 +00001220 struct window *parent, *owner = NULL;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001221
1222 if (!(parent = get_window( req->parent ))) return;
1223 if (req->owner && !(owner = get_window( req->owner ))) return;
Alexandre Julliard4a9c8392001-11-06 17:54:15 +00001224 if (owner == top_window) owner = NULL;
Alexandre Julliard7dafa612002-09-25 00:21:56 +00001225 else if (owner && parent != top_window)
Alexandre Julliardddc33172001-10-22 19:08:33 +00001226 {
Alexandre Julliard7dafa612002-09-25 00:21:56 +00001227 /* an owned window must be created as top-level */
Alexandre Julliardddc33172001-10-22 19:08:33 +00001228 set_error( STATUS_ACCESS_DENIED );
1229 return;
1230 }
Alexandre Julliardbd13ab82003-12-11 05:34:53 +00001231 if (!(win = create_window( parent, owner, req->atom, req->instance ))) return;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001232 }
Alexandre Julliardbd13ab82003-12-11 05:34:53 +00001233 reply->handle = win->handle;
1234 reply->extra = win->nb_extra_bytes;
1235 reply->class_ptr = get_class_client_ptr( win->class );
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001236}
1237
1238
Alexandre Julliard4d32a472005-03-25 10:38:56 +00001239/* set the parent of a window */
1240DECL_HANDLER(set_parent)
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001241{
Alexandre Julliard4d32a472005-03-25 10:38:56 +00001242 struct window *win, *parent = NULL;
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001243
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001244 if (!(win = get_window( req->handle ))) return;
1245 if (req->parent && !(parent = get_window( req->parent ))) return;
Alexandre Julliard844ceb92001-10-09 23:26:40 +00001246
1247 if (win == top_window)
1248 {
1249 set_error( STATUS_INVALID_PARAMETER );
1250 return;
1251 }
Alexandre Julliard4d32a472005-03-25 10:38:56 +00001252 reply->old_parent = win->parent->handle;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001253 reply->full_parent = parent ? parent->handle : 0;
Alexandre Julliard4d32a472005-03-25 10:38:56 +00001254 set_parent_window( win, parent );
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001255}
1256
1257
1258/* destroy a window */
1259DECL_HANDLER(destroy_window)
1260{
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001261 struct window *win = get_window( req->handle );
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001262 if (win)
1263 {
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001264 if (win != top_window) destroy_window( win );
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001265 else set_error( STATUS_ACCESS_DENIED );
1266 }
1267}
1268
1269
Alexandre Julliardddc33172001-10-22 19:08:33 +00001270/* set a window owner */
1271DECL_HANDLER(set_window_owner)
1272{
1273 struct window *win = get_window( req->handle );
Alexandre Julliard7dafa612002-09-25 00:21:56 +00001274 struct window *owner = NULL;
Alexandre Julliardddc33172001-10-22 19:08:33 +00001275
Alexandre Julliard7dafa612002-09-25 00:21:56 +00001276 if (!win) return;
1277 if (req->owner && !(owner = get_window( req->owner ))) return;
1278 if (win == top_window)
Alexandre Julliardddc33172001-10-22 19:08:33 +00001279 {
Alexandre Julliardddc33172001-10-22 19:08:33 +00001280 set_error( STATUS_ACCESS_DENIED );
1281 return;
1282 }
Alexandre Julliard7dafa612002-09-25 00:21:56 +00001283 reply->prev_owner = win->owner;
1284 reply->full_owner = win->owner = owner ? owner->handle : 0;
Alexandre Julliardddc33172001-10-22 19:08:33 +00001285}
1286
1287
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001288/* get information from a window handle */
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001289DECL_HANDLER(get_window_info)
1290{
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001291 struct window *win = get_window( req->handle );
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001292
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001293 reply->full_handle = 0;
1294 reply->tid = reply->pid = 0;
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001295 if (win)
1296 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001297 reply->full_handle = win->handle;
Alexandre Julliard5030bda2002-10-11 23:41:06 +00001298 reply->last_active = win->handle;
1299 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001300 if (win->thread)
1301 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001302 reply->tid = get_thread_id( win->thread );
1303 reply->pid = get_process_id( win->thread->process );
Alexandre Julliardbfce1512003-12-10 04:08:06 +00001304 reply->atom = get_class_atom( win->class );
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001305 }
1306 }
1307}
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001308
1309
Alexandre Julliardddc33172001-10-22 19:08:33 +00001310/* set some information in a window */
1311DECL_HANDLER(set_window_info)
1312{
1313 struct window *win = get_window( req->handle );
Alexandre Julliard7dafa612002-09-25 00:21:56 +00001314
Alexandre Julliardddc33172001-10-22 19:08:33 +00001315 if (!win) return;
Alexandre Julliard7dafa612002-09-25 00:21:56 +00001316 if (req->flags && win == top_window)
1317 {
1318 set_error( STATUS_ACCESS_DENIED );
1319 return;
1320 }
Alexandre Julliardebf12432003-12-10 01:34:51 +00001321 if (req->extra_size > sizeof(req->extra_value) ||
1322 req->extra_offset < -1 ||
1323 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
Alexandre Julliard97903d22003-11-26 22:15:41 +00001324 {
Alexandre Julliardebf12432003-12-10 01:34:51 +00001325 set_win32_error( ERROR_INVALID_INDEX );
Alexandre Julliard97903d22003-11-26 22:15:41 +00001326 return;
1327 }
1328 if (req->extra_offset != -1)
1329 {
Alexandre Julliardebf12432003-12-10 01:34:51 +00001330 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
Alexandre Julliard97903d22003-11-26 22:15:41 +00001331 }
Alexandre Julliardebf12432003-12-10 01:34:51 +00001332 else if (req->flags & SET_WIN_EXTRA)
Alexandre Julliard97903d22003-11-26 22:15:41 +00001333 {
Alexandre Julliardebf12432003-12-10 01:34:51 +00001334 set_win32_error( ERROR_INVALID_INDEX );
Alexandre Julliard97903d22003-11-26 22:15:41 +00001335 return;
1336 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001337 reply->old_style = win->style;
1338 reply->old_ex_style = win->ex_style;
1339 reply->old_id = win->id;
1340 reply->old_instance = win->instance;
1341 reply->old_user_data = win->user_data;
Alexandre Julliardddc33172001-10-22 19:08:33 +00001342 if (req->flags & SET_WIN_STYLE) win->style = req->style;
1343 if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
1344 if (req->flags & SET_WIN_ID) win->id = req->id;
1345 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
1346 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
Alexandre Julliardebf12432003-12-10 01:34:51 +00001347 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
1348 &req->extra_value, req->extra_size );
Alexandre Julliard5defa492004-12-07 17:31:53 +00001349
1350 /* changing window style triggers a non-client paint */
1351 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
Alexandre Julliardddc33172001-10-22 19:08:33 +00001352}
1353
1354
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001355/* get a list of the window parents, up to the root of the tree */
1356DECL_HANDLER(get_window_parents)
1357{
1358 struct window *ptr, *win = get_window( req->handle );
1359 int total = 0;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001360 user_handle_t *data;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001361 size_t len;
1362
1363 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
1364
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001365 reply->count = total;
1366 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1367 if (len && ((data = set_reply_data_size( len ))))
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001368 {
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001369 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
1370 *data++ = ptr->handle;
1371 }
1372}
1373
1374
1375/* get a list of the window children */
1376DECL_HANDLER(get_window_children)
1377{
1378 struct window *ptr, *parent = get_window( req->parent );
1379 int total = 0;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001380 user_handle_t *data;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001381 size_t len;
1382
1383 if (parent)
Alexandre Julliard705909a2005-03-16 19:54:33 +00001384 {
1385 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
Alexandre Julliard844ceb92001-10-09 23:26:40 +00001386 {
Alexandre Julliardbfce1512003-12-10 04:08:06 +00001387 if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
Alexandre Julliard844ceb92001-10-09 23:26:40 +00001388 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1389 total++;
1390 }
Alexandre Julliard705909a2005-03-16 19:54:33 +00001391 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001392 reply->count = total;
1393 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1394 if (len && ((data = set_reply_data_size( len ))))
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001395 {
Alexandre Julliard705909a2005-03-16 19:54:33 +00001396 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
Alexandre Julliard844ceb92001-10-09 23:26:40 +00001397 {
Alexandre Julliard705909a2005-03-16 19:54:33 +00001398 if (len < sizeof(*data)) break;
Alexandre Julliardbfce1512003-12-10 04:08:06 +00001399 if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
Alexandre Julliard844ceb92001-10-09 23:26:40 +00001400 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001401 *data++ = ptr->handle;
Uwe Bonnes4f7d8932002-04-11 17:32:10 +00001402 len -= sizeof(*data);
Alexandre Julliard844ceb92001-10-09 23:26:40 +00001403 }
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001404 }
1405}
1406
1407
Alexandre Julliard4616dcb2004-07-20 22:17:38 +00001408/* get a list of the window children that contain a given point */
1409DECL_HANDLER(get_window_children_from_point)
1410{
1411 struct user_handle_array array;
1412 struct window *parent = get_window( req->parent );
1413 size_t len;
1414
1415 if (!parent) return;
1416
1417 array.handles = NULL;
1418 array.count = 0;
1419 array.total = 0;
1420 if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
1421
1422 reply->count = array.count;
1423 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
1424 if (len) set_reply_data_ptr( array.handles, len );
1425 else free( array.handles );
1426}
1427
1428
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001429/* get window tree information from a window handle */
1430DECL_HANDLER(get_window_tree)
1431{
Alexandre Julliard705909a2005-03-16 19:54:33 +00001432 struct window *ptr, *win = get_window( req->handle );
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001433
1434 if (!win) return;
1435
Alexandre Julliard705909a2005-03-16 19:54:33 +00001436 reply->parent = 0;
1437 reply->owner = 0;
1438 reply->next_sibling = 0;
1439 reply->prev_sibling = 0;
1440 reply->first_sibling = 0;
1441 reply->last_sibling = 0;
1442 reply->first_child = 0;
1443 reply->last_child = 0;
1444
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001445 if (win->parent)
1446 {
1447 struct window *parent = win->parent;
Alexandre Julliard705909a2005-03-16 19:54:33 +00001448 reply->parent = parent->handle;
1449 reply->owner = win->owner;
1450 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
1451 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
1452 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
1453 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001454 }
Alexandre Julliard705909a2005-03-16 19:54:33 +00001455 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
1456 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
Alexandre Julliarda09da0c2001-09-21 21:08:40 +00001457}
Alexandre Julliard7a2017d2001-10-12 19:10:26 +00001458
1459
Alexandre Julliard5defa492004-12-07 17:31:53 +00001460/* set the position and Z order of a window */
1461DECL_HANDLER(set_window_pos)
Alexandre Julliard0d509652001-10-16 21:55:37 +00001462{
Alexandre Julliardf7560902005-03-17 19:10:41 +00001463 const rectangle_t *visible_rect = NULL, *valid_rects = NULL;
Alexandre Julliard5defa492004-12-07 17:31:53 +00001464 struct window *previous = NULL;
Alexandre Julliard0d509652001-10-16 21:55:37 +00001465 struct window *win = get_window( req->handle );
Alexandre Julliard5defa492004-12-07 17:31:53 +00001466 unsigned int flags = req->flags;
Alexandre Julliard0d509652001-10-16 21:55:37 +00001467
Alexandre Julliard5defa492004-12-07 17:31:53 +00001468 if (!win) return;
1469 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
1470
Alexandre Julliard5defa492004-12-07 17:31:53 +00001471 if (!(flags & SWP_NOZORDER))
1472 {
1473 if (!req->previous) /* special case: HWND_TOP */
1474 {
Alexandre Julliard705909a2005-03-16 19:54:33 +00001475 if (get_first_child(win->parent) == win) flags |= SWP_NOZORDER;
Alexandre Julliard5defa492004-12-07 17:31:53 +00001476 }
1477 else if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
1478 {
Alexandre Julliard705909a2005-03-16 19:54:33 +00001479 previous = get_last_child( win->parent );
Alexandre Julliard5defa492004-12-07 17:31:53 +00001480 }
1481 else
1482 {
1483 if (!(previous = get_window( req->previous ))) return;
1484 /* previous must be a sibling */
1485 if (previous->parent != win->parent)
1486 {
1487 set_error( STATUS_INVALID_PARAMETER );
1488 return;
1489 }
1490 }
1491 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
1492 }
1493
Alexandre Julliard548c9732005-02-22 19:41:43 +00001494 /* window rectangle must be ordered properly */
1495 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
Alexandre Julliard5defa492004-12-07 17:31:53 +00001496 {
1497 set_error( STATUS_INVALID_PARAMETER );
1498 return;
1499 }
1500
Alexandre Julliardf7560902005-03-17 19:10:41 +00001501 if (get_req_data_size() >= sizeof(rectangle_t)) visible_rect = get_req_data();
1502 if (get_req_data_size() >= 3 * sizeof(rectangle_t)) valid_rects = visible_rect + 1;
Alexandre Julliardae661da2005-02-03 13:40:12 +00001503
Alexandre Julliardf7560902005-03-17 19:10:41 +00001504 if (!visible_rect) visible_rect = &req->window;
1505 set_window_pos( win, previous, flags, &req->window, &req->client, visible_rect, valid_rects );
Alexandre Julliard5defa492004-12-07 17:31:53 +00001506 reply->new_style = win->style;
Alexandre Julliard0d509652001-10-16 21:55:37 +00001507}
1508
1509
1510/* get the window and client rectangles of a window */
1511DECL_HANDLER(get_window_rectangles)
1512{
1513 struct window *win = get_window( req->handle );
1514
1515 if (win)
1516 {
Alexandre Julliardf7560902005-03-17 19:10:41 +00001517 reply->window = win->window_rect;
1518 reply->visible = win->visible_rect;
1519 reply->client = win->client_rect;
Alexandre Julliard0d509652001-10-16 21:55:37 +00001520 }
1521}
1522
1523
Alexandre Julliard805bdc52001-11-13 22:23:48 +00001524/* get the window text */
1525DECL_HANDLER(get_window_text)
1526{
1527 struct window *win = get_window( req->handle );
Alexandre Julliard805bdc52001-11-13 22:23:48 +00001528
1529 if (win && win->text)
1530 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001531 size_t len = strlenW( win->text ) * sizeof(WCHAR);
1532 if (len > get_reply_max_size()) len = get_reply_max_size();
1533 set_reply_data( win->text, len );
Alexandre Julliard805bdc52001-11-13 22:23:48 +00001534 }
Alexandre Julliard805bdc52001-11-13 22:23:48 +00001535}
1536
1537
1538/* set the window text */
1539DECL_HANDLER(set_window_text)
1540{
1541 struct window *win = get_window( req->handle );
1542
1543 if (win)
1544 {
1545 WCHAR *text = NULL;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001546 size_t len = get_req_data_size() / sizeof(WCHAR);
Alexandre Julliard805bdc52001-11-13 22:23:48 +00001547 if (len)
1548 {
1549 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001550 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
Alexandre Julliard805bdc52001-11-13 22:23:48 +00001551 text[len] = 0;
1552 }
1553 if (win->text) free( win->text );
1554 win->text = text;
1555 }
1556}
1557
1558
Alexandre Julliard0d509652001-10-16 21:55:37 +00001559/* get the coordinates offset between two windows */
1560DECL_HANDLER(get_windows_offset)
1561{
1562 struct window *win;
1563
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001564 reply->x = reply->y = 0;
Alexandre Julliard0d509652001-10-16 21:55:37 +00001565 if (req->from)
1566 {
1567 if (!(win = get_window( req->from ))) return;
1568 while (win)
1569 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001570 reply->x += win->client_rect.left;
1571 reply->y += win->client_rect.top;
Alexandre Julliard0d509652001-10-16 21:55:37 +00001572 win = win->parent;
1573 }
1574 }
1575 if (req->to)
1576 {
1577 if (!(win = get_window( req->to ))) return;
1578 while (win)
1579 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001580 reply->x -= win->client_rect.left;
1581 reply->y -= win->client_rect.top;
Alexandre Julliard0d509652001-10-16 21:55:37 +00001582 win = win->parent;
1583 }
1584 }
1585}
1586
1587
Alexandre Julliarde8d86b72004-06-23 20:44:58 +00001588/* get the visible region of a window */
1589DECL_HANDLER(get_visible_region)
1590{
1591 struct region *region;
Alexandre Julliardbc75f2f2005-03-31 15:36:57 +00001592 struct window *top, *win = get_window( req->window );
Alexandre Julliarde8d86b72004-06-23 20:44:58 +00001593
1594 if (!win) return;
Alexandre Julliarde8d86b72004-06-23 20:44:58 +00001595
Alexandre Julliardbc75f2f2005-03-31 15:36:57 +00001596 top = get_top_clipping_window( win );
1597 if ((region = get_visible_region( win, top, req->flags )))
Alexandre Julliarde8d86b72004-06-23 20:44:58 +00001598 {
Alexandre Julliardeea70692005-03-30 17:11:46 +00001599 rectangle_t *data;
1600 map_win_region_to_screen( win, region );
1601 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
Alexandre Julliard807fe072004-09-17 18:20:11 +00001602 if (data) set_reply_data_ptr( data, reply->total_size );
Alexandre Julliarde8d86b72004-06-23 20:44:58 +00001603 }
Alexandre Julliardbc75f2f2005-03-31 15:36:57 +00001604 reply->top_win = top->handle;
1605 reply->top_org_x = top->visible_rect.left;
1606 reply->top_org_y = top->visible_rect.top;
1607 reply->win_org_x = (req->flags & DCX_WINDOW) ? win->window_rect.left : win->client_rect.left;
1608 reply->win_org_y = (req->flags & DCX_WINDOW) ? win->window_rect.top : win->client_rect.top;
1609 client_to_screen( top->parent, &reply->top_org_x, &reply->top_org_y );
1610 client_to_screen( win->parent, &reply->win_org_x, &reply->win_org_y );
Alexandre Julliarde8d86b72004-06-23 20:44:58 +00001611}
1612
1613
Alexandre Julliard618a7e52004-06-29 03:53:25 +00001614/* get the window region */
1615DECL_HANDLER(get_window_region)
1616{
1617 struct window *win = get_window( req->window );
1618
1619 if (!win) return;
1620
Alexandre Julliard618a7e52004-06-29 03:53:25 +00001621 if (win->win_region)
1622 {
Alexandre Julliard807fe072004-09-17 18:20:11 +00001623 rectangle_t *data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
1624 if (data) set_reply_data_ptr( data, reply->total_size );
Alexandre Julliard618a7e52004-06-29 03:53:25 +00001625 }
1626}
1627
1628
1629/* set the window region */
1630DECL_HANDLER(set_window_region)
1631{
1632 struct region *region = NULL;
1633 struct window *win = get_window( req->window );
1634
1635 if (!win) return;
1636
1637 if (get_req_data_size()) /* no data means remove the region completely */
1638 {
1639 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1640 return;
1641 }
1642 if (win->win_region) free_region( win->win_region );
1643 win->win_region = region;
1644}
1645
1646
Alexandre Julliard5defa492004-12-07 17:31:53 +00001647/* get a window update region */
1648DECL_HANDLER(get_update_region)
1649{
1650 rectangle_t *data;
1651 unsigned int flags = req->flags;
1652 struct window *win = get_window( req->window );
1653
1654 reply->flags = 0;
1655 if (!win || !is_visible( win )) return;
1656
1657 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1658 {
1659 /* non-client painting must be delayed if one of the parents is going to
1660 * be repainted and doesn't clip children */
1661 struct window *ptr;
1662
1663 for (ptr = win->parent; ptr && ptr != top_window; ptr = ptr->parent)
1664 {
1665 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1666 return;
1667 }
1668 }
1669
1670 if (!(reply->flags = get_update_flags( win, flags )))
1671 {
1672 /* if window doesn't need any repaint, check the children */
1673 if (!(flags & UPDATE_NOCHILDREN) &&
Alexandre Julliard1bc2caa2005-03-09 11:49:46 +00001674 ((flags & UPDATE_ALLCHILDREN) || (win->style & WS_CLIPCHILDREN)) &&
1675 !(win->style & WS_MINIMIZE))
Alexandre Julliard5defa492004-12-07 17:31:53 +00001676 {
1677 reply->flags = get_child_update_flags( win, flags, &win );
1678 }
1679 }
1680
1681 reply->child = win->handle;
1682
1683 if (flags & UPDATE_NOREGION) return;
1684
1685 if (win->update_region)
1686 {
Alexandre Julliardeea70692005-03-30 17:11:46 +00001687 /* convert update region to screen coordinates */
1688 struct region *region = create_empty_region();
1689
1690 if (!region) return;
1691 if (!copy_region( region, win->update_region ))
1692 {
1693 free_region( region );
1694 return;
1695 }
1696 map_win_region_to_screen( win, region );
1697 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
1698 &reply->total_size ))) return;
Alexandre Julliard5defa492004-12-07 17:31:53 +00001699 set_reply_data_ptr( data, reply->total_size );
1700 }
1701
1702 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
1703 {
1704 validate_whole_window( win );
1705 }
1706 else
1707 {
1708 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
Alexandre Julliard56206372005-01-11 15:15:11 +00001709 if (reply->flags & UPDATE_ERASE)
1710 {
1711 win->paint_flags &= ~PAINT_ERASE;
1712 /* desktop window only gets erased, not repainted */
1713 if (win == top_window) validate_whole_window( win );
1714 }
Alexandre Julliard5defa492004-12-07 17:31:53 +00001715 }
1716}
1717
1718
Alexandre Julliard5054c792005-03-21 12:37:00 +00001719/* update the z order of a window so that a given rectangle is fully visible */
1720DECL_HANDLER(update_window_zorder)
1721{
1722 rectangle_t tmp;
1723 struct window *ptr, *win = get_window( req->window );
1724
1725 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
1726
1727 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
1728 {
1729 if (ptr == win) break;
1730 if (!(ptr->style & WS_VISIBLE)) continue;
1731 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1732 if (!intersect_rect( &tmp, &ptr->visible_rect, &req->rect )) continue;
1733 if (ptr->win_region && !rect_in_region( ptr->win_region, &req->rect )) continue;
1734 /* found a window obscuring the rectangle, now move win above this one */
1735 list_remove( &win->entry );
1736 list_add_before( &ptr->entry, &win->entry );
1737 break;
1738 }
1739}
1740
1741
Alexandre Julliard5defa492004-12-07 17:31:53 +00001742/* mark parts of a window as needing a redraw */
1743DECL_HANDLER(redraw_window)
1744{
1745 struct region *region = NULL;
1746 struct window *win = get_window( req->window );
1747
1748 if (!win) return;
1749 if (!is_visible( win )) return; /* nothing to do */
1750
1751 if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
1752 {
1753 if (get_req_data_size()) /* no data means whole rectangle */
1754 {
1755 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1756 return;
1757 }
1758 }
1759
1760 redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
1761 req->flags );
1762 if (region) free_region( region );
1763}
1764
1765
Alexandre Julliard7a2017d2001-10-12 19:10:26 +00001766/* set a window property */
1767DECL_HANDLER(set_window_property)
1768{
1769 struct window *win = get_window( req->window );
1770
Alexandre Julliard9e73cdd2005-05-11 19:01:10 +00001771 if (!win) return;
1772
1773 if (get_req_data_size())
1774 {
1775 atom_t atom = add_global_atom( get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1776 if (atom)
1777 {
1778 set_property( win, atom, req->handle, PROP_TYPE_STRING );
1779 release_global_atom( atom );
1780 }
1781 }
1782 else set_property( win, req->atom, req->handle, PROP_TYPE_ATOM );
Alexandre Julliard7a2017d2001-10-12 19:10:26 +00001783}
1784
1785
1786/* remove a window property */
1787DECL_HANDLER(remove_window_property)
1788{
1789 struct window *win = get_window( req->window );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001790 reply->handle = 0;
Alexandre Julliard9e73cdd2005-05-11 19:01:10 +00001791 if (win)
1792 {
1793 atom_t atom = req->atom;
1794 if (get_req_data_size()) atom = find_global_atom( get_req_data(),
1795 get_req_data_size() / sizeof(WCHAR) );
1796 if (atom) reply->handle = remove_property( win, atom );
1797 }
Alexandre Julliard7a2017d2001-10-12 19:10:26 +00001798}
1799
1800
1801/* get a window property */
1802DECL_HANDLER(get_window_property)
1803{
1804 struct window *win = get_window( req->window );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001805 reply->handle = 0;
Alexandre Julliard9e73cdd2005-05-11 19:01:10 +00001806 if (win)
1807 {
1808 atom_t atom = req->atom;
1809 if (get_req_data_size()) atom = find_global_atom( get_req_data(),
1810 get_req_data_size() / sizeof(WCHAR) );
1811 if (atom) reply->handle = get_property( win, atom );
1812 }
Alexandre Julliard7a2017d2001-10-12 19:10:26 +00001813}
1814
1815
1816/* get the list of properties of a window */
1817DECL_HANDLER(get_window_properties)
1818{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001819 property_data_t *data;
1820 int i, count, max = get_reply_max_size() / sizeof(*data);
Alexandre Julliard7a2017d2001-10-12 19:10:26 +00001821 struct window *win = get_window( req->window );
1822
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001823 reply->total = 0;
1824 if (!win) return;
1825
1826 for (i = count = 0; i < win->prop_inuse; i++)
1827 if (win->properties[i].type != PROP_TYPE_FREE) count++;
1828 reply->total = count;
1829
1830 if (count > max) count = max;
1831 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
1832
1833 for (i = 0; i < win->prop_inuse && count; i++)
1834 {
1835 if (win->properties[i].type == PROP_TYPE_FREE) continue;
1836 data->atom = win->properties[i].atom;
1837 data->string = (win->properties[i].type == PROP_TYPE_STRING);
1838 data->handle = win->properties[i].handle;
1839 data++;
1840 count--;
1841 }
Alexandre Julliard7a2017d2001-10-12 19:10:26 +00001842}
Alexandre Julliard8d174d32003-10-07 03:40:23 +00001843
1844
1845/* get the new window pointer for a global window, checking permissions */
1846/* helper for set_global_windows request */
1847static int get_new_global_window( struct window **win, user_handle_t handle )
1848{
Alexandre Julliard8d174d32003-10-07 03:40:23 +00001849 if (!handle)
1850 {
1851 *win = NULL;
1852 return 1;
1853 }
Martin Fuchs76adb1f2003-11-18 00:13:34 +00001854 else if (*win)
1855 {
1856 set_error( STATUS_ACCESS_DENIED );
1857 return 0;
1858 }
Alexandre Julliard8d174d32003-10-07 03:40:23 +00001859 *win = get_window( handle );
1860 return (*win != NULL);
1861}
1862
1863/* Set/get the global windows */
1864DECL_HANDLER(set_global_windows)
1865{
1866 struct window *new_shell_window = shell_window;
1867 struct window *new_shell_listview = shell_listview;
1868 struct window *new_progman_window = progman_window;
1869 struct window *new_taskman_window = taskman_window;
1870
1871 reply->old_shell_window = shell_window ? shell_window->handle : 0;
1872 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
1873 reply->old_progman_window = progman_window ? progman_window->handle : 0;
1874 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
1875
1876 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
1877 {
1878 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
1879 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
1880 }
1881 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
1882 {
1883 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
1884 }
1885 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
1886 {
1887 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
1888 }
1889 shell_window = new_shell_window;
1890 shell_listview = new_shell_listview;
1891 progman_window = new_progman_window;
1892 taskman_window = new_taskman_window;
1893}