{-# LINE 2 "./Graphics/UI/Gtk/Gdk/GLContext.chs" #-}
-- -*-haskell-*-
-- GIMP Toolkit (GTK) GLContext
--
-- Author : Chris Mennie
--
-- Created: 23 April 2016
--
-- Copyright (C) 2016 Chis Mennie
--
-- 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)
--
-- OpenGL context
--
module Graphics.UI.Gtk.Gdk.GLContext (
-- * Detail
--
-- | GLContext is an object representing the platform-specific OpenGL drawing context.
--
-- GLContexts are created for a GdkWindow, and the context will match the GdkVisual of the window.
--
-- A 'GLContext' is not tied to any particular normal framebuffer. For instance, it cannot draw to
-- the Window back buffer. The GDK repaint system is in full control of the painting to that.
-- GDK will handle the integration of your rendering with that of other widgets.
--
-- Support for 'GLContext' is platform-specific, context creation can fail, returning NULL context.
--
-- A 'GLContext' has to be made "current" in order to start using it, otherwise any OpenGL call will
-- be ignored.

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

-- * Types

  GLContext,
  GLContextClass,
  castToGLContext, gTypeGLContext,


-- * Methods

    glContextGetDisplay,
    glContextGetWindow,
    glContextGetSharedContext,
    glContextGetVersion,
    glContextSetRequiredVersion,
    glContextGetRequiredVersion,
    glContextSetDebugEnabled,
    glContextGetDebugEnabled,
    glContextSetForwardCompatible,
    glContextGetForwardCompatible,
    glContextRealize,


    glContextIsLegacy,


    glContextMakeCurrent,
    glContextGetCurrent,
    glContextClearCurrent

  ) where

import Control.Monad (liftM)
import Data.Maybe (fromMaybe)

