A Container is created using container, passing it two controlling functions:
container :: (Size -> (Size, Rectangle))
-> (Size -> Rectangle -> Rectangle)
-> DisplayHandle
-> DisplayHandle
container initialSize resizeFun component
The first argument, initialSize, compute the natural size of the Container and the initial placement and size of the contained component. The resize behaviour is controlled by the second argument, resizeFun, mapping a new size for the container to a new size and placement for component. As an example, here's a combinator that ensures that a component always has a square bounding box:
squareW :: DisplayHandle -> DisplayHandle
squareW = container initSize resizeFun
where
computeSize :: Size -> Int -> Rectangle
computeSize (w,h) l =
let
x = (w-l) `div` 2
y = (h-l) `div` 2
in
Rect x y l l
initSize :: Size -> (Size,Rectangle)
initSize sz@(w,h) =
let
l = max w h
in
((l,l),computeSize sz l)
resizeFun :: Size -> Rectangle -> Rectangle
resizeFun (w,h) _ = computeSize (w,h) (min w h)
main =
wopen ["*name: Square"]
(\dc ->
button (text "Oblong button") () dc >>= \(btn,dh) ->
return (btn,squareW dh)) >>
return ()
