win32u: Use NtGdiDdDDICreateDCFromMemory for gdi16 DIBDRV.
[wine.git] / dlls / win32u / dce.c
blob5c4f691b460e46aa731b2b595b5a9283cd0b0d41
1 /*
2 * Window painting functions
4 * Copyright 1993, 1994, 1995, 2001, 2004, 2005, 2008 Alexandre Julliard
5 * Copyright 1996, 1997, 1999 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #if 0
23 #pragma makedep unix
24 #endif
26 #include <assert.h>
27 #include <pthread.h>
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "ntgdi_private.h"
31 #include "ntuser_private.h"
32 #include "wine/server.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(win);
37 struct dce
39 struct list entry; /* entry in global DCE list */
40 HDC hdc;
41 HWND hwnd;
42 HRGN clip_rgn;
43 UINT flags;
44 LONG count; /* usage count; 0 or 1 for cache DCEs, always 1 for window DCEs,
45 always >= 1 for class DCEs */
48 static struct list dce_list = LIST_INIT(dce_list);
50 #define DCE_CACHE_SIZE 64
52 static struct list window_surfaces = LIST_INIT( window_surfaces );
53 static pthread_mutex_t surfaces_lock = PTHREAD_MUTEX_INITIALIZER;
55 /*******************************************************************
56 * Dummy window surface for windows that shouldn't get painted.
59 static void dummy_surface_lock( struct window_surface *window_surface )
61 /* nothing to do */
64 static void dummy_surface_unlock( struct window_surface *window_surface )
66 /* nothing to do */
69 static void *dummy_surface_get_bitmap_info( struct window_surface *window_surface, BITMAPINFO *info )
71 static DWORD dummy_data;
73 info->bmiHeader.biSize = sizeof( info->bmiHeader );
74 info->bmiHeader.biWidth = dummy_surface.rect.right;
75 info->bmiHeader.biHeight = dummy_surface.rect.bottom;
76 info->bmiHeader.biPlanes = 1;
77 info->bmiHeader.biBitCount = 32;
78 info->bmiHeader.biCompression = BI_RGB;
79 info->bmiHeader.biSizeImage = 0;
80 info->bmiHeader.biXPelsPerMeter = 0;
81 info->bmiHeader.biYPelsPerMeter = 0;
82 info->bmiHeader.biClrUsed = 0;
83 info->bmiHeader.biClrImportant = 0;
84 return &dummy_data;
87 static RECT *dummy_surface_get_bounds( struct window_surface *window_surface )
89 static RECT dummy_bounds;
90 return &dummy_bounds;
93 static void dummy_surface_set_region( struct window_surface *window_surface, HRGN region )
95 /* nothing to do */
98 static void dummy_surface_flush( struct window_surface *window_surface )
100 /* nothing to do */
103 static void dummy_surface_destroy( struct window_surface *window_surface )
105 /* nothing to do */
108 static const struct window_surface_funcs dummy_surface_funcs =
110 dummy_surface_lock,
111 dummy_surface_unlock,
112 dummy_surface_get_bitmap_info,
113 dummy_surface_get_bounds,
114 dummy_surface_set_region,
115 dummy_surface_flush,
116 dummy_surface_destroy
119 struct window_surface dummy_surface = { &dummy_surface_funcs, { NULL, NULL }, 1, { 0, 0, 1, 1 } };
121 /*******************************************************************
122 * Off-screen window surface.
125 struct offscreen_window_surface
127 struct window_surface header;
128 pthread_mutex_t mutex;
129 RECT bounds;
130 char *bits;
131 BITMAPINFO info;
134 static const struct window_surface_funcs offscreen_window_surface_funcs;
136 static struct offscreen_window_surface *impl_from_window_surface( struct window_surface *base )
138 if (!base || base->funcs != &offscreen_window_surface_funcs) return NULL;
139 return CONTAINING_RECORD( base, struct offscreen_window_surface, header );
142 static void offscreen_window_surface_lock( struct window_surface *base )
144 struct offscreen_window_surface *impl = impl_from_window_surface( base );
145 pthread_mutex_lock( &impl->mutex );
148 static void offscreen_window_surface_unlock( struct window_surface *base )
150 struct offscreen_window_surface *impl = impl_from_window_surface( base );
151 pthread_mutex_unlock( &impl->mutex );
154 static RECT *offscreen_window_surface_get_bounds( struct window_surface *base )
156 struct offscreen_window_surface *impl = impl_from_window_surface( base );
157 return &impl->bounds;
160 static void *offscreen_window_surface_get_bitmap_info( struct window_surface *base, BITMAPINFO *info )
162 struct offscreen_window_surface *impl = impl_from_window_surface( base );
163 info->bmiHeader = impl->info.bmiHeader;
164 return impl->bits;
167 static void offscreen_window_surface_set_region( struct window_surface *base, HRGN region )
171 static void offscreen_window_surface_flush( struct window_surface *base )
173 struct offscreen_window_surface *impl = impl_from_window_surface( base );
174 base->funcs->lock( base );
175 reset_bounds( &impl->bounds );
176 base->funcs->unlock( base );
179 static void offscreen_window_surface_destroy( struct window_surface *base )
181 struct offscreen_window_surface *impl = impl_from_window_surface( base );
182 free( impl );
185 static const struct window_surface_funcs offscreen_window_surface_funcs =
187 offscreen_window_surface_lock,
188 offscreen_window_surface_unlock,
189 offscreen_window_surface_get_bitmap_info,
190 offscreen_window_surface_get_bounds,
191 offscreen_window_surface_set_region,
192 offscreen_window_surface_flush,
193 offscreen_window_surface_destroy
196 void create_offscreen_window_surface( const RECT *visible_rect, struct window_surface **surface )
198 struct offscreen_window_surface *impl;
199 SIZE_T size;
200 RECT surface_rect = *visible_rect;
201 pthread_mutexattr_t attr;
203 TRACE( "visible_rect %s, surface %p.\n", wine_dbgstr_rect( visible_rect ), surface );
205 OffsetRect( &surface_rect, -surface_rect.left, -surface_rect.top );
206 surface_rect.right = (surface_rect.right + 0x1f) & ~0x1f;
207 surface_rect.bottom = (surface_rect.bottom + 0x1f) & ~0x1f;
209 /* check that old surface is an offscreen_window_surface, or release it */
210 if ((impl = impl_from_window_surface( *surface )))
212 /* if the rect didn't change, keep the same surface */
213 if (EqualRect( &surface_rect, &impl->header.rect )) return;
214 window_surface_release( &impl->header );
216 else if (*surface) window_surface_release( *surface );
218 /* create a new window surface */
219 *surface = NULL;
220 size = surface_rect.right * surface_rect.bottom * 4;
221 if (!(impl = calloc(1, offsetof( struct offscreen_window_surface, info.bmiColors[0] ) + size))) return;
223 impl->header.funcs = &offscreen_window_surface_funcs;
224 impl->header.ref = 1;
225 impl->header.rect = surface_rect;
227 pthread_mutexattr_init( &attr );
228 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
229 pthread_mutex_init( &impl->mutex, &attr );
230 pthread_mutexattr_destroy( &attr );
232 reset_bounds( &impl->bounds );
234 impl->bits = (char *)&impl->info.bmiColors[0];
235 impl->info.bmiHeader.biSize = sizeof( impl->info );
236 impl->info.bmiHeader.biWidth = surface_rect.right;
237 impl->info.bmiHeader.biHeight = surface_rect.bottom;
238 impl->info.bmiHeader.biPlanes = 1;
239 impl->info.bmiHeader.biBitCount = 32;
240 impl->info.bmiHeader.biCompression = BI_RGB;
241 impl->info.bmiHeader.biSizeImage = size;
243 TRACE( "created window surface %p\n", &impl->header );
245 *surface = &impl->header;
248 /*******************************************************************
249 * register_window_surface
251 * Register a window surface in the global list, possibly replacing another one.
253 void register_window_surface( struct window_surface *old, struct window_surface *new )
255 if (old == &dummy_surface) old = NULL;
256 if (new == &dummy_surface) new = NULL;
257 if (old == new) return;
258 pthread_mutex_lock( &surfaces_lock );
259 if (old) list_remove( &old->entry );
260 if (new) list_add_tail( &window_surfaces, &new->entry );
261 pthread_mutex_unlock( &surfaces_lock );
264 /*******************************************************************
265 * flush_window_surfaces
267 * Flush pending output from all window surfaces.
269 void flush_window_surfaces( BOOL idle )
271 static DWORD last_idle;
272 DWORD now;
273 struct window_surface *surface;
275 pthread_mutex_lock( &surfaces_lock );
276 now = NtGetTickCount();
277 if (idle) last_idle = now;
278 /* if not idle, we only flush if there's evidence that the app never goes idle */
279 else if ((int)(now - last_idle) < 50) goto done;
281 LIST_FOR_EACH_ENTRY( surface, &window_surfaces, struct window_surface, entry )
282 surface->funcs->flush( surface );
283 done:
284 pthread_mutex_unlock( &surfaces_lock );
287 /***********************************************************************
288 * dump_rdw_flags
290 static void dump_rdw_flags(UINT flags)
292 TRACE("flags:");
293 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
294 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
295 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
296 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
297 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
298 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
299 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
300 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
301 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
302 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
303 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
304 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
306 #define RDW_FLAGS \
307 (RDW_INVALIDATE | \
308 RDW_INTERNALPAINT | \
309 RDW_ERASE | \
310 RDW_VALIDATE | \
311 RDW_NOINTERNALPAINT | \
312 RDW_NOERASE | \
313 RDW_NOCHILDREN | \
314 RDW_ALLCHILDREN | \
315 RDW_UPDATENOW | \
316 RDW_ERASENOW | \
317 RDW_FRAME | \
318 RDW_NOFRAME)
320 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
321 TRACE("\n");
322 #undef RDW_FLAGS
325 /***********************************************************************
326 * update_visible_region
328 * Set the visible region and X11 drawable for the DC associated to
329 * a given window.
331 static void update_visible_region( struct dce *dce )
333 struct window_surface *surface = NULL;
334 NTSTATUS status;
335 HRGN vis_rgn = 0;
336 HWND top_win = 0;
337 DWORD flags = dce->flags;
338 DWORD paint_flags = 0;
339 size_t size = 256;
340 RECT win_rect, top_rect;
341 WND *win;
343 /* don't clip siblings if using parent clip region */
344 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
346 /* fetch the visible region from the server */
349 RGNDATA *data = malloc( sizeof(*data) + size - 1 );
350 if (!data) return;
352 SERVER_START_REQ( get_visible_region )
354 req->window = wine_server_user_handle( dce->hwnd );
355 req->flags = flags;
356 wine_server_set_reply( req, data->Buffer, size );
357 if (!(status = wine_server_call( req )))
359 size_t reply_size = wine_server_reply_size( reply );
360 data->rdh.dwSize = sizeof(data->rdh);
361 data->rdh.iType = RDH_RECTANGLES;
362 data->rdh.nCount = reply_size / sizeof(RECT);
363 data->rdh.nRgnSize = reply_size;
364 vis_rgn = NtGdiExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
366 top_win = wine_server_ptr_handle( reply->top_win );
367 win_rect.left = reply->win_rect.left;
368 win_rect.top = reply->win_rect.top;
369 win_rect.right = reply->win_rect.right;
370 win_rect.bottom = reply->win_rect.bottom;
371 top_rect.left = reply->top_rect.left;
372 top_rect.top = reply->top_rect.top;
373 top_rect.right = reply->top_rect.right;
374 top_rect.bottom = reply->top_rect.bottom;
375 paint_flags = reply->paint_flags;
377 else size = reply->total_size;
379 SERVER_END_REQ;
380 free( data );
381 } while (status == STATUS_BUFFER_OVERFLOW);
383 if (status || !vis_rgn) return;
385 user_driver->pGetDC( dce->hdc, dce->hwnd, top_win, &win_rect, &top_rect, flags );
387 if (dce->clip_rgn) NtGdiCombineRgn( vis_rgn, vis_rgn, dce->clip_rgn,
388 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
390 /* don't use a surface to paint the client area of OpenGL windows */
391 if (!(paint_flags & SET_WINPOS_PIXEL_FORMAT) || (flags & DCX_WINDOW))
393 win = get_win_ptr( top_win );
394 if (win && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
396 surface = win->surface;
397 if (surface) window_surface_add_ref( surface );
398 release_win_ptr( win );
402 if (!surface) SetRectEmpty( &top_rect );
403 set_visible_region( dce->hdc, vis_rgn, &win_rect, &top_rect, surface );
404 if (surface) window_surface_release( surface );
407 /***********************************************************************
408 * release_dce
410 static void release_dce( struct dce *dce )
412 if (!dce->hwnd) return; /* already released */
414 set_visible_region( dce->hdc, 0, &dummy_surface.rect, &dummy_surface.rect, &dummy_surface );
415 user_driver->pReleaseDC( dce->hwnd, dce->hdc );
417 if (dce->clip_rgn) NtGdiDeleteObjectApp( dce->clip_rgn );
418 dce->clip_rgn = 0;
419 dce->hwnd = 0;
420 dce->flags &= DCX_CACHE;
423 /***********************************************************************
424 * delete_clip_rgn
426 static void delete_clip_rgn( struct dce *dce )
428 if (!dce->clip_rgn) return; /* nothing to do */
430 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
431 NtGdiDeleteObjectApp( dce->clip_rgn );
432 dce->clip_rgn = 0;
434 /* make it dirty so that the vis rgn gets recomputed next time */
435 set_dce_flags( dce->hdc, DCHF_INVALIDATEVISRGN );
438 /***********************************************************************
439 * delete_dce
441 BOOL delete_dce( struct dce *dce )
443 BOOL ret = TRUE;
445 TRACE( "hdc = %p\n", dce->hdc );
447 user_lock();
448 if (!(dce->flags & DCX_CACHE))
450 WARN("Application trying to delete an owned DC %p\n", dce->hdc);
451 ret = FALSE;
453 else
455 list_remove( &dce->entry );
456 if (dce->clip_rgn) NtGdiDeleteObjectApp( dce->clip_rgn );
457 free( dce );
459 user_unlock();
460 return ret;
463 /***********************************************************************
464 * update_dc
466 * Make sure the DC vis region is up to date.
467 * This function may need user lock so the GDI lock should _not_
468 * be held when calling it.
470 void update_dc( DC *dc )
472 if (!dc->dirty) return;
473 dc->dirty = 0;
474 if (dc->dce)
476 if (dc->dce->count) update_visible_region( dc->dce );
477 else /* non-fatal but shouldn't happen */
478 WARN("DC is not in use!\n");
482 /***********************************************************************
483 * alloc_dce
485 * Allocate a new DCE.
487 static struct dce *alloc_dce(void)
489 struct dce *dce;
491 if (!(dce = malloc( sizeof(*dce) ))) return NULL;
492 if (!(dce->hdc = NtGdiOpenDCW( NULL, NULL, NULL, 0, TRUE, 0, NULL, NULL )))
494 free( dce );
495 return 0;
497 dce->hwnd = 0;
498 dce->clip_rgn = 0;
499 dce->flags = 0;
500 dce->count = 1;
502 set_dc_dce( dce->hdc, dce );
503 return dce;
506 /***********************************************************************
507 * get_window_dce
509 static struct dce *get_window_dce( HWND hwnd )
511 struct dce *dce;
512 WND *win = get_win_ptr( hwnd );
514 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return NULL;
516 dce = win->dce;
517 if (!dce && (dce = get_class_dce( win->class )))
519 win->dce = dce;
520 dce->count++;
522 release_win_ptr( win );
524 if (!dce) /* try to allocate one */
526 struct dce *dce_to_free = NULL;
527 LONG class_style = get_class_long( hwnd, GCL_STYLE, FALSE );
529 if (class_style & CS_CLASSDC)
531 if (!(dce = alloc_dce())) return NULL;
533 win = get_win_ptr( hwnd );
534 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
536 if (win->dce) /* another thread beat us to it */
538 dce_to_free = dce;
539 dce = win->dce;
541 else if ((win->dce = set_class_dce( win->class, dce )) != dce)
543 dce_to_free = dce;
544 dce = win->dce;
545 dce->count++;
547 else
549 dce->count++;
550 list_add_tail( &dce_list, &dce->entry );
552 release_win_ptr( win );
554 else dce_to_free = dce;
556 else if (class_style & CS_OWNDC)
558 if (!(dce = alloc_dce())) return NULL;
560 win = get_win_ptr( hwnd );
561 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
563 if (win->dwStyle & WS_CLIPCHILDREN) dce->flags |= DCX_CLIPCHILDREN;
564 if (win->dwStyle & WS_CLIPSIBLINGS) dce->flags |= DCX_CLIPSIBLINGS;
565 if (win->dce) /* another thread beat us to it */
567 dce_to_free = dce;
568 dce = win->dce;
570 else
572 win->dce = dce;
573 dce->hwnd = hwnd;
574 list_add_tail( &dce_list, &dce->entry );
576 release_win_ptr( win );
578 else dce_to_free = dce;
581 if (dce_to_free)
583 set_dc_dce( dce_to_free->hdc, NULL );
584 NtGdiDeleteObjectApp( dce_to_free->hdc );
585 free( dce_to_free );
586 if (dce_to_free == dce)
587 dce = NULL;
590 return dce;
593 /***********************************************************************
594 * free_dce
596 * Free a class or window DCE.
598 void free_dce( struct dce *dce, HWND hwnd )
600 struct dce *dce_to_free = NULL;
602 user_lock();
604 if (dce)
606 if (!--dce->count)
608 release_dce( dce );
609 list_remove( &dce->entry );
610 dce_to_free = dce;
612 else if (dce->hwnd == hwnd)
614 release_dce( dce );
618 /* now check for cache DCEs */
620 if (hwnd)
622 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
624 if (dce->hwnd != hwnd) continue;
625 if (!(dce->flags & DCX_CACHE)) break;
627 release_dce( dce );
628 if (dce->count)
630 WARN( "GetDC() without ReleaseDC() for window %p\n", hwnd );
631 dce->count = 0;
632 set_dce_flags( dce->hdc, DCHF_DISABLEDC );
637 user_unlock();
639 if (dce_to_free)
641 set_dc_dce( dce_to_free->hdc, NULL );
642 NtGdiDeleteObjectApp( dce_to_free->hdc );
643 free( dce_to_free );
647 /***********************************************************************
648 * make_dc_dirty
650 * Mark the associated DC as dirty to force a refresh of the visible region
652 static void make_dc_dirty( struct dce *dce )
654 if (!dce->count)
656 /* Don't bother with visible regions of unused DCEs */
657 TRACE("purged %p hwnd %p\n", dce->hdc, dce->hwnd);
658 release_dce( dce );
660 else
662 /* Set dirty bits in the hDC and DCE structs */
663 TRACE("fixed up %p hwnd %p\n", dce->hdc, dce->hwnd);
664 set_dce_flags( dce->hdc, DCHF_INVALIDATEVISRGN );
668 /***********************************************************************
669 * invalidate_dce
671 * It is called from SetWindowPos() - we have to
672 * mark as dirty all busy DCEs for windows that have pWnd->parent as
673 * an ancestor and whose client rect intersects with specified update
674 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
675 * DCX_CLIPCHILDREN flag is set.
677 void invalidate_dce( WND *win, const RECT *extra_rect )
679 UINT context;
680 RECT window_rect;
681 struct dce *dce;
683 if (!win->parent) return;
685 context = set_thread_dpi_awareness_context( get_window_dpi_awareness_context( win->obj.handle ));
686 get_window_rect( win->obj.handle, &window_rect, get_thread_dpi() );
688 TRACE("%p parent %p %s (%s)\n",
689 win->obj.handle, win->parent, wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(extra_rect) );
691 /* walk all DCEs and fixup non-empty entries */
693 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
695 if (!dce->hwnd) continue;
697 TRACE( "%p: hwnd %p dcx %08x %s %s\n", dce->hdc, dce->hwnd, dce->flags,
698 (dce->flags & DCX_CACHE) ? "Cache" : "Owned", dce->count ? "InUse" : "" );
700 if ((dce->hwnd == win->parent) && !(dce->flags & DCX_CLIPCHILDREN))
701 continue; /* child window positions don't bother us */
703 /* if DCE window is a child of hwnd, it has to be invalidated */
704 if (dce->hwnd == win->obj.handle || is_child( win->obj.handle, dce->hwnd ))
706 make_dc_dirty( dce );
707 continue;
710 /* otherwise check if the window rectangle intersects this DCE window */
711 if (win->parent == dce->hwnd || is_child( win->parent, dce->hwnd ))
713 RECT dce_rect, tmp;
714 get_window_rect( dce->hwnd, &dce_rect, get_thread_dpi() );
715 if (intersect_rect( &tmp, &dce_rect, &window_rect ) ||
716 (extra_rect && intersect_rect( &tmp, &dce_rect, extra_rect )))
717 make_dc_dirty( dce );
720 set_thread_dpi_awareness_context( context );
723 /***********************************************************************
724 * release_dc
726 static INT release_dc( HWND hwnd, HDC hdc, BOOL end_paint )
728 struct dce *dce;
729 BOOL ret = FALSE;
731 TRACE( "%p %p\n", hwnd, hdc );
733 user_lock();
734 dce = get_dc_dce( hdc );
735 if (dce && dce->count && dce->hwnd)
737 if (!(dce->flags & DCX_NORESETATTRS)) set_dce_flags( dce->hdc, DCHF_RESETDC );
738 if (end_paint || (dce->flags & DCX_CACHE)) delete_clip_rgn( dce );
739 if (dce->flags & DCX_CACHE)
741 dce->count = 0;
742 set_dce_flags( dce->hdc, DCHF_DISABLEDC );
744 ret = TRUE;
746 user_unlock();
747 return ret;
750 /***********************************************************************
751 * NtUserGetDCEx (win32u.@)
753 HDC WINAPI NtUserGetDCEx( HWND hwnd, HRGN clip_rgn, DWORD flags )
755 const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
756 const DWORD user_flags = clip_flags | DCX_NORESETATTRS; /* flags that can be set by user */
757 BOOL update_vis_rgn = TRUE;
758 struct dce *dce;
759 HWND parent;
760 LONG window_style = get_window_long( hwnd, GWL_STYLE );
762 if (!hwnd) hwnd = get_desktop_window();
763 else hwnd = get_full_window_handle( hwnd );
765 TRACE( "hwnd %p, clip_rgn %p, flags %08x\n", hwnd, clip_rgn, (int)flags );
767 if (!is_window(hwnd)) return 0;
769 /* fixup flags */
771 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
773 if (flags & DCX_USESTYLE)
775 flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
777 if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
779 if (!(flags & DCX_WINDOW))
781 if (get_class_long( hwnd, GCL_STYLE, FALSE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
783 if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
784 flags |= DCX_CLIPCHILDREN;
788 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
790 parent = NtUserGetAncestor( hwnd, GA_PARENT );
791 if (!parent || (parent == get_desktop_window()))
792 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
794 /* it seems parent clip is ignored when clipping siblings or children */
795 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
797 if( flags & DCX_PARENTCLIP )
799 LONG parent_style = get_window_long( parent, GWL_STYLE );
800 if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
802 flags &= ~DCX_CLIPCHILDREN;
803 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
807 /* find a suitable DCE */
809 if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
811 struct dce *dceEmpty = NULL, *dceUnused = NULL, *found = NULL;
812 unsigned int count = 0;
814 /* Strategy: First, we attempt to find a non-empty but unused DCE with
815 * compatible flags. Next, we look for an empty entry. If the cache is
816 * full we have to purge one of the unused entries.
818 user_lock();
819 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
821 if (!(dce->flags & DCX_CACHE)) break;
822 count++;
823 if (dce->count) continue;
824 dceUnused = dce;
825 if (!dce->hwnd) dceEmpty = dce;
826 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
828 TRACE( "found valid %p hwnd %p, flags %08x\n", dce->hdc, hwnd, dce->flags );
829 found = dce;
830 update_vis_rgn = FALSE;
831 break;
834 if (!found) found = dceEmpty;
835 if (!found && count >= DCE_CACHE_SIZE) found = dceUnused;
837 dce = found;
838 if (dce)
840 dce->count = 1;
841 set_dce_flags( dce->hdc, DCHF_ENABLEDC );
843 user_unlock();
845 /* if there's no dce empty or unused, allocate a new one */
846 if (!dce)
848 if (!(dce = alloc_dce())) return 0;
849 dce->flags = DCX_CACHE;
850 user_lock();
851 list_add_head( &dce_list, &dce->entry );
852 user_unlock();
855 else
857 flags |= DCX_NORESETATTRS;
858 if (dce->hwnd != hwnd)
860 /* we should free dce->clip_rgn here, but Windows apparently doesn't */
861 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
862 dce->clip_rgn = 0;
864 else update_vis_rgn = FALSE; /* updated automatically, via DCHook() */
867 if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
869 /* if the extra clip region has changed, get rid of the old one */
870 if (dce->clip_rgn != clip_rgn || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
871 delete_clip_rgn( dce );
872 dce->clip_rgn = clip_rgn;
873 if (!dce->clip_rgn) dce->clip_rgn = NtGdiCreateRectRgn( 0, 0, 0, 0 );
874 dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
875 update_vis_rgn = TRUE;
878 if (get_window_long( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
879 NtGdiSetLayout( dce->hdc, -1, LAYOUT_RTL );
881 dce->hwnd = hwnd;
882 dce->flags = (dce->flags & ~user_flags) | (flags & user_flags);
884 /* cross-process invalidation is not supported yet, so always update the vis rgn */
885 if (!is_current_process_window( hwnd )) update_vis_rgn = TRUE;
887 if (set_dce_flags( dce->hdc, DCHF_VALIDATEVISRGN )) update_vis_rgn = TRUE; /* DC was dirty */
889 if (update_vis_rgn) update_visible_region( dce );
891 TRACE( "(%p,%p,0x%x): returning %p%s\n", hwnd, clip_rgn, (int)flags, dce->hdc,
892 update_vis_rgn ? " (updated)" : "" );
893 return dce->hdc;
896 /***********************************************************************
897 * NtUserReleaseDC (win32u.@)
899 INT WINAPI NtUserReleaseDC( HWND hwnd, HDC hdc )
901 return release_dc( hwnd, hdc, FALSE );
904 /***********************************************************************
905 * NtUserGetDC (win32u.@)
907 HDC WINAPI NtUserGetDC( HWND hwnd )
909 if (!hwnd) return NtUserGetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
910 return NtUserGetDCEx( hwnd, 0, DCX_USESTYLE );
913 /***********************************************************************
914 * NtUserGetWindowDC (win32u.@)
916 HDC WINAPI NtUserGetWindowDC( HWND hwnd )
918 return NtUserGetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
921 /**********************************************************************
922 * NtUserWindowFromDC (win32u.@)
924 HWND WINAPI NtUserWindowFromDC( HDC hdc )
926 struct dce *dce;
927 HWND hwnd = 0;
929 user_lock();
930 dce = get_dc_dce( hdc );
931 if (dce) hwnd = dce->hwnd;
932 user_unlock();
933 return hwnd;
936 /***********************************************************************
937 * get_update_region
939 * Return update region (in screen coordinates) for a window.
941 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
943 HRGN hrgn = 0;
944 NTSTATUS status;
945 RGNDATA *data;
946 size_t size = 256;
950 if (!(data = malloc( sizeof(*data) + size - 1 )))
952 RtlSetLastWin32Error( ERROR_OUTOFMEMORY );
953 return 0;
956 SERVER_START_REQ( get_update_region )
958 req->window = wine_server_user_handle( hwnd );
959 req->from_child = wine_server_user_handle( child ? *child : 0 );
960 req->flags = *flags;
961 wine_server_set_reply( req, data->Buffer, size );
962 if (!(status = wine_server_call( req )))
964 size_t reply_size = wine_server_reply_size( reply );
965 data->rdh.dwSize = sizeof(data->rdh);
966 data->rdh.iType = RDH_RECTANGLES;
967 data->rdh.nCount = reply_size / sizeof(RECT);
968 data->rdh.nRgnSize = reply_size;
969 hrgn = NtGdiExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
970 if (child) *child = wine_server_ptr_handle( reply->child );
971 *flags = reply->flags;
973 else size = reply->total_size;
975 SERVER_END_REQ;
976 free( data );
977 } while (status == STATUS_BUFFER_OVERFLOW);
979 if (status) RtlSetLastWin32Error( RtlNtStatusToDosError(status) );
980 return hrgn;
983 /***********************************************************************
984 * redraw_window_rects
986 * Redraw part of a window.
988 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
990 BOOL ret;
992 if (!(flags & (RDW_INVALIDATE|RDW_VALIDATE|RDW_INTERNALPAINT|RDW_NOINTERNALPAINT)))
993 return TRUE; /* nothing to do */
995 SERVER_START_REQ( redraw_window )
997 req->window = wine_server_user_handle( hwnd );
998 req->flags = flags;
999 wine_server_add_data( req, rects, count * sizeof(RECT) );
1000 ret = !wine_server_call_err( req );
1002 SERVER_END_REQ;
1003 return ret;
1006 /***********************************************************************
1007 * get_update_flags
1009 * Get only the update flags, not the update region.
1011 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
1013 BOOL ret;
1015 SERVER_START_REQ( get_update_region )
1017 req->window = wine_server_user_handle( hwnd );
1018 req->from_child = wine_server_user_handle( child ? *child : 0 );
1019 req->flags = *flags | UPDATE_NOREGION;
1020 if ((ret = !wine_server_call_err( req )))
1022 if (child) *child = wine_server_ptr_handle( reply->child );
1023 *flags = reply->flags;
1026 SERVER_END_REQ;
1027 return ret;
1030 /***********************************************************************
1031 * send_ncpaint
1033 * Send a WM_NCPAINT message if needed, and return the resulting update region (in screen coords).
1034 * Helper for erase_now and BeginPaint.
1036 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
1038 HRGN whole_rgn = get_update_region( hwnd, flags, child );
1039 HRGN client_rgn = 0;
1040 DWORD style;
1042 if (child) hwnd = *child;
1044 if (hwnd == get_desktop_window()) return whole_rgn;
1046 if (whole_rgn)
1048 UINT context;
1049 RECT client, window, update;
1050 INT type;
1052 context = set_thread_dpi_awareness_context( get_window_dpi_awareness_context( hwnd ));
1054 /* check if update rgn overlaps with nonclient area */
1055 type = NtGdiGetRgnBox( whole_rgn, &update );
1056 get_window_rects( hwnd, COORDS_SCREEN, &window, &client, get_thread_dpi() );
1058 if ((*flags & UPDATE_NONCLIENT) ||
1059 update.left < client.left || update.top < client.top ||
1060 update.right > client.right || update.bottom > client.bottom)
1062 client_rgn = NtGdiCreateRectRgn( client.left, client.top, client.right, client.bottom );
1063 NtGdiCombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
1065 /* check if update rgn contains complete nonclient area */
1066 if (type == SIMPLEREGION && EqualRect( &window, &update ))
1068 NtGdiDeleteObjectApp( whole_rgn );
1069 whole_rgn = (HRGN)1;
1072 else
1074 client_rgn = whole_rgn;
1075 whole_rgn = 0;
1078 if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
1080 if (*flags & UPDATE_NONCLIENT)
1082 /* Mark standard scroll bars as not painted before sending WM_NCPAINT */
1083 style = get_window_long( hwnd, GWL_STYLE );
1084 if (style & WS_HSCROLL)
1085 set_standard_scroll_painted( hwnd, SB_HORZ, FALSE );
1086 if (style & WS_VSCROLL)
1087 set_standard_scroll_painted( hwnd, SB_VERT, FALSE );
1089 send_message( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
1091 if (whole_rgn > (HRGN)1) NtGdiDeleteObjectApp( whole_rgn );
1093 set_thread_dpi_awareness_context( context );
1095 return client_rgn;
1098 /***********************************************************************
1099 * send_erase
1101 * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
1102 * If a DC is requested, the region is selected into it. In all cases the region is deleted.
1103 * Helper for erase_now and BeginPaint.
1105 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
1106 RECT *clip_rect, HDC *hdc_ret )
1108 BOOL need_erase = (flags & UPDATE_DELAYED_ERASE) != 0;
1109 HDC hdc = 0;
1110 RECT dummy;
1112 if (!clip_rect) clip_rect = &dummy;
1113 if (hdc_ret || (flags & UPDATE_ERASE))
1115 UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
1116 if (is_iconic(hwnd)) dcx_flags |= DCX_WINDOW;
1118 if ((hdc = NtUserGetDCEx( hwnd, client_rgn, dcx_flags )))
1120 INT type = NtGdiGetAppClipBox( hdc, clip_rect );
1122 if (flags & UPDATE_ERASE)
1124 /* don't erase if the clip box is empty */
1125 if (type != NULLREGION)
1126 need_erase = !send_message( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
1128 if (!hdc_ret) release_dc( hwnd, hdc, TRUE );
1131 if (hdc_ret) *hdc_ret = hdc;
1133 if (!hdc) NtGdiDeleteObjectApp( client_rgn );
1134 return need_erase;
1137 /***********************************************************************
1138 * copy_bits_from_surface
1140 * Copy bits from a window surface; helper for move_window_bits and move_window_bits_parent.
1142 static void copy_bits_from_surface( HWND hwnd, struct window_surface *surface,
1143 const RECT *dst, const RECT *src )
1145 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
1146 BITMAPINFO *info = (BITMAPINFO *)buffer;
1147 void *bits;
1148 UINT flags = UPDATE_NOCHILDREN | UPDATE_CLIPCHILDREN;
1149 HRGN rgn = get_update_region( hwnd, &flags, NULL );
1150 HDC hdc = NtUserGetDCEx( hwnd, rgn, DCX_CACHE | DCX_WINDOW | DCX_EXCLUDERGN );
1152 bits = surface->funcs->get_info( surface, info );
1153 surface->funcs->lock( surface );
1154 NtGdiSetDIBitsToDeviceInternal( hdc, dst->left, dst->top, dst->right - dst->left, dst->bottom - dst->top,
1155 src->left - surface->rect.left, surface->rect.bottom - src->bottom,
1156 0, surface->rect.bottom - surface->rect.top,
1157 bits, info, DIB_RGB_COLORS, 0, 0, FALSE, NULL );
1158 surface->funcs->unlock( surface );
1159 NtUserReleaseDC( hwnd, hdc );
1162 /***********************************************************************
1163 * move_window_bits
1165 * Move the window bits when a window is resized or its surface recreated.
1167 void move_window_bits( HWND hwnd, struct window_surface *old_surface,
1168 struct window_surface *new_surface,
1169 const RECT *visible_rect, const RECT *old_visible_rect,
1170 const RECT *window_rect, const RECT *valid_rects )
1172 RECT dst = valid_rects[0];
1173 RECT src = valid_rects[1];
1175 if (new_surface != old_surface ||
1176 src.left - old_visible_rect->left != dst.left - visible_rect->left ||
1177 src.top - old_visible_rect->top != dst.top - visible_rect->top)
1179 TRACE( "copying %s -> %s\n", wine_dbgstr_rect( &src ), wine_dbgstr_rect( &dst ));
1180 OffsetRect( &src, -old_visible_rect->left, -old_visible_rect->top );
1181 OffsetRect( &dst, -window_rect->left, -window_rect->top );
1182 copy_bits_from_surface( hwnd, old_surface, &dst, &src );
1187 /***********************************************************************
1188 * move_window_bits_parent
1190 * Move the window bits in the parent surface when a child is moved.
1192 void move_window_bits_parent( HWND hwnd, HWND parent, const RECT *window_rect, const RECT *valid_rects )
1194 struct window_surface *surface;
1195 RECT dst = valid_rects[0];
1196 RECT src = valid_rects[1];
1197 WND *win;
1199 if (src.left == dst.left && src.top == dst.top) return;
1201 if (!(win = get_win_ptr( parent ))) return;
1202 if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return;
1203 if (!(surface = win->surface))
1205 release_win_ptr( win );
1206 return;
1209 TRACE( "copying %s -> %s\n", wine_dbgstr_rect( &src ), wine_dbgstr_rect( &dst ));
1210 map_window_points( NtUserGetAncestor( hwnd, GA_PARENT ), parent, (POINT *)&src, 2, get_thread_dpi() );
1211 OffsetRect( &src, win->client_rect.left - win->visible_rect.left,
1212 win->client_rect.top - win->visible_rect.top );
1213 OffsetRect( &dst, -window_rect->left, -window_rect->top );
1214 window_surface_add_ref( surface );
1215 release_win_ptr( win );
1217 copy_bits_from_surface( hwnd, surface, &dst, &src );
1218 window_surface_release( surface );
1221 /***********************************************************************
1222 * NtUserBeginPaint (win32u.@)
1224 HDC WINAPI NtUserBeginPaint( HWND hwnd, PAINTSTRUCT *ps )
1226 HRGN hrgn;
1227 HDC hdc;
1228 BOOL erase;
1229 RECT rect;
1230 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
1232 NtUserHideCaret( hwnd );
1234 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
1236 erase = send_erase( hwnd, flags, hrgn, &rect, &hdc );
1238 TRACE( "hdc = %p box = (%s), fErase = %d\n", hdc, wine_dbgstr_rect(&rect), erase );
1240 if (!ps)
1242 release_dc( hwnd, hdc, TRUE );
1243 return 0;
1245 ps->fErase = erase;
1246 ps->rcPaint = rect;
1247 ps->hdc = hdc;
1248 return hdc;
1251 /***********************************************************************
1252 * NtUserEndPaint (win32u.@)
1254 BOOL WINAPI NtUserEndPaint( HWND hwnd, const PAINTSTRUCT *ps )
1256 NtUserShowCaret( hwnd );
1257 flush_window_surfaces( FALSE );
1258 if (!ps) return FALSE;
1259 release_dc( hwnd, ps->hdc, TRUE );
1260 return TRUE;
1263 /***********************************************************************
1264 * erase_now
1266 * Implementation of RDW_ERASENOW behavior.
1268 void erase_now( HWND hwnd, UINT rdw_flags )
1270 HWND child = 0;
1271 HRGN hrgn;
1272 BOOL need_erase = FALSE;
1274 /* loop while we find a child to repaint */
1275 for (;;)
1277 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
1279 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
1280 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
1281 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1283 if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
1284 need_erase = send_erase( child, flags, hrgn, NULL, NULL );
1286 if (!flags) break; /* nothing more to do */
1287 if ((rdw_flags & RDW_NOCHILDREN) && !need_erase) break;
1291 /***********************************************************************
1292 * update_now
1294 * Implementation of RDW_UPDATENOW behavior.
1296 static void update_now( HWND hwnd, UINT rdw_flags )
1298 HWND child = 0;
1300 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
1301 if (hwnd == get_desktop_window()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
1303 /* loop while we find a child to repaint */
1304 for (;;)
1306 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
1308 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
1309 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
1311 if (!get_update_flags( hwnd, &child, &flags )) break;
1312 if (!flags) break; /* nothing more to do */
1314 send_message( child, WM_PAINT, 0, 0 );
1315 if (rdw_flags & RDW_NOCHILDREN) break;
1319 /***********************************************************************
1320 * NtUserRedrawWindow (win32u.@)
1322 BOOL WINAPI NtUserRedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1324 static const RECT empty;
1325 BOOL ret;
1327 if (TRACE_ON(win))
1329 if (hrgn)
1331 RECT r;
1332 NtGdiGetRgnBox( hrgn, &r );
1333 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1335 else if (rect)
1336 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1337 else
1338 TRACE( "%p whole window ", hwnd );
1340 dump_rdw_flags(flags);
1343 /* process pending expose events before painting */
1344 if (flags & RDW_UPDATENOW) user_driver->pProcessEvents( QS_PAINT );
1346 if (rect && !hrgn)
1348 RECT ordered = *rect;
1350 order_rect( &ordered );
1351 if (IsRectEmpty( &ordered )) ordered = empty;
1352 ret = redraw_window_rects( hwnd, flags, &ordered, 1 );
1354 else if (!hrgn)
1356 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1358 else /* need to build a list of the region rectangles */
1360 DWORD size;
1361 RGNDATA *data;
1363 if (!(size = NtGdiGetRegionData( hrgn, 0, NULL ))) return FALSE;
1364 if (!(data = malloc( size ))) return FALSE;
1365 NtGdiGetRegionData( hrgn, size, data );
1366 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
1367 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1368 else
1369 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1370 free( data );
1373 if (!hwnd) hwnd = get_desktop_window();
1375 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1376 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1378 return ret;
1381 /***********************************************************************
1382 * NtUserValidateRect (win32u.@)
1384 BOOL WINAPI NtUserValidateRect( HWND hwnd, const RECT *rect )
1386 UINT flags = RDW_VALIDATE;
1388 if (!hwnd)
1390 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1391 rect = NULL;
1394 return NtUserRedrawWindow( hwnd, rect, 0, flags );
1397 /***********************************************************************
1398 * NtUserGetUpdateRgn (win32u.@)
1400 INT WINAPI NtUserGetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1402 INT retval = ERROR;
1403 UINT flags = UPDATE_NOCHILDREN, context;
1404 HRGN update_rgn;
1406 context = set_thread_dpi_awareness_context( get_window_dpi_awareness_context( hwnd ));
1408 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1410 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1412 retval = NtGdiCombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1413 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1415 flags = UPDATE_DELAYED_ERASE;
1416 get_update_flags( hwnd, NULL, &flags );
1418 /* map region to client coordinates */
1419 map_window_region( 0, hwnd, hrgn );
1421 set_thread_dpi_awareness_context( context );
1422 return retval;
1425 /***********************************************************************
1426 * NtUserGetUpdateRect (win32u.@)
1428 BOOL WINAPI NtUserGetUpdateRect( HWND hwnd, RECT *rect, BOOL erase )
1430 UINT flags = UPDATE_NOCHILDREN;
1431 HRGN update_rgn;
1432 BOOL need_erase;
1434 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1436 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
1438 if (rect && NtGdiGetRgnBox( update_rgn, rect ) != NULLREGION)
1440 HDC hdc = NtUserGetDCEx( hwnd, 0, DCX_USESTYLE );
1441 DWORD layout = NtGdiSetLayout( hdc, -1, 0 ); /* map_window_points mirrors already */
1442 UINT win_dpi = get_dpi_for_window( hwnd );
1443 map_window_points( 0, hwnd, (POINT *)rect, 2, win_dpi );
1444 *rect = map_dpi_rect( *rect, win_dpi, get_thread_dpi() );
1445 NtGdiTransformPoints( hdc, (POINT *)rect, (POINT *)rect, 2, NtGdiDPtoLP );
1446 NtGdiSetLayout( hdc, -1, layout );
1447 NtUserReleaseDC( hwnd, hdc );
1449 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1451 /* check if we still have an update region */
1452 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1453 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1454 return get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT);
1457 /***********************************************************************
1458 * NtUserExcludeUpdateRgn (win32u.@)
1460 INT WINAPI NtUserExcludeUpdateRgn( HDC hdc, HWND hwnd )
1462 HRGN update_rgn = NtGdiCreateRectRgn( 0, 0, 0, 0 );
1463 INT ret = NtUserGetUpdateRgn( hwnd, update_rgn, FALSE );
1465 if (ret != ERROR)
1467 UINT context;
1468 POINT pt;
1470 context = set_thread_dpi_awareness_context( get_window_dpi_awareness_context( hwnd ));
1471 NtGdiGetDCPoint( hdc, NtGdiGetDCOrg, &pt );
1472 map_window_points( 0, hwnd, &pt, 1, get_thread_dpi() );
1473 NtGdiOffsetRgn( update_rgn, -pt.x, -pt.y );
1474 ret = NtGdiExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1475 set_thread_dpi_awareness_context( context );
1477 NtGdiDeleteObjectApp( update_rgn );
1478 return ret;
1481 /***********************************************************************
1482 * NtUserInvalidateRgn (win32u.@)
1484 BOOL WINAPI NtUserInvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1486 if (!hwnd)
1488 RtlSetLastWin32Error( ERROR_INVALID_WINDOW_HANDLE );
1489 return FALSE;
1492 return NtUserRedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1495 /***********************************************************************
1496 * NtUserInvalidateRect (win32u.@)
1498 BOOL WINAPI NtUserInvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1500 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1502 if (!hwnd)
1504 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1505 rect = NULL;
1508 return NtUserRedrawWindow( hwnd, rect, 0, flags );
1511 /***********************************************************************
1512 * NtUserLockWindowUpdate (win32u.@)
1514 BOOL WINAPI NtUserLockWindowUpdate( HWND hwnd )
1516 static HWND locked_hwnd;
1518 FIXME( "(%p), partial stub!\n", hwnd );
1520 if (!hwnd)
1522 locked_hwnd = NULL;
1523 return TRUE;
1525 return !InterlockedCompareExchangePointer( (void **)&locked_hwnd, hwnd, 0 );
1528 /*************************************************************************
1529 * fix_caret
1531 * Helper for NtUserScrollWindowEx:
1532 * If the return value is 0, no special caret handling is necessary.
1533 * Otherwise the return value is the handle of the window that owns the
1534 * caret. Its caret needs to be hidden during the scroll operation and
1535 * moved to new_caret_pos if move_caret is TRUE.
1537 static HWND fix_caret( HWND hwnd, const RECT *scroll_rect, INT dx, INT dy,
1538 UINT flags, BOOL *move_caret, POINT *new_caret_pos )
1540 RECT rect, mapped_caret;
1541 GUITHREADINFO info;
1543 info.cbSize = sizeof(info);
1544 if (!NtUserGetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
1545 if (!info.hwndCaret) return 0;
1547 mapped_caret = info.rcCaret;
1548 if (info.hwndCaret == hwnd)
1550 /* The caret needs to be moved along with scrolling even if it's
1551 * outside the visible area. Otherwise, when the caret is scrolled
1552 * out from the view, the position won't get updated anymore and
1553 * the caret will never scroll back again. */
1554 *move_caret = TRUE;
1555 new_caret_pos->x = info.rcCaret.left + dx;
1556 new_caret_pos->y = info.rcCaret.top + dy;
1558 else
1560 *move_caret = FALSE;
1561 if (!(flags & SW_SCROLLCHILDREN) || !is_child( hwnd, info.hwndCaret ))
1562 return 0;
1563 map_window_points( info.hwndCaret, hwnd, (POINT *)&mapped_caret, 2, get_thread_dpi() );
1566 /* If the caret is not in the src/dest rects, all is fine done. */
1567 if (!intersect_rect( &rect, scroll_rect, &mapped_caret ))
1569 rect = *scroll_rect;
1570 OffsetRect( &rect, dx, dy );
1571 if (!intersect_rect( &rect, &rect, &mapped_caret ))
1572 return 0;
1575 /* Indicate that the caret needs to be updated during the scrolling. */
1576 return info.hwndCaret;
1579 /*************************************************************************
1580 * NtUserScrollWindowEx (win32u.@)
1582 * Note: contrary to what the doc says, pixels that are scrolled from the
1583 * outside of clipRect to the inside are NOT painted.
1585 INT WINAPI NtUserScrollWindowEx( HWND hwnd, INT dx, INT dy, const RECT *rect,
1586 const RECT *clip_rect, HRGN update_rgn,
1587 RECT *update_rect, UINT flags )
1589 BOOL update = update_rect || update_rgn || flags & (SW_INVALIDATE | SW_ERASE);
1590 BOOL own_rgn = TRUE, move_caret = FALSE;
1591 HRGN temp_rgn, winupd_rgn = 0;
1592 INT retval = NULLREGION;
1593 HWND caret_hwnd = NULL;
1594 POINT new_caret_pos;
1595 RECT rc, cliprc;
1596 int rdw_flags;
1597 HDC hdc;
1599 TRACE( "%p, %d,%d update_rgn=%p update_rect = %p %s %04x\n",
1600 hwnd, dx, dy, update_rgn, update_rect, wine_dbgstr_rect(rect), flags );
1601 TRACE( "clip_rect = %s\n", wine_dbgstr_rect(clip_rect) );
1602 if (flags & ~(SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE | SW_NODCCACHE))
1603 FIXME( "some flags (%04x) are unhandled\n", flags );
1605 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1606 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE;
1608 if (!is_window_drawable( hwnd, TRUE )) return ERROR;
1609 hwnd = get_full_window_handle( hwnd );
1611 get_client_rect( hwnd, &rc );
1612 if (clip_rect) intersect_rect( &cliprc, &rc, clip_rect );
1613 else cliprc = rc;
1615 if (rect) intersect_rect( &rc, &rc, rect );
1616 if (update_rgn) own_rgn = FALSE;
1617 else if (update) update_rgn = NtGdiCreateRectRgn( 0, 0, 0, 0 );
1619 new_caret_pos.x = new_caret_pos.y = 0;
1621 if (!IsRectEmpty( &cliprc ) && (dx || dy))
1623 DWORD style = get_window_long( hwnd, GWL_STYLE );
1624 DWORD dcxflags = 0;
1626 caret_hwnd = fix_caret( hwnd, &rc, dx, dy, flags, &move_caret, &new_caret_pos );
1627 if (caret_hwnd) NtUserHideCaret( caret_hwnd );
1629 if (!(flags & SW_NODCCACHE)) dcxflags |= DCX_CACHE;
1630 if (style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1631 if (get_class_long( hwnd, GCL_STYLE, FALSE ) & CS_PARENTDC) dcxflags |= DCX_PARENTCLIP;
1632 if (!(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1633 dcxflags |= DCX_CLIPCHILDREN;
1634 hdc = NtUserGetDCEx( hwnd, 0, dcxflags);
1635 if (hdc)
1637 NtUserScrollDC( hdc, dx, dy, &rc, &cliprc, update_rgn, update_rect );
1638 NtUserReleaseDC( hwnd, hdc );
1639 if (!update) NtUserRedrawWindow( hwnd, NULL, update_rgn, rdw_flags );
1642 /* If the windows has an update region, this must be scrolled as well.
1643 * Keep a copy in winupd_rgn to be added to hrngUpdate at the end. */
1644 temp_rgn = NtGdiCreateRectRgn( 0, 0, 0, 0 );
1645 retval = NtUserGetUpdateRgn( hwnd, temp_rgn, FALSE );
1646 if (retval != NULLREGION)
1648 HRGN clip_rgn = NtGdiCreateRectRgn( cliprc.left, cliprc.top,
1649 cliprc.right, cliprc.bottom );
1650 if (!own_rgn)
1652 winupd_rgn = NtGdiCreateRectRgn( 0, 0, 0, 0);
1653 NtGdiCombineRgn( winupd_rgn, temp_rgn, 0, RGN_COPY);
1655 NtGdiOffsetRgn( temp_rgn, dx, dy );
1656 NtGdiCombineRgn( temp_rgn, temp_rgn, clip_rgn, RGN_AND );
1657 if (!own_rgn) NtGdiCombineRgn( winupd_rgn, winupd_rgn, temp_rgn, RGN_OR );
1658 NtUserRedrawWindow( hwnd, NULL, temp_rgn, rdw_flags );
1661 * Catch the case where the scrolling amount exceeds the size of the
1662 * original window. This generated a second update area that is the
1663 * location where the original scrolled content would end up.
1664 * This second region is not returned by the ScrollDC and sets
1665 * ScrollWindowEx apart from just a ScrollDC.
1667 * This has been verified with testing on windows.
1669 if (abs( dx ) > abs( rc.right - rc.left ) || abs( dy ) > abs( rc.bottom - rc.top ))
1671 NtGdiSetRectRgn( temp_rgn, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy );
1672 NtGdiCombineRgn( temp_rgn, temp_rgn, clip_rgn, RGN_AND );
1673 NtGdiCombineRgn( update_rgn, update_rgn, temp_rgn, RGN_OR );
1675 if (update_rect)
1677 RECT temp_rect;
1678 NtGdiGetRgnBox( temp_rgn, &temp_rect );
1679 union_rect( update_rect, update_rect, &temp_rect );
1682 if (!own_rgn) NtGdiCombineRgn( winupd_rgn, winupd_rgn, temp_rgn, RGN_OR );
1684 NtGdiDeleteObjectApp( clip_rgn );
1686 NtGdiDeleteObjectApp( temp_rgn );
1688 else
1690 /* nothing was scrolled */
1691 if (!own_rgn) NtGdiSetRectRgn( update_rgn, 0, 0, 0, 0 );
1692 SetRectEmpty( update_rect );
1695 if (flags & SW_SCROLLCHILDREN)
1697 HWND *list = list_window_children( 0, hwnd, NULL, 0 );
1698 if (list)
1700 RECT r, dummy;
1701 int i;
1703 for (i = 0; list[i]; i++)
1705 get_window_rects( list[i], COORDS_PARENT, &r, NULL, get_thread_dpi() );
1706 if (!rect || intersect_rect( &dummy, &r, rect ))
1707 NtUserSetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
1708 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1709 SWP_NOREDRAW | SWP_DEFERERASE );
1711 free( list );
1715 if (flags & (SW_INVALIDATE | SW_ERASE))
1716 NtUserRedrawWindow( hwnd, NULL, update_rgn, rdw_flags |
1717 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1719 if (winupd_rgn)
1721 NtGdiCombineRgn( update_rgn, update_rgn, winupd_rgn, RGN_OR );
1722 NtGdiDeleteObjectApp( winupd_rgn );
1725 if (move_caret) set_caret_pos( new_caret_pos.x, new_caret_pos.y );
1726 if (caret_hwnd) NtUserShowCaret( caret_hwnd );
1727 if (own_rgn && update_rgn) NtGdiDeleteObjectApp( update_rgn );
1729 return retval;
1732 /************************************************************************
1733 * NtUserPrintWindow (win32u.@)
1735 BOOL WINAPI NtUserPrintWindow( HWND hwnd, HDC hdc, UINT flags )
1737 UINT prf_flags = PRF_CHILDREN | PRF_ERASEBKGND | PRF_OWNED | PRF_CLIENT;
1738 if (!(flags & PW_CLIENTONLY)) prf_flags |= PRF_NONCLIENT;
1739 send_message( hwnd, WM_PRINT, (WPARAM)hdc, prf_flags );
1740 return TRUE;