import System.Glib.FFI
import System.Glib.Flags (toFlags)
import Graphics.UI.Gtk.Types
{-# LINE 89 "./Graphics/UI/Gtk/Gdk/GLContext.chs" #-}
import Graphics.UI.Gtk.Gdk.Enums
{-# LINE 90 "./Graphics/UI/Gtk/Gdk/GLContext.chs" #-}
import Graphics.UI.Gtk.Gdk.Cursor
{-# LINE 91 "./Graphics/UI/Gtk/Gdk/GLContext.chs" #-}
import Graphics.UI.Gtk.General.Structs
import System.Glib.GError (propagateGError)
import System.Glib.Attributes


{-# LINE 96 "./Graphics/UI/Gtk/Gdk/GLContext.chs" #-}


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


-- | Retrieves the 'Graphics.UI.Gtk.Gdk.Display.Display' the context is created for.
--
glContextGetDisplay :: GLContextClass self => self -> IO (Maybe Display)
glContextGetDisplay self = do
    maybeNull (wrapNewGObject mkDisplay) $
        (\(GLContext arg1) -> withForeignPtr arg1 $ \argPtr1 ->gdk_gl_context_get_display argPtr1) (toGLContext self)


-- | Retrieves the 'Graphics.UI.Gtk.Gdk.DrawWindow.DrawWindow' used by the context.
--
glContextGetWindow :: GLContextClass self => self -> IO (Maybe DrawWindow)
glContextGetWindow self = do
    maybeNull (wrapNewGObject mkDrawWindow) $
        (\(GLContext arg1) -> withForeignPtr arg1 $ \argPtr1 ->gdk_gl_context_get_window argPtr1) (toGLContext self)


-- | Retrieves the 'GLContext' that this context share data with.
--
glContextGetSharedContext :: GLContextClass self => self -> IO (Maybe GLContext)
glContextGetSharedContext self = do
    maybeNull (wrapNewGObject mkGLContext) $
        (\(GLContext arg1) -> withForeignPtr arg1 $ \argPtr1 ->gdk_gl_context_get_shared_context argPtr1) (toGLContext self)


-- | Retrieves the OpenGL version of the context.
--
-- The context must be realized prior to calling this function.
--
glContextGetVersion :: GLContextClass self => self -> IO (Int, Int)
glContextGetVersion self = do
    alloca $ \majorPtr -> alloca $ \minorPtr -> do
        (\(GLContext arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gdk_gl_context_get_version argPtr1 arg2 arg3) (toGLContext self) majorPtr minorPtr
        major <- peek majorPtr
        minor <- peek minorPtr
        return (fromIntegral major, fromIntegral minor)


-- | Sets the major and minor version of OpenGL to request.
--
-- Setting major and minor to zero will use the default values.
--
-- The 'GLContext' must not be realized or made current prior to calling this function.
--
glContextSetRequiredVersion :: GLContextClass self => self -> Int -> Int -> IO ()
glContextSetRequiredVersion self major minor =
    (\(GLContext arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gdk_gl_context_set_required_version argPtr1 arg2 arg3)
{-# LINE 148 "./Graphics/UI/Gtk/Gdk/GLContext.chs" #-}
        (toGLContext self) (fromIntegral major) (fromIntegral minor)


-- | Retrieves the major and minor version requested by calling 'glContextSetRequiredVersion'.
--
glContextGetRequiredVersion :: GLContextClass self => self -> IO (Int, Int)
glContextGetRequiredVersion self = do
    alloca $ \majorPtr -> alloca $ \minorPtr -> do
        (\(GLContext arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gdk_gl_context_get_required_version argPtr1 arg2 arg3) (toGLContext self) majorPtr minorPtr
        major <- peek majorPtr
        minor <- peek minorPtr
        return (fromIntegral major, fromIntegral minor)


-- | Sets whether the 'GLContext' should perform extra validations and run time checking. This is
-- useful during development, but has additional overhead.
--
-- The 'GLContext' must not be realized or made current prior to calling this function.
--
glContextSetDebugEnabled :: GLContextClass self => self -> Bool -> IO ()
glContextSetDebugEnabled self enabled = do
    (\(GLContext arg1) arg2 -> withForeignPtr arg1 $ \argPtr1 ->gdk_gl_context_set_debug_enabled argPtr1 arg2) (toGLContext self) (fromBool enabled)


-- | Retrieves the value set using glContextSetDebugEnabled.
--
glContextGetDebugEnabled :: GLContextClass self => self -> IO Bool
glContextGetDebugEnabled self = do
    liftM toBool $ (\(GLContext arg1) -> withForeignPtr arg1 $ \argPtr1 ->gdk_gl_context_get_debug_enabled argPtr1) (toGLContext self)


-- | Sets whether the 'GLContext' should be forward compatible.
--
-- Forward compatibile contexts must not support OpenGL functionality that has been marked as
-- deprecated in the requested version; non-forward compatible contexts, on the other hand, must
-- support both deprecated and non deprecated functionality.
--
-- The 'GLContext' must not be realized or made current prior to calling this function.
--
glContextSetForwardCompatible :: GLContextClass self => self -> Bool -> IO ()
glContextSetForwardCompatible self compatible = do
    (\(GLContext arg1) arg2 -> withForeignPtr arg1 $ \argPtr1 ->gdk_gl_context_set_forward_compatible argPtr1 arg2) (toGLContext self) (fromBool compatible)


-- | Retrieves the value set using glContextSetForwardCompatible.
--
glContextGetForwardCompatible :: GLContextClass self => self -> IO Bool
glContextGetForwardCompatible self = do
    liftM toBool $ (\(GLContext arg1) -> withForeignPtr arg1 $ \argPtr1 ->gdk_gl_context_get_forward_compatible argPtr1) (toGLContext self)




-- | Whether the 'GLContext' is in legacy mode or not.
--
-- The 'GLContext' must be realized before calling this function.
--
-- When realizing a GL context, GDK will try to use the OpenGL 3.2 core profile; this profile
-- removes all the OpenGL API that was deprecated prior to the 3.2 version of the specification.
-- If the realization is successful, this function will return False.
--
-- If the underlying OpenGL implementation does not support core profiles, GDK will fall back to
-- a pre-3.2 compatibility profile, and this function will return True.
--
-- You can use the value returned by this function to decide which kind of OpenGL API to use, or
-- whether to do extension discovery, or what kind of shader programs to load.
--
glContextIsLegacy :: GLContextClass self => self -> IO Bool
glContextIsLegacy self = do
    liftM toBool $ (\(GLContext arg1) -> withForeignPtr arg1 $ \argPtr1 ->gdk_gl_context_is_legacy argPtr1) (toGLContext self)




-- | Realizes the given 'GLContext'.
--
-- It is safe to call this function on a realized 'GLContext'.
--
glContextRealize :: GLContextClass self => self -> IO Bool
glContextRealize self =
    liftM toBool $
        propagateGError $ \errPtr ->
            (\(GLContext arg1) arg2 -> withForeignPtr arg1 $ \argPtr1 ->gdk_gl_context_realize argPtr1 arg2) (toGLContext self) errPtr


-- | Makes the context the current one.
--
glContextMakeCurrent :: GLContextClass self => self -> IO ()
glContextMakeCurrent self = do
    (\(GLContext arg1) -> withForeignPtr arg1 $ \argPtr1 ->gdk_gl_context_make_current argPtr1) (toGLContext self)


-- | Retrieves the current 'GLContext'.
--
glContextGetCurrent :: IO (Maybe GLContext)
glContextGetCurrent = do
    maybeNull (wrapNewGObject mkGLContext) $
        gdk_gl_context_get_current
{-# LINE 246 "./Graphics/UI/Gtk/Gdk/GLContext.chs" #-}


-- | Clears the current 'GLContext'.
--
-- Any OpenGL call after this function returns will be ignored until glContextMakeCurrent
-- is called.
--
glContextClearCurrent :: IO ()
glContextClearCurrent = do
    gdk_gl_context_clear_current
{-# LINE 256 "./Graphics/UI/Gtk/Gdk/GLContext.chs" #-}

foreign import ccall safe "gdk_gl_context_get_display"
  gdk_gl_context_get_display :: ((Ptr GLContext) -> (IO (Ptr Display)))

foreign import ccall safe "gdk_gl_context_get_window"
  gdk_gl_context_get_window :: ((Ptr GLContext) -> (IO (Ptr DrawWindow)))

foreign import ccall safe "gdk_gl_context_get_shared_context"
  gdk_gl_context_get_shared_context :: ((Ptr GLContext) -> (IO (Ptr GLContext)))

foreign import ccall safe "gdk_gl_context_get_version"
  gdk_gl_context_get_version :: ((Ptr GLContext) -> ((Ptr CInt) -> ((Ptr CInt) -> (IO ()))))

foreign import ccall safe "gdk_gl_context_set_required_version"
  gdk_gl_context_set_required_version :: ((Ptr GLContext) -> (CInt -> (CInt -> (IO ()))))

foreign import ccall safe "gdk_gl_context_get_required_version"
  gdk_gl_context_get_required_version :: ((Ptr GLContext) -> ((Ptr CInt) -> ((Ptr CInt) -> (IO ()))))

foreign import ccall safe "gdk_gl_context_set_debug_enabled"
  gdk_gl_context_set_debug_enabled :: ((Ptr GLContext) -> (CInt -> (IO ())))

foreign import ccall safe "gdk_gl_context_get_debug_enabled"
  gdk_gl_context_get_debug_enabled :: ((Ptr GLContext) -> (IO CInt))

foreign import ccall safe "gdk_gl_context_set_forward_compatible"
  gdk_gl_context_set_forward_compatible :: ((Ptr GLContext) -> (CInt -> (IO ())))

foreign import ccall safe "gdk_gl_context_get_forward_compatible"
  gdk_gl_context_get_forward_compatible :: ((Ptr GLContext) -> (IO CInt))

foreign import ccall safe "gdk_gl_context_is_legacy"
  gdk_gl_context_is_legacy :: ((Ptr GLContext) -> (IO CInt))

foreign import ccall safe "gdk_gl_context_realize"
  gdk_gl_context_realize :: ((Ptr GLContext) -> ((Ptr (Ptr ())) -> (IO CInt)))

foreign import ccall safe "gdk_gl_context_make_current"
  gdk_gl_context_make_current :: ((Ptr GLContext) -> (IO ()))

foreign import ccall safe "gdk_gl_context_get_current"
  gdk_gl_context_get_current :: (IO (Ptr GLContext))

foreign import ccall safe "gdk_gl_context_clear_current"
  gdk_gl_context_clear_current :: (IO ())