309dd6499397f54e29beed0598573c6810f478d1
[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 //
1161 // Hax around internal painting of windows.
1162 //
1163 VOID FASTCALL
1164 IntPaintWindow( PWND Window )
1165 {
1166 // Handle normal painting.
1167 co_IntPaintWindows( Window, RDW_NOCHILDREN, FALSE );
1168
1169 // Hack to prevent more spamming from misbehaving application.
1170 // Handle it like a begin/end paint
1171 if (Window->hrgnUpdate != NULL)
1172 {
1173 ERR("HAX hrgnUpdate not NULL! Dec Paint Count!\n");
1174 MsqDecPaintCountQueue(Window->head.pti);
1175 IntGdiSetRegionOwner(Window->hrgnUpdate, GDI_OBJ_HMGR_POWNED);
1176 GreDeleteObject(Window->hrgnUpdate);
1177 Window->state &= ~WNDS_UPDATEDIRTY;
1178 Window->hrgnUpdate = NULL;
1179 }
1180 else
1181 {
1182 if (!(Window->state & WNDS_INTERNALPAINT))
1183 {
1184 ERR("HAX WNDS_INTERNALPAINT not set! Dec Paint Count!\n");
1185 MsqDecPaintCountQueue(Window->head.pti);
1186 }
1187 }
1188 }
1189
1190 BOOL FASTCALL
1191 IntGetPaintMessage(
1192 PWND Window,
1193 UINT MsgFilterMin,
1194 UINT MsgFilterMax,
1195 PTHREADINFO Thread,
1196 MSG *Message,
1197 BOOL Remove)
1198 {
1199 PWND PaintWnd, StartWnd;
1200
1201 if ((MsgFilterMin != 0 || MsgFilterMax != 0) &&
1202 (MsgFilterMin > WM_PAINT || MsgFilterMax < WM_PAINT))
1203 return FALSE;
1204
1205 if (Thread->TIF_flags & TIF_SYSTEMTHREAD )
1206 {
1207 ERR("WM_PAINT is in a System Thread!\n");
1208 }
1209
1210 StartWnd = UserGetDesktopWindow();
1211 PaintWnd = IntFindWindowToRepaint(StartWnd, Thread);
1212
1213 Message->hwnd = PaintWnd ? UserHMGetHandle(PaintWnd) : NULL;
1214
1215 if (Message->hwnd == NULL && Thread->cPaintsReady)
1216 {
1217 // Find note in window.c:"PAINTING BUG".
1218 ERR("WARNING SOMETHING HAS GONE WRONG: Thread marked as containing dirty windows, but no dirty windows found! Counts %u\n",Thread->cPaintsReady);
1219 /* Hack to stop spamming the debug log ! */
1220 Thread->cPaintsReady = 0;
1221 return FALSE;
1222 }
1223
1224 if (Message->hwnd == NULL)
1225 return FALSE;
1226
1227 if (!(Window == NULL ||
1228 PaintWnd == Window ||
1229 IntIsChildWindow(Window, PaintWnd))) /* check that it is a child of the specified parent */
1230 return FALSE;
1231
1232 if (PaintWnd->state & WNDS_INTERNALPAINT)
1233 {
1234 PaintWnd->state &= ~WNDS_INTERNALPAINT;
1235 if (!PaintWnd->hrgnUpdate)
1236 MsqDecPaintCountQueue(Thread);
1237 }
1238 PaintWnd->state2 &= ~WNDS2_STARTPAINT;
1239 PaintWnd->state &= ~WNDS_UPDATEDIRTY;
1240
1241 Window = PaintWnd;
1242 while( Window && Window != UserGetDesktopWindow())
1243 {
1244 // Role back and check for clip children, do not set if any.
1245 if (Window->spwndParent && !(Window->spwndParent->style & WS_CLIPCHILDREN))
1246 {
1247 PaintWnd->state2 |= WNDS2_WMPAINTSENT;
1248 }
1249 Window = Window->spwndParent;
1250 }
1251
1252 Message->wParam = Message->lParam = 0;
1253 Message->message = WM_PAINT;
1254 return TRUE;
1255 }
1256
1257 BOOL
1258 FASTCALL
1259 IntPrintWindow(
1260 PWND pwnd,
1261 HDC hdcBlt,
1262 UINT nFlags)
1263 {
1264 HDC hdcSrc;
1265 INT cx, cy, xSrc, ySrc;
1266
1267 if ( nFlags & PW_CLIENTONLY)
1268 {
1269 cx = pwnd->rcClient.right - pwnd->rcClient.left;
1270 cy = pwnd->rcClient.bottom - pwnd->rcClient.top;
1271 xSrc = pwnd->rcClient.left - pwnd->rcWindow.left;
1272 ySrc = pwnd->rcClient.top - pwnd->rcWindow.top;
1273 }
1274 else
1275 {
1276 cx = pwnd->rcWindow.right - pwnd->rcWindow.left;
1277 cy = pwnd->rcWindow.bottom - pwnd->rcWindow.top;
1278 xSrc = 0;
1279 ySrc = 0;
1280 }
1281
1282 // TODO: Setup Redirection for Print.
1283 return FALSE;
1284
1285 /* Update the window just incase. */
1286 co_IntUpdateWindows( pwnd, RDW_ALLCHILDREN, FALSE);
1287
1288 hdcSrc = UserGetDCEx( pwnd, NULL, DCX_CACHE|DCX_WINDOW);
1289 /* Print window to printer context. */
1290 NtGdiBitBlt( hdcBlt,
1291 0,
1292 0,
1293 cx,
1294 cy,
1295 hdcSrc,
1296 xSrc,
1297 ySrc,
1298 SRCCOPY,
1299 0,
1300 0);
1301
1302 UserReleaseDC( pwnd, hdcSrc, FALSE);
1303
1304 // TODO: Release Redirection from Print.
1305
1306 return TRUE;
1307 }
1308
1309 BOOL
1310 FASTCALL
1311 IntFlashWindowEx(PWND pWnd, PFLASHWINFO pfwi)
1312 {
1313 DWORD FlashState;
1314 UINT uCount = pfwi->uCount;
1315 BOOL Activate = FALSE, Ret = FALSE;
1316
1317 ASSERT(pfwi);
1318
1319 FlashState = (DWORD)UserGetProp(pWnd, AtomFlashWndState, TRUE);
1320
1321 if (FlashState == FLASHW_FINISHED)
1322 {
1323 // Cycle has finished, kill timer and set this to Stop.
1324 FlashState |= FLASHW_KILLSYSTIMER;
1325 pfwi->dwFlags = FLASHW_STOP;
1326 }
1327 else
1328 {
1329 if (FlashState)
1330 {
1331 if (pfwi->dwFlags == FLASHW_SYSTIMER)
1332 {
1333 // Called from system timer, restore flags, counts and state.
1334 pfwi->dwFlags = LOWORD(FlashState);
1335 uCount = HIWORD(FlashState);
1336 FlashState = MAKELONG(LOWORD(FlashState),0);
1337 }
1338 else
1339 {
1340 // Clean out the trash! Fix SeaMonkey crash after restart.
1341 FlashState = 0;
1342 }
1343 }
1344
1345 if (FlashState == 0)
1346 { // First time in cycle, setup flash state.
1347 if ( pWnd->state & WNDS_ACTIVEFRAME ||
1348 (pfwi->dwFlags & FLASHW_CAPTION && pWnd->style & (WS_BORDER|WS_DLGFRAME)))
1349 {
1350 FlashState = FLASHW_STARTED|FLASHW_ACTIVE;
1351 }
1352 }
1353
1354 // Set previous window state.
1355 Ret = !!(FlashState & FLASHW_ACTIVE);
1356
1357 if ( (pfwi->dwFlags & FLASHW_TIMERNOFG) == FLASHW_TIMERNOFG &&
1358 gpqForeground == pWnd->head.pti->MessageQueue )
1359 {
1360 // Flashing until foreground, set this to Stop.
1361 pfwi->dwFlags = FLASHW_STOP;
1362 }
1363 }
1364
1365 // Toggle activate flag.
1366 if ( pfwi->dwFlags == FLASHW_STOP )
1367 {
1368 if (gpqForeground && gpqForeground->spwndActive == pWnd)
1369 Activate = TRUE;
1370 else
1371 Activate = FALSE;
1372 }
1373 else
1374 {
1375 Activate = (FlashState & FLASHW_ACTIVE) == 0;
1376 }
1377
1378 if ( pfwi->dwFlags == FLASHW_STOP || pfwi->dwFlags & FLASHW_CAPTION )
1379 {
1380 co_IntSendMessage(UserHMGetHandle(pWnd), WM_NCACTIVATE, Activate, 0);
1381 }
1382
1383 // FIXME: Check for a Stop Sign here.
1384 if ( pfwi->dwFlags & FLASHW_TRAY )
1385 {
1386 // Need some shell work here too.
1387 TRACE("FIXME: Flash window no Tray support!\n");
1388 }
1389
1390 if ( pfwi->dwFlags == FLASHW_STOP )
1391 {
1392 if (FlashState & FLASHW_KILLSYSTIMER)
1393 {
1394 IntKillTimer(pWnd, ID_EVENT_SYSTIMER_FLASHWIN, TRUE);
1395 }
1396
1397 UserRemoveProp(pWnd, AtomFlashWndState, TRUE);
1398 }
1399 else
1400 { // Have a count and started, set timer.
1401 if ( uCount )
1402 {
1403 FlashState |= FLASHW_COUNT;
1404
1405 if (!(Activate ^ !!(FlashState & FLASHW_STARTED)))
1406 uCount--;
1407
1408 if (!(FlashState & FLASHW_KILLSYSTIMER))
1409 pfwi->dwFlags |= FLASHW_TIMER;
1410 }
1411
1412 if (pfwi->dwFlags & FLASHW_TIMER)
1413 {
1414 FlashState |= FLASHW_KILLSYSTIMER;
1415
1416 IntSetTimer( pWnd,
1417 ID_EVENT_SYSTIMER_FLASHWIN,
1418 pfwi->dwTimeout ? pfwi->dwTimeout : gpsi->dtCaretBlink,
1419 SystemTimerProc,
1420 TMRF_SYSTEM );
1421 }
1422
1423 if (FlashState & FLASHW_COUNT && uCount == 0)
1424 {
1425 // Keep spinning? Nothing else to do.
1426 FlashState = FLASHW_FINISHED;
1427 }
1428 else
1429 {
1430 // Save state and flags so this can be restored next time through.
1431 FlashState ^= (FlashState ^ -!!(Activate)) & FLASHW_ACTIVE;
1432 FlashState ^= (FlashState ^ pfwi->dwFlags) & (FLASHW_MASK & ~FLASHW_TIMER);
1433 }
1434 FlashState = MAKELONG(LOWORD(FlashState),uCount);
1435 UserSetProp(pWnd, AtomFlashWndState, (HANDLE)FlashState, TRUE);
1436 }
1437 return Ret;
1438 }
1439
1440 HDC FASTCALL
1441 IntBeginPaint(PWND Window, PPAINTSTRUCT Ps)
1442 {
1443 RECT Rect;
1444 INT type;
1445 BOOL Erase = FALSE;
1446
1447 co_UserHideCaret(Window);
1448
1449 Window->state2 |= WNDS2_STARTPAINT;
1450 Window->state &= ~WNDS_PAINTNOTPROCESSED;
1451
1452 if (Window->state & WNDS_SENDNCPAINT)
1453 {
1454 HRGN hRgn;
1455 // Application can keep update dirty.
1456 do
1457 {
1458 Window->state &= ~WNDS_UPDATEDIRTY;
1459 hRgn = IntGetNCUpdateRgn(Window, FALSE);
1460 IntSendNCPaint(Window, hRgn);
1461 if (hRgn > HRGN_WINDOW && GreIsHandleValid(hRgn))
1462 {
1463 /* NOTE: The region can already be deleted! */
1464 GreDeleteObject(hRgn);
1465 }
1466 }
1467 while(Window->state & WNDS_UPDATEDIRTY);
1468 }
1469 else
1470 {
1471 Window->state &= ~WNDS_UPDATEDIRTY;
1472 }
1473
1474 RtlZeroMemory(Ps, sizeof(PAINTSTRUCT));
1475
1476 if (Window->state2 & WNDS2_ENDPAINTINVALIDATE)
1477 {
1478 ERR("BP: Another thread invalidated this window\n");
1479 }
1480
1481 Ps->hdc = UserGetDCEx( Window,
1482 Window->hrgnUpdate,
1483 DCX_INTERSECTRGN | DCX_USESTYLE);
1484 if (!Ps->hdc)
1485 {
1486 return NULL;
1487 }
1488
1489 // If set, always clear flags out due to the conditions later on for sending the message.
1490 if (Window->state & WNDS_SENDERASEBACKGROUND)
1491 {
1492 Window->state &= ~(WNDS_SENDERASEBACKGROUND|WNDS_ERASEBACKGROUND);
1493 Erase = TRUE;
1494 }
1495
1496 if (Window->hrgnUpdate != NULL)
1497 {
1498 MsqDecPaintCountQueue(Window->head.pti);
1499 IntGdiSetRegionOwner(Window->hrgnUpdate, GDI_OBJ_HMGR_POWNED);
1500 /* The region is part of the dc now and belongs to the process! */
1501 Window->hrgnUpdate = NULL;
1502 }
1503 else
1504 {
1505 if (Window->state & WNDS_INTERNALPAINT)
1506 MsqDecPaintCountQueue(Window->head.pti);
1507 }
1508
1509 type = GdiGetClipBox(Ps->hdc, &Ps->rcPaint);
1510
1511 IntGetClientRect(Window, &Rect);
1512
1513 Window->state &= ~WNDS_INTERNALPAINT;
1514
1515 if ( Erase && // Set to erase,
1516 type != NULLREGION && // don't erase if the clip box is empty,
1517 (!(Window->pcls->style & CS_PARENTDC) || // not parent dc or
1518 RECTL_bIntersectRect( &Rect, &Rect, &Ps->rcPaint) ) ) // intersecting.
1519 {
1520 Ps->fErase = !co_IntSendMessage(UserHMGetHandle(Window), WM_ERASEBKGND, (WPARAM)Ps->hdc, 0);
1521 if ( Ps->fErase )
1522 {
1523 Window->state |= (WNDS_SENDERASEBACKGROUND|WNDS_ERASEBACKGROUND);
1524 }
1525 }
1526 else
1527 {
1528 Ps->fErase = FALSE;
1529 }
1530
1531 IntSendChildNCPaint(Window);
1532
1533 return Ps->hdc;
1534 }
1535
1536 BOOL FASTCALL
1537 IntEndPaint(PWND Wnd, PPAINTSTRUCT Ps)
1538 {
1539 HDC hdc = NULL;
1540
1541 hdc = Ps->hdc;
1542
1543 UserReleaseDC(Wnd, hdc, TRUE);
1544
1545 if (Wnd->state2 & WNDS2_ENDPAINTINVALIDATE)
1546 {
1547 ERR("EP: Another thread invalidated this window\n");
1548 Wnd->state2 &= ~WNDS2_ENDPAINTINVALIDATE;
1549 }
1550
1551 Wnd->state2 &= ~(WNDS2_WMPAINTSENT|WNDS2_STARTPAINT);
1552
1553 co_UserShowCaret(Wnd);
1554
1555 return TRUE;
1556 }
1557
1558 /* PUBLIC FUNCTIONS ***********************************************************/
1559
1560 /*
1561 * NtUserBeginPaint
1562 *
1563 * Status
1564 * @implemented
1565 */
1566
1567 HDC APIENTRY
1568 NtUserBeginPaint(HWND hWnd, PAINTSTRUCT* UnsafePs)
1569 {
1570 PWND Window = NULL;
1571 PAINTSTRUCT Ps;
1572 NTSTATUS Status;
1573 HDC hDC;
1574 USER_REFERENCE_ENTRY Ref;
1575 DECLARE_RETURN(HDC);
1576
1577 TRACE("Enter NtUserBeginPaint\n");
1578 UserEnterExclusive();
1579
1580 if (!(Window = UserGetWindowObject(hWnd)))
1581 {
1582 RETURN( NULL);
1583 }
1584
1585 UserRefObjectCo(Window, &Ref);
1586
1587 hDC = IntBeginPaint(Window, &Ps);
1588
1589 Status = MmCopyToCaller(UnsafePs, &Ps, sizeof(PAINTSTRUCT));
1590 if (! NT_SUCCESS(Status))
1591 {
1592 SetLastNtError(Status);
1593 RETURN(NULL);
1594 }
1595
1596 RETURN(hDC);
1597
1598 CLEANUP:
1599 if (Window) UserDerefObjectCo(Window);
1600
1601 TRACE("Leave NtUserBeginPaint, ret=%p\n",_ret_);
1602 UserLeave();
1603 END_CLEANUP;
1604
1605 }
1606
1607 /*
1608 * NtUserEndPaint
1609 *
1610 * Status
1611 * @implemented
1612 */
1613
1614 BOOL APIENTRY
1615 NtUserEndPaint(HWND hWnd, CONST PAINTSTRUCT* pUnsafePs)
1616 {
1617 NTSTATUS Status = STATUS_SUCCESS;
1618 PWND Window = NULL;
1619 PAINTSTRUCT Ps;
1620 USER_REFERENCE_ENTRY Ref;
1621 DECLARE_RETURN(BOOL);
1622
1623 TRACE("Enter NtUserEndPaint\n");
1624 UserEnterExclusive();
1625
1626 if (!(Window = UserGetWindowObject(hWnd)))
1627 {
1628 RETURN(FALSE);
1629 }
1630
1631 UserRefObjectCo(Window, &Ref); // Here for the exception.
1632
1633 _SEH2_TRY
1634 {
1635 ProbeForRead(pUnsafePs, sizeof(*pUnsafePs), 1);
1636 RtlCopyMemory(&Ps, pUnsafePs, sizeof(PAINTSTRUCT));
1637 }
1638 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1639 {
1640 Status = _SEH2_GetExceptionCode();
1641 }
1642 _SEH2_END
1643 if (!NT_SUCCESS(Status))
1644 {
1645 RETURN(FALSE);
1646 }
1647
1648 RETURN(IntEndPaint(Window, &Ps));
1649
1650 CLEANUP:
1651 if (Window) UserDerefObjectCo(Window);
1652
1653 TRACE("Leave NtUserEndPaint, ret=%i\n",_ret_);
1654 UserLeave();
1655 END_CLEANUP;
1656 }
1657
1658 /*
1659 * @implemented
1660 */
1661 BOOL APIENTRY
1662 NtUserFlashWindowEx(IN PFLASHWINFO pfwi)
1663 {
1664 PWND pWnd;
1665 FLASHWINFO finfo = {0};
1666 BOOL Ret = FALSE;
1667
1668 UserEnterExclusive();
1669
1670 _SEH2_TRY
1671 {
1672 ProbeForRead(pfwi, sizeof(FLASHWINFO), 1);
1673 RtlCopyMemory(&finfo, pfwi, sizeof(FLASHWINFO));
1674 }
1675 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1676 {
1677 SetLastNtError(_SEH2_GetExceptionCode());
1678 _SEH2_YIELD(goto Exit);
1679 }
1680 _SEH2_END
1681
1682 if (!( pWnd = ValidateHwndNoErr(finfo.hwnd)) ||
1683 finfo.cbSize != sizeof(FLASHWINFO) ||
1684 finfo.dwFlags & ~(FLASHW_ALL|FLASHW_TIMER|FLASHW_TIMERNOFG) )
1685 {
1686 EngSetLastError(ERROR_INVALID_PARAMETER);
1687 goto Exit;
1688 }
1689
1690 Ret = IntFlashWindowEx(pWnd, &finfo);
1691
1692 Exit:
1693 UserLeave();
1694 return Ret;
1695 }
1696
1697 /*
1698 GetUpdateRgn, this fails the same as the old one.
1699 */
1700 INT FASTCALL
1701 co_UserGetUpdateRgn(PWND Window, HRGN hRgn, BOOL bErase)
1702 {
1703 int RegionType;
1704 BOOL Type;
1705 RECTL Rect;
1706
1707 ASSERT_REFS_CO(Window);
1708
1709 if (bErase)
1710 {
1711 USER_REFERENCE_ENTRY Ref;
1712 UserRefObjectCo(Window, &Ref);
1713 co_IntPaintWindows(Window, RDW_NOCHILDREN, FALSE);
1714 UserDerefObjectCo(Window);
1715 }
1716
1717 Window->state &= ~WNDS_UPDATEDIRTY;
1718
1719 if (Window->hrgnUpdate == NULL)
1720 {
1721 NtGdiSetRectRgn(hRgn, 0, 0, 0, 0);
1722 return NULLREGION;
1723 }
1724
1725 Rect = Window->rcClient;
1726 Type = IntIntersectWithParents(Window, &Rect);
1727
1728 if (Window->hrgnUpdate == HRGN_WINDOW)
1729 {
1730 // Trap it out.
1731 ERR("GURn: Caller is passing Window Region 1\n");
1732 if (!Type)
1733 {
1734 NtGdiSetRectRgn(hRgn, 0, 0, 0, 0);
1735 return NULLREGION;
1736 }
1737
1738 RegionType = SIMPLEREGION;
1739
1740 if (Window != UserGetDesktopWindow()) // Window->fnid == FNID_DESKTOP
1741 {
1742 RECTL_vOffsetRect(&Rect,
1743 -Window->rcClient.left,
1744 -Window->rcClient.top);
1745 }
1746 GreSetRectRgnIndirect(hRgn, &Rect);
1747 }
1748 else
1749 {
1750 HRGN hrgnTemp = GreCreateRectRgnIndirect(&Rect);
1751
1752 RegionType = NtGdiCombineRgn(hRgn, hrgnTemp, Window->hrgnUpdate, RGN_AND);
1753
1754 if (RegionType == ERROR || RegionType == NULLREGION)
1755 {
1756 if (hrgnTemp) GreDeleteObject(hrgnTemp);
1757 NtGdiSetRectRgn(hRgn, 0, 0, 0, 0);
1758 return RegionType;
1759 }
1760
1761 if (Window != UserGetDesktopWindow()) // Window->fnid == FNID_DESKTOP
1762 {
1763 NtGdiOffsetRgn(hRgn,
1764 -Window->rcClient.left,
1765 -Window->rcClient.top);
1766 }
1767 if (hrgnTemp) GreDeleteObject(hrgnTemp);
1768 }
1769 return RegionType;
1770 }
1771
1772 BOOL FASTCALL
1773 co_UserGetUpdateRect(PWND Window, PRECT pRect, BOOL bErase)
1774 {
1775 INT RegionType;
1776 BOOL Ret = TRUE;
1777
1778 if (bErase)
1779 {
1780 USER_REFERENCE_ENTRY Ref;
1781 UserRefObjectCo(Window, &Ref);
1782 co_IntPaintWindows(Window, RDW_NOCHILDREN, FALSE);
1783 UserDerefObjectCo(Window);
1784 }
1785
1786 Window->state &= ~WNDS_UPDATEDIRTY;
1787
1788 if (Window->hrgnUpdate == NULL)
1789 {
1790 pRect->left = pRect->top = pRect->right = pRect->bottom = 0;
1791 Ret = FALSE;
1792 }
1793 else
1794 {
1795 /* Get the update region bounding box. */
1796 if (Window->hrgnUpdate == HRGN_WINDOW)
1797 {
1798 *pRect = Window->rcClient;
1799 ERR("GURt: Caller is retrieving Window Region 1\n");
1800 }
1801 else
1802 {
1803 RegionType = IntGdiGetRgnBox(Window->hrgnUpdate, pRect);
1804
1805 if (RegionType != ERROR && RegionType != NULLREGION)
1806 RECTL_bIntersectRect(pRect, pRect, &Window->rcClient);
1807 }
1808
1809 if (IntIntersectWithParents(Window, pRect))
1810 {
1811 if (Window != UserGetDesktopWindow()) // Window->fnid == FNID_DESKTOP
1812 {
1813 RECTL_vOffsetRect(pRect,
1814 -Window->rcClient.left,
1815 -Window->rcClient.top);
1816 }
1817 if (Window->pcls->style & CS_OWNDC)
1818 {
1819 HDC hdc;
1820 //DWORD layout;
1821 hdc = UserGetDCEx(Window, NULL, DCX_USESTYLE);
1822 //layout = NtGdiSetLayout(hdc, -1, 0);
1823 //IntMapWindowPoints( 0, Window, (LPPOINT)pRect, 2 );
1824 GreDPtoLP( hdc, (LPPOINT)pRect, 2 );
1825 //NtGdiSetLayout(hdc, -1, layout);
1826 UserReleaseDC(Window, hdc, FALSE);
1827 }
1828 }
1829 else
1830 {
1831 pRect->left = pRect->top = pRect->right = pRect->bottom = 0;
1832 }
1833 }
1834 return Ret;
1835 }
1836
1837 /*
1838 * NtUserGetUpdateRgn
1839 *
1840 * Status
1841 * @implemented
1842 */
1843
1844 INT APIENTRY
1845 NtUserGetUpdateRgn(HWND hWnd, HRGN hRgn, BOOL bErase)
1846 {
1847 DECLARE_RETURN(INT);
1848 PWND Window;
1849 INT ret;
1850
1851 TRACE("Enter NtUserGetUpdateRgn\n");
1852 UserEnterExclusive();
1853
1854 if (!(Window = UserGetWindowObject(hWnd)))
1855 {
1856 RETURN(ERROR);
1857 }
1858
1859 ret = co_UserGetUpdateRgn(Window, hRgn, bErase);
1860
1861 RETURN(ret);
1862
1863 CLEANUP:
1864 TRACE("Leave NtUserGetUpdateRgn, ret=%i\n",_ret_);
1865 UserLeave();
1866 END_CLEANUP;
1867 }
1868
1869 /*
1870 * NtUserGetUpdateRect
1871 *
1872 * Status
1873 * @implemented
1874 */
1875
1876 BOOL APIENTRY
1877 NtUserGetUpdateRect(HWND hWnd, LPRECT UnsafeRect, BOOL bErase)
1878 {
1879 PWND Window;
1880 RECTL Rect;
1881 NTSTATUS Status;
1882 BOOL Ret;
1883 DECLARE_RETURN(BOOL);
1884
1885 TRACE("Enter NtUserGetUpdateRect\n");
1886 UserEnterExclusive();
1887
1888 if (!(Window = UserGetWindowObject(hWnd)))
1889 {
1890 RETURN(FALSE);
1891 }
1892
1893 Ret = co_UserGetUpdateRect(Window, &Rect, bErase);
1894
1895 if (UnsafeRect != NULL)
1896 {
1897 Status = MmCopyToCaller(UnsafeRect, &Rect, sizeof(RECTL));
1898 if (!NT_SUCCESS(Status))
1899 {
1900 EngSetLastError(ERROR_INVALID_PARAMETER);
1901 RETURN(FALSE);
1902 }
1903 }
1904
1905 RETURN(Ret);
1906
1907 CLEANUP:
1908 TRACE("Leave NtUserGetUpdateRect, ret=%i\n",_ret_);
1909 UserLeave();
1910 END_CLEANUP;
1911 }
1912
1913 /*
1914 * NtUserRedrawWindow
1915 *
1916 * Status
1917 * @implemented
1918 */
1919
1920 BOOL APIENTRY
1921 NtUserRedrawWindow(
1922 HWND hWnd,
1923 CONST RECT *lprcUpdate,
1924 HRGN hrgnUpdate,
1925 UINT flags)
1926 {
1927 RECTL SafeUpdateRect;
1928 PWND Wnd;
1929 BOOL Ret;
1930 USER_REFERENCE_ENTRY Ref;
1931 NTSTATUS Status = STATUS_SUCCESS;
1932 PREGION RgnUpdate = NULL;
1933 DECLARE_RETURN(BOOL);
1934
1935 TRACE("Enter NtUserRedrawWindow\n");
1936 UserEnterExclusive();
1937
1938 if (!(Wnd = UserGetWindowObject(hWnd ? hWnd : IntGetDesktopWindow())))
1939 {
1940 RETURN( FALSE);
1941 }
1942
1943 if (lprcUpdate)
1944 {
1945 _SEH2_TRY
1946 {
1947 ProbeForRead(lprcUpdate, sizeof(RECTL), 1);
1948 RtlCopyMemory(&SafeUpdateRect, lprcUpdate, sizeof(RECTL));
1949 }
1950 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1951 {
1952 Status = _SEH2_GetExceptionCode();
1953 }
1954 _SEH2_END
1955 if (!NT_SUCCESS(Status))
1956 {
1957 EngSetLastError(RtlNtStatusToDosError(Status));
1958 RETURN( FALSE);
1959 }
1960 }
1961
1962 if ( flags & ~(RDW_ERASE|RDW_FRAME|RDW_INTERNALPAINT|RDW_INVALIDATE|
1963 RDW_NOERASE|RDW_NOFRAME|RDW_NOINTERNALPAINT|RDW_VALIDATE|
1964 RDW_ERASENOW|RDW_UPDATENOW|RDW_ALLCHILDREN|RDW_NOCHILDREN) )
1965 {
1966 /* RedrawWindow fails only in case that flags are invalid */
1967 EngSetLastError(ERROR_INVALID_FLAGS);
1968 RETURN( FALSE);
1969 }
1970
1971 /* We can't hold lock on GDI objects while doing roundtrips to user mode,
1972 * so it will be copied.
1973 */
1974 if (hrgnUpdate > HRGN_WINDOW)
1975 {
1976 RgnUpdate = REGION_LockRgn(hrgnUpdate);
1977 if (!RgnUpdate)
1978 {
1979 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
1980 RETURN(FALSE);
1981 }
1982 REGION_UnlockRgn(RgnUpdate);
1983 }
1984 else if (hrgnUpdate == HRGN_WINDOW) // Trap it out.
1985 {
1986 ERR("NTRW: Caller is passing Window Region 1\n");
1987 }
1988
1989 UserRefObjectCo(Wnd, &Ref);
1990
1991 Ret = co_UserRedrawWindow( Wnd,
1992 lprcUpdate ? &SafeUpdateRect : NULL,
1993 RgnUpdate,
1994 flags);
1995
1996 UserDerefObjectCo(Wnd);
1997
1998 RETURN( Ret);
1999
2000 CLEANUP:
2001 TRACE("Leave NtUserRedrawWindow, ret=%i\n",_ret_);
2002 UserLeave();
2003 END_CLEANUP;
2004 }
2005
2006 BOOL
2007 UserDrawCaptionText(
2008 PWND pWnd,
2009 HDC hDc,
2010 const PUNICODE_STRING Text,
2011 const RECTL *lpRc,
2012 UINT uFlags,
2013 HFONT hFont)
2014 {
2015 HFONT hOldFont = NULL;
2016 COLORREF OldTextColor;
2017 NONCLIENTMETRICSW nclm;
2018 NTSTATUS Status;
2019 BOOLEAN bDeleteFont = FALSE;
2020 SIZE Size;
2021 BOOL Ret = TRUE;
2022 ULONG fit = 0, Length;
2023 RECTL r = *lpRc;
2024
2025 TRACE("UserDrawCaptionText: %wZ\n", Text);
2026
2027 nclm.cbSize = sizeof(nclm);
2028 if (!UserSystemParametersInfo(SPI_GETNONCLIENTMETRICS, nclm.cbSize, &nclm, 0))
2029 {
2030 ERR("UserSystemParametersInfo() failed!\n");
2031 return FALSE;
2032 }
2033
2034 if (!hFont)
2035 {
2036 if(uFlags & DC_SMALLCAP)
2037 Status = TextIntCreateFontIndirect(&nclm.lfSmCaptionFont, &hFont);
2038 else
2039 Status = TextIntCreateFontIndirect(&nclm.lfCaptionFont, &hFont);
2040
2041 if(!NT_SUCCESS(Status))
2042 {
2043 ERR("TextIntCreateFontIndirect() failed! Status: 0x%x\n", Status);
2044 return FALSE;
2045 }
2046
2047 bDeleteFont = TRUE;
2048 }
2049
2050 IntGdiSetBkMode(hDc, TRANSPARENT);
2051
2052 hOldFont = NtGdiSelectFont(hDc, hFont);
2053
2054 if(uFlags & DC_INBUTTON)
2055 OldTextColor = IntGdiSetTextColor(hDc, IntGetSysColor(COLOR_BTNTEXT));
2056 else
2057 OldTextColor = IntGdiSetTextColor(hDc,
2058 IntGetSysColor(uFlags & DC_ACTIVE ? COLOR_CAPTIONTEXT : COLOR_INACTIVECAPTIONTEXT));
2059
2060 // Adjust for system menu.
2061 if (pWnd && pWnd->style & WS_SYSMENU)
2062 {
2063 r.right -= UserGetSystemMetrics(SM_CYCAPTION) - 1;
2064 if ((pWnd->style & (WS_MAXIMIZEBOX | WS_MINIMIZEBOX)) && !(pWnd->ExStyle & WS_EX_TOOLWINDOW))
2065 {
2066 r.right -= UserGetSystemMetrics(SM_CXSIZE) + 1;
2067 r.right -= UserGetSystemMetrics(SM_CXSIZE) + 1;
2068 }
2069 }
2070
2071 GreGetTextExtentExW(hDc, Text->Buffer, Text->Length/sizeof(WCHAR), r.right - r.left, &fit, 0, &Size, 0);
2072
2073 Length = (Text->Length/sizeof(WCHAR) == fit ? fit : fit+1);
2074
2075 if (Text->Length/sizeof(WCHAR) > Length)
2076 {
2077 Ret = FALSE;
2078 }
2079
2080 if (Ret)
2081 { // Faster while in setup.
2082 GreExtTextOutW( hDc,
2083 lpRc->left,
2084 lpRc->top + (lpRc->bottom - lpRc->top) / 2 - Size.cy / 2, // DT_SINGLELINE && DT_VCENTER
2085 ETO_CLIPPED,
2086 (RECTL *)lpRc,
2087 Text->Buffer,
2088 Length,
2089 NULL,
2090 0 );
2091 }
2092 else
2093 {
2094 DrawTextW( hDc,
2095 Text->Buffer,
2096 Text->Length/sizeof(WCHAR),
2097 (RECTL *)&r,
2098 DT_END_ELLIPSIS|DT_SINGLELINE|DT_VCENTER|DT_NOPREFIX|DT_LEFT);
2099 }
2100
2101 IntGdiSetTextColor(hDc, OldTextColor);
2102
2103 if (hOldFont)
2104 NtGdiSelectFont(hDc, hOldFont);
2105
2106 if (bDeleteFont)
2107 GreDeleteObject(hFont);
2108
2109 return Ret;
2110 }
2111
2112 //
2113 // This draws Buttons, Icons and Text...
2114 //
2115 BOOL UserDrawCaption(
2116 PWND pWnd,
2117 HDC hDc,
2118 RECTL *lpRc,
2119 HFONT hFont,
2120 HICON hIcon,
2121 const PUNICODE_STRING Str,
2122 UINT uFlags)
2123 {
2124 BOOL Ret = FALSE;
2125 HBRUSH hBgBrush, hOldBrush = NULL;
2126 RECTL Rect = *lpRc;
2127 BOOL HasIcon;
2128
2129 RECTL_vMakeWellOrdered(lpRc);
2130
2131 /* Determine whether the icon needs to be displayed */
2132 if (!hIcon && pWnd != NULL)
2133 {
2134 HasIcon = (uFlags & DC_ICON) && !(uFlags & DC_SMALLCAP) &&
2135 (pWnd->style & WS_SYSMENU) && !(pWnd->ExStyle & WS_EX_TOOLWINDOW);
2136 }
2137 else
2138 HasIcon = (hIcon != NULL);
2139
2140 // Draw the caption background
2141 if((uFlags & DC_GRADIENT) && !(uFlags & DC_INBUTTON))
2142 {
2143 static GRADIENT_RECT gcap = {0, 1};
2144 TRIVERTEX Vertices[2];
2145 COLORREF Colors[2];
2146
2147 Colors[0] = IntGetSysColor((uFlags & DC_ACTIVE) ?
2148 COLOR_ACTIVECAPTION : COLOR_INACTIVECAPTION);
2149
2150 Colors[1] = IntGetSysColor((uFlags & DC_ACTIVE) ?
2151 COLOR_GRADIENTACTIVECAPTION : COLOR_GRADIENTINACTIVECAPTION);
2152
2153 Vertices[0].x = Rect.left;
2154 Vertices[0].y = Rect.top;
2155 Vertices[0].Red = (WORD)Colors[0]<<8;
2156 Vertices[0].Green = (WORD)Colors[0] & 0xFF00;
2157 Vertices[0].Blue = (WORD)(Colors[0]>>8) & 0xFF00;
2158 Vertices[0].Alpha = 0;
2159
2160 Vertices[1].x = Rect.right;
2161 Vertices[1].y = Rect.bottom;
2162 Vertices[1].Red = (WORD)Colors[1]<<8;
2163 Vertices[1].Green = (WORD)Colors[1] & 0xFF00;
2164 Vertices[1].Blue = (WORD)(Colors[1]>>8) & 0xFF00;
2165 Vertices[1].Alpha = 0;
2166
2167 if(!GreGradientFill(hDc, Vertices, 2, &gcap, 1, GRADIENT_FILL_RECT_H))
2168 {
2169 ERR("GreGradientFill() failed!\n");
2170 goto cleanup;
2171 }
2172 }
2173 else
2174 {
2175 if(uFlags & DC_INBUTTON)
2176 hBgBrush = IntGetSysColorBrush(COLOR_3DFACE);
2177 else if(uFlags & DC_ACTIVE)
2178 hBgBrush = IntGetSysColorBrush(COLOR_ACTIVECAPTION);
2179 else
2180 hBgBrush = IntGetSysColorBrush(COLOR_INACTIVECAPTION);
2181
2182 hOldBrush = NtGdiSelectBrush(hDc, hBgBrush);
2183
2184 if(!hOldBrush)
2185 {
2186 ERR("NtGdiSelectBrush() failed!\n");
2187 goto cleanup;
2188 }
2189
2190 if(!NtGdiPatBlt(hDc, Rect.left, Rect.top,
2191 Rect.right - Rect.left,
2192 Rect.bottom - Rect.top,
2193 PATCOPY))
2194 {
2195 ERR("NtGdiPatBlt() failed!\n");
2196 goto cleanup;
2197 }
2198 }
2199
2200 /* Draw icon */
2201 if (HasIcon)
2202 {
2203 PCURICON_OBJECT pIcon = NULL;
2204
2205 if (hIcon)
2206 {
2207 pIcon = UserGetCurIconObject(hIcon);
2208 }
2209 else if (pWnd)
2210 {
2211 pIcon = NC_IconForWindow(pWnd);
2212 // FIXME: NC_IconForWindow should reference it for us */
2213 if (pIcon)
2214 UserReferenceObject(pIcon);
2215 }
2216
2217 if (pIcon)
2218 {
2219 LONG cx = UserGetSystemMetrics(SM_CXSMICON);
2220 LONG cy = UserGetSystemMetrics(SM_CYSMICON);
2221 LONG x = Rect.left - cx/2 + 1 + (Rect.bottom - Rect.top)/2; // this is really what Window does
2222 LONG y = (Rect.top + Rect.bottom - cy)/2; // center
2223 UserDrawIconEx(hDc, x, y, pIcon, cx, cy, 0, NULL, DI_NORMAL);
2224 UserDereferenceObject(pIcon);
2225 }
2226 else
2227 {
2228 HasIcon = FALSE;
2229 }
2230 }
2231
2232 if (HasIcon)
2233 Rect.left += Rect.bottom - Rect.top;
2234
2235 if((uFlags & DC_TEXT))
2236 {
2237 BOOL Set = FALSE;
2238 Rect.left += 2;
2239
2240 if (Str)
2241 Set = UserDrawCaptionText(pWnd, hDc, Str, &Rect, uFlags, hFont);
2242 else if (pWnd != NULL) // FIXME: Windows does not do that
2243 {
2244 UNICODE_STRING ustr;
2245 ustr.Buffer = pWnd->strName.Buffer; // FIXME: LARGE_STRING truncated!
2246 ustr.Length = (USHORT)min(pWnd->strName.Length, MAXUSHORT);
2247 ustr.MaximumLength = (USHORT)min(pWnd->strName.MaximumLength, MAXUSHORT);
2248 Set = UserDrawCaptionText(pWnd, hDc, &ustr, &Rect, uFlags, hFont);
2249 }
2250 if (pWnd)
2251 {
2252 if (Set)
2253 pWnd->state2 &= ~WNDS2_CAPTIONTEXTTRUNCATED;
2254 else
2255 pWnd->state2 |= WNDS2_CAPTIONTEXTTRUNCATED;
2256 }
2257 }
2258
2259 Ret = TRUE;
2260
2261 cleanup:
2262 if (hOldBrush) NtGdiSelectBrush(hDc, hOldBrush);
2263
2264 return Ret;
2265 }
2266
2267 INT
2268 FASTCALL
2269 UserRealizePalette(HDC hdc)
2270 {
2271 HWND hWnd, hWndDesktop;
2272 DWORD Ret;
2273
2274 Ret = IntGdiRealizePalette(hdc);
2275 if (Ret) // There was a change.
2276 {
2277 hWnd = IntWindowFromDC(hdc);
2278 if (hWnd) // Send broadcast if dc is associated with a window.
2279 { // FYI: Thread locked in CallOneParam.
2280 hWndDesktop = IntGetDesktopWindow();
2281 if ( hWndDesktop != hWnd )
2282 {
2283 PWND pWnd = UserGetWindowObject(hWndDesktop);
2284 ERR("RealizePalette Desktop.");
2285 hdc = UserGetWindowDC(pWnd);
2286 IntPaintDesktop(hdc);
2287 UserReleaseDC(pWnd,hdc,FALSE);
2288 }
2289 UserSendNotifyMessage((HWND)HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0);
2290 }
2291 }
2292 return Ret;
2293 }
2294
2295 BOOL
2296 APIENTRY
2297 NtUserDrawCaptionTemp(
2298 HWND hWnd,
2299 HDC hDC,
2300 LPCRECT lpRc,
2301 HFONT hFont,
2302 HICON hIcon,
2303 const PUNICODE_STRING str,
2304 UINT uFlags)
2305 {
2306 PWND pWnd = NULL;
2307 UNICODE_STRING SafeStr = {0};
2308 NTSTATUS Status = STATUS_SUCCESS;
2309 RECTL SafeRect;
2310 BOOL Ret;
2311
2312 UserEnterExclusive();
2313
2314 if (hWnd != NULL)
2315 {
2316 if(!(pWnd = UserGetWindowObject(hWnd)))
2317 {
2318 UserLeave();
2319 return FALSE;
2320 }
2321 }
2322
2323 _SEH2_TRY
2324 {
2325 ProbeForRead(lpRc, sizeof(RECTL), sizeof(ULONG));
2326 RtlCopyMemory(&SafeRect, lpRc, sizeof(RECTL));
2327 if (str != NULL)
2328 {
2329 SafeStr = ProbeForReadUnicodeString(str);
2330 if (SafeStr.Length != 0)
2331 {
2332 ProbeForRead( SafeStr.Buffer,
2333 SafeStr.Length,
2334 sizeof(WCHAR));
2335 }
2336 }
2337 }
2338 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
2339 {
2340 Status = _SEH2_GetExceptionCode();
2341 }
2342 _SEH2_END;
2343
2344 if (Status != STATUS_SUCCESS)
2345 {
2346 SetLastNtError(Status);
2347 UserLeave();
2348 return FALSE;
2349 }
2350
2351 if (str != NULL)
2352 Ret = UserDrawCaption(pWnd, hDC, &SafeRect, hFont, hIcon, &SafeStr, uFlags);
2353 else
2354 {
2355 if ( RECTL_bIsEmptyRect(&SafeRect) && hFont == 0 && hIcon == 0 )
2356 {
2357 Ret = TRUE;
2358 if (uFlags & DC_DRAWCAPTIONMD)
2359 {
2360 ERR("NC Caption Mode\n");
2361 UserDrawCaptionBar(pWnd, hDC, uFlags);
2362 goto Exit;
2363 }
2364 else if (uFlags & DC_DRAWFRAMEMD)
2365 {
2366 ERR("NC Paint Mode\n");
2367 NC_DoNCPaint(pWnd, hDC, uFlags); // Update Menus too!
2368 goto Exit;
2369 }
2370 }
2371 Ret = UserDrawCaption(pWnd, hDC, &SafeRect, hFont, hIcon, NULL, uFlags);
2372 }
2373 Exit:
2374 UserLeave();
2375 return Ret;
2376 }
2377
2378 BOOL
2379 APIENTRY
2380 NtUserDrawCaption(HWND hWnd,
2381 HDC hDC,
2382 LPCRECT lpRc,
2383 UINT uFlags)
2384 {
2385 return NtUserDrawCaptionTemp(hWnd, hDC, lpRc, 0, 0, NULL, uFlags);
2386 }
2387
2388 BOOL
2389 APIENTRY
2390 NtUserInvalidateRect(
2391 HWND hWnd,
2392 CONST RECT *lpUnsafeRect,
2393 BOOL bErase)
2394 {
2395 UINT flags = RDW_INVALIDATE | (bErase ? RDW_ERASE : 0);
2396 if (!hWnd)
2397 {
2398 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
2399 lpUnsafeRect = NULL;
2400 }
2401 return NtUserRedrawWindow(hWnd, lpUnsafeRect, NULL, flags);
2402 }
2403
2404 BOOL
2405 APIENTRY
2406 NtUserInvalidateRgn(
2407 HWND hWnd,
2408 HRGN hRgn,
2409 BOOL bErase)
2410 {
2411 if (!hWnd)
2412 {
2413 EngSetLastError( ERROR_INVALID_WINDOW_HANDLE );
2414 return FALSE;
2415 }
2416 return NtUserRedrawWindow(hWnd, NULL, hRgn, RDW_INVALIDATE | (bErase? RDW_ERASE : 0));
2417 }
2418
2419 BOOL
2420 APIENTRY
2421 NtUserPrintWindow(
2422 HWND hwnd,
2423 HDC hdcBlt,
2424 UINT nFlags)
2425 {
2426 PWND Window;
2427 BOOL Ret = FALSE;
2428
2429 UserEnterExclusive();
2430
2431 if (hwnd)
2432 {
2433 if (!(Window = UserGetWindowObject(hwnd)) || // FIXME:
2434 Window == UserGetDesktopWindow() || // pWnd->fnid == FNID_DESKTOP
2435 Window == UserGetMessageWindow() ) // pWnd->fnid == FNID_MESSAGEWND
2436 {
2437 goto Exit;
2438 }
2439
2440 if ( Window )
2441 {
2442 /* Validate flags and check it as a mask for 0 or 1. */
2443 if ( (nFlags & PW_CLIENTONLY) == nFlags)
2444 Ret = IntPrintWindow( Window, hdcBlt, nFlags);
2445 else
2446 EngSetLastError(ERROR_INVALID_PARAMETER);
2447 }
2448 }
2449 Exit:
2450 UserLeave();
2451 return Ret;
2452 }
2453
2454 /* ValidateRect gets redirected to NtUserValidateRect:
2455 http://blog.csdn.net/ntdll/archive/2005/10/19/509299.aspx */
2456 BOOL
2457 APIENTRY
2458 NtUserValidateRect(
2459 HWND hWnd,
2460 const RECT *lpRect)
2461 {
2462 UINT flags = RDW_VALIDATE;
2463 if (!hWnd)
2464 {
2465 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
2466 lpRect = NULL;
2467 }
2468 return NtUserRedrawWindow(hWnd, lpRect, NULL, flags);
2469 }
2470
2471 /* EOF */