This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
phi/lib/Phi/Widget.hs

120 lines
4.3 KiB
Haskell
Raw Normal View History

2011-07-14 01:47:10 +02:00
{-# LANGUAGE ExistentialQuantification, TypeFamilies, StandaloneDeriving, FlexibleContexts #-}
2011-07-14 00:09:20 +02:00
2011-07-14 20:21:30 +02:00
module Phi.Widget ( Display
, Widget(..)
2011-07-14 00:09:20 +02:00
, WidgetClass(..)
, WidgetState(..)
, separator
, createWidgetState
, layoutWidgets
, renderWidgets
2011-07-14 20:21:30 +02:00
, handleMessageWidgets
2011-07-14 00:09:20 +02:00
) where
2011-07-14 20:21:30 +02:00
import Control.Concurrent
2011-07-14 00:09:20 +02:00
import Control.Monad
2011-07-14 20:21:30 +02:00
2011-07-14 01:47:10 +02:00
import Data.Traversable
2011-07-14 00:09:20 +02:00
2011-07-14 20:21:30 +02:00
import qualified Graphics.X11.Xlib
2011-07-14 00:09:20 +02:00
import Graphics.Rendering.Cairo
2011-07-14 06:16:04 +02:00
import Phi.Phi
2011-07-14 00:09:20 +02:00
2011-07-14 20:21:30 +02:00
type Display = MVar Graphics.X11.Xlib.Display
2011-07-14 01:47:10 +02:00
class Show a => WidgetClass a where
2011-07-14 00:09:20 +02:00
type WidgetData a :: *
2011-07-14 20:21:30 +02:00
initWidget :: a -> Phi -> Display -> IO (WidgetData a)
2011-07-14 00:09:20 +02:00
minSize :: a -> Int
weight :: a -> Float
weight _ = 0
2011-07-14 01:47:10 +02:00
layout :: a -> WidgetData a -> Int -> Int -> WidgetData a
layout _ priv _ _ = priv
2011-07-14 00:09:20 +02:00
render :: a -> WidgetData a -> Int -> Int -> Render ()
2011-07-14 06:16:04 +02:00
handleMessage :: a -> WidgetData a -> Message -> WidgetData a
handleMessage _ priv _ = priv
2011-07-14 00:09:20 +02:00
2011-07-14 01:47:10 +02:00
data Widget = forall a. (WidgetClass a, Show (WidgetData a)) => Widget a
deriving instance Show Widget
2011-07-14 00:09:20 +02:00
2011-07-14 01:47:10 +02:00
data WidgetState = forall a. (WidgetClass a, Show (WidgetData a)) => WidgetState { stateWidget :: a
, stateX :: Int
, stateY :: Int
, stateWidth :: Int
, stateHeight :: Int
, statePrivateData :: WidgetData a
}
deriving instance Show WidgetState
2011-07-14 00:09:20 +02:00
2011-07-14 20:21:30 +02:00
createWidgetState :: Phi -> Display -> Widget -> IO WidgetState
createWidgetState phi disp (Widget w) = do
priv <- initWidget w phi disp
return WidgetState { stateWidget = w
, stateX = 0
, stateY = 0
, stateWidth = 0
, stateHeight = 0
, statePrivateData = priv
}
2011-07-14 00:09:20 +02:00
2011-07-14 01:47:10 +02:00
layoutWidgets :: [WidgetState] -> Int -> Int -> Int -> Int -> [WidgetState]
layoutWidgets widgets x y width height = snd $ mapAccumL layoutWidgetAndX x widgets
2011-07-14 00:09:20 +02:00
where
2011-07-14 01:47:10 +02:00
sizesum = sum $ map (\(WidgetState {stateWidget = w} ) -> nneg . minSize $ w) widgets
wsum = let wsum = sum $ map (\(WidgetState {stateWidget = w} ) -> nneg . weight $ w) widgets
in if wsum > 0 then wsum else 1
surplus = width - sizesum
layoutWidgetAndX wX state = let lw = layoutWidget wX state
in (wX + stateWidth lw, lw)
layoutWidget wX state = case state of
WidgetState {stateWidget = w, statePrivateData = priv} ->
let wWidth = floor $ (fromIntegral $ minSize w) + (fromIntegral surplus)*(nneg $ weight w)/wsum
priv' = layout w priv wWidth height
2011-07-14 06:16:04 +02:00
in WidgetState w wX y wWidth height priv'
2011-07-14 01:47:10 +02:00
nneg :: (Num a, Ord a) => a -> a
nneg x = max 0 x
2011-07-14 00:09:20 +02:00
renderWidgets :: [WidgetState] -> Render ()
2011-07-14 01:47:10 +02:00
renderWidgets widgets = forM_ widgets $ \WidgetState { stateWidget = widget
, stateX = x
, stateY = y
, stateWidth = w
, stateHeight = h
, statePrivateData = priv } -> do
save
translate (fromIntegral x) (fromIntegral y)
render widget priv w h
restore
2011-07-14 00:09:20 +02:00
2011-07-14 06:16:04 +02:00
handleMessageWidgets :: Message -> [WidgetState] -> [WidgetState]
handleMessageWidgets message = map handleMessageWidget
where
handleMessageWidget (WidgetState w x y width height priv) = WidgetState w x y width height $ handleMessage w priv message
2011-07-14 00:09:20 +02:00
2011-07-14 01:47:10 +02:00
data Separator = Separator Int Float deriving Show
2011-07-14 00:09:20 +02:00
instance WidgetClass Separator where
type WidgetData Separator = ()
2011-07-14 20:21:30 +02:00
initWidget _ _ _ = return ()
2011-07-14 00:09:20 +02:00
minSize (Separator s _) = s
weight (Separator _ w) = w
render _ _ _ _ = return ()
separator :: Int -> Float -> Widget
separator s w = Widget $ Separator s w