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