{-# LANGUAGE CPP #-}
module Game.Goatee.Ui.Gtk.Widget (
GoateeEntry, goateeEntryWidget, goateeEntryNew, goateeEntryGetText, goateeEntrySetText,
goateeEntryOnChange,
GoateeSpinButton, goateeSpinButtonNewWithRange, goateeSpinButtonWidget, goateeSpinButtonGetValue,
goateeSpinButtonSetValue, goateeSpinButtonOnSpinned,
) where
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>), (<*>))
#endif
import Control.Monad (void)
import qualified Game.Goatee.Common.Bigfloat as BF
import Game.Goatee.Ui.Gtk.Latch
import Game.Goatee.Ui.Gtk.Utils
import Graphics.UI.Gtk (
AttrOp ((:=)),
Entry,
SpinButton,
entryNew, entryText,
get,
onValueSpinned,
set,
spinButtonNewWithRange, spinButtonSetValue,
)
data GoateeEntry = GoateeEntry Entry Latch
goateeEntryNew :: IO GoateeEntry
goateeEntryNew = GoateeEntry <$> entryNew <*> newLatch
goateeEntryWidget :: GoateeEntry -> Entry
goateeEntryWidget (GoateeEntry entry _) = entry
goateeEntryGetText :: GoateeEntry -> IO String
goateeEntryGetText (GoateeEntry entry _) = get entry entryText
goateeEntrySetText :: GoateeEntry -> String -> IO ()
goateeEntrySetText (GoateeEntry entry latch) value =
withLatchOn latch $ set entry [entryText := value]
goateeEntryOnChange :: GoateeEntry -> (String -> IO ()) -> IO ()
goateeEntryOnChange (GoateeEntry entry latch) handler =
onEntryChange entry $ \value -> whenLatchOff latch $ handler value
data GoateeSpinButton = GoateeSpinButton SpinButton Latch
goateeSpinButtonNewWithRange :: Double -> Double -> Double -> IO GoateeSpinButton
goateeSpinButtonNewWithRange min max step =
GoateeSpinButton <$> spinButtonNewWithRange min max step <*> newLatch
goateeSpinButtonWidget :: GoateeSpinButton -> SpinButton
goateeSpinButtonWidget (GoateeSpinButton spinButton _) = spinButton
goateeSpinButtonGetValue :: GoateeSpinButton -> IO BF.Bigfloat
goateeSpinButtonGetValue (GoateeSpinButton spinButton _) =
spinButtonGetValueAsBigfloat spinButton
goateeSpinButtonSetValue :: GoateeSpinButton -> BF.Bigfloat -> IO ()
goateeSpinButtonSetValue (GoateeSpinButton spinButton latch) value =
withLatchOn latch $ spinButtonSetValue spinButton $ BF.toDouble value
goateeSpinButtonOnSpinned :: GoateeSpinButton -> (BF.Bigfloat -> IO ()) -> IO ()
goateeSpinButtonOnSpinned (GoateeSpinButton spinButton latch) handler =
void $ onValueSpinned spinButton $ whenLatchOff latch $
spinButtonGetValueAsBigfloat spinButton >>= handler