xref: /OK3568_Linux_fs/external/xserver/hw/xwin/winmultiwindowshape.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *Copyright (C) 1994-2000 The XFree86 Project, Inc. All Rights Reserved.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *Permission is hereby granted, free of charge, to any person obtaining
5*4882a593Smuzhiyun  * a copy of this software and associated documentation files (the
6*4882a593Smuzhiyun  *"Software"), to deal in the Software without restriction, including
7*4882a593Smuzhiyun  *without limitation the rights to use, copy, modify, merge, publish,
8*4882a593Smuzhiyun  *distribute, sublicense, and/or sell copies of the Software, and to
9*4882a593Smuzhiyun  *permit persons to whom the Software is furnished to do so, subject to
10*4882a593Smuzhiyun  *the following conditions:
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *The above copyright notice and this permission notice shall be
13*4882a593Smuzhiyun  *included in all copies or substantial portions of the Software.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*4882a593Smuzhiyun  *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*4882a593Smuzhiyun  *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*4882a593Smuzhiyun  *NONINFRINGEMENT. IN NO EVENT SHALL THE XFREE86 PROJECT BE LIABLE FOR
19*4882a593Smuzhiyun  *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20*4882a593Smuzhiyun  *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21*4882a593Smuzhiyun  *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  *Except as contained in this notice, the name of the XFree86 Project
24*4882a593Smuzhiyun  *shall not be used in advertising or otherwise to promote the sale, use
25*4882a593Smuzhiyun  *or other dealings in this Software without prior written authorization
26*4882a593Smuzhiyun  *from the XFree86 Project.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * Authors:	Kensuke Matsuzaki
29*4882a593Smuzhiyun  *		Harold L Hunt II
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #ifdef HAVE_XWIN_CONFIG_H
33*4882a593Smuzhiyun #include <xwin-config.h>
34*4882a593Smuzhiyun #endif
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #include "win.h"
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun  * winSetShapeMultiWindow - See Porting Layer Definition - p. 42
40*4882a593Smuzhiyun  */
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun void
winSetShapeMultiWindow(WindowPtr pWin,int kind)43*4882a593Smuzhiyun winSetShapeMultiWindow(WindowPtr pWin, int kind)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun     ScreenPtr pScreen = pWin->drawable.pScreen;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun     winScreenPriv(pScreen);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #if CYGMULTIWINDOW_DEBUG
50*4882a593Smuzhiyun     ErrorF("winSetShapeMultiWindow - pWin: %p kind: %i\n", pWin, kind);
51*4882a593Smuzhiyun #endif
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun     WIN_UNWRAP(SetShape);
54*4882a593Smuzhiyun     (*pScreen->SetShape) (pWin, kind);
55*4882a593Smuzhiyun     WIN_WRAP(SetShape, winSetShapeMultiWindow);
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun     /* Update the Windows window's shape */
58*4882a593Smuzhiyun     winReshapeMultiWindow(pWin);
59*4882a593Smuzhiyun     winUpdateRgnMultiWindow(pWin);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun     return;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun  * winUpdateRgnMultiWindow - Local function to update a Windows window region
66*4882a593Smuzhiyun  */
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun void
winUpdateRgnMultiWindow(WindowPtr pWin)69*4882a593Smuzhiyun winUpdateRgnMultiWindow(WindowPtr pWin)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun     SetWindowRgn(winGetWindowPriv(pWin)->hWnd,
72*4882a593Smuzhiyun                  winGetWindowPriv(pWin)->hRgn, TRUE);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun     /* The system now owns the region specified by the region handle and will delete it when it is no longer needed. */
75*4882a593Smuzhiyun     winGetWindowPriv(pWin)->hRgn = NULL;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun  * winReshapeMultiWindow - Computes the composite clipping region for a window
80*4882a593Smuzhiyun  */
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun void
winReshapeMultiWindow(WindowPtr pWin)83*4882a593Smuzhiyun winReshapeMultiWindow(WindowPtr pWin)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun     int nRects;
86*4882a593Smuzhiyun     RegionRec rrNewShape;
87*4882a593Smuzhiyun     BoxPtr pShape, pRects, pEnd;
88*4882a593Smuzhiyun     HRGN hRgn, hRgnRect;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun     winWindowPriv(pWin);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun #if CYGDEBUG
93*4882a593Smuzhiyun     winDebug("winReshape ()\n");
94*4882a593Smuzhiyun #endif
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun     /* Bail if the window is the root window */
97*4882a593Smuzhiyun     if (pWin->parent == NULL)
98*4882a593Smuzhiyun         return;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun     /* Bail if the window is not top level */
101*4882a593Smuzhiyun     if (pWin->parent->parent != NULL)
102*4882a593Smuzhiyun         return;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun     /* Bail if Windows window handle is invalid */
105*4882a593Smuzhiyun     if (pWinPriv->hWnd == NULL)
106*4882a593Smuzhiyun         return;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun     /* Free any existing window region stored in the window privates */
109*4882a593Smuzhiyun     if (pWinPriv->hRgn != NULL) {
110*4882a593Smuzhiyun         DeleteObject(pWinPriv->hRgn);
111*4882a593Smuzhiyun         pWinPriv->hRgn = NULL;
112*4882a593Smuzhiyun     }
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun     /* Bail if the window has no bounding region defined */
115*4882a593Smuzhiyun     if (!wBoundingShape(pWin))
116*4882a593Smuzhiyun         return;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun     RegionNull(&rrNewShape);
119*4882a593Smuzhiyun     RegionCopy(&rrNewShape, wBoundingShape(pWin));
120*4882a593Smuzhiyun     RegionTranslate(&rrNewShape, pWin->borderWidth, pWin->borderWidth);
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun     nRects = RegionNumRects(&rrNewShape);
123*4882a593Smuzhiyun     pShape = RegionRects(&rrNewShape);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun     /* Don't do anything if there are no rectangles in the region */
126*4882a593Smuzhiyun     if (nRects > 0) {
127*4882a593Smuzhiyun         RECT rcClient;
128*4882a593Smuzhiyun         RECT rcWindow;
129*4882a593Smuzhiyun         int iOffsetX, iOffsetY;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun         /* Get client rectangle */
132*4882a593Smuzhiyun         if (!GetClientRect(pWinPriv->hWnd, &rcClient)) {
133*4882a593Smuzhiyun             ErrorF("winReshape - GetClientRect failed, bailing: %d\n",
134*4882a593Smuzhiyun                    (int) GetLastError());
135*4882a593Smuzhiyun             return;
136*4882a593Smuzhiyun         }
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun         /* Translate client rectangle coords to screen coords */
139*4882a593Smuzhiyun         /* NOTE: Only transforms top and left members */
140*4882a593Smuzhiyun         ClientToScreen(pWinPriv->hWnd, (LPPOINT) &rcClient);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun         /* Get window rectangle */
143*4882a593Smuzhiyun         if (!GetWindowRect(pWinPriv->hWnd, &rcWindow)) {
144*4882a593Smuzhiyun             ErrorF("winReshape - GetWindowRect failed, bailing: %d\n",
145*4882a593Smuzhiyun                    (int) GetLastError());
146*4882a593Smuzhiyun             return;
147*4882a593Smuzhiyun         }
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun         /* Calculate offset from window upper-left to client upper-left */
150*4882a593Smuzhiyun         iOffsetX = rcClient.left - rcWindow.left;
151*4882a593Smuzhiyun         iOffsetY = rcClient.top - rcWindow.top;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun         /* Create initial Windows region for title bar */
154*4882a593Smuzhiyun         /* FIXME: Mean, nasty, ugly hack!!! */
155*4882a593Smuzhiyun         hRgn = CreateRectRgn(0, 0, rcWindow.right, iOffsetY);
156*4882a593Smuzhiyun         if (hRgn == NULL) {
157*4882a593Smuzhiyun             ErrorF("winReshape - Initial CreateRectRgn (%d, %d, %d, %d) "
158*4882a593Smuzhiyun                    "failed: %d\n",
159*4882a593Smuzhiyun                    0, 0, (int) rcWindow.right, iOffsetY, (int) GetLastError());
160*4882a593Smuzhiyun         }
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun         /* Loop through all rectangles in the X region */
163*4882a593Smuzhiyun         for (pRects = pShape, pEnd = pShape + nRects; pRects < pEnd; pRects++) {
164*4882a593Smuzhiyun             /* Create a Windows region for the X rectangle */
165*4882a593Smuzhiyun             hRgnRect = CreateRectRgn(pRects->x1 + iOffsetX,
166*4882a593Smuzhiyun                                      pRects->y1 + iOffsetY,
167*4882a593Smuzhiyun                                      pRects->x2 + iOffsetX,
168*4882a593Smuzhiyun                                      pRects->y2 + iOffsetY);
169*4882a593Smuzhiyun             if (hRgnRect == NULL) {
170*4882a593Smuzhiyun                 ErrorF("winReshape - Loop CreateRectRgn (%d, %d, %d, %d) "
171*4882a593Smuzhiyun                        "failed: %d\n"
172*4882a593Smuzhiyun                        "\tx1: %d x2: %d xOff: %d y1: %d y2: %d yOff: %d\n",
173*4882a593Smuzhiyun                        pRects->x1 + iOffsetX,
174*4882a593Smuzhiyun                        pRects->y1 + iOffsetY,
175*4882a593Smuzhiyun                        pRects->x2 + iOffsetX,
176*4882a593Smuzhiyun                        pRects->y2 + iOffsetY,
177*4882a593Smuzhiyun                        (int) GetLastError(),
178*4882a593Smuzhiyun                        pRects->x1, pRects->x2, iOffsetX,
179*4882a593Smuzhiyun                        pRects->y1, pRects->y2, iOffsetY);
180*4882a593Smuzhiyun             }
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun             /* Merge the Windows region with the accumulated region */
183*4882a593Smuzhiyun             if (CombineRgn(hRgn, hRgn, hRgnRect, RGN_OR) == ERROR) {
184*4882a593Smuzhiyun                 ErrorF("winReshape - CombineRgn () failed: %d\n",
185*4882a593Smuzhiyun                        (int) GetLastError());
186*4882a593Smuzhiyun             }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun             /* Delete the temporary Windows region */
189*4882a593Smuzhiyun             DeleteObject(hRgnRect);
190*4882a593Smuzhiyun         }
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun         /* Save a handle to the composite region in the window privates */
193*4882a593Smuzhiyun         pWinPriv->hRgn = hRgn;
194*4882a593Smuzhiyun     }
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun     RegionUninit(&rrNewShape);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun     return;
199*4882a593Smuzhiyun }
200