{-|
Module contains functionality for creating the menu to be
located at the top of the file browser. This menu contains
actions that may be selected by the user, and upon selection
there are associated actions that will be carried out by the file
manager.
-}
module Menu
(
menu,
contextMenu
)
where
import Graphics.UI.Gtk
import Components
import Types
import DirectoryOperations
import TreeViewOperations
import Graphics.UI.Gtk.Gdk.Events
{-|
Construct the File Manager's menu.
-}
fmMenubar :: String -- ^ XML representation of the menu.
fmMenubar =
"\
\ \
\ \
\ \
\ \
\"
{-|
Construct the Directory & File context menu.
-}
contextMenuOpt :: String -- ^ XML representation of the menu.
contextMenuOpt =
" \
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\ \
\"
{-|
Constructs the menu. Creates actions and associates them with
the individual parts of the menu. Then adds \"listeners\" to the
menu options, and sets the actions to be carried out upon selection.
-}
menu :: FileBrowser -- ^ The main components of the file browser.
-> Window -- ^ The window containing all widgets.
-> IO Widget -- ^ Widget conatining the menu.
menu browse window = do
--Define actions.
fileAct <- actionNew "FileAction" "File" Nothing Nothing
helpAct <- actionNew "HelpAction" "Help" Nothing Nothing
newFileAct <- actionNew "NewFile" "New File"
( Just "Create a new file." )
( Nothing )
newFolderAct <- actionNew "NewFolder" "New Folder"
( Just "Create a new folder." )
( Nothing )
deleteSelectedAct <- actionNew "DeleteSelected" "Delete"
( Just "Delete the selected item." )
( Nothing )
moveSelectedAct <- actionNew "MoveSelected" "Move"
( Just "Move the selected item." )
( Nothing )
copySelectedAct <- actionNew "CopySelected" "Copy"
( Just "Copy the selected item." )
( Nothing )
renameSelectedAct <- actionNew "RenameSelected" "Rename"
( Just "Rename the selected item." )
( Nothing )
exitAct <- actionNew "ExitAction" "Exit"
( Just "Exit Haskell File Manager." )
( Nothing )
--Help items.
aboutAct <- actionNew "AboutAction" "About HaskellFM"
(Just "About Haskell File Manager.")
( Nothing )
--Define actions to be taken upon option selection.
onActionActivate newFileAct ( createFile ( fileStore browse ) )
onActionActivate newFolderAct ( createDir ( dirStore browse ) )
onActionActivate deleteSelectedAct ( operateSelection deleteFolder deleteFile browse )
onActionActivate moveSelectedAct ( operateSelection moveFolder moveFile browse )
onActionActivate copySelectedAct ( operateSelection copyFolder copyFile browse )
onActionActivate renameSelectedAct( operateSelection renameFolder renameFile browse )
onActionActivate exitAct ( widgetDestroy window )
onActionActivate aboutAct ( aboutHaskellFM )
--Create group of actions
actionGroup <- actionGroupNew "standard"
--Map actions to group with/without accelerators
mapM_ ( actionGroupAddAction actionGroup )
[fileAct, helpAct, exitAct, newFileAct, newFolderAct, deleteSelectedAct, moveSelectedAct, copySelectedAct, renameSelectedAct, aboutAct]
--ialise UI menu
ui <- uiManagerNew
uiManagerAddUiFromString ui fmMenubar
uiManagerInsertActionGroup ui actionGroup 0
(Just menuBar) <- uiManagerGetWidget ui "/ui/menubar"
return menuBar
{-|
Constructs the menu to be displayed in the file and
directory TreeViews. It creates actions, then creates
listeners for these actions, and then adds a listener
to the supplied TreeView.
-}
contextMenu :: FileBrowser -- ^ The 'FileBrowser' actions are to be carried out upon.
-> TreeView -- ^ The TreeView that the context menu shall appear in.
-> IO ( )
contextMenu browse tree = do
newFile <- actionNew "NewFile" "New File"
( Just "Create File" )
Nothing
newDir <- actionNew "NewFolder" "New Folder"
( Just "Create Folder" )
Nothing
delSel <- actionNew "DeleteSelected" "Delete"
( Just "Delete Selection" )
Nothing
moveSel <- actionNew "MoveSelected" "Move"
( Just "Move Selection" )
Nothing
copySel <- actionNew "CopySelected" "Copy"
( Just "Copy Selection" )
Nothing
remSel <- actionNew "RenameSelected" "Rename"
( Just "Rename Selection" )
Nothing
--Add listeners
onActionActivate newFile ( createFile ( fileStore browse ) )
onActionActivate newDir ( createDir ( dirStore browse ) )
onActionActivate delSel ( operateSelection deleteFolder deleteFile browse )
onActionActivate moveSel ( operateSelection moveFolder moveFile browse )
onActionActivate copySel ( operateSelection copyFolder copyFile browse )
onActionActivate remSel ( operateSelection renameFolder renameFile browse )
--map actions
actions <- actionGroupNew "context"
mapM_ ( actionGroupAddAction actions )
[newFile, newDir, delSel, moveSel, copySel, remSel]
conMan <- uiManagerNew
uiManagerAddUiFromString conMan contextMenuOpt
uiManagerInsertActionGroup conMan actions 0
( Just popupMen ) <- uiManagerGetWidget conMan "/ui/popup"
onButtonPress tree (\x -> case ( eventButton x ) of
RightButton -> ( menuPopup ( castToMenu ( popupMen ) ) Nothing ) >> return ( eventSent x )
_ -> return ( eventSent x ) )
return ( )