2 * Server-side window handling
4 * Copyright (C) 2001 Alexandre Julliard
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.
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.
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
22 #include "wine/port.h"
39 /* a window property */
42 unsigned short type; /* property type (see below) */
43 atom_t atom; /* property atom */
44 obj_handle_t handle; /* property handle (user-defined storage) */
49 PROP_TYPE_FREE, /* free entry */
50 PROP_TYPE_STRING, /* atom that was originally a string */
51 PROP_TYPE_ATOM /* plain atom */
57 struct window *parent; /* parent window */
58 user_handle_t owner; /* owner of this window */
59 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 */
66 atom_t atom; /* class atom */
67 user_handle_t last_active; /* last active popup */
68 rectangle_t window_rect; /* window rectangle */
69 rectangle_t client_rect; /* client rectangle */
70 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 */
75 WCHAR *text; /* window caption text */
76 int paint_count; /* count of pending paints for this window */
77 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 */
82 static struct window *top_window; /* top-level (desktop) window */
85 /* retrieve a pointer to a window from its handle */
86 inline static struct window *get_window( user_handle_t handle )
88 struct window *ret = get_user_object( handle, USER_WINDOW );
89 if (!ret) set_error( STATUS_INVALID_HANDLE );
93 /* unlink a window from the tree */
94 static void unlink_window( struct window *win )
96 struct window *parent = win->parent;
100 if (win->next) win->next->prev = win->prev;
101 else if (parent->last_child == win) parent->last_child = win->prev;
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;
109 /* link a window into the tree (or unlink it if the new parent is NULL) */
110 static void link_window( struct window *win, struct window *parent, struct window *previous )
112 unlink_window( win ); /* unlink it from the previous location */
116 win->parent = parent;
117 if ((win->prev = previous))
119 if ((win->next = previous->next)) win->next->prev = win;
120 else if (win->parent->last_child == previous) win->parent->last_child = win;
121 win->prev->next = win;
125 if ((win->next = parent->first_child)) win->next->prev = win;
126 else win->parent->last_child = win;
127 parent->first_child = win;
130 else /* move it to parent unlinked list */
132 parent = win->parent;
133 if ((win->next = parent->first_unlinked)) win->next->prev = win;
135 parent->first_unlinked = win;
139 /* set a window property */
140 static void set_property( struct window *win, atom_t atom, obj_handle_t handle,
141 enum property_type type )
144 struct property *new_props;
146 /* check if it exists already */
147 for (i = 0; i < win->prop_inuse; i++)
149 if (win->properties[i].type == PROP_TYPE_FREE)
154 if (win->properties[i].atom == atom)
156 win->properties[i].type = type;
157 win->properties[i].handle = handle;
162 /* need to add an entry */
163 if (!grab_global_atom( atom )) return;
167 if (win->prop_inuse >= win->prop_alloc)
169 /* need to grow the array */
170 if (!(new_props = realloc( win->properties,
171 sizeof(*new_props) * (win->prop_alloc + 16) )))
173 set_error( STATUS_NO_MEMORY );
174 release_global_atom( atom );
177 win->prop_alloc += 16;
178 win->properties = new_props;
180 free = win->prop_inuse++;
182 win->properties[free].atom = atom;
183 win->properties[free].type = type;
184 win->properties[free].handle = handle;
187 /* remove a window property */
188 static obj_handle_t remove_property( struct window *win, atom_t atom )
192 for (i = 0; i < win->prop_inuse; i++)
194 if (win->properties[i].type == PROP_TYPE_FREE) continue;
195 if (win->properties[i].atom == atom)
197 release_global_atom( atom );
198 win->properties[i].type = PROP_TYPE_FREE;
199 return win->properties[i].handle;
202 /* FIXME: last error? */
206 /* find a window property */
207 static obj_handle_t get_property( struct window *win, atom_t atom )
211 for (i = 0; i < win->prop_inuse; i++)
213 if (win->properties[i].type == PROP_TYPE_FREE) continue;
214 if (win->properties[i].atom == atom) return win->properties[i].handle;
216 /* FIXME: last error? */
220 /* destroy all properties of a window */
221 inline static void destroy_properties( struct window *win )
225 if (!win->properties) return;
226 for (i = 0; i < win->prop_inuse; i++)
228 if (win->properties[i].type == PROP_TYPE_FREE) continue;
229 release_global_atom( win->properties[i].atom );
231 free( win->properties );
234 /* destroy a window */
235 static void destroy_window( struct window *win )
237 assert( win != top_window );
239 /* destroy all children */
240 while (win->first_child) destroy_window( win->first_child );
241 while (win->first_unlinked) destroy_window( win->first_unlinked );
243 if (win->thread->queue)
245 if (win->paint_count) inc_queue_paint_count( win->thread, -win->paint_count );
246 queue_cleanup_window( win->thread, win->handle );
248 free_user_handle( win->handle );
249 destroy_properties( win );
250 unlink_window( win );
251 if (win->text) free( win->text );
252 memset( win, 0x55, sizeof(*win) );
256 /* create a new window structure (note: the window is not linked in the window tree) */
257 static struct window *create_window( struct window *parent, struct window *owner, atom_t atom )
259 struct window *win = mem_alloc( sizeof(*win) );
260 if (!win) return NULL;
262 if (!(win->handle = alloc_user_handle( win, USER_WINDOW )))
267 win->parent = parent;
268 win->owner = owner ? owner->handle : 0;
269 win->first_child = NULL;
270 win->last_child = NULL;
271 win->first_unlinked = NULL;
272 win->thread = current;
274 win->last_active = win->handle;
278 win->instance = NULL;
279 win->user_data = NULL;
281 win->paint_count = 0;
284 win->properties = NULL;
286 if (parent) /* put it on parent unlinked list */
288 if ((win->next = parent->first_unlinked)) win->next->prev = win;
290 parent->first_unlinked = win;
292 else win->next = win->prev = NULL;
294 /* 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 );
300 /* destroy all windows belonging to a given thread */
301 void destroy_thread_windows( struct thread *thread )
303 user_handle_t handle = 0;
306 while ((win = next_user_handle( &handle, USER_WINDOW )))
308 if (win->thread != thread) continue;
309 destroy_window( win );
313 /* check whether child is a descendant of parent */
314 int is_child_window( user_handle_t parent, user_handle_t child )
316 struct window *child_ptr = get_user_object( child, USER_WINDOW );
317 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
319 if (!child_ptr || !parent_ptr) return 0;
320 while (child_ptr->parent)
322 if (child_ptr->parent == parent_ptr) return 1;
323 child_ptr = child_ptr->parent;
328 /* check whether window is a top-level window */
329 int is_top_level_window( user_handle_t window )
331 struct window *win = get_user_object( window, USER_WINDOW );
332 return (win && win->parent == top_window);
335 /* make a window active if possible */
336 int make_window_active( user_handle_t window )
338 struct window *owner, *win = get_window( window );
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;
348 /* find child of 'parent' that contains the given point (in parent-relative coords) */
349 static struct window *child_window_from_point( struct window *parent, int x, int y )
353 for (ptr = parent->first_child; ptr; ptr = ptr->next)
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 */
364 /* FIXME: check window region here */
366 /* if window is minimized or disabled, return at once */
367 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
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)
374 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
376 return parent; /* not found any child */
379 /* find window containing point (in absolute coords) */
380 user_handle_t window_from_point( int x, int y )
384 if (!top_window) return 0;
385 ret = child_window_from_point( top_window, x, y );
389 /* return the thread owning a window */
390 struct thread *get_window_thread( user_handle_t handle )
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 );
397 /* find a child of the specified window that needs repainting */
398 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
400 struct window *ptr, *ret = NULL;
402 for (ptr = parent->first_child; ptr && !ret; ptr = ptr->next)
404 if (!(ptr->style & WS_VISIBLE)) continue;
405 if (ptr->paint_count && ptr->thread == thread)
407 else /* explore its children */
408 ret = find_child_to_repaint( ptr, thread );
411 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
413 /* transparent window, check for non-transparent sibling to paint first */
414 for (ptr = ret->next; ptr; ptr = ptr->next)
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;
425 /* find a window that needs repainting */
426 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
428 struct window *win = parent ? get_window( parent ) : top_window;
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;
437 /* create a window */
438 DECL_HANDLER(create_window)
441 if (!req->parent) /* return desktop window */
445 if (!(top_window = create_window( NULL, NULL, req->atom ))) return;
446 top_window->thread = NULL; /* no thread owns the desktop */
447 top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
449 reply->handle = top_window->handle;
453 struct window *win, *parent, *owner = NULL;
455 if (!(parent = get_window( req->parent ))) return;
456 if (req->owner && !(owner = get_window( req->owner ))) return;
457 if (owner == top_window) owner = NULL;
458 else if (owner && parent != top_window)
460 /* an owned window must be created as top-level */
461 set_error( STATUS_ACCESS_DENIED );
464 if (!(win = create_window( parent, owner, req->atom ))) return;
465 reply->handle = win->handle;
470 /* link a window into the tree */
471 DECL_HANDLER(link_window)
473 struct window *win, *parent = NULL, *previous = NULL;
475 if (!(win = get_window( req->handle ))) return;
476 if (req->parent && !(parent = get_window( req->parent ))) return;
478 if (win == top_window)
480 set_error( STATUS_INVALID_PARAMETER );
483 reply->full_parent = parent ? parent->handle : 0;
484 if (parent && req->previous)
486 if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
488 previous = parent->last_child;
489 if (previous == win) return; /* nothing to do */
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)
497 set_error( STATUS_INVALID_PARAMETER );
502 link_window( win, parent, previous );
506 /* destroy a window */
507 DECL_HANDLER(destroy_window)
509 struct window *win = get_window( req->handle );
512 if (win != top_window) destroy_window( win );
513 else set_error( STATUS_ACCESS_DENIED );
518 /* set a window owner */
519 DECL_HANDLER(set_window_owner)
521 struct window *win = get_window( req->handle );
522 struct window *owner = NULL;
525 if (req->owner && !(owner = get_window( req->owner ))) return;
526 if (win == top_window)
528 set_error( STATUS_ACCESS_DENIED );
531 reply->prev_owner = win->owner;
532 reply->full_owner = win->owner = owner ? owner->handle : 0;
536 /* get information from a window handle */
537 DECL_HANDLER(get_window_info)
539 struct window *win = get_window( req->handle );
541 reply->full_handle = 0;
542 reply->tid = reply->pid = 0;
545 reply->full_handle = win->handle;
546 reply->last_active = win->handle;
547 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
550 reply->tid = get_thread_id( win->thread );
551 reply->pid = get_process_id( win->thread->process );
552 reply->atom = win->atom;
558 /* set some information in a window */
559 DECL_HANDLER(set_window_info)
561 struct window *win = get_window( req->handle );
564 if (req->flags && win == top_window)
566 set_error( STATUS_ACCESS_DENIED );
569 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;
574 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;
582 /* get a list of the window parents, up to the root of the tree */
583 DECL_HANDLER(get_window_parents)
585 struct window *ptr, *win = get_window( req->handle );
590 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
592 reply->count = total;
593 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
594 if (len && ((data = set_reply_data_size( len ))))
596 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
597 *data++ = ptr->handle;
602 /* get a list of the window children */
603 DECL_HANDLER(get_window_children)
605 struct window *ptr, *parent = get_window( req->parent );
611 for (ptr = parent->first_child, total = 0; ptr; ptr = ptr->next)
613 if (req->atom && ptr->atom != req->atom) continue;
614 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
618 reply->count = total;
619 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
620 if (len && ((data = set_reply_data_size( len ))))
622 for (ptr = parent->first_child; ptr && len; ptr = ptr->next)
624 if (req->atom && ptr->atom != req->atom) continue;
625 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
626 *data++ = ptr->handle;
627 len -= sizeof(*data);
633 /* get window tree information from a window handle */
634 DECL_HANDLER(get_window_tree)
636 struct window *win = get_window( req->handle );
642 struct window *parent = win->parent;
643 reply->parent = parent->handle;
644 reply->owner = win->owner;
645 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;
654 reply->next_sibling = 0;
655 reply->prev_sibling = 0;
656 reply->first_sibling = 0;
657 reply->last_sibling = 0;
659 reply->first_child = win->first_child ? win->first_child->handle : 0;
660 reply->last_child = win->last_child ? win->last_child->handle : 0;
664 /* set the window and client rectangles of a window */
665 DECL_HANDLER(set_window_rectangles)
667 struct window *win = get_window( req->handle );
671 win->window_rect = req->window;
672 win->client_rect = req->client;
677 /* get the window and client rectangles of a window */
678 DECL_HANDLER(get_window_rectangles)
680 struct window *win = get_window( req->handle );
684 reply->window = win->window_rect;
685 reply->client = win->client_rect;
690 /* get the window text */
691 DECL_HANDLER(get_window_text)
693 struct window *win = get_window( req->handle );
695 if (win && win->text)
697 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 );
704 /* set the window text */
705 DECL_HANDLER(set_window_text)
707 struct window *win = get_window( req->handle );
712 size_t len = get_req_data_size() / sizeof(WCHAR);
715 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
716 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
719 if (win->text) free( win->text );
725 /* increment the window paint count */
726 DECL_HANDLER(inc_window_paint_count)
728 struct window *win = get_window( req->handle );
730 if (win && win->thread)
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 );
739 /* get the coordinates offset between two windows */
740 DECL_HANDLER(get_windows_offset)
744 reply->x = reply->y = 0;
747 if (!(win = get_window( req->from ))) return;
750 reply->x += win->client_rect.left;
751 reply->y += win->client_rect.top;
757 if (!(win = get_window( req->to ))) return;
760 reply->x -= win->client_rect.left;
761 reply->y -= win->client_rect.top;
768 /* set a window property */
769 DECL_HANDLER(set_window_property)
771 struct window *win = get_window( req->window );
773 if (win) set_property( win, req->atom, req->handle,
774 req->string ? PROP_TYPE_STRING : PROP_TYPE_ATOM );
778 /* remove a window property */
779 DECL_HANDLER(remove_window_property)
781 struct window *win = get_window( req->window );
783 if (win) reply->handle = remove_property( win, req->atom );
787 /* get a window property */
788 DECL_HANDLER(get_window_property)
790 struct window *win = get_window( req->window );
792 if (win) reply->handle = get_property( win, req->atom );
796 /* get the list of properties of a window */
797 DECL_HANDLER(get_window_properties)
799 property_data_t *data;
800 int i, count, max = get_reply_max_size() / sizeof(*data);
801 struct window *win = get_window( req->window );
806 for (i = count = 0; i < win->prop_inuse; i++)
807 if (win->properties[i].type != PROP_TYPE_FREE) count++;
808 reply->total = count;
810 if (count > max) count = max;
811 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
813 for (i = 0; i < win->prop_inuse && count; i++)
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;