[NtUser]
[reactos.git] / reactos / win32ss / user / ntuser / painting.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32k subsystem
4 * PURPOSE: Window painting function
5 * FILE: win32ss/user/ntuser/painting.c
6 * PROGRAMER: Filip Navara (xnavara@volny.cz)
7 */
8
9 #include <win32k.h>
10 DBG_DEFAULT_CHANNEL(UserPainting);
11
12 /* PRIVATE FUNCTIONS **********************************************************/
13
14 /**
15 * @name IntIntersectWithParents
16 *
17 * Intersect window rectangle with all parent client rectangles.
18 *
19 * @param Child
20 * Pointer to child window to start intersecting from.
21 * @param WindowRect
22 * Pointer to rectangle that we want to intersect in screen
23 * coordinates on input and intersected rectangle on output (if TRUE
24 * is returned).
25 *
26 * @return
27 * If any parent is minimized or invisible or the resulting rectangle
28 * is empty then FALSE is returned. Otherwise TRUE is returned.
29 */
30
31 BOOL FASTCALL
32 IntIntersectWithParents(PWND Child, RECTL *WindowRect)
33 {
34 PWND ParentWnd;
35
36 if (Child->ExStyle & WS_EX_REDIRECTED)
37 return TRUE;
38
39 ParentWnd = Child->spwndParent;
40 while (ParentWnd != NULL)
41 {
42 if (!(ParentWnd->style & WS_VISIBLE) ||
43 (ParentWnd->style & WS_MINIMIZE) ||
44 !RECTL_bIntersectRect(WindowRect, WindowRect, &ParentWnd->rcClient) )
45 {
46 return FALSE;
47 }
48
49 if (ParentWnd->ExStyle & WS_EX_REDIRECTED)
50 return TRUE;
51
52 ParentWnd = ParentWnd->spwndParent;
53 }
54
55 return TRUE;
56 }
57
58 BOOL FASTCALL
59 IntValidateParents(PWND Child, BOOL Recurse)
60 {
61 RECTL ParentRect, Rect;
62 BOOL Start, Ret = TRUE;
63 PWND ParentWnd = Child;
64 PREGION Rgn = NULL;
65
66 if (ParentWnd->style & WS_CHILD)
67 {
68 do
69 ParentWnd = ParentWnd->spwndParent;
70 while (ParentWnd->style & WS_CHILD);
71 }
72
73 // No pending nonclient paints.
74 if (!(ParentWnd->state & WNDS_SYNCPAINTPENDING)) Recurse = FALSE;
75
76 Start = TRUE;
77 ParentWnd = Child->spwndParent;
78 while (ParentWnd)
79 {
80 if (ParentWnd->style & WS_CLIPCHILDREN)
81 break;
82
83 if (ParentWnd->hrgnUpdate != 0)
84 {
85 if (Recurse)
86 {
87 Ret = FALSE;
88 break;
89 }
90 // Start with child clipping.
91 if (Start)
92 {
93 Start = FALSE;
94
95 Rect = Child->rcWindow;
96
97 if (!IntIntersectWithParents(Child, &Rect)) break;
98
99 Rgn = IntSysCreateRectpRgnIndirect(&Rect);
100
101 if (Child->hrgnClip)
102 {
103 PREGION RgnClip = REGION_LockRgn(Child->hrgnClip);
104 IntGdiCombineRgn(Rgn, Rgn, RgnClip, RGN_AND);
105 REGION_UnlockRgn(RgnClip);
106 }
107 }
108
109 ParentRect = ParentWnd->rcWindow;
110
111 if (!IntIntersectWithParents(ParentWnd, &ParentRect)) break;
112
113 IntInvalidateWindows( ParentWnd,
114 Rgn,
115 RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOUPDATEDIRTY);
116 }
117 ParentWnd = ParentWnd->spwndParent;
118 }
119
120 if (Rgn) REGION_Delete(Rgn);
121
122 return Ret;
123 }
124
125 /*
126 Synchronize painting to the top-level windows of other threads.
127 */
128 VOID FASTCALL
129 IntSendSyncPaint(PWND Wnd, ULONG Flags)
130 {
131 PTHREADINFO ptiCur, ptiWnd;
132 PUSER_SENT_MESSAGE Message;
133 PLIST_ENTRY Entry;
134 BOOL bSend = TRUE;
135
136 ptiWnd = Wnd->head.pti;
137 ptiCur = PsGetCurrentThreadWin32Thread();
138 /*
139 Not the current thread, Wnd is in send Nonclient paint also in send erase background and it is visiable.
140 */
141 if ( Wnd->head.pti != ptiCur &&
142 Wnd->state & WNDS_SENDNCPAINT &&
143 Wnd->state & WNDS_SENDERASEBACKGROUND &&
144 Wnd->style & WS_VISIBLE)
145 {
146 // For testing, if you see this, break out the Champagne and have a party!
147 TRACE("SendSyncPaint Wnd in State!\n");
148 if (!IsListEmpty(&ptiWnd->SentMessagesListHead))
149 {
150 // Scan sent queue messages to see if we received sync paint messages.
151 Entry = ptiWnd->SentMessagesListHead.Flink;
152 Message = CONTAINING_RECORD(Entry, USER_SENT_MESSAGE, ListEntry);
153 do
154 {
155 ERR("LOOP it\n");
156 if (Message->Msg.message == WM_SYNCPAINT &&
157 Message->Msg.hwnd == UserHMGetHandle(Wnd))
158 { // Already received so exit out.
159 ERR("SendSyncPaint Found one in the Sent Msg Queue!\n");
160 bSend = FALSE;
161 break;
162 }
163 Entry = Message->ListEntry.Flink;
164 Message = CONTAINING_RECORD(Entry, USER_SENT_MESSAGE, ListEntry);
165 }
166 while (Entry != &ptiWnd->SentMessagesListHead);
167 }
168 if (bSend)
169 {
170 TRACE("Sending WM_SYNCPAINT\n");
171 // This message has no parameters. But it does! Pass Flags along.
172 co_IntSendMessageNoWait(UserHMGetHandle(Wnd), WM_SYNCPAINT, Flags, 0);
173 Wnd->state |= WNDS_SYNCPAINTPENDING;
174 }
175 }
176
177 // Send to all the children if this is the desktop window.
178 if ( Wnd == UserGetDesktopWindow() )
179 {
180 if ( Flags & RDW_ALLCHILDREN ||
181 ( !(Flags & RDW_NOCHILDREN) && Wnd->style & WS_CLIPCHILDREN))
182 {
183 PWND spwndChild = Wnd->spwndChild;
184 while(spwndChild)
185 {
186 if ( spwndChild->style & WS_CHILD &&
187 spwndChild->head.pti != ptiCur)
188 {
189 spwndChild = spwndChild->spwndNext;
190 continue;
191 }
192 IntSendSyncPaint( spwndChild, Flags );
193 spwndChild = spwndChild->spwndNext;
194 }
195 }
196 }
197 }
198
199 /*
200 * @name IntCalcWindowRgn
201 *
202 * Get a window or client region.
203 */
204
205 HRGN FASTCALL
206 IntCalcWindowRgn(PWND Wnd, BOOL Client)
207 {
208 HRGN hRgnWindow;
209
210 if (Client)
211 {
212 hRgnWindow = NtGdiCreateRectRgn(
213 Wnd->rcClient.left,
214 Wnd->rcClient.top,
215 Wnd->rcClient.right,
216 Wnd->rcClient.bottom);
217 }
218 else
219 {
220 hRgnWindow = NtGdiCreateRectRgn(
221 Wnd->rcWindow.left,
222 Wnd->rcWindow.top,
223 Wnd->rcWindow.right,
224 Wnd->rcWindow.bottom);
225 }
226
227 if (Wnd->hrgnClip != NULL && !(Wnd->style & WS_MINIMIZE))
228 {
229 NtGdiOffsetRgn(hRgnWindow,
230 -Wnd->rcWindow.left,
231 -Wnd->rcWindow.top);
232 NtGdiCombineRgn(hRgnWindow, hRgnWindow, Wnd->hrgnClip, RGN_AND);
233 NtGdiOffsetRgn(hRgnWindow,
234 Wnd->rcWindow.left,
235 Wnd->rcWindow.top);
236 }
237
238 return hRgnWindow;
239 }
240
241 /*
242 * @name IntGetNCUpdateRgn
243 *
244 * Get non-client update region of a window and optionally validate it.
245 *
246 * @param Window
247 * Pointer to window to get the NC update region from.
248 * @param Validate
249 * Set to TRUE to force validating the NC update region.
250 *
251 * @return
252 * Handle to NC update region. The caller is responsible for deleting
253 * it.
254 */
255
256 HRGN FASTCALL
257 IntGetNCUpdateRgn(PWND Window, BOOL Validate)
258 {
259 HRGN hRgnNonClient;
260 HRGN hRgnWindow;
261 UINT RgnType, NcType;
262 RECT update;
263
264 if (Window->hrgnUpdate != NULL &&
265 Window->hrgnUpdate != HRGN_WINDOW)
266 {
267 hRgnNonClient = IntCalcWindowRgn(Window, FALSE);
268
269 /*
270 * If region creation fails it's safe to fallback to whole
271 * window region.
272 */
273 if (hRgnNonClient == NULL)
274 {
275 return HRGN_WINDOW;
276 }
277
278 hRgnWindow = IntCalcWindowRgn(Window, TRUE);
279 if (hRgnWindow == NULL)
280 {
281 GreDeleteObject(hRgnNonClient);
282 return HRGN_WINDOW;
283 }
284
285 NcType = IntGdiGetRgnBox(hRgnNonClient, &update);
286
287 RgnType = NtGdiCombineRgn(hRgnNonClient, hRgnNonClient, hRgnWindow, RGN_DIFF);
288
289 if (RgnType == ERROR)
290 {
291 GreDeleteObject(hRgnWindow);
292 GreDeleteObject(hRgnNonClient);
293 return HRGN_WINDOW;
294 }
295 else if (RgnType == NULLREGION)
296 {
297 GreDeleteObject(hRgnWindow);
298 GreDeleteObject(hRgnNonClient);
299 Window->state &= ~WNDS_UPDATEDIRTY;
300 return NULL;
301 }
302
303 /*
304 * Remove the nonclient region from the standard update region if
305 * we were asked for it.
306 */
307
308 if (Validate)
309 {
310 if (NtGdiCombineRgn(Window->hrgnUpdate, Window->hrgnUpdate, hRgnWindow, RGN_AND) == NULLREGION)
311 {
312 IntGdiSetRegionOwner(Window->hrgnUpdate, GDI_OBJ_HMGR_POWNED);
313 GreDeleteObject(Window->hrgnUpdate);
314 Window->state &= ~WNDS_UPDATEDIRTY;
315 Window->hrgnUpdate = NULL;
316 if (!(Window->state & WNDS_INTERNALPAINT))
317 MsqDecPaintCountQueue(Window->head.pti);
318 }
319 }
320
321 /* check if update rgn contains complete nonclient area */
322 if (NcType == SIMPLEREGION)
323 {
324 RECT window;
325 IntGetWindowRect( Window, &window );
326
327 if (IntEqualRect( &window, &update ))
328 {
329 GreDeleteObject(hRgnNonClient);
330 hRgnNonClient = HRGN_WINDOW;
331 }
332 }
333
334 GreDeleteObject(hRgnWindow);
335
336 return hRgnNonClient;
337 }
338 else
339 {
340 return Window->hrgnUpdate;
341 }
342 }
343
344 VOID FASTCALL
345 IntSendNCPaint(PWND pWnd, HRGN hRgn)
346 {
347 pWnd->state &= ~WNDS_SENDNCPAINT;
348
349 if ( pWnd == GetW32ThreadInfo()->MessageQueue->spwndActive &&
350 !(pWnd->state & WNDS_ACTIVEFRAME))
351 {
352 pWnd->state |= WNDS_ACTIVEFRAME;
353 pWnd->state &= ~WNDS_NONCPAINT;
354 hRgn = HRGN_WINDOW;
355 }
356
357 if (pWnd->state2 & WNDS2_FORCEFULLNCPAINTCLIPRGN)
358 {
359 pWnd->state2 &= ~WNDS2_FORCEFULLNCPAINTCLIPRGN;
360 hRgn = HRGN_WINDOW;
361 }
362
363 if (hRgn) co_IntSendMessage(UserHMGetHandle(pWnd), WM_NCPAINT, (WPARAM)hRgn, 0);
364 }
365
366 VOID FASTCALL
367 IntSendChildNCPaint(PWND pWnd)
368 {
369 pWnd = pWnd->spwndChild;
370 while(pWnd)
371 {
372 if (pWnd->hrgnUpdate == NULL && pWnd->state & WNDS_SENDNCPAINT)
373 {
374 USER_REFERENCE_ENTRY Ref;
375 UserRefObjectCo(pWnd, &Ref);
376 IntSendNCPaint(pWnd, HRGN_WINDOW);
377 UserDerefObjectCo(pWnd);
378 }
379 pWnd = pWnd->spwndNext;
380 }
381 }
382
383 /*
384 * IntPaintWindows
385 *
386 * Internal function used by IntRedrawWindow.
387 */
388
389 VOID FASTCALL
390 co_IntPaintWindows(PWND Wnd, ULONG Flags, BOOL Recurse)
391 {
392 HDC hDC;
393 HWND hWnd = Wnd->head.h;
394 HRGN TempRegion = NULL;
395
396 Wnd->state &= ~WNDS_PAINTNOTPROCESSED;
397
398 if (Wnd->state & WNDS_SENDNCPAINT ||
399 Wnd->state & WNDS_SENDERASEBACKGROUND)
400 {
401 if (!(Wnd->style & WS_VISIBLE))
402 {
403 Wnd->state &= ~(WNDS_SENDNCPAINT|WNDS_SENDERASEBACKGROUND|WNDS_ERASEBACKGROUND);
404 return;
405 }
406 else
407 {
408 if (Wnd->hrgnUpdate == NULL)
409 {
410 Wnd->state &= ~(WNDS_SENDERASEBACKGROUND|WNDS_ERASEBACKGROUND);
411 }
412
413 if (Wnd->head.pti == PsGetCurrentThreadWin32Thread())
414 {
415 if (Wnd->state & WNDS_SENDNCPAINT)
416 {
417 TempRegion = IntGetNCUpdateRgn(Wnd, TRUE);
418
419 IntSendNCPaint(Wnd, TempRegion);
420
421 if (TempRegion > HRGN_WINDOW && GreIsHandleValid(TempRegion))
422 {
423 /* NOTE: The region can already be deleted! */
424 GreDeleteObject(TempRegion);
425 }
426 }
427
428 if (Wnd->state & WNDS_SENDERASEBACKGROUND)
429 {
430 PTHREADINFO pti = PsGetCurrentThreadWin32Thread();
431 if (Wnd->hrgnUpdate)
432 {
433 hDC = UserGetDCEx( Wnd,
434 Wnd->hrgnUpdate,
435 DCX_CACHE|DCX_USESTYLE|DCX_INTERSECTRGN|DCX_KEEPCLIPRGN);
436
437 if (Wnd->head.pti->ppi != pti->ppi)
438 {
439 ERR("Sending DC to another Process!!!\n");
440 }
441
442 Wnd->state &= ~(WNDS_SENDERASEBACKGROUND|WNDS_ERASEBACKGROUND);
443 // Kill the loop, so Clear before we send.
444 if (!co_IntSendMessage(hWnd, WM_ERASEBKGND, (WPARAM)hDC, 0))
445 {
446 Wnd->state |= (WNDS_SENDERASEBACKGROUND|WNDS_ERASEBACKGROUND);
447 }
448 UserReleaseDC(Wnd, hDC, FALSE);
449 }
450 }
451 }
452
453 }
454 }
455
456 /*
457 * Check that the window is still valid at this point
458 */
459 if (!IntIsWindow(hWnd))
460 {
461 return;
462 }
463
464 /*
465 * Paint child windows.
466 */
467
468 if (!(Flags & RDW_NOCHILDREN) &&
469 !(Wnd->style & WS_MINIMIZE) &&
470 ( Flags & RDW_ALLCHILDREN ||
471 (Flags & RDW_CLIPCHILDREN && Wnd->style & WS_CLIPCHILDREN) ) )
472 {
473 HWND *List, *phWnd;
474 PTHREADINFO pti = PsGetCurrentThreadWin32Thread();
475
476 if ((List = IntWinListChildren(Wnd)))
477 {
478 for (phWnd = List; *phWnd; ++phWnd)
479 {
480 if ((Wnd = UserGetWindowObject(*phWnd)) == NULL)
481 continue;
482
483 if (Wnd->head.pti != pti && Wnd->style & WS_CHILD)
484 continue;
485
486 if (Wnd->style & WS_VISIBLE)
487 {
488 USER_REFERENCE_ENTRY Ref;
489 UserRefObjectCo(Wnd, &Ref);
490 co_IntPaintWindows(Wnd, Flags, TRUE);
491 UserDerefObjectCo(Wnd);
492 }
493 }
494 ExFreePoolWithTag(List, USERTAG_WINDOWLIST);
495 }
496 }
497 }
498
499 /*
500 * IntUpdateWindows
501 *
502 * Internal function used by IntRedrawWindow, simplecall.
503 */
504
505 VOID FASTCALL
506 co_IntUpdateWindows(PWND Wnd, ULONG Flags, BOOL Recurse)
507 {
508 HWND hWnd = Wnd->head.h;
509
510 if ( Wnd->hrgnUpdate != NULL || Wnd->state & WNDS_INTERNALPAINT )
511 {
512 if (Wnd->hrgnUpdate)
513 {
514 if (!IntValidateParents(Wnd, Recurse))
515 {
516 return;
517 }
518 }
519
520 if (Wnd->state & WNDS_INTERNALPAINT)
521 {
522 Wnd->state &= ~WNDS_INTERNALPAINT;
523
524 if (Wnd->hrgnUpdate == NULL)
525 MsqDecPaintCountQueue(Wnd->head.pti);
526 }
527
528 Wnd->state |= WNDS_PAINTNOTPROCESSED;
529 Wnd->state &= ~WNDS_UPDATEDIRTY;
530
531 Wnd->state2 |= WNDS2_WMPAINTSENT;
532 co_IntSendMessage(hWnd, WM_PAINT, 0, 0);
533
534 if (Wnd->state & WNDS_PAINTNOTPROCESSED)
535 {
536 USER_REFERENCE_ENTRY Ref;
537 UserRefObjectCo(Wnd, &Ref);
538 co_IntPaintWindows(Wnd, RDW_NOCHILDREN, FALSE);
539 UserDerefObjectCo(Wnd);
540 }
541 }
542
543 // Force flags as a toggle. Fixes msg:test_paint_messages:WmChildPaintNc.
544 Flags = (Flags & RDW_NOCHILDREN) ? RDW_NOCHILDREN : RDW_ALLCHILDREN; // All children is the default.
545
546 /*
547 * Update child windows.
548 */
549
550 if (!(Flags & RDW_NOCHILDREN) &&
551 Flags & RDW_ALLCHILDREN &&
552 Wnd != UserGetDesktopWindow() )
553 {
554 PWND Child;
555
556 for (Child = Wnd->spwndChild; Child; Child = Child->spwndNext)
557 {
558 /* transparent window, check for non-transparent sibling to paint first, then skip it */
559 if ( Child->ExStyle & WS_EX_TRANSPARENT &&
560 ( Child->hrgnUpdate != NULL || Child->state & WNDS_INTERNALPAINT ) )
561 {
562 PWND Next = Child->spwndNext;
563 while (Next)
564 {
565 if ( Next->hrgnUpdate != NULL || Next->state & WNDS_INTERNALPAINT ) break;
566
567 Next = Next->spwndNext;
568 }
569
570 if (Next) continue;
571 }
572
573 if (Child->style & WS_VISIBLE)
574 {
575 USER_REFERENCE_ENTRY Ref;
576 UserRefObjectCo(Child, &Ref);
577 co_IntUpdateWindows(Child, Flags, TRUE);
578 UserDerefObjectCo(Child);
579 }
580 }
581 }
582 }
583
584 VOID FASTCALL
585 UserUpdateWindows(PWND pWnd, ULONG Flags)
586 {
587 // If transparent and any sibling windows below needs to be painted, leave.
588 if (pWnd->ExStyle & WS_EX_TRANSPARENT)
589 {
590 PWND Next = pWnd->spwndNext;
591
592 while(Next)
593 {
594 if ( Next->head.pti == pWnd->head.pti &&
595 ( Next->hrgnUpdate != NULL || Next->state & WNDS_INTERNALPAINT) )
596 {
597 return;
598 }
599
600 Next = Next->spwndNext;
601 }
602 }
603 co_IntUpdateWindows(pWnd, Flags, FALSE);
604 }
605
606 VOID FASTCALL
607 UserSyncAndPaintWindows(PWND pWnd, ULONG Flags)
608 {
609 PWND Parent = pWnd;
610 // Find parent, if it needs to be painted, leave.
611 while(TRUE)
612 {
613 if ((Parent = Parent->spwndParent) == NULL) break;
614 if ( Parent->style & WS_CLIPCHILDREN ) break;
615 if ( Parent->hrgnUpdate != NULL || Parent->state & WNDS_INTERNALPAINT ) return;
616 }
617
618 IntSendSyncPaint(pWnd, Flags);
619 co_IntPaintWindows(pWnd, Flags, FALSE);
620 }
621
622 /*
623 * IntInvalidateWindows
624 *
625 * Internal function used by IntRedrawWindow, UserRedrawDesktop,
626 * co_WinPosSetWindowPos, co_UserRedrawWindow.
627 */
628 VOID FASTCALL
629 IntInvalidateWindows(PWND Wnd, PREGION Rgn, ULONG Flags)
630 {
631 INT RgnType = NULLREGION;
632 BOOL HadPaintMessage;
633
634 TRACE("IntInvalidateWindows start Rgn %p\n",Rgn);
635
636 if ( Rgn > PRGN_WINDOW )
637 {
638 /*
639 * If the nonclient is not to be redrawn, clip the region to the client
640 * rect
641 */
642 if ((Flags & RDW_INVALIDATE) != 0 && (Flags & RDW_FRAME) == 0)
643 {
644 PREGION RgnClient;
645
646 RgnClient = IntSysCreateRectpRgnIndirect(&Wnd->rcClient);
647 if (RgnClient)
648 {
649 RgnType = IntGdiCombineRgn(Rgn, Rgn, RgnClient, RGN_AND);
650 REGION_Delete(RgnClient);
651 }
652 }
653
654 /*
655 * Clip the given region with window rectangle (or region)
656 */
657
658 if (!Wnd->hrgnClip || (Wnd->style & WS_MINIMIZE))
659 {
660 PREGION RgnWindow = IntSysCreateRectpRgnIndirect(&Wnd->rcWindow);
661 if (RgnWindow)
662 {
663 RgnType = IntGdiCombineRgn(Rgn, Rgn, RgnWindow, RGN_AND);
664 REGION_Delete(RgnWindow);
665 }
666 }
667 else
668 {
669 PREGION RgnClip = REGION_LockRgn(Wnd->hrgnClip);
670 if (RgnClip)
671 {
672 REGION_bOffsetRgn(Rgn,
673 -Wnd->rcWindow.left,
674 -Wnd->rcWindow.top);
675 RgnType = IntGdiCombineRgn(Rgn, Rgn, RgnClip, RGN_AND);
676 REGION_bOffsetRgn(Rgn,
677 Wnd->rcWindow.left,
678 Wnd->rcWindow.top);
679 REGION_UnlockRgn(RgnClip);
680 }
681 }
682 }
683 else
684 {
685 RgnType = NULLREGION;
686 }
687
688 /*
689 * Save current state of pending updates
690 */
691
692 HadPaintMessage = IntIsWindowDirty(Wnd);
693
694 /*
695 * Update the region and flags
696 */
697
698 // The following flags are used to invalidate the window.
699 if (Flags & (RDW_INVALIDATE|RDW_INTERNALPAINT|RDW_ERASE|RDW_FRAME))
700 {
701 if (Flags & RDW_INTERNALPAINT)
702 {
703 Wnd->state |= WNDS_INTERNALPAINT;
704 }
705
706 if (Flags & RDW_INVALIDATE )
707 {
708 PREGION RgnUpdate;
709
710 Wnd->state &= ~WNDS_NONCPAINT;
711
712 /* If not the same thread set it dirty. */
713 if (Wnd->head.pti != PsGetCurrentThreadWin32Thread())
714 {
715 Wnd->state |= WNDS_UPDATEDIRTY;
716 if (Wnd->state2 & WNDS2_WMPAINTSENT)
717 Wnd->state2 |= WNDS2_ENDPAINTINVALIDATE;
718 }
719
720 if (Flags & RDW_FRAME)
721 Wnd->state |= WNDS_SENDNCPAINT;
722
723 if (Flags & RDW_ERASE)
724 Wnd->state |= WNDS_SENDERASEBACKGROUND;
725
726 if (RgnType != NULLREGION && Rgn > PRGN_WINDOW)
727 {
728 if (Wnd->hrgnUpdate == NULL)
729 {
730 Wnd->hrgnUpdate = NtGdiCreateRectRgn(0, 0, 0, 0);
731 IntGdiSetRegionOwner(Wnd->hrgnUpdate, GDI_OBJ_HMGR_PUBLIC);
732 }
733
734 if (Wnd->hrgnUpdate != HRGN_WINDOW)
735 {
736 RgnUpdate = REGION_LockRgn(Wnd->hrgnUpdate);
737 if (RgnUpdate)
738 {
739 RgnType = IntGdiCombineRgn(RgnUpdate, RgnUpdate, Rgn, RGN_OR);
740 REGION_UnlockRgn(RgnUpdate);
741 if (RgnType == NULLREGION)
742 {
743 IntGdiSetRegionOwner(Wnd->hrgnUpdate, GDI_OBJ_HMGR_POWNED);
744 GreDeleteObject(Wnd->hrgnUpdate);
745 Wnd->hrgnUpdate = NULL;
746 }
747 }
748 }
749 }
750
751 Flags |= RDW_ERASE|RDW_FRAME; // For children.
752
753 }
754
755 if (!HadPaintMessage && IntIsWindowDirty(Wnd))
756 {
757 MsqIncPaintCountQueue(Wnd->head.pti);
758 }
759
760 } // The following flags are used to validate the window.
761 else if (Flags & (RDW_VALIDATE|RDW_NOINTERNALPAINT|RDW_NOERASE|RDW_NOFRAME))
762 {
763 if (Wnd->state & WNDS_UPDATEDIRTY && !(Flags & RDW_NOUPDATEDIRTY))
764 return;
765
766 if (Flags & RDW_NOINTERNALPAINT)
767 {
768 Wnd->state &= ~WNDS_INTERNALPAINT;
769 }
770
771 if (Flags & RDW_VALIDATE)
772 {
773 if (Flags & RDW_NOFRAME)
774 Wnd->state &= ~WNDS_SENDNCPAINT;
775
776 if (Flags & RDW_NOERASE)
777 Wnd->state &= ~(WNDS_SENDERASEBACKGROUND|WNDS_ERASEBACKGROUND);
778
779 if (Wnd->hrgnUpdate > HRGN_WINDOW && RgnType != NULLREGION && Rgn > PRGN_WINDOW)
780 {
781 PREGION RgnUpdate = REGION_LockRgn(Wnd->hrgnUpdate);
782
783 if (RgnUpdate)
784 {
785 RgnType = IntGdiCombineRgn(RgnUpdate, RgnUpdate, Rgn, RGN_DIFF);
786 REGION_UnlockRgn(RgnUpdate);
787
788 if (RgnType == NULLREGION)
789 {
790 IntGdiSetRegionOwner(Wnd->hrgnUpdate, GDI_OBJ_HMGR_POWNED);
791 GreDeleteObject(Wnd->hrgnUpdate);
792 Wnd->hrgnUpdate = NULL;
793 }
794 }
795 }
796 // If update is null, do not erase.
797 if (Wnd->hrgnUpdate == NULL)
798 {
799 Wnd->state &= ~(WNDS_SENDERASEBACKGROUND|WNDS_ERASEBACKGROUND);
800 }
801 }
802
803 if (HadPaintMessage && !IntIsWindowDirty(Wnd))
804 {
805 MsqDecPaintCountQueue(Wnd->head.pti);
806 }
807 }
808
809 /*
810 * Process children if needed
811 */
812
813 if (!(Flags & RDW_NOCHILDREN) &&
814 !(Wnd->style & WS_MINIMIZE) &&
815 ((Flags & RDW_ALLCHILDREN) || !(Wnd->style & WS_CLIPCHILDREN)))
816 {
817 PWND Child;
818
819 for (Child = Wnd->spwndChild; Child; Child = Child->spwndNext)
820 {
821 if (Child->style & WS_VISIBLE)
822 {
823 /*
824 * Recursive call to update children hrgnUpdate
825 */
826 PREGION RgnTemp = IntSysCreateRectpRgn(0, 0, 0, 0);
827 if (RgnTemp)
828 {
829 if (Rgn > PRGN_WINDOW) IntGdiCombineRgn(RgnTemp, Rgn, 0, RGN_COPY);
830 IntInvalidateWindows(Child, ((Rgn > PRGN_WINDOW)?RgnTemp:Rgn), Flags);
831 REGION_Delete(RgnTemp);
832 }
833 }
834 }
835 }
836 TRACE("IntInvalidateWindows exit\n");
837 }
838
839 /*
840 * IntIsWindowDrawable
841 *
842 * Remarks
843 * Window is drawable when it is visible and all parents are not
844 * minimized.
845 */
846
847 BOOL FASTCALL
848 IntIsWindowDrawable(PWND Wnd)
849 {
850 PWND WndObject;
851
852 for (WndObject = Wnd; WndObject != NULL; WndObject = WndObject->spwndParent)
853 {
854 if ( WndObject->state2 & WNDS2_INDESTROY ||
855 WndObject->state & WNDS_DESTROYED ||
856 !WndObject ||
857 !(WndObject->style & WS_VISIBLE) ||
858 ((WndObject->style & WS_MINIMIZE) && (WndObject != Wnd)))
859 {
860 return FALSE;
861 }
862 }
863
864 return TRUE;
865 }
866
867 /*
868 * IntRedrawWindow
869 *
870 * Internal version of NtUserRedrawWindow that takes WND as
871 * first parameter.
872 */
873
874 BOOL FASTCALL
875 co_UserRedrawWindow(
876 PWND Window,
877 const RECTL* UpdateRect,
878 PREGION UpdateRgn,
879 ULONG Flags)
880 {
881 PREGION TmpRgn = NULL;
882 TRACE("co_UserRedrawWindow start Rgn %p\n",UpdateRgn);
883
884 /*
885 * Step 1.
886 * Validation of passed parameters.
887 */
888
889 if (!IntIsWindowDrawable(Window))
890 {
891 return TRUE; // Just do nothing!!!
892 }
893
894 if (Window == NULL)
895 {
896 Window = UserGetDesktopWindow();
897 }
898
899 /*
900 * Step 2.
901 * Transform the parameters UpdateRgn and UpdateRect into
902 * a region hRgn specified in screen coordinates.
903 */
904
905 if (Flags & (RDW_INVALIDATE | RDW_VALIDATE)) // Both are OKAY!
906 {
907 /* We can't hold lock on GDI objects while doing roundtrips to user mode,
908 * so use a copy instead */
909 if (UpdateRgn)
910 {
911 TmpRgn = IntSysCreateRectpRgn(0, 0, 0, 0);
912
913 if (UpdateRgn > PRGN_WINDOW)
914 {
915 IntGdiCombineRgn(TmpRgn, UpdateRgn, NULL, RGN_COPY);
916 }
917
918 if (Window != UserGetDesktopWindow())
919 {
920 REGION_bOffsetRgn(TmpRgn, Window->rcClient.left, Window->rcClient.top);
921 }
922 }
923 else
924 {
925 if (UpdateRect != NULL)
926 {
927 if (Window == UserGetDesktopWindow())
928 {
929 TmpRgn = IntSysCreateRectpRgnIndirect(UpdateRect);
930 }
931 else
932 {
933 TmpRgn = IntSysCreateRectpRgn(Window->rcClient.left + UpdateRect->left,
934 Window->rcClient.top + UpdateRect->top,
935 Window->rcClient.left + UpdateRect->right,
936 Window->rcClient.top + UpdateRect->bottom);
937 }
938 }
939 else
940 {
941 if ((Flags & (RDW_INVALIDATE | RDW_FRAME)) == (RDW_INVALIDATE | RDW_FRAME) ||
942 (Flags & (RDW_VALIDATE | RDW_NOFRAME)) == (RDW_VALIDATE | RDW_NOFRAME))
943 {
944 if (!RECTL_bIsEmptyRect(&Window->rcWindow))
945 TmpRgn = IntSysCreateRectpRgnIndirect(&Window->rcWindow);
946 }
947 else
948 {
949 if (!RECTL_bIsEmptyRect(&Window->rcClient))
950 TmpRgn = IntSysCreateRectpRgnIndirect(&Window->rcClient);
951 }
952 }
953 }
954 }
955
956 /* Fixes test RDW_INTERNALPAINT behavior */
957 if (TmpRgn == NULL)
958 {
959 TmpRgn = PRGN_WINDOW; // Need a region so the bits can be set!!!
960 }
961
962 /*
963 * Step 3.
964 * Adjust the window update region depending on hRgn and flags.
965 */
966
967 if (Flags & (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT) &&
968 TmpRgn != NULL)
969 {
970 IntInvalidateWindows(Window, TmpRgn, Flags);
971 }
972
973 /*
974 * Step 4.
975 * Repaint and erase windows if needed.
976 */
977
978 if (Flags & RDW_UPDATENOW)
979 {
980 UserUpdateWindows(Window, Flags);
981 }
982 else if (Flags & RDW_ERASENOW)
983 {
984 if ((Flags & (RDW_NOCHILDREN|RDW_ALLCHILDREN)) == 0)
985 Flags |= RDW_CLIPCHILDREN;
986
987 UserSyncAndPaintWindows(Window, Flags);
988 }
989
990 /*
991 * Step 5.
992 * Cleanup ;-)
993 */
994
995 if (TmpRgn > PRGN_WINDOW)
996 {
997 REGION_Delete(TmpRgn);
998 }
999 TRACE("co_UserRedrawWindow exit\n");
1000
1001 return TRUE;
1002 }
1003
1004 VOID FASTCALL
1005 PaintSuspendedWindow(PWND pwnd, HRGN hrgnOrig)
1006 {
1007 if (pwnd->hrgnUpdate)
1008 {
1009 HDC hDC;
1010 INT Flags = DC_NC|DC_NOSENDMSG;
1011 HRGN hrgnTemp;
1012 RECT Rect;
1013 INT type;
1014 PREGION prgn;
1015
1016 if (pwnd->hrgnUpdate > HRGN_WINDOW)
1017 {
1018 hrgnTemp = NtGdiCreateRectRgn(0, 0, 0, 0);
1019 type = NtGdiCombineRgn( hrgnTemp, pwnd->hrgnUpdate, 0, RGN_COPY);
1020 if (type == ERROR)
1021 {
1022 GreDeleteObject(hrgnTemp);
1023 hrgnTemp = HRGN_WINDOW;
1024 }
1025 }
1026 else
1027 {
1028 hrgnTemp = GreCreateRectRgnIndirect(&pwnd->rcWindow);
1029 }
1030
1031 if ( hrgnOrig &&
1032 hrgnTemp > HRGN_WINDOW &&
1033 NtGdiCombineRgn(hrgnTemp, hrgnTemp, hrgnOrig, RGN_AND) == NULLREGION)
1034 {
1035 GreDeleteObject(hrgnTemp);
1036 return;
1037 }
1038
1039 hDC = UserGetDCEx(pwnd, hrgnTemp, DCX_WINDOW|DCX_INTERSECTRGN|DCX_USESTYLE|DCX_KEEPCLIPRGN);
1040
1041 Rect = pwnd->rcWindow;
1042 RECTL_vOffsetRect(&Rect, -pwnd->rcWindow.left, -pwnd->rcWindow.top);
1043
1044 // Clear out client area!
1045 FillRect(hDC, &Rect, IntGetSysColorBrush(COLOR_WINDOW));
1046
1047 NC_DoNCPaint(pwnd, hDC, Flags); // Redraw without MENUs.
1048
1049 UserReleaseDC(pwnd, hDC, FALSE);
1050
1051 prgn = REGION_LockRgn(hrgnTemp);
1052 IntInvalidateWindows(pwnd, prgn, RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN);
1053 REGION_UnlockRgn(prgn);
1054
1055 // Set updates for this window.
1056 pwnd->state |= WNDS_SENDNCPAINT|WNDS_SENDERASEBACKGROUND|WNDS_UPDATEDIRTY;
1057
1058 // DCX_KEEPCLIPRGN is set. Check it anyway.
1059 if (hrgnTemp > HRGN_WINDOW && GreIsHandleValid(hrgnTemp)) GreDeleteObject(hrgnTemp);
1060 }
1061 }
1062
1063 VOID FASTCALL
1064 UpdateTheadChildren(PWND pWnd, HRGN hRgn)
1065 {
1066 PaintSuspendedWindow( pWnd, hRgn );
1067
1068 if (!(pWnd->style & WS_CLIPCHILDREN))
1069 return;
1070
1071 pWnd = pWnd->spwndChild; // invalidate children if any.
1072 while (pWnd)
1073 {
1074 UpdateTheadChildren( pWnd, hRgn );
1075 pWnd = pWnd->spwndNext;
1076 }
1077 }
1078
1079 VOID FASTCALL
1080 UpdateThreadWindows(PWND pWnd, PTHREADINFO pti, HRGN hRgn)
1081 {
1082 PWND pwndTemp;
1083
1084 for ( pwndTemp = pWnd;
1085 pwndTemp;
1086 pwndTemp = pwndTemp->spwndNext )
1087 {
1088 if (pwndTemp->head.pti == pti)
1089 {
1090 UserUpdateWindows(pwndTemp, RDW_ALLCHILDREN);
1091 }
1092 else
1093 {
1094 if (IsThreadSuspended(pwndTemp->head.pti))
1095 {
1096 UpdateTheadChildren(pwndTemp, hRgn);
1097 }
1098 else
1099 UserUpdateWindows(pwndTemp, RDW_ALLCHILDREN);
1100 }
1101 }
1102 }
1103
1104 BOOL FASTCALL
1105 IntIsWindowDirty(PWND Wnd)
1106 {
1107 return ( Wnd->style & WS_VISIBLE &&
1108 ( Wnd->hrgnUpdate != NULL ||
1109 Wnd->state & WNDS_INTERNALPAINT ) );
1110 }
1111
1112 /*
1113 Conditions to paint any window:
1114
1115 1. Update region is not null.
1116 2. Internal paint flag is set.
1117 3. Paint count is not zero.
1118
1119 */
1120 PWND FASTCALL
1121 IntFindWindowToRepaint(PWND Window, PTHREADINFO Thread)
1122 {
1123 PWND hChild;
1124 PWND TempWindow;
1125
1126 for (; Window != NULL; Window = Window->spwndNext)
1127 {
1128 if (IntWndBelongsToThread(Window, Thread))
1129 {
1130 if (IntIsWindowDirty(Window))
1131 {
1132 /* Make sure all non-transparent siblings are already drawn. */
1133 if (Window->ExStyle & WS_EX_TRANSPARENT)
1134 {
1135 for (TempWindow = Window->spwndNext; TempWindow != NULL;
1136 TempWindow = TempWindow->spwndNext)
1137 {
1138 if (!(TempWindow->ExStyle & WS_EX_TRANSPARENT) &&
1139 IntWndBelongsToThread(TempWindow, Thread) &&
1140 IntIsWindowDirty(TempWindow))
1141 {
1142 return TempWindow;
1143 }
1144 }
1145 }
1146 return Window;
1147 }
1148 }
1149 /* find a child of the specified window that needs repainting */
1150 if (Window->spwndChild)
1151 {
1152 hChild = IntFindWindowToRepaint(Window->spwndChild, Thread);
1153 if (hChild != NULL)
1154 return hChild;
1155 }
1156 }
1157 return Window;
1158 }
1159
1160 BOOL FASTCALL
1161 IntGetPaintMessage(
1162 PWND Window,
1163 UINT MsgFilterMin,
1164 UINT MsgFilterMax,
1165 PTHREADINFO Thread,
1166 MSG *Message,
1167 BOOL Remove)
1168 {
1169 PWND PaintWnd, StartWnd;
1170
1171 if ((MsgFilterMin != 0 || MsgFilterMax != 0) &&
1172 (MsgFilterMin > WM_PAINT || MsgFilterMax < WM_PAINT))
1173 return FALSE;
1174
1175 if (Thread->TIF_flags & TIF_SYSTEMTHREAD )
1176 {
1177 ERR("WM_PAINT is in a System Thread!\n");
1178 }
1179
1180 StartWnd = UserGetDesktopWindow();
1181 PaintWnd = IntFindWindowToRepaint(StartWnd, Thread);
1182
1183 Message->hwnd = PaintWnd ? UserHMGetHandle(PaintWnd) : NULL;
1184
1185 if (Message->hwnd == NULL && Thread->cPaintsReady)
1186 {
1187 // Find note in window.c:"PAINTING BUG".
1188 ERR("WARNING SOMETHING HAS GONE WRONG: Thread marked as containing dirty windows, but no dirty windows found! Counts %u\n",Thread->cPaintsReady);
1189 /* Hack to stop spamming the debug log ! */
1190 Thread->cPaintsReady = 0;
1191 return FALSE;
1192 }
1193
1194 if (Message->hwnd == NULL)
1195 return FALSE;
1196
1197 if (!(Window == NULL ||
1198 PaintWnd == Window ||
1199 IntIsChildWindow(Window, PaintWnd))) /* check that it is a child of the specified parent */
1200 return FALSE;
1201
1202 if (PaintWnd->state & WNDS_INTERNALPAINT)
1203 {
1204 PaintWnd->state &= ~WNDS_INTERNALPAINT;
1205 if (!PaintWnd->hrgnUpdate)
1206 MsqDecPaintCountQueue(Thread);
1207 }
1208 PaintWnd->state2 &= ~WNDS2_STARTPAINT;
1209 PaintWnd->state &= ~WNDS_UPDATEDIRTY;
1210
1211 Window = PaintWnd;
1212 while( Window && Window != UserGetDesktopWindow())
1213 {
1214 // Role back and check for clip children, do not set if any.
1215 if (Window->spwndParent && !(Window->spwndParent->style & WS_CLIPCHILDREN))
1216 {
1217 PaintWnd->state2 |= WNDS2_WMPAINTSENT;
1218 }
1219 Window = Window->spwndParent;
1220 }
1221
1222 Message->wParam = Message->lParam = 0;
1223 Message->message = WM_PAINT;
1224 return TRUE;
1225 }
1226
1227 BOOL
1228 FASTCALL
1229 IntPrintWindow(
1230 PWND pwnd,
1231 HDC hdcBlt,
1232 UINT nFlags)
1233 {
1234 HDC hdcSrc;
1235 INT cx, cy, xSrc, ySrc;
1236
1237 if ( nFlags & PW_CLIENTONLY)
1238 {
1239 cx = pwnd->rcClient.right - pwnd->rcClient.left;
1240 cy = pwnd->rcClient.bottom - pwnd->rcClient.top;
1241 xSrc = pwnd->rcClient.left - pwnd->rcWindow.left;
1242 ySrc = pwnd->rcClient.top - pwnd->rcWindow.top;
1243 }
1244 else
1245 {
1246 cx = pwnd->rcWindow.right - pwnd->rcWindow.left;
1247 cy = pwnd->rcWindow.bottom - pwnd->rcWindow.top;
1248 xSrc = 0;
1249 ySrc = 0;
1250 }
1251
1252 // TODO: Setup Redirection for Print.
1253 return FALSE;
1254
1255 /* Update the window just incase. */
1256 co_IntUpdateWindows( pwnd, RDW_ALLCHILDREN, FALSE);
1257
1258 hdcSrc = UserGetDCEx( pwnd, NULL, DCX_CACHE|DCX_WINDOW);
1259 /* Print window to printer context. */
1260 NtGdiBitBlt( hdcBlt,
1261 0,
1262 0,
1263 cx,
1264 cy,
1265 hdcSrc,
1266 xSrc,
1267 ySrc,
1268 SRCCOPY,
1269 0,
1270 0);
1271
1272 UserReleaseDC( pwnd, hdcSrc, FALSE);
1273
1274 // TODO: Release Redirection from Print.
1275
1276 return TRUE;
1277 }
1278
1279 BOOL
1280 FASTCALL
1281 IntFlashWindowEx(PWND pWnd, PFLASHWINFO pfwi)
1282 {
1283 DWORD FlashState;
1284 UINT uCount = pfwi->uCount;
1285 BOOL Activate = FALSE, Ret = FALSE;
1286
1287 ASSERT(pfwi);
1288
1289 FlashState = (DWORD)UserGetProp(pWnd, AtomFlashWndState, TRUE);
1290
1291 if (FlashState == FLASHW_FINISHED)
1292 {
1293 // Cycle has finished, kill timer and set this to Stop.
1294 FlashState |= FLASHW_KILLSYSTIMER;
1295 pfwi->dwFlags = FLASHW_STOP;
1296 }
1297 else
1298 {
1299 if (FlashState)
1300 {
1301 if (pfwi->dwFlags == FLASHW_SYSTIMER)
1302 {
1303 // Called from system timer, restore flags, counts and state.
1304 pfwi->dwFlags = LOWORD(FlashState);
1305 uCount = HIWORD(FlashState);
1306 FlashState = MAKELONG(LOWORD(FlashState),0);
1307 }
1308 else
1309 {
1310 // Clean out the trash! Fix SeaMonkey crash after restart.
1311 FlashState = 0;
1312 }
1313 }
1314
1315 if (FlashState == 0)
1316 { // First time in cycle, setup flash state.
1317 if ( pWnd->state & WNDS_ACTIVEFRAME ||
1318 (pfwi->dwFlags & FLASHW_CAPTION && pWnd->style & (WS_BORDER|WS_DLGFRAME)))
1319 {
1320 FlashState = FLASHW_STARTED|FLASHW_ACTIVE;
1321 }
1322 }
1323
1324 // Set previous window state.
1325 Ret = !!(FlashState & FLASHW_ACTIVE);
1326
1327 if ( (pfwi->dwFlags & FLASHW_TIMERNOFG) == FLASHW_TIMERNOFG &&
1328 gpqForeground == pWnd->head.pti->MessageQueue )
1329 {
1330 // Flashing until foreground, set this to Stop.
1331 pfwi->dwFlags = FLASHW_STOP;
1332 }
1333 }
1334
1335 // Toggle activate flag.
1336 if ( pfwi->dwFlags == FLASHW_STOP )
1337 {
1338 if (gpqForeground && gpqForeground->spwndActive == pWnd)
1339 Activate = TRUE;
1340 else
1341 Activate = FALSE;
1342 }
1343 else
1344 {
1345 Activate = (FlashState & FLASHW_ACTIVE) == 0;
1346 }
1347
1348 if ( pfwi->dwFlags == FLASHW_STOP || pfwi->dwFlags & FLASHW_CAPTION )
1349 {
1350 co_IntSendMessage(UserHMGetHandle(pWnd), WM_NCACTIVATE, Activate, 0);
1351 }
1352
1353 // FIXME: Check for a Stop Sign here.
1354 if ( pfwi->dwFlags & FLASHW_TRAY )
1355 {
1356 // Need some shell work here too.
1357 TRACE("FIXME: Flash window no Tray support!\n");
1358 }
1359
1360 if ( pfwi->dwFlags == FLASHW_STOP )
1361 {
1362 if (FlashState & FLASHW_KILLSYSTIMER)
1363 {
1364 IntKillTimer(pWnd, ID_EVENT_SYSTIMER_FLASHWIN, TRUE);
1365 }
1366
1367 UserRemoveProp(pWnd, AtomFlashWndState, TRUE);
1368 }
1369 else
1370 { // Have a count and started, set timer.
1371 if ( uCount )
1372 {
1373 FlashState |= FLASHW_COUNT;
1374
1375 if (!(Activate ^ !!(FlashState & FLASHW_STARTED)))
1376 uCount--;
1377
1378 if (!(FlashState & FLASHW_KILLSYSTIMER))
1379 pfwi->dwFlags |= FLASHW_TIMER;
1380 }
1381
1382 if (pfwi->dwFlags & FLASHW_TIMER)
1383 {
1384 FlashState |= FLASHW_KILLSYSTIMER;
1385
1386 IntSetTimer( pWnd,
1387 ID_EVENT_SYSTIMER_FLASHWIN,
1388 pfwi->dwTimeout ? pfwi->dwTimeout : gpsi->dtCaretBlink,
1389 SystemTimerProc,
1390 TMRF_SYSTEM );
1391 }
1392
1393 if (FlashState & FLASHW_COUNT && uCount == 0)
1394 {
1395 // Keep spinning? Nothing else to do.
1396 FlashState = FLASHW_FINISHED;
1397 }
1398 else
1399 {
1400 // Save state and flags so this can be restored next time through.
1401 FlashState ^= (FlashState ^ -!!(Activate)) & FLASHW_ACTIVE;
1402 FlashState ^= (FlashState ^ pfwi->dwFlags) & (FLASHW_MASK & ~FLASHW_TIMER);
1403 }
1404 FlashState = MAKELONG(LOWORD(FlashState),uCount);
1405 UserSetProp(pWnd, AtomFlashWndState, (HANDLE)FlashState, TRUE);
1406 }
1407 return Ret;
1408 }
1409
1410 HDC FASTCALL
1411 IntBeginPaint(PWND Window, PPAINTSTRUCT Ps)
1412 {
1413 RECT Rect;
1414 INT type;
1415 BOOL Erase = FALSE;
1416
1417 co_UserHideCaret(Window);
1418
1419 Window->state2 |= WNDS2_STARTPAINT;
1420 Window->state &= ~WNDS_PAINTNOTPROCESSED;
1421
1422 if (Window->state & WNDS_SENDNCPAINT)
1423 {
1424 HRGN hRgn;
1425 // Application can keep update dirty.
1426 do
1427 {
1428 Window->state &= ~WNDS_UPDATEDIRTY;
1429 hRgn = IntGetNCUpdateRgn(Window, FALSE);
1430 IntSendNCPaint(Window, hRgn);
1431 if (hRgn > HRGN_WINDOW && GreIsHandleValid(hRgn))
1432 {
1433 /* NOTE: The region can already be deleted! */
1434 GreDeleteObject(hRgn);
1435 }
1436 }
1437 while(Window->state & WNDS_UPDATEDIRTY);
1438 }
1439 else
1440 {
1441 Window->state &= ~WNDS_UPDATEDIRTY;
1442 }
1443
1444 RtlZeroMemory(Ps, sizeof(PAINTSTRUCT));
1445
1446 if (Window->state2 & WNDS2_ENDPAINTINVALIDATE)
1447 {
1448 ERR("BP: Another thread invalidated this window\n");
1449 }
1450
1451 Ps->hdc = UserGetDCEx( Window,
1452 Window->hrgnUpdate,
1453 DCX_INTERSECTRGN | DCX_USESTYLE);
1454 if (!Ps->hdc)
1455 {
1456 return NULL;
1457 }
1458
1459 // If set, always clear flags out due to the conditions later on for sending the message.
1460 if (Window->state & WNDS_SENDERASEBACKGROUND)
1461 {
1462 Window->state &= ~(WNDS_SENDERASEBACKGROUND|WNDS_ERASEBACKGROUND);
1463 Erase = TRUE;
1464 }
1465
1466 if (Window->hrgnUpdate != NULL)
1467 {
1468 MsqDecPaintCountQueue(Window->head.pti);
1469 IntGdiSetRegionOwner(Window->hrgnUpdate, GDI_OBJ_HMGR_POWNED);
1470 /* The region is part of the dc now and belongs to the process! */
1471 Window->hrgnUpdate = NULL;
1472 }
1473 else
1474 {
1475 if (Window->state & WNDS_INTERNALPAINT)
1476 MsqDecPaintCountQueue(Window->head.pti);
1477 }
1478
1479 type = GdiGetClipBox(Ps->hdc, &Ps->rcPaint);
1480
1481 IntGetClientRect(Window, &Rect);
1482
1483 Window->state &= ~WNDS_INTERNALPAINT;
1484
1485 if ( Erase && // Set to erase,
1486 type != NULLREGION && // don't erase if the clip box is empty,
1487 (!(Window->pcls->style & CS_PARENTDC) || // not parent dc or
1488 RECTL_bIntersectRect( &Rect, &Rect, &Ps->rcPaint) ) ) // intersecting.
1489 {
1490 Ps->fErase = !co_IntSendMessage(UserHMGetHandle(Window), WM_ERASEBKGND, (WPARAM)Ps->hdc, 0);
1491 if ( Ps->fErase )
1492 {
1493 Window->state |= (WNDS_SENDERASEBACKGROUND|WNDS_ERASEBACKGROUND);
1494 }
1495 }
1496 else
1497 {
1498 Ps->fErase = FALSE;
1499 }
1500
1501 IntSendChildNCPaint(Window);
1502
1503 return Ps->hdc;
1504 }
1505
1506 BOOL FASTCALL
1507 IntEndPaint(PWND Wnd, PPAINTSTRUCT Ps)
1508 {
1509 HDC hdc = NULL;
1510
1511 hdc = Ps->hdc;
1512
1513 UserReleaseDC(Wnd, hdc, TRUE);
1514
1515 if (Wnd->state2 & WNDS2_ENDPAINTINVALIDATE)
1516 {
1517 ERR("EP: Another thread invalidated this window\n");
1518 Wnd->state2 &= ~WNDS2_ENDPAINTINVALIDATE;
1519 }
1520
1521 Wnd->state2 &= ~(WNDS2_WMPAINTSENT|WNDS2_STARTPAINT);
1522
1523 co_UserShowCaret(Wnd);
1524
1525 return TRUE;
1526 }
1527
1528 /* PUBLIC FUNCTIONS ***********************************************************/
1529
1530 /*
1531 * NtUserBeginPaint
1532 *
1533 * Status
1534 * @implemented
1535 */
1536
1537 HDC APIENTRY
1538 NtUserBeginPaint(HWND hWnd, PAINTSTRUCT* UnsafePs)
1539 {
1540 PWND Window = NULL;
1541 PAINTSTRUCT Ps;
1542 NTSTATUS Status;
1543 HDC hDC;
1544 USER_REFERENCE_ENTRY Ref;
1545 DECLARE_RETURN(HDC);
1546
1547 TRACE("Enter NtUserBeginPaint\n");
1548 UserEnterExclusive();
1549
1550 if (!(Window = UserGetWindowObject(hWnd)))
1551 {
1552 RETURN( NULL);
1553 }
1554
1555 UserRefObjectCo(Window, &Ref);
1556
1557 hDC = IntBeginPaint(Window, &Ps);
1558
1559 Status = MmCopyToCaller(UnsafePs, &Ps, sizeof(PAINTSTRUCT));
1560 if (! NT_SUCCESS(Status))
1561 {
1562 SetLastNtError(Status);
1563 RETURN(NULL);
1564 }
1565
1566 RETURN(hDC);
1567
1568 CLEANUP:
1569 if (Window) UserDerefObjectCo(Window);
1570
1571 TRACE("Leave NtUserBeginPaint, ret=%p\n",_ret_);
1572 UserLeave();
1573 END_CLEANUP;
1574
1575 }
1576
1577 /*
1578 * NtUserEndPaint
1579 *
1580 * Status
1581 * @implemented
1582 */
1583
1584 BOOL APIENTRY
1585 NtUserEndPaint(HWND hWnd, CONST PAINTSTRUCT* pUnsafePs)
1586 {
1587 NTSTATUS Status = STATUS_SUCCESS;
1588 PWND Window = NULL;
1589 PAINTSTRUCT Ps;
1590 USER_REFERENCE_ENTRY Ref;
1591 DECLARE_RETURN(BOOL);
1592
1593 TRACE("Enter NtUserEndPaint\n");
1594 UserEnterExclusive();
1595
1596 if (!(Window = UserGetWindowObject(hWnd)))
1597 {
1598 RETURN(FALSE);
1599 }
1600
1601 UserRefObjectCo(Window, &Ref); // Here for the exception.
1602
1603 _SEH2_TRY
1604 {
1605 ProbeForRead(pUnsafePs, sizeof(*pUnsafePs), 1);
1606 RtlCopyMemory(&Ps, pUnsafePs, sizeof(PAINTSTRUCT));
1607 }
1608 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1609 {
1610 Status = _SEH2_GetExceptionCode();
1611 }
1612 _SEH2_END
1613 if (!NT_SUCCESS(Status))
1614 {
1615 RETURN(FALSE);
1616 }
1617
1618 RETURN(IntEndPaint(Window, &Ps));
1619
1620 CLEANUP:
1621 if (Window) UserDerefObjectCo(Window);
1622
1623 TRACE("Leave NtUserEndPaint, ret=%i\n",_ret_);
1624 UserLeave();
1625 END_CLEANUP;
1626 }
1627
1628 /*
1629 * @implemented
1630 */
1631 BOOL APIENTRY
1632 NtUserFlashWindowEx(IN PFLASHWINFO pfwi)
1633 {
1634 PWND pWnd;
1635 FLASHWINFO finfo = {0};
1636 BOOL Ret = FALSE;
1637
1638 UserEnterExclusive();
1639
1640 _SEH2_TRY
1641 {
1642 ProbeForRead(pfwi, sizeof(FLASHWINFO), 1);
1643 RtlCopyMemory(&finfo, pfwi, sizeof(FLASHWINFO));
1644 }
1645 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1646 {
1647 SetLastNtError(_SEH2_GetExceptionCode());
1648 _SEH2_YIELD(goto Exit);
1649 }
1650 _SEH2_END
1651
1652 if (!( pWnd = ValidateHwndNoErr(finfo.hwnd)) ||
1653 finfo.cbSize != sizeof(FLASHWINFO) ||
1654 finfo.dwFlags & ~(FLASHW_ALL|FLASHW_TIMER|FLASHW_TIMERNOFG) )
1655 {
1656 EngSetLastError(ERROR_INVALID_PARAMETER);
1657 goto Exit;
1658 }
1659
1660 Ret = IntFlashWindowEx(pWnd, &finfo);
1661
1662 Exit:
1663 UserLeave();
1664 return Ret;
1665 }
1666
1667 /*
1668 GetUpdateRgn, this fails the same as the old one.
1669 */
1670 INT FASTCALL
1671 co_UserGetUpdateRgn(PWND Window, HRGN hRgn, BOOL bErase)
1672 {
1673 int RegionType;
1674 BOOL Type;
1675 RECTL Rect;
1676
1677 ASSERT_REFS_CO(Window);
1678
1679 if (bErase)
1680 {
1681 USER_REFERENCE_ENTRY Ref;
1682 UserRefObjectCo(Window, &Ref);
1683 co_IntPaintWindows(Window, RDW_NOCHILDREN, FALSE);
1684 UserDerefObjectCo(Window);
1685 }
1686
1687 Window->state &= ~WNDS_UPDATEDIRTY;
1688
1689 if (Window->hrgnUpdate == NULL)
1690 {
1691 NtGdiSetRectRgn(hRgn, 0, 0, 0, 0);
1692 return NULLREGION;
1693 }
1694
1695 Rect = Window->rcClient;
1696 Type = IntIntersectWithParents(Window, &Rect);
1697
1698 if (Window->hrgnUpdate == HRGN_WINDOW)
1699 {
1700 // Trap it out.
1701 ERR("GURn: Caller is passing Window Region 1\n");
1702 if (!Type)
1703 {
1704 NtGdiSetRectRgn(hRgn, 0, 0, 0, 0);
1705 return NULLREGION;
1706 }
1707
1708 RegionType = SIMPLEREGION;
1709
1710 if (Window != UserGetDesktopWindow()) // Window->fnid == FNID_DESKTOP
1711 {
1712 RECTL_vOffsetRect(&Rect,
1713 -Window->rcClient.left,
1714 -Window->rcClient.top);
1715 }
1716 GreSetRectRgnIndirect(hRgn, &Rect);
1717 }
1718 else
1719 {
1720 HRGN hrgnTemp = GreCreateRectRgnIndirect(&Rect);
1721
1722 RegionType = NtGdiCombineRgn(hRgn, hrgnTemp, Window->hrgnUpdate, RGN_AND);
1723
1724 if (RegionType == ERROR || RegionType == NULLREGION)
1725 {
1726 if (hrgnTemp) GreDeleteObject(hrgnTemp);
1727 NtGdiSetRectRgn(hRgn, 0, 0, 0, 0);
1728 return RegionType;
1729 }
1730
1731 if (Window != UserGetDesktopWindow()) // Window->fnid == FNID_DESKTOP
1732 {
1733 NtGdiOffsetRgn(hRgn,
1734 -Window->rcClient.left,
1735 -Window->rcClient.top);
1736 }
1737 if (hrgnTemp) GreDeleteObject(hrgnTemp);
1738 }
1739 return RegionType;
1740 }
1741
1742 BOOL FASTCALL
1743 co_UserGetUpdateRect(PWND Window, PRECT pRect, BOOL bErase)
1744 {
1745 INT RegionType;
1746 BOOL Ret = TRUE;
1747
1748 if (bErase)
1749 {
1750 USER_REFERENCE_ENTRY Ref;
1751 UserRefObjectCo(Window, &Ref);
1752 co_IntPaintWindows(Window, RDW_NOCHILDREN, FALSE);
1753 UserDerefObjectCo(Window);
1754 }
1755
1756 Window->state &= ~WNDS_UPDATEDIRTY;
1757
1758 if (Window->hrgnUpdate == NULL)
1759 {
1760 pRect->left = pRect->top = pRect->right = pRect->bottom = 0;
1761 Ret = FALSE;
1762 }
1763 else
1764 {
1765 /* Get the update region bounding box. */
1766 if (Window->hrgnUpdate == HRGN_WINDOW)
1767 {
1768 *pRect = Window->rcClient;
1769 ERR("GURt: Caller is retrieving Window Region 1\n");
1770 }
1771 else
1772 {
1773 RegionType = IntGdiGetRgnBox(Window->hrgnUpdate, pRect);
1774
1775 if (RegionType != ERROR && RegionType != NULLREGION)
1776 RECTL_bIntersectRect(pRect, pRect, &Window->rcClient);
1777 }
1778
1779 if (IntIntersectWithParents(Window, pRect))
1780 {
1781 if (Window != UserGetDesktopWindow()) // Window->fnid == FNID_DESKTOP
1782 {
1783 RECTL_vOffsetRect(pRect,
1784 -Window->rcClient.left,
1785 -Window->rcClient.top);
1786 }
1787 if (Window->pcls->style & CS_OWNDC)
1788 {
1789 HDC hdc;
1790 //DWORD layout;
1791 hdc = UserGetDCEx(Window, NULL, DCX_USESTYLE);
1792 //layout = NtGdiSetLayout(hdc, -1, 0);
1793 //IntMapWindowPoints( 0, Window, (LPPOINT)pRect, 2 );
1794 GreDPtoLP( hdc, (LPPOINT)pRect, 2 );
1795 //NtGdiSetLayout(hdc, -1, layout);
1796 UserReleaseDC(Window, hdc, FALSE);
1797 }
1798 }
1799 else
1800 {
1801 pRect->left = pRect->top = pRect->right = pRect->bottom = 0;
1802 }
1803 }
1804 return Ret;
1805 }
1806
1807 /*
1808 * NtUserGetUpdateRgn
1809 *
1810 * Status
1811 * @implemented
1812 */
1813
1814 INT APIENTRY
1815 NtUserGetUpdateRgn(HWND hWnd, HRGN hRgn, BOOL bErase)
1816 {
1817 DECLARE_RETURN(INT);
1818 PWND Window;
1819 INT ret;
1820
1821 TRACE("Enter NtUserGetUpdateRgn\n");
1822 UserEnterExclusive();
1823
1824 if (!(Window = UserGetWindowObject(hWnd)))
1825 {
1826 RETURN(ERROR);
1827 }
1828
1829 ret = co_UserGetUpdateRgn(Window, hRgn, bErase);
1830
1831 RETURN(ret);
1832
1833 CLEANUP:
1834 TRACE("Leave NtUserGetUpdateRgn, ret=%i\n",_ret_);
1835 UserLeave();
1836 END_CLEANUP;
1837 }
1838
1839 /*
1840 * NtUserGetUpdateRect
1841 *
1842 * Status
1843 * @implemented
1844 */
1845
1846 BOOL APIENTRY
1847 NtUserGetUpdateRect(HWND hWnd, LPRECT UnsafeRect, BOOL bErase)
1848 {
1849 PWND Window;
1850 RECTL Rect;
1851 NTSTATUS Status;
1852 BOOL Ret;
1853 DECLARE_RETURN(BOOL);
1854
1855 TRACE("Enter NtUserGetUpdateRect\n");
1856 UserEnterExclusive();
1857
1858 if (!(Window = UserGetWindowObject(hWnd)))
1859 {
1860 RETURN(FALSE);
1861 }
1862
1863 Ret = co_UserGetUpdateRect(Window, &Rect, bErase);
1864
1865 if (UnsafeRect != NULL)
1866 {
1867 Status = MmCopyToCaller(UnsafeRect, &Rect, sizeof(RECTL));
1868 if (!NT_SUCCESS(Status))
1869 {
1870 EngSetLastError(ERROR_INVALID_PARAMETER);
1871 RETURN(FALSE);
1872 }
1873 }
1874
1875 RETURN(Ret);
1876
1877 CLEANUP:
1878 TRACE("Leave NtUserGetUpdateRect, ret=%i\n",_ret_);
1879 UserLeave();
1880 END_CLEANUP;
1881 }
1882
1883 /*
1884 * NtUserRedrawWindow
1885 *
1886 * Status
1887 * @implemented
1888 */
1889
1890 BOOL APIENTRY
1891 NtUserRedrawWindow(
1892 HWND hWnd,
1893 CONST RECT *lprcUpdate,
1894 HRGN hrgnUpdate,
1895 UINT flags)
1896 {
1897 RECTL SafeUpdateRect;
1898 PWND Wnd;
1899 BOOL Ret;
1900 USER_REFERENCE_ENTRY Ref;
1901 NTSTATUS Status = STATUS_SUCCESS;
1902 PREGION RgnUpdate = NULL;
1903 DECLARE_RETURN(BOOL);
1904
1905 TRACE("Enter NtUserRedrawWindow\n");
1906 UserEnterExclusive();
1907
1908 if (!(Wnd = UserGetWindowObject(hWnd ? hWnd : IntGetDesktopWindow())))
1909 {
1910 RETURN( FALSE);
1911 }
1912
1913 if (lprcUpdate)
1914 {
1915 _SEH2_TRY
1916 {
1917 ProbeForRead(lprcUpdate, sizeof(RECTL), 1);
1918 RtlCopyMemory(&SafeUpdateRect, lprcUpdate, sizeof(RECTL));
1919 }
1920 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1921 {
1922 Status = _SEH2_GetExceptionCode();
1923 }
1924 _SEH2_END
1925 if (!NT_SUCCESS(Status))
1926 {
1927 EngSetLastError(RtlNtStatusToDosError(Status));
1928 RETURN( FALSE);
1929 }
1930 }
1931
1932 if ( flags & ~(RDW_ERASE|RDW_FRAME|RDW_INTERNALPAINT|RDW_INVALIDATE|
1933 RDW_NOERASE|RDW_NOFRAME|RDW_NOINTERNALPAINT|RDW_VALIDATE|
1934 RDW_ERASENOW|RDW_UPDATENOW|RDW_ALLCHILDREN|RDW_NOCHILDREN) )
1935 {
1936 /* RedrawWindow fails only in case that flags are invalid */
1937 EngSetLastError(ERROR_INVALID_FLAGS);
1938 RETURN( FALSE);
1939 }
1940
1941 /* We can't hold lock on GDI objects while doing roundtrips to user mode,
1942 * so it will be copied.
1943 */
1944 if (hrgnUpdate > HRGN_WINDOW)
1945 {
1946 RgnUpdate = REGION_LockRgn(hrgnUpdate);
1947 if (!RgnUpdate)
1948 {
1949 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
1950 RETURN(FALSE);
1951 }
1952 REGION_UnlockRgn(RgnUpdate);
1953 }
1954 else if (hrgnUpdate == HRGN_WINDOW) // Trap it out.
1955 {
1956 ERR("NTRW: Caller is passing Window Region 1\n");
1957 }
1958
1959 UserRefObjectCo(Wnd, &Ref);
1960
1961 Ret = co_UserRedrawWindow( Wnd,
1962 lprcUpdate ? &SafeUpdateRect : NULL,
1963 RgnUpdate,
1964 flags);
1965
1966 UserDerefObjectCo(Wnd);
1967
1968 RETURN( Ret);
1969
1970 CLEANUP:
1971 TRACE("Leave NtUserRedrawWindow, ret=%i\n",_ret_);
1972 UserLeave();
1973 END_CLEANUP;
1974 }
1975
1976 BOOL
1977 UserDrawCaptionText(
1978 PWND pWnd,
1979 HDC hDc,
1980 const PUNICODE_STRING Text,
1981 const RECTL *lpRc,
1982 UINT uFlags,
1983 HFONT hFont)
1984 {
1985 HFONT hOldFont = NULL;
1986 COLORREF OldTextColor;
1987 NONCLIENTMETRICSW nclm;
1988 NTSTATUS Status;
1989 BOOLEAN bDeleteFont = FALSE;
1990 SIZE Size;
1991 BOOL Ret = TRUE;
1992 ULONG fit = 0, Length;
1993 RECTL r = *lpRc;
1994
1995 TRACE("UserDrawCaptionText: %wZ\n", Text);
1996
1997 nclm.cbSize = sizeof(nclm);
1998 if(!UserSystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &nclm, 0))
1999 {
2000 ERR("UserSystemParametersInfo() failed!\n");
2001 return FALSE;
2002 }
2003
2004 if (!hFont)
2005 {
2006 if(uFlags & DC_SMALLCAP)
2007 Status = TextIntCreateFontIndirect(&nclm.lfSmCaptionFont, &hFont);
2008 else
2009 Status = TextIntCreateFontIndirect(&nclm.lfCaptionFont, &hFont);
2010
2011 if(!NT_SUCCESS(Status))
2012 {
2013 ERR("TextIntCreateFontIndirect() failed! Status: 0x%x\n", Status);
2014 return FALSE;
2015 }
2016
2017 bDeleteFont = TRUE;
2018 }
2019
2020 IntGdiSetBkMode(hDc, TRANSPARENT);
2021
2022 hOldFont = NtGdiSelectFont(hDc, hFont);
2023
2024 if(uFlags & DC_INBUTTON)
2025 OldTextColor = IntGdiSetTextColor(hDc, IntGetSysColor(COLOR_BTNTEXT));
2026 else
2027 OldTextColor = IntGdiSetTextColor(hDc,
2028 IntGetSysColor(uFlags & DC_ACTIVE ? COLOR_CAPTIONTEXT : COLOR_INACTIVECAPTIONTEXT));
2029
2030 // Adjust for system menu.
2031 if (pWnd && pWnd->style & WS_SYSMENU)
2032 {
2033 r.right -= UserGetSystemMetrics(SM_CYCAPTION) - 1;
2034 if ((pWnd->style & (WS_MAXIMIZEBOX | WS_MINIMIZEBOX)) && !(pWnd->ExStyle & WS_EX_TOOLWINDOW))
2035 {
2036 r.right -= UserGetSystemMetrics(SM_CXSIZE) + 1;
2037 r.right -= UserGetSystemMetrics(SM_CXSIZE) + 1;
2038 }
2039 }
2040
2041 GreGetTextExtentExW(hDc, Text->Buffer, Text->Length/sizeof(WCHAR), r.right - r.left, &fit, 0, &Size, 0);
2042
2043 Length = (Text->Length/sizeof(WCHAR) == fit ? fit : fit+1);
2044
2045 if (Text->Length/sizeof(WCHAR) > Length)
2046 {
2047 Ret = FALSE;
2048 }
2049
2050 if (Ret)
2051 { // Faster while in setup.
2052 GreExtTextOutW( hDc,
2053 lpRc->left,
2054 lpRc->top + (lpRc->bottom - lpRc->top) / 2 - Size.cy / 2, // DT_SINGLELINE && DT_VCENTER
2055 ETO_CLIPPED,
2056 (RECTL *)lpRc,
2057 Text->Buffer,
2058 Length,
2059 NULL,
2060 0 );
2061 }
2062 else
2063 {
2064 DrawTextW( hDc,
2065 Text->Buffer,
2066 Text->Length/sizeof(WCHAR),
2067 (RECTL *)&r,
2068 DT_END_ELLIPSIS|DT_SINGLELINE|DT_VCENTER|DT_NOPREFIX|DT_LEFT);
2069 }
2070
2071 IntGdiSetTextColor(hDc, OldTextColor);
2072
2073 if (hOldFont)
2074 NtGdiSelectFont(hDc, hOldFont);
2075
2076 if (bDeleteFont)
2077 GreDeleteObject(hFont);
2078
2079 return Ret;
2080 }
2081
2082 //
2083 // This draws Buttons, Icons and Text...
2084 //
2085 BOOL UserDrawCaption(
2086 PWND pWnd,
2087 HDC hDc,
2088 RECTL *lpRc,
2089 HFONT hFont,
2090 HICON hIcon,
2091 const PUNICODE_STRING Str,
2092 UINT uFlags)
2093 {
2094 BOOL Ret = FALSE;
2095 HBRUSH hBgBrush, hOldBrush = NULL;
2096 RECTL Rect = *lpRc;
2097 BOOL HasIcon;
2098
2099 RECTL_vMakeWellOrdered(lpRc);
2100
2101 /* Determine whether the icon needs to be displayed */
2102 if (!hIcon && pWnd != NULL)
2103 {
2104 HasIcon = (uFlags & DC_ICON) && !(uFlags & DC_SMALLCAP) &&
2105 (pWnd->style & WS_SYSMENU) && !(pWnd->ExStyle & WS_EX_TOOLWINDOW);
2106 }
2107 else
2108 HasIcon = (hIcon != NULL);
2109
2110 // Draw the caption background
2111 if((uFlags & DC_GRADIENT) && !(uFlags & DC_INBUTTON))
2112 {
2113 static GRADIENT_RECT gcap = {0, 1};
2114 TRIVERTEX Vertices[2];
2115 COLORREF Colors[2];
2116
2117 Colors[0] = IntGetSysColor((uFlags & DC_ACTIVE) ?
2118 COLOR_ACTIVECAPTION : COLOR_INACTIVECAPTION);
2119
2120 Colors[1] = IntGetSysColor((uFlags & DC_ACTIVE) ?
2121 COLOR_GRADIENTACTIVECAPTION : COLOR_GRADIENTINACTIVECAPTION);
2122
2123 Vertices[0].x = Rect.left;
2124 Vertices[0].y = Rect.top;
2125 Vertices[0].Red = (WORD)Colors[0]<<8;
2126 Vertices[0].Green = (WORD)Colors[0] & 0xFF00;
2127 Vertices[0].Blue = (WORD)(Colors[0]>>8) & 0xFF00;
2128 Vertices[0].Alpha = 0;
2129
2130 Vertices[1].x = Rect.right;
2131 Vertices[1].y = Rect.bottom;
2132 Vertices[1].Red = (WORD)Colors[1]<<8;
2133 Vertices[1].Green = (WORD)Colors[1] & 0xFF00;
2134 Vertices[1].Blue = (WORD)(Colors[1]>>8) & 0xFF00;
2135 Vertices[1].Alpha = 0;
2136
2137 if(!GreGradientFill(hDc, Vertices, 2, &gcap, 1, GRADIENT_FILL_RECT_H))
2138 {
2139 ERR("GreGradientFill() failed!\n");
2140 goto cleanup;
2141 }
2142 }
2143 else
2144 {
2145 if(uFlags & DC_INBUTTON)
2146 hBgBrush = IntGetSysColorBrush(COLOR_3DFACE);
2147 else if(uFlags & DC_ACTIVE)
2148 hBgBrush = IntGetSysColorBrush(COLOR_ACTIVECAPTION);
2149 else
2150 hBgBrush = IntGetSysColorBrush(COLOR_INACTIVECAPTION);
2151
2152 hOldBrush = NtGdiSelectBrush(hDc, hBgBrush);
2153
2154 if(!hOldBrush)
2155 {
2156 ERR("NtGdiSelectBrush() failed!\n");
2157 goto cleanup;
2158 }
2159
2160 if(!NtGdiPatBlt(hDc, Rect.left, Rect.top,
2161 Rect.right - Rect.left,
2162 Rect.bottom - Rect.top,
2163 PATCOPY))
2164 {
2165 ERR("NtGdiPatBlt() failed!\n");
2166 goto cleanup;
2167 }
2168 }
2169
2170 /* Draw icon */
2171 if (HasIcon)
2172 {
2173 PCURICON_OBJECT pIcon = NULL;
2174
2175 if (hIcon)
2176 {
2177 pIcon = UserGetCurIconObject(hIcon);
2178 }
2179 else if (pWnd)
2180 {
2181 pIcon = NC_IconForWindow(pWnd);
2182 // FIXME: NC_IconForWindow should reference it for us */
2183 if (pIcon)
2184 UserReferenceObject(pIcon);
2185 }
2186
2187 if (pIcon)
2188 {
2189 LONG cx = UserGetSystemMetrics(SM_CXSMICON);
2190 LONG cy = UserGetSystemMetrics(SM_CYSMICON);
2191 LONG x = Rect.left - cx/2 + 1 + (Rect.bottom - Rect.top)/2; // this is really what Window does
2192 LONG y = (Rect.top + Rect.bottom - cy)/2; // center
2193 UserDrawIconEx(hDc, x, y, pIcon, cx, cy, 0, NULL, DI_NORMAL);
2194 UserDereferenceObject(pIcon);
2195 }
2196 else
2197 {
2198 HasIcon = FALSE;
2199 }
2200 }
2201
2202 if (HasIcon)
2203 Rect.left += Rect.bottom - Rect.top;
2204
2205 if((uFlags & DC_TEXT))
2206 {
2207 BOOL Set = FALSE;
2208 Rect.left += 2;
2209
2210 if (Str)
2211 Set = UserDrawCaptionText(pWnd, hDc, Str, &Rect, uFlags, hFont);
2212 else if (pWnd != NULL) // FIXME: Windows does not do that
2213 {
2214 UNICODE_STRING ustr;
2215 ustr.Buffer = pWnd->strName.Buffer; // FIXME: LARGE_STRING truncated!
2216 ustr.Length = (USHORT)min(pWnd->strName.Length, MAXUSHORT);
2217 ustr.MaximumLength = (USHORT)min(pWnd->strName.MaximumLength, MAXUSHORT);
2218 Set = UserDrawCaptionText(pWnd, hDc, &ustr, &Rect, uFlags, hFont);
2219 }
2220 if (pWnd)
2221 {
2222 if (Set)
2223 pWnd->state2 &= ~WNDS2_CAPTIONTEXTTRUNCATED;
2224 else
2225 pWnd->state2 |= WNDS2_CAPTIONTEXTTRUNCATED;
2226 }
2227 }
2228
2229 Ret = TRUE;
2230
2231 cleanup:
2232 if (hOldBrush) NtGdiSelectBrush(hDc, hOldBrush);
2233
2234 return Ret;
2235 }
2236
2237 INT
2238 FASTCALL
2239 UserRealizePalette(HDC hdc)
2240 {
2241 HWND hWnd, hWndDesktop;
2242 DWORD Ret;
2243
2244 Ret = IntGdiRealizePalette(hdc);
2245 if (Ret) // There was a change.
2246 {
2247 hWnd = IntWindowFromDC(hdc);
2248 if (hWnd) // Send broadcast if dc is associated with a window.
2249 { // FYI: Thread locked in CallOneParam.
2250 hWndDesktop = IntGetDesktopWindow();
2251 if ( hWndDesktop != hWnd )
2252 {
2253 PWND pWnd = UserGetWindowObject(hWndDesktop);
2254 ERR("RealizePalette Desktop.");
2255 hdc = UserGetWindowDC(pWnd);
2256 IntPaintDesktop(hdc);
2257 UserReleaseDC(pWnd,hdc,FALSE);
2258 }
2259 UserSendNotifyMessage((HWND)HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0);
2260 }
2261 }
2262 return Ret;
2263 }
2264
2265 BOOL
2266 APIENTRY
2267 NtUserDrawCaptionTemp(
2268 HWND hWnd,
2269 HDC hDC,
2270 LPCRECT lpRc,
2271 HFONT hFont,
2272 HICON hIcon,
2273 const PUNICODE_STRING str,
2274 UINT uFlags)
2275 {
2276 PWND pWnd = NULL;
2277 UNICODE_STRING SafeStr = {0};
2278 NTSTATUS Status = STATUS_SUCCESS;
2279 RECTL SafeRect;
2280 BOOL Ret;
2281
2282 UserEnterExclusive();
2283
2284 if (hWnd != NULL)
2285 {
2286 if(!(pWnd = UserGetWindowObject(hWnd)))
2287 {
2288 UserLeave();
2289 return FALSE;
2290 }
2291 }
2292
2293 _SEH2_TRY
2294 {
2295 ProbeForRead(lpRc, sizeof(RECTL), sizeof(ULONG));
2296 RtlCopyMemory(&SafeRect, lpRc, sizeof(RECTL));
2297 if (str != NULL)
2298 {
2299 SafeStr = ProbeForReadUnicodeString(str);
2300 if (SafeStr.Length != 0)
2301 {
2302 ProbeForRead( SafeStr.Buffer,
2303 SafeStr.Length,
2304 sizeof(WCHAR));
2305 }
2306 }
2307 }
2308 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
2309 {
2310 Status = _SEH2_GetExceptionCode();
2311 }
2312 _SEH2_END;
2313
2314 if (Status != STATUS_SUCCESS)
2315 {
2316 SetLastNtError(Status);
2317 UserLeave();
2318 return FALSE;
2319 }
2320
2321 if (str != NULL)
2322 Ret = UserDrawCaption(pWnd, hDC, &SafeRect, hFont, hIcon, &SafeStr, uFlags);
2323 else
2324 {
2325 if ( RECTL_bIsEmptyRect(&SafeRect) && hFont == 0 && hIcon == 0 )
2326 {
2327 Ret = TRUE;
2328 if (uFlags & DC_DRAWCAPTIONMD)
2329 {
2330 ERR("NC Caption Mode\n");
2331 UserDrawCaptionBar(pWnd, hDC, uFlags);
2332 goto Exit;
2333 }
2334 else if (uFlags & DC_DRAWFRAMEMD)
2335 {
2336 ERR("NC Paint Mode\n");
2337 NC_DoNCPaint(pWnd, hDC, uFlags); // Update Menus too!
2338 goto Exit;
2339 }
2340 }
2341 Ret = UserDrawCaption(pWnd, hDC, &SafeRect, hFont, hIcon, NULL, uFlags);
2342 }
2343 Exit:
2344 UserLeave();
2345 return Ret;
2346 }
2347
2348 BOOL
2349 APIENTRY
2350 NtUserDrawCaption(HWND hWnd,
2351 HDC hDC,
2352 LPCRECT lpRc,
2353 UINT uFlags)
2354 {
2355 return NtUserDrawCaptionTemp(hWnd, hDC, lpRc, 0, 0, NULL, uFlags);
2356 }
2357
2358 BOOL
2359 APIENTRY
2360 NtUserInvalidateRect(
2361 HWND hWnd,
2362 CONST RECT *lpUnsafeRect,
2363 BOOL bErase)
2364 {
2365 UINT flags = RDW_INVALIDATE | (bErase ? RDW_ERASE : 0);
2366 if (!hWnd)
2367 {
2368 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
2369 lpUnsafeRect = NULL;
2370 }
2371 return NtUserRedrawWindow(hWnd, lpUnsafeRect, NULL, flags);
2372 }
2373
2374 BOOL
2375 APIENTRY
2376 NtUserInvalidateRgn(
2377 HWND hWnd,
2378 HRGN hRgn,
2379 BOOL bErase)
2380 {
2381 if (!hWnd)
2382 {
2383 EngSetLastError( ERROR_INVALID_WINDOW_HANDLE );
2384 return FALSE;
2385 }
2386 return NtUserRedrawWindow(hWnd, NULL, hRgn, RDW_INVALIDATE | (bErase? RDW_ERASE : 0));
2387 }
2388
2389 BOOL
2390 APIENTRY
2391 NtUserPrintWindow(
2392 HWND hwnd,
2393 HDC hdcBlt,
2394 UINT nFlags)
2395 {
2396 PWND Window;
2397 BOOL Ret = FALSE;
2398
2399 UserEnterExclusive();
2400
2401 if (hwnd)
2402 {
2403 if (!(Window = UserGetWindowObject(hwnd)) || // FIXME:
2404 Window == UserGetDesktopWindow() || // pWnd->fnid == FNID_DESKTOP
2405 Window == UserGetMessageWindow() ) // pWnd->fnid == FNID_MESSAGEWND
2406 {
2407 goto Exit;
2408 }
2409
2410 if ( Window )
2411 {
2412 /* Validate flags and check it as a mask for 0 or 1. */
2413 if ( (nFlags & PW_CLIENTONLY) == nFlags)
2414 Ret = IntPrintWindow( Window, hdcBlt, nFlags);
2415 else
2416 EngSetLastError(ERROR_INVALID_PARAMETER);
2417 }
2418 }
2419 Exit:
2420 UserLeave();
2421 return Ret;
2422 }
2423
2424 /* ValidateRect gets redirected to NtUserValidateRect:
2425 http://blog.csdn.net/ntdll/archive/2005/10/19/509299.aspx */
2426 BOOL
2427 APIENTRY
2428 NtUserValidateRect(
2429 HWND hWnd,
2430 const RECT *lpRect)
2431 {
2432 UINT flags = RDW_VALIDATE;
2433 if (!hWnd)
2434 {
2435 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
2436 lpRect = NULL;
2437 }
2438 return NtUserRedrawWindow(hWnd, lpRect, NULL, flags);
2439 }
2440
2441 /* EOF */