packages feed

coin-1.1: src/Coin/UI/Builder/GtkUIUtils.hs

{-
 *  Programmer:	Piotr Borek
 *  E-mail:     piotrborek@op.pl
 *  Copyright 2015 Piotr Borek
 *
 *  Distributed under the terms of the GPL (GNU Public License)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-}

module Coin.UI.Builder.GtkUIUtils (
    uiBuildGtk',
    uiBuildGtk,
    recordUI,
    recordUI',
    record
) where

import qualified Data.Map.Strict as Map
import qualified Graphics.UI.Gtk as Gtk
import Graphics.UI.Gtk ( AttrOp(..) )

import Control.Monad
import Control.Monad.State.Strict
import Data.Maybe
import Control.Lens

import Coin.UI.Utils.CssUtils
import Coin.UI.Builder.GtkUIBuilderState
import Coin.UI.Builder.GtkUIAttributes

uiGetObject :: Map.Map String Gtk.Widget -> String -> Maybe Gtk.Widget
uiGetObject hashMap name = Map.lookup name hashMap

uiBuildGtk' :: UIBuilderState -> UIBuilder a -> IO (Map.Map String Gtk.Widget, [WidgetData])
uiBuildGtk' st builder = do
    bs <- execUIBuilder $ do
        uiStatePack .= st^.uiStatePack
        uiStateCss .= st^.uiStateCss
        builder
    return (bs^.uiStateMap, bs^.uiStateList)

uiBuildGtk :: UIBuilder a -> IO (String -> Maybe Gtk.Widget, Gtk.Widget)
uiBuildGtk builder = do
    (hashMap, widgetList) <- uiBuildGtk' builderStateEmpty builder
    return (uiGetObject hashMap, fromJust $  widgetList^?traverse.wdWidget)

recordUI :: UIBuilder a -> (Gtk.Widget -> IO ()) -> UIBuilder a
recordUI builder f = do
    st <- get
    (hashMap, widgetList) <- liftIO $ uiBuildGtk' st builder
    case widgetList^?traverse.wdWidget of
        Just root -> liftIO $ f root
        Nothing   -> return ()
    uiStateMap %= Map.union hashMap

recordUI' :: UIBuilder a -> (Gtk.Widget -> Gtk.Packing -> Int -> IO ()) -> UIBuilder a
recordUI' builder f = do
    st <- get
    (hashMap, widgetList) <- liftIO $ uiBuildGtk' st builder
    liftIO $ forM_ widgetList $ \(WidgetData widget packing padding) -> do
        f widget packing padding
    uiStateMap %= Map.union hashMap

record :: Gtk.WidgetClass cls => Maybe String -> cls -> UIBuilder a
record name widget = do
    packing <- use uiStatePack
    css     <- use uiStateCss

    when (css /= []) $ liftIO $ Gtk.set widget [cssStyle := css]

    let wd = WidgetData {
                 _wdWidget  = Gtk.castToWidget widget,
                 _wdPacking = packing^._1,
                 _wdPadding = packing^._2
             }

    uiStateList <>= [wd]
    when (isJust name) $ do
        uiStateMap %= Map.insert (fromJust name) (Gtk.castToWidget widget)
    setWidgetAttributes widget

setWidgetAttributes :: Gtk.WidgetClass cls => cls -> UIBuilder a
setWidgetAttributes widget = do
    attrs <- use uiStateAttrs

    liftIO $ case attrs of
        WidgetAttrs attrs'    -> Gtk.set (Gtk.castToWidget widget) attrs'
        WindowAttrs attrs'    -> Gtk.set (Gtk.castToWindow widget) attrs'
        ButtonAttrs attrs'    -> Gtk.set (Gtk.castToButton widget) attrs'
        LabelAttrs  attrs'    -> Gtk.set (Gtk.castToLabel widget) attrs'
        FrameAttrs  attrs'    -> Gtk.set (Gtk.castToFrame widget) attrs'
        ArrowAttrs  attrs'    -> Gtk.set (Gtk.castToArrow widget) attrs'
        EntryAttrs  attrs'    -> Gtk.set (Gtk.castToEntry widget) attrs'
        GridAttrs   attrs'    -> Gtk.set (Gtk.castToGrid widget) attrs'
        ContainerAttrs attrs' -> Gtk.set (Gtk.castToContainer widget) attrs'
        _                     -> return ()

    uiStateAttrs .= EmptyAttrs