window.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. /*
  2. * Server-side window handling
  3. *
  4. * Copyright (C) 2001 Alexandre Julliard
  5. *
  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
  19. */
  20. #include "config.h"
  21. #include "wine/port.h"
  22. #include <assert.h>
  23. #include <stdarg.h>
  24. #include "windef.h"
  25. #include "winbase.h"
  26. #include "wingdi.h"
  27. #include "winuser.h"
  28. #include "object.h"
  29. #include "request.h"
  30. #include "thread.h"
  31. #include "process.h"
  32. #include "user.h"
  33. #include "unicode.h"
  34. /* a window property */
  35. struct property
  36. {
  37. unsigned short type; /* property type (see below) */
  38. atom_t atom; /* property atom */
  39. obj_handle_t handle; /* property handle (user-defined storage) */
  40. };
  41. enum property_type
  42. {
  43. PROP_TYPE_FREE, /* free entry */
  44. PROP_TYPE_STRING, /* atom that was originally a string */
  45. PROP_TYPE_ATOM /* plain atom */
  46. };
  47. struct window
  48. {
  49. struct window *parent; /* parent window */
  50. user_handle_t owner; /* owner of this window */
  51. struct window *first_child; /* first child in Z-order */
  52. struct window *last_child; /* last child in Z-order */
  53. struct window *first_unlinked; /* first child not linked in the Z-order list */
  54. struct window *next; /* next window in Z-order */
  55. struct window *prev; /* prev window in Z-order */
  56. user_handle_t handle; /* full handle for this window */
  57. struct thread *thread; /* thread owning the window */
  58. struct window_class *class; /* window class */
  59. atom_t atom; /* class atom */
  60. user_handle_t last_active; /* last active popup */
  61. rectangle_t window_rect; /* window rectangle (relative to parent client area) */
  62. rectangle_t client_rect; /* client rectangle (relative to parent client area) */
  63. struct region *win_region; /* region for shaped windows (relative to window rect) */
  64. struct region *update_region; /* update region (relative to window rect) */
  65. unsigned int style; /* window style */
  66. unsigned int ex_style; /* window extended style */
  67. unsigned int id; /* window id */
  68. void* instance; /* creator instance */
  69. void* user_data; /* user-specific data */
  70. WCHAR *text; /* window caption text */
  71. unsigned int paint_flags; /* various painting flags */
  72. int prop_inuse; /* number of in-use window properties */
  73. int prop_alloc; /* number of allocated window properties */
  74. struct property *properties; /* window properties array */
  75. int nb_extra_bytes; /* number of extra bytes */
  76. char extra_bytes[1]; /* extra bytes storage */
  77. };
  78. #define PAINT_INTERNAL 0x01 /* internal WM_PAINT pending */
  79. #define PAINT_ERASE 0x02 /* needs WM_ERASEBKGND */
  80. #define PAINT_NONCLIENT 0x04 /* needs WM_NCPAINT */
  81. /* growable array of user handles */
  82. struct user_handle_array
  83. {
  84. user_handle_t *handles;
  85. int count;
  86. int total;
  87. };
  88. static struct window *top_window; /* top-level (desktop) window */
  89. /* global window pointers */
  90. static struct window *shell_window;
  91. static struct window *shell_listview;
  92. static struct window *progman_window;
  93. static struct window *taskman_window;
  94. /* retrieve a pointer to a window from its handle */
  95. inline static struct window *get_window( user_handle_t handle )
  96. {
  97. struct window *ret = get_user_object( handle, USER_WINDOW );
  98. if (!ret) set_error( STATUS_INVALID_HANDLE );
  99. return ret;
  100. }
  101. /* unlink a window from the tree */
  102. static void unlink_window( struct window *win )
  103. {
  104. struct window *parent = win->parent;
  105. assert( parent );
  106. if (win->next) win->next->prev = win->prev;
  107. else if (parent->last_child == win) parent->last_child = win->prev;
  108. if (win->prev) win->prev->next = win->next;
  109. else if (parent->first_child == win) parent->first_child = win->next;
  110. else if (parent->first_unlinked == win) parent->first_unlinked = win->next;
  111. }
  112. /* link a window into the tree (or unlink it if the new parent is NULL) */
  113. static void link_window( struct window *win, struct window *parent, struct window *previous )
  114. {
  115. unlink_window( win ); /* unlink it from the previous location */
  116. if (parent)
  117. {
  118. win->parent = parent;
  119. if ((win->prev = previous))
  120. {
  121. if ((win->next = previous->next)) win->next->prev = win;
  122. else if (win->parent->last_child == previous) win->parent->last_child = win;
  123. win->prev->next = win;
  124. }
  125. else
  126. {
  127. if ((win->next = parent->first_child)) win->next->prev = win;
  128. else win->parent->last_child = win;
  129. parent->first_child = win;
  130. }
  131. }
  132. else /* move it to parent unlinked list */
  133. {
  134. parent = win->parent;
  135. if ((win->next = parent->first_unlinked)) win->next->prev = win;
  136. win->prev = NULL;
  137. parent->first_unlinked = win;
  138. }
  139. }
  140. /* append a user handle to a handle array */
  141. static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
  142. {
  143. if (array->count >= array->total)
  144. {
  145. int new_total = max( array->total * 2, 32 );
  146. user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
  147. if (!new_array)
  148. {
  149. free( array->handles );
  150. set_error( STATUS_NO_MEMORY );
  151. return 0;
  152. }
  153. array->handles = new_array;
  154. array->total = new_total;
  155. }
  156. array->handles[array->count++] = handle;
  157. return 1;
  158. }
  159. /* set a window property */
  160. static void set_property( struct window *win, atom_t atom, obj_handle_t handle,
  161. enum property_type type )
  162. {
  163. int i, free = -1;
  164. struct property *new_props;
  165. /* check if it exists already */
  166. for (i = 0; i < win->prop_inuse; i++)
  167. {
  168. if (win->properties[i].type == PROP_TYPE_FREE)
  169. {
  170. free = i;
  171. continue;
  172. }
  173. if (win->properties[i].atom == atom)
  174. {
  175. win->properties[i].type = type;
  176. win->properties[i].handle = handle;
  177. return;
  178. }
  179. }
  180. /* need to add an entry */
  181. if (!grab_global_atom( atom )) return;
  182. if (free == -1)
  183. {
  184. /* no free entry */
  185. if (win->prop_inuse >= win->prop_alloc)
  186. {
  187. /* need to grow the array */
  188. if (!(new_props = realloc( win->properties,
  189. sizeof(*new_props) * (win->prop_alloc + 16) )))
  190. {
  191. set_error( STATUS_NO_MEMORY );
  192. release_global_atom( atom );
  193. return;
  194. }
  195. win->prop_alloc += 16;
  196. win->properties = new_props;
  197. }
  198. free = win->prop_inuse++;
  199. }
  200. win->properties[free].atom = atom;
  201. win->properties[free].type = type;
  202. win->properties[free].handle = handle;
  203. }
  204. /* remove a window property */
  205. static obj_handle_t remove_property( struct window *win, atom_t atom )
  206. {
  207. int i;
  208. for (i = 0; i < win->prop_inuse; i++)
  209. {
  210. if (win->properties[i].type == PROP_TYPE_FREE) continue;
  211. if (win->properties[i].atom == atom)
  212. {
  213. release_global_atom( atom );
  214. win->properties[i].type = PROP_TYPE_FREE;
  215. return win->properties[i].handle;
  216. }
  217. }
  218. /* FIXME: last error? */
  219. return 0;
  220. }
  221. /* find a window property */
  222. static obj_handle_t get_property( struct window *win, atom_t atom )
  223. {
  224. int i;
  225. for (i = 0; i < win->prop_inuse; i++)
  226. {
  227. if (win->properties[i].type == PROP_TYPE_FREE) continue;
  228. if (win->properties[i].atom == atom) return win->properties[i].handle;
  229. }
  230. /* FIXME: last error? */
  231. return 0;
  232. }
  233. /* destroy all properties of a window */
  234. inline static void destroy_properties( struct window *win )
  235. {
  236. int i;
  237. if (!win->properties) return;
  238. for (i = 0; i < win->prop_inuse; i++)
  239. {
  240. if (win->properties[i].type == PROP_TYPE_FREE) continue;
  241. release_global_atom( win->properties[i].atom );
  242. }
  243. free( win->properties );
  244. }
  245. /* destroy a window */
  246. static void destroy_window( struct window *win )
  247. {
  248. assert( win != top_window );
  249. /* destroy all children */
  250. while (win->first_child) destroy_window( win->first_child );
  251. while (win->first_unlinked) destroy_window( win->first_unlinked );
  252. if (win->thread->queue)
  253. {
  254. if (win->update_region) inc_queue_paint_count( win->thread, -1 );
  255. if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( win->thread, -1 );
  256. queue_cleanup_window( win->thread, win->handle );
  257. }
  258. /* reset global window pointers, if the corresponding window is destroyed */
  259. if (win == shell_window) shell_window = NULL;
  260. if (win == shell_listview) shell_listview = NULL;
  261. if (win == progman_window) progman_window = NULL;
  262. if (win == taskman_window) taskman_window = NULL;
  263. free_user_handle( win->handle );
  264. destroy_properties( win );
  265. unlink_window( win );
  266. if (win->win_region) free_region( win->win_region );
  267. if (win->update_region) free_region( win->update_region );
  268. release_class( win->class );
  269. if (win->text) free( win->text );
  270. memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
  271. free( win );
  272. }
  273. /* create a new window structure (note: the window is not linked in the window tree) */
  274. static struct window *create_window( struct window *parent, struct window *owner,
  275. atom_t atom, void *instance )
  276. {
  277. int extra_bytes;
  278. struct window *win;
  279. struct window_class *class = grab_class( current->process, atom, instance, &extra_bytes );
  280. if (!class) return NULL;
  281. win = mem_alloc( sizeof(*win) + extra_bytes - 1 );
  282. if (!win)
  283. {
  284. release_class( class );
  285. return NULL;
  286. }
  287. if (!(win->handle = alloc_user_handle( win, USER_WINDOW )))
  288. {
  289. release_class( class );
  290. free( win );
  291. return NULL;
  292. }
  293. win->parent = parent;
  294. win->owner = owner ? owner->handle : 0;
  295. win->first_child = NULL;
  296. win->last_child = NULL;
  297. win->first_unlinked = NULL;
  298. win->thread = current;
  299. win->class = class;
  300. win->atom = atom;
  301. win->last_active = win->handle;
  302. win->win_region = NULL;
  303. win->update_region = NULL;
  304. win->style = 0;
  305. win->ex_style = 0;
  306. win->id = 0;
  307. win->instance = NULL;
  308. win->user_data = NULL;
  309. win->text = NULL;
  310. win->paint_flags = 0;
  311. win->prop_inuse = 0;
  312. win->prop_alloc = 0;
  313. win->properties = NULL;
  314. win->nb_extra_bytes = extra_bytes;
  315. memset( win->extra_bytes, 0, extra_bytes );
  316. if (parent) /* put it on parent unlinked list */
  317. {
  318. if ((win->next = parent->first_unlinked)) win->next->prev = win;
  319. win->prev = NULL;
  320. parent->first_unlinked = win;
  321. }
  322. else win->next = win->prev = NULL;
  323. /* if parent belongs to a different thread, attach the two threads */
  324. if (parent && parent->thread && parent->thread != current)
  325. attach_thread_input( current, parent->thread );
  326. return win;
  327. }
  328. /* destroy all windows belonging to a given thread */
  329. void destroy_thread_windows( struct thread *thread )
  330. {
  331. user_handle_t handle = 0;
  332. struct window *win;
  333. while ((win = next_user_handle( &handle, USER_WINDOW )))
  334. {
  335. if (win->thread != thread) continue;
  336. destroy_window( win );
  337. }
  338. }
  339. /* check whether child is a descendant of parent */
  340. int is_child_window( user_handle_t parent, user_handle_t child )
  341. {
  342. struct window *child_ptr = get_user_object( child, USER_WINDOW );
  343. struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
  344. if (!child_ptr || !parent_ptr) return 0;
  345. while (child_ptr->parent)
  346. {
  347. if (child_ptr->parent == parent_ptr) return 1;
  348. child_ptr = child_ptr->parent;
  349. }
  350. return 0;
  351. }
  352. /* check whether window is a top-level window */
  353. int is_top_level_window( user_handle_t window )
  354. {
  355. struct window *win = get_user_object( window, USER_WINDOW );
  356. return (win && win->parent == top_window);
  357. }
  358. /* make a window active if possible */
  359. int make_window_active( user_handle_t window )
  360. {
  361. struct window *owner, *win = get_window( window );
  362. if (!win) return 0;
  363. /* set last active for window and its owner */
  364. win->last_active = win->handle;
  365. if ((owner = get_user_object( win->owner, USER_WINDOW ))) owner->last_active = win->handle;
  366. return 1;
  367. }
  368. /* increment (or decrement) the window paint count */
  369. static inline void inc_window_paint_count( struct window *win, int incr )
  370. {
  371. if (win->thread) inc_queue_paint_count( win->thread, incr );
  372. }
  373. /* check if window and all its ancestors are visible */
  374. static int is_visible( const struct window *win )
  375. {
  376. while (win && win != top_window)
  377. {
  378. if (!(win->style & WS_VISIBLE)) return 0;
  379. win = win->parent;
  380. /* if parent is minimized children are not visible */
  381. if (win && (win->style & WS_MINIMIZE)) return 0;
  382. }
  383. return 1;
  384. }
  385. /* same as is_visible but takes a window handle */
  386. int is_window_visible( user_handle_t window )
  387. {
  388. struct window *win = get_user_object( window, USER_WINDOW );
  389. if (!win) return 0;
  390. return is_visible( win );
  391. }
  392. /* check if point is inside the window */
  393. static inline int is_point_in_window( struct window *win, int x, int y )
  394. {
  395. if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
  396. if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
  397. return 0; /* disabled child */
  398. if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
  399. return 0; /* transparent */
  400. if (x < win->window_rect.left || x >= win->window_rect.right ||
  401. y < win->window_rect.top || y >= win->window_rect.bottom)
  402. return 0; /* not in window */
  403. if (win->win_region &&
  404. !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
  405. return 0; /* not in window region */
  406. return 1;
  407. }
  408. /* find child of 'parent' that contains the given point (in parent-relative coords) */
  409. static struct window *child_window_from_point( struct window *parent, int x, int y )
  410. {
  411. struct window *ptr;
  412. for (ptr = parent->first_child; ptr; ptr = ptr->next)
  413. {
  414. if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
  415. /* if window is minimized or disabled, return at once */
  416. if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
  417. /* if point is not in client area, return at once */
  418. if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
  419. y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
  420. return ptr;
  421. return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
  422. }
  423. return parent; /* not found any child */
  424. }
  425. /* find all children of 'parent' that contain the given point */
  426. static int get_window_children_from_point( struct window *parent, int x, int y,
  427. struct user_handle_array *array )
  428. {
  429. struct window *ptr;
  430. for (ptr = parent->first_child; ptr; ptr = ptr->next)
  431. {
  432. if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
  433. /* if point is in client area, and window is not minimized or disabled, check children */
  434. if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) &&
  435. x >= ptr->client_rect.left && x < ptr->client_rect.right &&
  436. y >= ptr->client_rect.top && y < ptr->client_rect.bottom)
  437. {
  438. if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
  439. y - ptr->client_rect.top, array ))
  440. return 0;
  441. }
  442. /* now add window to the array */
  443. if (!add_handle_to_array( array, ptr->handle )) return 0;
  444. }
  445. return 1;
  446. }
  447. /* find window containing point (in absolute coords) */
  448. user_handle_t window_from_point( int x, int y )
  449. {
  450. struct window *ret;
  451. if (!top_window) return 0;
  452. ret = child_window_from_point( top_window, x, y );
  453. return ret->handle;
  454. }
  455. /* return list of all windows containing point (in absolute coords) */
  456. static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
  457. {
  458. struct window *ptr;
  459. /* make point relative to top window */
  460. for (ptr = top->parent; ptr && ptr != top_window; ptr = ptr->parent)
  461. {
  462. x -= ptr->client_rect.left;
  463. y -= ptr->client_rect.top;
  464. }
  465. if (!is_point_in_window( top, x, y )) return 1;
  466. /* if point is in client area, and window is not minimized or disabled, check children */
  467. if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) &&
  468. x >= top->client_rect.left && x < top->client_rect.right &&
  469. y >= top->client_rect.top && y < top->client_rect.bottom)
  470. {
  471. if (!get_window_children_from_point( top, x - top->client_rect.left,
  472. y - top->client_rect.top, array ))
  473. return 0;
  474. }
  475. /* now add window to the array */
  476. if (!add_handle_to_array( array, top->handle )) return 0;
  477. return 1;
  478. }
  479. /* return the thread owning a window */
  480. struct thread *get_window_thread( user_handle_t handle )
  481. {
  482. struct window *win = get_user_object( handle, USER_WINDOW );
  483. if (!win || !win->thread) return NULL;
  484. return (struct thread *)grab_object( win->thread );
  485. }
  486. /* check if any area of a window needs repainting */
  487. static inline int win_needs_repaint( struct window *win )
  488. {
  489. return win->update_region || (win->paint_flags & PAINT_INTERNAL);
  490. }
  491. /* find a child of the specified window that needs repainting */
  492. static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
  493. {
  494. struct window *ptr, *ret = NULL;
  495. for (ptr = parent->first_child; ptr && !ret; ptr = ptr->next)
  496. {
  497. if (!(ptr->style & WS_VISIBLE)) continue;
  498. if (ptr->thread == thread && win_needs_repaint( ptr ))
  499. ret = ptr;
  500. else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
  501. ret = find_child_to_repaint( ptr, thread );
  502. }
  503. if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
  504. {
  505. /* transparent window, check for non-transparent sibling to paint first */
  506. for (ptr = ret->next; ptr; ptr = ptr->next)
  507. {
  508. if (!(ptr->style & WS_VISIBLE)) continue;
  509. if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
  510. if (ptr->thread != thread) continue;
  511. if (win_needs_repaint( ptr )) return ptr;
  512. }
  513. }
  514. return ret;
  515. }
  516. /* find a window that needs to receive a WM_PAINT */
  517. user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
  518. {
  519. struct window *ptr, *win = find_child_to_repaint( top_window, thread );
  520. if (win)
  521. {
  522. if (!parent) return win->handle;
  523. /* check that it is a child of the specified parent */
  524. for (ptr = win; ptr; ptr = ptr->parent)
  525. if (ptr->handle == parent) return win->handle;
  526. /* otherwise don't return any window, we don't repaint a child before its parent */
  527. }
  528. return 0;
  529. }
  530. /* intersect the window region with the specified region, relative to the window parent */
  531. static struct region *intersect_window_region( struct region *region, struct window *win )
  532. {
  533. /* make region relative to window rect */
  534. offset_region( region, -win->window_rect.left, -win->window_rect.top );
  535. if (!intersect_region( region, region, win->win_region )) return NULL;
  536. /* make region relative to parent again */
  537. offset_region( region, win->window_rect.left, win->window_rect.top );
  538. return region;
  539. }
  540. /* clip all children of a given window out of the visible region */
  541. static struct region *clip_children( struct window *parent, struct window *last,
  542. struct region *region, int offset_x, int offset_y )
  543. {
  544. struct window *ptr;
  545. struct region *tmp = create_empty_region();
  546. if (!tmp) return NULL;
  547. for (ptr = parent->first_child; ptr && ptr != last; ptr = ptr->next)
  548. {
  549. if (!(ptr->style & WS_VISIBLE)) continue;
  550. if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
  551. set_region_rect( tmp, &ptr->window_rect );
  552. if (ptr->win_region && !intersect_window_region( tmp, ptr ))
  553. {
  554. free_region( tmp );
  555. return NULL;
  556. }
  557. offset_region( tmp, offset_x, offset_y );
  558. if (!(region = subtract_region( region, region, tmp ))) break;
  559. if (is_region_empty( region )) break;
  560. }
  561. free_region( tmp );
  562. return region;
  563. }
  564. /* compute the visible region of a window */
  565. static struct region *get_visible_region( struct window *win, struct window *top,
  566. unsigned int flags )
  567. {
  568. struct region *tmp, *region;
  569. int offset_x, offset_y;
  570. if (!(region = create_empty_region())) return NULL;
  571. /* first check if all ancestors are visible */
  572. if (!is_visible( win )) return region; /* empty region */
  573. /* create a region relative to the window itself */
  574. if ((flags & DCX_PARENTCLIP) && win != top && win->parent)
  575. {
  576. rectangle_t rect;
  577. rect.left = rect.top = 0;
  578. rect.right = win->parent->client_rect.right - win->parent->client_rect.left;
  579. rect.bottom = win->parent->client_rect.bottom - win->parent->client_rect.top;
  580. set_region_rect( region, &rect );
  581. offset_x = win->client_rect.left;
  582. offset_y = win->client_rect.top;
  583. }
  584. else if (flags & DCX_WINDOW)
  585. {
  586. set_region_rect( region, &win->window_rect );
  587. if (win->win_region && !intersect_window_region( region, win )) goto error;
  588. offset_x = win->window_rect.left;
  589. offset_y = win->window_rect.top;
  590. }
  591. else
  592. {
  593. set_region_rect( region, &win->client_rect );
  594. if (win->win_region && !intersect_window_region( region, win )) goto error;
  595. offset_x = win->client_rect.left;
  596. offset_y = win->client_rect.top;
  597. }
  598. offset_region( region, -offset_x, -offset_y );
  599. /* clip children */
  600. if (flags & DCX_CLIPCHILDREN)
  601. {
  602. if (!clip_children( win, NULL, region,
  603. offset_x - win->client_rect.left,
  604. offset_y - win->client_rect.top )) goto error;
  605. }
  606. /* clip siblings of ancestors */
  607. if (top && top != win && (tmp = create_empty_region()) != NULL)
  608. {
  609. offset_region( region, offset_x, offset_y ); /* make it relative to parent */
  610. while (win != top && win->parent)
  611. {
  612. if (win->style & WS_CLIPSIBLINGS)
  613. {
  614. if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
  615. if (is_region_empty( region )) break;
  616. }
  617. /* clip to parent client area */
  618. win = win->parent;
  619. offset_x += win->client_rect.left;
  620. offset_y += win->client_rect.top;
  621. offset_region( region, win->client_rect.left, win->client_rect.top );
  622. set_region_rect( tmp, &win->client_rect );
  623. if (win->win_region && !intersect_window_region( tmp, win ))
  624. {
  625. free_region( tmp );
  626. goto error;
  627. }
  628. if (!intersect_region( region, region, tmp ))
  629. {
  630. free_region( tmp );
  631. goto error;
  632. }
  633. if (is_region_empty( region )) break;
  634. }
  635. offset_region( region, -offset_x, -offset_y ); /* make it relative to target window again */
  636. free_region( tmp );
  637. }
  638. return region;
  639. error:
  640. free_region( region );
  641. return NULL;
  642. }
  643. /* get the window class of a window */
  644. struct window_class* get_window_class( user_handle_t window )
  645. {
  646. struct window *win;
  647. if (!(win = get_window( window ))) return NULL;
  648. return win->class;
  649. }
  650. /* return a copy of the specified region cropped to the window client or frame rectangle, */
  651. /* and converted from client to window coordinates. Helper for (in)validate_window. */
  652. static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
  653. {
  654. rectangle_t rect;
  655. struct region *tmp = create_empty_region();
  656. if (!tmp) return NULL;
  657. /* get bounding rect in client coords */
  658. if (frame)
  659. {
  660. rect.left = win->window_rect.left - win->client_rect.left;
  661. rect.top = win->window_rect.top - win->client_rect.top;
  662. rect.right = win->window_rect.right - win->client_rect.left;
  663. rect.bottom = win->window_rect.bottom - win->client_rect.top;
  664. }
  665. else
  666. {
  667. rect.left = 0;
  668. rect.top = 0;
  669. rect.right = win->client_rect.right - win->client_rect.left;
  670. rect.bottom = win->client_rect.bottom - win->client_rect.top;
  671. }
  672. set_region_rect( tmp, &rect );
  673. /* intersect specified region with bounding rect */
  674. if (region && !intersect_region( tmp, region, tmp )) goto done;
  675. if (is_region_empty( tmp )) goto done;
  676. /* map it to window coords */
  677. offset_region( tmp, win->client_rect.left - win->window_rect.left,
  678. win->client_rect.top - win->window_rect.top );
  679. return tmp;
  680. done:
  681. free_region( tmp );
  682. return NULL;
  683. }
  684. /* set a region as new update region for the window */
  685. static void set_update_region( struct window *win, struct region *region )
  686. {
  687. if (region && !is_region_empty( region ))
  688. {
  689. if (!win->update_region) inc_window_paint_count( win, 1 );
  690. else free_region( win->update_region );
  691. win->update_region = region;
  692. }
  693. else
  694. {
  695. if (win->update_region) inc_window_paint_count( win, -1 );
  696. win->paint_flags &= ~(PAINT_ERASE | PAINT_NONCLIENT);
  697. win->update_region = NULL;
  698. if (region) free_region( region );
  699. }
  700. }
  701. /* add a region to the update region; the passed region is freed or reused */
  702. static int add_update_region( struct window *win, struct region *region )
  703. {
  704. if (win->update_region && !union_region( region, win->update_region, region ))
  705. {
  706. free_region( region );
  707. return 0;
  708. }
  709. set_update_region( win, region );
  710. return 1;
  711. }
  712. /* validate the non client area of a window */
  713. static void validate_non_client( struct window *win )
  714. {
  715. struct region *tmp;
  716. rectangle_t rect;
  717. if (!win->update_region) return; /* nothing to do */
  718. /* get client rect in window coords */
  719. rect.left = win->client_rect.left - win->window_rect.left;
  720. rect.top = win->client_rect.top - win->window_rect.top;
  721. rect.right = win->client_rect.right - win->window_rect.left;
  722. rect.bottom = win->client_rect.bottom - win->window_rect.top;
  723. if ((tmp = create_region( &rect, 1 )))
  724. {
  725. if (intersect_region( tmp, win->update_region, tmp ))
  726. set_update_region( win, tmp );
  727. else
  728. free_region( tmp );
  729. }
  730. win->paint_flags &= ~PAINT_NONCLIENT;
  731. }
  732. /* validate a window completely so that we don't get any further paint messages for it */
  733. static void validate_whole_window( struct window *win )
  734. {
  735. set_update_region( win, NULL );
  736. if (win->paint_flags & PAINT_INTERNAL)
  737. {
  738. win->paint_flags &= ~PAINT_INTERNAL;
  739. inc_window_paint_count( win, -1 );
  740. }
  741. }
  742. /* validate the update region of a window on all parents; helper for redraw_window */
  743. static void validate_parents( struct window *child )
  744. {
  745. int offset_x = 0, offset_y = 0;
  746. struct window *win = child;
  747. struct region *tmp = NULL;
  748. if (!child->update_region) return;
  749. while (win->parent && win->parent != top_window)
  750. {
  751. /* map to parent client coords */
  752. offset_x += win->window_rect.left;
  753. offset_y += win->window_rect.top;
  754. win = win->parent;
  755. /* and now map to window coords */
  756. offset_x += win->client_rect.left - win->window_rect.left;
  757. offset_y += win->client_rect.top - win->window_rect.top;
  758. if (win->update_region && !(win->style & WS_CLIPCHILDREN))
  759. {
  760. if (!tmp && !(tmp = create_empty_region())) return;
  761. offset_region( child->update_region, offset_x, offset_y );
  762. if (subtract_region( tmp, win->update_region, child->update_region ))
  763. {
  764. set_update_region( win, tmp );
  765. tmp = NULL;
  766. }
  767. /* restore child coords */
  768. offset_region( child->update_region, -offset_x, -offset_y );
  769. }
  770. }
  771. if (tmp) free_region( tmp );
  772. }
  773. /* add/subtract a region (in client coordinates) to the update region of the window */
  774. static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
  775. {
  776. struct region *tmp;
  777. struct window *child;
  778. if (flags & RDW_INVALIDATE)
  779. {
  780. if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
  781. if (!add_update_region( win, tmp )) return;
  782. if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
  783. if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
  784. }
  785. else if (flags & RDW_VALIDATE)
  786. {
  787. if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
  788. {
  789. set_update_region( win, NULL );
  790. }
  791. else if (win->update_region)
  792. {
  793. if ((tmp = crop_region_to_win_rect( win, region, frame )))
  794. {
  795. if (!subtract_region( tmp, win->update_region, tmp ))
  796. {
  797. free_region( tmp );
  798. return;
  799. }
  800. set_update_region( win, tmp );
  801. }
  802. if (flags & RDW_NOFRAME) validate_non_client( win );
  803. if (flags & RDW_NOERASE) win->paint_flags &= ~PAINT_ERASE;
  804. }
  805. }
  806. if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
  807. {
  808. win->paint_flags |= PAINT_INTERNAL;
  809. inc_window_paint_count( win, 1 );
  810. }
  811. else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
  812. {
  813. win->paint_flags &= ~PAINT_INTERNAL;
  814. inc_window_paint_count( win, -1 );
  815. }
  816. if (flags & RDW_UPDATENOW)
  817. {
  818. validate_parents( win );
  819. flags &= ~RDW_UPDATENOW;
  820. }
  821. /* now process children recursively */
  822. if (flags & RDW_NOCHILDREN) return;
  823. if (win->style & WS_MINIMIZE) return;
  824. if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
  825. if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
  826. /* map to client coordinates */
  827. offset_region( tmp, win->window_rect.left - win->client_rect.left,
  828. win->window_rect.top - win->client_rect.top );
  829. if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
  830. for (child = win->first_child; child; child = child->next)
  831. {
  832. if (!(child->style & WS_VISIBLE)) continue;
  833. if (!rect_in_region( tmp, &child->window_rect )) continue;
  834. offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
  835. redraw_window( child, tmp, 1, flags );
  836. offset_region( tmp, child->client_rect.left, child->client_rect.top );
  837. }
  838. free_region( tmp );
  839. }
  840. /* retrieve the update flags for a window depending on the state of the update region */
  841. static unsigned int get_update_flags( struct window *win, unsigned int flags )
  842. {
  843. unsigned int ret = 0;
  844. if (flags & UPDATE_NONCLIENT)
  845. {
  846. if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
  847. }
  848. if (flags & UPDATE_ERASE)
  849. {
  850. if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
  851. }
  852. if (flags & UPDATE_PAINT)
  853. {
  854. if (win->update_region) ret |= UPDATE_PAINT;
  855. }
  856. if (flags & UPDATE_INTERNALPAINT)
  857. {
  858. if (win->paint_flags & PAINT_INTERNAL) ret |= UPDATE_INTERNALPAINT;
  859. }
  860. return ret;
  861. }
  862. /* iterate through the children of the given window until we find one with some update flags */
  863. static unsigned int get_child_update_flags( struct window *win, unsigned int flags,
  864. struct window **child )
  865. {
  866. struct window *ptr;
  867. unsigned int ret = 0;
  868. for (ptr = win->first_child; ptr && !ret; ptr = ptr->next)
  869. {
  870. if (!(ptr->style & WS_VISIBLE)) continue;
  871. if ((ret = get_update_flags( ptr, flags )) != 0)
  872. {
  873. *child = ptr;
  874. break;
  875. }
  876. if (ptr->style & WS_MINIMIZE) continue;
  877. /* Note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
  878. * here we only want to repaint children of windows that clip them, others
  879. * need to wait for WM_PAINT to be done in the parent first.
  880. */
  881. if (!(flags & UPDATE_NOCHILDREN) &&
  882. ((flags & UPDATE_ALLCHILDREN) || (ptr->style & WS_CLIPCHILDREN)))
  883. ret = get_child_update_flags( ptr, flags, child );
  884. }
  885. return ret;
  886. }
  887. /* expose a region of a window, looking for the top most parent that needs to be exposed */
  888. /* the region is in window coordinates */
  889. static void expose_window( struct window *win, struct window *top, struct region *region )
  890. {
  891. struct window *parent, *ptr;
  892. int offset_x, offset_y;
  893. /* find the top most parent that doesn't clip either siblings or children */
  894. for (parent = ptr = win; ptr != top; ptr = ptr->parent)
  895. {
  896. if (!(ptr->style & WS_CLIPCHILDREN)) parent = ptr;
  897. if (!(ptr->style & WS_CLIPSIBLINGS)) parent = ptr->parent;
  898. }
  899. if (parent == win && parent != top && win->parent)
  900. parent = win->parent; /* always go up at least one level if possible */
  901. offset_x = win->window_rect.left - win->client_rect.left;
  902. offset_y = win->window_rect.top - win->client_rect.top;
  903. for (ptr = win; ptr != parent; ptr = ptr->parent)
  904. {
  905. offset_x += ptr->client_rect.left;
  906. offset_y += ptr->client_rect.top;
  907. }
  908. offset_region( region, offset_x, offset_y );
  909. redraw_window( parent, region, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
  910. offset_region( region, -offset_x, -offset_y );
  911. }
  912. /* validate that the specified window and client rects are valid */
  913. static int validate_window_rectangles( const rectangle_t *window_rect, const rectangle_t *client_rect )
  914. {
  915. /* rectangles must be ordered properly */
  916. if (window_rect->right < window_rect->left) return 0;
  917. if (window_rect->bottom < window_rect->top) return 0;
  918. if (client_rect->right < client_rect->left) return 0;
  919. if (client_rect->bottom < client_rect->top) return 0;
  920. /* client rect must be inside window rect */
  921. if (client_rect->left < window_rect->left) return 0;
  922. if (client_rect->right > window_rect->right) return 0;
  923. if (client_rect->top < window_rect->top) return 0;
  924. if (client_rect->bottom > window_rect->bottom) return 0;
  925. return 1;
  926. }
  927. /* set the window and client rectangles, updating the update region if necessary */
  928. static void set_window_pos( struct window *win, struct window *top, struct window *previous,
  929. unsigned int swp_flags, const rectangle_t *window_rect,
  930. const rectangle_t *client_rect, const rectangle_t *valid_rects )
  931. {
  932. struct region *old_vis_rgn, *new_vis_rgn;
  933. const rectangle_t old_window_rect = win->window_rect;
  934. const rectangle_t old_client_rect = win->client_rect;
  935. /* if the window is not visible, everything is easy */
  936. if ((win->parent && !is_visible( win->parent )) ||
  937. (!(win->style & WS_VISIBLE) && !(swp_flags & SWP_SHOWWINDOW)))
  938. {
  939. win->window_rect = *window_rect;
  940. win->client_rect = *client_rect;
  941. if (!(swp_flags & SWP_NOZORDER)) link_window( win, win->parent, previous );
  942. if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
  943. else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
  944. return;
  945. }
  946. if (!(old_vis_rgn = get_visible_region( win, top, DCX_WINDOW ))) return;
  947. /* set the new window info before invalidating anything */
  948. win->window_rect = *window_rect;
  949. win->client_rect = *client_rect;
  950. if (!(swp_flags & SWP_NOZORDER)) link_window( win, win->parent, previous );
  951. if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
  952. else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
  953. if (!(new_vis_rgn = get_visible_region( win, top, DCX_WINDOW )))
  954. {
  955. free_region( old_vis_rgn );
  956. clear_error(); /* ignore error since the window info has been modified already */
  957. return;
  958. }
  959. /* expose anything revealed by the change */
  960. if (!(swp_flags & SWP_NOREDRAW))
  961. {
  962. offset_region( old_vis_rgn, old_window_rect.left - window_rect->left,
  963. old_window_rect.top - window_rect->top );
  964. if (xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
  965. expose_window( win, top, new_vis_rgn );
  966. }
  967. free_region( old_vis_rgn );
  968. if (!(win->style & WS_VISIBLE))
  969. {
  970. /* clear the update region since the window is no longer visible */
  971. validate_whole_window( win );
  972. goto done;
  973. }
  974. if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
  975. /* expose the whole non-client area if it changed in any way */
  976. if ((swp_flags & SWP_FRAMECHANGED) ||
  977. memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
  978. memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
  979. {
  980. struct region *tmp;
  981. /* subtract the valid portion of client rect from the total region */
  982. if (!memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
  983. tmp = create_region( client_rect, 1 );
  984. else if (valid_rects)
  985. tmp = create_region( &valid_rects[0], 1 );
  986. else
  987. tmp = create_empty_region();
  988. if (tmp)
  989. {
  990. set_region_rect( new_vis_rgn, window_rect );
  991. if (subtract_region( tmp, new_vis_rgn, tmp ))
  992. {
  993. offset_region( tmp, -client_rect->left, -client_rect->top );
  994. redraw_window( win, tmp, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
  995. }
  996. free_region( tmp );
  997. }
  998. }
  999. done:
  1000. free_region( new_vis_rgn );
  1001. clear_error(); /* we ignore out of memory errors once the new rects have been set */
  1002. }
  1003. /* create a window */
  1004. DECL_HANDLER(create_window)
  1005. {
  1006. struct window *win;
  1007. reply->handle = 0;
  1008. if (!req->parent) /* return desktop window */
  1009. {
  1010. if (!top_window)
  1011. {
  1012. if (!(top_window = create_window( NULL, NULL, req->atom, req->instance ))) return;
  1013. top_window->thread = NULL; /* no thread owns the desktop */
  1014. top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  1015. }
  1016. win = top_window;
  1017. }
  1018. else
  1019. {
  1020. struct window *parent, *owner = NULL;
  1021. if (!(parent = get_window( req->parent ))) return;
  1022. if (req->owner && !(owner = get_window( req->owner ))) return;
  1023. if (owner == top_window) owner = NULL;
  1024. else if (owner && parent != top_window)
  1025. {
  1026. /* an owned window must be created as top-level */
  1027. set_error( STATUS_ACCESS_DENIED );
  1028. return;
  1029. }
  1030. if (!(win = create_window( parent, owner, req->atom, req->instance ))) return;
  1031. }
  1032. reply->handle = win->handle;
  1033. reply->extra = win->nb_extra_bytes;
  1034. reply->class_ptr = get_class_client_ptr( win->class );
  1035. }
  1036. /* link a window into the tree */
  1037. DECL_HANDLER(link_window)
  1038. {
  1039. struct window *win, *parent = NULL, *previous = NULL;
  1040. if (!(win = get_window( req->handle ))) return;
  1041. if (req->parent && !(parent = get_window( req->parent ))) return;
  1042. if (win == top_window)
  1043. {
  1044. set_error( STATUS_INVALID_PARAMETER );
  1045. return;
  1046. }
  1047. reply->full_parent = parent ? parent->handle : 0;
  1048. if (parent && req->previous)
  1049. {
  1050. if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
  1051. {
  1052. previous = parent->last_child;
  1053. if (previous == win) return; /* nothing to do */
  1054. }
  1055. else
  1056. {
  1057. if (!(previous = get_window( req->previous ))) return;
  1058. /* previous must be a child of parent, and not win itself */
  1059. if (previous->parent != parent || previous == win)
  1060. {
  1061. set_error( STATUS_INVALID_PARAMETER );
  1062. return;
  1063. }
  1064. }
  1065. }
  1066. link_window( win, parent, previous );
  1067. }
  1068. /* destroy a window */
  1069. DECL_HANDLER(destroy_window)
  1070. {
  1071. struct window *win = get_window( req->handle );
  1072. if (win)
  1073. {
  1074. if (win != top_window) destroy_window( win );
  1075. else set_error( STATUS_ACCESS_DENIED );
  1076. }
  1077. }
  1078. /* set a window owner */
  1079. DECL_HANDLER(set_window_owner)
  1080. {
  1081. struct window *win = get_window( req->handle );
  1082. struct window *owner = NULL;
  1083. if (!win) return;
  1084. if (req->owner && !(owner = get_window( req->owner ))) return;
  1085. if (win == top_window)
  1086. {
  1087. set_error( STATUS_ACCESS_DENIED );
  1088. return;
  1089. }
  1090. reply->prev_owner = win->owner;
  1091. reply->full_owner = win->owner = owner ? owner->handle : 0;
  1092. }
  1093. /* get information from a window handle */
  1094. DECL_HANDLER(get_window_info)
  1095. {
  1096. struct window *win = get_window( req->handle );
  1097. reply->full_handle = 0;
  1098. reply->tid = reply->pid = 0;
  1099. if (win)
  1100. {
  1101. reply->full_handle = win->handle;
  1102. reply->last_active = win->handle;
  1103. if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
  1104. if (win->thread)
  1105. {
  1106. reply->tid = get_thread_id( win->thread );
  1107. reply->pid = get_process_id( win->thread->process );
  1108. reply->atom = get_class_atom( win->class );
  1109. }
  1110. }
  1111. }
  1112. /* set some information in a window */
  1113. DECL_HANDLER(set_window_info)
  1114. {
  1115. struct window *win = get_window( req->handle );
  1116. if (!win) return;
  1117. if (req->flags && win == top_window)
  1118. {
  1119. set_error( STATUS_ACCESS_DENIED );
  1120. return;
  1121. }
  1122. if (req->extra_size > sizeof(req->extra_value) ||
  1123. req->extra_offset < -1 ||
  1124. req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
  1125. {
  1126. set_win32_error( ERROR_INVALID_INDEX );
  1127. return;
  1128. }
  1129. if (req->extra_offset != -1)
  1130. {
  1131. memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
  1132. }
  1133. else if (req->flags & SET_WIN_EXTRA)
  1134. {
  1135. set_win32_error( ERROR_INVALID_INDEX );
  1136. return;
  1137. }
  1138. reply->old_style = win->style;
  1139. reply->old_ex_style = win->ex_style;
  1140. reply->old_id = win->id;
  1141. reply->old_instance = win->instance;
  1142. reply->old_user_data = win->user_data;
  1143. if (req->flags & SET_WIN_STYLE) win->style = req->style;
  1144. if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
  1145. if (req->flags & SET_WIN_ID) win->id = req->id;
  1146. if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
  1147. if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
  1148. if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
  1149. &req->extra_value, req->extra_size );
  1150. /* changing window style triggers a non-client paint */
  1151. if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
  1152. }
  1153. /* get a list of the window parents, up to the root of the tree */
  1154. DECL_HANDLER(get_window_parents)
  1155. {
  1156. struct window *ptr, *win = get_window( req->handle );
  1157. int total = 0;
  1158. user_handle_t *data;
  1159. size_t len;
  1160. if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
  1161. reply->count = total;
  1162. len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
  1163. if (len && ((data = set_reply_data_size( len ))))
  1164. {
  1165. for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
  1166. *data++ = ptr->handle;
  1167. }
  1168. }
  1169. /* get a list of the window children */
  1170. DECL_HANDLER(get_window_children)
  1171. {
  1172. struct window *ptr, *parent = get_window( req->parent );
  1173. int total = 0;
  1174. user_handle_t *data;
  1175. size_t len;
  1176. if (parent)
  1177. for (ptr = parent->first_child, total = 0; ptr; ptr = ptr->next)
  1178. {
  1179. if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
  1180. if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
  1181. total++;
  1182. }
  1183. reply->count = total;
  1184. len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
  1185. if (len && ((data = set_reply_data_size( len ))))
  1186. {
  1187. for (ptr = parent->first_child; ptr && len; ptr = ptr->next)
  1188. {
  1189. if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
  1190. if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
  1191. *data++ = ptr->handle;
  1192. len -= sizeof(*data);
  1193. }
  1194. }
  1195. }
  1196. /* get a list of the window children that contain a given point */
  1197. DECL_HANDLER(get_window_children_from_point)
  1198. {
  1199. struct user_handle_array array;
  1200. struct window *parent = get_window( req->parent );
  1201. size_t len;
  1202. if (!parent) return;
  1203. array.handles = NULL;
  1204. array.count = 0;
  1205. array.total = 0;
  1206. if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
  1207. reply->count = array.count;
  1208. len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
  1209. if (len) set_reply_data_ptr( array.handles, len );
  1210. else free( array.handles );
  1211. }
  1212. /* get window tree information from a window handle */
  1213. DECL_HANDLER(get_window_tree)
  1214. {
  1215. struct window *win = get_window( req->handle );
  1216. if (!win) return;
  1217. if (win->parent)
  1218. {
  1219. struct window *parent = win->parent;
  1220. reply->parent = parent->handle;
  1221. reply->owner = win->owner;
  1222. reply->next_sibling = win->next ? win->next->handle : 0;
  1223. reply->prev_sibling = win->prev ? win->prev->handle : 0;
  1224. reply->first_sibling = parent->first_child ? parent->first_child->handle : 0;
  1225. reply->last_sibling = parent->last_child ? parent->last_child->handle : 0;
  1226. }
  1227. else
  1228. {
  1229. reply->parent = 0;
  1230. reply->owner = 0;
  1231. reply->next_sibling = 0;
  1232. reply->prev_sibling = 0;
  1233. reply->first_sibling = 0;
  1234. reply->last_sibling = 0;
  1235. }
  1236. reply->first_child = win->first_child ? win->first_child->handle : 0;
  1237. reply->last_child = win->last_child ? win->last_child->handle : 0;
  1238. }
  1239. /* set the position and Z order of a window */
  1240. DECL_HANDLER(set_window_pos)
  1241. {
  1242. const rectangle_t *valid_rects = NULL;
  1243. struct window *previous = NULL;
  1244. struct window *top = top_window;
  1245. struct window *win = get_window( req->handle );
  1246. unsigned int flags = req->flags;
  1247. if (!win) return;
  1248. if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
  1249. if (req->top_win)
  1250. {
  1251. if (!(top = get_window( req->top_win ))) return;
  1252. }
  1253. if (!(flags & SWP_NOZORDER))
  1254. {
  1255. if (!req->previous) /* special case: HWND_TOP */
  1256. {
  1257. if (win->parent->first_child == win) flags |= SWP_NOZORDER;
  1258. }
  1259. else if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
  1260. {
  1261. previous = win->parent->last_child;
  1262. }
  1263. else
  1264. {
  1265. if (!(previous = get_window( req->previous ))) return;
  1266. /* previous must be a sibling */
  1267. if (previous->parent != win->parent)
  1268. {
  1269. set_error( STATUS_INVALID_PARAMETER );
  1270. return;
  1271. }
  1272. }
  1273. if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
  1274. }
  1275. if (!validate_window_rectangles( &req->window, &req->client ))
  1276. {
  1277. set_error( STATUS_INVALID_PARAMETER );
  1278. return;
  1279. }
  1280. if (get_req_data_size() >= 2 * sizeof(rectangle_t)) valid_rects = get_req_data();
  1281. set_window_pos( win, top, previous, flags, &req->window, &req->client, valid_rects );
  1282. reply->new_style = win->style;
  1283. }
  1284. /* get the window and client rectangles of a window */
  1285. DECL_HANDLER(get_window_rectangles)
  1286. {
  1287. struct window *win = get_window( req->handle );
  1288. if (win)
  1289. {
  1290. reply->window = win->window_rect;
  1291. reply->client = win->client_rect;
  1292. }
  1293. }
  1294. /* get the window text */
  1295. DECL_HANDLER(get_window_text)
  1296. {
  1297. struct window *win = get_window( req->handle );
  1298. if (win && win->text)
  1299. {
  1300. size_t len = strlenW( win->text ) * sizeof(WCHAR);
  1301. if (len > get_reply_max_size()) len = get_reply_max_size();
  1302. set_reply_data( win->text, len );
  1303. }
  1304. }
  1305. /* set the window text */
  1306. DECL_HANDLER(set_window_text)
  1307. {
  1308. struct window *win = get_window( req->handle );
  1309. if (win)
  1310. {
  1311. WCHAR *text = NULL;
  1312. size_t len = get_req_data_size() / sizeof(WCHAR);
  1313. if (len)
  1314. {
  1315. if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
  1316. memcpy( text, get_req_data(), len * sizeof(WCHAR) );
  1317. text[len] = 0;
  1318. }
  1319. if (win->text) free( win->text );
  1320. win->text = text;
  1321. }
  1322. }
  1323. /* get the coordinates offset between two windows */
  1324. DECL_HANDLER(get_windows_offset)
  1325. {
  1326. struct window *win;
  1327. reply->x = reply->y = 0;
  1328. if (req->from)
  1329. {
  1330. if (!(win = get_window( req->from ))) return;
  1331. while (win)
  1332. {
  1333. reply->x += win->client_rect.left;
  1334. reply->y += win->client_rect.top;
  1335. win = win->parent;
  1336. }
  1337. }
  1338. if (req->to)
  1339. {
  1340. if (!(win = get_window( req->to ))) return;
  1341. while (win)
  1342. {
  1343. reply->x -= win->client_rect.left;
  1344. reply->y -= win->client_rect.top;
  1345. win = win->parent;
  1346. }
  1347. }
  1348. }
  1349. /* get the visible region of a window */
  1350. DECL_HANDLER(get_visible_region)
  1351. {
  1352. struct region *region;
  1353. struct window *win = get_window( req->window );
  1354. struct window *top = NULL;
  1355. if (!win) return;
  1356. if (req->top_win && !(top = get_window( req->top_win ))) return;
  1357. if ((region = get_visible_region( win, top, req->flags )))
  1358. {
  1359. rectangle_t *data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
  1360. if (data) set_reply_data_ptr( data, reply->total_size );
  1361. }
  1362. }
  1363. /* get the window region */
  1364. DECL_HANDLER(get_window_region)
  1365. {
  1366. struct window *win = get_window( req->window );
  1367. if (!win) return;
  1368. if (win->win_region)
  1369. {
  1370. rectangle_t *data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
  1371. if (data) set_reply_data_ptr( data, reply->total_size );
  1372. }
  1373. }
  1374. /* set the window region */
  1375. DECL_HANDLER(set_window_region)
  1376. {
  1377. struct region *region = NULL;
  1378. struct window *win = get_window( req->window );
  1379. if (!win) return;
  1380. if (get_req_data_size()) /* no data means remove the region completely */
  1381. {
  1382. if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
  1383. return;
  1384. }
  1385. if (win->win_region) free_region( win->win_region );
  1386. win->win_region = region;
  1387. }
  1388. /* get a window update region */
  1389. DECL_HANDLER(get_update_region)
  1390. {
  1391. rectangle_t *data;
  1392. unsigned int flags = req->flags;
  1393. struct window *win = get_window( req->window );
  1394. reply->flags = 0;
  1395. if (!win || !is_visible( win )) return;
  1396. if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
  1397. {
  1398. /* non-client painting must be delayed if one of the parents is going to
  1399. * be repainted and doesn't clip children */
  1400. struct window *ptr;
  1401. for (ptr = win->parent; ptr && ptr != top_window; ptr = ptr->parent)
  1402. {
  1403. if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
  1404. return;
  1405. }
  1406. }
  1407. if (!(reply->flags = get_update_flags( win, flags )))
  1408. {
  1409. /* if window doesn't need any repaint, check the children */
  1410. if (!(flags & UPDATE_NOCHILDREN) &&
  1411. ((flags & UPDATE_ALLCHILDREN) || (win->style & WS_CLIPCHILDREN)))
  1412. {
  1413. reply->flags = get_child_update_flags( win, flags, &win );
  1414. }
  1415. }
  1416. reply->child = win->handle;
  1417. if (flags & UPDATE_NOREGION) return;
  1418. if (win->update_region)
  1419. {
  1420. if (!(data = get_region_data( win->update_region, get_reply_max_size(),
  1421. &reply->total_size ))) return;
  1422. set_reply_data_ptr( data, reply->total_size );
  1423. }
  1424. if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
  1425. {
  1426. validate_whole_window( win );
  1427. }
  1428. else
  1429. {
  1430. if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
  1431. if (reply->flags & UPDATE_ERASE)
  1432. {
  1433. win->paint_flags &= ~PAINT_ERASE;
  1434. /* desktop window only gets erased, not repainted */
  1435. if (win == top_window) validate_whole_window( win );
  1436. }
  1437. }
  1438. }
  1439. /* mark parts of a window as needing a redraw */
  1440. DECL_HANDLER(redraw_window)
  1441. {
  1442. struct region *region = NULL;
  1443. struct window *win = get_window( req->window );
  1444. if (!win) return;
  1445. if (!is_visible( win )) return; /* nothing to do */
  1446. if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
  1447. {
  1448. if (get_req_data_size()) /* no data means whole rectangle */
  1449. {
  1450. if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
  1451. return;
  1452. }
  1453. }
  1454. redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
  1455. req->flags );
  1456. if (region) free_region( region );
  1457. }
  1458. /* set a window property */
  1459. DECL_HANDLER(set_window_property)
  1460. {
  1461. struct window *win = get_window( req->window );
  1462. if (win) set_property( win, req->atom, req->handle,
  1463. req->string ? PROP_TYPE_STRING : PROP_TYPE_ATOM );
  1464. }
  1465. /* remove a window property */
  1466. DECL_HANDLER(remove_window_property)
  1467. {
  1468. struct window *win = get_window( req->window );
  1469. reply->handle = 0;
  1470. if (win) reply->handle = remove_property( win, req->atom );
  1471. }
  1472. /* get a window property */
  1473. DECL_HANDLER(get_window_property)
  1474. {
  1475. struct window *win = get_window( req->window );
  1476. reply->handle = 0;
  1477. if (win) reply->handle = get_property( win, req->atom );
  1478. }
  1479. /* get the list of properties of a window */
  1480. DECL_HANDLER(get_window_properties)
  1481. {
  1482. property_data_t *data;
  1483. int i, count, max = get_reply_max_size() / sizeof(*data);
  1484. struct window *win = get_window( req->window );
  1485. reply->total = 0;
  1486. if (!win) return;
  1487. for (i = count = 0; i < win->prop_inuse; i++)
  1488. if (win->properties[i].type != PROP_TYPE_FREE) count++;
  1489. reply->total = count;
  1490. if (count > max) count = max;
  1491. if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
  1492. for (i = 0; i < win->prop_inuse && count; i++)
  1493. {
  1494. if (win->properties[i].type == PROP_TYPE_FREE) continue;
  1495. data->atom = win->properties[i].atom;
  1496. data->string = (win->properties[i].type == PROP_TYPE_STRING);
  1497. data->handle = win->properties[i].handle;
  1498. data++;
  1499. count--;
  1500. }
  1501. }
  1502. /* get the new window pointer for a global window, checking permissions */
  1503. /* helper for set_global_windows request */
  1504. static int get_new_global_window( struct window **win, user_handle_t handle )
  1505. {
  1506. if (!handle)
  1507. {
  1508. *win = NULL;
  1509. return 1;
  1510. }
  1511. else if (*win)
  1512. {
  1513. set_error( STATUS_ACCESS_DENIED );
  1514. return 0;
  1515. }
  1516. *win = get_window( handle );
  1517. return (*win != NULL);
  1518. }
  1519. /* Set/get the global windows */
  1520. DECL_HANDLER(set_global_windows)
  1521. {
  1522. struct window *new_shell_window = shell_window;
  1523. struct window *new_shell_listview = shell_listview;
  1524. struct window *new_progman_window = progman_window;
  1525. struct window *new_taskman_window = taskman_window;
  1526. reply->old_shell_window = shell_window ? shell_window->handle : 0;
  1527. reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
  1528. reply->old_progman_window = progman_window ? progman_window->handle : 0;
  1529. reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
  1530. if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
  1531. {
  1532. if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
  1533. if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
  1534. }
  1535. if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
  1536. {
  1537. if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
  1538. }
  1539. if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
  1540. {
  1541. if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
  1542. }
  1543. shell_window = new_shell_window;
  1544. shell_listview = new_shell_listview;
  1545. progman_window = new_progman_window;
  1546. taskman_window = new_taskman_window;
  1547. }