vty-ui-1.0: doc/ch3/cursor_positioning.tex
\section{Cursor Positioning}
\label{sec:cursor_positioning}
Once a widget is properly positioned, the widget can display a cursor.
This is especially useful for edit widgets, since the user needs to
know the cursor position. The \fw{Core} module provides a top-level
function to accomplish this called \fw{getCursorPosition}; this
function calls the \fw{WidgetImpl} type's \fw{getCursorPosition\_}
function.
The \fw{getCursorPosition\_} function returns \fw{Maybe
DisplayRegion}. A return value of \fw{Nothing} indicates that the
widget does not want to show a cursor, so when it gains focus, no
cursor will be displayed. Otherwise, positioning the cursor at row
\fw{r} and column \fw{c} is accomplished by returning \fw{Just
(DisplayRegion r c)}. The cursor is then shown at that location by
the event loop.
Typically, the position of the cursor is computed as an offset to the
widget's current position. In the \fw{Wrapper} widget example in
Section \ref{sec:deferring} we deferred to the child widget to control
the cursor, but we might instead specify our own position:
\begin{haskellcode}
getCursorPosition_ = \this -> do
(Wrapper child) <- getState this
childCursor <- getCursorPosition child
case childCursor of
Nothing -> return Nothing
Just pos -> return $ Just $ pos `plusWidth` 1 `plusHeight` 1
\end{haskellcode}
Although contrived, this example shows how we can return a new cursor
position based on the child widget's cursor position.