window.c 99 KB

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