module Manatee.Toolkit.Gtk.Editable where
import Control.Monad
import Graphics.UI.Gtk
import Manatee.Toolkit.General.List
import Manatee.Toolkit.General.String
type EditableContent = String
type EditableBound = (Int, Int)
type EditableStatus = (EditableContent, EditableBound)
editableFocus :: EditableClass self => self -> IO ()
editableFocus ed = do
bound <- editableGetSelectionBounds ed
widgetGrabFocus $ castToWidget ed
editableSetBound ed bound
editableGetUnselectText :: EditableClass self => self -> IO String
editableGetUnselectText ed = do
allText <- editableGetAllText ed
(start, _) <- editableGetSelectionBounds ed
return $ take start allText
editableSelectToEnd :: EditableClass self => self -> Int -> IO ()
editableSelectToEnd ed current =
editableSelectRegion ed current (1)
editableGetAllText :: EditableClass self => self -> IO String
editableGetAllText ed = editableGetChars ed 0 (1)
editableSetText :: EditableClass self => self -> String -> IO ()
editableSetText ed content = do
editableDeleteAllText ed
end <- editableInsertText ed content 0
editableSetPosition ed end
editableSetCompletionText :: EditableClass self => self -> String -> String -> IO ()
editableSetCompletionText ed input common = do
editableSetText ed (input ++ common)
editableSelectToEnd ed (length input)
editableSetBound :: EditableClass self => self -> (Int, Int) -> IO ()
editableSetBound ed (start, end) = do
editableSelectRegion ed start end
editableSetPosition ed start
editableGetStatus :: EditableClass self => self -> IO EditableStatus
editableGetStatus ed =
liftM2 (,) (editableGetAllText ed) (editableGetSelectionBounds ed)
editableSetStatus :: EditableClass self => self -> EditableStatus -> IO ()
editableSetStatus ed (content, bound) = do
editableSetText ed content
editableSetBound ed bound
editableDeleteAllText :: EditableClass self => self -> IO ()
editableDeleteAllText ed = editableDeleteText ed 0 (1)
editableExpandCompletion :: EditableClass self => self -> IO ()
editableExpandCompletion ed =
editableSetPosition ed (1)
editableDeleteBackwardChar :: EditableClass self => self -> IO ()
editableDeleteBackwardChar ed = do
unselectText <- editableGetUnselectText ed
editableSetText ed (init_ unselectText)
editableDeleteBackwardWord :: EditableClass self => self -> IO ()
editableDeleteBackwardWord ed = do
unselectText <- editableGetUnselectText ed
let (word, restStr) = searchBackwardWord unselectText
text = if null word
then init_ unselectText
else restStr
editableSetText ed text
editableBackwardChar :: EditableClass self => self -> IO ()
editableBackwardChar ed = do
pos <- editableGetPosition ed
editableSetPosition ed (if pos >= 1 then pos 1 else 0)
editableIsChanged :: EditableClass self => self -> IO () -> IO Bool
editableIsChanged ed action = do
let getState = do
text <- editableGetAllText ed
(start, end) <- editableGetSelectionBounds ed
return (text, start, end)
beforeState <- getState
action
afterState <- getState
return (beforeState /= afterState)