/***********************************************************
Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
and the Massachusetts Institute of Technology, Cambridge, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Digital or MIT not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/
/* $Header: mivaltree.c,v 1.31 87/06/15 01:10:02 sue Exp $ */
#include "X.h"
#include "scrnintstr.h"
#include "windowstr.h"
#include "mi.h"
#include "regionstr.h"
#include "opaque.h"

/*
 * mivaltree.c - calculates window clip lists
 *
 * Author:	sue
 * 		Digital Equipment Corporation
 * Date:	August 1986
 */


/*****************
 * ValidateTree
 *    Recomputes clip list for PWIN and all its childern (inferiors)
 *    Recomputes breadth first -- children then each child's childern,
 *    in turn.  The routine assumes you have already fixed up the
 *    winSize region of the windows that have been alterned and reset
 *    the serialNumber of the window.  This is necessary to figure out if
 *    any of the other windows have been affected by the changes.
 *    
 *    Does set operations with the "universe" the extents of the pWin.
 *    As we traverse the window stack, remove each successive
 *    window's extents from the universe.  The cliplist for any window
 *    can be gotten by intersecting the current universe with the
 *    window's extents.  At the end of this operation, the universe
 *    is the clipList for the parent.
 *    
 *    PCHILD was the one affecting the clipping, no need to look at
 *    anything before it.  TOP is FALSE if called recursively -- and tells
 *    whether to use PWIN's borderClip or borderSize to do the clipping.
 *****************/

