window.c 95 KB

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