-- | Menu and toolbars for playing Chu Shogi
-- Copyright 2009 Colin Adams
--
-- This file is part of chu-shogi.
--
-- Chu-shogi is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- Chu-shogi is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with chu-shogi. If not, see .
module Menu
(
-- * Creation
make_menu_bar
) where
import Data.IORef
import Graphics.UI.Gtk hiding (function)
import Game_state hiding (pieces)
import Move_parser
-- | Create menu bar for main window
make_menu_bar :: IORef Game_state -- ^ Mutable reference to current game state
-> IORef Bool -- ^ Mutable reference - should the board be displayed with White at the bottom?
-> DrawingArea -- ^ Image of the board
-> (Game_state -> Game_state) -- ^ Function to update newly created game state when New game is requested
-> IO (MenuBar, Toolbar)
make_menu_bar game_state rotate_ior canvas function = do
file_action <- actionNew "file_action" "File" Nothing Nothing
view_action <- actionNew "view_action" "View" Nothing Nothing
load_game_action <- actionNew "load_game_action" "Load game" (Just "Load a previously saved game") (Just stockOpen)
new_game_action <- actionNew "new_game_action" "New game" (Just "Start a new game") (Just stockNew)
quit_action <- actionNew "quit_action" "Quit" (Just "Quit playing Chu Shogi") (Just stockQuit)
rotate_action <- actionNew "rotate_action" "Rotate" (Just "Rotate the board by 180 degress") Nothing
load_game_action `onActionActivate` (load_game game_state rotate_ior canvas)
new_game_action `onActionActivate` (set_game_state game_state canvas function)
quit_action `onActionActivate` mainQuit
rotate_action `onActionActivate` (rotate_board rotate_ior canvas)
standard_group <- actionGroupNew "standard"
mapM_ (actionGroupAddAction standard_group) [file_action, view_action]
mapM_ (\act -> actionGroupAddActionWithAccel standard_group act Nothing)
[load_game_action, new_game_action, quit_action, rotate_action]
ui <- uiManagerNew
mid <- uiManagerAddUiFromString ui basic_menu_description
uiManagerInsertActionGroup ui standard_group 0
(Just menu_bar) <- uiManagerGetWidget ui "/ui/menubar"
(Just tool_bar) <- uiManagerGetWidget ui "/ui/toolbar"
return (castToMenuBar menu_bar, castToToolbar tool_bar)
-- Implementation
basic_menu_description :: String
basic_menu_description =
"\
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\"
set_game_state :: IORef Game_state -> DrawingArea -> (Game_state -> Game_state) -> IO ()
set_game_state game_state canvas function = do
writeIORef game_state new_game_state
modifyIORef game_state function
widgetQueueDraw canvas
return ()
load_game :: IORef Game_state -> IORef Bool -> DrawingArea -> IO ()
load_game game_state_ior _ canvas = do
chooser <- fileChooserDialogNew (Just "Load game from file") Nothing FileChooserActionOpen [("Load", ResponseOk), ("Cancel", ResponseCancel)]
fileChooserSetSelectMultiple chooser False
csg_filter <- fileFilterNew
fileFilterSetName csg_filter "Chu Shogi game"
fileFilterAddPattern csg_filter "*.csg"
fileChooserAddFilter chooser csg_filter
csg_filter2 <- fileFilterNew
fileFilterSetName csg_filter2 "All files"
fileFilterAddPattern csg_filter2 "*.*"
fileChooserAddFilter chooser csg_filter2
response <- dialogRun chooser
widgetHide chooser
case response of
ResponseCancel -> return ()
ResponseOk -> do
file_name <- fileChooserGetFilename chooser
case file_name of
Nothing -> return ()
Just f -> do
let (i, ni_state, moves) = new_game_state
r <- parse_moves f (ni_state, Just moves)
case r of
Left e -> print e
Right (ni_state', Just moves') -> do
writeIORef game_state_ior (i, ni_state', moves')
widgetQueueDraw canvas
{-# contract rotate_board :: Ok -> Ok -> {??? } #-}
rotate_board :: IORef Bool -> DrawingArea -> IO ()
rotate_board rotate_ior canvas = do
modifyIORef rotate_ior not
widgetQueueDraw canvas
return ()