GetUpdateRect can be called with a NULL rect.
[wine/testsucceed.git] / dlls / user / painting.c
blob3aeee06227c256ea5e2860dc11c554f763ea9bbe
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "ntstatus.h"
32 #include "winuser.h"
33 #include "wine/server.h"
34 #include "win.h"
35 #include "dce.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(win);
41 /***********************************************************************
42 * dump_rdw_flags
44 static void dump_rdw_flags(UINT flags)
46 TRACE("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");
60 #define RDW_FLAGS \
61 (RDW_INVALIDATE | \
62 RDW_INTERNALPAINT | \
63 RDW_ERASE | \
64 RDW_VALIDATE | \
65 RDW_NOINTERNALPAINT | \
66 RDW_NOERASE | \
67 RDW_NOCHILDREN | \
68 RDW_ALLCHILDREN | \
69 RDW_UPDATENOW | \
70 RDW_ERASENOW | \
71 RDW_FRAME | \
72 RDW_NOFRAME)
74 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
75 TRACE("\n");
76 #undef RDW_FLAGS
80 /***********************************************************************
81 * get_update_region
83 * Return update region for a window.
85 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
87 HRGN hrgn = 0;
88 NTSTATUS status;
89 RGNDATA *data;
90 size_t size = 256;
94 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
96 SetLastError( ERROR_OUTOFMEMORY );
97 return 0;
100 SERVER_START_REQ( get_update_region )
102 req->window = hwnd;
103 req->flags = *flags;
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;
118 SERVER_END_REQ;
119 HeapFree( GetProcessHeap(), 0, data );
120 } while (status == STATUS_BUFFER_OVERFLOW);
122 if (status) SetLastError( RtlNtStatusToDosError(status) );
123 return hrgn;
127 /***********************************************************************
128 * get_update_flags
130 * Get only the update flags, not the update region.
132 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
134 BOOL ret;
136 SERVER_START_REQ( get_update_region )
138 req->window = hwnd;
139 req->flags = *flags | UPDATE_NOREGION;
140 if ((ret = !wine_server_call_err( req )))
142 if (child) *child = reply->child;
143 *flags = reply->flags;
146 SERVER_END_REQ;
147 return ret;
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 )
158 BOOL ret;
160 SERVER_START_REQ( redraw_window )
162 req->window = hwnd;
163 req->flags = flags;
164 wine_server_add_data( req, rects, count * sizeof(RECT) );
165 ret = !wine_server_call_err( req );
167 SERVER_END_REQ;
168 return ret;
172 /***********************************************************************
173 * send_ncpaint
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 );
181 HRGN client_rgn = 0;
183 if (child) hwnd = *child;
185 if (whole_rgn)
187 RECT client, update;
188 INT type;
189 WND *win = WIN_GetPtr( hwnd );
191 if (!win || win == WND_OTHER_PROCESS)
193 DeleteObject( whole_rgn );
194 return 0;
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 );
215 whole_rgn = (HRGN)1;
218 else
220 client_rgn = whole_rgn;
221 whole_rgn = 0;
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 );
234 return client_rgn;
238 /***********************************************************************
239 * send_erase
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;
249 HDC hdc;
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 );
266 if (!hdc_ret)
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;
276 return need_erase;
280 /***********************************************************************
281 * erase_now
283 * Implementation of RDW_ERASENOW behavior.
285 void erase_now( HWND hwnd, UINT rdw_flags )
287 HWND child;
288 HRGN hrgn;
290 /* loop while we find a child to repaint */
291 for (;;)
293 RECT rect;
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 /***********************************************************************
310 * update_now
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
316 * from 16-bit SDK:
318 * This message avoids lots of inter-app message traffic
319 * by switching to the other task and continuing the
320 * recursion there.
322 * wParam = flags
323 * LOWORD(lParam) = hrgnClip
324 * HIWORD(lParam) = hwndSkip (not used; always NULL)
327 void update_now( HWND hwnd, UINT rdw_flags )
329 HWND child;
331 /* loop while we find a child to repaint */
332 for (;;)
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 )
354 HWND full_handle;
355 HRGN hrgn;
356 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
358 if (!lps) return 0;
360 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
362 if (IsWindow(hwnd))
363 FIXME( "window %p belongs to other thread\n", hwnd );
364 return 0;
366 hwnd = full_handle;
368 HideCaret( 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,
377 lps->fErase);
379 return lps->hdc;
383 /***********************************************************************
384 * EndPaint (USER32.@)
386 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
388 if (!lps) return FALSE;
390 ReleaseDC( hwnd, lps->hdc );
391 ShowCaret( hwnd );
392 return TRUE;
396 /***********************************************************************
397 * RedrawWindow (USER32.@)
399 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
401 BOOL ret;
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 );
413 if (TRACE_ON(win))
415 if (hrgn)
417 RECT r;
418 GetRgnBox( hrgn, &r );
419 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
421 else if (rect)
422 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
423 else
424 TRACE( "%p whole window ", hwnd );
426 dump_rdw_flags(flags);
429 if (rect && !hrgn)
431 ret = redraw_window_rects( hwnd, flags, rect, 1 );
433 else if (!hrgn)
435 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
437 else /* need to build a list of the region rectangles */
439 DWORD size;
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 );
452 return ret;
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 )
506 INT retval = ERROR;
507 UINT flags = UPDATE_NOCHILDREN;
508 HRGN update_rgn;
510 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
512 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
514 RECT rect;
515 send_erase( hwnd, flags, update_rgn, &rect, NULL );
516 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
517 DeleteObject( update_rgn );
519 return retval;
523 /***********************************************************************
524 * GetUpdateRect (USER32.@)
526 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
528 HDC hdc;
529 RECT dummy;
530 UINT flags = UPDATE_NOCHILDREN;
531 HRGN update_rgn;
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 );
540 if (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 );
561 if (ret != ERROR)
563 /* do ugly coordinate translations in dce.c */
565 ret = DCE_ExcludeRgn( hdc, hwnd, update_rgn );
566 DeleteObject( update_rgn );
568 return ret;