packages feed

manatee-template-0.0.8: Manatee/Extension/Template/TemplateView.hs

-- Author:     Your Name <YourMail>
-- Maintainer: Your Name <YourMail>
-- 
-- Copyright (C) 2010 Your Name, all rights reserved.
-- 
-- 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 3 of the License, or
-- 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, see <http://www.gnu.org/licenses/>.

{-# LANGUAGE ExistentialQuantification, RankNTypes, DeriveDataTypeable, ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
module Manatee.Extension.Template.TemplateView where

import Control.Applicative
import Control.Concurrent.STM 
import Data.Map (Map)
import Data.Text.Lazy (Text)
import Data.Typeable
import Graphics.UI.Gtk hiding (Statusbar, statusbarNew, get)
import Graphics.UI.Gtk.Gdk.SerializedEvent
import Manatee.Core.PageFrame
import Manatee.Core.Types
import Manatee.Extension.Template.TemplateBuffer
import Manatee.Toolkit.General.STM
import Manatee.Toolkit.Gtk.Gtk
import Paths_manatee_template
import System.FilePath

import qualified Data.Map as M
import qualified Graphics.UI.Gtk.SourceView.SourceView as SV

data TemplateView =
    TemplateView {templateViewPlugId          :: TVar PagePlugId
                 ,templateViewFrame           :: PageFrame
                 ,templateViewView            :: SV.SourceView
                 ,templateViewBuffer          :: TemplateBuffer
                 }
    deriving Typeable

instance PageBuffer TemplateBuffer where
    -- Get buffer name from Template buffer.
    pageBufferGetName           = readTVarIO . templateBufferFilePath

    -- Set buffer name.
    pageBufferSetName a         = writeTVarIO (templateBufferFilePath a)

    -- DBus client.
    pageBufferClient            = templateBufferClient

    -- How to create TemplateView from TemplateBuffer for multiple view design.
    pageBufferCreateView a pId  = PageViewWrap <$> templateViewNew a pId

    -- Page mode.
    pageBufferMode              = templateBufferMode

    -- Get package name to update user's configure.
    pageBufferPackageName _     = fmap takeFileName getDataDir

instance PageView TemplateView where
    -- Page buffer.
    pageViewBuffer              = PageBufferWrap . templateViewBuffer

    -- GtkPlug id.
    pageViewPlugId              = templateViewPlugId

    -- Box.
    pageViewBox                 = pageFrameBox . templateViewFrame

    -- Scrolled window.
    pageViewScrolledWindow      = pageFrameScrolledWindow . templateViewFrame

    -- How to focus when manatee focus current application.
    pageViewFocus               = widgetGrabFocus . templateViewView

    -- How to handle key action that pass from daemon process.
    pageViewHandleKeyAction     = templateViewHandleKeyAction

    -- How to handle cut action, can ignore.
    -- pageViewCut                 = templateViewCut

    -- How to handle copy action, can ignore.
    -- pageViewCopy                = templateViewCopy

    -- How to handle paste action, can ignore.
    -- pageViewPaste               = templateViewPaste

    -- How to scroll top postion, can ignore.
    -- pageViewScrollToTop         = templateViewScrollToTop

    -- How to scroll bottom postion, can ignore.
    -- pageViewScrollToBottom      = templateViewScrollToBottom

    -- How to scroll vertical page, can ignore.
    -- pageViewScrollVerticalPage  = templateViewScrollVerticalPage

    -- How to scroll vertical step, can ignore.
    -- pageViewScrollVerticalStep  = templateViewScrollVerticalStep

    -- How to scroll to left side, can ignore.
    -- pageViewScrollToLeft        = templateViewScrollToLeft

    -- How to scroll to right side, can ignore.
    -- pageViewScrollToRight       = templateViewScrollToRight

    -- How to scroll horizontal page, can ignore.
    -- pageViewScrollHorizontalPage= templateViewScrollHorizontalPage

    -- How to scroll horizontal step, can ignore.
    -- pageViewScrollHorizontalStep= templateViewScrollHorizontalStep


-- | Internal function for create string buffer.
templateViewNew :: TemplateBuffer -> PagePlugId -> IO TemplateView
templateViewNew sb plugId = do
  -- Create plug id.
  pId <- newTVarIO plugId

  -- Create UI frame.
  pFrame <- pageFrameNewWithModeName (pageModeName $ templateBufferMode sb)

  templateView <- SV.sourceViewNewWithBuffer (templateBufferBuffer sb)
  pageFrameAddChild pFrame templateView

  return $ TemplateView pId pFrame templateView sb

-- | String buffer keymap.
templateViewKeymap :: Map Text (TemplateView -> IO ())
templateViewKeymap = 
    M.fromList []

-- | Handle key action.
templateViewHandleKeyAction :: TemplateView -> Text -> SerializedEvent -> IO ()
templateViewHandleKeyAction view keystoke sEvent = 
  case M.lookup keystoke templateViewKeymap of
    -- Execute action when found in keymap.
    Just action -> action view
    -- Otherwise propagate event.
    Nothing -> widgetPropagateEvent (templateViewView view) sEvent