2 * Window painting functions
4 * Copyright 1993, 1994, 1995, 2001 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
27 #include "wine/winuser16.h"
28 #include "wine/server.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(win);
35 /***********************************************************************
38 * Add an increment (normally 1 or -1) to the current paint count of a window.
40 static void add_paint_count( HWND hwnd, int incr )
42 SERVER_START_REQ( inc_window_paint_count )
46 wine_server_call( req );
52 /***********************************************************************
55 * copy a region, doing the right thing if the source region is 0 or 1
57 static HRGN copy_rgn( HRGN hSrc )
61 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
62 CombineRgn( hrgn, hSrc, 0, RGN_COPY );
69 /***********************************************************************
72 * Return update regions for the whole window and for the client area.
74 static void get_update_regions( WND *win, HRGN *whole_rgn, HRGN *client_rgn )
76 if (win->hrgnUpdate > (HRGN)1)
80 /* check if update rgn overlaps with nonclient area */
81 GetRgnBox( win->hrgnUpdate, &update );
82 client = win->rectClient;
83 OffsetRect( &client, -win->rectWindow.left, -win->rectWindow.top );
85 if (update.left < client.left || update.top < client.top ||
86 update.right > client.right || update.bottom > client.bottom)
88 *whole_rgn = copy_rgn( win->hrgnUpdate );
89 *client_rgn = CreateRectRgnIndirect( &client );
90 if (CombineRgn( *client_rgn, *client_rgn, win->hrgnUpdate, RGN_AND ) == NULLREGION)
92 DeleteObject( *client_rgn );
99 *client_rgn = copy_rgn( win->hrgnUpdate );
104 *client_rgn = *whole_rgn = win->hrgnUpdate; /* 0 or 1 */
109 /***********************************************************************
112 * Send a WM_NCPAINT message from inside BeginPaint().
113 * Returns update region cropped to client rectangle (and in client coords),
114 * and clears window update region and internal paint flag.
116 static HRGN begin_ncpaint( HWND hwnd )
118 HRGN whole_rgn, client_rgn;
119 WND *wnd = WIN_GetPtr( hwnd );
121 if (!wnd || wnd == WND_OTHER_PROCESS) return 0;
123 TRACE("hwnd %p [%p] ncf %i\n",
124 hwnd, wnd->hrgnUpdate, wnd->flags & WIN_NEEDS_NCPAINT);
126 get_update_regions( wnd, &whole_rgn, &client_rgn );
128 if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
130 WIN_ReleasePtr( wnd );
131 SendMessageA( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
132 if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
133 /* make sure the window still exists before continuing */
134 if (!(wnd = WIN_GetPtr( hwnd )) || wnd == WND_OTHER_PROCESS)
136 if (client_rgn > (HRGN)1) DeleteObject( client_rgn );
141 if (wnd->hrgnUpdate || (wnd->flags & WIN_INTERNAL_PAINT)) add_paint_count( hwnd, -1 );
142 if (wnd->hrgnUpdate > (HRGN)1) DeleteObject( wnd->hrgnUpdate );
144 wnd->flags &= ~(WIN_INTERNAL_PAINT | WIN_NEEDS_NCPAINT | WIN_NEEDS_BEGINPAINT);
145 if (client_rgn > (HRGN)1) OffsetRgn( client_rgn, wnd->rectWindow.left - wnd->rectClient.left,
146 wnd->rectWindow.top - wnd->rectClient.top );
147 WIN_ReleasePtr( wnd );
152 /***********************************************************************
153 * BeginPaint (USER32.@)
155 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
160 RECT clipRect, clientRect;
166 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
169 FIXME( "window %p belongs to other thread\n", hwnd );
174 /* send WM_NCPAINT and retrieve update region */
175 hrgnUpdate = begin_ncpaint( hwnd );
176 if (!hrgnUpdate && !IsWindow( hwnd )) return 0;
180 bIcon = (IsIconic(hwnd) && GetClassLongA(hwnd, GCL_HICON));
182 dcx_flags = DCX_INTERSECTRGN | DCX_WINDOWPAINT | DCX_USESTYLE;
183 if (bIcon) dcx_flags |= DCX_WINDOW;
185 if (GetClassLongA( hwnd, GCL_STYLE ) & CS_PARENTDC)
187 /* Don't clip the output to the update region for CS_PARENTDC window */
188 if (hrgnUpdate > (HRGN)1) DeleteObject( hrgnUpdate );
190 dcx_flags &= ~DCX_INTERSECTRGN;
194 if (!hrgnUpdate) /* empty region, clip everything */
196 hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
198 else if (hrgnUpdate == (HRGN)1) /* whole client area, don't clip */
201 dcx_flags &= ~DCX_INTERSECTRGN;
204 lps->hdc = GetDCEx( hwnd, hrgnUpdate, dcx_flags );
205 /* ReleaseDC() in EndPaint() will delete the region */
209 WARN("GetDCEx() failed in BeginPaint(), hwnd=%p\n", hwnd);
210 DeleteObject( hrgnUpdate );
214 /* It is possible that the clip box is bigger than the window itself,
215 if CS_PARENTDC flag is set. Windows never return a paint rect bigger
216 than the window itself, so we need to intersect the cliprect with
218 GetClientRect( hwnd, &clientRect );
220 GetClipBox( lps->hdc, &clipRect );
221 LPtoDP(lps->hdc, (LPPOINT)&clipRect, 2); /* GetClipBox returns LP */
223 IntersectRect(&lps->rcPaint, &clientRect, &clipRect);
224 DPtoLP(lps->hdc, (LPPOINT)&lps->rcPaint, 2); /* we must return LP */
226 TRACE("hdc = %p box = (%ld,%ld - %ld,%ld)\n",
227 lps->hdc, lps->rcPaint.left, lps->rcPaint.top, lps->rcPaint.right, lps->rcPaint.bottom );
229 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
230 lps->fErase = (wndPtr->flags & WIN_NEEDS_ERASEBKGND) != 0;
231 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
232 WIN_ReleasePtr( wndPtr );
235 lps->fErase = !SendMessageA( hwnd, bIcon ? WM_ICONERASEBKGND : WM_ERASEBKGND,
236 (WPARAM)lps->hdc, 0 );
238 TRACE("hdc = %p box = (%ld,%ld - %ld,%ld), fErase = %d\n",
239 lps->hdc, lps->rcPaint.left, lps->rcPaint.top, lps->rcPaint.right, lps->rcPaint.bottom,
246 /***********************************************************************
247 * EndPaint (USER32.@)
249 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
251 if (!lps) return FALSE;
253 ReleaseDC( hwnd, lps->hdc );