window.c 98 KB

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