root/trunk/GUI/GraphTypes.hs @ 11229

Revision 11229, 6.7 kB (checked in by rpascanu, 11 months ago)

Changes to GUI to use common datatypes in Interfaces

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1{- |
2Module      :  $Header$
3Description :  Types for the Central GUI of Hets
4Copyright   :  (c) Jorina Freya Gerken, Till Mossakowski, Uni Bremen 2002-2006
5License     :  similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
6
7Maintainer  :  till@informatik.uni-bremen.de
8Stability   :  provisional
9Portability :  non-portable (imports Logic)
10-}
11
12module GUI.GraphTypes
13    ( GInfo(..)
14    , InternalNames(..)
15    , ConvFunc
16    , LibFunc
17    , DaVinciGraphTypeSyn
18    , Colors(..)
19    , getColor
20    , emptyGInfo
21    , copyGInfo
22    , lockGlobal
23    , tryLockGlobal
24    , unlockGlobal
25    )
26    where
27
28import GUI.GraphAbstraction(GraphInfo, initgraphs)
29import GUI.ProofManagement (GUIMVar)
30-- import GUI.History(CommandHistory, emptyCommandHistory)
31import GUI.UDGUtils
32
33import Static.DevGraph
34
35import Common.LibName
36import Common.Id(nullRange)
37
38import Driver.Options(HetcatsOpts(uncolored), defaultHetcatsOpts)
39
40import Data.IORef
41import qualified Data.Map as Map
42
43import Control.Concurrent.MVar
44
45import Interfaces.DataTypes
46import Interfaces.Utils
47
48
49data InternalNames = InternalNames
50                     { showNames :: Bool
51                     , updater :: [(String,(String -> String) -> IO ())]
52                     }
53
54-- | Global datatype for all GUI functions
55data GInfo = GInfo
56             { -- Global
57               intState :: IORef IntState
58--               libEnvIORef :: IORef LibEnv
59             , gi_hetcatsOpts :: HetcatsOpts
60             , windowCount :: MVar Integer
61             , exitMVar :: MVar ()
62             , globalLock :: MVar ()
63--             , globalHist :: MVar ([[LIB_NAME]],[[LIB_NAME]])
64--             , commandHist :: CommandHistory
65             , functionLock :: MVar ()
66               -- Local
67--             , gi_LIB_NAME :: LIB_NAME
68             , gi_GraphInfo :: GraphInfo
69             , internalNamesIORef :: IORef InternalNames
70             , proofGUIMVar :: GUIMVar
71             }
72
73{- | Type of the convertGraph function. Used as type of a parameter of some
74     functions in GraphMenu and GraphLogic. -}
75type ConvFunc = GInfo -> String -> LibFunc -> IO ()
76
77type LibFunc =  GInfo -> IO DaVinciGraphTypeSyn
78
79type DaVinciGraphTypeSyn =
80     Graph DaVinciGraph
81           DaVinciGraphParms
82           DaVinciNode
83           DaVinciNodeType
84           DaVinciNodeTypeParms
85           DaVinciArc
86           DaVinciArcType
87           DaVinciArcTypeParms
88
89-- | Colors to use.
90data Colors = Black
91            | Blue
92            | Coral
93            | Green
94            | Yellow
95            | Khaki
96            deriving (Eq, Ord, Show)
97
98-- | Creates an empty GInfo
99emptyGInfo :: IO GInfo
100emptyGInfo = do
101  let ihist = IntHistory {
102                 undoList = [],
103                 redoList = [] }
104      istate = emptyIntIState emptyLibEnv $ Lib_id $ Indirect_link
105                                        "" nullRange "" noTime
106      st = IntState {
107            i_state = Just istate,
108            i_hist  = ihist }
109 
110  intSt <- newIORef st
111--  iorLE <- newIORef emptyLibEnv
112  graphInfo <- initgraphs
113  iorIN <- newIORef $ InternalNames False []
114  guiMVar <- newEmptyMVar
115  gl <- newEmptyMVar
116  fl <- newEmptyMVar
117  exit <- newEmptyMVar
118  wc <- newMVar 0
119--  gh <- newMVar ([],[])
120--  ch <- emptyCommandHistory
121  return $ GInfo {
122  --              libEnvIORef = iorLE
123  --             , gi_LIB_NAME = Lib_id $ Indirect_link "" nullRange "" noTime
124                   intState = intSt
125                 , gi_GraphInfo = graphInfo
126                 , internalNamesIORef = iorIN
127                 , gi_hetcatsOpts = defaultHetcatsOpts
128                 , proofGUIMVar = guiMVar
129                 , windowCount = wc
130                 , exitMVar = exit
131                 , globalLock = gl
132  --             , globalHist = gh
133  --             , commandHist = ch
134                 , functionLock = fl
135                 }
136
137-- | Creates an empty GInfo
138copyGInfo :: GInfo -> LIB_NAME -> IO GInfo
139copyGInfo gInfo newLN = do
140  graphInfo <- initgraphs
141  iorIN <- newIORef $ InternalNames False []
142  guiMVar <- newEmptyMVar
143  intSt <- readIORef $ intState gInfo
144  let intSt' = intSt {
145                i_state = case i_state intSt of
146                           Nothing -> Nothing
147                           Just st -> Just $ st {
148                                                 i_ln = newLN}
149                    }
150  writeIORef (intState gInfo) $ intSt'
151  return $ gInfo { -- gi_LIB_NAME = newLN
152                   gi_GraphInfo = graphInfo
153                 , internalNamesIORef = iorIN
154                 , proofGUIMVar = guiMVar
155                 }
156
157{- | Acquire the global lock. If already locked it waits till it is unlocked
158     again.-}
159lockGlobal :: GInfo -> IO ()
160lockGlobal (GInfo { globalLock = lock }) = putMVar lock ()
161
162-- | Tries to acquire the global lock. Return False if already acquired.
163tryLockGlobal :: GInfo -> IO Bool
164tryLockGlobal (GInfo { globalLock = lock }) = tryPutMVar lock ()
165
166-- | Releases the global lock.
167unlockGlobal :: GInfo -> IO ()
168unlockGlobal (GInfo { globalLock = lock }) = do
169  unlocked <- tryTakeMVar lock
170  case unlocked of
171    Just () -> return ()
172    Nothing -> error "Global lock wasn't locked."
173
174-- | Generates the colortable
175colors :: Map.Map (Colors, Bool, Bool) (String, String)
176colors = Map.fromList
177  [ ((Black,  False, False), ("gray0",           "gray0" ))
178  , ((Black,  False, True ), ("gray30",          "gray5" ))
179  , ((Blue,   False, False), ("RoyalBlue3",      "gray20"))
180  , ((Blue,   False, True ), ("RoyalBlue1",      "gray23"))
181  , ((Blue,   True,  False), ("SteelBlue3",      "gray27"))
182  , ((Blue,   True,  True ), ("SteelBlue1",      "gray30"))
183  , ((Coral,  False, False), ("coral3",          "gray40"))
184  , ((Coral,  False, True ), ("coral1",          "gray43"))
185  , ((Coral,  True,  False), ("LightSalmon2",    "gray47"))
186  , ((Coral,  True,  True ), ("LightSalmon",     "gray50"))
187  , ((Green,  False, False), ("MediumSeaGreen",  "gray60"))
188  , ((Green,  False, True ), ("PaleGreen3",      "gray63"))
189  , ((Green,  True,  False), ("PaleGreen2",      "gray67"))
190  , ((Green,  True,  True ), ("LightGreen",      "gray70"))
191  , ((Yellow, False, False), ("gold2",           "gray78"))
192  , ((Yellow, False, True ), ("gold",            "gray81"))
193  , ((Khaki,  False, False), ("LightGoldenrod3", "gray85"))
194  , ((Khaki,  False, True ), ("LightGoldenrod",  "gray88"))
195  ]
196
197-- | Converts colors to grayscale if needed
198getColor :: HetcatsOpts
199         -> Colors -- ^ Colorname
200         -> Bool -- ^ Colorvariant
201         -> Bool -- ^ Lightvariant
202         -> String
203getColor opts c v l = case Map.lookup (c, v, l) colors of
204  Just (cname, gname) -> if uncolored opts then gname else cname
205  Nothing -> error $ "Color not defined: "
206                  ++ (if v then "alternative " else "")
207                  ++ (if l then "light " else "")
208                  ++ show c
Note: See TracBrowser for help on using the browser.