File: PrintContext.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 (226 lines) | stat: -rw-r--r-- 7,368 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
{-# LANGUAGE CPP, OverloadedStrings #-}
-- -*-haskell-*-
--  GIMP Toolkit (GTK) Widget PrintContext
--
--  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)
--
-- Encapsulates context for drawing pages
--
-- * Module available since Gtk+ version 2.10
--
module Graphics.UI.Gtk.Printing.PrintContext (

-- * Detail
--
-- | A 'PrintContext' encapsulates context information that is required when
-- drawing pages for printing, such as the cairo context and important
-- parameters like page size and resolution. It also lets you easily create
-- 'PangoLayout' and 'Context' objects that match the font metrics of the cairo
-- surface.
--
-- 'PrintContext' objects gets passed to the 'beginPrint', 'endPrint',
-- 'requestPageSetup' and 'drawPage' signals on the 'PrintOperation'.
--
-- Printing support was added in Gtk+ 2.10.

-- * Class Hierarchy
--
-- |
-- @
-- |  'GObject'
-- |   +----PrintContext
-- @

#if GTK_CHECK_VERSION(2,10,0)
-- * Types
  PrintContext,
  PrintContextClass,
  castToPrintContext,
  toPrintContext,

-- * Methods
  printContextGetCairoContext,
  printContextSetCairoContext,
  printContextGetPageSetup,
  printContextGetWidth,
  printContextGetHeight,
  printContextGetDpiX,
  printContextGetDpiY,
  printContextGetPangoFontmap,
  printContextCreatePangoContext,
  printContextCreatePangoLayout,
#if GTK_CHECK_VERSION(2,20,0)
  printContextGetHardMargins,
#endif
#endif
  ) where

import Control.Monad    (liftM)
import Data.IORef (newIORef)

import System.Glib.FFI
import System.Glib.UTFString
{#import Graphics.UI.Gtk.Types#}
{#import Graphics.Rendering.Pango.Types#}
{#import Graphics.Rendering.Pango.BasicTypes#}
{#import Graphics.Rendering.Cairo.Types#}

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

#if GTK_CHECK_VERSION(2,10,0)
--------------------
-- Methods

-- | Obtains the cairo context that is associated with the 'PrintContext'.
--
printContextGetCairoContext :: PrintContextClass self => self
 -> IO Cairo -- ^ returns the cairo context of @context@
printContextGetCairoContext self =
  liftM Cairo $
  {# call gtk_print_context_get_cairo_context #} (toPrintContext self)

-- | Sets a new cairo context on a print context.
--
-- This function is intended to be used when implementing an internal print
-- preview, it is not needed for printing, since Gtk+ itself creates a suitable
-- cairo context in that case.
--
printContextSetCairoContext :: PrintContextClass self => self
 -> Cairo -- ^ @cr@ - the cairo context
 -> Double       -- ^ @dpiX@ - the horizontal resolution to use with @cr@
 -> Double       -- ^ @dpiY@ - the vertical resolution to use with @cr@
 -> IO ()
printContextSetCairoContext self cr dpiX dpiY =
  {# call gtk_print_context_set_cairo_context #}
    (toPrintContext self)
    cr
    (realToFrac dpiX)
    (realToFrac dpiY)

-- | Obtains the 'PageSetup' that determines the page dimensions of the
-- 'PrintContext'.
--
printContextGetPageSetup :: PrintContextClass self => self
 -> IO PageSetup -- ^ returns the page setup of @context@
printContextGetPageSetup self =
  makeNewGObject mkPageSetup $
  {# call gtk_print_context_get_page_setup #}
    (toPrintContext self)

-- | Obtains the width of the 'PrintContext', in pixels.
--
printContextGetWidth :: PrintContextClass self => self
 -> IO Double -- ^ returns the width of @context@
printContextGetWidth self =
  liftM realToFrac $
  {# call gtk_print_context_get_width #}
    (toPrintContext self)

-- | Obtains the height of the 'PrintContext', in pixels.
--
printContextGetHeight :: PrintContextClass self => self
 -> IO Double -- ^ returns the height of @context@
printContextGetHeight self =
  liftM realToFrac $
  {# call gtk_print_context_get_height #}
    (toPrintContext self)

-- | Obtains the horizontal resolution of the 'PrintContext', in dots per
-- inch.
--
printContextGetDpiX :: PrintContextClass self => self
 -> IO Double -- ^ returns the horizontal resolution of @context@
printContextGetDpiX self =
  liftM realToFrac $
  {# call gtk_print_context_get_dpi_x #}
    (toPrintContext self)

-- | Obtains the vertical resolution of the 'PrintContext', in dots per inch.
--
printContextGetDpiY :: PrintContextClass self => self
 -> IO Double -- ^ returns the vertical resolution of @context@
printContextGetDpiY self =
  liftM realToFrac $
  {# call gtk_print_context_get_dpi_y #}
    (toPrintContext self)

-- | Returns a 'FontMap' that is suitable for use with the 'PrintContext'.
--
printContextGetPangoFontmap :: PrintContextClass self => self
 -> IO FontMap -- ^ returns the font map of @context@
printContextGetPangoFontmap self =
  makeNewGObject mkFontMap $
  {# call gtk_print_context_get_pango_fontmap #}
    (toPrintContext self)

-- | Creates a new 'Context' that can be used with the 'PrintContext'.
--
printContextCreatePangoContext :: PrintContextClass self => self
 -> IO PangoContext -- ^ returns a new Pango context for @context@
printContextCreatePangoContext self =
  wrapNewGObject mkPangoContext $
  {# call gtk_print_context_create_pango_context #}
    (toPrintContext self)

-- | Creates a new 'PangoLayout' that is suitable for use with the
-- 'PrintContext'.
--
printContextCreatePangoLayout :: PrintContextClass self => self
 -> IO PangoLayout -- ^ returns a new Pango layout for @context@
printContextCreatePangoLayout self = do
  pl <- wrapNewGObject mkPangoLayoutRaw $
    {# call gtk_print_context_create_pango_layout #}
    (toPrintContext self)
  ps <- makeNewPangoString (""::DefaultGlibString)
  psRef <- newIORef ps
  return (PangoLayout psRef pl)

#if GTK_CHECK_VERSION(2,20,0)
printContextGetHardMargins :: PrintContextClass self => self
                           -> IO (Maybe (Double, Double, Double, Double))
                             -- ^ returns @(top, bottom, left, right)@
                             -- @top@ top hardware printer margin
                             -- @bottom@ bottom hardware printer margin
                             -- @left@ left hardware printer margin
                             -- @right@ right hardware printer margin
printContextGetHardMargins self =
  alloca $ \ topPtr ->
  alloca $ \ bottomPtr ->
  alloca $ \ leftPtr ->
  alloca $ \ rightPtr -> do
  success <- liftM toBool $ {#call gtk_print_context_get_hard_margins #}
               (toPrintContext self)
               topPtr
               bottomPtr
               leftPtr
               rightPtr
  if success
     then do
       top <- liftM realToFrac $ peek topPtr
       bottom <- liftM realToFrac $ peek bottomPtr
       left <- liftM realToFrac $ peek leftPtr
       right <- liftM realToFrac $ peek rightPtr
       return $ Just (top, bottom, left, right)
     else return Nothing
#endif

#endif