{-# LINE 2 "./Graphics/UI/Gtk/SourceView/SourceIter.chs" #-}
-- -*-haskell-*-
-- GIMP Toolkit (GTK) SourceIter
--
-- Author : Peter Gavin
-- derived from sourceview bindings by Axel Simon and Duncan Coutts
--
-- Created: 18 December 2008
--
-- Copyright (C) 2004-2008 Peter Gavin, Duncan Coutts, Axel Simon
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library 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
-- Lesser General Public License for more details.
--
-- |
-- Maintainer : gtk2hs-users@lists.sourceforge.net
-- Stability : provisional
-- Portability : portable (depends on GHC)
--
-- Adds extra useful methods for "TextIter" for searching forwards and
-- backwards within a region in the buffer and matching brackets.
--
-- * There is no SourceIter object, just extra methods for "TextIter"
--
module Graphics.UI.Gtk.SourceView.SourceIter (
  SourceSearchFlags(..),
  sourceIterForwardSearch,
  sourceIterBackwardSearch,
) where

import Control.Monad (liftM)
import Data.Maybe (fromMaybe)

import System.Glib.FFI
import System.Glib.Flags (Flags, fromFlags)
import System.Glib.UTFString
import Graphics.UI.Gtk.Abstract.Object (makeNewObject)
import Graphics.UI.Gtk.SourceView.Types
{-# LINE 46 "./Graphics/UI/Gtk/SourceView/SourceIter.chs" #-}
import Graphics.UI.Gtk.Multiline.TextIter
{-# LINE 47 "./Graphics/UI/Gtk/SourceView/SourceIter.chs" #-}


{-# LINE 49 "./Graphics/UI/Gtk/SourceView/SourceIter.chs" #-}

data SourceSearchFlags = SourceSearchVisibleOnly
                       | SourceSearchTextOnly
                       | SourceSearchCaseInsensitive
                       deriving (Eq,Bounded,Show,Read)
instance Enum SourceSearchFlags where
  fromEnum SourceSearchVisibleOnly = 1
  fromEnum SourceSearchTextOnly = 2
  fromEnum SourceSearchCaseInsensitive = 4

  toEnum 1 = SourceSearchVisibleOnly
  toEnum 2 = SourceSearchTextOnly
  toEnum 4 = SourceSearchCaseInsensitive
  toEnum unmatched = error ("SourceSearchFlags.toEnum: Cannot match " ++ show unmatched)

  succ SourceSearchVisibleOnly = SourceSearchTextOnly
  succ SourceSearchTextOnly = SourceSearchCaseInsensitive
  succ _ = undefined

  pred SourceSearchTextOnly = SourceSearchVisibleOnly
  pred SourceSearchCaseInsensitive = SourceSearchTextOnly
  pred _ = undefined

  enumFromTo x y | fromEnum x == fromEnum y = [ y ]
                 | otherwise = x : enumFromTo (succ x) y
  enumFrom x = enumFromTo x SourceSearchCaseInsensitive
  enumFromThen _ _ =     error "Enum SourceSearchFlags: enumFromThen not implemented"
  enumFromThenTo _ _ _ =     error "Enum SourceSearchFlags: enumFromThenTo not implemented"

{-# LINE 51 "./Graphics/UI/Gtk/SourceView/SourceIter.chs" #-}

instance Flags SourceSearchFlags

-- methods

-- | same as 'textIterForwardSearch' but allows
-- case insensitive search and possibly in the future regular expressions.
--
sourceIterForwardSearch :: TextIter -> String -> [SourceSearchFlags] ->
                           Maybe TextIter -> IO (Maybe (TextIter, TextIter))
sourceIterForwardSearch ti str flags limit = do
   start <- makeEmptyTextIter
   end <- makeEmptyTextIter
   found <- liftM toBool $ withUTFString str $ \cStr ->
     (\(TextIter arg1) arg2 arg3 (TextIter arg4) (TextIter arg5) (TextIter arg6) -> withForeignPtr arg1 $ \argPtr1 ->withForeignPtr arg4 $ \argPtr4 ->withForeignPtr arg5 $ \argPtr5 ->withForeignPtr arg6 $ \argPtr6 ->gtk_source_iter_forward_search argPtr1 arg2 arg3 argPtr4 argPtr5 argPtr6) ti cStr
       ((fromIntegral.fromFlags) flags) start end
       (fromMaybe (TextIter nullForeignPtr) limit)
   return $ if found then Just (start,end) else Nothing

-- | same as 'textIterForwardSearch' but allows
-- case insensitive search and possibly in the future regular expressions.
--
sourceIterBackwardSearch :: TextIter -> String -> [SourceSearchFlags] ->
                           Maybe TextIter -> IO (Maybe (TextIter, TextIter))
sourceIterBackwardSearch ti str flags limit = do
   start <- makeEmptyTextIter
   end <- makeEmptyTextIter
   found <- liftM toBool $ withUTFString str $ \cStr ->
     (\(TextIter arg1) arg2 arg3 (TextIter arg4) (TextIter arg5) (TextIter arg6) -> withForeignPtr arg1 $ \argPtr1 ->withForeignPtr arg4 $ \argPtr4 ->withForeignPtr arg5 $ \argPtr5 ->withForeignPtr arg6 $ \argPtr6 ->gtk_source_iter_backward_search argPtr1 arg2 arg3 argPtr4 argPtr5 argPtr6) ti cStr
       ((fromIntegral.fromFlags) flags) start end
       (fromMaybe (TextIter nullForeignPtr) limit)
   return $ if found then Just (start,end) else Nothing

foreign import ccall unsafe "gtk_source_iter_forward_search"
  gtk_source_iter_forward_search :: ((Ptr TextIter) -> ((Ptr CChar) -> (CInt -> ((Ptr TextIter) -> ((Ptr TextIter) -> ((Ptr TextIter) -> (IO CInt)))))))

foreign import ccall unsafe "gtk_source_iter_backward_search"
  gtk_source_iter_backward_search :: ((Ptr TextIter) -> ((Ptr CChar) -> (CInt -> ((Ptr TextIter) -> ((Ptr TextIter) -> ((Ptr TextIter) -> (IO CInt)))))))