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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
33 #include "wine/server.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(win);
41 /***********************************************************************
44 static void dump_rdw_flags(UINT flags)
47 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
48 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
49 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
50 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
51 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
52 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
53 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
54 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
55 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
56 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
57 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
58 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
65 RDW_NOINTERNALPAINT | \
74 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
80 /***********************************************************************
83 * Return update region for a window.
85 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
94 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
96 SetLastError( ERROR_OUTOFMEMORY );
100 SERVER_START_REQ( get_update_region )
104 wine_server_set_reply( req, data->Buffer, size );
105 if (!(status = wine_server_call( req )))
107 size_t reply_size = wine_server_reply_size( reply );
108 data->rdh.dwSize = sizeof(data->rdh);
109 data->rdh.iType = RDH_RECTANGLES;
110 data->rdh.nCount = reply_size / sizeof(RECT);
111 data->rdh.nRgnSize = reply_size;
112 hrgn = ExtCreateRegion( NULL, size, data );
113 if (child) *child = reply->child;
114 *flags = reply->flags;
116 else size = reply->total_size;
119 HeapFree( GetProcessHeap(), 0, data );
120 } while (status == STATUS_BUFFER_OVERFLOW);
122 if (status) SetLastError( RtlNtStatusToDosError(status) );
127 /***********************************************************************
130 * Get only the update flags, not the update region.
132 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
136 SERVER_START_REQ( get_update_region )
139 req->flags = *flags | UPDATE_NOREGION;
140 if ((ret = !wine_server_call_err( req )))
142 if (child) *child = reply->child;
143 *flags = reply->flags;
151 /***********************************************************************
152 * redraw_window_rects
154 * Redraw part of a window.
156 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
160 SERVER_START_REQ( redraw_window )
164 wine_server_add_data( req, rects, count * sizeof(RECT) );
165 ret = !wine_server_call_err( req );
172 /***********************************************************************
175 * Send a WM_NCPAINT message if needed, and return the resulting update region.
176 * Helper for erase_now and BeginPaint.
178 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
180 HRGN whole_rgn = get_update_region( hwnd, flags, child );
183 if (child) hwnd = *child;
189 WND *win = WIN_GetPtr( hwnd );
191 if (!win || win == WND_OTHER_PROCESS)
193 DeleteObject( whole_rgn );
197 /* check if update rgn overlaps with nonclient area */
198 type = GetRgnBox( whole_rgn, &update );
199 client = win->rectClient;
200 OffsetRect( &client, -win->rectWindow.left, -win->rectWindow.top );
202 if ((*flags & UPDATE_NONCLIENT) ||
203 update.left < client.left || update.top < client.top ||
204 update.right > client.right || update.bottom > client.bottom)
206 client_rgn = CreateRectRgnIndirect( &client );
207 CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
209 /* check if update rgn contains complete nonclient area */
210 if (type == SIMPLEREGION && update.left == 0 && update.top == 0 &&
211 update.right == win->rectWindow.right - win->rectWindow.left &&
212 update.bottom == win->rectWindow.bottom - win->rectWindow.top)
214 DeleteObject( whole_rgn );
220 client_rgn = whole_rgn;
223 /* map client region to client coordinates */
224 OffsetRgn( client_rgn, win->rectWindow.left - win->rectClient.left,
225 win->rectWindow.top - win->rectClient.top );
226 WIN_ReleasePtr( win );
228 if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
230 if (*flags & UPDATE_NONCLIENT) SendMessageW( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
231 if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
238 /***********************************************************************
241 * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
242 * If a DC is requested, the region is selected into it.
243 * Helper for erase_now and BeginPaint.
245 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
246 RECT *clip_rect, HDC *hdc_ret )
248 BOOL need_erase = FALSE;
251 if (hdc_ret || (flags & UPDATE_ERASE))
253 UINT dcx_flags = DCX_INTERSECTRGN | DCX_WINDOWPAINT | 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 );
268 if (need_erase && hwnd != GetDesktopWindow()) /* FIXME: mark it as needing erase again */
269 RedrawWindow( hwnd, NULL, client_rgn, RDW_INVALIDATE | RDW_ERASE | RDW_NOCHILDREN );
270 ReleaseDC( hwnd, hdc );
274 if (hdc_ret) *hdc_ret = hdc;
280 /***********************************************************************
283 * Implementation of RDW_ERASENOW behavior.
285 static void erase_now( HWND hwnd, UINT rdw_flags )
290 /* loop while we find a child to repaint */
294 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
296 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
297 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
299 if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
300 send_erase( child, flags, hrgn, &rect, NULL );
301 DeleteObject( hrgn );
303 if (!flags) break; /* nothing more to do */
304 if (rdw_flags & RDW_NOCHILDREN) break;
309 /***********************************************************************
312 * Implementation of RDW_UPDATENOW behavior.
314 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
315 * SendMessage() calls. This is a comment inside DefWindowProc() source
318 * This message avoids lots of inter-app message traffic
319 * by switching to the other task and continuing the
323 * LOWORD(lParam) = hrgnClip
324 * HIWORD(lParam) = hwndSkip (not used; always NULL)
327 static void update_now( HWND hwnd, UINT rdw_flags )
329 HWND prev = 0, child;
331 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
332 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
334 /* loop while we find a child to repaint */
337 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
339 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
340 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
342 if (!get_update_flags( hwnd, &child, &flags )) break;
343 if (!flags) break; /* nothing more to do */
345 if (child == prev) /* same window again, didn't get repainted properly */
347 UINT erase_flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_NOCHILDREN;
351 TRACE( "%p not repainted properly, erasing\n", child );
352 if ((hrgn = send_ncpaint( child, NULL, &erase_flags )))
354 send_erase( child, erase_flags, hrgn, &rect, NULL );
355 DeleteObject( hrgn );
361 SendMessageW( child, WM_PAINT, 0, 0 );
363 if (rdw_flags & RDW_NOCHILDREN) break;
368 /***********************************************************************
369 * BeginPaint (USER32.@)
371 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
375 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
379 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
382 FIXME( "window %p belongs to other thread\n", hwnd );
389 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
391 lps->fErase = send_erase( hwnd, flags, hrgn, &lps->rcPaint, &lps->hdc );
392 if (!lps->hdc) DeleteObject( hrgn );
394 TRACE("hdc = %p box = (%ld,%ld - %ld,%ld), fErase = %d\n",
395 lps->hdc, lps->rcPaint.left, lps->rcPaint.top, lps->rcPaint.right, lps->rcPaint.bottom,
402 /***********************************************************************
403 * EndPaint (USER32.@)
405 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
407 if (!lps) return FALSE;
409 ReleaseDC( hwnd, lps->hdc );
415 /***********************************************************************
416 * RedrawWindow (USER32.@)
418 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
422 if (!hwnd) hwnd = GetDesktopWindow();
424 /* check if the window or its parents are visible/not minimized */
426 if (!WIN_IsWindowDrawable( hwnd, !(flags & RDW_FRAME) )) return TRUE;
428 /* process pending events and messages before painting */
429 if (flags & RDW_UPDATENOW)
430 MsgWaitForMultipleObjects( 0, NULL, FALSE, 0, QS_ALLINPUT );
437 GetRgnBox( hrgn, &r );
438 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
441 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
443 TRACE( "%p whole window ", hwnd );
445 dump_rdw_flags(flags);
450 ret = redraw_window_rects( hwnd, flags, rect, 1 );
454 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
456 else /* need to build a list of the region rectangles */
459 RGNDATA *data = NULL;
460 static const RECT empty;
462 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
463 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
464 GetRegionData( hrgn, size, data );
465 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
466 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
468 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
469 HeapFree( GetProcessHeap(), 0, data );
472 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
473 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
479 /***********************************************************************
480 * UpdateWindow (USER32.@)
482 BOOL WINAPI UpdateWindow( HWND hwnd )
484 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
488 /***********************************************************************
489 * InvalidateRgn (USER32.@)
491 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
493 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
497 /***********************************************************************
498 * InvalidateRect (USER32.@)
500 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
502 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
506 /***********************************************************************
507 * ValidateRgn (USER32.@)
509 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
511 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
515 /***********************************************************************
516 * ValidateRect (USER32.@)
518 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
520 return RedrawWindow( hwnd, rect, 0, RDW_VALIDATE );
524 /***********************************************************************
525 * GetUpdateRgn (USER32.@)
527 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
530 UINT flags = UPDATE_NOCHILDREN;
533 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
535 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
538 send_erase( hwnd, flags, update_rgn, &rect, NULL );
539 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
540 DeleteObject( update_rgn );
546 /***********************************************************************
547 * GetUpdateRect (USER32.@)
549 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
553 UINT flags = UPDATE_NOCHILDREN;
556 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
558 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
560 if (rect) GetRgnBox( update_rgn, rect );
562 send_erase( hwnd, flags, update_rgn, &dummy, &hdc );
565 if (rect) DPtoLP( hdc, (LPPOINT)rect, 2 );
566 ReleaseDC( hwnd, hdc );
568 else DeleteObject( update_rgn );
570 /* check if we still have an update region */
571 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
572 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
576 /***********************************************************************
577 * ExcludeUpdateRgn (USER32.@)
579 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
581 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
582 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
588 GetDCOrgEx( hdc, &pt );
589 MapWindowPoints( 0, hwnd, &pt, 1 );
590 OffsetRgn( update_rgn, -pt.x, -pt.y );
591 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
592 DeleteObject( update_rgn );