| 1 |
{-# OPTIONS_GHC -fno-cse #-} |
| 2 |
|
| 3 |
-------------------------------------------------------------------------------- |
| 4 |
-- | |
| 5 |
-- Module : Graphics.UI.GLUT.Menu |
| 6 |
-- Copyright : (c) Sven Panne 2002-2018 |
| 7 |
-- License : BSD3 |
| 8 |
-- |
| 9 |
-- Maintainer : Sven Panne <[email protected]> |
| 10 |
-- Stability : stable |
| 11 |
-- Portability : portable |
| 12 |
-- |
| 13 |
-- GLUT supports simple cascading pop-up menus. They are designed to let a user |
| 14 |
-- select various modes within a program. The functionality is simple and |
| 15 |
-- minimalistic and is meant to be that way. Do not mistake GLUT\'s pop-up menu |
| 16 |
-- facility with an attempt to create a full-featured user interface. |
| 17 |
-- |
| 18 |
-------------------------------------------------------------------------------- |
| 19 |
|
| 20 |
module Graphics.UI.GLUT.Menu ( |
| 21 |
Menu(..), MenuItem(..), MenuCallback, attachMenu, |
| 22 |
numMenuItems |
| 23 |
) where |
| 24 |
|
| 25 |
import Control.Monad.IO.Class ( MonadIO(..) ) |
| 26 |
import Control.Monad ( when, unless, zipWithM ) |
| 27 |
import Data.Array ( listArray, (!) ) |
| 28 |
import Data.IORef ( IORef, newIORef, readIORef, modifyIORef ) |
| 29 |
import qualified Data.Map as M |
| 30 |
import Data.StateVar ( get, ($=), GettableStateVar, makeGettableStateVar |
| 31 |
, StateVar, makeStateVar ) |
| 32 |
import Foreign.C.String ( withCString ) |
| 33 |
import Foreign.C.Types ( CInt ) |
| 34 |
import Foreign.Ptr ( freeHaskellFunPtr ) |
| 35 |
import System.IO.Unsafe ( unsafePerformIO ) |
| 36 |
|
| 37 |
import Graphics.UI.GLUT.Callbacks.Registration |
| 38 |
import Graphics.UI.GLUT.QueryUtils |
| 39 |
import Graphics.UI.GLUT.Raw |
| 40 |
import Graphics.UI.GLUT.Types |
| 41 |
|
| 42 |
-------------------------------------------------------------------------------- |
| 43 |
|
| 44 |
-- | A menu is simply a list of menu items, possibly with an associated font. |
| 45 |
data Menu |
| 46 |
= Menu [MenuItem] |
| 47 |
| MenuWithFont BitmapFont [MenuItem] |
| 48 |
|
| 49 |
menuFont :: Menu -> Maybe BitmapFont |
| 50 |
menuFont (Menu _) = Nothing |
| 51 |
menuFont (MenuWithFont font _) = Just font |
| 52 |
|
| 53 |
menuItems :: Menu -> [MenuItem] |
| 54 |
menuItems (Menu items) = items |
| 55 |
menuItems (MenuWithFont _ items) = items |
| 56 |
|
| 57 |
-- | A single item within a menu can either be a plain menu entry or a sub-menu |
| 58 |
-- entry, allowing for arbitrarily deep nested menus. |
| 59 |
data MenuItem |
| 60 |
= MenuEntry String MenuCallback -- ^ A plain menu entry with an associated |
| 61 |
-- callback, which is triggered when the |
| 62 |
-- user selects the entry |
| 63 |
| SubMenu String Menu -- ^ A sub-menu, which is cascaded when the |
| 64 |
-- user selects the entry, allowing |
| 65 |
-- sub-menu entries to be selected |
| 66 |
|
| 67 |
type MenuCallback = IO () |
| 68 |
|
| 69 |
-- | Create a new pop-up menu for the /current window,/ attaching it to the |
| 70 |
-- given mouse button. A previously attached menu (if any), is detached before |
| 71 |
-- and won\'t receive callbacks anymore. |
| 72 |
-- |
| 73 |
-- It is illegal to call 'attachMenu' while any (sub-)menu is in use, i.e. |
| 74 |
-- popped up. |
| 75 |
-- |
| 76 |
-- /X Implementation Notes:/ If available, GLUT for X will take advantage of |
| 77 |
-- overlay planes for implementing pop-up menus. The use of overlay planes can |
| 78 |
-- eliminate display callbacks when pop-up menus are deactivated. The |
| 79 |
-- @SERVER_OVERLAY_VISUALS@ convention is used to determine if overlay visuals |
| 80 |
-- are available. |
| 81 |
|
| 82 |
attachMenu :: MonadIO m => MouseButton -> Menu -> m () |
| 83 |
attachMenu mouseButton menu = liftIO $ do |
| 84 |
win <- getCurrentWindow "attachMenu" |
| 85 |
let hook = MenuHook win mouseButton |
| 86 |
detachMenu hook |
| 87 |
unless (null (menuItems menu)) $ do |
| 88 |
(_, destructor) <- traverseMenu menu |
| 89 |
addToMenuTable hook destructor |
| 90 |
attachMenu_ mouseButton |
| 91 |
|
| 92 |
detachMenu :: MenuHook -> IO () |
| 93 |
detachMenu hook@(MenuHook _ mouseButton) = do |
| 94 |
maybeDestructor <- lookupInMenuTable hook |
| 95 |
case maybeDestructor of |
| 96 |
Nothing -> return () |
| 97 |
Just destructor -> do detachMenu_ mouseButton |
| 98 |
destructor |
| 99 |
deleteFromMenuTable hook |
| 100 |
|
| 101 |
traverseMenu :: Menu -> IO (MenuID, Destructor) |
| 102 |
traverseMenu menu = do |
| 103 |
let items = menuItems menu |
| 104 |
callbackArray = listArray (1, length items) (map makeCallback items) |
| 105 |
cb <- makeMenuFunc (\i -> callbackArray ! (fromIntegral i)) |
| 106 |
menuID <- glutCreateMenu cb |
| 107 |
maybe (return ()) (setMenuFont menuID) (menuFont menu) |
| 108 |
destructors <- zipWithM addMenuItem items [1..] |
| 109 |
let destructor = do sequence_ destructors |
| 110 |
glutDestroyMenu menuID |
| 111 |
freeHaskellFunPtr cb |
| 112 |
return (menuID, destructor) |
| 113 |
|
| 114 |
makeCallback :: MenuItem -> MenuCallback |
| 115 |
makeCallback (MenuEntry _ cb) = cb |
| 116 |
makeCallback _ = error "shouldn't receive a callback for submenus" |
| 117 |
|
| 118 |
addMenuItem :: MenuItem -> Value -> IO Destructor |
| 119 |
addMenuItem (MenuEntry s _) v = do |
| 120 |
addMenuEntry s v |
| 121 |
return $ glutRemoveMenuItem 1 |
| 122 |
addMenuItem (SubMenu s m) _ = do |
| 123 |
(menuID, destructor) <- saveExcursion (traverseMenu m) |
| 124 |
addSubMenu s menuID |
| 125 |
return $ do glutRemoveMenuItem 1 |
| 126 |
destructor |
| 127 |
|
| 128 |
-- Perform an action, saving/restoring the current menu around it |
| 129 |
saveExcursion :: IO a -> IO a |
| 130 |
saveExcursion act = do |
| 131 |
menuID <- get currentMenu |
| 132 |
returnValue <- act |
| 133 |
when (isRealMenu menuID) $ |
| 134 |
currentMenu $= menuID |
| 135 |
return returnValue |
| 136 |
|
| 137 |
-------------------------------------------------------------------------------- |
| 138 |
-- This seems to be a common Haskell hack nowadays: A plain old global variable |
| 139 |
-- with an associated mutator. Perhaps some language/library support is needed? |
| 140 |
|
| 141 |
{-# NOINLINE theMenuTable #-} |
| 142 |
theMenuTable :: IORef MenuTable |
| 143 |
theMenuTable = unsafePerformIO (newIORef emptyMenuTable) |
| 144 |
|
| 145 |
getMenuTable :: IO MenuTable |
| 146 |
getMenuTable = readIORef theMenuTable |
| 147 |
|
| 148 |
modifyMenuTable :: (MenuTable -> MenuTable) -> IO () |
| 149 |
modifyMenuTable = modifyIORef theMenuTable |
| 150 |
|
| 151 |
-------------------------------------------------------------------------------- |
| 152 |
-- To facilitate cleanup, we have to keep track how to destroy menus which are |
| 153 |
-- currently attached in a window to a mouse button. |
| 154 |
|
| 155 |
data MenuHook = MenuHook Window MouseButton |
| 156 |
deriving ( Eq, Ord ) |
| 157 |
|
| 158 |
type Destructor = IO () |
| 159 |
|
| 160 |
type MenuTable = M.Map MenuHook Destructor |
| 161 |
|
| 162 |
emptyMenuTable :: MenuTable |
| 163 |
emptyMenuTable = M.empty |
| 164 |
|
| 165 |
lookupInMenuTable :: MenuHook -> IO (Maybe Destructor) |
| 166 |
lookupInMenuTable callbackID = |
| 167 |
fmap (M.lookup callbackID) getMenuTable |
| 168 |
|
| 169 |
deleteFromMenuTable :: MenuHook -> IO () |
| 170 |
deleteFromMenuTable callbackID = |
| 171 |
modifyMenuTable (M.delete callbackID) |
| 172 |
|
| 173 |
addToMenuTable :: MenuHook -> Destructor -> IO () |
| 174 |
addToMenuTable callbackID funPtr = |
| 175 |
modifyMenuTable (M.insert callbackID funPtr) |
| 176 |
|
| 177 |
-------------------------------------------------------------------------------- |
| 178 |
|
| 179 |
type MenuID = CInt |
| 180 |
type Value = CInt |
| 181 |
|
| 182 |
-------------------------------------------------------------------------------- |
| 183 |
|
| 184 |
-- | Controls the /current menu./ If no menus exist or the previous /current |
| 185 |
-- menu/ was destroyed, a pseudo menu is returned. |
| 186 |
|
| 187 |
currentMenu :: StateVar MenuID |
| 188 |
currentMenu = makeStateVar glutGetMenu glutSetMenu |
| 189 |
|
| 190 |
-- | Returns 'True' if the given menu identifier refers to a real menu, not |
| 191 |
-- a pseudo one. |
| 192 |
|
| 193 |
isRealMenu :: MenuID -> Bool |
| 194 |
isRealMenu = (/= 0) |
| 195 |
|
| 196 |
-------------------------------------------------------------------------------- |
| 197 |
|
| 198 |
-- | Add a menu entry to the bottom of the /current menu./ The given string will |
| 199 |
-- be displayed for the newly added menu entry. If the menu entry is selected by |
| 200 |
-- the user, the menu\'s callback will be called passing the given value as the |
| 201 |
-- callback\'s parameter. |
| 202 |
|
| 203 |
addMenuEntry :: String -> Value -> IO () |
| 204 |
addMenuEntry name value = withCString name $ \n -> glutAddMenuEntry n value |
| 205 |
|
| 206 |
-- | Add a sub-menu trigger to the bottom of the /current menu./ The given |
| 207 |
-- string will be displayed for the newly added sub-menu trigger. If the |
| 208 |
-- sub-menu trigger is entered, the sub-menu specified by the given menu |
| 209 |
-- identifier will be cascaded, allowing sub-menu menu items to be selected. |
| 210 |
|
| 211 |
addSubMenu :: String -> MenuID -> IO () |
| 212 |
addSubMenu name menuID = withCString name $ \n -> glutAddSubMenu n menuID |
| 213 |
|
| 214 |
-------------------------------------------------------------------------------- |
| 215 |
|
| 216 |
{- UNUSED |
| 217 |
-- | Change the specified menu entry in the /current menu/ into a menu entry. |
| 218 |
-- The given position determines which menu item should be changed and must be |
| 219 |
-- between 1 (the topmost menu item) and |
| 220 |
-- 'Graphics.UI.GLUT.State.getNumMenuItems' inclusive. The menu item to change |
| 221 |
-- does not have to be a menu entry already. The given string will be displayed |
| 222 |
-- for the newly changed menu entry. The given value will be returned to the |
| 223 |
-- menu\'s callback if this menu entry is selected. |
| 224 |
|
| 225 |
foreign import CALLCONV unsafe "glutChangeToMenuEntry" glutChangeToMenuEntry :: |
| 226 |
Item -> CString -> Value -> IO () |
| 227 |
|
| 228 |
-- | Change the specified menu item in the /current menu/ into a sub-menu |
| 229 |
-- trigger. The given position determines which menu item should be changed and |
| 230 |
-- must be between 1 and 'Graphics.UI.GLUT.State.getNumMenuItems' inclusive. The |
| 231 |
-- menu item to change does not have to be a sub-menu trigger already. The |
| 232 |
-- given name will be displayed for the newly changed sub-menu trigger. The |
| 233 |
-- given menu identifier names the sub-menu to cascade from the newly added |
| 234 |
-- sub-menu trigger. |
| 235 |
|
| 236 |
foreign import CALLCONV unsafe "glutChangeToSubMenu" glutChangeToSubMenu :: |
| 237 |
Item -> CString -> MenuID -> IO () |
| 238 |
-} |
| 239 |
|
| 240 |
-------------------------------------------------------------------------------- |
| 241 |
|
| 242 |
-- | Attach a mouse button for the /current window/ to the identifier of the |
| 243 |
-- /current menu./ By attaching a menu identifier to a button, the named menu |
| 244 |
-- will be popped up when the user presses the specified button. Note that the |
| 245 |
-- menu is attached to the button by identifier, not by reference. |
| 246 |
|
| 247 |
attachMenu_ :: MouseButton -> IO () |
| 248 |
attachMenu_ = glutAttachMenu . marshalMouseButton |
| 249 |
|
| 250 |
-- | Detach an attached mouse button from the /current window./ |
| 251 |
|
| 252 |
detachMenu_ :: MouseButton -> IO () |
| 253 |
detachMenu_ = glutDetachMenu . marshalMouseButton |
| 254 |
|
| 255 |
-------------------------------------------------------------------------------- |
| 256 |
|
| 257 |
-- | Contains the number of menu items in the /current menu./ |
| 258 |
|
| 259 |
numMenuItems :: GettableStateVar Int |
| 260 |
numMenuItems = makeGettableStateVar $ simpleGet fromIntegral glut_MENU_NUM_ITEMS |
| 261 |
|
| 262 |
-------------------------------------------------------------------------------- |
| 263 |
|
| 264 |
setMenuFont :: MenuID -> BitmapFont -> IO () |
| 265 |
setMenuFont menuID font = glutSetMenuFont menuID =<< marshalBitmapFont font |