drawCell :: IORef SpreadsheetProperties -> Ref Input -> Ref Table -> TableContext -> TableCoordinate -> Rectangle -> IO ()
drawCell props' intInput' table' context' (TableCoordinate (Row row') (Column col')) rectangle' = do
  _p <- readIORef props'
  (Rows numRows') <- getRows table'
  (Columns numCols') <- getCols table'
  case context' of
   ContextStartPage -> do
     (p1,p2) <- getSelection table'
     modifyIORef props' (\p -> p {sLeftTop = p1, sRightBottom = p2})
   ContextColHeader -> do
     flcSetFont helveticaBold (FontSize 14)
     flcPushClip rectangle'
     getColHeaderColor table' >>= flcDrawBox ThinUpBox rectangle'
     flcSetColor blackColor
     if (col' == numCols' - 1)
       then flcDrawInBox "TOTAL" rectangle' alignCenter Nothing Nothing
       else flcDrawInBox (T.pack [toEnum $ fromEnum 'A' + col']) rectangle' alignCenter Nothing Nothing
     flcPopClip
   ContextRowHeader -> do
     flcSetFont helveticaBold (FontSize 14)
     flcPushClip rectangle'
     getRowHeaderColor table' >>= flcDrawBox ThinUpBox rectangle'
     flcSetColor blackColor
     if (row' == numRows' - 1)
       then flcDrawInBox "TOTAL" rectangle' alignCenter Nothing Nothing
       else flcDrawInBox (T.pack (show $ row' + 1)) rectangle' alignCenter Nothing Nothing
     flcPopClip
   ContextCell-> do
     visible' <- getVisible intInput'
     let (TableCoordinate (Row sTop') (Column sLeft')) = sLeftTop _p
         (TableCoordinate (Row sBottom') (Column sRight')) = sRightBottom _p
     if (row' == (rowEdit _p) && col' == (colEdit _p) && visible')
       then return ()
       else do
            if (row' >= sTop' && row' <= sBottom' && col' >= sLeft' && col' <= sRight')
              then flcDrawBox ThinUpBox rectangle' yellowColor
              else if (col' < numCols' - 1 && row' < numRows' - 1)
                   then do
                     selected' <- isSelected table' (TableCoordinate (Row row') (Column col'))
                     flcDrawBox ThinUpBox rectangle' (if selected' then yellowColor else whiteColor)
                   else flcDrawBox ThinUpBox rectangle' (Color 0xbbddbb00)
            flcPushClip rectangle'
            flcSetColor blackColor
            if (col' == numCols' - 1 || row' == numRows' - 1)
              then do
                flcSetFont helveticaBold (FontSize 14)
                let shownValues = map (take (numCols'- 1)) $ take (numRows' -1) (values _p)
                let s' = if (col' == numCols' - 1 && row' == numRows' - 1)
                         then (show . sum . map sum) shownValues
                         else if (col' == numCols' - 1)
                              then (show $ sum $ shownValues !! row')
                              else if (row' == numRows' - 1)
                                   then (show . sum . map (\r -> r !! col')) shownValues
                                   else ""
                let (x',y',w',h') = fromRectangle rectangle'
                flcDrawInBox (T.pack s') (toRectangle (x'+3,y'+3,w'-6,h'-6)) alignRight Nothing Nothing
              else do
                flcSetFont helvetica (FontSize 14)
                let s' = show $ (values _p) !! row' !! col'
                let (x',y',w',h') = fromRectangle rectangle'
                flcDrawInBox (T.pack s') (toRectangle (x'+3,y'+3,w'-6,h'-6)) alignRight Nothing Nothing
            flcPopClip
   ContextRCResize -> do
     visible' <- getVisible intInput'
     if (not visible')
       then return ()
       else do
         cellRectangle' <- findCell table' ContextTable (TableCoordinate (Row row') (Column  col'))
         case cellRectangle' of
           Just cr' -> if (cr' == rectangle')
                       then return ()
                       else resize intInput' cr'
           Nothing -> return ()
   _  -> return ()
drawCell :: IORef SpreadsheetProperties -> Ref Input -> Ref Table -> TableContext -> TableCoordinate -> Rectangle -> IO ()
drawCell props' intInput' table' context' (TableCoordinate (Row row') (Column col')) rectangle' = do
  _p <- readIORef props'
  (Rows numRows') <- getRows table'
  (Columns numCols') <- getCols table'
  case context' of
   ContextStartPage -> do
     (p1,p2) <- getSelection table'
     modifyIORef props' (\p -> p {sLeftTop = p1, sRightBottom = p2})
   ContextColHeader -> do
     flcSetFont helveticaBold (FontSize 14)
     flcPushClip rectangle'
     getColHeaderColor table' >>= flcDrawBox ThinUpBox rectangle'
     flcSetColor blackColor
     if (col' == numCols' - 1)
       then flcDrawInBox "TOTAL" rectangle' alignCenter Nothing Nothing
       else flcDrawInBox (T.pack [toEnum $ fromEnum 'A' + col']) rectangle' alignCenter Nothing Nothing
     flcPopClip
   ContextRowHeader -> do
     flcSetFont helveticaBold (FontSize 14)
     flcPushClip rectangle'
     getRowHeaderColor table' >>= flcDrawBox ThinUpBox rectangle'
     flcSetColor blackColor
     if (row' == numRows' - 1)
       then flcDrawInBox "TOTAL" rectangle' alignCenter Nothing Nothing
       else flcDrawInBox (T.pack (show $ row' + 1)) rectangle' alignCenter Nothing Nothing
     flcPopClip
   ContextCell-> do
     visible' <- getVisible intInput'
     let (TableCoordinate (Row sTop') (Column sLeft')) = sLeftTop _p
         (TableCoordinate (Row sBottom') (Column sRight')) = sRightBottom _p
     if (row' == (rowEdit _p) && col' == (colEdit _p) && visible')
       then return ()
       else do
            if (row' >= sTop' && row' <= sBottom' && col' >= sLeft' && col' <= sRight')
              then flcDrawBox ThinUpBox rectangle' yellowColor
              else if (col' < numCols' - 1 && row' < numRows' - 1)
                   then do
                     selected' <- isSelected table' (TableCoordinate (Row row') (Column col'))
                     flcDrawBox ThinUpBox rectangle' (if selected' then yellowColor else whiteColor)
                   else flcDrawBox ThinUpBox rectangle' (Color 0xbbddbb00)
            flcPushClip rectangle'
            flcSetColor blackColor
            if (col' == numCols' - 1 || row' == numRows' - 1)
              then do
                flcSetFont helveticaBold (FontSize 14)
                let shownValues = map (take (numCols'- 1)) $ take (numRows' -1) (values _p)
                let s' = if (col' == numCols' - 1 && row' == numRows' - 1)
                         then (show . sum . map sum) shownValues
                         else if (col' == numCols' - 1)
                              then (show $ sum $ shownValues !! row')
                              else if (row' == numRows' - 1)
                                   then (show . sum . map (\r -> r !! col')) shownValues
                                   else ""
                let (x',y',w',h') = fromRectangle rectangle'
                flcDrawInBox (T.pack s') (toRectangle (x'+3,y'+3,w'-6,h'-6)) alignRight Nothing Nothing
              else do
                flcSetFont helvetica (FontSize 14)
                let s' = show $ (values _p) !! row' !! col'
                let (x',y',w',h') = fromRectangle rectangle'
                flcDrawInBox (T.pack s') (toRectangle (x'+3,y'+3,w'-6,h'-6)) alignRight Nothing Nothing
            flcPopClip
   ContextRCResize -> do
     visible' <- getVisible intInput'
     if (not visible')
       then return ()
       else do
         cellRectangle' <- findCell table' ContextTable (TableCoordinate (Row row') (Column  col'))
         case cellRectangle' of
           Just cr' -> if (cr' == rectangle')
                       then return ()
                       else resize intInput' cr'
           Nothing -> return ()
   _  -> return ()
drawCell props' intInput' table' context' (TableCoordinate (Row row') (Column col')) rectangle' = do
  _p <- readIORef props'
  (Rows numRows') <- getRows table'
  (Columns numCols') <- getCols table'
  case context' of
   ContextStartPage -> do
     (p1,p2) <- getSelection table'
     modifyIORef props' (\p -> p {sLeftTop = p1, sRightBottom = p2})
   ContextColHeader -> do
     flcSetFont helveticaBold (FontSize 14)
     flcPushClip rectangle'
     getColHeaderColor table' >>= flcDrawBox ThinUpBox rectangle'
     flcSetColor blackColor
     if (col' == numCols' - 1)
       then flcDrawInBox "TOTAL" rectangle' alignCenter Nothing Nothing
       else flcDrawInBox (T.pack [toEnum $ fromEnum 'A' + col']) rectangle' alignCenter Nothing Nothing
     flcPopClip
   ContextRowHeader -> do
     flcSetFont helveticaBold (FontSize 14)
     flcPushClip rectangle'
     getRowHeaderColor table' >>= flcDrawBox ThinUpBox rectangle'
     flcSetColor blackColor
     if (row' == numRows' - 1)
       then flcDrawInBox "TOTAL" rectangle' alignCenter Nothing Nothing
       else flcDrawInBox (T.pack (show $ row' + 1)) rectangle' alignCenter Nothing Nothing
     flcPopClip
   ContextCell-> do
     visible' <- getVisible intInput'
     let (TableCoordinate (Row sTop') (Column sLeft')) = sLeftTop _p
         (TableCoordinate (Row sBottom') (Column sRight')) = sRightBottom _p
     if (row' == (rowEdit _p) && col' == (colEdit _p) && visible')
       then return ()
       else do
            if (row' >= sTop' && row' <= sBottom' && col' >= sLeft' && col' <= sRight')
              then flcDrawBox ThinUpBox rectangle' yellowColor
              else if (col' < numCols' - 1 && row' < numRows' - 1)
                   then do
                     selected' <- isSelected table' (TableCoordinate (Row row') (Column col'))
                     flcDrawBox ThinUpBox rectangle' (if selected' then yellowColor else whiteColor)
                   else flcDrawBox ThinUpBox rectangle' (Color 0xbbddbb00)
            flcPushClip rectangle'
            flcSetColor blackColor
            if (col' == numCols' - 1 || row' == numRows' - 1)
              then do
                flcSetFont helveticaBold (FontSize 14)
                let shownValues = map (take (numCols'- 1)) $ take (numRows' -1) (values _p)
                let s' = if (col' == numCols' - 1 && row' == numRows' - 1)
                         then (show . sum . map sum) shownValues
                         else if (col' == numCols' - 1)
                              then (show $ sum $ shownValues !! row')
                              else if (row' == numRows' - 1)
                                   then (show . sum . map (\r -> r !! col')) shownValues
                                   else ""
                let (x',y',w',h') = fromRectangle rectangle'
                flcDrawInBox (T.pack s') (toRectangle (x'+3,y'+3,w'-6,h'-6)) alignRight Nothing Nothing
              else do
                flcSetFont helvetica (FontSize 14)
                let s' = show $ (values _p) !! row' !! col'
                let (x',y',w',h') = fromRectangle rectangle'
                flcDrawInBox (T.pack s') (toRectangle (x'+3,y'+3,w'-6,h'-6)) alignRight Nothing Nothing
            flcPopClip
   ContextRCResize -> do
     visible' <- getVisible intInput'
     if (not visible')
       then return ()
       else do
         cellRectangle' <- findCell table' ContextTable (TableCoordinate (Row row') (Column  col'))
         case cellRectangle' of
           Just cr' -> if (cr' == rectangle')
                       then return ()
                       else resize intInput' cr'
           Nothing -> return ()
   _  -> return ()