blob: 61dc89ff1fe77e635da1180ca46d43a8517e64c3 [file] [log] [blame]
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18#ifndef __REGIONHELPER_H__
19#define __REGIONHELPER_H__
20
21// RegionHelper is a class which helps in using X server regions by
22// automatically freeing them in the destructor. It also fixes a problem with
23// REGION_INIT when given an empty rectangle.
24
25// REGION_NULL was introduced in the Xorg tree as the way to initialise an
26// empty region. If it's not already defined do it the old way. Note that the
27// old way causes a segfault in the new tree...
28#ifndef REGION_NULL
29#define REGION_NULL(pScreen,pReg) REGION_INIT(pScreen,pReg,NullBox,0)
30#endif
31
32class RegionHelper {
33public:
34
35 // constructor from a single rect
36 RegionHelper(ScreenPtr pScreen_, BoxPtr rect, int size)
37 : pScreen(pScreen_), reg(0)
38 {
39 init(rect, size);
40 }
41
42 // constructor from an existing X server region
43 RegionHelper(ScreenPtr pScreen_, RegionPtr pRegion)
44 : pScreen(pScreen_), reg(&regRec)
45 {
46 REGION_NULL(pScreen, reg);
47 REGION_COPY(pScreen, reg, pRegion);
48 }
49
50 // constructor from an array of rectangles
51 RegionHelper(ScreenPtr pScreen_, int nrects, xRectanglePtr rects,
52 int ctype=CT_NONE)
53 : pScreen(pScreen_)
54 {
55 reg = RECTS_TO_REGION(pScreen, nrects, rects, ctype);
56 }
57
58 // constructor for calling init() later
59 RegionHelper(ScreenPtr pScreen_) : pScreen(pScreen_), reg(0) {
60 }
61
62 void init(BoxPtr rect, int size) {
63 reg = &regRec;
64 if (!rect || (rect && (rect->x2 == rect->x1 || rect->y2 == rect->y1))) {
65 REGION_NULL(pScreen, reg);
66 } else {
67 REGION_INIT(pScreen, reg, rect, size);
68 }
69 }
70
71 // destructor frees as appropriate
72 ~RegionHelper() {
73 if (reg == &regRec) {
74 REGION_UNINIT(pScreen, reg);
75 } else if (reg) {
76 REGION_DESTROY(pScreen, reg);
77 }
78 }
79 ScreenPtr pScreen;
80 RegionRec regRec;
81 RegionPtr reg;
82};
83
84#endif