comctl32: Fix a typo in comment.
[wine.git] / dlls / user32 / painting.c
blobbf3f1809caa018e8a76d4dbce5484baf8064a075
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 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "wine/server.h"
36 #include "win.h"
37 #include "user_private.h"
38 #include "controls.h"
39 #include "wine/gdi_driver.h"
40 #include "wine/list.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(win);
46 struct dce
48 struct list entry; /* entry in global DCE list */
49 HDC hdc;
50 HWND hwnd;
51 HRGN clip_rgn;
52 DWORD flags;
53 LONG count; /* usage count; 0 or 1 for cache DCEs, always 1 for window DCEs,
54 always >= 1 for class DCEs */
57 static struct list dce_list = LIST_INIT(dce_list);
59 #define DCE_CACHE_SIZE 64
61 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam );
63 static const WCHAR displayW[] = { 'D','I','S','P','L','A','Y',0 };
66 /***********************************************************************
67 * dump_rdw_flags
69 static void dump_rdw_flags(UINT flags)
71 TRACE("flags:");
72 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
73 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
74 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
75 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
76 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
77 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
78 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
79 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
80 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
81 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
82 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
83 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
85 #define RDW_FLAGS \
86 (RDW_INVALIDATE | \
87 RDW_INTERNALPAINT | \
88 RDW_ERASE | \
89 RDW_VALIDATE | \
90 RDW_NOINTERNALPAINT | \
91 RDW_NOERASE | \
92 RDW_NOCHILDREN | \
93 RDW_ALLCHILDREN | \
94 RDW_UPDATENOW | \
95 RDW_ERASENOW | \
96 RDW_FRAME | \
97 RDW_NOFRAME)
99 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
100 TRACE("\n");
101 #undef RDW_FLAGS
105 /***********************************************************************
106 * update_visible_region
108 * Set the visible region and X11 drawable for the DC associated to
109 * a given window.
111 static void update_visible_region( struct dce *dce )
113 struct window_surface *surface = NULL;
114 NTSTATUS status;
115 HRGN vis_rgn = 0;
116 HWND top_win = 0;
117 DWORD flags = dce->flags;
118 DWORD paint_flags = 0;
119 size_t size = 256;
120 RECT win_rect, top_rect;
121 WND *win;
123 /* don't clip siblings if using parent clip region */
124 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
126 /* fetch the visible region from the server */
129 RGNDATA *data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 );
130 if (!data) return;
132 SERVER_START_REQ( get_visible_region )
134 req->window = wine_server_user_handle( dce->hwnd );
135 req->flags = flags;
136 wine_server_set_reply( req, data->Buffer, size );
137 if (!(status = wine_server_call( req )))
139 size_t reply_size = wine_server_reply_size( reply );
140 data->rdh.dwSize = sizeof(data->rdh);
141 data->rdh.iType = RDH_RECTANGLES;
142 data->rdh.nCount = reply_size / sizeof(RECT);
143 data->rdh.nRgnSize = reply_size;
144 vis_rgn = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
146 top_win = wine_server_ptr_handle( reply->top_win );
147 win_rect.left = reply->win_rect.left;
148 win_rect.top = reply->win_rect.top;
149 win_rect.right = reply->win_rect.right;
150 win_rect.bottom = reply->win_rect.bottom;
151 top_rect.left = reply->top_rect.left;
152 top_rect.top = reply->top_rect.top;
153 top_rect.right = reply->top_rect.right;
154 top_rect.bottom = reply->top_rect.bottom;
155 paint_flags = reply->paint_flags;
157 else size = reply->total_size;
159 SERVER_END_REQ;
160 HeapFree( GetProcessHeap(), 0, data );
161 } while (status == STATUS_BUFFER_OVERFLOW);
163 if (status || !vis_rgn) return;
165 USER_Driver->pGetDC( dce->hdc, dce->hwnd, top_win, &win_rect, &top_rect, flags );
167 if (dce->clip_rgn) CombineRgn( vis_rgn, vis_rgn, dce->clip_rgn,
168 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
170 /* don't use a surface to paint the client area of OpenGL windows */
171 if (!(paint_flags & SET_WINPOS_PIXEL_FORMAT) || (flags & DCX_WINDOW))
173 win = WIN_GetPtr( top_win );
174 if (win && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
176 surface = win->surface;
177 if (surface) window_surface_add_ref( surface );
178 WIN_ReleasePtr( win );
182 if (!surface) top_rect = get_virtual_screen_rect();
183 __wine_set_visible_region( dce->hdc, vis_rgn, &win_rect, &top_rect, surface );
184 if (surface) window_surface_release( surface );
188 /***********************************************************************
189 * release_dce
191 static void release_dce( struct dce *dce )
193 if (!dce->hwnd) return; /* already released */
195 __wine_set_visible_region( dce->hdc, 0, &dummy_surface.rect, &dummy_surface.rect, &dummy_surface );
196 USER_Driver->pReleaseDC( dce->hwnd, dce->hdc );
198 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
199 dce->clip_rgn = 0;
200 dce->hwnd = 0;
201 dce->flags &= DCX_CACHE;
205 /***********************************************************************
206 * delete_clip_rgn
208 static void delete_clip_rgn( struct dce *dce )
210 if (!dce->clip_rgn) return; /* nothing to do */
212 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
213 DeleteObject( dce->clip_rgn );
214 dce->clip_rgn = 0;
216 /* make it dirty so that the vis rgn gets recomputed next time */
217 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
221 /***********************************************************************
222 * alloc_dce
224 * Allocate a new DCE.
226 static struct dce *alloc_dce(void)
228 struct dce *dce;
230 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(*dce) ))) return NULL;
231 if (!(dce->hdc = CreateDCW( displayW, NULL, NULL, NULL )))
233 HeapFree( GetProcessHeap(), 0, dce );
234 return 0;
236 dce->hwnd = 0;
237 dce->clip_rgn = 0;
238 dce->flags = 0;
239 dce->count = 1;
241 /* store DCE handle in DC hook data field */
242 SetDCHook( dce->hdc, dc_hook, (DWORD_PTR)dce );
243 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
244 return dce;
248 /***********************************************************************
249 * get_window_dce
251 static struct dce *get_window_dce( HWND hwnd )
253 struct dce *dce;
254 WND *win = WIN_GetPtr( hwnd );
256 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return NULL;
258 dce = win->dce;
259 if (!dce && (dce = get_class_dce( win->class )))
261 win->dce = dce;
262 dce->count++;
264 WIN_ReleasePtr( win );
266 if (!dce) /* try to allocate one */
268 struct dce *dce_to_free = NULL;
269 LONG class_style = GetClassLongW( hwnd, GCL_STYLE );
271 if (class_style & CS_CLASSDC)
273 if (!(dce = alloc_dce())) return NULL;
275 win = WIN_GetPtr( hwnd );
276 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
278 if (win->dce) /* another thread beat us to it */
280 dce_to_free = dce;
281 dce = win->dce;
283 else if ((win->dce = set_class_dce( win->class, dce )) != dce)
285 dce_to_free = dce;
286 dce = win->dce;
287 dce->count++;
289 else
291 dce->count++;
292 list_add_tail( &dce_list, &dce->entry );
294 WIN_ReleasePtr( win );
296 else dce_to_free = dce;
298 else if (class_style & CS_OWNDC)
300 if (!(dce = alloc_dce())) return NULL;
302 win = WIN_GetPtr( hwnd );
303 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
305 if (win->dwStyle & WS_CLIPCHILDREN) dce->flags |= DCX_CLIPCHILDREN;
306 if (win->dwStyle & WS_CLIPSIBLINGS) dce->flags |= DCX_CLIPSIBLINGS;
307 if (win->dce) /* another thread beat us to it */
309 dce_to_free = dce;
310 dce = win->dce;
312 else
314 win->dce = dce;
315 dce->hwnd = hwnd;
316 list_add_tail( &dce_list, &dce->entry );
318 WIN_ReleasePtr( win );
320 else dce_to_free = dce;
323 if (dce_to_free)
325 SetDCHook( dce_to_free->hdc, NULL, 0 );
326 DeleteDC( dce_to_free->hdc );
327 HeapFree( GetProcessHeap(), 0, dce_to_free );
328 if (dce_to_free == dce)
329 dce = NULL;
332 return dce;
336 /***********************************************************************
337 * free_dce
339 * Free a class or window DCE.
341 void free_dce( struct dce *dce, HWND hwnd )
343 struct dce *dce_to_free = NULL;
345 USER_Lock();
347 if (dce)
349 if (!--dce->count)
351 release_dce( dce );
352 list_remove( &dce->entry );
353 dce_to_free = dce;
355 else if (dce->hwnd == hwnd)
357 release_dce( dce );
361 /* now check for cache DCEs */
363 if (hwnd)
365 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
367 if (dce->hwnd != hwnd) continue;
368 if (!(dce->flags & DCX_CACHE)) break;
370 release_dce( dce );
371 if (dce->count)
373 WARN( "GetDC() without ReleaseDC() for window %p\n", hwnd );
374 dce->count = 0;
375 SetHookFlags( dce->hdc, DCHF_DISABLEDC );
380 USER_Unlock();
382 if (dce_to_free)
384 SetDCHook( dce_to_free->hdc, NULL, 0 );
385 DeleteDC( dce_to_free->hdc );
386 HeapFree( GetProcessHeap(), 0, dce_to_free );
391 /***********************************************************************
392 * make_dc_dirty
394 * Mark the associated DC as dirty to force a refresh of the visible region
396 static void make_dc_dirty( struct dce *dce )
398 if (!dce->count)
400 /* Don't bother with visible regions of unused DCEs */
401 TRACE("purged %p hwnd %p\n", dce->hdc, dce->hwnd);
402 release_dce( dce );
404 else
406 /* Set dirty bits in the hDC and DCE structs */
407 TRACE("fixed up %p hwnd %p\n", dce->hdc, dce->hwnd);
408 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
413 /***********************************************************************
414 * invalidate_dce
416 * It is called from SetWindowPos() - we have to
417 * mark as dirty all busy DCEs for windows that have pWnd->parent as
418 * an ancestor and whose client rect intersects with specified update
419 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
420 * DCX_CLIPCHILDREN flag is set.
422 void invalidate_dce( WND *win, const RECT *extra_rect )
424 RECT window_rect;
425 struct dce *dce;
427 if (!win->parent) return;
429 GetWindowRect( win->obj.handle, &window_rect );
431 TRACE("%p parent %p %s (%s)\n",
432 win->obj.handle, win->parent, wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(extra_rect) );
434 /* walk all DCEs and fixup non-empty entries */
436 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
438 if (!dce->hwnd) continue;
440 TRACE( "%p: hwnd %p dcx %08x %s %s\n", dce->hdc, dce->hwnd, dce->flags,
441 (dce->flags & DCX_CACHE) ? "Cache" : "Owned", dce->count ? "InUse" : "" );
443 if ((dce->hwnd == win->parent) && !(dce->flags & DCX_CLIPCHILDREN))
444 continue; /* child window positions don't bother us */
446 /* if DCE window is a child of hwnd, it has to be invalidated */
447 if (dce->hwnd == win->obj.handle || IsChild( win->obj.handle, dce->hwnd ))
449 make_dc_dirty( dce );
450 continue;
453 /* otherwise check if the window rectangle intersects this DCE window */
454 if (win->parent == dce->hwnd || IsChild( win->parent, dce->hwnd ))
456 RECT dce_rect, tmp;
457 GetWindowRect( dce->hwnd, &dce_rect );
458 if (IntersectRect( &tmp, &dce_rect, &window_rect ) ||
459 (extra_rect && IntersectRect( &tmp, &dce_rect, extra_rect )))
460 make_dc_dirty( dce );
465 /***********************************************************************
466 * release_dc
468 * Implementation of ReleaseDC.
470 static INT release_dc( HWND hwnd, HDC hdc, BOOL end_paint )
472 struct dce *dce;
473 BOOL ret = FALSE;
475 TRACE("%p %p\n", hwnd, hdc );
477 USER_Lock();
478 dce = (struct dce *)GetDCHook( hdc, NULL );
479 if (dce && dce->count && dce->hwnd)
481 if (!(dce->flags & DCX_NORESETATTRS)) SetHookFlags( dce->hdc, DCHF_RESETDC );
482 if (end_paint || (dce->flags & DCX_CACHE)) delete_clip_rgn( dce );
483 if (dce->flags & DCX_CACHE)
485 dce->count = 0;
486 SetHookFlags( dce->hdc, DCHF_DISABLEDC );
488 ret = TRUE;
490 USER_Unlock();
491 return ret;
495 /***********************************************************************
496 * dc_hook
498 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
500 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam )
502 BOOL retv = TRUE;
503 struct dce *dce = (struct dce *)data;
505 TRACE("hDC = %p, %u\n", hDC, code);
507 if (!dce) return FALSE;
508 assert( dce->hdc == hDC );
510 switch( code )
512 case DCHC_INVALIDVISRGN:
513 /* GDI code calls this when it detects that the
514 * DC is dirty (usually after SetHookFlags()). This
515 * means that we have to recompute the visible region.
517 if (dce->count) update_visible_region( dce );
518 else /* non-fatal but shouldn't happen */
519 WARN("DC is not in use!\n");
520 break;
521 case DCHC_DELETEDC:
522 USER_Lock();
523 if (!(dce->flags & DCX_CACHE))
525 WARN("Application trying to delete an owned DC %p\n", dce->hdc);
526 retv = FALSE;
528 else
530 list_remove( &dce->entry );
531 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
532 HeapFree( GetProcessHeap(), 0, dce );
534 USER_Unlock();
535 break;
537 return retv;
541 /***********************************************************************
542 * get_update_region
544 * Return update region (in screen coordinates) for a window.
546 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
548 HRGN hrgn = 0;
549 NTSTATUS status;
550 RGNDATA *data;
551 size_t size = 256;
555 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
557 SetLastError( ERROR_OUTOFMEMORY );
558 return 0;
561 SERVER_START_REQ( get_update_region )
563 req->window = wine_server_user_handle( hwnd );
564 req->from_child = wine_server_user_handle( child ? *child : 0 );
565 req->flags = *flags;
566 wine_server_set_reply( req, data->Buffer, size );
567 if (!(status = wine_server_call( req )))
569 size_t reply_size = wine_server_reply_size( reply );
570 data->rdh.dwSize = sizeof(data->rdh);
571 data->rdh.iType = RDH_RECTANGLES;
572 data->rdh.nCount = reply_size / sizeof(RECT);
573 data->rdh.nRgnSize = reply_size;
574 hrgn = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
575 if (child) *child = wine_server_ptr_handle( reply->child );
576 *flags = reply->flags;
578 else size = reply->total_size;
580 SERVER_END_REQ;
581 HeapFree( GetProcessHeap(), 0, data );
582 } while (status == STATUS_BUFFER_OVERFLOW);
584 if (status) SetLastError( RtlNtStatusToDosError(status) );
585 return hrgn;
589 /***********************************************************************
590 * get_update_flags
592 * Get only the update flags, not the update region.
594 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
596 BOOL ret;
598 SERVER_START_REQ( get_update_region )
600 req->window = wine_server_user_handle( hwnd );
601 req->from_child = wine_server_user_handle( child ? *child : 0 );
602 req->flags = *flags | UPDATE_NOREGION;
603 if ((ret = !wine_server_call_err( req )))
605 if (child) *child = wine_server_ptr_handle( reply->child );
606 *flags = reply->flags;
609 SERVER_END_REQ;
610 return ret;
614 /***********************************************************************
615 * redraw_window_rects
617 * Redraw part of a window.
619 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
621 BOOL ret;
623 if (!(flags & (RDW_INVALIDATE|RDW_VALIDATE|RDW_INTERNALPAINT|RDW_NOINTERNALPAINT)))
624 return TRUE; /* nothing to do */
626 SERVER_START_REQ( redraw_window )
628 req->window = wine_server_user_handle( hwnd );
629 req->flags = flags;
630 wine_server_add_data( req, rects, count * sizeof(RECT) );
631 ret = !wine_server_call_err( req );
633 SERVER_END_REQ;
634 return ret;
638 /***********************************************************************
639 * send_ncpaint
641 * Send a WM_NCPAINT message if needed, and return the resulting update region (in screen coords).
642 * Helper for erase_now and BeginPaint.
644 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
646 HRGN whole_rgn = get_update_region( hwnd, flags, child );
647 HRGN client_rgn = 0;
649 if (child) hwnd = *child;
651 if (hwnd == GetDesktopWindow()) return whole_rgn;
653 if (whole_rgn)
655 RECT client, update;
656 INT type;
658 /* check if update rgn overlaps with nonclient area */
659 type = GetRgnBox( whole_rgn, &update );
660 WIN_GetRectangles( hwnd, COORDS_SCREEN, 0, &client );
662 if ((*flags & UPDATE_NONCLIENT) ||
663 update.left < client.left || update.top < client.top ||
664 update.right > client.right || update.bottom > client.bottom)
666 client_rgn = CreateRectRgnIndirect( &client );
667 CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
669 /* check if update rgn contains complete nonclient area */
670 if (type == SIMPLEREGION)
672 RECT window;
673 GetWindowRect( hwnd, &window );
674 if (EqualRect( &window, &update ))
676 DeleteObject( whole_rgn );
677 whole_rgn = (HRGN)1;
681 else
683 client_rgn = whole_rgn;
684 whole_rgn = 0;
687 if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
689 if (*flags & UPDATE_NONCLIENT) SendMessageW( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
690 if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
693 return client_rgn;
697 /***********************************************************************
698 * send_erase
700 * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
701 * If a DC is requested, the region is selected into it. In all cases the region is deleted.
702 * Helper for erase_now and BeginPaint.
704 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
705 RECT *clip_rect, HDC *hdc_ret )
707 BOOL need_erase = (flags & UPDATE_DELAYED_ERASE) != 0;
708 HDC hdc = 0;
709 RECT dummy;
711 if (!clip_rect) clip_rect = &dummy;
712 if (hdc_ret || (flags & UPDATE_ERASE))
714 UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
715 if (IsIconic(hwnd)) dcx_flags |= DCX_WINDOW;
717 if ((hdc = GetDCEx( hwnd, client_rgn, dcx_flags )))
719 INT type = GetClipBox( hdc, clip_rect );
721 if (flags & UPDATE_ERASE)
723 /* don't erase if the clip box is empty */
724 if (type != NULLREGION)
725 need_erase = !SendMessageW( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
727 if (!hdc_ret) release_dc( hwnd, hdc, TRUE );
730 if (hdc_ret) *hdc_ret = hdc;
732 if (!hdc) DeleteObject( client_rgn );
733 return need_erase;
737 /***********************************************************************
738 * erase_now
740 * Implementation of RDW_ERASENOW behavior.
742 void erase_now( HWND hwnd, UINT rdw_flags )
744 HWND child = 0;
745 HRGN hrgn;
746 BOOL need_erase = FALSE;
748 /* loop while we find a child to repaint */
749 for (;;)
751 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
753 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
754 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
755 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
757 if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
758 need_erase = send_erase( child, flags, hrgn, NULL, NULL );
760 if (!flags) break; /* nothing more to do */
761 if ((rdw_flags & RDW_NOCHILDREN) && !need_erase) break;
766 /***********************************************************************
767 * copy_bits_from_surface
769 * Copy bits from a window surface; helper for move_window_bits and move_window_bits_parent.
771 static void copy_bits_from_surface( HWND hwnd, struct window_surface *surface,
772 const RECT *dst, const RECT *src )
774 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
775 BITMAPINFO *info = (BITMAPINFO *)buffer;
776 void *bits;
777 UINT flags = UPDATE_NOCHILDREN | UPDATE_CLIPCHILDREN;
778 HRGN rgn = get_update_region( hwnd, &flags, NULL );
779 HDC hdc = GetDCEx( hwnd, rgn, DCX_CACHE | DCX_WINDOW | DCX_EXCLUDERGN );
781 bits = surface->funcs->get_info( surface, info );
782 surface->funcs->lock( surface );
783 SetDIBitsToDevice( hdc, dst->left, dst->top, dst->right - dst->left, dst->bottom - dst->top,
784 src->left - surface->rect.left, surface->rect.bottom - src->bottom,
785 0, surface->rect.bottom - surface->rect.top,
786 bits, info, DIB_RGB_COLORS );
787 surface->funcs->unlock( surface );
788 ReleaseDC( hwnd, hdc );
792 /***********************************************************************
793 * move_window_bits
795 * Move the window bits when a window is resized or its surface recreated.
797 void move_window_bits( HWND hwnd, struct window_surface *old_surface,
798 struct window_surface *new_surface,
799 const RECT *visible_rect, const RECT *old_visible_rect,
800 const RECT *window_rect, const RECT *valid_rects )
802 RECT dst = valid_rects[0];
803 RECT src = valid_rects[1];
805 if (new_surface != old_surface ||
806 src.left - old_visible_rect->left != dst.left - visible_rect->left ||
807 src.top - old_visible_rect->top != dst.top - visible_rect->top)
809 TRACE( "copying %s -> %s\n", wine_dbgstr_rect( &src ), wine_dbgstr_rect( &dst ));
810 OffsetRect( &src, -old_visible_rect->left, -old_visible_rect->top );
811 OffsetRect( &dst, -window_rect->left, -window_rect->top );
812 copy_bits_from_surface( hwnd, old_surface, &dst, &src );
817 /***********************************************************************
818 * move_window_bits_parent
820 * Move the window bits in the parent surface when a child is moved.
822 void move_window_bits_parent( HWND hwnd, HWND parent, const RECT *window_rect, const RECT *valid_rects )
824 struct window_surface *surface;
825 RECT dst = valid_rects[0];
826 RECT src = valid_rects[1];
827 WND *win;
829 if (src.left == dst.left && src.top == dst.top) return;
831 if (!(win = WIN_GetPtr( parent ))) return;
832 if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return;
833 if (!(surface = win->surface))
835 WIN_ReleasePtr( win );
836 return;
839 TRACE( "copying %s -> %s\n", wine_dbgstr_rect( &src ), wine_dbgstr_rect( &dst ));
840 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), parent, (POINT *)&src, 2 );
841 OffsetRect( &src, win->rectClient.left - win->visible_rect.left,
842 win->rectClient.top - win->visible_rect.top );
843 OffsetRect( &dst, -window_rect->left, -window_rect->top );
844 window_surface_add_ref( surface );
845 WIN_ReleasePtr( win );
847 copy_bits_from_surface( hwnd, surface, &dst, &src );
848 window_surface_release( surface );
852 /***********************************************************************
853 * update_now
855 * Implementation of RDW_UPDATENOW behavior.
857 static void update_now( HWND hwnd, UINT rdw_flags )
859 HWND child = 0;
861 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
862 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
864 /* loop while we find a child to repaint */
865 for (;;)
867 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
869 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
870 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
872 if (!get_update_flags( hwnd, &child, &flags )) break;
873 if (!flags) break; /* nothing more to do */
875 SendMessageW( child, WM_PAINT, 0, 0 );
876 if (rdw_flags & RDW_NOCHILDREN) break;
881 /*************************************************************************
882 * fix_caret
884 * Helper for ScrollWindowEx:
885 * If the return value is 0, no special caret handling is necessary.
886 * Otherwise the return value is the handle of the window that owns the
887 * caret. Its caret needs to be hidden during the scroll operation and
888 * moved to new_caret_pos if move_caret is TRUE.
890 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
891 UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
893 GUITHREADINFO info;
894 RECT rect, mapped_rcCaret;
896 info.cbSize = sizeof(info);
897 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
898 if (!info.hwndCaret) return 0;
900 mapped_rcCaret = info.rcCaret;
901 if (info.hwndCaret == hWnd)
903 /* The caret needs to be moved along with scrolling even if it's
904 * outside the visible area. Otherwise, when the caret is scrolled
905 * out from the view, the position won't get updated anymore and
906 * the caret will never scroll back again. */
907 *move_caret = TRUE;
908 new_caret_pos->x = info.rcCaret.left + dx;
909 new_caret_pos->y = info.rcCaret.top + dy;
911 else
913 *move_caret = FALSE;
914 if (!(flags & SW_SCROLLCHILDREN) || !IsChild(hWnd, info.hwndCaret))
915 return 0;
916 MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
919 /* If the caret is not in the src/dest rects, all is fine done. */
920 if (!IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
922 rect = *scroll_rect;
923 OffsetRect(&rect, dx, dy);
924 if (!IntersectRect(&rect, &rect, &mapped_rcCaret))
925 return 0;
928 /* Indicate that the caret needs to be updated during the scrolling. */
929 return info.hwndCaret;
933 /***********************************************************************
934 * BeginPaint (USER32.@)
936 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
938 HRGN hrgn;
939 HDC hdc;
940 BOOL erase;
941 RECT rect;
942 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
944 HideCaret( hwnd );
946 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
948 erase = send_erase( hwnd, flags, hrgn, &rect, &hdc );
950 TRACE("hdc = %p box = (%s), fErase = %d\n", hdc, wine_dbgstr_rect(&rect), erase);
952 if (!lps)
954 release_dc( hwnd, hdc, TRUE );
955 return 0;
957 lps->fErase = erase;
958 lps->rcPaint = rect;
959 lps->hdc = hdc;
960 return hdc;
964 /***********************************************************************
965 * EndPaint (USER32.@)
967 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
969 ShowCaret( hwnd );
970 flush_window_surfaces( FALSE );
971 if (!lps) return FALSE;
972 release_dc( hwnd, lps->hdc, TRUE );
973 return TRUE;
977 /***********************************************************************
978 * GetDCEx (USER32.@)
980 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
982 const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
983 const DWORD user_flags = clip_flags | DCX_NORESETATTRS; /* flags that can be set by user */
984 struct dce *dce;
985 BOOL bUpdateVisRgn = TRUE;
986 HWND parent;
987 LONG window_style = GetWindowLongW( hwnd, GWL_STYLE );
989 if (!hwnd) hwnd = GetDesktopWindow();
990 else hwnd = WIN_GetFullHandle( hwnd );
992 TRACE("hwnd %p, hrgnClip %p, flags %08x\n", hwnd, hrgnClip, flags);
994 if (!IsWindow(hwnd)) return 0;
996 /* fixup flags */
998 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
1000 if (flags & DCX_USESTYLE)
1002 flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
1004 if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
1006 if (!(flags & DCX_WINDOW))
1008 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
1010 if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
1011 flags |= DCX_CLIPCHILDREN;
1015 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
1017 parent = GetAncestor( hwnd, GA_PARENT );
1018 if (!parent || (parent == GetDesktopWindow()))
1019 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
1021 /* it seems parent clip is ignored when clipping siblings or children */
1022 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
1024 if( flags & DCX_PARENTCLIP )
1026 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
1027 if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
1029 flags &= ~DCX_CLIPCHILDREN;
1030 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
1034 /* find a suitable DCE */
1036 if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
1038 struct dce *dceEmpty = NULL, *dceUnused = NULL, *found = NULL;
1039 unsigned int count = 0;
1041 /* Strategy: First, we attempt to find a non-empty but unused DCE with
1042 * compatible flags. Next, we look for an empty entry. If the cache is
1043 * full we have to purge one of the unused entries.
1045 USER_Lock();
1046 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
1048 if (!(dce->flags & DCX_CACHE)) break;
1049 count++;
1050 if (dce->count) continue;
1051 dceUnused = dce;
1052 if (!dce->hwnd) dceEmpty = dce;
1053 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
1055 TRACE( "found valid %p hwnd %p, flags %08x\n", dce->hdc, hwnd, dce->flags );
1056 found = dce;
1057 bUpdateVisRgn = FALSE;
1058 break;
1061 if (!found) found = dceEmpty;
1062 if (!found && count >= DCE_CACHE_SIZE) found = dceUnused;
1064 dce = found;
1065 if (dce)
1067 dce->count = 1;
1068 SetHookFlags( dce->hdc, DCHF_ENABLEDC );
1070 USER_Unlock();
1072 /* if there's no dce empty or unused, allocate a new one */
1073 if (!dce)
1075 if (!(dce = alloc_dce())) return 0;
1076 dce->flags = DCX_CACHE;
1077 USER_Lock();
1078 list_add_head( &dce_list, &dce->entry );
1079 USER_Unlock();
1082 else
1084 flags |= DCX_NORESETATTRS;
1085 if (dce->hwnd != hwnd)
1087 /* we should free dce->clip_rgn here, but Windows apparently doesn't */
1088 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
1089 dce->clip_rgn = 0;
1091 else bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
1094 if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
1096 /* if the extra clip region has changed, get rid of the old one */
1097 if (dce->clip_rgn != hrgnClip || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
1098 delete_clip_rgn( dce );
1099 dce->clip_rgn = hrgnClip;
1100 if (!dce->clip_rgn) dce->clip_rgn = CreateRectRgn( 0, 0, 0, 0 );
1101 dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
1102 bUpdateVisRgn = TRUE;
1105 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) SetLayout( dce->hdc, LAYOUT_RTL );
1107 dce->hwnd = hwnd;
1108 dce->flags = (dce->flags & ~user_flags) | (flags & user_flags);
1110 /* cross-process invalidation is not supported yet, so always update the vis rgn */
1111 if (!WIN_IsCurrentProcess( hwnd )) bUpdateVisRgn = TRUE;
1113 if (SetHookFlags( dce->hdc, DCHF_VALIDATEVISRGN )) bUpdateVisRgn = TRUE; /* DC was dirty */
1115 if (bUpdateVisRgn) update_visible_region( dce );
1117 TRACE("(%p,%p,0x%x): returning %p%s\n", hwnd, hrgnClip, flags, dce->hdc,
1118 bUpdateVisRgn ? " (updated)" : "");
1119 return dce->hdc;
1123 /***********************************************************************
1124 * GetDC (USER32.@)
1126 * Get a device context.
1128 * RETURNS
1129 * Success: Handle to the device context
1130 * Failure: NULL.
1132 HDC WINAPI GetDC( HWND hwnd )
1134 if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
1135 return GetDCEx( hwnd, 0, DCX_USESTYLE );
1139 /***********************************************************************
1140 * GetWindowDC (USER32.@)
1142 HDC WINAPI GetWindowDC( HWND hwnd )
1144 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
1148 /***********************************************************************
1149 * ReleaseDC (USER32.@)
1151 * Release a device context.
1153 * RETURNS
1154 * Success: Non-zero. Resources used by hdc are released.
1155 * Failure: 0.
1157 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
1159 return release_dc( hwnd, hdc, FALSE );
1163 /**********************************************************************
1164 * WindowFromDC (USER32.@)
1166 HWND WINAPI WindowFromDC( HDC hdc )
1168 struct dce *dce;
1169 HWND hwnd = 0;
1171 USER_Lock();
1172 dce = (struct dce *)GetDCHook( hdc, NULL );
1173 if (dce) hwnd = dce->hwnd;
1174 USER_Unlock();
1175 return hwnd;
1179 /***********************************************************************
1180 * LockWindowUpdate (USER32.@)
1182 * Enables or disables painting in the chosen window.
1184 * PARAMS
1185 * hwnd [I] handle to a window.
1187 * RETURNS
1188 * If successful, returns nonzero value. Otherwise,
1189 * returns 0.
1191 * NOTES
1192 * You can lock only one window at a time.
1194 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1196 static HWND lockedWnd;
1198 FIXME("(%p), partial stub!\n",hwnd);
1200 USER_Lock();
1201 if (lockedWnd)
1203 if (!hwnd)
1205 /* Unlock lockedWnd */
1206 /* FIXME: Do something */
1208 else
1210 /* Attempted to lock a second window */
1211 /* Return FALSE and do nothing */
1212 USER_Unlock();
1213 return FALSE;
1216 lockedWnd = hwnd;
1217 USER_Unlock();
1218 return TRUE;
1222 /***********************************************************************
1223 * RedrawWindow (USER32.@)
1225 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1227 static const RECT empty;
1228 BOOL ret;
1230 if (!hwnd) hwnd = GetDesktopWindow();
1232 if (TRACE_ON(win))
1234 if (hrgn)
1236 RECT r;
1237 GetRgnBox( hrgn, &r );
1238 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1240 else if (rect)
1241 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1242 else
1243 TRACE( "%p whole window ", hwnd );
1245 dump_rdw_flags(flags);
1248 /* process pending expose events before painting */
1249 if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
1251 if (rect && !hrgn)
1253 if (IsRectEmpty( rect )) rect = &empty;
1254 ret = redraw_window_rects( hwnd, flags, rect, 1 );
1256 else if (!hrgn)
1258 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1260 else /* need to build a list of the region rectangles */
1262 DWORD size;
1263 RGNDATA *data;
1265 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
1266 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1267 GetRegionData( hrgn, size, data );
1268 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
1269 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1270 else
1271 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1272 HeapFree( GetProcessHeap(), 0, data );
1275 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1276 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1278 return ret;
1282 /***********************************************************************
1283 * UpdateWindow (USER32.@)
1285 BOOL WINAPI UpdateWindow( HWND hwnd )
1287 if (!hwnd)
1289 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1290 return FALSE;
1293 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1297 /***********************************************************************
1298 * InvalidateRgn (USER32.@)
1300 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1302 if (!hwnd)
1304 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1305 return FALSE;
1308 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1312 /***********************************************************************
1313 * InvalidateRect (USER32.@)
1315 * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
1316 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1318 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1320 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1322 if (!hwnd)
1324 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1325 rect = NULL;
1328 return RedrawWindow( hwnd, rect, 0, flags );
1332 /***********************************************************************
1333 * ValidateRgn (USER32.@)
1335 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1337 if (!hwnd)
1339 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1340 return FALSE;
1343 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
1347 /***********************************************************************
1348 * ValidateRect (USER32.@)
1350 * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
1351 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1353 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1355 UINT flags = RDW_VALIDATE;
1357 if (!hwnd)
1359 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1360 rect = NULL;
1363 return RedrawWindow( hwnd, rect, 0, flags );
1367 /***********************************************************************
1368 * GetUpdateRgn (USER32.@)
1370 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1372 INT retval = ERROR;
1373 UINT flags = UPDATE_NOCHILDREN;
1374 HRGN update_rgn;
1376 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1378 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1380 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1381 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1383 flags = UPDATE_DELAYED_ERASE;
1384 get_update_flags( hwnd, NULL, &flags );
1386 /* map region to client coordinates */
1387 map_window_region( 0, hwnd, hrgn );
1389 return retval;
1393 /***********************************************************************
1394 * GetUpdateRect (USER32.@)
1396 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1398 UINT flags = UPDATE_NOCHILDREN;
1399 HRGN update_rgn;
1400 BOOL need_erase;
1402 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1404 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
1406 if (rect)
1408 if (GetRgnBox( update_rgn, rect ) != NULLREGION)
1410 HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
1411 DWORD layout = SetLayout( hdc, 0 ); /* MapWindowPoints mirrors already */
1412 MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
1413 DPtoLP( hdc, (LPPOINT)rect, 2 );
1414 SetLayout( hdc, layout );
1415 ReleaseDC( hwnd, hdc );
1418 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1420 /* check if we still have an update region */
1421 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1422 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1423 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
1427 /***********************************************************************
1428 * ExcludeUpdateRgn (USER32.@)
1430 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1432 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
1433 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
1435 if (ret != ERROR)
1437 POINT pt;
1439 GetDCOrgEx( hdc, &pt );
1440 MapWindowPoints( 0, hwnd, &pt, 1 );
1441 OffsetRgn( update_rgn, -pt.x, -pt.y );
1442 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1444 DeleteObject( update_rgn );
1445 return ret;
1449 static INT scroll_window( HWND hwnd, INT dx, INT dy, const RECT *rect, const RECT *clipRect,
1450 HRGN hrgnUpdate, LPRECT rcUpdate, UINT flags, BOOL is_ex )
1452 INT retVal = NULLREGION;
1453 BOOL bOwnRgn = TRUE;
1454 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
1455 int rdw_flags;
1456 HRGN hrgnTemp;
1457 HRGN hrgnWinupd = 0;
1458 HDC hDC;
1459 RECT rc, cliprc;
1460 HWND hwndCaret = NULL;
1461 BOOL moveCaret = FALSE;
1462 POINT newCaretPos;
1464 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
1465 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
1466 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
1467 if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
1468 FIXME("some flags (%04x) are unhandled\n", flags);
1470 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1471 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
1473 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
1474 hwnd = WIN_GetFullHandle( hwnd );
1476 GetClientRect(hwnd, &rc);
1478 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
1479 else cliprc = rc;
1481 if (rect) IntersectRect(&rc, &rc, rect);
1483 if( hrgnUpdate ) bOwnRgn = FALSE;
1484 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
1486 newCaretPos.x = newCaretPos.y = 0;
1488 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
1489 DWORD dcxflags = 0;
1490 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1492 hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
1493 if (hwndCaret)
1494 HideCaret(hwndCaret);
1496 if (is_ex) dcxflags |= DCX_CACHE;
1497 if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1498 if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
1499 dcxflags |= DCX_PARENTCLIP;
1500 if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1501 dcxflags |= DCX_CLIPCHILDREN;
1502 hDC = GetDCEx( hwnd, 0, dcxflags);
1503 if (hDC)
1505 ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
1507 ReleaseDC( hwnd, hDC );
1509 if (!bUpdate)
1510 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
1513 /* If the windows has an update region, this must be
1514 * scrolled as well. Keep a copy in hrgnWinupd
1515 * to be added to hrngUpdate at the end. */
1516 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
1517 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
1518 if (retVal != NULLREGION)
1520 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
1521 if( !bOwnRgn) {
1522 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
1523 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
1525 OffsetRgn( hrgnTemp, dx, dy );
1526 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1527 if( !bOwnRgn)
1528 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1529 RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
1531 /* Catch the case where the scrolling amount exceeds the size of the
1532 * original window. This generated a second update area that is the
1533 * location where the original scrolled content would end up.
1534 * This second region is not returned by the ScrollDC and sets
1535 * ScrollWindowEx apart from just a ScrollDC.
1537 * This has been verified with testing on windows.
1539 if (abs(dx) > abs(rc.right - rc.left) ||
1540 abs(dy) > abs(rc.bottom - rc.top))
1542 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
1543 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1544 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
1546 if (rcUpdate)
1548 RECT rcTemp;
1549 GetRgnBox( hrgnTemp, &rcTemp );
1550 UnionRect( rcUpdate, rcUpdate, &rcTemp );
1553 if( !bOwnRgn)
1554 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1556 DeleteObject( hrgnClip );
1558 DeleteObject( hrgnTemp );
1559 } else {
1560 /* nothing was scrolled */
1561 if( !bOwnRgn)
1562 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
1563 SetRectEmpty( rcUpdate);
1566 if( flags & SW_SCROLLCHILDREN )
1568 HWND *list = WIN_ListChildren( hwnd );
1569 if (list)
1571 int i;
1572 RECT r, dummy;
1573 for (i = 0; list[i]; i++)
1575 WIN_GetRectangles( list[i], COORDS_PARENT, &r, NULL );
1576 if (!rect || IntersectRect(&dummy, &r, rect))
1577 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
1578 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1579 SWP_NOREDRAW | SWP_DEFERERASE );
1581 HeapFree( GetProcessHeap(), 0, list );
1585 if( flags & (SW_INVALIDATE | SW_ERASE) )
1586 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
1587 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1589 if( hrgnWinupd) {
1590 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
1591 DeleteObject( hrgnWinupd);
1594 if( moveCaret )
1595 SetCaretPos( newCaretPos.x, newCaretPos.y );
1596 if( hwndCaret )
1597 ShowCaret( hwndCaret );
1599 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
1601 return retVal;
1605 /*************************************************************************
1606 * ScrollWindowEx (USER32.@)
1608 * Note: contrary to what the doc says, pixels that are scrolled from the
1609 * outside of clipRect to the inside are NOT painted.
1612 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
1613 const RECT *rect, const RECT *clipRect,
1614 HRGN hrgnUpdate, LPRECT rcUpdate,
1615 UINT flags )
1617 return scroll_window( hwnd, dx, dy, rect, clipRect, hrgnUpdate, rcUpdate, flags, TRUE );
1620 /*************************************************************************
1621 * ScrollWindow (USER32.@)
1624 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
1625 const RECT *rect, const RECT *clipRect )
1627 return scroll_window( hwnd, dx, dy, rect, clipRect, 0, NULL,
1628 SW_INVALIDATE | SW_ERASE | (rect ? 0 : SW_SCROLLCHILDREN), FALSE ) != ERROR;
1632 /*************************************************************************
1633 * ScrollDC (USER32.@)
1635 * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is
1636 * wrong) hrgnUpdate is returned in device coordinates with rcUpdate in
1637 * logical coordinates.
1639 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *scroll, const RECT *clip,
1640 HRGN ret_update_rgn, LPRECT update_rect )
1643 HRGN update_rgn = ret_update_rgn;
1644 RECT src_rect, clip_rect, offset;
1645 INT dxdev, dydev;
1646 HRGN dstrgn, cliprgn, visrgn;
1647 BOOL ret;
1649 TRACE( "dx,dy %d,%d scroll %s clip %s update %p rect %p\n",
1650 dx, dy, wine_dbgstr_rect(scroll), wine_dbgstr_rect(clip), ret_update_rgn, update_rect );
1652 /* get the visible region */
1653 visrgn = CreateRectRgn( 0, 0, 0, 0 );
1654 GetRandomRgn( hdc, visrgn, SYSRGN );
1655 if (!(GetVersion() & 0x80000000))
1657 POINT org;
1658 GetDCOrgEx( hdc, &org );
1659 OffsetRgn( visrgn, -org.x, -org.y );
1662 /* intersect with the clipping region if the DC has one */
1663 cliprgn = CreateRectRgn( 0, 0, 0, 0);
1664 if (GetClipRgn( hdc, cliprgn ) != 1)
1666 DeleteObject( cliprgn );
1667 cliprgn = 0;
1669 else CombineRgn( visrgn, visrgn, cliprgn, RGN_AND );
1671 /* only those pixels in the scroll rectangle that remain in the clipping
1672 * rect are scrolled. */
1673 if (clip)
1674 clip_rect = *clip;
1675 else
1676 GetClipBox( hdc, &clip_rect );
1677 src_rect = clip_rect;
1678 OffsetRect( &clip_rect, -dx, -dy );
1679 IntersectRect( &src_rect, &src_rect, &clip_rect );
1681 /* if an scroll rectangle is specified, only the pixels within that
1682 * rectangle are scrolled */
1683 if (scroll) IntersectRect( &src_rect, &src_rect, scroll );
1685 /* now convert to device coordinates */
1686 LPtoDP( hdc, (LPPOINT)&src_rect, 2 );
1687 TRACE( "source rect: %s\n", wine_dbgstr_rect(&src_rect) );
1688 /* also dx and dy */
1689 SetRect( &offset, 0, 0, dx, dy );
1690 LPtoDP( hdc, (LPPOINT)&offset, 2 );
1691 dxdev = offset.right - offset.left;
1692 dydev = offset.bottom - offset.top;
1694 /* now intersect with the visible region to get the pixels that will actually scroll */
1695 dstrgn = CreateRectRgnIndirect( &src_rect );
1696 CombineRgn( dstrgn, dstrgn, visrgn, RGN_AND );
1697 OffsetRgn( dstrgn, dxdev, dydev );
1698 ExtSelectClipRgn( hdc, dstrgn, RGN_AND );
1700 /* compute the update areas. This is the combined clip rectangle
1701 * minus the scrolled region, and intersected with the visible region. */
1702 if (ret_update_rgn || update_rect)
1704 /* intersect clip and scroll rectangles, allowing NULL values */
1705 if (scroll)
1707 if (clip)
1708 IntersectRect( &clip_rect, clip, scroll );
1709 else
1710 clip_rect = *scroll;
1712 else if (clip)
1713 clip_rect = *clip;
1714 else
1715 GetClipBox( hdc, &clip_rect );
1717 /* Convert the combined clip rectangle to device coordinates */
1718 LPtoDP( hdc, (LPPOINT)&clip_rect, 2 );
1719 if (update_rgn)
1720 SetRectRgn( update_rgn, clip_rect.left, clip_rect.top, clip_rect.right, clip_rect.bottom );
1721 else
1722 update_rgn = CreateRectRgnIndirect( &clip_rect );
1724 CombineRgn( update_rgn, update_rgn, visrgn, RGN_AND );
1725 CombineRgn( update_rgn, update_rgn, dstrgn, RGN_DIFF );
1728 ret = USER_Driver->pScrollDC( hdc, dx, dy, update_rgn );
1730 if (ret && update_rect)
1732 GetRgnBox( update_rgn, update_rect );
1733 DPtoLP( hdc, (LPPOINT)update_rect, 2 );
1734 TRACE( "returning update_rect %s\n", wine_dbgstr_rect(update_rect) );
1736 if (!ret_update_rgn) DeleteObject( update_rgn );
1737 SelectClipRgn( hdc, cliprgn );
1738 if (cliprgn) DeleteObject( cliprgn );
1739 DeleteObject( visrgn );
1740 DeleteObject( dstrgn );
1741 return ret;
1744 /************************************************************************
1745 * PrintWindow (USER32.@)
1748 BOOL WINAPI PrintWindow(HWND hwnd, HDC hdcBlt, UINT nFlags)
1750 UINT flags = PRF_CHILDREN | PRF_ERASEBKGND | PRF_OWNED | PRF_CLIENT;
1751 if(!(nFlags & PW_CLIENTONLY))
1753 flags |= PRF_NONCLIENT;
1755 SendMessageW(hwnd, WM_PRINT, (WPARAM)hdcBlt, flags);
1756 return TRUE;