opengl32: Fix some style issues.
[wine.git] / dlls / user32 / painting.c
blob11447da1cdadb348ef035ec020062005645f595f
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 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam );
61 static const WCHAR displayW[] = { 'D','I','S','P','L','A','Y',0 };
64 /***********************************************************************
65 * dump_rdw_flags
67 static void dump_rdw_flags(UINT flags)
69 TRACE("flags:");
70 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
71 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
72 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
73 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
74 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
75 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
76 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
77 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
78 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
79 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
80 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
81 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
83 #define RDW_FLAGS \
84 (RDW_INVALIDATE | \
85 RDW_INTERNALPAINT | \
86 RDW_ERASE | \
87 RDW_VALIDATE | \
88 RDW_NOINTERNALPAINT | \
89 RDW_NOERASE | \
90 RDW_NOCHILDREN | \
91 RDW_ALLCHILDREN | \
92 RDW_UPDATENOW | \
93 RDW_ERASENOW | \
94 RDW_FRAME | \
95 RDW_NOFRAME)
97 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
98 TRACE("\n");
99 #undef RDW_FLAGS
103 /***********************************************************************
104 * update_visible_region
106 * Set the visible region and X11 drawable for the DC associated to
107 * a given window.
109 static void update_visible_region( struct dce *dce )
111 struct window_surface *surface = NULL;
112 NTSTATUS status;
113 HRGN vis_rgn = 0;
114 HWND top_win = 0;
115 DWORD flags = dce->flags;
116 DWORD paint_flags = 0;
117 size_t size = 256;
118 RECT win_rect, top_rect;
119 WND *win;
121 /* don't clip siblings if using parent clip region */
122 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
124 /* fetch the visible region from the server */
127 RGNDATA *data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 );
128 if (!data) return;
130 SERVER_START_REQ( get_visible_region )
132 req->window = wine_server_user_handle( dce->hwnd );
133 req->flags = flags;
134 wine_server_set_reply( req, data->Buffer, size );
135 if (!(status = wine_server_call( req )))
137 size_t reply_size = wine_server_reply_size( reply );
138 data->rdh.dwSize = sizeof(data->rdh);
139 data->rdh.iType = RDH_RECTANGLES;
140 data->rdh.nCount = reply_size / sizeof(RECT);
141 data->rdh.nRgnSize = reply_size;
142 vis_rgn = ExtCreateRegion( NULL, size, data );
144 top_win = wine_server_ptr_handle( reply->top_win );
145 win_rect.left = reply->win_rect.left;
146 win_rect.top = reply->win_rect.top;
147 win_rect.right = reply->win_rect.right;
148 win_rect.bottom = reply->win_rect.bottom;
149 top_rect.left = reply->top_rect.left;
150 top_rect.top = reply->top_rect.top;
151 top_rect.right = reply->top_rect.right;
152 top_rect.bottom = reply->top_rect.bottom;
153 paint_flags = reply->paint_flags;
155 else size = reply->total_size;
157 SERVER_END_REQ;
158 HeapFree( GetProcessHeap(), 0, data );
159 } while (status == STATUS_BUFFER_OVERFLOW);
161 if (status || !vis_rgn) return;
163 USER_Driver->pGetDC( dce->hdc, dce->hwnd, top_win, &win_rect, &top_rect, flags );
165 if (dce->clip_rgn) CombineRgn( vis_rgn, vis_rgn, dce->clip_rgn,
166 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
168 /* don't use a surface to paint the client area of OpenGL windows */
169 if (!(paint_flags & SET_WINPOS_PIXEL_FORMAT) || (flags & DCX_WINDOW))
171 win = WIN_GetPtr( top_win );
172 if (win && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
174 surface = win->surface;
175 if (surface) window_surface_add_ref( surface );
176 WIN_ReleasePtr( win );
180 if (!surface) top_rect = get_virtual_screen_rect();
181 __wine_set_visible_region( dce->hdc, vis_rgn, &win_rect, &top_rect, surface );
182 if (surface) window_surface_release( surface );
186 /***********************************************************************
187 * release_dce
189 static void release_dce( struct dce *dce )
191 if (!dce->hwnd) return; /* already released */
193 __wine_set_visible_region( dce->hdc, 0, &dummy_surface.rect, &dummy_surface.rect, &dummy_surface );
194 USER_Driver->pReleaseDC( dce->hwnd, dce->hdc );
196 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
197 dce->clip_rgn = 0;
198 dce->hwnd = 0;
199 dce->flags &= DCX_CACHE;
203 /***********************************************************************
204 * delete_clip_rgn
206 static void delete_clip_rgn( struct dce *dce )
208 if (!dce->clip_rgn) return; /* nothing to do */
210 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
211 DeleteObject( dce->clip_rgn );
212 dce->clip_rgn = 0;
214 /* make it dirty so that the vis rgn gets recomputed next time */
215 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
219 /***********************************************************************
220 * alloc_dce
222 * Allocate a new DCE.
224 static struct dce *alloc_dce(void)
226 struct dce *dce;
228 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(*dce) ))) return NULL;
229 if (!(dce->hdc = CreateDCW( displayW, NULL, NULL, NULL )))
231 HeapFree( GetProcessHeap(), 0, dce );
232 return 0;
234 dce->hwnd = 0;
235 dce->clip_rgn = 0;
236 dce->flags = 0;
237 dce->count = 1;
239 /* store DCE handle in DC hook data field */
240 SetDCHook( dce->hdc, dc_hook, (DWORD_PTR)dce );
241 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
242 return dce;
246 /***********************************************************************
247 * get_window_dce
249 static struct dce *get_window_dce( HWND hwnd )
251 struct dce *dce;
252 WND *win = WIN_GetPtr( hwnd );
254 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return NULL;
256 dce = win->dce;
257 if (!dce && (dce = get_class_dce( win->class )))
259 win->dce = dce;
260 dce->count++;
262 WIN_ReleasePtr( win );
264 if (!dce) /* try to allocate one */
266 struct dce *dce_to_free = NULL;
267 LONG class_style = GetClassLongW( hwnd, GCL_STYLE );
269 if (class_style & CS_CLASSDC)
271 if (!(dce = alloc_dce())) return NULL;
273 win = WIN_GetPtr( hwnd );
274 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
276 if (win->dce) /* another thread beat us to it */
278 dce_to_free = dce;
279 dce = win->dce;
281 else if ((win->dce = set_class_dce( win->class, dce )) != dce)
283 dce_to_free = dce;
284 dce = win->dce;
285 dce->count++;
287 else
289 dce->count++;
290 list_add_tail( &dce_list, &dce->entry );
292 WIN_ReleasePtr( win );
294 else dce_to_free = dce;
296 else if (class_style & CS_OWNDC)
298 if (!(dce = alloc_dce())) return NULL;
300 win = WIN_GetPtr( hwnd );
301 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
303 if (win->dwStyle & WS_CLIPCHILDREN) dce->flags |= DCX_CLIPCHILDREN;
304 if (win->dwStyle & WS_CLIPSIBLINGS) dce->flags |= DCX_CLIPSIBLINGS;
305 if (win->dce) /* another thread beat us to it */
307 dce_to_free = dce;
308 dce = win->dce;
310 else
312 win->dce = dce;
313 dce->hwnd = hwnd;
314 dce->count++;
315 list_add_tail( &dce_list, &dce->entry );
317 WIN_ReleasePtr( win );
319 else dce_to_free = dce;
322 if (dce_to_free)
324 SetDCHook( dce_to_free->hdc, NULL, 0 );
325 DeleteDC( dce_to_free->hdc );
326 HeapFree( GetProcessHeap(), 0, dce_to_free );
327 if (dce_to_free == dce)
328 dce = NULL;
331 return dce;
335 /***********************************************************************
336 * free_dce
338 * Free a class or window DCE.
340 void free_dce( struct dce *dce, HWND hwnd )
342 USER_Lock();
344 if (dce)
346 if (!--dce->count)
348 /* turn it into a cache entry */
349 SetHookFlags( dce->hdc, DCHF_RESETDC );
350 release_dce( dce );
351 dce->flags |= DCX_CACHE;
353 else if (dce->hwnd == hwnd)
355 release_dce( dce );
359 /* now check for cache DCEs */
361 if (hwnd)
363 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
365 if (dce->hwnd != hwnd) continue;
366 if (!(dce->flags & DCX_CACHE)) continue;
368 if (dce->count) WARN( "GetDC() without ReleaseDC() for window %p\n", hwnd );
369 dce->count = 0;
370 release_dce( dce );
374 USER_Unlock();
378 /***********************************************************************
379 * make_dc_dirty
381 * Mark the associated DC as dirty to force a refresh of the visible region
383 static void make_dc_dirty( struct dce *dce )
385 if (!dce->count)
387 /* Don't bother with visible regions of unused DCEs */
388 TRACE("\tpurged %p dce [%p]\n", dce, dce->hwnd);
389 release_dce( dce );
391 else
393 /* Set dirty bits in the hDC and DCE structs */
394 TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwnd);
395 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
400 /***********************************************************************
401 * invalidate_dce
403 * It is called from SetWindowPos() - we have to
404 * mark as dirty all busy DCEs for windows that have pWnd->parent as
405 * an ancestor and whose client rect intersects with specified update
406 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
407 * DCX_CLIPCHILDREN flag is set.
409 void invalidate_dce( WND *win, const RECT *extra_rect )
411 RECT window_rect;
412 struct dce *dce;
414 if (!win->parent) return;
416 GetWindowRect( win->obj.handle, &window_rect );
418 TRACE("%p parent %p %s (%s)\n",
419 win->obj.handle, win->parent, wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(extra_rect) );
421 /* walk all DCEs and fixup non-empty entries */
423 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
425 TRACE( "%p: hwnd %p dcx %08x %s %s\n", dce, dce->hwnd, dce->flags,
426 (dce->flags & DCX_CACHE) ? "Cache" : "Owned", dce->count ? "InUse" : "" );
428 if (!dce->hwnd) continue;
429 if ((dce->hwnd == win->parent) && !(dce->flags & DCX_CLIPCHILDREN))
430 continue; /* child window positions don't bother us */
432 /* if DCE window is a child of hwnd, it has to be invalidated */
433 if (dce->hwnd == win->obj.handle || IsChild( win->obj.handle, dce->hwnd ))
435 make_dc_dirty( dce );
436 continue;
439 /* otherwise check if the window rectangle intersects this DCE window */
440 if (win->parent == dce->hwnd || IsChild( win->parent, dce->hwnd ))
442 RECT dce_rect, tmp;
443 GetWindowRect( dce->hwnd, &dce_rect );
444 if (IntersectRect( &tmp, &dce_rect, &window_rect ) ||
445 (extra_rect && IntersectRect( &tmp, &dce_rect, extra_rect )))
446 make_dc_dirty( dce );
451 /***********************************************************************
452 * release_dc
454 * Implementation of ReleaseDC.
456 static INT release_dc( HWND hwnd, HDC hdc, BOOL end_paint )
458 struct dce *dce;
459 BOOL ret = FALSE;
461 TRACE("%p %p\n", hwnd, hdc );
463 USER_Lock();
464 dce = (struct dce *)GetDCHook( hdc, NULL );
465 if (dce && dce->count)
467 if (!(dce->flags & DCX_NORESETATTRS)) SetHookFlags( dce->hdc, DCHF_RESETDC );
468 if (end_paint || (dce->flags & DCX_CACHE)) delete_clip_rgn( dce );
469 if (dce->flags & DCX_CACHE) dce->count = 0;
470 ret = TRUE;
472 USER_Unlock();
473 return ret;
477 /***********************************************************************
478 * dc_hook
480 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
482 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam )
484 BOOL retv = TRUE;
485 struct dce *dce = (struct dce *)data;
487 TRACE("hDC = %p, %u\n", hDC, code);
489 if (!dce) return 0;
490 assert( dce->hdc == hDC );
492 switch( code )
494 case DCHC_INVALIDVISRGN:
495 /* GDI code calls this when it detects that the
496 * DC is dirty (usually after SetHookFlags()). This
497 * means that we have to recompute the visible region.
499 if (dce->count) update_visible_region( dce );
500 else /* non-fatal but shouldn't happen */
501 WARN("DC is not in use!\n");
502 break;
503 case DCHC_DELETEDC:
505 * Windows will not let you delete a DC that is busy
506 * (between GetDC and ReleaseDC)
508 USER_Lock();
509 if (dce->count > 1)
511 WARN("Application trying to delete a busy DC %p\n", dce->hdc);
512 retv = FALSE;
514 else
516 list_remove( &dce->entry );
517 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
518 HeapFree( GetProcessHeap(), 0, dce );
520 USER_Unlock();
521 break;
523 return retv;
527 /***********************************************************************
528 * get_update_region
530 * Return update region (in screen coordinates) for a window.
532 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
534 HRGN hrgn = 0;
535 NTSTATUS status;
536 RGNDATA *data;
537 size_t size = 256;
541 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
543 SetLastError( ERROR_OUTOFMEMORY );
544 return 0;
547 SERVER_START_REQ( get_update_region )
549 req->window = wine_server_user_handle( hwnd );
550 req->from_child = wine_server_user_handle( child ? *child : 0 );
551 req->flags = *flags;
552 wine_server_set_reply( req, data->Buffer, size );
553 if (!(status = wine_server_call( req )))
555 size_t reply_size = wine_server_reply_size( reply );
556 data->rdh.dwSize = sizeof(data->rdh);
557 data->rdh.iType = RDH_RECTANGLES;
558 data->rdh.nCount = reply_size / sizeof(RECT);
559 data->rdh.nRgnSize = reply_size;
560 hrgn = ExtCreateRegion( NULL, size, data );
561 if (child) *child = wine_server_ptr_handle( reply->child );
562 *flags = reply->flags;
564 else size = reply->total_size;
566 SERVER_END_REQ;
567 HeapFree( GetProcessHeap(), 0, data );
568 } while (status == STATUS_BUFFER_OVERFLOW);
570 if (status) SetLastError( RtlNtStatusToDosError(status) );
571 return hrgn;
575 /***********************************************************************
576 * get_update_flags
578 * Get only the update flags, not the update region.
580 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
582 BOOL ret;
584 SERVER_START_REQ( get_update_region )
586 req->window = wine_server_user_handle( hwnd );
587 req->from_child = wine_server_user_handle( child ? *child : 0 );
588 req->flags = *flags | UPDATE_NOREGION;
589 if ((ret = !wine_server_call_err( req )))
591 if (child) *child = wine_server_ptr_handle( reply->child );
592 *flags = reply->flags;
595 SERVER_END_REQ;
596 return ret;
600 /***********************************************************************
601 * redraw_window_rects
603 * Redraw part of a window.
605 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
607 BOOL ret;
609 if (!(flags & (RDW_INVALIDATE|RDW_VALIDATE|RDW_INTERNALPAINT|RDW_NOINTERNALPAINT)))
610 return TRUE; /* nothing to do */
612 SERVER_START_REQ( redraw_window )
614 req->window = wine_server_user_handle( hwnd );
615 req->flags = flags;
616 wine_server_add_data( req, rects, count * sizeof(RECT) );
617 ret = !wine_server_call_err( req );
619 SERVER_END_REQ;
620 return ret;
624 /***********************************************************************
625 * send_ncpaint
627 * Send a WM_NCPAINT message if needed, and return the resulting update region (in screen coords).
628 * Helper for erase_now and BeginPaint.
630 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
632 HRGN whole_rgn = get_update_region( hwnd, flags, child );
633 HRGN client_rgn = 0;
635 if (child) hwnd = *child;
637 if (hwnd == GetDesktopWindow()) return whole_rgn;
639 if (whole_rgn)
641 RECT client, update;
642 INT type;
644 /* check if update rgn overlaps with nonclient area */
645 type = GetRgnBox( whole_rgn, &update );
646 WIN_GetRectangles( hwnd, COORDS_SCREEN, 0, &client );
648 if ((*flags & UPDATE_NONCLIENT) ||
649 update.left < client.left || update.top < client.top ||
650 update.right > client.right || update.bottom > client.bottom)
652 client_rgn = CreateRectRgnIndirect( &client );
653 CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
655 /* check if update rgn contains complete nonclient area */
656 if (type == SIMPLEREGION)
658 RECT window;
659 GetWindowRect( hwnd, &window );
660 if (EqualRect( &window, &update ))
662 DeleteObject( whole_rgn );
663 whole_rgn = (HRGN)1;
667 else
669 client_rgn = whole_rgn;
670 whole_rgn = 0;
673 if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
675 if (*flags & UPDATE_NONCLIENT) SendMessageW( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
676 if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
679 return client_rgn;
683 /***********************************************************************
684 * send_erase
686 * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
687 * If a DC is requested, the region is selected into it. In all cases the region is deleted.
688 * Helper for erase_now and BeginPaint.
690 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
691 RECT *clip_rect, HDC *hdc_ret )
693 BOOL need_erase = (flags & UPDATE_DELAYED_ERASE) != 0;
694 HDC hdc = 0;
695 RECT dummy;
697 if (!clip_rect) clip_rect = &dummy;
698 if (hdc_ret || (flags & UPDATE_ERASE))
700 UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
701 if (IsIconic(hwnd)) dcx_flags |= DCX_WINDOW;
703 if ((hdc = GetDCEx( hwnd, client_rgn, dcx_flags )))
705 INT type = GetClipBox( hdc, clip_rect );
707 if (flags & UPDATE_ERASE)
709 /* don't erase if the clip box is empty */
710 if (type != NULLREGION)
711 need_erase = !SendMessageW( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
713 if (!hdc_ret) release_dc( hwnd, hdc, TRUE );
716 if (hdc_ret) *hdc_ret = hdc;
718 if (!hdc) DeleteObject( client_rgn );
719 return need_erase;
723 /***********************************************************************
724 * erase_now
726 * Implementation of RDW_ERASENOW behavior.
728 void erase_now( HWND hwnd, UINT rdw_flags )
730 HWND child = 0;
731 HRGN hrgn;
732 BOOL need_erase = FALSE;
734 /* loop while we find a child to repaint */
735 for (;;)
737 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
739 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
740 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
741 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
743 if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
744 need_erase = send_erase( child, flags, hrgn, NULL, NULL );
746 if (!flags) break; /* nothing more to do */
747 if ((rdw_flags & RDW_NOCHILDREN) && !need_erase) break;
752 /***********************************************************************
753 * move_window_bits
755 * Move the window bits when a window is resized or its surface recreated.
757 void move_window_bits( HWND hwnd, struct window_surface *old_surface,
758 struct window_surface *new_surface,
759 const RECT *visible_rect, const RECT *old_visible_rect,
760 const RECT *client_rect, const RECT *valid_rects )
762 RECT dst = valid_rects[0];
763 RECT src = valid_rects[1];
765 if (new_surface != old_surface ||
766 src.left - old_visible_rect->left != dst.left - visible_rect->left ||
767 src.top - old_visible_rect->top != dst.top - visible_rect->top)
769 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
770 BITMAPINFO *info = (BITMAPINFO *)buffer;
771 void *bits;
772 UINT flags = UPDATE_NOCHILDREN;
773 HRGN rgn = get_update_region( hwnd, &flags, NULL );
774 HDC hdc = GetDCEx( hwnd, rgn, DCX_CACHE | DCX_EXCLUDERGN );
776 OffsetRect( &dst, -client_rect->left, -client_rect->top );
777 TRACE( "copying %s -> %s\n", wine_dbgstr_rect(&src), wine_dbgstr_rect(&dst) );
778 bits = old_surface->funcs->get_info( old_surface, info );
779 old_surface->funcs->lock( old_surface );
780 SetDIBitsToDevice( hdc, dst.left, dst.top, dst.right - dst.left, dst.bottom - dst.top,
781 src.left - old_visible_rect->left - old_surface->rect.left,
782 old_surface->rect.bottom - (src.bottom - old_visible_rect->top),
783 0, old_surface->rect.bottom - old_surface->rect.top,
784 bits, info, DIB_RGB_COLORS );
785 old_surface->funcs->unlock( old_surface );
786 ReleaseDC( hwnd, hdc );
791 /***********************************************************************
792 * update_now
794 * Implementation of RDW_UPDATENOW behavior.
796 static void update_now( HWND hwnd, UINT rdw_flags )
798 HWND child = 0;
800 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
801 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
803 /* loop while we find a child to repaint */
804 for (;;)
806 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
808 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
809 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
811 if (!get_update_flags( hwnd, &child, &flags )) break;
812 if (!flags) break; /* nothing more to do */
814 SendMessageW( child, WM_PAINT, 0, 0 );
815 if (rdw_flags & RDW_NOCHILDREN) break;
820 /*************************************************************************
821 * fix_caret
823 * Helper for ScrollWindowEx:
824 * If the return value is 0, no special caret handling is necessary.
825 * Otherwise the return value is the handle of the window that owns the
826 * caret. Its caret needs to be hidden during the scroll operation and
827 * moved to new_caret_pos if move_caret is TRUE.
829 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
830 UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
832 GUITHREADINFO info;
833 RECT rect, mapped_rcCaret;
834 BOOL hide_caret = FALSE;
836 info.cbSize = sizeof(info);
837 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
838 if (!info.hwndCaret) return 0;
840 if (info.hwndCaret == hWnd)
842 /* Move the caret if it's (partially) in the source rectangle */
843 if (IntersectRect(&rect, scroll_rect, &info.rcCaret))
845 *move_caret = TRUE;
846 hide_caret = TRUE;
847 new_caret_pos->x = info.rcCaret.left + dx;
848 new_caret_pos->y = info.rcCaret.top + dy;
850 else
852 *move_caret = FALSE;
854 /* Hide the caret if it's in the destination rectangle */
855 rect = *scroll_rect;
856 OffsetRect(&rect, dx, dy);
857 hide_caret = IntersectRect(&rect, &rect, &info.rcCaret);
860 else
862 if ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret))
864 *move_caret = FALSE;
866 /* Hide the caret if it's in the source or in the destination
867 rectangle */
868 mapped_rcCaret = info.rcCaret;
869 MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
871 if (IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
873 hide_caret = TRUE;
875 else
877 rect = *scroll_rect;
878 OffsetRect(&rect, dx, dy);
879 hide_caret = IntersectRect(&rect, &rect, &mapped_rcCaret);
882 else
883 return 0;
886 if (hide_caret)
888 HideCaret(info.hwndCaret);
889 return info.hwndCaret;
891 else
892 return 0;
896 /***********************************************************************
897 * BeginPaint (USER32.@)
899 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
901 HRGN hrgn;
902 HDC hdc;
903 BOOL erase;
904 RECT rect;
905 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
907 HideCaret( hwnd );
909 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
911 erase = send_erase( hwnd, flags, hrgn, &rect, &hdc );
913 TRACE("hdc = %p box = (%s), fErase = %d\n", hdc, wine_dbgstr_rect(&rect), erase);
915 if (!lps)
917 release_dc( hwnd, hdc, TRUE );
918 return 0;
920 lps->fErase = erase;
921 lps->rcPaint = rect;
922 lps->hdc = hdc;
923 return hdc;
927 /***********************************************************************
928 * EndPaint (USER32.@)
930 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
932 ShowCaret( hwnd );
933 flush_window_surfaces( FALSE );
934 if (!lps) return FALSE;
935 release_dc( hwnd, lps->hdc, TRUE );
936 return TRUE;
940 /***********************************************************************
941 * GetDCEx (USER32.@)
943 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
945 const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
946 const DWORD user_flags = clip_flags | DCX_NORESETATTRS; /* flags that can be set by user */
947 struct dce *dce;
948 BOOL bUpdateVisRgn = TRUE;
949 HWND parent;
950 LONG window_style = GetWindowLongW( hwnd, GWL_STYLE );
952 if (!hwnd) hwnd = GetDesktopWindow();
953 else hwnd = WIN_GetFullHandle( hwnd );
955 TRACE("hwnd %p, hrgnClip %p, flags %08x\n", hwnd, hrgnClip, flags);
957 if (!IsWindow(hwnd)) return 0;
959 /* fixup flags */
961 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
963 if (flags & DCX_USESTYLE)
965 flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
967 if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
969 if (!(flags & DCX_WINDOW))
971 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
973 if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
974 flags |= DCX_CLIPCHILDREN;
978 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
980 parent = GetAncestor( hwnd, GA_PARENT );
981 if (!parent || (parent == GetDesktopWindow()))
982 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
984 /* it seems parent clip is ignored when clipping siblings or children */
985 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
987 if( flags & DCX_PARENTCLIP )
989 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
990 if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
992 flags &= ~DCX_CLIPCHILDREN;
993 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
997 /* find a suitable DCE */
999 if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
1001 struct dce *dceEmpty = NULL, *dceUnused = NULL;
1003 /* Strategy: First, we attempt to find a non-empty but unused DCE with
1004 * compatible flags. Next, we look for an empty entry. If the cache is
1005 * full we have to purge one of the unused entries.
1007 USER_Lock();
1008 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
1010 if ((dce->flags & DCX_CACHE) && !dce->count)
1012 dceUnused = dce;
1014 if (!dce->hwnd) dceEmpty = dce;
1015 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
1017 TRACE("\tfound valid %p dce [%p], flags %08x\n",
1018 dce, hwnd, dce->flags );
1019 bUpdateVisRgn = FALSE;
1020 break;
1025 if (&dce->entry == &dce_list) /* nothing found */
1026 dce = dceEmpty ? dceEmpty : dceUnused;
1028 if (dce) dce->count = 1;
1030 USER_Unlock();
1032 /* if there's no dce empty or unused, allocate a new one */
1033 if (!dce)
1035 if (!(dce = alloc_dce())) return 0;
1036 dce->flags = DCX_CACHE;
1037 USER_Lock();
1038 list_add_head( &dce_list, &dce->entry );
1039 USER_Unlock();
1042 else
1044 flags |= DCX_NORESETATTRS;
1045 if (dce->hwnd == hwnd)
1047 TRACE("\tskipping hVisRgn update\n");
1048 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
1050 else
1052 /* we should free dce->clip_rgn here, but Windows apparently doesn't */
1053 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
1054 dce->clip_rgn = 0;
1058 if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
1060 /* if the extra clip region has changed, get rid of the old one */
1061 if (dce->clip_rgn != hrgnClip || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
1062 delete_clip_rgn( dce );
1063 dce->clip_rgn = hrgnClip;
1064 if (!dce->clip_rgn) dce->clip_rgn = CreateRectRgn( 0, 0, 0, 0 );
1065 dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
1066 bUpdateVisRgn = TRUE;
1069 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) SetLayout( dce->hdc, LAYOUT_RTL );
1071 dce->hwnd = hwnd;
1072 dce->flags = (dce->flags & ~user_flags) | (flags & user_flags);
1074 if (SetHookFlags( dce->hdc, DCHF_VALIDATEVISRGN )) bUpdateVisRgn = TRUE; /* DC was dirty */
1076 if (bUpdateVisRgn) update_visible_region( dce );
1078 TRACE("(%p,%p,0x%x): returning %p\n", hwnd, hrgnClip, flags, dce->hdc);
1079 return dce->hdc;
1083 /***********************************************************************
1084 * GetDC (USER32.@)
1086 * Get a device context.
1088 * RETURNS
1089 * Success: Handle to the device context
1090 * Failure: NULL.
1092 HDC WINAPI GetDC( HWND hwnd )
1094 if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
1095 return GetDCEx( hwnd, 0, DCX_USESTYLE );
1099 /***********************************************************************
1100 * GetWindowDC (USER32.@)
1102 HDC WINAPI GetWindowDC( HWND hwnd )
1104 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
1108 /***********************************************************************
1109 * ReleaseDC (USER32.@)
1111 * Release a device context.
1113 * RETURNS
1114 * Success: Non-zero. Resources used by hdc are released.
1115 * Failure: 0.
1117 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
1119 return release_dc( hwnd, hdc, FALSE );
1123 /**********************************************************************
1124 * WindowFromDC (USER32.@)
1126 HWND WINAPI WindowFromDC( HDC hdc )
1128 struct dce *dce;
1129 HWND hwnd = 0;
1131 USER_Lock();
1132 dce = (struct dce *)GetDCHook( hdc, NULL );
1133 if (dce) hwnd = dce->hwnd;
1134 USER_Unlock();
1135 return hwnd;
1139 /***********************************************************************
1140 * LockWindowUpdate (USER32.@)
1142 * Enables or disables painting in the chosen window.
1144 * PARAMS
1145 * hwnd [I] handle to a window.
1147 * RETURNS
1148 * If successful, returns nonzero value. Otherwise,
1149 * returns 0.
1151 * NOTES
1152 * You can lock only one window at a time.
1154 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1156 static HWND lockedWnd;
1158 FIXME("(%p), partial stub!\n",hwnd);
1160 USER_Lock();
1161 if (lockedWnd)
1163 if (!hwnd)
1165 /* Unlock lockedWnd */
1166 /* FIXME: Do something */
1168 else
1170 /* Attempted to lock a second window */
1171 /* Return FALSE and do nothing */
1172 USER_Unlock();
1173 return FALSE;
1176 lockedWnd = hwnd;
1177 USER_Unlock();
1178 return TRUE;
1182 /***********************************************************************
1183 * RedrawWindow (USER32.@)
1185 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1187 static const RECT empty;
1188 BOOL ret;
1190 if (!hwnd) hwnd = GetDesktopWindow();
1192 if (TRACE_ON(win))
1194 if (hrgn)
1196 RECT r;
1197 GetRgnBox( hrgn, &r );
1198 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1200 else if (rect)
1201 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1202 else
1203 TRACE( "%p whole window ", hwnd );
1205 dump_rdw_flags(flags);
1208 /* process pending expose events before painting */
1209 if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
1211 if (rect && !hrgn)
1213 if (IsRectEmpty( rect )) rect = &empty;
1214 ret = redraw_window_rects( hwnd, flags, rect, 1 );
1216 else if (!hrgn)
1218 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1220 else /* need to build a list of the region rectangles */
1222 DWORD size;
1223 RGNDATA *data = NULL;
1225 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
1226 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1227 GetRegionData( hrgn, size, data );
1228 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
1229 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1230 else
1231 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1232 HeapFree( GetProcessHeap(), 0, data );
1235 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1236 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1238 return ret;
1242 /***********************************************************************
1243 * UpdateWindow (USER32.@)
1245 BOOL WINAPI UpdateWindow( HWND hwnd )
1247 if (!hwnd)
1249 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1250 return FALSE;
1253 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1257 /***********************************************************************
1258 * InvalidateRgn (USER32.@)
1260 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1262 if (!hwnd)
1264 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1265 return FALSE;
1268 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1272 /***********************************************************************
1273 * InvalidateRect (USER32.@)
1275 * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
1276 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1278 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1280 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1282 if (!hwnd)
1284 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1285 rect = NULL;
1288 return RedrawWindow( hwnd, rect, 0, flags );
1292 /***********************************************************************
1293 * ValidateRgn (USER32.@)
1295 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1297 if (!hwnd)
1299 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1300 return FALSE;
1303 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
1307 /***********************************************************************
1308 * ValidateRect (USER32.@)
1310 * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
1311 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1313 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1315 UINT flags = RDW_VALIDATE;
1317 if (!hwnd)
1319 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1320 rect = NULL;
1323 return RedrawWindow( hwnd, rect, 0, flags );
1327 /***********************************************************************
1328 * GetUpdateRgn (USER32.@)
1330 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1332 INT retval = ERROR;
1333 UINT flags = UPDATE_NOCHILDREN;
1334 HRGN update_rgn;
1336 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1338 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1340 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1341 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1343 flags = UPDATE_DELAYED_ERASE;
1344 get_update_flags( hwnd, NULL, &flags );
1346 /* map region to client coordinates */
1347 map_window_region( 0, hwnd, hrgn );
1349 return retval;
1353 /***********************************************************************
1354 * GetUpdateRect (USER32.@)
1356 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1358 UINT flags = UPDATE_NOCHILDREN;
1359 HRGN update_rgn;
1360 BOOL need_erase;
1362 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1364 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
1366 if (rect)
1368 if (GetRgnBox( update_rgn, rect ) != NULLREGION)
1370 HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
1371 DWORD layout = SetLayout( hdc, 0 ); /* MapWindowPoints mirrors already */
1372 MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
1373 DPtoLP( hdc, (LPPOINT)rect, 2 );
1374 SetLayout( hdc, layout );
1375 ReleaseDC( hwnd, hdc );
1378 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1380 /* check if we still have an update region */
1381 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1382 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1383 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
1387 /***********************************************************************
1388 * ExcludeUpdateRgn (USER32.@)
1390 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1392 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
1393 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
1395 if (ret != ERROR)
1397 POINT pt;
1399 GetDCOrgEx( hdc, &pt );
1400 MapWindowPoints( 0, hwnd, &pt, 1 );
1401 OffsetRgn( update_rgn, -pt.x, -pt.y );
1402 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1403 DeleteObject( update_rgn );
1405 return ret;
1409 static INT scroll_window( HWND hwnd, INT dx, INT dy, const RECT *rect, const RECT *clipRect,
1410 HRGN hrgnUpdate, LPRECT rcUpdate, UINT flags, BOOL is_ex )
1412 INT retVal = NULLREGION;
1413 BOOL bOwnRgn = TRUE;
1414 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
1415 int rdw_flags;
1416 HRGN hrgnTemp;
1417 HRGN hrgnWinupd = 0;
1418 HDC hDC;
1419 RECT rc, cliprc;
1420 HWND hwndCaret = NULL;
1421 BOOL moveCaret = FALSE;
1422 POINT newCaretPos;
1424 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
1425 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
1426 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
1427 if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
1428 FIXME("some flags (%04x) are unhandled\n", flags);
1430 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1431 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
1433 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
1434 hwnd = WIN_GetFullHandle( hwnd );
1436 GetClientRect(hwnd, &rc);
1438 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
1439 else cliprc = rc;
1441 if (rect) IntersectRect(&rc, &rc, rect);
1443 if( hrgnUpdate ) bOwnRgn = FALSE;
1444 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
1446 newCaretPos.x = newCaretPos.y = 0;
1448 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
1449 DWORD dcxflags = 0;
1450 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1452 hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
1454 if (is_ex) dcxflags |= DCX_CACHE;
1455 if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1456 if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
1457 dcxflags |= DCX_PARENTCLIP;
1458 if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1459 dcxflags |= DCX_CLIPCHILDREN;
1460 hDC = GetDCEx( hwnd, 0, dcxflags);
1461 if (hDC)
1463 ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
1465 ReleaseDC( hwnd, hDC );
1467 if (!bUpdate)
1468 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
1471 /* If the windows has an update region, this must be
1472 * scrolled as well. Keep a copy in hrgnWinupd
1473 * to be added to hrngUpdate at the end. */
1474 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
1475 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
1476 if (retVal != NULLREGION)
1478 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
1479 if( !bOwnRgn) {
1480 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
1481 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
1483 OffsetRgn( hrgnTemp, dx, dy );
1484 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1485 if( !bOwnRgn)
1486 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1487 RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
1489 /* Catch the case where the scrolling amount exceeds the size of the
1490 * original window. This generated a second update area that is the
1491 * location where the original scrolled content would end up.
1492 * This second region is not returned by the ScrollDC and sets
1493 * ScrollWindowEx apart from just a ScrollDC.
1495 * This has been verified with testing on windows.
1497 if (abs(dx) > abs(rc.right - rc.left) ||
1498 abs(dy) > abs(rc.bottom - rc.top))
1500 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
1501 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1502 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
1504 if( !bOwnRgn)
1505 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1507 DeleteObject( hrgnClip );
1509 DeleteObject( hrgnTemp );
1510 } else {
1511 /* nothing was scrolled */
1512 if( !bOwnRgn)
1513 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
1514 SetRectEmpty( rcUpdate);
1517 if( flags & SW_SCROLLCHILDREN )
1519 HWND *list = WIN_ListChildren( hwnd );
1520 if (list)
1522 int i;
1523 RECT r, dummy;
1524 for (i = 0; list[i]; i++)
1526 WIN_GetRectangles( list[i], COORDS_PARENT, &r, NULL );
1527 if (!rect || IntersectRect(&dummy, &r, rect))
1528 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
1529 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1530 SWP_NOREDRAW | SWP_DEFERERASE );
1532 HeapFree( GetProcessHeap(), 0, list );
1536 if( flags & (SW_INVALIDATE | SW_ERASE) )
1537 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
1538 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1540 if( hrgnWinupd) {
1541 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
1542 DeleteObject( hrgnWinupd);
1545 if( hwndCaret ) {
1546 if ( moveCaret ) SetCaretPos( newCaretPos.x, newCaretPos.y );
1547 ShowCaret(hwndCaret);
1550 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
1552 return retVal;
1556 /*************************************************************************
1557 * ScrollWindowEx (USER32.@)
1559 * Note: contrary to what the doc says, pixels that are scrolled from the
1560 * outside of clipRect to the inside are NOT painted.
1563 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
1564 const RECT *rect, const RECT *clipRect,
1565 HRGN hrgnUpdate, LPRECT rcUpdate,
1566 UINT flags )
1568 return scroll_window( hwnd, dx, dy, rect, clipRect, hrgnUpdate, rcUpdate, flags, TRUE );
1571 /*************************************************************************
1572 * ScrollWindow (USER32.@)
1575 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
1576 const RECT *rect, const RECT *clipRect )
1578 return scroll_window( hwnd, dx, dy, rect, clipRect, 0, NULL,
1579 SW_INVALIDATE | SW_ERASE | (rect ? 0 : SW_SCROLLCHILDREN), FALSE ) != ERROR;
1583 /*************************************************************************
1584 * ScrollDC (USER32.@)
1586 * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is
1587 * wrong) hrgnUpdate is returned in device coordinates with rcUpdate in
1588 * logical coordinates.
1590 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *scroll, const RECT *clip,
1591 HRGN ret_update_rgn, LPRECT update_rect )
1594 HRGN update_rgn = ret_update_rgn;
1595 RECT src_rect, clip_rect, offset;
1596 INT dxdev, dydev;
1597 HRGN dstrgn, cliprgn, visrgn;
1598 BOOL ret;
1600 TRACE( "dx,dy %d,%d scroll %s clip %s update %p rect %p\n",
1601 dx, dy, wine_dbgstr_rect(scroll), wine_dbgstr_rect(clip), ret_update_rgn, update_rect );
1603 /* get the visible region */
1604 visrgn = CreateRectRgn( 0, 0, 0, 0 );
1605 GetRandomRgn( hdc, visrgn, SYSRGN );
1606 if (!(GetVersion() & 0x80000000))
1608 POINT org;
1609 GetDCOrgEx( hdc, &org );
1610 OffsetRgn( visrgn, -org.x, -org.y );
1613 /* intersect with the clipping region if the DC has one */
1614 cliprgn = CreateRectRgn( 0, 0, 0, 0);
1615 if (GetClipRgn( hdc, cliprgn ) != 1)
1617 DeleteObject( cliprgn );
1618 cliprgn = 0;
1620 else CombineRgn( visrgn, visrgn, cliprgn, RGN_AND );
1622 /* only those pixels in the scroll rectangle that remain in the clipping
1623 * rect are scrolled. */
1624 if (clip)
1625 clip_rect = *clip;
1626 else
1627 GetClipBox( hdc, &clip_rect );
1628 src_rect = clip_rect;
1629 OffsetRect( &clip_rect, -dx, -dy );
1630 IntersectRect( &src_rect, &src_rect, &clip_rect );
1632 /* if an scroll rectangle is specified, only the pixels within that
1633 * rectangle are scrolled */
1634 if (scroll) IntersectRect( &src_rect, &src_rect, scroll );
1636 /* now convert to device coordinates */
1637 LPtoDP( hdc, (LPPOINT)&src_rect, 2 );
1638 TRACE( "source rect: %s\n", wine_dbgstr_rect(&src_rect) );
1639 /* also dx and dy */
1640 SetRect( &offset, 0, 0, dx, dy );
1641 LPtoDP( hdc, (LPPOINT)&offset, 2 );
1642 dxdev = offset.right - offset.left;
1643 dydev = offset.bottom - offset.top;
1645 /* now intersect with the visible region to get the pixels that will actually scroll */
1646 dstrgn = CreateRectRgnIndirect( &src_rect );
1647 CombineRgn( dstrgn, dstrgn, visrgn, RGN_AND );
1648 OffsetRgn( dstrgn, dxdev, dydev );
1649 ExtSelectClipRgn( hdc, dstrgn, RGN_AND );
1651 /* compute the update areas. This is the combined clip rectangle
1652 * minus the scrolled region, and intersected with the visible region. */
1653 if (ret_update_rgn || update_rect)
1655 /* intersect clip and scroll rectangles, allowing NULL values */
1656 if (scroll)
1658 if (clip)
1659 IntersectRect( &clip_rect, clip, scroll );
1660 else
1661 clip_rect = *scroll;
1663 else if (clip)
1664 clip_rect = *clip;
1665 else
1666 GetClipBox( hdc, &clip_rect );
1668 /* Convert the combined clip rectangle to device coordinates */
1669 LPtoDP( hdc, (LPPOINT)&clip_rect, 2 );
1670 if (update_rgn)
1671 SetRectRgn( update_rgn, clip_rect.left, clip_rect.top, clip_rect.right, clip_rect.bottom );
1672 else
1673 update_rgn = CreateRectRgnIndirect( &clip_rect );
1675 CombineRgn( update_rgn, update_rgn, visrgn, RGN_AND );
1676 CombineRgn( update_rgn, update_rgn, dstrgn, RGN_DIFF );
1679 ret = USER_Driver->pScrollDC( hdc, dx, dy, update_rgn );
1681 if (ret && update_rect)
1683 GetRgnBox( update_rgn, update_rect );
1684 DPtoLP( hdc, (LPPOINT)update_rect, 2 );
1685 TRACE( "returning update_rect %s\n", wine_dbgstr_rect(update_rect) );
1687 if (!ret_update_rgn) DeleteObject( update_rgn );
1688 SelectClipRgn( hdc, cliprgn );
1689 if (cliprgn) DeleteObject( cliprgn );
1690 DeleteObject( visrgn );
1691 DeleteObject( dstrgn );
1692 return ret;
1695 /************************************************************************
1696 * PrintWindow (USER32.@)
1699 BOOL WINAPI PrintWindow(HWND hwnd, HDC hdcBlt, UINT nFlags)
1701 UINT flags = PRF_CHILDREN | PRF_ERASEBKGND | PRF_OWNED | PRF_CLIENT;
1702 if(!(nFlags & PW_CLIENTONLY))
1704 flags |= PRF_NONCLIENT;
1706 SendMessageW(hwnd, WM_PRINT, (WPARAM)hdcBlt, flags);
1707 return TRUE;