File: PaperSize.chs

package info (click to toggle)
haskell-gtk 0.15.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,964 kB
  • sloc: haskell: 3,346; ansic: 826; makefile: 161
file content (361 lines) | stat: -rw-r--r-- 10,351 bytes parent folder | download | duplicates (9)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
{-# LANGUAGE CPP #-}
-- -*-haskell-*-
--  GIMP Toolkit (GTK) Widget PaperSize
--
--  Author : Andy Stewart
--
--  Created: 28 Mar 2010
--
--  Copyright (C) 2010 Andy Stewart
--
--  This library is free software; you can redistribute it and/or
--  modify it under the terms of the GNU Lesser General Public
--  License as published by the Free Software Foundation; either
--  version 2.1 of the License, or (at your option) any later version.
--
--  This library is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--  Lesser General Public License for more details.
--
-- |
-- Maintainer  : gtk2hs-users@lists.sourceforge.net
-- Stability   : provisional
-- Portability : portable (depends on GHC)
--
-- Support for named paper sizes
--
module Graphics.UI.Gtk.Printing.PaperSize (

-- * Detail
--
-- | 'PaperSize' handles paper sizes. It uses the
-- standard called \"PWG 5101.1-2002 PWG: Standard for Media Standardized
-- Names\" to name the paper sizes (and to get the data for the page sizes). In
-- addition to standard paper sizes, 'PaperSize' allows
-- to construct custom paper sizes with arbitrary dimensions.
--
-- The 'PaperSize' object stores not only the
-- dimensions (width and height) of a paper size and its name, it also provides
-- default print margins.
--
-- Printing support has been added in Gtk+ 2.10.

#if GTK_CHECK_VERSION(2,10,0)
-- * Types
  PaperSize(..),
  mkPaperSize,

-- * Enums
  Unit(..),

-- * Constructors
  paperSizeNew,
  paperSizeNewFromPpd,
  paperSizeNewCustom,

-- * Methods
  paperSizeCopy,
  paperSizeIsEqual,
  paperSizeGetName,
  paperSizeGetDisplayName,
  paperSizeGetPpdName,
  paperSizeGetWidth,
  paperSizeGetHeight,
  paperSizeIsCustom,
  paperSizeSetSize,
  paperSizeGetDefaultTopMargin,
  paperSizeGetDefaultBottomMargin,
  paperSizeGetDefaultLeftMargin,
  paperSizeGetDefaultRightMargin,
  paperSizeGetDefault,
#endif
#if GTK_CHECK_VERSION(2,12,0)
  paperSizeGetPaperSizes,
#endif
  ) where

import Control.Monad    (liftM)

import System.Glib.FFI
import System.Glib.UTFString
import System.Glib.GList

{# context lib="gtk" prefix="gtk" #}

--------------------
-- Types
{#pointer *PaperSize foreign newtype#}

--------------------
-- Enums
{#enum Unit {underscoreToCase} deriving (Bounded,Eq,Show)#}

--------------------
-- Constructors

mkPaperSize :: Ptr PaperSize -> IO PaperSize
mkPaperSize pPtr = do
  size <- newForeignPtr pPtr paper_size_free
  return (PaperSize size)

foreign import ccall unsafe "&gtk_paper_size_free"
  paper_size_free :: FinalizerPtr PaperSize

#if GTK_CHECK_VERSION(2,10,0)
-- | Creates a new 'PaperSize' object by parsing a PWG
-- 5101.1-2002 paper name.
--
-- If @name@ is Nothing, the default paper size is returned, see 'paperSizeGetDefault'.
--
-- * Available since Gtk+ version 2.10
--
paperSizeNew :: GlibString string
 => Maybe string -- ^ @name@ - a paper size name, or 'Nothing'
 -> IO PaperSize
paperSizeNew name =
  maybeWith withUTFString name $ \namePtr ->
  {# call gtk_paper_size_new #}
    namePtr
  >>= mkPaperSize

-- | Creates a new 'PaperSize' object by using PPD
-- information.
--
-- If @ppdName@ is not a recognized PPD paper name, @ppdDisplayName@,
-- @width@ and @height@ are used to construct a custom 'PaperSize' object.
--
-- * Available since Gtk+ version 2.10
--
paperSizeNewFromPpd :: GlibString string
 => string -- ^ @ppdName@ - a PPD paper name
 -> string -- ^ @ppdDisplayName@ - the corresponding human-readable name
 -> Double -- ^ @width@ - the paper width, in points
 -> Double -- ^ @height@ - the paper height in points
 -> IO PaperSize
paperSizeNewFromPpd ppdName ppdDisplayName width height =
  withUTFString ppdDisplayName $ \ppdDisplayNamePtr ->
  withUTFString ppdName $ \ppdNamePtr ->
  {# call gtk_paper_size_new_from_ppd #}
    ppdNamePtr
    ppdDisplayNamePtr
    (realToFrac width)
    (realToFrac height)
  >>= mkPaperSize

-- | Creates a new 'PaperSize' object with the given
-- parameters.
--
-- * Available since Gtk+ version 2.10
--
paperSizeNewCustom :: GlibString string
 => string -- ^ @name@ - the paper name
 -> string -- ^ @displayName@ - the human-readable name
 -> Double -- ^ @width@ - the paper width, in units of @unit@
 -> Double -- ^ @height@ - the paper height, in units of @unit@
 -> Unit   -- ^ @unit@ - the unit for @width@ and @height@
 -> IO PaperSize
paperSizeNewCustom name displayName width height unit =
  withUTFString displayName $ \displayNamePtr ->
  withUTFString name $ \namePtr ->
  {# call gtk_paper_size_new_custom #}
    namePtr
    displayNamePtr
    (realToFrac width)
    (realToFrac height)
    ((fromIntegral . fromEnum) unit)
  >>= mkPaperSize


--------------------
-- Methods

-- | Copies an existing 'PaperSize'.
--
-- * Available since Gtk+ version 2.10
--
paperSizeCopy :: PaperSize
 -> IO PaperSize -- ^ returns a copy of @other@
paperSizeCopy self =
  {# call gtk_paper_size_copy #} self >>= mkPaperSize

-- | Compares two 'PaperSize' objects.
--
-- * Available since Gtk+ version 2.10
--
paperSizeIsEqual :: PaperSize
 -> PaperSize -- ^ @size2@ - another 'PaperSize' object
 -> IO Bool   -- ^ returns @True@, if @size1@ and @size2@ represent
             -- the same paper size
paperSizeIsEqual self size2 =
  liftM toBool $
  {# call gtk_paper_size_is_equal #}
    self
    size2

-- | Gets the name of the 'PaperSize'.
--
-- * Available since Gtk+ version 2.10
--
paperSizeGetName :: GlibString string => PaperSize
 -> IO string -- ^ returns the name of @size@
paperSizeGetName self =
  {# call gtk_paper_size_get_name #}
    self
  >>= peekUTFString

-- | Gets the human-readable name of the 'PaperSize'.
--
-- * Available since Gtk+ version 2.10
--
paperSizeGetDisplayName :: GlibString string => PaperSize
 -> IO string -- ^ returns the human-readable name of @size@
paperSizeGetDisplayName self =
  {# call gtk_paper_size_get_display_name #}
    self
  >>= peekUTFString

-- | Gets the PPD name of the 'PaperSize', which may be
--
-- * Available since Gtk+ version 2.10
--
paperSizeGetPpdName :: GlibString string => PaperSize
 -> IO (Maybe string)            -- ^ returns the PPD name of @size@, or 'Nothing'
paperSizeGetPpdName self =
  {# call gtk_paper_size_get_ppd_name #}
    self
  >>= maybePeekUTFString

-- | Gets the paper width of the 'PaperSize', in units
-- of @unit@.
--
-- * Available since Gtk+ version 2.10
--
paperSizeGetWidth :: PaperSize
 -> Unit      -- ^ @unit@ - the unit for the return value
 -> IO Double -- ^ returns the paper width
paperSizeGetWidth self unit =
  liftM realToFrac $
  {# call gtk_paper_size_get_width #}
    self
    ((fromIntegral . fromEnum) unit)

-- | Gets the paper height of the 'PaperSize', in units
-- of @unit@.
--
-- * Available since Gtk+ version 2.10
--
paperSizeGetHeight :: PaperSize
 -> Unit      -- ^ @unit@ - the unit for the return value
 -> IO Double -- ^ returns the paper height
paperSizeGetHeight self unit =
  liftM realToFrac $
  {# call gtk_paper_size_get_height #}
    self
    ((fromIntegral . fromEnum) unit)

-- | Returns @True@ if @size@ is not a standard paper size.
--
paperSizeIsCustom :: PaperSize
 -> IO Bool -- ^ returns whether @size@ is a custom paper size.
paperSizeIsCustom self =
  liftM toBool $
  {# call gtk_paper_size_is_custom #}
    self

-- | Changes the dimensions of a @size@ to @width@ x @height@.
--
-- * Available since Gtk+ version 2.10
--
paperSizeSetSize :: PaperSize
 -> Double -- ^ @width@ - the new width in units of @unit@
 -> Double -- ^ @height@ - the new height in units of @unit@
 -> Unit   -- ^ @unit@ - the unit for @width@ and @height@
 -> IO ()
paperSizeSetSize self width height unit =
  {# call gtk_paper_size_set_size #}
    self
    (realToFrac width)
    (realToFrac height)
    ((fromIntegral . fromEnum) unit)

-- | Gets the default top margin for the 'PaperSize'.
--
-- * Available since Gtk+ version 2.10
--
paperSizeGetDefaultTopMargin :: PaperSize
 -> Unit      -- ^ @unit@ - the unit for the return value
 -> IO Double -- ^ returns the default top margin
paperSizeGetDefaultTopMargin self unit =
  liftM realToFrac $
  {# call gtk_paper_size_get_default_top_margin #}
    self
    ((fromIntegral . fromEnum) unit)

-- | Gets the default bottom margin for the 'PaperSize'.
--
-- * Available since Gtk+ version 2.10
--
paperSizeGetDefaultBottomMargin :: PaperSize
 -> Unit      -- ^ @unit@ - the unit for the return value
 -> IO Double -- ^ returns the default bottom margin
paperSizeGetDefaultBottomMargin self unit =
  liftM realToFrac $
  {# call gtk_paper_size_get_default_bottom_margin #}
    self
    ((fromIntegral . fromEnum) unit)

-- | Gets the default left margin for the 'PaperSize'.
--
-- * Available since Gtk+ version 2.10
--
paperSizeGetDefaultLeftMargin :: PaperSize
 -> Unit      -- ^ @unit@ - the unit for the return value
 -> IO Double -- ^ returns the default left margin
paperSizeGetDefaultLeftMargin self unit =
  liftM realToFrac $
  {# call gtk_paper_size_get_default_left_margin #}
    self
    ((fromIntegral . fromEnum) unit)

-- | Gets the default right margin for the 'PaperSize'.
--
-- * Available since Gtk+ version 2.10
--
paperSizeGetDefaultRightMargin :: PaperSize
 -> Unit      -- ^ @unit@ - the unit for the return value
 -> IO Double -- ^ returns the default right margin
paperSizeGetDefaultRightMargin self unit =
  liftM realToFrac $
  {# call gtk_paper_size_get_default_right_margin #}
    self
    ((fromIntegral . fromEnum) unit)

-- | Returns the name of the default paper size, which depends on the current
-- locale.
--
-- * Available since Gtk+ version 2.10
--
paperSizeGetDefault :: GlibString string
 => IO string -- ^ returns the name of the default paper size.
paperSizeGetDefault =
  {# call gtk_paper_size_get_default #}
  >>= peekUTFString

#endif

#if GTK_CHECK_VERSION(2,12,0)
-- | Creates a list of known paper sizes.
--
-- * Available since Gtk+ version 2.12
--
paperSizeGetPaperSizes ::
    Bool                    -- ^ @includeCustom@ - whether to include custom
                            -- paper sizes as defined in the page setup dialog
 -> IO [PaperSize]
paperSizeGetPaperSizes includeCustom = do
  glist <- {# call gtk_paper_size_get_paper_sizes #} (fromBool includeCustom)
  list <- fromGList glist
  mapM mkPaperSize list
#endif