window.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912
  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include "wine/port.h"
  22. #include <assert.h>
  23. #include <stdarg.h>
  24. #include "ntstatus.h"
  25. #define WIN32_NO_STATUS
  26. #include "windef.h"
  27. #include "winbase.h"
  28. #include "wingdi.h"
  29. #include "winuser.h"
  30. #include "winternl.h"
  31. #include "object.h"
  32. #include "request.h"
  33. #include "thread.h"
  34. #include "process.h"
  35. #include "user.h"
  36. #include "unicode.h"
  37. /* a window property */
  38. struct property
  39. {
  40. unsigned short type; /* property type (see below) */
  41. atom_t atom; /* property atom */
  42. lparam_t data; /* property data (user-defined storage) */
  43. };
  44. enum property_type
  45. {
  46. PROP_TYPE_FREE, /* free entry */
  47. PROP_TYPE_STRING, /* atom that was originally a string */
  48. PROP_TYPE_ATOM /* plain atom */
  49. };
  50. struct window
  51. {
  52. struct window *parent; /* parent window */
  53. user_handle_t owner; /* owner of this window */
  54. struct list children; /* list of children in Z-order */
  55. struct list unlinked; /* list of children not linked in the Z-order list */
  56. struct list entry; /* entry in parent's children list */
  57. user_handle_t handle; /* full handle for this window */
  58. struct thread *thread; /* thread owning the window */
  59. struct desktop *desktop; /* desktop that the window belongs to */
  60. struct window_class *class; /* window class */
  61. atom_t atom; /* class atom */
  62. user_handle_t last_active; /* last active popup */
  63. rectangle_t window_rect; /* window rectangle (relative to parent client area) */
  64. rectangle_t visible_rect; /* visible part of window rect (relative to parent client area) */
  65. rectangle_t surface_rect; /* window surface rectangle (relative to parent client area) */
  66. rectangle_t client_rect; /* client rectangle (relative to parent client area) */
  67. struct region *win_region; /* region for shaped windows (relative to window rect) */
  68. struct region *layer_region; /* region for layered windows (relative to window rect) */
  69. struct region *update_region; /* update region (relative to window rect) */
  70. unsigned int style; /* window style */
  71. unsigned int ex_style; /* window extended style */
  72. unsigned int id; /* window id */
  73. mod_handle_t instance; /* creator instance */
  74. unsigned int is_unicode : 1; /* ANSI or unicode */
  75. unsigned int is_linked : 1; /* is it linked into the parent z-order list? */
  76. unsigned int is_layered : 1; /* has layered info been set? */
  77. unsigned int color_key; /* color key for a layered window */
  78. unsigned int alpha; /* alpha value for a layered window */
  79. unsigned int layered_flags; /* flags for a layered window */
  80. unsigned int dpi; /* window DPI or 0 if per-monitor aware */
  81. DPI_AWARENESS dpi_awareness; /* DPI awareness mode */
  82. lparam_t user_data; /* user-specific data */
  83. WCHAR *text; /* window caption text */
  84. data_size_t text_len; /* length of window caption */
  85. unsigned int paint_flags; /* various painting flags */
  86. int prop_inuse; /* number of in-use window properties */
  87. int prop_alloc; /* number of allocated window properties */
  88. struct property *properties; /* window properties array */
  89. int nb_extra_bytes; /* number of extra bytes */
  90. char extra_bytes[1]; /* extra bytes storage */
  91. };
  92. /* flags that can be set by the client */
  93. #define PAINT_HAS_SURFACE SET_WINPOS_PAINT_SURFACE
  94. #define PAINT_HAS_PIXEL_FORMAT SET_WINPOS_PIXEL_FORMAT
  95. #define PAINT_CLIENT_FLAGS (PAINT_HAS_SURFACE | PAINT_HAS_PIXEL_FORMAT)
  96. /* flags only manipulated by the server */
  97. #define PAINT_INTERNAL 0x0010 /* internal WM_PAINT pending */
  98. #define PAINT_ERASE 0x0020 /* needs WM_ERASEBKGND */
  99. #define PAINT_NONCLIENT 0x0040 /* needs WM_NCPAINT */
  100. #define PAINT_DELAYED_ERASE 0x0080 /* still needs erase after WM_ERASEBKGND */
  101. #define PAINT_PIXEL_FORMAT_CHILD 0x0100 /* at least one child has a custom pixel format */
  102. /* growable array of user handles */
  103. struct user_handle_array
  104. {
  105. user_handle_t *handles;
  106. int count;
  107. int total;
  108. };
  109. static const rectangle_t empty_rect;
  110. /* global window pointers */
  111. static struct window *shell_window;
  112. static struct window *shell_listview;
  113. static struct window *progman_window;
  114. static struct window *taskman_window;
  115. /* magic HWND_TOP etc. pointers */
  116. #define WINPTR_TOP ((struct window *)1L)
  117. #define WINPTR_BOTTOM ((struct window *)2L)
  118. #define WINPTR_TOPMOST ((struct window *)3L)
  119. #define WINPTR_NOTOPMOST ((struct window *)4L)
  120. /* retrieve a pointer to a window from its handle */
  121. static inline struct window *get_window( user_handle_t handle )
  122. {
  123. struct window *ret = get_user_object( handle, USER_WINDOW );
  124. if (!ret) set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
  125. return ret;
  126. }
  127. /* check if window is the desktop */
  128. static inline int is_desktop_window( const struct window *win )
  129. {
  130. return !win->parent; /* only desktop windows have no parent */
  131. }
  132. /* get next window in Z-order list */
  133. static inline struct window *get_next_window( struct window *win )
  134. {
  135. struct list *ptr = list_next( &win->parent->children, &win->entry );
  136. return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
  137. }
  138. /* get previous window in Z-order list */
  139. static inline struct window *get_prev_window( struct window *win )
  140. {
  141. struct list *ptr = list_prev( &win->parent->children, &win->entry );
  142. return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
  143. }
  144. /* get first child in Z-order list */
  145. static inline struct window *get_first_child( struct window *win )
  146. {
  147. struct list *ptr = list_head( &win->children );
  148. return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
  149. }
  150. /* get last child in Z-order list */
  151. static inline struct window *get_last_child( struct window *win )
  152. {
  153. struct list *ptr = list_tail( &win->children );
  154. return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
  155. }
  156. /* set the PAINT_PIXEL_FORMAT_CHILD flag on all the parents */
  157. /* note: we never reset the flag, it's just a heuristic */
  158. static inline void update_pixel_format_flags( struct window *win )
  159. {
  160. for (win = win->parent; win && win->parent; win = win->parent)
  161. win->paint_flags |= PAINT_PIXEL_FORMAT_CHILD;
  162. }
  163. /* get the per-monitor DPI for a window */
  164. static unsigned int get_monitor_dpi( struct window *win )
  165. {
  166. /* FIXME: we return the desktop window DPI for now */
  167. while (!is_desktop_window( win )) win = win->parent;
  168. return win->dpi ? win->dpi : USER_DEFAULT_SCREEN_DPI;
  169. }
  170. /* link a window at the right place in the siblings list */
  171. static void link_window( struct window *win, struct window *previous )
  172. {
  173. if (previous == WINPTR_NOTOPMOST)
  174. {
  175. if (!(win->ex_style & WS_EX_TOPMOST) && win->is_linked) return; /* nothing to do */
  176. win->ex_style &= ~WS_EX_TOPMOST;
  177. previous = WINPTR_TOP; /* fallback to the HWND_TOP case */
  178. }
  179. list_remove( &win->entry ); /* unlink it from the previous location */
  180. if (previous == WINPTR_BOTTOM)
  181. {
  182. list_add_tail( &win->parent->children, &win->entry );
  183. win->ex_style &= ~WS_EX_TOPMOST;
  184. }
  185. else if (previous == WINPTR_TOPMOST)
  186. {
  187. list_add_head( &win->parent->children, &win->entry );
  188. win->ex_style |= WS_EX_TOPMOST;
  189. }
  190. else if (previous == WINPTR_TOP)
  191. {
  192. struct list *entry = win->parent->children.next;
  193. if (!(win->ex_style & WS_EX_TOPMOST)) /* put it above the first non-topmost window */
  194. {
  195. while (entry != &win->parent->children)
  196. {
  197. struct window *next = LIST_ENTRY( entry, struct window, entry );
  198. if (!(next->ex_style & WS_EX_TOPMOST)) break;
  199. if (next->handle == win->owner) /* keep it above owner */
  200. {
  201. win->ex_style |= WS_EX_TOPMOST;
  202. break;
  203. }
  204. entry = entry->next;
  205. }
  206. }
  207. list_add_before( entry, &win->entry );
  208. }
  209. else
  210. {
  211. list_add_after( &previous->entry, &win->entry );
  212. if (!(previous->ex_style & WS_EX_TOPMOST)) win->ex_style &= ~WS_EX_TOPMOST;
  213. else
  214. {
  215. struct window *next = get_next_window( win );
  216. if (next && (next->ex_style & WS_EX_TOPMOST)) win->ex_style |= WS_EX_TOPMOST;
  217. }
  218. }
  219. win->is_linked = 1;
  220. }
  221. /* change the parent of a window (or unlink the window if the new parent is NULL) */
  222. static int set_parent_window( struct window *win, struct window *parent )
  223. {
  224. struct window *ptr;
  225. /* make sure parent is not a child of window */
  226. for (ptr = parent; ptr; ptr = ptr->parent)
  227. {
  228. if (ptr == win)
  229. {
  230. set_error( STATUS_INVALID_PARAMETER );
  231. return 0;
  232. }
  233. }
  234. if (parent)
  235. {
  236. win->parent = parent;
  237. link_window( win, WINPTR_TOP );
  238. if (!is_desktop_window( parent ))
  239. {
  240. win->dpi = parent->dpi;
  241. win->dpi_awareness = parent->dpi_awareness;
  242. }
  243. /* if parent belongs to a different thread and the window isn't */
  244. /* top-level, attach the two threads */
  245. if (parent->thread && parent->thread != win->thread && !is_desktop_window(parent))
  246. attach_thread_input( win->thread, parent->thread );
  247. if (win->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))
  248. update_pixel_format_flags( win );
  249. }
  250. else /* move it to parent unlinked list */
  251. {
  252. list_remove( &win->entry ); /* unlink it from the previous location */
  253. list_add_head( &win->parent->unlinked, &win->entry );
  254. win->is_linked = 0;
  255. }
  256. return 1;
  257. }
  258. /* append a user handle to a handle array */
  259. static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
  260. {
  261. if (array->count >= array->total)
  262. {
  263. int new_total = max( array->total * 2, 32 );
  264. user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
  265. if (!new_array)
  266. {
  267. free( array->handles );
  268. set_error( STATUS_NO_MEMORY );
  269. return 0;
  270. }
  271. array->handles = new_array;
  272. array->total = new_total;
  273. }
  274. array->handles[array->count++] = handle;
  275. return 1;
  276. }
  277. /* set a window property */
  278. static void set_property( struct window *win, atom_t atom, lparam_t data, enum property_type type )
  279. {
  280. int i, free = -1;
  281. struct property *new_props;
  282. /* check if it exists already */
  283. for (i = 0; i < win->prop_inuse; i++)
  284. {
  285. if (win->properties[i].type == PROP_TYPE_FREE)
  286. {
  287. free = i;
  288. continue;
  289. }
  290. if (win->properties[i].atom == atom)
  291. {
  292. win->properties[i].type = type;
  293. win->properties[i].data = data;
  294. return;
  295. }
  296. }
  297. /* need to add an entry */
  298. if (!grab_global_atom( NULL, atom )) return;
  299. if (free == -1)
  300. {
  301. /* no free entry */
  302. if (win->prop_inuse >= win->prop_alloc)
  303. {
  304. /* need to grow the array */
  305. if (!(new_props = realloc( win->properties,
  306. sizeof(*new_props) * (win->prop_alloc + 16) )))
  307. {
  308. set_error( STATUS_NO_MEMORY );
  309. release_global_atom( NULL, atom );
  310. return;
  311. }
  312. win->prop_alloc += 16;
  313. win->properties = new_props;
  314. }
  315. free = win->prop_inuse++;
  316. }
  317. win->properties[free].atom = atom;
  318. win->properties[free].type = type;
  319. win->properties[free].data = data;
  320. }
  321. /* remove a window property */
  322. static lparam_t remove_property( struct window *win, atom_t atom )
  323. {
  324. int i;
  325. for (i = 0; i < win->prop_inuse; i++)
  326. {
  327. if (win->properties[i].type == PROP_TYPE_FREE) continue;
  328. if (win->properties[i].atom == atom)
  329. {
  330. release_global_atom( NULL, atom );
  331. win->properties[i].type = PROP_TYPE_FREE;
  332. return win->properties[i].data;
  333. }
  334. }
  335. /* FIXME: last error? */
  336. return 0;
  337. }
  338. /* find a window property */
  339. static lparam_t get_property( struct window *win, atom_t atom )
  340. {
  341. int i;
  342. for (i = 0; i < win->prop_inuse; i++)
  343. {
  344. if (win->properties[i].type == PROP_TYPE_FREE) continue;
  345. if (win->properties[i].atom == atom) return win->properties[i].data;
  346. }
  347. /* FIXME: last error? */
  348. return 0;
  349. }
  350. /* destroy all properties of a window */
  351. static inline void destroy_properties( struct window *win )
  352. {
  353. int i;
  354. if (!win->properties) return;
  355. for (i = 0; i < win->prop_inuse; i++)
  356. {
  357. if (win->properties[i].type == PROP_TYPE_FREE) continue;
  358. release_global_atom( NULL, win->properties[i].atom );
  359. }
  360. free( win->properties );
  361. }
  362. /* detach a window from its owner thread but keep the window around */
  363. static void detach_window_thread( struct window *win )
  364. {
  365. struct thread *thread = win->thread;
  366. if (!thread) return;
  367. if (thread->queue)
  368. {
  369. if (win->update_region) inc_queue_paint_count( thread, -1 );
  370. if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( thread, -1 );
  371. queue_cleanup_window( thread, win->handle );
  372. }
  373. assert( thread->desktop_users > 0 );
  374. thread->desktop_users--;
  375. release_class( win->class );
  376. win->class = NULL;
  377. /* don't hold a reference to the desktop so that the desktop window can be */
  378. /* destroyed when the desktop ref count reaches zero */
  379. release_object( win->desktop );
  380. win->thread = NULL;
  381. }
  382. /* get the process owning the top window of a given desktop */
  383. struct process *get_top_window_owner( struct desktop *desktop )
  384. {
  385. struct window *win = desktop->top_window;
  386. if (!win || !win->thread) return NULL;
  387. return win->thread->process;
  388. }
  389. /* get the top window size of a given desktop */
  390. void get_top_window_rectangle( struct desktop *desktop, rectangle_t *rect )
  391. {
  392. struct window *win = desktop->top_window;
  393. *rect = win ? win->window_rect : empty_rect;
  394. }
  395. /* post a message to the desktop window */
  396. void post_desktop_message( struct desktop *desktop, unsigned int message,
  397. lparam_t wparam, lparam_t lparam )
  398. {
  399. struct window *win = desktop->top_window;
  400. if (win && win->thread) post_message( win->handle, message, wparam, lparam );
  401. }
  402. /* create a new window structure (note: the window is not linked in the window tree) */
  403. static struct window *create_window( struct window *parent, struct window *owner,
  404. atom_t atom, mod_handle_t instance )
  405. {
  406. int extra_bytes;
  407. struct window *win = NULL;
  408. struct desktop *desktop;
  409. struct window_class *class;
  410. if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
  411. if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
  412. {
  413. release_object( desktop );
  414. return NULL;
  415. }
  416. if (!parent) /* null parent is only allowed for desktop or HWND_MESSAGE top window */
  417. {
  418. if (is_desktop_class( class ))
  419. parent = desktop->top_window; /* use existing desktop if any */
  420. else if (is_hwnd_message_class( class ))
  421. /* use desktop window if message window is already created */
  422. parent = desktop->msg_window ? desktop->top_window : NULL;
  423. else if (!(parent = desktop->top_window)) /* must already have a desktop then */
  424. {
  425. set_error( STATUS_ACCESS_DENIED );
  426. goto failed;
  427. }
  428. }
  429. /* parent must be on the same desktop */
  430. if (parent && parent->desktop != desktop)
  431. {
  432. set_error( STATUS_ACCESS_DENIED );
  433. goto failed;
  434. }
  435. if (!(win = mem_alloc( sizeof(*win) + extra_bytes - 1 ))) goto failed;
  436. if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
  437. win->parent = parent;
  438. win->owner = owner ? owner->handle : 0;
  439. win->thread = current;
  440. win->desktop = desktop;
  441. win->class = class;
  442. win->atom = atom;
  443. win->last_active = win->handle;
  444. win->win_region = NULL;
  445. win->layer_region = NULL;
  446. win->update_region = NULL;
  447. win->style = 0;
  448. win->ex_style = 0;
  449. win->id = 0;
  450. win->instance = 0;
  451. win->is_unicode = 1;
  452. win->is_linked = 0;
  453. win->is_layered = 0;
  454. win->dpi_awareness = DPI_AWARENESS_PER_MONITOR_AWARE;
  455. win->dpi = 0;
  456. win->user_data = 0;
  457. win->text = NULL;
  458. win->text_len = 0;
  459. win->paint_flags = 0;
  460. win->prop_inuse = 0;
  461. win->prop_alloc = 0;
  462. win->properties = NULL;
  463. win->nb_extra_bytes = extra_bytes;
  464. win->window_rect = win->visible_rect = win->surface_rect = win->client_rect = empty_rect;
  465. memset( win->extra_bytes, 0, extra_bytes );
  466. list_init( &win->children );
  467. list_init( &win->unlinked );
  468. /* if parent belongs to a different thread and the window isn't */
  469. /* top-level, attach the two threads */
  470. if (parent && parent->thread && parent->thread != current && !is_desktop_window(parent))
  471. {
  472. if (!attach_thread_input( current, parent->thread )) goto failed;
  473. }
  474. else /* otherwise just make sure that the thread has a message queue */
  475. {
  476. if (!current->queue && !init_thread_queue( current )) goto failed;
  477. }
  478. /* put it on parent unlinked list */
  479. if (parent) list_add_head( &parent->unlinked, &win->entry );
  480. else
  481. {
  482. list_init( &win->entry );
  483. if (is_desktop_class( class ))
  484. {
  485. assert( !desktop->top_window );
  486. desktop->top_window = win;
  487. set_process_default_desktop( current->process, desktop, current->desktop );
  488. }
  489. else
  490. {
  491. assert( !desktop->msg_window );
  492. desktop->msg_window = win;
  493. }
  494. }
  495. current->desktop_users++;
  496. return win;
  497. failed:
  498. if (win)
  499. {
  500. if (win->handle) free_user_handle( win->handle );
  501. free( win );
  502. }
  503. release_object( desktop );
  504. release_class( class );
  505. return NULL;
  506. }
  507. /* destroy all windows belonging to a given thread */
  508. void destroy_thread_windows( struct thread *thread )
  509. {
  510. user_handle_t handle = 0;
  511. struct window *win;
  512. while ((win = next_user_handle( &handle, USER_WINDOW )))
  513. {
  514. if (win->thread != thread) continue;
  515. if (is_desktop_window( win )) detach_window_thread( win );
  516. else destroy_window( win );
  517. }
  518. }
  519. /* get the desktop window */
  520. static struct window *get_desktop_window( struct thread *thread )
  521. {
  522. struct window *top_window;
  523. struct desktop *desktop = get_thread_desktop( thread, 0 );
  524. if (!desktop) return NULL;
  525. top_window = desktop->top_window;
  526. release_object( desktop );
  527. return top_window;
  528. }
  529. /* check whether child is a descendant of parent */
  530. int is_child_window( user_handle_t parent, user_handle_t child )
  531. {
  532. struct window *child_ptr = get_user_object( child, USER_WINDOW );
  533. struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
  534. if (!child_ptr || !parent_ptr) return 0;
  535. while (child_ptr->parent)
  536. {
  537. if (child_ptr->parent == parent_ptr) return 1;
  538. child_ptr = child_ptr->parent;
  539. }
  540. return 0;
  541. }
  542. /* check if window can be set as foreground window */
  543. int is_valid_foreground_window( user_handle_t window )
  544. {
  545. struct window *win = get_user_object( window, USER_WINDOW );
  546. return win && (win->style & (WS_POPUP|WS_CHILD)) != WS_CHILD;
  547. }
  548. /* make a window active if possible */
  549. int make_window_active( user_handle_t window )
  550. {
  551. struct window *owner, *win = get_window( window );
  552. if (!win) return 0;
  553. /* set last active for window and its owners */
  554. owner = win;
  555. while (owner)
  556. {
  557. owner->last_active = win->handle;
  558. owner = get_user_object( owner->owner, USER_WINDOW );
  559. }
  560. return 1;
  561. }
  562. /* increment (or decrement) the window paint count */
  563. static inline void inc_window_paint_count( struct window *win, int incr )
  564. {
  565. if (win->thread) inc_queue_paint_count( win->thread, incr );
  566. }
  567. /* map a point between different DPI scaling levels */
  568. static void map_dpi_point( struct window *win, int *x, int *y, unsigned int from, unsigned int to )
  569. {
  570. if (!from) from = get_monitor_dpi( win );
  571. if (!to) to = get_monitor_dpi( win );
  572. if (from == to) return;
  573. *x = scale_dpi( *x, from, to );
  574. *y = scale_dpi( *y, from, to );
  575. }
  576. /* map a window rectangle between different DPI scaling levels */
  577. static void map_dpi_rect( struct window *win, rectangle_t *rect, unsigned int from, unsigned int to )
  578. {
  579. if (!from) from = get_monitor_dpi( win );
  580. if (!to) to = get_monitor_dpi( win );
  581. if (from == to) return;
  582. scale_dpi_rect( rect, from, to );
  583. }
  584. /* map a region between different DPI scaling levels */
  585. static void map_dpi_region( struct window *win, struct region *region, unsigned int from, unsigned int to )
  586. {
  587. if (!from) from = get_monitor_dpi( win );
  588. if (!to) to = get_monitor_dpi( win );
  589. if (from == to) return;
  590. scale_region( region, from, to );
  591. }
  592. /* convert coordinates from client to screen coords */
  593. static inline void client_to_screen( struct window *win, int *x, int *y )
  594. {
  595. for ( ; win && !is_desktop_window(win); win = win->parent)
  596. {
  597. *x += win->client_rect.left;
  598. *y += win->client_rect.top;
  599. }
  600. }
  601. /* convert coordinates from screen to client coords and dpi */
  602. static void screen_to_client( struct window *win, int *x, int *y, unsigned int dpi )
  603. {
  604. int offset_x = 0, offset_y = 0;
  605. if (is_desktop_window( win )) return;
  606. client_to_screen( win, &offset_x, &offset_y );
  607. map_dpi_point( win, x, y, dpi, win->dpi );
  608. *x -= offset_x;
  609. *y -= offset_y;
  610. }
  611. /* check if window and all its ancestors are visible */
  612. static int is_visible( const struct window *win )
  613. {
  614. while (win)
  615. {
  616. if (!(win->style & WS_VISIBLE)) return 0;
  617. win = win->parent;
  618. /* if parent is minimized children are not visible */
  619. if (win && (win->style & WS_MINIMIZE)) return 0;
  620. }
  621. return 1;
  622. }
  623. /* same as is_visible but takes a window handle */
  624. int is_window_visible( user_handle_t window )
  625. {
  626. struct window *win = get_user_object( window, USER_WINDOW );
  627. if (!win) return 0;
  628. return is_visible( win );
  629. }
  630. int is_window_transparent( user_handle_t window )
  631. {
  632. struct window *win = get_user_object( window, USER_WINDOW );
  633. if (!win) return 0;
  634. return (win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT);
  635. }
  636. /* check if point is inside the window, and map to window dpi */
  637. static int is_point_in_window( struct window *win, int *x, int *y, unsigned int dpi )
  638. {
  639. if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
  640. if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
  641. return 0; /* disabled child */
  642. if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
  643. return 0; /* transparent */
  644. map_dpi_point( win, x, y, dpi, win->dpi );
  645. if (!point_in_rect( &win->visible_rect, *x, *y ))
  646. return 0; /* not in window */
  647. if (win->win_region &&
  648. !point_in_region( win->win_region, *x - win->window_rect.left, *y - win->window_rect.top ))
  649. return 0; /* not in window region */
  650. if (win->layer_region &&
  651. !point_in_region( win->layer_region, *x - win->window_rect.left, *y - win->window_rect.top ))
  652. return 0; /* not in layer mask region */
  653. return 1;
  654. }
  655. /* fill an array with the handles of the children of a specified window */
  656. static unsigned int get_children_windows( struct window *parent, atom_t atom, thread_id_t tid,
  657. user_handle_t *handles, unsigned int max_count )
  658. {
  659. struct window *ptr;
  660. unsigned int count = 0;
  661. if (!parent) return 0;
  662. LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
  663. {
  664. if (atom && get_class_atom(ptr->class) != atom) continue;
  665. if (tid && get_thread_id(ptr->thread) != tid) continue;
  666. if (handles)
  667. {
  668. if (count >= max_count) break;
  669. handles[count] = ptr->handle;
  670. }
  671. count++;
  672. }
  673. return count;
  674. }
  675. /* find child of 'parent' that contains the given point (in parent-relative coords) */
  676. static struct window *child_window_from_point( struct window *parent, int x, int y )
  677. {
  678. struct window *ptr;
  679. LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
  680. {
  681. int x_child = x, y_child = y;
  682. if (!is_point_in_window( ptr, &x_child, &y_child, parent->dpi )) continue; /* skip it */
  683. /* if window is minimized or disabled, return at once */
  684. if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
  685. /* if point is not in client area, return at once */
  686. if (!point_in_rect( &ptr->client_rect, x_child, y_child )) return ptr;
  687. return child_window_from_point( ptr, x_child - ptr->client_rect.left,
  688. y_child - ptr->client_rect.top );
  689. }
  690. return parent; /* not found any child */
  691. }
  692. /* find all children of 'parent' that contain the given point */
  693. static int get_window_children_from_point( struct window *parent, int x, int y,
  694. struct user_handle_array *array )
  695. {
  696. struct window *ptr;
  697. LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
  698. {
  699. int x_child = x, y_child = y;
  700. if (!is_point_in_window( ptr, &x_child, &y_child, parent->dpi )) continue; /* skip it */
  701. /* if point is in client area, and window is not minimized or disabled, check children */
  702. if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) && point_in_rect( &ptr->client_rect, x_child, y_child ))
  703. {
  704. if (!get_window_children_from_point( ptr, x_child - ptr->client_rect.left,
  705. y_child - ptr->client_rect.top, array ))
  706. return 0;
  707. }
  708. /* now add window to the array */
  709. if (!add_handle_to_array( array, ptr->handle )) return 0;
  710. }
  711. return 1;
  712. }
  713. /* get handle of root of top-most window containing point */
  714. user_handle_t shallow_window_from_point( struct desktop *desktop, int x, int y )
  715. {
  716. struct window *ptr;
  717. if (!desktop->top_window) return 0;
  718. LIST_FOR_EACH_ENTRY( ptr, &desktop->top_window->children, struct window, entry )
  719. {
  720. int x_child = x, y_child = y;
  721. if (!is_point_in_window( ptr, &x_child, &y_child, 0 )) continue; /* skip it */
  722. return ptr->handle;
  723. }
  724. return desktop->top_window->handle;
  725. }
  726. /* return thread of top-most window containing point (in absolute coords) */
  727. struct thread *window_thread_from_point( user_handle_t scope, int x, int y )
  728. {
  729. struct window *win = get_user_object( scope, USER_WINDOW );
  730. if (!win) return NULL;
  731. screen_to_client( win, &x, &y, 0 );
  732. win = child_window_from_point( win, x, y );
  733. if (!win->thread) return NULL;
  734. return (struct thread *)grab_object( win->thread );
  735. }
  736. /* return list of all windows containing point (in absolute coords) */
  737. static int all_windows_from_point( struct window *top, int x, int y, unsigned int dpi,
  738. struct user_handle_array *array )
  739. {
  740. if (!is_desktop_window( top ) && !is_desktop_window( top->parent ))
  741. {
  742. screen_to_client( top->parent, &x, &y, dpi );
  743. dpi = top->parent->dpi;
  744. }
  745. if (!is_point_in_window( top, &x, &y, dpi )) return 1;
  746. /* if point is in client area, and window is not minimized or disabled, check children */
  747. if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) && point_in_rect( &top->client_rect, x, y ))
  748. {
  749. if (!is_desktop_window(top))
  750. {
  751. x -= top->client_rect.left;
  752. y -= top->client_rect.top;
  753. }
  754. if (!get_window_children_from_point( top, x, y, array )) return 0;
  755. }
  756. /* now add window to the array */
  757. if (!add_handle_to_array( array, top->handle )) return 0;
  758. return 1;
  759. }
  760. /* return the thread owning a window */
  761. struct thread *get_window_thread( user_handle_t handle )
  762. {
  763. struct window *win = get_user_object( handle, USER_WINDOW );
  764. if (!win || !win->thread) return NULL;
  765. return (struct thread *)grab_object( win->thread );
  766. }
  767. /* check if any area of a window needs repainting */
  768. static inline int win_needs_repaint( struct window *win )
  769. {
  770. return win->update_region || (win->paint_flags & PAINT_INTERNAL);
  771. }
  772. /* find a child of the specified window that needs repainting */
  773. static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
  774. {
  775. struct window *ptr, *ret = NULL;
  776. LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
  777. {
  778. if (!(ptr->style & WS_VISIBLE)) continue;
  779. if (ptr->thread == thread && win_needs_repaint( ptr ))
  780. ret = ptr;
  781. else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
  782. ret = find_child_to_repaint( ptr, thread );
  783. if (ret) break;
  784. }
  785. if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
  786. {
  787. /* transparent window, check for non-transparent sibling to paint first */
  788. for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
  789. {
  790. if (!(ptr->style & WS_VISIBLE)) continue;
  791. if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
  792. if (ptr->thread != thread) continue;
  793. if (win_needs_repaint( ptr )) return ptr;
  794. }
  795. }
  796. return ret;
  797. }
  798. /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
  799. user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
  800. {
  801. struct window *ptr, *win, *top_window = get_desktop_window( thread );
  802. if (!top_window) return 0;
  803. if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
  804. else win = find_child_to_repaint( top_window, thread );
  805. if (win && parent)
  806. {
  807. /* check that it is a child of the specified parent */
  808. for (ptr = win; ptr; ptr = ptr->parent)
  809. if (ptr->handle == parent) break;
  810. /* otherwise don't return any window, we don't repaint a child before its parent */
  811. if (!ptr) win = NULL;
  812. }
  813. if (!win) return 0;
  814. win->paint_flags &= ~PAINT_INTERNAL;
  815. return win->handle;
  816. }
  817. /* intersect the window region with the specified region, relative to the window parent */
  818. static struct region *intersect_window_region( struct region *region, struct window *win )
  819. {
  820. /* make region relative to window rect */
  821. offset_region( region, -win->window_rect.left, -win->window_rect.top );
  822. if (!intersect_region( region, region, win->win_region )) return NULL;
  823. /* make region relative to parent again */
  824. offset_region( region, win->window_rect.left, win->window_rect.top );
  825. return region;
  826. }
  827. /* convert coordinates from client to screen coords */
  828. static inline void client_to_screen_rect( struct window *win, rectangle_t *rect )
  829. {
  830. for ( ; win && !is_desktop_window(win); win = win->parent)
  831. offset_rect( rect, win->client_rect.left, win->client_rect.top );
  832. }
  833. /* map the region from window to screen coordinates */
  834. static inline void map_win_region_to_screen( struct window *win, struct region *region )
  835. {
  836. if (!is_desktop_window(win))
  837. {
  838. int x = win->window_rect.left;
  839. int y = win->window_rect.top;
  840. client_to_screen( win->parent, &x, &y );
  841. offset_region( region, x, y );
  842. }
  843. }
  844. /* clip all children of a given window out of the visible region */
  845. static struct region *clip_children( struct window *parent, struct window *last,
  846. struct region *region, int offset_x, int offset_y )
  847. {
  848. struct window *ptr;
  849. struct region *tmp = create_empty_region();
  850. if (!tmp) return NULL;
  851. LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
  852. {
  853. if (ptr == last) break;
  854. if (!(ptr->style & WS_VISIBLE)) continue;
  855. if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
  856. set_region_rect( tmp, &ptr->visible_rect );
  857. if (ptr->win_region && !intersect_window_region( tmp, ptr ))
  858. {
  859. free_region( tmp );
  860. return NULL;
  861. }
  862. offset_region( tmp, offset_x, offset_y );
  863. if (!(region = subtract_region( region, region, tmp ))) break;
  864. if (is_region_empty( region )) break;
  865. }
  866. free_region( tmp );
  867. return region;
  868. }
  869. /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
  870. static void set_region_client_rect( struct region *region, struct window *win )
  871. {
  872. rectangle_t rect;
  873. intersect_rect( &rect, &win->window_rect, &win->client_rect );
  874. intersect_rect( &rect, &rect, &win->surface_rect );
  875. set_region_rect( region, &rect );
  876. }
  877. /* set the region to the visible rect clipped by the window surface, in parent-relative coordinates */
  878. static void set_region_visible_rect( struct region *region, struct window *win )
  879. {
  880. rectangle_t rect;
  881. intersect_rect( &rect, &win->visible_rect, &win->surface_rect );
  882. set_region_rect( region, &rect );
  883. }
  884. /* get the top-level window to clip against for a given window */
  885. static inline struct window *get_top_clipping_window( struct window *win )
  886. {
  887. while (!(win->paint_flags & PAINT_HAS_SURFACE) && win->parent && !is_desktop_window(win->parent))
  888. win = win->parent;
  889. return win;
  890. }
  891. /* compute the visible region of a window, in window coordinates */
  892. static struct region *get_visible_region( struct window *win, unsigned int flags )
  893. {
  894. struct region *tmp = NULL, *region;
  895. int offset_x, offset_y;
  896. if (!(region = create_empty_region())) return NULL;
  897. /* first check if all ancestors are visible */
  898. if (!is_visible( win )) return region; /* empty region */
  899. if (is_desktop_window( win ))
  900. {
  901. set_region_rect( region, &win->window_rect );
  902. return region;
  903. }
  904. /* create a region relative to the window itself */
  905. if ((flags & DCX_PARENTCLIP) && !is_desktop_window( win->parent ))
  906. {
  907. set_region_client_rect( region, win->parent );
  908. offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
  909. }
  910. else if (flags & DCX_WINDOW)
  911. {
  912. set_region_visible_rect( region, win );
  913. if (win->win_region && !intersect_window_region( region, win )) goto error;
  914. }
  915. else
  916. {
  917. set_region_client_rect( region, win );
  918. if (win->win_region && !intersect_window_region( region, win )) goto error;
  919. }
  920. /* clip children */
  921. if (flags & DCX_CLIPCHILDREN)
  922. {
  923. if (!clip_children( win, NULL, region, win->client_rect.left, win->client_rect.top )) goto error;
  924. }
  925. /* clip siblings of ancestors */
  926. offset_x = win->window_rect.left;
  927. offset_y = win->window_rect.top;
  928. if ((tmp = create_empty_region()) != NULL)
  929. {
  930. while (!is_desktop_window( win->parent ))
  931. {
  932. /* we don't clip out top-level siblings as that's up to the native windowing system */
  933. if (win->style & WS_CLIPSIBLINGS)
  934. {
  935. if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
  936. if (is_region_empty( region )) break;
  937. }
  938. /* clip to parent client area */
  939. win = win->parent;
  940. offset_x += win->client_rect.left;
  941. offset_y += win->client_rect.top;
  942. offset_region( region, win->client_rect.left, win->client_rect.top );
  943. set_region_client_rect( tmp, win );
  944. if (win->win_region && !intersect_window_region( tmp, win )) goto error;
  945. if (!intersect_region( region, region, tmp )) goto error;
  946. if (is_region_empty( region )) break;
  947. }
  948. free_region( tmp );
  949. }
  950. offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
  951. return region;
  952. error:
  953. if (tmp) free_region( tmp );
  954. free_region( region );
  955. return NULL;
  956. }
  957. /* clip all children with a custom pixel format out of the visible region */
  958. static struct region *clip_pixel_format_children( struct window *parent, struct region *parent_clip,
  959. struct region *region, int offset_x, int offset_y )
  960. {
  961. struct window *ptr;
  962. struct region *clip = create_empty_region();
  963. if (!clip) return NULL;
  964. LIST_FOR_EACH_ENTRY_REV( ptr, &parent->children, struct window, entry )
  965. {
  966. if (!(ptr->style & WS_VISIBLE)) continue;
  967. if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
  968. /* add the visible rect */
  969. set_region_rect( clip, &ptr->visible_rect );
  970. if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
  971. offset_region( clip, offset_x, offset_y );
  972. if (!intersect_region( clip, clip, parent_clip )) break;
  973. if (!union_region( region, region, clip )) break;
  974. if (!(ptr->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))) continue;
  975. /* subtract the client rect if it uses a custom pixel format */
  976. set_region_rect( clip, &ptr->client_rect );
  977. if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
  978. offset_region( clip, offset_x, offset_y );
  979. if (!intersect_region( clip, clip, parent_clip )) break;
  980. if ((ptr->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
  981. break;
  982. if (!clip_pixel_format_children( ptr, clip, region, offset_x + ptr->client_rect.left,
  983. offset_y + ptr->client_rect.top ))
  984. break;
  985. }
  986. free_region( clip );
  987. return region;
  988. }
  989. /* compute the visible surface region of a window, in parent coordinates */
  990. static struct region *get_surface_region( struct window *win )
  991. {
  992. struct region *region, *clip;
  993. int offset_x, offset_y;
  994. /* create a region relative to the window itself */
  995. if (!(region = create_empty_region())) return NULL;
  996. if (!(clip = create_empty_region())) goto error;
  997. set_region_rect( region, &win->visible_rect );
  998. if (win->win_region && !intersect_window_region( region, win )) goto error;
  999. set_region_rect( clip, &win->client_rect );
  1000. if (win->win_region && !intersect_window_region( clip, win )) goto error;
  1001. if ((win->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
  1002. goto error;
  1003. /* clip children */
  1004. if (!is_desktop_window(win))
  1005. {
  1006. offset_x = win->client_rect.left;
  1007. offset_y = win->client_rect.top;
  1008. }
  1009. else offset_x = offset_y = 0;
  1010. if (!clip_pixel_format_children( win, clip, region, offset_x, offset_y )) goto error;
  1011. free_region( clip );
  1012. return region;
  1013. error:
  1014. if (clip) free_region( clip );
  1015. free_region( region );
  1016. return NULL;
  1017. }
  1018. /* get the window class of a window */
  1019. struct window_class* get_window_class( user_handle_t window )
  1020. {
  1021. struct window *win;
  1022. if (!(win = get_window( window ))) return NULL;
  1023. if (!win->class) set_error( STATUS_ACCESS_DENIED );
  1024. return win->class;
  1025. }
  1026. /* determine the window visible rectangle, i.e. window or client rect cropped by parent rects */
  1027. /* the returned rectangle is in window coordinates; return 0 if rectangle is empty */
  1028. static int get_window_visible_rect( struct window *win, rectangle_t *rect, int frame )
  1029. {
  1030. int offset_x = win->window_rect.left, offset_y = win->window_rect.top;
  1031. *rect = frame ? win->window_rect : win->client_rect;
  1032. if (!(win->style & WS_VISIBLE)) return 0;
  1033. if (is_desktop_window( win )) return 1;
  1034. while (!is_desktop_window( win->parent ))
  1035. {
  1036. win = win->parent;
  1037. if (!(win->style & WS_VISIBLE) || win->style & WS_MINIMIZE) return 0;
  1038. offset_x += win->client_rect.left;
  1039. offset_y += win->client_rect.top;
  1040. offset_rect( rect, win->client_rect.left, win->client_rect.top );
  1041. if (!intersect_rect( rect, rect, &win->client_rect )) return 0;
  1042. if (!intersect_rect( rect, rect, &win->window_rect )) return 0;
  1043. }
  1044. offset_rect( rect, -offset_x, -offset_y );
  1045. return 1;
  1046. }
  1047. /* return a copy of the specified region cropped to the window client or frame rectangle, */
  1048. /* and converted from client to window coordinates. Helper for (in)validate_window. */
  1049. static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
  1050. {
  1051. rectangle_t rect;
  1052. struct region *tmp;
  1053. if (!get_window_visible_rect( win, &rect, frame )) return NULL;
  1054. if (!(tmp = create_empty_region())) return NULL;
  1055. set_region_rect( tmp, &rect );
  1056. if (region)
  1057. {
  1058. /* map it to client coords */
  1059. offset_region( tmp, win->window_rect.left - win->client_rect.left,
  1060. win->window_rect.top - win->client_rect.top );
  1061. /* intersect specified region with bounding rect */
  1062. if (!intersect_region( tmp, region, tmp )) goto done;
  1063. if (is_region_empty( tmp )) goto done;
  1064. /* map it back to window coords */
  1065. offset_region( tmp, win->client_rect.left - win->window_rect.left,
  1066. win->client_rect.top - win->window_rect.top );
  1067. }
  1068. return tmp;
  1069. done:
  1070. free_region( tmp );
  1071. return NULL;
  1072. }
  1073. /* set a region as new update region for the window */
  1074. static void set_update_region( struct window *win, struct region *region )
  1075. {
  1076. if (region && !is_region_empty( region ))
  1077. {
  1078. if (!win->update_region) inc_window_paint_count( win, 1 );
  1079. else free_region( win->update_region );
  1080. win->update_region = region;
  1081. }
  1082. else
  1083. {
  1084. if (win->update_region)
  1085. {
  1086. inc_window_paint_count( win, -1 );
  1087. free_region( win->update_region );
  1088. }
  1089. win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE | PAINT_NONCLIENT);
  1090. win->update_region = NULL;
  1091. if (region) free_region( region );
  1092. }
  1093. }
  1094. /* add a region to the update region; the passed region is freed or reused */
  1095. static int add_update_region( struct window *win, struct region *region )
  1096. {
  1097. if (win->update_region && !union_region( region, win->update_region, region ))
  1098. {
  1099. free_region( region );
  1100. return 0;
  1101. }
  1102. set_update_region( win, region );
  1103. return 1;
  1104. }
  1105. /* crop the update region of children to the specified rectangle, in client coords */
  1106. static void crop_children_update_region( struct window *win, rectangle_t *rect )
  1107. {
  1108. struct window *child;
  1109. struct region *tmp;
  1110. rectangle_t child_rect;
  1111. LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
  1112. {
  1113. if (!(child->style & WS_VISIBLE)) continue;
  1114. if (!rect) /* crop everything out */
  1115. {
  1116. crop_children_update_region( child, NULL );
  1117. set_update_region( child, NULL );
  1118. continue;
  1119. }
  1120. /* nothing to do if child is completely inside rect */
  1121. if (child->window_rect.left >= rect->left &&
  1122. child->window_rect.top >= rect->top &&
  1123. child->window_rect.right <= rect->right &&
  1124. child->window_rect.bottom <= rect->bottom) continue;
  1125. /* map to child client coords and crop grand-children */
  1126. child_rect = *rect;
  1127. offset_rect( &child_rect, -child->client_rect.left, -child->client_rect.top );
  1128. crop_children_update_region( child, &child_rect );
  1129. /* now crop the child itself */
  1130. if (!child->update_region) continue;
  1131. if (!(tmp = create_empty_region())) continue;
  1132. set_region_rect( tmp, rect );
  1133. offset_region( tmp, -child->window_rect.left, -child->window_rect.top );
  1134. if (intersect_region( tmp, child->update_region, tmp )) set_update_region( child, tmp );
  1135. else free_region( tmp );
  1136. }
  1137. }
  1138. /* validate the non client area of a window */
  1139. static void validate_non_client( struct window *win )
  1140. {
  1141. struct region *tmp;
  1142. rectangle_t rect;
  1143. if (!win->update_region) return; /* nothing to do */
  1144. /* get client rect in window coords */
  1145. rect.left = win->client_rect.left - win->window_rect.left;
  1146. rect.top = win->client_rect.top - win->window_rect.top;
  1147. rect.right = win->client_rect.right - win->window_rect.left;
  1148. rect.bottom = win->client_rect.bottom - win->window_rect.top;
  1149. if ((tmp = create_empty_region()))
  1150. {
  1151. set_region_rect( tmp, &rect );
  1152. if (intersect_region( tmp, win->update_region, tmp ))
  1153. set_update_region( win, tmp );
  1154. else
  1155. free_region( tmp );
  1156. }
  1157. win->paint_flags &= ~PAINT_NONCLIENT;
  1158. }
  1159. /* validate a window completely so that we don't get any further paint messages for it */
  1160. static void validate_whole_window( struct window *win )
  1161. {
  1162. set_update_region( win, NULL );
  1163. if (win->paint_flags & PAINT_INTERNAL)
  1164. {
  1165. win->paint_flags &= ~PAINT_INTERNAL;
  1166. inc_window_paint_count( win, -1 );
  1167. }
  1168. }
  1169. /* validate a window's children so that we don't get any further paint messages for it */
  1170. static void validate_children( struct window *win )
  1171. {
  1172. struct window *child;
  1173. LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
  1174. {
  1175. if (!(child->style & WS_VISIBLE)) continue;
  1176. validate_children(child);
  1177. validate_whole_window(child);
  1178. }
  1179. }
  1180. /* validate the update region of a window on all parents; helper for get_update_region */
  1181. static void validate_parents( struct window *child )
  1182. {
  1183. int offset_x = 0, offset_y = 0;
  1184. struct window *win = child;
  1185. struct region *tmp = NULL;
  1186. if (!child->update_region) return;
  1187. while (win->parent)
  1188. {
  1189. /* map to parent client coords */
  1190. offset_x += win->window_rect.left;
  1191. offset_y += win->window_rect.top;
  1192. win = win->parent;
  1193. /* and now map to window coords */
  1194. offset_x += win->client_rect.left - win->window_rect.left;
  1195. offset_y += win->client_rect.top - win->window_rect.top;
  1196. if (win->update_region && !(win->style & WS_CLIPCHILDREN))
  1197. {
  1198. if (!tmp && !(tmp = create_empty_region())) return;
  1199. offset_region( child->update_region, offset_x, offset_y );
  1200. if (subtract_region( tmp, win->update_region, child->update_region ))
  1201. {
  1202. set_update_region( win, tmp );
  1203. tmp = NULL;
  1204. }
  1205. /* restore child coords */
  1206. offset_region( child->update_region, -offset_x, -offset_y );
  1207. }
  1208. }
  1209. if (tmp) free_region( tmp );
  1210. }
  1211. /* add/subtract a region (in client coordinates) to the update region of the window */
  1212. static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
  1213. {
  1214. struct region *child_rgn, *tmp;
  1215. struct window *child;
  1216. if (flags & RDW_INVALIDATE)
  1217. {
  1218. if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
  1219. if (!add_update_region( win, tmp )) return;
  1220. if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
  1221. if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
  1222. }
  1223. else if (flags & RDW_VALIDATE)
  1224. {
  1225. if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
  1226. {
  1227. set_update_region( win, NULL );
  1228. }
  1229. else if (win->update_region)
  1230. {
  1231. if ((tmp = crop_region_to_win_rect( win, region, frame )))
  1232. {
  1233. if (!subtract_region( tmp, win->update_region, tmp ))
  1234. {
  1235. free_region( tmp );
  1236. return;
  1237. }
  1238. set_update_region( win, tmp );
  1239. }
  1240. if (flags & RDW_NOFRAME) validate_non_client( win );
  1241. if (flags & RDW_NOERASE) win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
  1242. }
  1243. }
  1244. if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
  1245. {
  1246. win->paint_flags |= PAINT_INTERNAL;
  1247. inc_window_paint_count( win, 1 );
  1248. }
  1249. else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
  1250. {
  1251. win->paint_flags &= ~PAINT_INTERNAL;
  1252. inc_window_paint_count( win, -1 );
  1253. }
  1254. /* now process children recursively */
  1255. if (flags & RDW_NOCHILDREN) return;
  1256. if (win->style & WS_MINIMIZE) return;
  1257. if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
  1258. if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
  1259. /* map to client coordinates */
  1260. offset_region( tmp, win->window_rect.left - win->client_rect.left,
  1261. win->window_rect.top - win->client_rect.top );
  1262. if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
  1263. LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
  1264. {
  1265. if (!(child->style & WS_VISIBLE)) continue;
  1266. if (!(child_rgn = create_empty_region())) continue;
  1267. if (copy_region( child_rgn, tmp ))
  1268. {
  1269. map_dpi_region( child, child_rgn, win->dpi, child->dpi );
  1270. if (rect_in_region( child_rgn, &child->window_rect ))
  1271. {
  1272. offset_region( child_rgn, -child->client_rect.left, -child->client_rect.top );
  1273. redraw_window( child, child_rgn, 1, flags );
  1274. }
  1275. }
  1276. free_region( child_rgn );
  1277. }
  1278. free_region( tmp );
  1279. }
  1280. /* retrieve the update flags for a window depending on the state of the update region */
  1281. static unsigned int get_update_flags( struct window *win, unsigned int flags )
  1282. {
  1283. unsigned int ret = 0;
  1284. if (flags & UPDATE_NONCLIENT)
  1285. {
  1286. if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
  1287. }
  1288. if (flags & UPDATE_ERASE)
  1289. {
  1290. if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
  1291. }
  1292. if (flags & UPDATE_PAINT)
  1293. {
  1294. if (win->update_region)
  1295. {
  1296. if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
  1297. ret |= UPDATE_PAINT;
  1298. }
  1299. }
  1300. if (flags & UPDATE_INTERNALPAINT)
  1301. {
  1302. if (win->paint_flags & PAINT_INTERNAL)
  1303. {
  1304. ret |= UPDATE_INTERNALPAINT;
  1305. if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
  1306. }
  1307. }
  1308. return ret;
  1309. }
  1310. /* iterate through the children of the given window until we find one with some update flags */
  1311. static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
  1312. unsigned int flags, struct window **child )
  1313. {
  1314. struct window *ptr;
  1315. unsigned int ret = 0;
  1316. /* first make sure we want to iterate children at all */
  1317. if (win->style & WS_MINIMIZE) return 0;
  1318. /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
  1319. * here we only want to repaint children of windows that clip them, others
  1320. * need to wait for WM_PAINT to be done in the parent first.
  1321. */
  1322. if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
  1323. LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
  1324. {
  1325. if (from_child) /* skip all children until from_child is found */
  1326. {
  1327. if (ptr == from_child) from_child = NULL;
  1328. continue;
  1329. }
  1330. if (!(ptr->style & WS_VISIBLE)) continue;
  1331. if ((ret = get_update_flags( ptr, flags )) != 0)
  1332. {
  1333. *child = ptr;
  1334. break;
  1335. }
  1336. if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
  1337. }
  1338. return ret;
  1339. }
  1340. /* iterate through children and siblings of the given window until we find one with some update flags */
  1341. static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
  1342. unsigned int flags, struct window **child )
  1343. {
  1344. unsigned int ret;
  1345. struct window *ptr, *from_sibling = NULL;
  1346. /* if some parent is not visible start from the next sibling */
  1347. if (!is_visible( win )) return 0;
  1348. for (ptr = from_child; ptr; ptr = ptr->parent)
  1349. {
  1350. if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
  1351. if (ptr == win) break;
  1352. }
  1353. /* non-client painting must be delayed if one of the parents is going to
  1354. * be repainted and doesn't clip children */
  1355. if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
  1356. {
  1357. for (ptr = win->parent; ptr; ptr = ptr->parent)
  1358. {
  1359. if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
  1360. return 0;
  1361. }
  1362. if (from_child && !(flags & UPDATE_ALLCHILDREN))
  1363. {
  1364. for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
  1365. {
  1366. if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
  1367. if (ptr == win) break;
  1368. }
  1369. }
  1370. }
  1371. /* check window itself (only if not restarting from a child) */
  1372. if (!from_child)
  1373. {
  1374. if ((ret = get_update_flags( win, flags )))
  1375. {
  1376. *child = win;
  1377. return ret;
  1378. }
  1379. from_child = win;
  1380. }
  1381. /* now check children */
  1382. if (flags & UPDATE_NOCHILDREN) return 0;
  1383. if (!from_sibling)
  1384. {
  1385. if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
  1386. from_sibling = from_child;
  1387. }
  1388. /* then check siblings and parent siblings */
  1389. while (from_sibling->parent && from_sibling != win)
  1390. {
  1391. if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
  1392. return ret;
  1393. from_sibling = from_sibling->parent;
  1394. }
  1395. return 0;
  1396. }
  1397. /* expose the areas revealed by a vis region change on the window parent */
  1398. /* returns the region exposed on the window itself (in client coordinates) */
  1399. static struct region *expose_window( struct window *win, const rectangle_t *old_window_rect,
  1400. struct region *old_vis_rgn )
  1401. {
  1402. struct region *new_vis_rgn, *exposed_rgn;
  1403. if (!(new_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return NULL;
  1404. if ((exposed_rgn = create_empty_region()))
  1405. {
  1406. if (subtract_region( exposed_rgn, new_vis_rgn, old_vis_rgn ) && !is_region_empty( exposed_rgn ))
  1407. {
  1408. /* make it relative to the new client area */
  1409. offset_region( exposed_rgn, win->window_rect.left - win->client_rect.left,
  1410. win->window_rect.top - win->client_rect.top );
  1411. }
  1412. else
  1413. {
  1414. free_region( exposed_rgn );
  1415. exposed_rgn = NULL;
  1416. }
  1417. }
  1418. if (win->parent && !is_desktop_window( win->parent ))
  1419. {
  1420. /* make it relative to the old window pos for subtracting */
  1421. offset_region( new_vis_rgn, win->window_rect.left - old_window_rect->left,
  1422. win->window_rect.top - old_window_rect->top );
  1423. if ((win->parent->style & WS_CLIPCHILDREN) ?
  1424. subtract_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ) :
  1425. xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
  1426. {
  1427. if (!is_region_empty( new_vis_rgn ))
  1428. {
  1429. /* make it relative to parent */
  1430. offset_region( new_vis_rgn, old_window_rect->left, old_window_rect->top );
  1431. redraw_window( win->parent, new_vis_rgn, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
  1432. }
  1433. }
  1434. }
  1435. free_region( new_vis_rgn );
  1436. return exposed_rgn;
  1437. }
  1438. /* set the window and client rectangles, updating the update region if necessary */
  1439. static void set_window_pos( struct window *win, struct window *previous,
  1440. unsigned int swp_flags, const rectangle_t *window_rect,
  1441. const rectangle_t *client_rect, const rectangle_t *visible_rect,
  1442. const rectangle_t *surface_rect, const rectangle_t *valid_rect )
  1443. {
  1444. struct region *old_vis_rgn = NULL, *exposed_rgn = NULL;
  1445. const rectangle_t old_window_rect = win->window_rect;
  1446. const rectangle_t old_visible_rect = win->visible_rect;
  1447. const rectangle_t old_client_rect = win->client_rect;
  1448. rectangle_t rect;
  1449. int client_changed, frame_changed;
  1450. int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
  1451. if (win->parent && !is_visible( win->parent )) visible = 0;
  1452. if (visible && !(old_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return;
  1453. /* set the new window info before invalidating anything */
  1454. win->window_rect = *window_rect;
  1455. win->visible_rect = *visible_rect;
  1456. win->surface_rect = *surface_rect;
  1457. win->client_rect = *client_rect;
  1458. if (!(swp_flags & SWP_NOZORDER) && win->parent) link_window( win, previous );
  1459. if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
  1460. else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
  1461. /* keep children at the same position relative to top right corner when the parent is mirrored */
  1462. if (win->ex_style & WS_EX_LAYOUTRTL)
  1463. {
  1464. struct window *child;
  1465. int old_size = old_client_rect.right - old_client_rect.left;
  1466. int new_size = win->client_rect.right - win->client_rect.left;
  1467. if (old_size != new_size) LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
  1468. {
  1469. offset_rect( &child->window_rect, new_size - old_size, 0 );
  1470. offset_rect( &child->visible_rect, new_size - old_size, 0 );
  1471. offset_rect( &child->surface_rect, new_size - old_size, 0 );
  1472. offset_rect( &child->client_rect, new_size - old_size, 0 );
  1473. }
  1474. }
  1475. /* reset cursor clip rectangle when the desktop changes size */
  1476. if (win == win->desktop->top_window) win->desktop->cursor.clip = *window_rect;
  1477. /* if the window is not visible, everything is easy */
  1478. if (!visible) return;
  1479. /* expose anything revealed by the change */
  1480. if (!(swp_flags & SWP_NOREDRAW))
  1481. exposed_rgn = expose_window( win, &old_window_rect, old_vis_rgn );
  1482. if (!(win->style & WS_VISIBLE))
  1483. {
  1484. /* clear the update region since the window is no longer visible */
  1485. validate_whole_window( win );
  1486. validate_children( win );
  1487. goto done;
  1488. }
  1489. /* crop update region to the new window rect */
  1490. if (win->update_region)
  1491. {
  1492. if (get_window_visible_rect( win, &rect, 1 ))
  1493. {
  1494. struct region *tmp = create_empty_region();
  1495. if (tmp)
  1496. {
  1497. set_region_rect( tmp, &rect );
  1498. if (intersect_region( tmp, win->update_region, tmp ))
  1499. set_update_region( win, tmp );
  1500. else
  1501. free_region( tmp );
  1502. }
  1503. }
  1504. else set_update_region( win, NULL ); /* visible rect is empty */
  1505. }
  1506. /* crop children regions to the new window rect */
  1507. if (get_window_visible_rect( win, &rect, 0 ))
  1508. {
  1509. /* map to client coords */
  1510. offset_rect( &rect, win->window_rect.left - win->client_rect.left,
  1511. win->window_rect.top - win->client_rect.top );
  1512. crop_children_update_region( win, &rect );
  1513. }
  1514. else crop_children_update_region( win, NULL );
  1515. if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
  1516. /* expose the whole non-client area if it changed in any way */
  1517. if (swp_flags & SWP_NOCOPYBITS)
  1518. {
  1519. frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
  1520. memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
  1521. memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ));
  1522. client_changed = memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) );
  1523. }
  1524. else
  1525. {
  1526. /* assume the bits have been moved to follow the window rect */
  1527. int x_offset = window_rect->left - old_window_rect.left;
  1528. int y_offset = window_rect->top - old_window_rect.top;
  1529. frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
  1530. window_rect->right - old_window_rect.right != x_offset ||
  1531. window_rect->bottom - old_window_rect.bottom != y_offset ||
  1532. visible_rect->left - old_visible_rect.left != x_offset ||
  1533. visible_rect->right - old_visible_rect.right != x_offset ||
  1534. visible_rect->top - old_visible_rect.top != y_offset ||
  1535. visible_rect->bottom - old_visible_rect.bottom != y_offset);
  1536. client_changed = (client_rect->left - old_client_rect.left != x_offset ||
  1537. client_rect->right - old_client_rect.right != x_offset ||
  1538. client_rect->top - old_client_rect.top != y_offset ||
  1539. client_rect->bottom - old_client_rect.bottom != y_offset ||
  1540. memcmp( valid_rect, client_rect, sizeof(*client_rect) ));
  1541. }
  1542. if (frame_changed || client_changed)
  1543. {
  1544. struct region *win_rgn = old_vis_rgn; /* reuse previous region */
  1545. set_region_rect( win_rgn, window_rect );
  1546. if (!is_rect_empty( valid_rect ))
  1547. {
  1548. /* subtract the valid portion of client rect from the total region */
  1549. struct region *tmp = create_empty_region();
  1550. if (tmp)
  1551. {
  1552. set_region_rect( tmp, valid_rect );
  1553. /* subtract update region since invalid parts of the valid rect won't be copied */
  1554. if (win->update_region)
  1555. {
  1556. offset_region( tmp, -window_rect->left, -window_rect->top );
  1557. subtract_region( tmp, tmp, win->update_region );
  1558. offset_region( tmp, window_rect->left, window_rect->top );
  1559. }
  1560. if (subtract_region( tmp, win_rgn, tmp )) win_rgn = tmp;
  1561. else free_region( tmp );
  1562. }
  1563. }
  1564. if (!is_desktop_window(win))
  1565. offset_region( win_rgn, -client_rect->left, -client_rect->top );
  1566. if (exposed_rgn)
  1567. {
  1568. union_region( exposed_rgn, exposed_rgn, win_rgn );
  1569. if (win_rgn != old_vis_rgn) free_region( win_rgn );
  1570. }
  1571. else
  1572. {
  1573. exposed_rgn = win_rgn;
  1574. if (win_rgn == old_vis_rgn) old_vis_rgn = NULL;
  1575. }
  1576. }
  1577. if (exposed_rgn)
  1578. redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
  1579. done:
  1580. if (old_vis_rgn) free_region( old_vis_rgn );
  1581. if (exposed_rgn) free_region( exposed_rgn );
  1582. clear_error(); /* we ignore out of memory errors once the new rects have been set */
  1583. }
  1584. /* set the window region, updating the update region if necessary */
  1585. static void set_window_region( struct window *win, struct region *region, int redraw )
  1586. {
  1587. struct region *old_vis_rgn = NULL, *exposed_rgn;
  1588. /* no need to redraw if window is not visible */
  1589. if (redraw && !is_visible( win )) redraw = 0;
  1590. if (redraw) old_vis_rgn = get_visible_region( win, DCX_WINDOW );
  1591. if (win->win_region) free_region( win->win_region );
  1592. win->win_region = region;
  1593. /* expose anything revealed by the change */
  1594. if (old_vis_rgn && ((exposed_rgn = expose_window( win, &win->window_rect, old_vis_rgn ))))
  1595. {
  1596. redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
  1597. free_region( exposed_rgn );
  1598. }
  1599. if (old_vis_rgn) free_region( old_vis_rgn );
  1600. clear_error(); /* we ignore out of memory errors since the region has been set */
  1601. }
  1602. /* set the layer region */
  1603. static void set_layer_region( struct window *win, struct region *region )
  1604. {
  1605. if (win->layer_region) free_region( win->layer_region );
  1606. win->layer_region = region;
  1607. }
  1608. /* destroy a window */
  1609. void destroy_window( struct window *win )
  1610. {
  1611. /* hide the window */
  1612. if (is_visible(win))
  1613. {
  1614. struct region *vis_rgn = get_visible_region( win, DCX_WINDOW );
  1615. win->style &= ~WS_VISIBLE;
  1616. if (vis_rgn)
  1617. {
  1618. struct region *exposed_rgn = expose_window( win, &win->window_rect, vis_rgn );
  1619. if (exposed_rgn) free_region( exposed_rgn );
  1620. free_region( vis_rgn );
  1621. }
  1622. validate_whole_window( win );
  1623. validate_children( win );
  1624. }
  1625. /* destroy all children */
  1626. while (!list_empty(&win->children))
  1627. destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
  1628. while (!list_empty(&win->unlinked))
  1629. destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
  1630. /* reset global window pointers, if the corresponding window is destroyed */
  1631. if (win == shell_window) shell_window = NULL;
  1632. if (win == shell_listview) shell_listview = NULL;
  1633. if (win == progman_window) progman_window = NULL;
  1634. if (win == taskman_window) taskman_window = NULL;
  1635. free_hotkeys( win->desktop, win->handle );
  1636. cleanup_clipboard_window( win->desktop, win->handle );
  1637. free_user_handle( win->handle );
  1638. destroy_properties( win );
  1639. list_remove( &win->entry );
  1640. if (is_desktop_window(win))
  1641. {
  1642. struct desktop *desktop = win->desktop;
  1643. assert( desktop->top_window == win || desktop->msg_window == win );
  1644. if (desktop->top_window == win) desktop->top_window = NULL;
  1645. else desktop->msg_window = NULL;
  1646. }
  1647. else if (is_desktop_window( win->parent ))
  1648. {
  1649. post_message( win->parent->handle, WM_PARENTNOTIFY, WM_DESTROY, win->handle );
  1650. }
  1651. detach_window_thread( win );
  1652. if (win->win_region) free_region( win->win_region );
  1653. if (win->layer_region) free_region( win->layer_region );
  1654. if (win->update_region) free_region( win->update_region );
  1655. if (win->class) release_class( win->class );
  1656. free( win->text );
  1657. memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
  1658. free( win );
  1659. }
  1660. /* create a window */
  1661. DECL_HANDLER(create_window)
  1662. {
  1663. struct window *win, *parent = NULL, *owner = NULL;
  1664. struct unicode_str cls_name = get_req_unicode_str();
  1665. atom_t atom;
  1666. reply->handle = 0;
  1667. if (req->parent && !(parent = get_window( req->parent ))) return;
  1668. if (req->owner)
  1669. {
  1670. if (!(owner = get_window( req->owner ))) return;
  1671. if (is_desktop_window(owner)) owner = NULL;
  1672. else if (parent && !is_desktop_window(parent))
  1673. {
  1674. /* an owned window must be created as top-level */
  1675. set_error( STATUS_ACCESS_DENIED );
  1676. return;
  1677. }
  1678. else /* owner must be a top-level window */
  1679. while ((owner->style & (WS_POPUP|WS_CHILD)) == WS_CHILD && !is_desktop_window(owner->parent))
  1680. owner = owner->parent;
  1681. }
  1682. atom = cls_name.len ? find_global_atom( NULL, &cls_name ) : req->atom;
  1683. if (!(win = create_window( parent, owner, atom, req->instance ))) return;
  1684. if (parent && !is_desktop_window( parent ))
  1685. {
  1686. win->dpi_awareness = parent->dpi_awareness;
  1687. win->dpi = parent->dpi;
  1688. }
  1689. else if (!parent || req->awareness != DPI_AWARENESS_PER_MONITOR_AWARE)
  1690. {
  1691. win->dpi_awareness = req->awareness;
  1692. win->dpi = req->dpi;
  1693. }
  1694. reply->handle = win->handle;
  1695. reply->parent = win->parent ? win->parent->handle : 0;
  1696. reply->owner = win->owner;
  1697. reply->extra = win->nb_extra_bytes;
  1698. reply->dpi = win->dpi;
  1699. reply->awareness = win->dpi_awareness;
  1700. reply->class_ptr = get_class_client_ptr( win->class );
  1701. }
  1702. /* set the parent of a window */
  1703. DECL_HANDLER(set_parent)
  1704. {
  1705. struct window *win, *parent = NULL;
  1706. if (!(win = get_window( req->handle ))) return;
  1707. if (req->parent && !(parent = get_window( req->parent ))) return;
  1708. if (is_desktop_window(win))
  1709. {
  1710. set_error( STATUS_INVALID_PARAMETER );
  1711. return;
  1712. }
  1713. reply->old_parent = win->parent->handle;
  1714. reply->full_parent = parent ? parent->handle : 0;
  1715. set_parent_window( win, parent );
  1716. reply->dpi = win->dpi;
  1717. reply->awareness = win->dpi_awareness;
  1718. }
  1719. /* destroy a window */
  1720. DECL_HANDLER(destroy_window)
  1721. {
  1722. struct window *win = get_window( req->handle );
  1723. if (win)
  1724. {
  1725. if (!is_desktop_window(win)) destroy_window( win );
  1726. else if (win->thread == current) detach_window_thread( win );
  1727. else set_error( STATUS_ACCESS_DENIED );
  1728. }
  1729. }
  1730. /* retrieve the desktop window for the current thread */
  1731. DECL_HANDLER(get_desktop_window)
  1732. {
  1733. struct desktop *desktop = get_thread_desktop( current, 0 );
  1734. int force;
  1735. if (!desktop) return;
  1736. /* if winstation is invisible, then avoid roundtrip */
  1737. force = req->force || !(desktop->winstation->flags & WSF_VISIBLE);
  1738. if (!desktop->top_window && force) /* create it */
  1739. {
  1740. if ((desktop->top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
  1741. {
  1742. detach_window_thread( desktop->top_window );
  1743. desktop->top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  1744. }
  1745. }
  1746. if (!desktop->msg_window && force) /* create it */
  1747. {
  1748. static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
  1749. static const struct unicode_str name = { messageW, sizeof(messageW) };
  1750. atom_t atom = add_global_atom( NULL, &name );
  1751. if (atom && (desktop->msg_window = create_window( NULL, NULL, atom, 0 )))
  1752. {
  1753. detach_window_thread( desktop->msg_window );
  1754. desktop->msg_window->style = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  1755. }
  1756. }
  1757. reply->top_window = desktop->top_window ? desktop->top_window->handle : 0;
  1758. reply->msg_window = desktop->msg_window ? desktop->msg_window->handle : 0;
  1759. release_object( desktop );
  1760. }
  1761. /* set a window owner */
  1762. DECL_HANDLER(set_window_owner)
  1763. {
  1764. struct window *win = get_window( req->handle );
  1765. struct window *owner = NULL, *ptr;
  1766. if (!win) return;
  1767. if (req->owner && !(owner = get_window( req->owner ))) return;
  1768. if (is_desktop_window(win))
  1769. {
  1770. set_error( STATUS_ACCESS_DENIED );
  1771. return;
  1772. }
  1773. /* make sure owner is not a successor of window */
  1774. for (ptr = owner; ptr; ptr = ptr->owner ? get_window( ptr->owner ) : NULL)
  1775. {
  1776. if (ptr == win)
  1777. {
  1778. set_error( STATUS_INVALID_PARAMETER );
  1779. return;
  1780. }
  1781. }
  1782. reply->prev_owner = win->owner;
  1783. reply->full_owner = win->owner = owner ? owner->handle : 0;
  1784. }
  1785. /* get information from a window handle */
  1786. DECL_HANDLER(get_window_info)
  1787. {
  1788. struct window *win = get_window( req->handle );
  1789. if (!win) return;
  1790. reply->full_handle = win->handle;
  1791. reply->last_active = win->handle;
  1792. reply->is_unicode = win->is_unicode;
  1793. reply->awareness = win->dpi_awareness;
  1794. reply->dpi = win->dpi ? win->dpi : get_monitor_dpi( win );
  1795. if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
  1796. if (win->thread)
  1797. {
  1798. reply->tid = get_thread_id( win->thread );
  1799. reply->pid = get_process_id( win->thread->process );
  1800. reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
  1801. }
  1802. }
  1803. /* set some information in a window */
  1804. DECL_HANDLER(set_window_info)
  1805. {
  1806. struct window *win = get_window( req->handle );
  1807. if (!win) return;
  1808. if (req->flags && is_desktop_window(win) && win->thread != current)
  1809. {
  1810. set_error( STATUS_ACCESS_DENIED );
  1811. return;
  1812. }
  1813. if (req->extra_size > sizeof(req->extra_value) ||
  1814. req->extra_offset < -1 ||
  1815. req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
  1816. {
  1817. set_win32_error( ERROR_INVALID_INDEX );
  1818. return;
  1819. }
  1820. if (req->extra_offset != -1)
  1821. {
  1822. memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
  1823. }
  1824. else if (req->flags & SET_WIN_EXTRA)
  1825. {
  1826. set_win32_error( ERROR_INVALID_INDEX );
  1827. return;
  1828. }
  1829. reply->old_style = win->style;
  1830. reply->old_ex_style = win->ex_style;
  1831. reply->old_id = win->id;
  1832. reply->old_instance = win->instance;
  1833. reply->old_user_data = win->user_data;
  1834. if (req->flags & SET_WIN_STYLE) win->style = req->style;
  1835. if (req->flags & SET_WIN_EXSTYLE)
  1836. {
  1837. /* WS_EX_TOPMOST can only be changed for unlinked windows */
  1838. if (!win->is_linked) win->ex_style = req->ex_style;
  1839. else win->ex_style = (req->ex_style & ~WS_EX_TOPMOST) | (win->ex_style & WS_EX_TOPMOST);
  1840. if (!(win->ex_style & WS_EX_LAYERED)) win->is_layered = 0;
  1841. }
  1842. if (req->flags & SET_WIN_ID) win->id = req->id;
  1843. if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
  1844. if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
  1845. if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
  1846. if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
  1847. &req->extra_value, req->extra_size );
  1848. /* changing window style triggers a non-client paint */
  1849. if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
  1850. }
  1851. /* get a list of the window parents, up to the root of the tree */
  1852. DECL_HANDLER(get_window_parents)
  1853. {
  1854. struct window *ptr, *win = get_window( req->handle );
  1855. int total = 0;
  1856. user_handle_t *data;
  1857. data_size_t len;
  1858. if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
  1859. reply->count = total;
  1860. len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
  1861. if (len && ((data = set_reply_data_size( len ))))
  1862. {
  1863. for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
  1864. *data++ = ptr->handle;
  1865. }
  1866. }
  1867. /* get a list of the window children */
  1868. DECL_HANDLER(get_window_children)
  1869. {
  1870. struct window *parent = NULL;
  1871. unsigned int total;
  1872. user_handle_t *data;
  1873. data_size_t len;
  1874. struct unicode_str cls_name = get_req_unicode_str();
  1875. atom_t atom = req->atom;
  1876. struct desktop *desktop = NULL;
  1877. if (cls_name.len && !(atom = find_global_atom( NULL, &cls_name ))) return;
  1878. if (req->desktop)
  1879. {
  1880. if (!(desktop = get_desktop_obj( current->process, req->desktop, DESKTOP_ENUMERATE ))) return;
  1881. parent = desktop->top_window;
  1882. }
  1883. else
  1884. {
  1885. if (req->parent && !(parent = get_window( req->parent ))) return;
  1886. if (!parent && !(desktop = get_thread_desktop( current, 0 ))) return;
  1887. }
  1888. if (parent)
  1889. total = get_children_windows( parent, atom, req->tid, NULL, 0 );
  1890. else
  1891. total = get_children_windows( desktop->top_window, atom, req->tid, NULL, 0 ) +
  1892. get_children_windows( desktop->msg_window, atom, req->tid, NULL, 0 );
  1893. reply->count = total;
  1894. len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
  1895. if (len && ((data = set_reply_data_size( len ))))
  1896. {
  1897. if (parent) get_children_windows( parent, atom, req->tid, data, len / sizeof(user_handle_t) );
  1898. else
  1899. {
  1900. total = get_children_windows( desktop->top_window, atom, req->tid,
  1901. data, len / sizeof(user_handle_t) );
  1902. data += total;
  1903. len -= total * sizeof(user_handle_t);
  1904. if (len >= sizeof(user_handle_t))
  1905. get_children_windows( desktop->msg_window, atom, req->tid,
  1906. data, len / sizeof(user_handle_t) );
  1907. }
  1908. }
  1909. if (desktop) release_object( desktop );
  1910. }
  1911. /* get a list of the window children that contain a given point */
  1912. DECL_HANDLER(get_window_children_from_point)
  1913. {
  1914. struct user_handle_array array;
  1915. struct window *parent = get_window( req->parent );
  1916. data_size_t len;
  1917. if (!parent) return;
  1918. array.handles = NULL;
  1919. array.count = 0;
  1920. array.total = 0;
  1921. if (!all_windows_from_point( parent, req->x, req->y, req->dpi, &array )) return;
  1922. reply->count = array.count;
  1923. len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
  1924. if (len) set_reply_data_ptr( array.handles, len );
  1925. else free( array.handles );
  1926. }
  1927. /* get window tree information from a window handle */
  1928. DECL_HANDLER(get_window_tree)
  1929. {
  1930. struct window *ptr, *win = get_window( req->handle );
  1931. if (!win) return;
  1932. reply->parent = 0;
  1933. reply->owner = 0;
  1934. reply->next_sibling = 0;
  1935. reply->prev_sibling = 0;
  1936. reply->first_sibling = 0;
  1937. reply->last_sibling = 0;
  1938. reply->first_child = 0;
  1939. reply->last_child = 0;
  1940. if (win->parent)
  1941. {
  1942. struct window *parent = win->parent;
  1943. reply->parent = parent->handle;
  1944. reply->owner = win->owner;
  1945. if (win->is_linked)
  1946. {
  1947. if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
  1948. if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
  1949. }
  1950. if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
  1951. if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
  1952. }
  1953. if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
  1954. if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
  1955. }
  1956. /* set the position and Z order of a window */
  1957. DECL_HANDLER(set_window_pos)
  1958. {
  1959. rectangle_t window_rect, client_rect, visible_rect, surface_rect, valid_rect;
  1960. const rectangle_t *extra_rects = get_req_data();
  1961. struct window *previous = NULL;
  1962. struct window *top, *win = get_window( req->handle );
  1963. unsigned int flags = req->swp_flags;
  1964. if (!win) return;
  1965. if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
  1966. if (!(flags & SWP_NOZORDER))
  1967. {
  1968. switch ((int)req->previous)
  1969. {
  1970. case 0: /* HWND_TOP */
  1971. previous = WINPTR_TOP;
  1972. break;
  1973. case 1: /* HWND_BOTTOM */
  1974. previous = WINPTR_BOTTOM;
  1975. break;
  1976. case -1: /* HWND_TOPMOST */
  1977. previous = WINPTR_TOPMOST;
  1978. break;
  1979. case -2: /* HWND_NOTOPMOST */
  1980. previous = WINPTR_NOTOPMOST;
  1981. break;
  1982. default:
  1983. if (!(previous = get_window( req->previous ))) return;
  1984. /* previous must be a sibling */
  1985. if (previous->parent != win->parent)
  1986. {
  1987. set_error( STATUS_INVALID_PARAMETER );
  1988. return;
  1989. }
  1990. break;
  1991. }
  1992. if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
  1993. }
  1994. /* windows that use UpdateLayeredWindow don't trigger repaints */
  1995. if ((win->ex_style & WS_EX_LAYERED) && !win->is_layered) flags |= SWP_NOREDRAW;
  1996. /* window rectangle must be ordered properly */
  1997. if (req->window.right < req->window.left || req->window.bottom < req->window.top)
  1998. {
  1999. set_error( STATUS_INVALID_PARAMETER );
  2000. return;
  2001. }
  2002. window_rect = req->window;
  2003. client_rect = req->client;
  2004. if (get_req_data_size() >= sizeof(rectangle_t)) visible_rect = extra_rects[0];
  2005. else visible_rect = window_rect;
  2006. if (get_req_data_size() >= 2 * sizeof(rectangle_t)) surface_rect = extra_rects[1];
  2007. else surface_rect = visible_rect;
  2008. if (get_req_data_size() >= 3 * sizeof(rectangle_t)) valid_rect = extra_rects[2];
  2009. else valid_rect = empty_rect;
  2010. if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
  2011. {
  2012. mirror_rect( &win->parent->client_rect, &window_rect );
  2013. mirror_rect( &win->parent->client_rect, &visible_rect );
  2014. mirror_rect( &win->parent->client_rect, &client_rect );
  2015. mirror_rect( &win->parent->client_rect, &surface_rect );
  2016. mirror_rect( &win->parent->client_rect, &valid_rect );
  2017. }
  2018. win->paint_flags = (win->paint_flags & ~PAINT_CLIENT_FLAGS) | (req->paint_flags & PAINT_CLIENT_FLAGS);
  2019. if (win->paint_flags & PAINT_HAS_PIXEL_FORMAT) update_pixel_format_flags( win );
  2020. set_window_pos( win, previous, flags, &window_rect, &client_rect,
  2021. &visible_rect, &surface_rect, &valid_rect );
  2022. reply->new_style = win->style;
  2023. reply->new_ex_style = win->ex_style;
  2024. top = get_top_clipping_window( win );
  2025. if (is_visible( top ) && (top->paint_flags & PAINT_HAS_SURFACE))
  2026. {
  2027. reply->surface_win = top->handle;
  2028. reply->needs_update = !!(top->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD));
  2029. }
  2030. }
  2031. /* get the window and client rectangles of a window */
  2032. DECL_HANDLER(get_window_rectangles)
  2033. {
  2034. struct window *win = get_window( req->handle );
  2035. if (!win) return;
  2036. reply->window = win->window_rect;
  2037. reply->client = win->client_rect;
  2038. switch (req->relative)
  2039. {
  2040. case COORDS_CLIENT:
  2041. offset_rect( &reply->window, -win->client_rect.left, -win->client_rect.top );
  2042. offset_rect( &reply->client, -win->client_rect.left, -win->client_rect.top );
  2043. if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->client_rect, &reply->window );
  2044. break;
  2045. case COORDS_WINDOW:
  2046. offset_rect( &reply->window, -win->window_rect.left, -win->window_rect.top );
  2047. offset_rect( &reply->client, -win->window_rect.left, -win->window_rect.top );
  2048. if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->window_rect, &reply->client );
  2049. break;
  2050. case COORDS_PARENT:
  2051. if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
  2052. {
  2053. mirror_rect( &win->parent->client_rect, &reply->window );
  2054. mirror_rect( &win->parent->client_rect, &reply->client );
  2055. }
  2056. break;
  2057. case COORDS_SCREEN:
  2058. client_to_screen_rect( win->parent, &reply->window );
  2059. client_to_screen_rect( win->parent, &reply->client );
  2060. break;
  2061. default:
  2062. set_error( STATUS_INVALID_PARAMETER );
  2063. break;
  2064. }
  2065. map_dpi_rect( win, &reply->window, win->dpi, req->dpi );
  2066. map_dpi_rect( win, &reply->client, win->dpi, req->dpi );
  2067. }
  2068. /* get the window text */
  2069. DECL_HANDLER(get_window_text)
  2070. {
  2071. struct window *win = get_window( req->handle );
  2072. if (win && win->text_len)
  2073. {
  2074. reply->length = win->text_len / sizeof(WCHAR);
  2075. set_reply_data( win->text, min( win->text_len, get_reply_max_size() ));
  2076. }
  2077. }
  2078. /* set the window text */
  2079. DECL_HANDLER(set_window_text)
  2080. {
  2081. data_size_t len;
  2082. WCHAR *text = NULL;
  2083. struct window *win = get_window( req->handle );
  2084. if (!win) return;
  2085. len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
  2086. if (len && !(text = memdup( get_req_data(), len ))) return;
  2087. free( win->text );
  2088. win->text = text;
  2089. win->text_len = len;
  2090. }
  2091. /* get the coordinates offset between two windows */
  2092. DECL_HANDLER(get_windows_offset)
  2093. {
  2094. struct window *win;
  2095. int x, y, mirror_from = 0, mirror_to = 0;
  2096. reply->x = reply->y = 0;
  2097. if (req->from)
  2098. {
  2099. if (!(win = get_window( req->from ))) return;
  2100. if (win->ex_style & WS_EX_LAYOUTRTL) mirror_from = 1;
  2101. x = mirror_from ? win->client_rect.right - win->client_rect.left : 0;
  2102. y = 0;
  2103. client_to_screen( win, &x, &y );
  2104. map_dpi_point( win, &x, &y, win->dpi, req->dpi );
  2105. reply->x += x;
  2106. reply->y += y;
  2107. }
  2108. if (req->to)
  2109. {
  2110. if (!(win = get_window( req->to ))) return;
  2111. if (win->ex_style & WS_EX_LAYOUTRTL) mirror_to = 1;
  2112. x = mirror_to ? win->client_rect.right - win->client_rect.left : 0;
  2113. y = 0;
  2114. client_to_screen( win, &x, &y );
  2115. map_dpi_point( win, &x, &y, win->dpi, req->dpi );
  2116. reply->x -= x;
  2117. reply->y -= y;
  2118. }
  2119. if (mirror_from) reply->x = -reply->x;
  2120. reply->mirror = mirror_from ^ mirror_to;
  2121. }
  2122. /* get the visible region of a window */
  2123. DECL_HANDLER(get_visible_region)
  2124. {
  2125. struct region *region;
  2126. struct window *top, *win = get_window( req->window );
  2127. if (!win) return;
  2128. top = get_top_clipping_window( win );
  2129. if ((region = get_visible_region( win, req->flags )))
  2130. {
  2131. rectangle_t *data;
  2132. map_win_region_to_screen( win, region );
  2133. data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
  2134. if (data) set_reply_data_ptr( data, reply->total_size );
  2135. }
  2136. reply->top_win = top->handle;
  2137. reply->top_rect = top->surface_rect;
  2138. if (!is_desktop_window(win))
  2139. {
  2140. reply->win_rect = (req->flags & DCX_WINDOW) ? win->window_rect : win->client_rect;
  2141. client_to_screen_rect( top->parent, &reply->top_rect );
  2142. client_to_screen_rect( win->parent, &reply->win_rect );
  2143. }
  2144. else
  2145. {
  2146. reply->win_rect.left = 0;
  2147. reply->win_rect.top = 0;
  2148. reply->win_rect.right = win->client_rect.right - win->client_rect.left;
  2149. reply->win_rect.bottom = win->client_rect.bottom - win->client_rect.top;
  2150. }
  2151. reply->paint_flags = win->paint_flags & PAINT_CLIENT_FLAGS;
  2152. }
  2153. /* get the surface visible region of a window */
  2154. DECL_HANDLER(get_surface_region)
  2155. {
  2156. struct region *region;
  2157. struct window *win = get_window( req->window );
  2158. if (!win || !is_visible( win )) return;
  2159. if ((region = get_surface_region( win )))
  2160. {
  2161. rectangle_t *data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
  2162. if (data) set_reply_data_ptr( data, reply->total_size );
  2163. }
  2164. reply->visible_rect = win->visible_rect;
  2165. }
  2166. /* get the window region */
  2167. DECL_HANDLER(get_window_region)
  2168. {
  2169. rectangle_t *data;
  2170. struct window *win = get_window( req->window );
  2171. if (!win) return;
  2172. if (!win->win_region) return;
  2173. if (win->ex_style & WS_EX_LAYOUTRTL)
  2174. {
  2175. struct region *region = create_empty_region();
  2176. if (!region) return;
  2177. if (!copy_region( region, win->win_region ))
  2178. {
  2179. free_region( region );
  2180. return;
  2181. }
  2182. mirror_region( &win->window_rect, region );
  2183. data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
  2184. }
  2185. else data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
  2186. if (data) set_reply_data_ptr( data, reply->total_size );
  2187. }
  2188. /* set the window region */
  2189. DECL_HANDLER(set_window_region)
  2190. {
  2191. struct region *region = NULL;
  2192. struct window *win = get_window( req->window );
  2193. if (!win) return;
  2194. if (get_req_data_size()) /* no data means remove the region completely */
  2195. {
  2196. if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
  2197. return;
  2198. if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->window_rect, region );
  2199. }
  2200. set_window_region( win, region, req->redraw );
  2201. }
  2202. /* set the layer region */
  2203. DECL_HANDLER(set_layer_region)
  2204. {
  2205. struct region *region = NULL;
  2206. struct window *win = get_window( req->window );
  2207. if (!win) return;
  2208. if (get_req_data_size()) /* no data means remove the region completely */
  2209. {
  2210. if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
  2211. return;
  2212. if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->window_rect, region );
  2213. }
  2214. set_layer_region( win, region );
  2215. }
  2216. /* get a window update region */
  2217. DECL_HANDLER(get_update_region)
  2218. {
  2219. rectangle_t *data;
  2220. unsigned int flags = req->flags;
  2221. struct window *from_child = NULL;
  2222. struct window *win = get_window( req->window );
  2223. reply->flags = 0;
  2224. if (!win) return;
  2225. if (req->from_child)
  2226. {
  2227. struct window *ptr;
  2228. if (!(from_child = get_window( req->from_child ))) return;
  2229. /* make sure from_child is a child of win */
  2230. ptr = from_child;
  2231. while (ptr && ptr != win) ptr = ptr->parent;
  2232. if (!ptr)
  2233. {
  2234. set_error( STATUS_INVALID_PARAMETER );
  2235. return;
  2236. }
  2237. }
  2238. if (flags & UPDATE_DELAYED_ERASE) /* this means that the previous call didn't erase */
  2239. {
  2240. if (from_child) from_child->paint_flags |= PAINT_DELAYED_ERASE;
  2241. else win->paint_flags |= PAINT_DELAYED_ERASE;
  2242. }
  2243. reply->flags = get_window_update_flags( win, from_child, flags, &win );
  2244. reply->child = win->handle;
  2245. if (flags & UPDATE_NOREGION) return;
  2246. if (win->update_region)
  2247. {
  2248. /* convert update region to screen coordinates */
  2249. struct region *region = create_empty_region();
  2250. if (!region) return;
  2251. if (!copy_region( region, win->update_region ))
  2252. {
  2253. free_region( region );
  2254. return;
  2255. }
  2256. if ((flags & UPDATE_CLIPCHILDREN) && (win->style & WS_CLIPCHILDREN))
  2257. clip_children( win, NULL, region, win->client_rect.left - win->window_rect.left,
  2258. win->client_rect.top - win->window_rect.top );
  2259. map_win_region_to_screen( win, region );
  2260. if (!(data = get_region_data_and_free( region, get_reply_max_size(),
  2261. &reply->total_size ))) return;
  2262. set_reply_data_ptr( data, reply->total_size );
  2263. }
  2264. if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
  2265. {
  2266. validate_parents( win );
  2267. validate_whole_window( win );
  2268. }
  2269. else
  2270. {
  2271. if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
  2272. if (reply->flags & UPDATE_ERASE)
  2273. {
  2274. win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
  2275. /* desktop window only gets erased, not repainted */
  2276. if (is_desktop_window(win)) validate_whole_window( win );
  2277. }
  2278. }
  2279. }
  2280. /* update the z order of a window so that a given rectangle is fully visible */
  2281. DECL_HANDLER(update_window_zorder)
  2282. {
  2283. rectangle_t tmp, rect = req->rect;
  2284. struct window *ptr, *win = get_window( req->window );
  2285. if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
  2286. LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
  2287. {
  2288. if (ptr == win) break;
  2289. if (!(ptr->style & WS_VISIBLE)) continue;
  2290. if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
  2291. if (ptr->is_layered && (ptr->layered_flags & LWA_COLORKEY)) continue;
  2292. tmp = rect;
  2293. map_dpi_rect( win, &tmp, win->parent->dpi, win->dpi );
  2294. if (!intersect_rect( &tmp, &tmp, &ptr->visible_rect )) continue;
  2295. if (ptr->win_region)
  2296. {
  2297. offset_rect( &tmp, -ptr->window_rect.left, -ptr->window_rect.top );
  2298. if (!rect_in_region( ptr->win_region, &tmp )) continue;
  2299. }
  2300. /* found a window obscuring the rectangle, now move win above this one */
  2301. /* making sure to not violate the topmost rule */
  2302. if (!(ptr->ex_style & WS_EX_TOPMOST) || (win->ex_style & WS_EX_TOPMOST))
  2303. {
  2304. list_remove( &win->entry );
  2305. list_add_before( &ptr->entry, &win->entry );
  2306. }
  2307. break;
  2308. }
  2309. }
  2310. /* mark parts of a window as needing a redraw */
  2311. DECL_HANDLER(redraw_window)
  2312. {
  2313. unsigned int flags = req->flags;
  2314. struct region *region = NULL;
  2315. struct window *win;
  2316. if (!req->window)
  2317. {
  2318. if (!(win = get_desktop_window( current ))) return;
  2319. }
  2320. else
  2321. {
  2322. if (!(win = get_window( req->window ))) return;
  2323. if (is_desktop_window( win )) flags &= ~RDW_ALLCHILDREN;
  2324. }
  2325. if (!is_visible( win )) return; /* nothing to do */
  2326. if (flags & (RDW_VALIDATE|RDW_INVALIDATE))
  2327. {
  2328. if (get_req_data_size()) /* no data means whole rectangle */
  2329. {
  2330. if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
  2331. return;
  2332. if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->client_rect, region );
  2333. }
  2334. }
  2335. redraw_window( win, region, (flags & RDW_INVALIDATE) && (flags & RDW_FRAME), flags );
  2336. if (region) free_region( region );
  2337. }
  2338. /* set a window property */
  2339. DECL_HANDLER(set_window_property)
  2340. {
  2341. struct unicode_str name = get_req_unicode_str();
  2342. struct window *win = get_window( req->window );
  2343. if (!win) return;
  2344. if (name.len)
  2345. {
  2346. atom_t atom = add_global_atom( NULL, &name );
  2347. if (atom)
  2348. {
  2349. set_property( win, atom, req->data, PROP_TYPE_STRING );
  2350. release_global_atom( NULL, atom );
  2351. }
  2352. }
  2353. else set_property( win, req->atom, req->data, PROP_TYPE_ATOM );
  2354. }
  2355. /* remove a window property */
  2356. DECL_HANDLER(remove_window_property)
  2357. {
  2358. struct unicode_str name = get_req_unicode_str();
  2359. struct window *win = get_window( req->window );
  2360. if (win)
  2361. {
  2362. atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
  2363. if (atom) reply->data = remove_property( win, atom );
  2364. }
  2365. }
  2366. /* get a window property */
  2367. DECL_HANDLER(get_window_property)
  2368. {
  2369. struct unicode_str name = get_req_unicode_str();
  2370. struct window *win = get_window( req->window );
  2371. if (win)
  2372. {
  2373. atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
  2374. if (atom) reply->data = get_property( win, atom );
  2375. }
  2376. }
  2377. /* get the list of properties of a window */
  2378. DECL_HANDLER(get_window_properties)
  2379. {
  2380. property_data_t *data;
  2381. int i, count, max = get_reply_max_size() / sizeof(*data);
  2382. struct window *win = get_window( req->window );
  2383. reply->total = 0;
  2384. if (!win) return;
  2385. for (i = count = 0; i < win->prop_inuse; i++)
  2386. if (win->properties[i].type != PROP_TYPE_FREE) count++;
  2387. reply->total = count;
  2388. if (count > max) count = max;
  2389. if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
  2390. for (i = 0; i < win->prop_inuse && count; i++)
  2391. {
  2392. if (win->properties[i].type == PROP_TYPE_FREE) continue;
  2393. data->atom = win->properties[i].atom;
  2394. data->string = (win->properties[i].type == PROP_TYPE_STRING);
  2395. data->data = win->properties[i].data;
  2396. data++;
  2397. count--;
  2398. }
  2399. }
  2400. /* get the new window pointer for a global window, checking permissions */
  2401. /* helper for set_global_windows request */
  2402. static int get_new_global_window( struct window **win, user_handle_t handle )
  2403. {
  2404. if (!handle)
  2405. {
  2406. *win = NULL;
  2407. return 1;
  2408. }
  2409. else if (*win)
  2410. {
  2411. set_error( STATUS_ACCESS_DENIED );
  2412. return 0;
  2413. }
  2414. *win = get_window( handle );
  2415. return (*win != NULL);
  2416. }
  2417. /* Set/get the global windows */
  2418. DECL_HANDLER(set_global_windows)
  2419. {
  2420. struct window *new_shell_window = shell_window;
  2421. struct window *new_shell_listview = shell_listview;
  2422. struct window *new_progman_window = progman_window;
  2423. struct window *new_taskman_window = taskman_window;
  2424. reply->old_shell_window = shell_window ? shell_window->handle : 0;
  2425. reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
  2426. reply->old_progman_window = progman_window ? progman_window->handle : 0;
  2427. reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
  2428. if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
  2429. {
  2430. if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
  2431. if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
  2432. }
  2433. if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
  2434. {
  2435. if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
  2436. }
  2437. if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
  2438. {
  2439. if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
  2440. }
  2441. shell_window = new_shell_window;
  2442. shell_listview = new_shell_listview;
  2443. progman_window = new_progman_window;
  2444. taskman_window = new_taskman_window;
  2445. }
  2446. /* retrieve layered info for a window */
  2447. DECL_HANDLER(get_window_layered_info)
  2448. {
  2449. struct window *win = get_window( req->handle );
  2450. if (!win) return;
  2451. if (win->is_layered)
  2452. {
  2453. reply->color_key = win->color_key;
  2454. reply->alpha = win->alpha;
  2455. reply->flags = win->layered_flags;
  2456. }
  2457. else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
  2458. }
  2459. /* set layered info for a window */
  2460. DECL_HANDLER(set_window_layered_info)
  2461. {
  2462. struct window *win = get_window( req->handle );
  2463. if (!win) return;
  2464. if (win->ex_style & WS_EX_LAYERED)
  2465. {
  2466. int was_layered = win->is_layered;
  2467. if (req->flags & LWA_ALPHA) win->alpha = req->alpha;
  2468. else if (!win->is_layered) win->alpha = 0; /* alpha init value is 0 */
  2469. win->color_key = req->color_key;
  2470. win->layered_flags = req->flags;
  2471. win->is_layered = 1;
  2472. /* repaint since we know now it's not going to use UpdateLayeredWindow */
  2473. if (!was_layered) redraw_window( win, 0, 1, RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
  2474. }
  2475. else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
  2476. }