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 )
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 SendMessageW( child, WM_PAINT, 0, 0 );
347 if (rdw_flags & RDW_NOCHILDREN) break;
352 /***********************************************************************
353 * BeginPaint (USER32.@)
355 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
359 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
363 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
366 FIXME( "window %p belongs to other thread\n", hwnd );
373 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
375 lps->fErase = send_erase( hwnd, flags, hrgn, &lps->rcPaint, &lps->hdc );
376 if (!lps->hdc) DeleteObject( hrgn );
378 TRACE("hdc = %p box = (%ld,%ld - %ld,%ld), fErase = %d\n",
379 lps->hdc, lps->rcPaint.left, lps->rcPaint.top, lps->rcPaint.right, lps->rcPaint.bottom,
386 /***********************************************************************
387 * EndPaint (USER32.@)
389 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
391 if (!lps) return FALSE;
393 ReleaseDC( hwnd, lps->hdc );
399 /***********************************************************************
400 * RedrawWindow (USER32.@)
402 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
406 if (!hwnd) hwnd = GetDesktopWindow();
408 /* check if the window or its parents are visible/not minimized */
410 if (!WIN_IsWindowDrawable( hwnd, !(flags & RDW_FRAME) )) return TRUE;
412 /* process pending events and messages before painting */
413 if (flags & RDW_UPDATENOW)
414 MsgWaitForMultipleObjects( 0, NULL, FALSE, 0, QS_ALLINPUT );
421 GetRgnBox( hrgn, &r );
422 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
425 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
427 TRACE( "%p whole window ", hwnd );
429 dump_rdw_flags(flags);
434 ret = redraw_window_rects( hwnd, flags, rect, 1 );
438 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
440 else /* need to build a list of the region rectangles */
443 RGNDATA *data = NULL;
444 static const RECT empty;
446 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
447 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
448 GetRegionData( hrgn, size, data );
449 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
450 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
452 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
453 HeapFree( GetProcessHeap(), 0, data );
456 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
457 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
463 /***********************************************************************
464 * UpdateWindow (USER32.@)
466 BOOL WINAPI UpdateWindow( HWND hwnd )
468 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
472 /***********************************************************************
473 * InvalidateRgn (USER32.@)
475 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
477 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
481 /***********************************************************************
482 * InvalidateRect (USER32.@)
484 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
486 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
490 /***********************************************************************
491 * ValidateRgn (USER32.@)
493 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
495 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
499 /***********************************************************************
500 * ValidateRect (USER32.@)
502 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
504 return RedrawWindow( hwnd, rect, 0, RDW_VALIDATE );
508 /***********************************************************************
509 * GetUpdateRgn (USER32.@)
511 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
514 UINT flags = UPDATE_NOCHILDREN;
517 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
519 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
522 send_erase( hwnd, flags, update_rgn, &rect, NULL );
523 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
524 DeleteObject( update_rgn );
530 /***********************************************************************
531 * GetUpdateRect (USER32.@)
533 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
537 UINT flags = UPDATE_NOCHILDREN;
540 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
542 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
544 if (rect) GetRgnBox( update_rgn, rect );
546 send_erase( hwnd, flags, update_rgn, &dummy, &hdc );
549 if (rect) DPtoLP( hdc, (LPPOINT)rect, 2 );
550 ReleaseDC( hwnd, hdc );
552 else DeleteObject( update_rgn );
554 /* check if we still have an update region */
555 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
556 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
560 /***********************************************************************
561 * ExcludeUpdateRgn (USER32.@)
563 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
565 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
566 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
572 GetDCOrgEx( hdc, &pt );
573 MapWindowPoints( 0, hwnd, &pt, 1 );
574 OffsetRgn( update_rgn, -pt.x, -pt.y );
575 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
576 DeleteObject( update_rgn );