int
miValidateTree(pParent, pChild, top, anyMarked)
    WindowPtr pParent, pChild;
    Bool top, anyMarked;
{
    WindowPtr pSib;
    RegionPtr 
           oldBRegion, oldCRegion, 
	   exposed,
	   oldBorderClip, oldClipList,
           universe;     /* ends up being pParent's borderClips */
    Bool translateNecessary;
    register ScreenPtr pScreen;
    int oldVis;
    
    pScreen = pParent->drawable.pScreen;

    oldBRegion = (* pScreen->RegionCreate)((BoxPtr)NULL, 1);
    oldCRegion = (* pScreen->RegionCreate)((BoxPtr)NULL, 1);
    exposed = (* pScreen->RegionCreate)((BoxPtr)NULL, 1);    /* sue */
    if (pParent->borderWidth)
    {
        oldBorderClip = (* pScreen->RegionCreate)((BoxPtr)NULL, 1);
        (* pScreen->RegionCopy)(oldBorderClip, pParent->borderClip);
    }
    if (!top)
    {
	universe = (* pScreen->RegionCreate)((BoxPtr)NULL, 1);
	oldClipList = (* pScreen->RegionCreate)((BoxPtr)NULL, 1);
        (* pScreen->RegionCopy)(oldClipList, pParent->clipList);
        (* pScreen->RegionCopy)(universe, pParent->borderClip);
    }
    if (pChild)                           
	pSib = pChild;                    
    else                                  
	pSib = pParent->firstChild;       
    while (pSib) 
    {
        if (pSib->viewable && pSib->marked)
	{
            if (pSib->borderWidth)
    	        (* pScreen->RegionCopy)(oldBRegion, pSib->borderClip);
  	    (* pScreen->RegionCopy)(oldCRegion, pSib->clipList);	    
	    if (!top)
	    {
	        (* pScreen->Intersect)(pSib->clipList, pSib->winSize, universe); 
   	        (* pScreen->Intersect)(pSib->borderClip, pSib->borderSize, 
				       universe);
	    }
            else
	    {
	        (* pScreen->Intersect)(pSib->clipList, pSib->winSize, 
				   pParent->clipList);       /* sue */
       	        (* pScreen->Intersect)(pSib->borderClip, pSib->borderSize, 
				   pParent->clipList);       /* sue */
	    }
	    if (pSib->firstChild) 
	    {
                miValidateTree(pSib, (WindowPtr)NULL, FALSE, TRUE);
	    }
	    oldVis = pSib->visibility;
	    if (pSib->borderClip->numRects)
    		pSib->visibility = 
		   SingleRectRegionEqual(pSib->borderClip, 
		       pSib->borderSize) ? 
			   VisibilityUnobscured : VisibilityPartiallyObscured;
	    else
		pSib->visibility = VisibilityFullyObscured;
	    if (oldVis != pSib->visibility)
	    {
		SendVisibilityNotify(pSib);
	    }
	    pSib->drawable.serialNumber = NEXT←SERIAL←NUMBER;

                    /* now figure exposure */
            translateNecessary =  ((pSib->oldAbsCorner.x != pSib->absCorner.x)  ||
				   ((pSib->oldAbsCorner.y != pSib->absCorner.y)));
	        /* first expose the borders */
            if (pSib->borderWidth)
	    {
		if (translateNecessary)
	        {
		    /* expose the entire border */
		    
		    (* pScreen->Subtract)(pSib->borderExposed, pSib->borderClip, 
					  pSib->winSize);
		}
		else
		{
	            (* pScreen->Subtract)(exposed, pSib->borderClip, oldBRegion);
    	            if (exposed->numRects)
	            {
		        (* pScreen->Subtract)(exposed, exposed, pSib->winSize);
		        (* pScreen->Union)(pSib->borderExposed, 
					   pSib->borderExposed, exposed);
		    }
		}
	    }
	        /* now expose the window */
	    if (translateNecessary)
	    {
		(* pScreen->TranslateRegion)(oldCRegion, 
				pSib->absCorner.x - pSib->oldAbsCorner.x,
		                pSib->absCorner.y - pSib->oldAbsCorner.y);
	    }
	    (* pScreen->Subtract)(exposed, pSib->clipList, oldCRegion);
    	    if (exposed->numRects)
	    {
		(* pScreen->Union)(pSib->exposed, pSib->exposed, exposed);
	    }
                    /* figure obscures, if necessary */
            if (pSib->backStorage && (pSib->backingStore != NotUseful))
	    {
		(* pScreen->Subtract)(exposed, pSib->clipList, oldCRegion);
		if (exposed->numRects)
		    (* pScreen->Union)(pSib->backStorage->obscured, 
				       pSib->backStorage->obscured, exposed);
	    }
	    pSib->oldAbsCorner.x = pSib->absCorner.x;
	    pSib->oldAbsCorner.y = pSib->absCorner.y;
                    /* Subtract borderSize from universe */
	    if (!top)
                (* pScreen->Subtract)(universe, universe, pSib->borderSize);
	    else
                (* pScreen->Subtract)(pParent->clipList, 
				  pParent->clipList, pSib->borderSize);  
	    if (pSib->exposed->numRects)
    	        (* pScreen->Intersect)(pSib->exposed, pSib->exposed, 
				       pSib->clipList);
	    if (pSib->borderExposed->numRects)
    	        (* pScreen->Intersect)(pSib->borderExposed, 
			  pSib->borderExposed, pSib->borderClip);
            pSib->marked = 0;
	}
        
        else if (!top && pSib->viewable)
            (* pScreen->Subtract)(universe, universe, pSib->borderSize);
        pSib = pSib->nextSib;
    }
        /* then fix up parent region.... */

    if (pParent->borderWidth)
    {
	(* pScreen->Subtract)(exposed, pParent->borderClip, oldBorderClip);
	if (exposed->numRects)
	    (* pScreen->Union)(pParent->borderExposed, 
			       pParent->borderExposed, exposed);
    }
    if (!top)
    {
        (* pScreen->Intersect)(pParent->clipList, universe, pParent->winSize); 
	(* pScreen->Subtract)(exposed, pParent->clipList, oldClipList);
	if (exposed->numRects)
	    (* pScreen->Union)(pParent->exposed, pParent->exposed, exposed);
	(* pScreen->Subtract)(exposed, pParent->clipList, oldClipList);
	if (exposed->numRects)
            (* pScreen->Union)(pParent->exposed, pParent->exposed, exposed);
    }
    else
        (* pScreen->Intersect)(pParent->exposed, pParent->exposed,   /* sue */
			  pParent->clipList);       /* sue */
    pParent->drawable.serialNumber = NEXT←SERIAL←NUMBER;
    pParent->marked = 0;
    (* pScreen->RegionDestroy)(exposed);
    if (pParent->borderWidth)
        (* pScreen->RegionDestroy)(oldBorderClip);
    if (!top)
    {
        (* pScreen->RegionDestroy)(universe);
        (* pScreen->RegionDestroy)(oldClipList);
    }
    (* pScreen->RegionDestroy)(oldBRegion);
    (* pScreen->RegionDestroy)(oldCRegion);

    if (top)
        WindowsRestructured();
    return(1);
}