push a18c97e3310ae68c9cf1d09430f25ca292f320eb
[wine/hacks.git] / dlls / user32 / painting.c
blob93fe97cf869f2180230709ba772db38072c9cc6c
1 /*
2 * Window painting functions
4 * Copyright 1993, 1994, 1995, 2001, 2004 Alexandre Julliard
5 * Copyright 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 <stdarg.h>
26 #include <string.h>
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "wine/server.h"
35 #include "win.h"
36 #include "user_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(win);
42 /***********************************************************************
43 * dump_rdw_flags
45 static void dump_rdw_flags(UINT flags)
47 TRACE("flags:");
48 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
49 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
50 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
51 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
52 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
53 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
54 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
55 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
56 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
57 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
58 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
59 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
61 #define RDW_FLAGS \
62 (RDW_INVALIDATE | \
63 RDW_INTERNALPAINT | \
64 RDW_ERASE | \
65 RDW_VALIDATE | \
66 RDW_NOINTERNALPAINT | \
67 RDW_NOERASE | \
68 RDW_NOCHILDREN | \
69 RDW_ALLCHILDREN | \
70 RDW_UPDATENOW | \
71 RDW_ERASENOW | \
72 RDW_FRAME | \
73 RDW_NOFRAME)
75 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
76 TRACE("\n");
77 #undef RDW_FLAGS
81 /***********************************************************************
82 * get_update_region
84 * Return update region (in screen coordinates) for a window.
86 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
88 HRGN hrgn = 0;
89 NTSTATUS status;
90 RGNDATA *data;
91 size_t size = 256;
95 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
97 SetLastError( ERROR_OUTOFMEMORY );
98 return 0;
101 SERVER_START_REQ( get_update_region )
103 req->window = hwnd;
104 req->from_child = child ? *child : 0;
105 req->flags = *flags;
106 wine_server_set_reply( req, data->Buffer, size );
107 if (!(status = wine_server_call( req )))
109 size_t reply_size = wine_server_reply_size( reply );
110 data->rdh.dwSize = sizeof(data->rdh);
111 data->rdh.iType = RDH_RECTANGLES;
112 data->rdh.nCount = reply_size / sizeof(RECT);
113 data->rdh.nRgnSize = reply_size;
114 hrgn = ExtCreateRegion( NULL, size, data );
115 if (child) *child = reply->child;
116 *flags = reply->flags;
118 else size = reply->total_size;
120 SERVER_END_REQ;
121 HeapFree( GetProcessHeap(), 0, data );
122 } while (status == STATUS_BUFFER_OVERFLOW);
124 if (status) SetLastError( RtlNtStatusToDosError(status) );
125 return hrgn;
129 /***********************************************************************
130 * get_update_flags
132 * Get only the update flags, not the update region.
134 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
136 BOOL ret;
138 SERVER_START_REQ( get_update_region )
140 req->window = hwnd;
141 req->from_child = child ? *child : 0;
142 req->flags = *flags | UPDATE_NOREGION;
143 if ((ret = !wine_server_call_err( req )))
145 if (child) *child = reply->child;
146 *flags = reply->flags;
149 SERVER_END_REQ;
150 return ret;
154 /***********************************************************************
155 * redraw_window_rects
157 * Redraw part of a window.
159 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
161 BOOL ret;
163 if (!(flags & (RDW_INVALIDATE|RDW_VALIDATE|RDW_INTERNALPAINT|RDW_NOINTERNALPAINT)))
164 return TRUE; /* nothing to do */
166 SERVER_START_REQ( redraw_window )
168 req->window = hwnd;
169 req->flags = flags;
170 wine_server_add_data( req, rects, count * sizeof(RECT) );
171 ret = !wine_server_call_err( req );
173 SERVER_END_REQ;
174 return ret;
178 /***********************************************************************
179 * send_ncpaint
181 * Send a WM_NCPAINT message if needed, and return the resulting update region (in screen coords).
182 * Helper for erase_now and BeginPaint.
184 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
186 HRGN whole_rgn = get_update_region( hwnd, flags, child );
187 HRGN client_rgn = 0;
189 if (child) hwnd = *child;
191 if (whole_rgn)
193 RECT client, update;
194 INT type;
196 /* check if update rgn overlaps with nonclient area */
197 type = GetRgnBox( whole_rgn, &update );
198 GetClientRect( hwnd, &client );
199 MapWindowPoints( hwnd, 0, (POINT *)&client, 2 );
201 if ((*flags & UPDATE_NONCLIENT) ||
202 update.left < client.left || update.top < client.top ||
203 update.right > client.right || update.bottom > client.bottom)
205 client_rgn = CreateRectRgnIndirect( &client );
206 CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
208 /* check if update rgn contains complete nonclient area */
209 if (type == SIMPLEREGION)
211 RECT window;
212 GetWindowRect( hwnd, &window );
213 if (EqualRect( &window, &update ))
215 DeleteObject( whole_rgn );
216 whole_rgn = (HRGN)1;
220 else
222 client_rgn = whole_rgn;
223 whole_rgn = 0;
226 if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
228 if (*flags & UPDATE_NONCLIENT) SendMessageW( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
229 if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
232 return client_rgn;
236 /***********************************************************************
237 * send_erase
239 * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
240 * If a DC is requested, the region is selected into it. In all cases the region is deleted.
241 * Helper for erase_now and BeginPaint.
243 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
244 RECT *clip_rect, HDC *hdc_ret )
246 BOOL need_erase = (flags & UPDATE_DELAYED_ERASE) != 0;
247 HDC hdc = 0;
248 RECT dummy;
250 if (!clip_rect) clip_rect = &dummy;
251 if (hdc_ret || (flags & UPDATE_ERASE))
253 UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
254 if (IsIconic(hwnd)) dcx_flags |= DCX_WINDOW;
256 if ((hdc = GetDCEx( hwnd, client_rgn, dcx_flags )))
258 INT type = GetClipBox( hdc, clip_rect );
260 if (flags & UPDATE_ERASE)
262 /* don't erase if the clip box is empty */
263 if (type != NULLREGION)
264 need_erase = !SendMessageW( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
266 if (!hdc_ret) USER_Driver->pReleaseDC( hwnd, hdc, TRUE );
269 if (hdc_ret) *hdc_ret = hdc;
271 if (!hdc) DeleteObject( client_rgn );
272 return need_erase;
276 /***********************************************************************
277 * erase_now
279 * Implementation of RDW_ERASENOW behavior.
281 void erase_now( HWND hwnd, UINT rdw_flags )
283 HWND child = 0;
284 HRGN hrgn;
285 BOOL need_erase = FALSE;
287 /* loop while we find a child to repaint */
288 for (;;)
290 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
292 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
293 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
294 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
296 if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
297 need_erase = send_erase( child, flags, hrgn, NULL, NULL );
299 if (!flags) break; /* nothing more to do */
300 if ((rdw_flags & RDW_NOCHILDREN) && !need_erase) break;
305 /***********************************************************************
306 * update_now
308 * Implementation of RDW_UPDATENOW behavior.
310 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
311 * SendMessage() calls. This is a comment inside DefWindowProc() source
312 * from 16-bit SDK:
314 * This message avoids lots of inter-app message traffic
315 * by switching to the other task and continuing the
316 * recursion there.
318 * wParam = flags
319 * LOWORD(lParam) = hrgnClip
320 * HIWORD(lParam) = hwndSkip (not used; always NULL)
323 static void update_now( HWND hwnd, UINT rdw_flags )
325 HWND child = 0;
327 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
328 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
330 /* loop while we find a child to repaint */
331 for (;;)
333 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
335 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
336 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
338 if (!get_update_flags( hwnd, &child, &flags )) break;
339 if (!flags) break; /* nothing more to do */
341 SendMessageW( child, WM_PAINT, 0, 0 );
342 if (rdw_flags & RDW_NOCHILDREN) break;
347 /*************************************************************************
348 * fix_caret
350 * Helper for ScrollWindowEx:
351 * If the return value is 0, no special caret handling is necessary.
352 * Otherwise the return value is the handle of the window that owns the
353 * caret. Its caret needs to be hidden during the scroll operation and
354 * moved to new_caret_pos if move_caret is TRUE.
356 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
357 UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
359 GUITHREADINFO info;
360 RECT rect, mapped_rcCaret;
361 BOOL hide_caret = FALSE;
363 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
364 if (!info.hwndCaret) return 0;
366 if (info.hwndCaret == hWnd)
368 /* Move the caret if it's (partially) in the source rectangle */
369 if (IntersectRect(&rect, scroll_rect, &info.rcCaret))
371 *move_caret = TRUE;
372 hide_caret = TRUE;
373 new_caret_pos->x = info.rcCaret.left + dx;
374 new_caret_pos->y = info.rcCaret.top + dy;
376 else
378 *move_caret = FALSE;
380 /* Hide the caret if it's in the destination rectangle */
381 rect = *scroll_rect;
382 OffsetRect(&rect, dx, dy);
383 hide_caret = IntersectRect(&rect, &rect, &info.rcCaret);
386 else
388 if ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret))
390 *move_caret = FALSE;
392 /* Hide the caret if it's in the source or in the destination
393 rectangle */
394 mapped_rcCaret = info.rcCaret;
395 MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
397 if (IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
399 hide_caret = TRUE;
401 else
403 rect = *scroll_rect;
404 OffsetRect(&rect, dx, dy);
405 hide_caret = IntersectRect(&rect, &rect, &mapped_rcCaret);
408 else
409 return 0;
412 if (hide_caret)
414 HideCaret(info.hwndCaret);
415 return info.hwndCaret;
417 else
418 return 0;
422 /***********************************************************************
423 * BeginPaint (USER32.@)
425 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
427 HWND full_handle;
428 HRGN hrgn;
429 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
431 if (!lps) return 0;
433 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
435 if (IsWindow(hwnd))
436 FIXME( "window %p belongs to other thread\n", hwnd );
437 return 0;
439 hwnd = full_handle;
441 HideCaret( hwnd );
443 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
445 lps->fErase = send_erase( hwnd, flags, hrgn, &lps->rcPaint, &lps->hdc );
447 TRACE("hdc = %p box = (%s), fErase = %d\n",
448 lps->hdc, wine_dbgstr_rect(&lps->rcPaint), lps->fErase);
450 return lps->hdc;
454 /***********************************************************************
455 * EndPaint (USER32.@)
457 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
459 if (!lps) return FALSE;
460 USER_Driver->pReleaseDC( hwnd, lps->hdc, TRUE );
461 ShowCaret( hwnd );
462 return TRUE;
466 /***********************************************************************
467 * GetDCEx (USER32.@)
469 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
471 if (!hwnd) hwnd = GetDesktopWindow();
472 else hwnd = WIN_GetFullHandle( hwnd );
474 return USER_Driver->pGetDCEx( hwnd, hrgnClip, flags );
478 /***********************************************************************
479 * GetDC (USER32.@)
481 * Get a device context.
483 * RETURNS
484 * Success: Handle to the device context
485 * Failure: NULL.
487 HDC WINAPI GetDC( HWND hwnd )
489 if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
490 return GetDCEx( hwnd, 0, DCX_USESTYLE );
494 /***********************************************************************
495 * GetWindowDC (USER32.@)
497 HDC WINAPI GetWindowDC( HWND hwnd )
499 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
503 /***********************************************************************
504 * ReleaseDC (USER32.@)
506 * Release a device context.
508 * RETURNS
509 * Success: Non-zero. Resources used by hdc are released.
510 * Failure: 0.
512 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
514 return USER_Driver->pReleaseDC( hwnd, hdc, FALSE );
518 /**********************************************************************
519 * WindowFromDC (USER32.@)
521 HWND WINAPI WindowFromDC( HDC hDC )
523 return USER_Driver->pWindowFromDC( hDC );
527 /***********************************************************************
528 * LockWindowUpdate (USER32.@)
530 * Enables or disables painting in the chosen window.
532 * PARAMS
533 * hwnd [I] handle to a window.
535 * RETURNS
536 * If successful, returns nonzero value. Otherwise,
537 * returns 0.
539 * NOTES
540 * You can lock only one window at a time.
542 BOOL WINAPI LockWindowUpdate( HWND hwnd )
544 static HWND lockedWnd;
546 /* This function is fully implemented by the following patch:
548 * http://www.winehq.org/hypermail/wine-patches/2004/01/0142.html
550 * but in order to work properly, it needs the ability to invalidate
551 * DCEs in other processes when the lock window is changed, which
552 * isn't possible yet.
553 * -mike
556 FIXME("(%p), partial stub!\n",hwnd);
558 USER_Lock();
559 if (lockedWnd)
561 if (!hwnd)
563 /* Unlock lockedWnd */
564 /* FIXME: Do something */
566 else
568 /* Attempted to lock a second window */
569 /* Return FALSE and do nothing */
570 USER_Unlock();
571 return FALSE;
574 lockedWnd = hwnd;
575 USER_Unlock();
576 return TRUE;
580 /***********************************************************************
581 * RedrawWindow (USER32.@)
583 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
585 static const RECT empty;
586 BOOL ret;
588 if (!hwnd) hwnd = GetDesktopWindow();
590 if (TRACE_ON(win))
592 if (hrgn)
594 RECT r;
595 GetRgnBox( hrgn, &r );
596 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
598 else if (rect)
599 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
600 else
601 TRACE( "%p whole window ", hwnd );
603 dump_rdw_flags(flags);
606 /* process pending expose events before painting */
607 if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
609 if (rect && !hrgn)
611 if (IsRectEmpty( rect )) rect = &empty;
612 ret = redraw_window_rects( hwnd, flags, rect, 1 );
614 else if (!hrgn)
616 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
618 else /* need to build a list of the region rectangles */
620 DWORD size;
621 RGNDATA *data = NULL;
623 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
624 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
625 GetRegionData( hrgn, size, data );
626 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
627 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
628 else
629 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
630 HeapFree( GetProcessHeap(), 0, data );
633 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
634 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
636 return ret;
640 /***********************************************************************
641 * UpdateWindow (USER32.@)
643 BOOL WINAPI UpdateWindow( HWND hwnd )
645 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
649 /***********************************************************************
650 * InvalidateRgn (USER32.@)
652 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
654 if (!hwnd)
656 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
657 return FALSE;
660 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
664 /***********************************************************************
665 * InvalidateRect (USER32.@)
667 * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
668 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
670 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
672 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
674 if (!hwnd)
676 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
677 rect = NULL;
680 return RedrawWindow( hwnd, rect, 0, flags );
684 /***********************************************************************
685 * ValidateRgn (USER32.@)
687 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
689 if (!hwnd)
691 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
692 return FALSE;
695 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
699 /***********************************************************************
700 * ValidateRect (USER32.@)
702 * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
703 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
705 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
707 UINT flags = RDW_VALIDATE;
709 if (!hwnd)
711 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
712 rect = NULL;
715 return RedrawWindow( hwnd, rect, 0, flags );
719 /***********************************************************************
720 * GetUpdateRgn (USER32.@)
722 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
724 INT retval = ERROR;
725 UINT flags = UPDATE_NOCHILDREN;
726 HRGN update_rgn;
728 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
730 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
732 POINT offset;
734 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
735 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
737 flags = UPDATE_DELAYED_ERASE;
738 get_update_flags( hwnd, NULL, &flags );
740 /* map region to client coordinates */
741 offset.x = offset.y = 0;
742 ScreenToClient( hwnd, &offset );
743 OffsetRgn( hrgn, offset.x, offset.y );
745 return retval;
749 /***********************************************************************
750 * GetUpdateRect (USER32.@)
752 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
754 UINT flags = UPDATE_NOCHILDREN;
755 HRGN update_rgn;
756 BOOL need_erase;
758 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
760 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
762 if (rect)
764 if (GetRgnBox( update_rgn, rect ) != NULLREGION)
766 HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
767 MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
768 DPtoLP( hdc, (LPPOINT)rect, 2 );
769 ReleaseDC( hwnd, hdc );
772 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
774 /* check if we still have an update region */
775 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
776 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
777 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
781 /***********************************************************************
782 * ExcludeUpdateRgn (USER32.@)
784 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
786 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
787 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
789 if (ret != ERROR)
791 POINT pt;
793 GetDCOrgEx( hdc, &pt );
794 MapWindowPoints( 0, hwnd, &pt, 1 );
795 OffsetRgn( update_rgn, -pt.x, -pt.y );
796 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
797 DeleteObject( update_rgn );
799 return ret;
803 /*************************************************************************
804 * ScrollWindowEx (USER32.@)
806 * Note: contrary to what the doc says, pixels that are scrolled from the
807 * outside of clipRect to the inside are NOT painted.
810 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
811 const RECT *rect, const RECT *clipRect,
812 HRGN hrgnUpdate, LPRECT rcUpdate,
813 UINT flags )
815 INT retVal = NULLREGION;
816 BOOL bOwnRgn = TRUE;
817 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
818 int rdw_flags;
819 HRGN hrgnTemp;
820 HRGN hrgnWinupd = 0;
821 HDC hDC;
822 RECT rc, cliprc;
823 HWND hwndCaret = NULL;
824 BOOL moveCaret = FALSE;
825 POINT newCaretPos;
827 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
828 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
829 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
830 if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
831 FIXME("some flags (%04x) are unhandled\n", flags);
833 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
834 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
836 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
837 hwnd = WIN_GetFullHandle( hwnd );
839 GetClientRect(hwnd, &rc);
840 if (rect) IntersectRect(&rc, &rc, rect);
842 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
843 else cliprc = rc;
845 if( hrgnUpdate ) bOwnRgn = FALSE;
846 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
848 newCaretPos.x = newCaretPos.y = 0;
850 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
851 DWORD dcxflags = DCX_CACHE;
852 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
854 hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
856 if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
857 if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
858 dcxflags |= DCX_PARENTCLIP;
859 if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
860 dcxflags |= DCX_CLIPCHILDREN;
861 hDC = GetDCEx( hwnd, 0, dcxflags);
862 if (hDC)
864 ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
866 ReleaseDC( hwnd, hDC );
868 if (!bUpdate)
869 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
872 /* If the windows has an update region, this must be
873 * scrolled as well. Keep a copy in hrgnWinupd
874 * to be added to hrngUpdate at the end. */
875 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
876 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
877 if (retVal != NULLREGION)
879 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
880 if( !bOwnRgn) {
881 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
882 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
884 OffsetRgn( hrgnTemp, dx, dy );
885 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
886 if( !bOwnRgn)
887 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
888 RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
890 /* Catch the case where the scolling amount exceeds the size of the
891 * original window. This generated a second update area that is the
892 * location where the original scrolled content would end up.
893 * This second region is not returned by the ScrollDC and sets
894 * ScrollWindowEx apart from just a ScrollDC.
896 * This has been verified with testing on windows.
898 if (abs(dx) > abs(rc.right - rc.left) ||
899 abs(dy) > abs(rc.bottom - rc.top))
901 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
902 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
903 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
905 if( !bOwnRgn)
906 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
908 DeleteObject( hrgnClip );
910 DeleteObject( hrgnTemp );
911 } else {
912 /* nothing was scrolled */
913 if( !bOwnRgn)
914 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
915 SetRectEmpty( rcUpdate);
918 if( flags & SW_SCROLLCHILDREN )
920 HWND *list = WIN_ListChildren( hwnd );
921 if (list)
923 int i;
924 RECT r, dummy;
925 for (i = 0; list[i]; i++)
927 GetWindowRect( list[i], &r );
928 MapWindowPoints( 0, hwnd, (POINT *)&r, 2 );
929 if (!rect || IntersectRect(&dummy, &r, rect))
930 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
931 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
932 SWP_NOREDRAW | SWP_DEFERERASE );
934 HeapFree( GetProcessHeap(), 0, list );
938 if( flags & (SW_INVALIDATE | SW_ERASE) )
939 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
940 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
942 if( hrgnWinupd) {
943 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
944 DeleteObject( hrgnWinupd);
947 if( hwndCaret ) {
948 if ( moveCaret ) SetCaretPos( newCaretPos.x, newCaretPos.y );
949 ShowCaret(hwndCaret);
952 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
954 return retVal;
958 /*************************************************************************
959 * ScrollWindow (USER32.@)
962 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
963 const RECT *rect, const RECT *clipRect )
965 return (ERROR != ScrollWindowEx( hwnd, dx, dy, rect, clipRect, 0, NULL,
966 (rect ? 0 : SW_SCROLLCHILDREN) |
967 SW_INVALIDATE | SW_ERASE ));
971 /*************************************************************************
972 * ScrollDC (USER32.@)
974 * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is
975 * wrong) hrgnUpdate is returned in device coordinates with rcUpdate in
976 * logical coordinates.
978 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *lprcScroll,
979 const RECT *lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate )
982 return USER_Driver->pScrollDC( hdc, dx, dy, lprcScroll, lprcClip, hrgnUpdate, lprcUpdate );