window.c 98 KB

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