File: Region.hs

package info (click to toggle)
haskell-x11 1.10.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,392 kB
  • sloc: haskell: 761; ansic: 160; makefile: 2
file content (273 lines) | stat: -rw-r--r-- 9,508 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
{-# LANGUAGE DeriveDataTypeable #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Graphics.X11.Xlib.Region
-- Copyright   :  (c) Alastair Reid, 1999-2003
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  libraries@haskell.org
-- Stability   :  provisional
-- Portability :  portable
--
-- A collection of FFI declarations for interfacing with Xlib Regions.
--
-----------------------------------------------------------------------------

module Graphics.X11.Xlib.Region(
        Region,

        RectInRegionResult,
        rectangleOut,
        rectangleIn,
        rectanglePart,

        createRegion,
        polygonRegion,
        intersectRegion,
        subtractRegion,
        unionRectWithRegion,
        unionRegion,
        xorRegion,
        emptyRegion,
        equalRegion,
        pointInRegion,
        rectInRegion,
        clipBox,
        offsetRegion,
        shrinkRegion,
        setRegion,

        ) where

import Graphics.X11.Types
import Graphics.X11.Xlib.Types

import Foreign.ForeignPtr
import Foreign.Ptr
import Foreign.C.Types
import Foreign.Storable
import Foreign.Marshal.Alloc
import Foreign.Marshal.Array
import Foreign.Marshal.Utils

#if __GLASGOW_HASKELL__
import Data.Data
#endif

----------------------------------------------------------------
-- Regions
----------------------------------------------------------------

newtype Region = Region (ForeignPtr Region)
#if __GLASGOW_HASKELL__
        deriving (Eq, Ord, Show, Typeable, Data)
#else
        deriving (Eq, Ord, Show)
#endif

withRegion :: Region -> (Ptr Region -> IO a) -> IO a
withRegion (Region r) = withForeignPtr r

type RectInRegionResult = CInt

-- Return values from XRectInRegion()
rectangleOut, rectangleIn, rectanglePart :: RectInRegionResult
rectangleOut  = 0
rectangleIn   = 1
rectanglePart = 2

----------------------------------------------------------------
-- Creating regions
----------------------------------------------------------------

-- regions deallocation is handled by the GC (ForeignPtr magic)
-- so we don't provide XDestroyRegion explicitly
-- no idea what the int is for
-- %fun XDestroyRegion :: Region -> IO Int
foreign import ccall unsafe "HsXlib.h &XDestroyRegion"
        xDestroyRegionPtr :: FunPtr (Ptr Region -> IO ())

makeRegion :: Ptr Region -> IO Region
makeRegion rp = do
        r <- newForeignPtr xDestroyRegionPtr rp
        return (Region r)

-- an empty region
-- (often used as "out argument" to binary operators which return regions)

-- | interface to the X11 library function @XCreateRegion()@.
createRegion :: IO Region
createRegion = do
        rp <- xCreateRegion
        makeRegion rp
foreign import ccall unsafe "HsXlib.h XCreateRegion"
        xCreateRegion :: IO (Ptr Region)

-- | interface to the X11 library function @XPolygonRegion()@.
polygonRegion :: [Point] -> FillRule -> IO Region
polygonRegion points fill_rule =
        withArrayLen points $ \ n point_arr -> do
        rp <- xPolygonRegion point_arr (fromIntegral n) fill_rule
        makeRegion rp
foreign import ccall unsafe "HsXlib.h XPolygonRegion"
        xPolygonRegion :: Ptr Point -> CInt -> FillRule -> IO (Ptr Region)

----------------------------------------------------------------
-- Combining Regions
--
-- The usual shoddy state of Xlib documentation fails to mention
-- what the Int is for.
--
-- All operations overwrite the region in their third argument
-- which is usually a freshly created region.
----------------------------------------------------------------

-- | interface to the X11 library function @XIntersectRegion()@.
intersectRegion     :: Region -> Region -> Region -> IO CInt
intersectRegion src1 src2 dest =
        withRegion src1 $ \ src1_ptr ->
        withRegion src2 $ \ src2_ptr ->
        withRegion dest $ \ dest_ptr ->
        xIntersectRegion src1_ptr src2_ptr dest_ptr
foreign import ccall unsafe
        "HsXlib.h XIntersectRegion" xIntersectRegion ::
        Ptr Region -> Ptr Region -> Ptr Region -> IO CInt

-- | interface to the X11 library function @XSubtractRegion()@.
subtractRegion     :: Region -> Region -> Region -> IO CInt
subtractRegion src1 src2 dest =
        withRegion src1 $ \ src1_ptr ->
        withRegion src2 $ \ src2_ptr ->
        withRegion dest $ \ dest_ptr ->
        xSubtractRegion src1_ptr src2_ptr dest_ptr
foreign import ccall unsafe
        "HsXlib.h XSubtractRegion" xSubtractRegion ::
        Ptr Region -> Ptr Region -> Ptr Region -> IO CInt

-- | interface to the X11 library function @XUnionRectWithRegion()@.
unionRectWithRegion     :: Rectangle -> Region -> Region -> IO CInt
unionRectWithRegion rect src dest =
        with rect $ \ rect_ptr ->
        withRegion src $ \ src_ptr ->
        withRegion dest $ \ dest_ptr ->
        xUnionRectWithRegion rect_ptr src_ptr dest_ptr
foreign import ccall unsafe
        "HsXlib.h XUnionRectWithRegion" xUnionRectWithRegion ::
        Ptr Rectangle -> Ptr Region -> Ptr Region -> IO CInt

-- | interface to the X11 library function @XUnionRegion()@.
unionRegion     :: Region -> Region -> Region -> IO CInt
unionRegion src1 src2 dest =
        withRegion src1 $ \ src1_ptr ->
        withRegion src2 $ \ src2_ptr ->
        withRegion dest $ \ dest_ptr ->
        xUnionRegion src1_ptr src2_ptr dest_ptr
foreign import ccall unsafe
        "HsXlib.h XUnionRegion" xUnionRegion ::
        Ptr Region -> Ptr Region -> Ptr Region -> IO CInt

-- | interface to the X11 library function @XXorRegion()@.
xorRegion     :: Region -> Region -> Region -> IO CInt
xorRegion src1 src2 dest =
        withRegion src1 $ \ src1_ptr ->
        withRegion src2 $ \ src2_ptr ->
        withRegion dest $ \ dest_ptr ->
        xXorRegion src1_ptr src2_ptr dest_ptr
foreign import ccall unsafe
        "HsXlib.h XXorRegion" xXorRegion ::
        Ptr Region -> Ptr Region -> Ptr Region -> IO CInt

----------------------------------------------------------------
-- Examining regions (tests, bounding boxes, etc)
----------------------------------------------------------------

-- | interface to the X11 library function @XEmptyRegion()@.
emptyRegion :: Region -> IO Bool
emptyRegion r = withRegion r xEmptyRegion
foreign import ccall unsafe "HsXlib.h XEmptyRegion"
        xEmptyRegion :: Ptr Region -> IO Bool

-- | interface to the X11 library function @XEqualRegion()@.
equalRegion :: Region -> Region -> IO Bool
equalRegion r1 r2 =
        withRegion r1 $ \ rp1 ->
        withRegion r2 $ \ rp2 ->
        xEqualRegion rp1 rp2
foreign import ccall unsafe "HsXlib.h XEqualRegion"
        xEqualRegion :: Ptr Region -> Ptr Region -> IO Bool

-- | interface to the X11 library function @XPointInRegion()@.
pointInRegion :: Region -> Point -> IO Bool
pointInRegion r (Point x y) =
        withRegion r $ \ rp ->
        xPointInRegion rp x y
foreign import ccall unsafe "HsXlib.h XPointInRegion"
        xPointInRegion :: Ptr Region -> Position -> Position -> IO Bool

-- | interface to the X11 library function @XRectInRegion()@.
rectInRegion :: Region -> Rectangle -> IO RectInRegionResult
rectInRegion r (Rectangle x y w h) =
        withRegion r $ \ rp ->
        xRectInRegion rp x y w h
foreign import ccall unsafe "HsXlib.h XRectInRegion"
        xRectInRegion :: Ptr Region -> Position -> Position ->
                Dimension -> Dimension -> IO RectInRegionResult

-- I have no idea what the int is for

-- | interface to the X11 library function @XClipBox()@.
clipBox :: Region -> IO (Rectangle,CInt)
clipBox r =
        withRegion r $ \ rp ->
        alloca $ \ rect_ptr -> do
        res <- xClipBox rp rect_ptr
        rect <- peek rect_ptr
        return (rect, res)
foreign import ccall unsafe "HsXlib.h XClipBox"
        xClipBox :: Ptr Region -> Ptr Rectangle -> IO CInt

----------------------------------------------------------------
-- Modifying regions
-- (If you use any of these, you can't make regions look like
--  first class data structures.)
----------------------------------------------------------------

-- translate region

-- | interface to the X11 library function @XOffsetRegion()@.
offsetRegion :: Region -> Point -> IO CInt
offsetRegion r (Point x y) =
        withRegion r $ \ rp ->
        xOffsetRegion rp x y
foreign import ccall unsafe "HsXlib.h XOffsetRegion"
        xOffsetRegion :: Ptr Region -> Position -> Position -> IO CInt

-- increase size of region by +ve or -ve number of pixels
-- while preserving the centre of the region (ie half the pixels
-- come off the left, and half off the right)

-- | interface to the X11 library function @XShrinkRegion()@.
shrinkRegion :: Region -> Point -> IO CInt
shrinkRegion r (Point x y) =
        withRegion r $ \ rp ->
        xShrinkRegion rp x y
foreign import ccall unsafe "HsXlib.h XShrinkRegion"
        xShrinkRegion :: Ptr Region -> Position -> Position -> IO CInt

----------------------------------------------------------------
-- Graphics Context
----------------------------------------------------------------

-- set clip mask of GC

-- | interface to the X11 library function @XSetRegion()@.
setRegion :: Display -> GC -> Region -> IO CInt
setRegion disp gc r =
        withRegion r $ \ rp ->
        xSetRegion disp gc rp
foreign import ccall unsafe "HsXlib.h XSetRegion"
        xSetRegion :: Display -> GC -> Ptr Region -> IO CInt

----------------------------------------------------------------
-- End
----------------------------------------------------------------