winex11: Move code behind error return.
[wine] / dlls / winex11.drv / clipping.c
1 /*
2  * X11DRV clipping functions
3  *
4  * Copyright 1998 Huw Davies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24
25 #include "x11drv.h"
26
27 /***********************************************************************
28  *           X11DRV_GetRegionData
29  *
30  * Calls GetRegionData on the given region and converts the rectangle
31  * array to XRectangle format. The returned buffer must be freed by
32  * caller using HeapFree(GetProcessHeap(),...).
33  * If hdc_lptodp is not 0, the rectangles are converted through LPtoDP.
34  */
35 RGNDATA *X11DRV_GetRegionData( HRGN hrgn, HDC hdc_lptodp )
36 {
37     RGNDATA *data;
38     DWORD size;
39     unsigned int i;
40     RECT *rect, tmp;
41     XRectangle *xrect;
42
43     if (!(size = GetRegionData( hrgn, 0, NULL ))) return NULL;
44     if (sizeof(XRectangle) > sizeof(RECT))
45     {
46         /* add extra size for XRectangle array */
47         int count = (size - sizeof(RGNDATAHEADER)) / sizeof(RECT);
48         size += count * (sizeof(XRectangle) - sizeof(RECT));
49     }
50     if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
51     if (!GetRegionData( hrgn, size, data ))
52     {
53         HeapFree( GetProcessHeap(), 0, data );
54         return NULL;
55     }
56
57     rect = (RECT *)data->Buffer;
58     xrect = (XRectangle *)data->Buffer;
59     if (hdc_lptodp)  /* map to device coordinates */
60     {
61         LPtoDP( hdc_lptodp, (POINT *)rect, data->rdh.nCount * 2 );
62         for (i = 0; i < data->rdh.nCount; i++)
63         {
64             if (rect[i].right < rect[i].left)
65             {
66                 INT tmp = rect[i].right;
67                 rect[i].right = rect[i].left;
68                 rect[i].left = tmp;
69             }
70             if (rect[i].bottom < rect[i].top)
71             {
72                 INT tmp = rect[i].bottom;
73                 rect[i].bottom = rect[i].top;
74                 rect[i].top = tmp;
75             }
76         }
77     }
78
79     if (sizeof(XRectangle) > sizeof(RECT))
80     {
81         int j;
82         /* need to start from the end */
83         for (j = data->rdh.nCount-1; j >= 0; j--)
84         {
85             tmp = rect[j];
86             xrect[j].x      = tmp.left;
87             xrect[j].y      = tmp.top;
88             xrect[j].width  = tmp.right - tmp.left;
89             xrect[j].height = tmp.bottom - tmp.top;
90         }
91     }
92     else
93     {
94         for (i = 0; i < data->rdh.nCount; i++)
95         {
96             tmp = rect[i];
97             xrect[i].x      = tmp.left;
98             xrect[i].y      = tmp.top;
99             xrect[i].width  = tmp.right - tmp.left;
100             xrect[i].height = tmp.bottom - tmp.top;
101         }
102     }
103     return data;
104 }
105
106
107 /***********************************************************************
108  *           X11DRV_SetDeviceClipping
109  */
110 void X11DRV_SetDeviceClipping( X11DRV_PDEVICE *physDev, HRGN vis_rgn, HRGN clip_rgn )
111 {
112     RGNDATA *data;
113
114     CombineRgn( physDev->region, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
115     if (!(data = X11DRV_GetRegionData( physDev->region, 0 ))) return;
116
117     wine_tsx11_lock();
118     XSetClipRectangles( gdi_display, physDev->gc, physDev->dc_rect.left, physDev->dc_rect.top,
119                         (XRectangle *)data->Buffer, data->rdh.nCount, YXBanded );
120     wine_tsx11_unlock();
121     HeapFree( GetProcessHeap(), 0, data );
122 }