{-# LINE 2 "./Graphics/UI/Gtk/Cairo.chs" #-}
-- -*-haskell-*-
-- GIMP Toolkit (GTK) Cairo GDK integration
--
-- Author : Duncan Coutts
--
-- Created: 17 August 2005
--
-- Copyright (C) 2005 Duncan Coutts
--
-- 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.
--
-- #hide

-- |
-- Maintainer : gtk2hs-users@lists.sourceforge.net
-- Stability : provisional
-- Portability : portable (depends on GHC)
--
--
-- Gtk specific functions to for rendering with Cairo.
--
-- Cairo is a graphics library that supports vector graphics and image
-- compositing that can be used with Gdk.
-- The Cairo API is an addition to Gdk\/Gtk (rather than a replacement).
-- Cairo rendering can be performed on any 'Graphics.UI.Gtk.Gdk.Drawable'
-- by calling 'renderWithDrawable'. The functions in this module provide
-- ways of drawing Gtk specific elements, such as 'Pixbuf's or text
-- laid out with Pango.
--
-- All functions in this module are only available in Gtk 2.8 or higher.
--
module Graphics.UI.Gtk.Cairo (

  -- * Global Cairo settings.
  cairoFontMapGetDefault,
  cairoFontMapSetResolution,
  cairoFontMapGetResolution,
  cairoCreateContext,
  cairoContextSetResolution,
  cairoContextGetResolution,
  cairoContextSetFontOptions,
  cairoContextGetFontOptions,
  -- * Functions for the 'Render' monad.

  renderWithDrawable,




  region,
  setSourceColor,
  setSourcePixbuf,
  rectangle,
  updateContext,
  createLayout,
  updateLayout,
  showGlyphString,
  showLayoutLine,
  showLayout,
  glyphStringPath,
  layoutLinePath,
  layoutPath

  ) where

import Control.Exception (bracket)

import System.Glib.FFI
import Graphics.UI.Gtk.Types
{-# LINE 79 "./Graphics/UI/Gtk/Cairo.chs" #-}

import Graphics.UI.Gtk.Gdk.Region (Region(..))

import Graphics.Rendering.Pango.Cairo
{-# LINE 83 "./Graphics/UI/Gtk/Cairo.chs" #-}



import Graphics.Rendering.Cairo.Types as Cairo hiding (Region)



import qualified Graphics.Rendering.Cairo.Internal as Cairo.Internal
import Graphics.Rendering.Cairo.Internal (Render(Render))
import Control.Monad.Reader
import Graphics.UI.Gtk.General.Structs (Rectangle(..))

import Control.Monad (unless, liftM)


{-# LINE 98 "./Graphics/UI/Gtk/Cairo.chs" #-}

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



-- | Creates a Cairo context for drawing to a 'Drawable'.
--
-- Removed in Gtk3.
renderWithDrawable :: DrawableClass drawable =>
    drawable -- ^ @drawable@ - a 'Drawable'
 -> Render a -- ^ A newly created Cairo context.
 -> IO a
renderWithDrawable :: forall drawable a.
DrawableClass drawable =>
drawable -> Render a -> IO a
renderWithDrawable drawable
drawable Render a
m =
  IO Cairo -> (Cairo -> IO ()) -> (Cairo -> IO a) -> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket ((Ptr Cairo -> Cairo) -> IO (Ptr Cairo) -> IO Cairo
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Ptr Cairo -> Cairo
Cairo.Cairo (IO (Ptr Cairo) -> IO Cairo) -> IO (Ptr Cairo) -> IO Cairo
forall a b. (a -> b) -> a -> b
$ (\(Drawable ForeignPtr Drawable
arg1) -> ForeignPtr Drawable
-> (Ptr Drawable -> IO (Ptr Cairo)) -> IO (Ptr Cairo)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Drawable
arg1 ((Ptr Drawable -> IO (Ptr Cairo)) -> IO (Ptr Cairo))
-> (Ptr Drawable -> IO (Ptr Cairo)) -> IO (Ptr Cairo)
forall a b. (a -> b) -> a -> b
$ \Ptr Drawable
argPtr1 ->Ptr Drawable -> IO (Ptr Cairo)
gdk_cairo_create Ptr Drawable
argPtr1) (drawable -> Drawable
forall o. DrawableClass o => o -> Drawable
toDrawable drawable
drawable))
          (\Cairo
context -> do Status
status <- Cairo -> IO Status
Cairo.Internal.status Cairo
context
                          Cairo -> IO ()
Cairo.Internal.destroy Cairo
context
                          Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Status
status Status -> Status -> Bool
forall a. Eq a => a -> a -> Bool
== Status
Cairo.StatusSuccess) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
                            String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO ()) -> IO String -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Status -> IO String
Cairo.Internal.statusToString Status
status)
          (\Cairo
context -> ReaderT Cairo IO a -> Cairo -> IO a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (Render a -> ReaderT Cairo IO a
forall m. Render m -> ReaderT Cairo IO m
Cairo.Internal.runRender Render a
m) Cairo
context)
{-# LINE 149 "./Graphics/UI/Gtk/Cairo.chs" #-}
-- | Sets the given pixbuf as the source pattern for the Cairo context. The
-- pattern has an extend mode of 'ExtendNone' and is aligned so that the
-- origin of pixbuf is @(x, y)@.
--
setSourcePixbuf ::
    Pixbuf
 -> Double -- ^ x
 -> Double -- ^ y
 -> Render ()
setSourcePixbuf :: Pixbuf -> Double -> Double -> Render ()
setSourcePixbuf Pixbuf
pixbuf Double
pixbufX Double
pixbufY = ReaderT Cairo IO () -> Render ()
forall m. ReaderT Cairo IO m -> Render m
Render (ReaderT Cairo IO () -> Render ())
-> ReaderT Cairo IO () -> Render ()
forall a b. (a -> b) -> a -> b
$ do
  Cairo
cr <- ReaderT Cairo IO Cairo
forall r (m :: * -> *). MonadReader r m => m r
ask
  IO () -> ReaderT Cairo IO ()
forall a. IO a -> ReaderT Cairo IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ReaderT Cairo IO ()) -> IO () -> ReaderT Cairo IO ()
forall a b. (a -> b) -> a -> b
$ (\(Cairo Ptr Cairo
arg1) (Pixbuf ForeignPtr Pixbuf
arg2) CDouble
arg3 CDouble
arg4 -> ForeignPtr Pixbuf -> (Ptr Pixbuf -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Pixbuf
arg2 ((Ptr Pixbuf -> IO ()) -> IO ()) -> (Ptr Pixbuf -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Pixbuf
argPtr2 ->Ptr Cairo -> Ptr Pixbuf -> CDouble -> CDouble -> IO ()
gdk_cairo_set_source_pixbuf Ptr Cairo
arg1 Ptr Pixbuf
argPtr2 CDouble
arg3 CDouble
arg4)
{-# LINE 161 "./Graphics/UI/Gtk/Cairo.chs" #-}
    cr
    Pixbuf
pixbuf
    (Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
pixbufX)
    (Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
pixbufY)

-- | Adds the given region to the current path of the 'Render' context.
rectangle :: Rectangle -> Render ()
rectangle :: Rectangle -> Render ()
rectangle Rectangle
rect = ReaderT Cairo IO () -> Render ()
forall m. ReaderT Cairo IO m -> Render m
Render (ReaderT Cairo IO () -> Render ())
-> ReaderT Cairo IO () -> Render ()
forall a b. (a -> b) -> a -> b
$ do
  Cairo
cr <- ReaderT Cairo IO Cairo
forall r (m :: * -> *). MonadReader r m => m r
ask
  IO () -> ReaderT Cairo IO ()
forall a. IO a -> ReaderT Cairo IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ReaderT Cairo IO ()) -> IO () -> ReaderT Cairo IO ()
forall a b. (a -> b) -> a -> b
$ Rectangle -> (Ptr Rectangle -> IO ()) -> IO ()
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with Rectangle
rect ((Ptr Rectangle -> IO ()) -> IO ())
-> (Ptr Rectangle -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr Rectangle
rectPtr ->
    (\(Cairo Ptr Cairo
arg1) Ptr ()
arg2 -> Ptr Cairo -> Ptr () -> IO ()
gdk_cairo_rectangle Ptr Cairo
arg1 Ptr ()
arg2)
{-# LINE 172 "./Graphics/UI/Gtk/Cairo.chs" #-}
      cr
      (Ptr Rectangle -> Ptr ()
forall a b. Ptr a -> Ptr b
castPtr Ptr Rectangle
rectPtr)

-- | Adds the given region to the current path of the 'Render' context.
region :: Region -> Render ()
region :: Region -> Render ()
region Region
region = ReaderT Cairo IO () -> Render ()
forall m. ReaderT Cairo IO m -> Render m
Render (ReaderT Cairo IO () -> Render ())
-> ReaderT Cairo IO () -> Render ()
forall a b. (a -> b) -> a -> b
$ do
  Cairo
cr <- ReaderT Cairo IO Cairo
forall r (m :: * -> *). MonadReader r m => m r
ask
  IO () -> ReaderT Cairo IO ()
forall a. IO a -> ReaderT Cairo IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ReaderT Cairo IO ()) -> IO () -> ReaderT Cairo IO ()
forall a b. (a -> b) -> a -> b
$ (\(Cairo Ptr Cairo
arg1) (Region ForeignPtr Region
arg2) -> ForeignPtr Region -> (Ptr Region -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Region
arg2 ((Ptr Region -> IO ()) -> IO ()) -> (Ptr Region -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Region
argPtr2 ->Ptr Cairo -> Ptr Region -> IO ()
gdk_cairo_region Ptr Cairo
arg1 Ptr Region
argPtr2)
{-# LINE 180 "./Graphics/UI/Gtk/Cairo.chs" #-}
    cr
    Region
region

foreign import ccall unsafe "gdk_cairo_create"
  gdk_cairo_create :: ((Ptr Drawable) -> (IO (Ptr Cairo)))

foreign import ccall unsafe "gdk_cairo_set_source_pixbuf"
  gdk_cairo_set_source_pixbuf :: ((Ptr Cairo) -> ((Ptr Pixbuf) -> (CDouble -> (CDouble -> (IO ())))))

foreign import ccall unsafe "gdk_cairo_rectangle"
  gdk_cairo_rectangle :: ((Ptr Cairo) -> ((Ptr ()) -> (IO ())))

foreign import ccall unsafe "gdk_cairo_region"
  gdk_cairo_region :: ((Ptr Cairo) -> ((Ptr Region) -> (IO ())))