views:

42

answers:

1

Hello, Could someone explain me what this code does line by line ?

how t ounderstand excactly first line with declaration ? what does it mean: [Prop (Grid ())]?

thanks for help

gridCtrl :: Window a -> [Prop (Grid ())] -> IO (Grid ())
gridCtrl parent props
  = feed2 props 0 $
    initialWindow $ \id rect -> \props flags ->
    do g <- gridCreate parent id rect flags
       gridCreateGrid g 0 0 0
       set g props
       return g
+2  A: 

In wxHaskell, controls have attached properties which can be read or changed. The stanza [Prop (Grid ())] can be understood as "a list of properties for any Grid type".

This is wxHaskell's way of dealing with the fact that the wxWidgets library, which it is built on, is object-oriented. Grid () actually means "anything in the inheritance hierarchy from which Grid derives" - i.e. Grid, ScrolledWindow, Panel, Window, EvtHandler, wxObject (you can follow this through if you start at at http://wxhaskell.sourceforge.net/doc/Graphics-UI-WXCore-WxcClassTypes.html#204)

When you look at the documentation of a Control (e.g. ListCtrl) you will find that it is reported as having a set of Attributes and Instances. Basically, you can use those which apply to the hierarchy for Grid. For example, Grid derives from ScrolledWindow, so you can use:

  • Attribute scrollRate
  • Attributes of Colored class e.g. bgcolor, color
  • etc.

You use these properties as follows, e.g.

g <- gridCtrl parent [color := red, bgcolor := green, scrollRate := 41]
...
set g [color := blue]

Line by line, the code reads something like the following:

  • Using the supplied properties (props)
  • and an initialWindow (which will fill in the window id and initial rect and flags), call the floowing wrapped functions in order:
  • gridCreate to create a new Grid instance
  • Using the new grid instance, set the grid inside with 0 rows, 0 columns and nothing selected.
  • Apply the properties (props) supplied by the caller to the grid (e.g. put data in, set styles etc.)

The part which makes the code hardest to read is the feed2 function, and that fact that the code is written in '$' style to provide the correct parameters to feed2. My slightly hand-waving explanation above should be enough, but if you want to understand the details, understand that feed2 is just composed inverted function application

feed2 x y f = f x y

then replace the '$' applications with parentheses. This doesn't look as cute, but is easier to read.

gridCtrl parent props =
  feed2 props 0
    (initialWindow (\id rect ->
                      \props flags ->
                        do
                        g <- gridCreate parent id rect flags
                        gridCreateGrid g 0 0 0
                        set g props
                        return g )))
Jeremy O'Donoghue