packages feed

coin-1.2: src/Coin/UI/MainWindow.hs

{-
 *  Programmer:	Piotr Borek
 *  E-mail:     piotrborek@op.pl
 *  Copyright 2015 Piotr Borek
 *
 *  Distributed under the terms of the GPL (GNU Public License)
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-}

module Coin.UI.MainWindow (
    mainWindowNew
) where

import Graphics.UI.Gtk
import Control.Monad
import Control.Monad.IO.Class
import Data.Maybe

import Coin.DB.Tables
import Coin.UI.MainMultiList
import Coin.UI.Accounts.AccountsWidget
import Coin.UI.Options.OptionsTag
import Coin.UI.Options.OptionsAccount
import Coin.UI.Widgets.StackWidget
import Coin.UI.Builder.GtkUIBuilder
import Coin.UI.Raports.RaportHistory
import Coin.UI.Raports.RaportHistoryAll
import Coin.UI.Raports.RaportSummary
import Coin.UI.Raports.RaportQuery
import Coin.UI.Widgets.MultiList

import Coin.UI.MainState

mainWindowNew :: MainState -> IO Window
mainWindowNew mainState = do
    stack <- stackWidgetNew
    multiList <- mainMultiListNew mainState stack

    emptyPage <- labelNew $ Just ""
    accounts <- accountsWidgetNew mainState
    tags <- optionsTagNew mainState
    accountsList <- optionsAccountNew mainState
    raportHistory <- raportHistoryNew mainState
    raportHistoryAll <- raportHistoryAllNew mainState
    raportSummary <- raportSummaryNew mainState
    raportQuery <- raportQueryNew mainState

    stackWidgetAdd stack "empty" emptyPage
    stackWidgetAdd stack "accounts" accounts
    stackWidgetAdd stack "tagsList" tags
    stackWidgetAdd stack "accountsList" accountsList
    stackWidgetAdd stack "raportHistory" raportHistory
    stackWidgetAdd stack "raportHistoryAll" raportHistoryAll
    stackWidgetAdd stack "raportSummary" raportSummary
    stackWidgetAdd stack "raportQuery" raportQuery

    (getObject, root) <- uiBuildGtk $ do
        windowAttrs [ windowTitle := "Coin"]
        window Nothing "" $
            scrolledWindow Nothing $
                hPaned (Just "paned")
                    (putWidget multiList)
                    (putWidget stack)

    let mainWindow = castToWindow root
    let paned = castToPaned . fromJust . getObject $ "paned"

    widgetShowAll mainWindow
    multiListButtonShow multiList 0
    stackWidgetShow stack "empty"

    void $ on mainWindow objectDestroy mainQuit
    void $ on mainWindow deleteEvent $ do
        liftIO $ do
            mainStateSavePropertiesOnDisk mainState
            accountIDs <- optionsAccountToIDList accountsList
            accountsOptionsTableUpdate accountIDs

        return False

    mainStateSavePropertiesAction mainState "Coin.UI.MainWindow" $ do
        (width, height) <- liftIO $ windowGetSize mainWindow
        propertyInsert "width" width
        propertyInsert "height" height

        (x, y) <- liftIO $ windowGetPosition mainWindow
        propertyInsert "x" x
        propertyInsert "y" y

        panedPos <- liftIO $ panedGetPosition paned
        propertyInsert "panedPos" panedPos

        cursor <- liftIO $ multiListOptionGetCursor multiList
        let (index, path) = case cursor of
                                Just (i, p) -> (Just i, Just p)
                                Nothing     -> (Nothing, Nothing)
        propertyInsertMaybe "multiListIndex" index
        propertyInsertMaybe "multiListPath" path

        stackIndex <- liftIO $ stackWidgetGetVisible stack
        propertyInsertMaybe "stackIndex" stackIndex

    windowResize mainWindow 1024 768
    panedSetPosition paned 200

    mainStateReadPropertiesAction mainState "Coin.UI.MainWindow" $ do
        propertyRead2 "width" "height" $ \width height ->
            liftIO $ windowResize mainWindow width height
        propertyRead2 "x" "y" $ \x y ->
            liftIO $ windowMove mainWindow x y
        propertyRead "panedPos" $ \panedPos ->
            liftIO $ panedSetPosition paned panedPos

        propertyRead "multiListIndex" $ \index ->
            propertyRead "multiListPath" $ \path -> do
                liftIO $ multiListOptionSetCursor multiList index path
                propertyRead "stackIndex" $ \stackIndex ->
                    liftIO $ stackWidgetShow stack stackIndex

    return mainWindow