HTkUtils.hs revision 98890889ffb2e8f6f722b00e265a211f13b5a861
2155N/A{-# LANGUAGE ExistentialQuantification #-}
2155N/A{- |
2155N/AModule : $Header$
2155N/ACopyright : (c) K. Luettich, Rene Wagner, Uni Bremen 2002-2005
2155N/ALicense : GPLv2 or higher, see LICENSE.txt
2155N/A
2155N/AMaintainer : luecke@informatik.uni-bremen.de
2155N/AStability : provisional
2155N/APortability : non-portable (imports HTk)
2155N/A
2155N/AUtilities on top of HTk
2155N/A-}
2155N/A
2155N/Amodule GUI.HTkUtils
2155N/A ( LBGoalView (..)
2155N/A , LBStatusIndicator (..)
2155N/A , EnableWid (..)
2155N/A , GUIMVar
2155N/A , listBox
2155N/A , errorMess
2155N/A , confirmMess
2155N/A , messageMess
2155N/A , askFileNameAndSave
2155N/A , createTextSaveDisplay
2155N/A , newFileDialogStr
2155N/A , fileDialogStr
2155N/A , displayTheory
2155N/A , displayTheoryWithWarning
2155N/A , populateGoalsListBox
2155N/A , indicatorFromProofStatus
2155N/A , indicatorFromBasicProof
2155N/A , indicatorString
2155N/A , enableWids
2155N/A , disableWids
2155N/A , enableWidsUponSelection
2155N/A , module HTk.Toplevel.HTk
2155N/A , module HTk.Toolkit.ScrollBox
2155N/A , module HTk.Toolkit.SimpleForm
2155N/A , module HTk.Toolkit.TextDisplay
2155N/A ) where
2155N/A
2155N/Aimport System.Directory
2155N/A
2155N/Aimport Util.Messages
2155N/Aimport HTk.Toplevel.HTk hiding (x, y)
2155N/Aimport HTk.Toolkit.ScrollBox
2155N/Aimport HTk.Toolkit.SimpleForm
2155N/Aimport HTk.Toolkit.TextDisplay
2155N/Aimport HTk.Toolkit.FileDialog
2155N/A
2155N/Aimport Logic.Prover
2155N/Aimport Static.GTheory
2155N/A
2155N/Aimport Common.DocUtils
2155N/A
2155N/Aimport Control.Concurrent.MVar
2155N/A
2155N/A
2155N/A-- ** some types
2155N/A
2155N/A-- | Type for storing the proof management window
2155N/Atype GUIMVar = MVar (Maybe Toplevel)
2155N/A
2155N/A-- | create a window with title and list of options, return selected option
2155N/AlistBox :: String -> [String] -> IO (Maybe Int)
2155N/AlistBox title entries =
2155N/A do
2155N/A main <- createToplevel [text title]
2155N/A lb <- newListBox main [value entries, bg "white", size (100, 39)] ::
2155N/A IO (ListBox String)
2155N/A pack lb [Side AtLeft, Expand On, Fill Both]
2155N/A scb <- newScrollBar main []
2155N/A pack scb [Side AtRight, Fill Y]
lb # scrollbar Vertical scb
(press, _) <- bindSimple lb (ButtonPress (Just 1))
(closeWindow,_) <- bindSimple main Destroy
sync ( (press >>> do
sel <- getSelection lb
destroy main
return (case sel of
Just [i] -> Just i
_ -> Nothing) )
+> (closeWindow >>> do
destroy main
return Nothing ))
-- |
-- Display some (longish) text in an uneditable, scrollable editor.
-- Returns immediately-- the display is forked off to separate thread.
createTextSaveDisplayExt :: String -- ^ title of the window
-> String -- ^ default filename for saving the text
-> String -- ^ text to be displayed
-> [Config Editor] -- ^ configuration options for
-- the text editor
-> IO() -- ^ action to be executed when
-- the window is closed
-> IO (Toplevel,Editor) -- ^ the window in which
-- the text is displayed
createTextSaveDisplayExt title fname txt conf upost =
do win <- createToplevel [text title]
b <- newFrame win [relief Groove, borderwidth (cm 0.05)]
t <- newLabel b [text title, font (Helvetica, Roman, 18::Int)]
q <- newButton b [text "Close", width 12]
s <- newButton b [text "Save", width 12]
(sb, ed) <- newScrollBox b (flip newEditor conf) []
ed # state Disabled
pack b [Side AtTop, Fill Both,Expand On]
pack t [Side AtTop, Expand Off, PadY 10]
pack sb [Side AtTop, Expand On,Fill Both]
pack ed [Side AtTop, Expand On, Fill Both]
pack q [Side AtRight, PadX 8, PadY 5]
pack s [Side AtLeft, PadX 5, PadY 5]
ed # state Normal
ed # value txt
ed # state Disabled
forceFocus ed
(editClicked, _) <- bindSimple ed (ButtonPress (Just 1))
quit <- clicked q
save <- clicked s
_ <- spawnEvent $ forever $ quit >>> (destroy win >> upost)
+> save >>> do
disableButs q s
askFileNameAndSave fname txt
enableButs q s
done
+> editClicked >>> forceFocus ed
return (win, ed)
where disableButs b1 b2 = disable b1 >> disable b2
enableButs b1 b2 = enable b1 >> enable b2
-- |
-- Display some (longish) text in an uneditable, scrollable editor.
-- Simplified version of createTextSaveDisplayExt
createTextSaveDisplay :: String -- ^ title of the window
-> String -- ^ default filename for saving the text
-> String -- ^ text to be displayed
-> IO()
createTextSaveDisplay t f txt = do
createTextSaveDisplayExt t f txt [size(100,44)] done
done
--- added by KL
-- |
-- opens a FileDialog and saves to the selected file if OK is clicked
-- otherwise nothing happens
askFileNameAndSave :: String -- ^ default filename for saving the text
-> String -- ^ text to be saved
-> IO ()
askFileNameAndSave defFN txt =
do curDir <- getCurrentDirectory
selev <- newFileDialogStr "Save file" (curDir ++ '/' : defFN)
mfile <- sync selev
maybe done saveFile mfile
where saveFile fp = writeFile fp txt
-- | displays a theory in a window
displayTheory :: String -- ^ kind of theory
-> String -- ^ name of theory
-> G_theory -> IO ()
displayTheory kind thname gth =
let str = showDoc gth "\n"
title = kind ++ " of " ++ thname
in createTextSaveDisplay title (thname++".het") str
{- | returns a window displaying the given theory and the given
warning text.
-}
displayTheoryWithWarning :: String -- ^ kind of theory
-> String -- ^ name of theory
-> String -- ^ warning text
-> G_theory -- ^ to be shown theory
-> IO ()
displayTheoryWithWarning kind thname warningTxt gth =
let str = warningTxt ++ showDoc gth "\n"
title = kind ++ " of " ++ thname
in createTextSaveDisplay title (thname++".het") str
--- added by RW
{- |
Represents the state of a goal in a 'ListBox' that uses 'populateGoalsListBox'
-}
data LBStatusIndicator = LBIndicatorProved
| LBIndicatorProvedInconsistent
| LBIndicatorDisproved
| LBIndicatorOpen
| LBIndicatorGuessed
| LBIndicatorConjectured
| LBIndicatorHandwritten
-- |
-- Converts a 'LBStatusIndicator' into a short 'String' representing it in
-- a 'ListBox'
indicatorString :: LBStatusIndicator
-> String
indicatorString i = case i of
LBIndicatorProved -> "[+]"
LBIndicatorProvedInconsistent -> "[*]"
LBIndicatorDisproved -> "[-]"
LBIndicatorOpen -> "[ ]"
LBIndicatorGuessed -> "[.]"
LBIndicatorConjectured -> "[:]"
LBIndicatorHandwritten -> "[/]"
-- |
-- Represents a goal in a 'ListBox' that uses 'populateGoalsListBox'
data LBGoalView = LBGoalView { -- | status indicator
statIndicator :: LBStatusIndicator,
-- | description
goalDescription :: String
}
-- |
-- Populates a 'ListBox' with goals. After the initial call to this function
-- the number of goals is assumed to remain constant in ensuing calls.
populateGoalsListBox :: ListBox String -- ^ listbox
-> [LBGoalView] -- ^ list of goals
-- length must remain constant after the first call
-> IO ()
populateGoalsListBox lb v = do
selectedOld <- getSelection lb :: IO (Maybe [Int])
lb # value (toString v)
maybe (return ()) (mapM_ (flip selection lb)) selectedOld
where
toString = map (\ LBGoalView {statIndicator = i, goalDescription = d} ->
indicatorString i ++ ' ' : d)
-- | Converts a 'Logic.Prover.ProofStatus' into a 'LBStatusIndicator'
indicatorFromProofStatus :: ProofStatus a
-> LBStatusIndicator
indicatorFromProofStatus st = case goalStatus st of
Proved c -> if c then LBIndicatorProved else LBIndicatorProvedInconsistent
Disproved -> LBIndicatorDisproved
Open _ -> LBIndicatorOpen
-- | Converts a 'BasicProof' into a 'LBStatusIndicator'
indicatorFromBasicProof :: BasicProof
-> LBStatusIndicator
indicatorFromBasicProof p = case p of
BasicProof _ st -> indicatorFromProofStatus st
Guessed -> LBIndicatorGuessed
Conjectured -> LBIndicatorConjectured
Handwritten -> LBIndicatorHandwritten
-- | existential type for widgets that can be enabled and disabled
data EnableWid = forall wid . HasEnable wid => EnW wid
enableWids :: [EnableWid] -> IO ()
enableWids = mapM_ ( \ ew -> case ew of EnW w -> enable w >> return ())
disableWids :: [EnableWid] -> IO ()
disableWids = mapM_ ( \ ew -> case ew of EnW w -> disable w >> return ())
-- | enables widgets only if at least one entry is selected in the listbox,
-- otherwise the widgets are disabled
enableWidsUponSelection :: ListBox String -> [EnableWid] -> IO ()
enableWidsUponSelection lb goalSpecificWids =
(getSelection lb :: IO (Maybe [Int])) >>=
maybe (disableWids goalSpecificWids)
(const $ enableWids goalSpecificWids)