packages feed

lhae-0.0.2: src/View/Component/Notebook.hs

module View.Component.Notebook 
    (Notebook,GridId,new,addGrid,deleteCurrentView
    ,currentSelectionNum,currentSelectionId,getGrid
    ,getCurrentCaption,setCurrentCaption,pageId,numPages)
    where

import Control.Applicative ((<$>))
import qualified Graphics.UI.WX as WX
import qualified Graphics.UI.WXCore as WXC
import View.Component.Grid (Grid)

type Notebook = WXC.Notebook ()
type GridId = Int

new :: WX.Window a -> IO Notebook
new parent =
    let createFromParent id rect props _ = do
          n <- WXC.notebookCreate parent id rect WXC.wxNB_BOTTOM
          WX.set n props
          return n
    in
      WX.initialWindow createFromParent [] 0

addGrid :: Grid -> String -> Notebook -> IO ()
addGrid grid caption notebook = do
  _ <- WXC.notebookAddPage notebook grid caption True (-1)
  return ()

deleteCurrentView :: Notebook -> IO ()
deleteCurrentView notebook = do
  _ <- WXC.notebookGetSelection notebook >>=
       WXC.notebookDeletePage notebook
  return ()

currentSelectionNum :: Notebook -> IO (Maybe Int)
currentSelectionNum notebook = do
  n <- numPages notebook
  if n == 0 then return Nothing
   else Just <$> WXC.notebookGetSelection notebook

currentSelectionId :: Notebook -> IO (Maybe GridId)
currentSelectionId notebook = do
  n <- numPages notebook
  if n == 0 then return Nothing
   else Just <$> (WXC.notebookGetSelection notebook >>=
                  flip pageId notebook)

pageId :: Int -> Notebook -> IO GridId
pageId n notebook = WXC.notebookGetPage notebook n >>= WXC.windowGetId

getGrid :: Int -> Notebook -> IO Grid
getGrid n notebook = WX.objectCast <$> WXC.notebookGetPage notebook n

setCurrentCaption :: String -> Notebook -> IO ()
setCurrentCaption caption notebook = do
  current <- WXC.notebookGetSelection notebook
  _ <- WXC.notebookSetPageText notebook current caption
  return ()

getCurrentCaption :: Notebook -> IO String
getCurrentCaption notebook = WXC.notebookGetSelection notebook 
                             >>= WXC.notebookGetPageText notebook

numPages :: Notebook -> IO Int
numPages = WXC.notebookGetPageCount