eventCallback :: IORef SpreadsheetProperties -> Ref Input -> Ref Table -> IO ()
eventCallback props' intInput' table' = do
  _p <- readIORef props'
  (Row r') <- callbackRow table'
  (Column c') <- callbackCol table'
  context' <- callbackContext table'
  (Rows numRows') <- getRows table'
  (Columns numCols') <- getCols table'
  case context' of
    ContextCell -> do
      event' <- FL.event
      case event' of
        Push -> do
          doneEditing props' intInput' table'
          if (r' /= (numRows' -1) && c' /= (numCols' -1))
            then startEditing props' intInput' table' (TableCoordinate (Row r') (Column c'))
            else return ()
          return ()
        Keydown -> do
          eventKey' <- FL.eventKey
          if (eventKey' == (SpecialKeyType Kb_Escape))
            then return ()
            else if (r' == numRows' - 1 || c' == numCols' -1)
                 then return ()
                 else do
                   doneEditing props' intInput' table'
                   setSelection table' (TableCoordinate (Row r') (Column c')) (TableCoordinate (Row r') (Column c'))
                   startEditing props' intInput' table' (TableCoordinate (Row r') (Column c'))
                   newEvent <- FL.event
                   if (newEvent == Keydown)
                     then handle intInput' newEvent >> return ()
                     else return ()
                   return ()
        _  -> return ()
    _c -> if (any (== _c) [ContextTable, ContextRowHeader, ContextColHeader])
          then doneEditing props' intInput' table'
          else return ()
eventCallback :: IORef SpreadsheetProperties -> Ref Input -> Ref Table -> IO ()
eventCallback props' intInput' table' = do
  _p <- readIORef props'
  (Row r') <- callbackRow table'
  (Column c') <- callbackCol table'
  context' <- callbackContext table'
  (Rows numRows') <- getRows table'
  (Columns numCols') <- getCols table'
  case context' of
    ContextCell -> do
      event' <- FL.event
      case event' of
        Push -> do
          doneEditing props' intInput' table'
          if (r' /= (numRows' -1) && c' /= (numCols' -1))
            then startEditing props' intInput' table' (TableCoordinate (Row r') (Column c'))
            else return ()
          return ()
        Keydown -> do
          eventKey' <- FL.eventKey
          if (eventKey' == (SpecialKeyType Kb_Escape))
            then return ()
            else if (r' == numRows' - 1 || c' == numCols' -1)
                 then return ()
                 else do
                   doneEditing props' intInput' table'
                   setSelection table' (TableCoordinate (Row r') (Column c')) (TableCoordinate (Row r') (Column c'))
                   startEditing props' intInput' table' (TableCoordinate (Row r') (Column c'))
                   newEvent <- FL.event
                   if (newEvent == Keydown)
                     then handle intInput' newEvent >> return ()
                     else return ()
                   return ()
        _  -> return ()
    _c -> if (any (== _c) [ContextTable, ContextRowHeader, ContextColHeader])
          then doneEditing props' intInput' table'
          else return ()
eventCallback props' intInput' table' = do
  _p <- readIORef props'
  (Row r') <- callbackRow table'
  (Column c') <- callbackCol table'
  context' <- callbackContext table'
  (Rows numRows') <- getRows table'
  (Columns numCols') <- getCols table'
  case context' of
    ContextCell -> do
      event' <- FL.event
      case event' of
        Push -> do
          doneEditing props' intInput' table'
          if (r' /= (numRows' -1) && c' /= (numCols' -1))
            then startEditing props' intInput' table' (TableCoordinate (Row r') (Column c'))
            else return ()
          return ()
        Keydown -> do
          eventKey' <- FL.eventKey
          if (eventKey' == (SpecialKeyType Kb_Escape))
            then return ()
            else if (r' == numRows' - 1 || c' == numCols' -1)
                 then return ()
                 else do
                   doneEditing props' intInput' table'
                   setSelection table' (TableCoordinate (Row r') (Column c')) (TableCoordinate (Row r') (Column c'))
                   startEditing props' intInput' table' (TableCoordinate (Row r') (Column c'))
                   newEvent <- FL.event
                   if (newEvent == Keydown)
                     then handle intInput' newEvent >> return ()
                     else return ()
                   return ()
        _  -> return ()
    _c -> if (any (== _c) [ContextTable, ContextRowHeader, ContextColHeader])
          then doneEditing props' intInput' table'
          else return ()