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) /* 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 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 void update_now( HWND hwnd, UINT rdw_flags )
331 /* loop while we find a child to repaint */
334 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
336 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
337 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
339 if (!get_update_flags( hwnd, &child, &flags )) break;
340 if (!flags) break; /* nothing more to do */
342 SendMessageW( child, WM_PAINT, 0, 0 );
344 if (rdw_flags & RDW_NOCHILDREN) break;
349 /***********************************************************************
350 * BeginPaint (USER32.@)
352 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
356 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
360 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
363 FIXME( "window %p belongs to other thread\n", hwnd );
370 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
372 lps->fErase = send_erase( hwnd, flags, hrgn, &lps->rcPaint, &lps->hdc );
373 if (!lps->hdc) DeleteObject( hrgn );
375 TRACE("hdc = %p box = (%ld,%ld - %ld,%ld), fErase = %d\n",
376 lps->hdc, lps->rcPaint.left, lps->rcPaint.top, lps->rcPaint.right, lps->rcPaint.bottom,
383 /***********************************************************************
384 * EndPaint (USER32.@)
386 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
388 if (!lps) return FALSE;
390 ReleaseDC( hwnd, lps->hdc );
396 /***********************************************************************
397 * RedrawWindow (USER32.@)
399 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
403 if (!hwnd) hwnd = GetDesktopWindow();
405 /* check if the window or its parents are visible/not minimized */
407 if (!WIN_IsWindowDrawable( hwnd, !(flags & RDW_FRAME) )) return TRUE;
409 /* process pending events and messages before painting */
410 if (flags & RDW_UPDATENOW)
411 MsgWaitForMultipleObjects( 0, NULL, FALSE, 0, QS_ALLINPUT );
418 GetRgnBox( hrgn, &r );
419 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
422 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
424 TRACE( "%p whole window ", hwnd );
426 dump_rdw_flags(flags);
431 ret = redraw_window_rects( hwnd, flags, rect, 1 );
435 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
437 else /* need to build a list of the region rectangles */
440 RGNDATA *data = NULL;
442 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
443 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
444 GetRegionData( hrgn, size, data );
445 ret = redraw_window_rects( hwnd, flags, (RECT *)data->Buffer, data->rdh.nCount );
446 HeapFree( GetProcessHeap(), 0, data );
449 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
450 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
456 /***********************************************************************
457 * UpdateWindow (USER32.@)
459 BOOL WINAPI UpdateWindow( HWND hwnd )
461 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
465 /***********************************************************************
466 * InvalidateRgn (USER32.@)
468 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
470 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
474 /***********************************************************************
475 * InvalidateRect (USER32.@)
477 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
479 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
483 /***********************************************************************
484 * ValidateRgn (USER32.@)
486 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
488 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
492 /***********************************************************************
493 * ValidateRect (USER32.@)
495 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
497 return RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
501 /***********************************************************************
502 * GetUpdateRgn (USER32.@)
504 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
507 UINT flags = UPDATE_NOCHILDREN;
510 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
512 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
515 send_erase( hwnd, flags, update_rgn, &rect, NULL );
516 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
517 DeleteObject( update_rgn );
523 /***********************************************************************
524 * GetUpdateRect (USER32.@)
526 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
530 UINT flags = UPDATE_NOCHILDREN;
533 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
535 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
537 if (rect) GetRgnBox( update_rgn, rect );
539 send_erase( hwnd, flags, update_rgn, &dummy, &hdc );
542 if (rect) DPtoLP( hdc, (LPPOINT)rect, 2 );
543 ReleaseDC( hwnd, hdc );
545 else DeleteObject( update_rgn );
547 /* check if we still have an update region */
548 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
549 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
553 /***********************************************************************
554 * ExcludeUpdateRgn (USER32.@)
556 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
558 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
559 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
563 /* do ugly coordinate translations in dce.c */
565 ret = DCE_ExcludeRgn( hdc, hwnd, update_rgn );
566 DeleteObject( update_rgn );