    {-# LINE 2 "./Graphics/UI/Gtk/ModelView/TreeSortable.chs" #-}
    -- -*-haskell-*-
    -- GIMP Toolkit (GTK) Interface TreeSortable
    --
    -- Author : Axel Simon
    --
    -- Created: 8 Mar 2007
    --
    -- Copyright (C) 1999-2007 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.
    --
    -- Note: there is a constant called (-2)
    -- which is only used in the C implementation of list store and tree store.
    -- The TreeModelSort proxy only uses the default column constant. Hence, we do
    -- not expose or tell the user about the UNSORTED constant since it can only
    -- be confusing.
    --
    -- |
    -- Maintainer : gtk2hs-users@lists.sourceforge.net
    -- Stability : provisional
    -- Portability : portable (depends on GHC)
    --
    -- The interface for sortable models used by 'TreeView'
    --
    module Graphics.UI.Gtk.ModelView.TreeSortable (

    -- * Detail
    --
    -- | 'TreeSortable' is an interface to be implemented by tree models which
    -- support sorting. The 'TreeView' uses the methods provided by this interface
    -- to sort the model. As of now, only the
    -- 'Graphics.UI.Gtk.ModelView.TreeModelSort.TreeModelSort' proxy supports the
    -- sortable interface. Thus, in order to enable sortable columns in a
    -- 'TreeView', it is necessary to wrap a
    -- 'Graphics.UI.Gtk.ModelView.ListStore.ListStore' or
    -- 'Graphics.UI.Gtk.ModelView.TreeStore.TreeStore' model in a
    -- 'Graphics.UI.Gtk.ModelView.TreeModelSort.TreeModelSort'.
    --
    -- A 'Graphics.UI.Gtk.ModelView.TreeViewColumn' can be sorted by the user
    -- though clicking into the column's header. The rows in the view will then be
    -- sorted by the sorting function set for that column. Specifically, a set of
    -- sorting functions must be set using the interface provided in this module.
    -- Each sorting function is associated with a 'SortColumnId', which is some
    -- positive number. A tree view column is then associated with the sorting
    -- function by passing the 'SortColumnId' to
    -- 'Graphics.UI.Gtk.ModelView.TreeViewColumn.treeViewColumnSetSortColumnId'.
    -- There exists one special 'SortColumnId', namely
    -- 'treeSortableDefaultSortColumnId' for which a default sorting function can
    -- be set. If no such function is set, the order of the rows is the order in
    -- which they are stored in the model.

    -- * Class Hierarchy
    --
    -- |
    -- @
    -- | 'GInterface'
    -- | +----TreeSortable
    -- @

    -- * Types
      TreeSortable,
      TreeSortableClass,
      castToTreeSortable, gTypeTreeSortable,
      toTreeSortable,
      SortColumnId,

    -- * Constants
      treeSortableDefaultSortColumnId,

    -- * Methods
      treeSortableGetSortColumnId,
      treeSortableSetSortColumnId,
      treeSortableSetSortFunc,
      treeSortableSetDefaultSortFunc,
      treeSortableHasDefaultSortFunc,
      treeSortableSortColumnChanged,

    -- * Signals
      sortColumnChanged
      ) where

    import Control.Monad (liftM)

    import System.Glib.FFI
    import Graphics.UI.Gtk.Types
    {-# LINE 95 "./Graphics/UI/Gtk/ModelView/TreeSortable.chs" #-}
    import Graphics.UI.Gtk.General.Enums (SortType(..))
    import Graphics.UI.Gtk.Signals
    {-# LINE 97 "./Graphics/UI/Gtk/ModelView/TreeSortable.chs" #-}
    import Graphics.UI.Gtk.ModelView.Types
    {-# LINE 98 "./Graphics/UI/Gtk/ModelView/TreeSortable.chs" #-}
    import Graphics.UI.Gtk.General.Structs (SortColumnId,
                                             treeSortableDefaultSortColumnId )

    {-# LINE 102 "./Graphics/UI/Gtk/ModelView/TreeSortable.chs" #-}

    --------------------
    -- Methods

    -- %hash c:a53 d:e0d2
    -- | Query the sort column id that is currently in use. The return value may
    -- be the special constant 'treeSortableDefaultSortColumnId' in which case
    -- the returned Boolean flag is @False@.
    --
    treeSortableGetSortColumnId :: TreeSortableClass self => self
     -> IO (SortType, Bool, SortColumnId) -- ^ @(type, columnSet, sortColumnId)@
                         -- returns @True@ in @columnSet@ if @sortColumnId@ is not
                         -- 'treeSortableDefaultSortColumnId'. The @type@ value indicates increasing
                         -- or decreasing ordering.
    treeSortableGetSortColumnId :: forall self.
    TreeSortableClass self =>
    self -> IO (SortType, Bool, SortColumnId)
    treeSortableGetSortColumnId self
    self =
      (Ptr CInt -> IO (SortType, Bool, SortColumnId))
    -> IO (SortType, Bool, SortColumnId)
    forall a b. Storable a => (Ptr a -> IO b) -> IO b
    alloca ((Ptr CInt -> IO (SortType, Bool, SortColumnId))
     -> IO (SortType, Bool, SortColumnId))
    -> (Ptr CInt -> IO (SortType, Bool, SortColumnId))
    -> IO (SortType, Bool, SortColumnId)
    forall a b. (a -> b) -> a -> b
    $ \Ptr CInt
    orderPtr -> (Ptr CInt -> IO (SortType, Bool, SortColumnId))
    -> IO (SortType, Bool, SortColumnId)
    forall a b. Storable a => (Ptr a -> IO b) -> IO b
    alloca ((Ptr CInt -> IO (SortType, Bool, SortColumnId))
     -> IO (SortType, Bool, SortColumnId))
    -> (Ptr CInt -> IO (SortType, Bool, SortColumnId))
    -> IO (SortType, Bool, SortColumnId)
    forall a b. (a -> b) -> a -> b
    $ \Ptr CInt
    sortColumnIdPtr -> do
      columnSet <- (CInt -> Bool) -> IO CInt -> IO Bool
    forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
    liftM CInt -> Bool
    forall a. (Eq a, Num a) => a -> Bool
    toBool (IO CInt -> IO Bool) -> IO CInt -> IO Bool
    forall a b. (a -> b) -> a -> b
    $ (\(TreeSortable ForeignPtr TreeSortable
    arg1) Ptr CInt
    arg2 Ptr CInt
    arg3 -> ForeignPtr TreeSortable -> (Ptr TreeSortable -> IO CInt) -> IO CInt
    forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
    withForeignPtr ForeignPtr TreeSortable
    arg1 ((Ptr TreeSortable -> IO CInt) -> IO CInt)
    -> (Ptr TreeSortable -> IO CInt) -> IO CInt
    forall a b. (a -> b) -> a -> b
    $ \Ptr TreeSortable
    argPtr1 ->Ptr TreeSortable -> Ptr CInt -> Ptr CInt -> IO CInt
    gtk_tree_sortable_get_sort_column_id Ptr TreeSortable
    argPtr1 Ptr CInt
    arg2 Ptr CInt
    arg3)
    {-# LINE 119 "./Graphics/UI/Gtk/ModelView/TreeSortable.chs" #-}
        (toTreeSortable self)
        Ptr CInt
    sortColumnIdPtr
        Ptr CInt
    orderPtr
      order <- peek orderPtr
      sortColumnId <- peek sortColumnIdPtr
      return (toEnum (fromIntegral order), columnSet, fromIntegral sortColumnId)

    -- %hash c:8951 d:33ab
    -- | Sets the current sort column to be @sortColumnId@. The @sortable@ will
    -- resort itself to reflect this change, after emitting a 'sortColumnChanged'
    -- signal. If @sortColumnId@ is 'treeSortableDefaultSortColumnId', then the
    -- default sort function will be used, if it is set. Note that this function
    -- is mainly used by the view and that the user program should simply set the
    -- 'SortColumnId' of the 'TreeViewColumn's.
    --
    treeSortableSetSortColumnId :: TreeSortableClass self => self
     -> SortColumnId -- ^ @sortColumnId@ - the sort column id to set
     -> SortType -- ^ @order@ - The sort order of the column
     -> IO ()
    treeSortableSetSortColumnId :: forall self.
    TreeSortableClass self =>
    self -> SortColumnId -> SortType -> IO ()
    treeSortableSetSortColumnId self
    self SortColumnId
    sortColumnId SortType
    order =
      (\(TreeSortable ForeignPtr TreeSortable
    arg1) CInt
    arg2 CInt
    arg3 -> ForeignPtr TreeSortable -> (Ptr TreeSortable -> IO ()) -> IO ()
    forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
    withForeignPtr ForeignPtr TreeSortable
    arg1 ((Ptr TreeSortable -> IO ()) -> IO ())
    -> (Ptr TreeSortable -> IO ()) -> IO ()
    forall a b. (a -> b) -> a -> b
    $ \Ptr TreeSortable
    argPtr1 ->Ptr TreeSortable -> CInt -> CInt -> IO ()
    gtk_tree_sortable_set_sort_column_id Ptr TreeSortable
    argPtr1 CInt
    arg2 CInt
    arg3)
    {-# LINE 140 "./Graphics/UI/Gtk/ModelView/TreeSortable.chs" #-}
        (toTreeSortable self)
        (SortColumnId -> CInt
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral SortColumnId
    sortColumnId)
        ((SortColumnId -> CInt
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (SortColumnId -> CInt)
    -> (SortType -> SortColumnId) -> SortType -> CInt
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . SortType -> SortColumnId
    forall a. Enum a => a -> SortColumnId
    fromEnum) SortType
    order)

    -- %hash c:9048 d:c49d
    -- | Sets the comparison function used when sorting to be @sortFunc@. If the
    -- current sort column id of @self@ is the same as @sortColumnId@, then the
    -- model will sort using this function.
    --
    treeSortableSetSortFunc :: TreeSortableClass self => self
     -> SortColumnId -- ^ @sortColumnId@ - the sort column id to set
                                    -- the function for
     -> (TreeIter -> TreeIter -> IO Ordering)
                                    -- ^ @sortFunc@ - The comparison function
     -> IO ()
    treeSortableSetSortFunc :: forall self.
    TreeSortableClass self =>
    self
    -> SortColumnId -> (TreeIter -> TreeIter -> IO Ordering) -> IO ()
    treeSortableSetSortFunc self
    self SortColumnId
    sortColumnId TreeIter -> TreeIter -> IO Ordering
    sortFunc = do
      fPtr <- (Ptr TreeModel
     -> Ptr TreeIter -> Ptr TreeIter -> Ptr () -> IO CInt)
    -> IO TreeIterCompareFunc
    mkTreeIterCompareFunc (\Ptr TreeModel
    _ Ptr TreeIter
    iter1Ptr Ptr TreeIter
    iter2Ptr Ptr ()
    _ -> do
        iter1 <- Ptr TreeIter -> IO TreeIter
    forall a. Storable a => Ptr a -> IO a
    peek Ptr TreeIter
    iter1Ptr
        iter2 <- peek iter2Ptr
        liftM orderToGInt $ sortFunc iter1 iter2)
      (\(TreeSortable ForeignPtr TreeSortable
    arg1) CInt
    arg2 TreeIterCompareFunc
    arg3 Ptr ()
    arg4 FunPtr (Ptr () -> IO ())
    arg5 -> ForeignPtr TreeSortable -> (Ptr TreeSortable -> IO ()) -> IO ()
    forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
    withForeignPtr ForeignPtr TreeSortable
    arg1 ((Ptr TreeSortable -> IO ()) -> IO ())
    -> (Ptr TreeSortable -> IO ()) -> IO ()
    forall a b. (a -> b) -> a -> b
    $ \Ptr TreeSortable
    argPtr1 ->Ptr TreeSortable
    -> CInt
    -> TreeIterCompareFunc
    -> Ptr ()
    -> FunPtr (Ptr () -> IO ())
    -> IO ()
    gtk_tree_sortable_set_sort_func Ptr TreeSortable
    argPtr1 CInt
    arg2 TreeIterCompareFunc
    arg3 Ptr ()
    arg4 FunPtr (Ptr () -> IO ())
    arg5)
    {-# LINE 162 "./Graphics/UI/Gtk/ModelView/TreeSortable.chs" #-}
        (toTreeSortable self)
        (fromIntegral sortColumnId)
        fPtr (castFunPtrToPtr fPtr) destroyFunPtr

    orderToGInt :: Ordering -> (CInt)
    {-# LINE 167 "./Graphics/UI/Gtk/ModelView/TreeSortable.chs" #-}
    orderToGInt LT = -1
    orderToGInt EQ = 0
    orderToGInt GT = 1

    type TreeIterCompareFunc = FunPtr (((Ptr TreeModel) -> ((Ptr TreeIter) -> ((Ptr TreeIter) -> ((Ptr ()) -> (IO CInt))))))
    {-# LINE 172 "./Graphics/UI/Gtk/ModelView/TreeSortable.chs" #-}

    foreign import ccall "wrapper" mkTreeIterCompareFunc ::
      (Ptr TreeModel -> Ptr TreeIter -> Ptr TreeIter -> Ptr () -> IO (CInt)) ->
      IO TreeIterCompareFunc

    -- %hash c:221e d:7c9
    -- | Sets the default comparison function used when sorting to be @sortFunc@.
    -- If the current sort column id of @self@ is
    -- 'treeSortableDefaultSortColumnId' then the model will sort using
    -- this function.
    --
    -- | If @sortFunc@ is 'Nothing', then there will be no default comparison function.
    -- This means that once the
    -- model has been sorted, it can't go back to the default state. In this case, when the current sort
    -- column id of sortable is 'TreeSortableDefaultSortColumnId', the model will be unsorted.
    treeSortableSetDefaultSortFunc :: TreeSortableClass self => self
     -> Maybe (TreeIter -> TreeIter -> IO Ordering)
                                    -- ^ @sortFunc@ - The comparison function
                                    -- or 'Nothing' to use default comparison function.
     -> IO ()
    treeSortableSetDefaultSortFunc :: forall self.
    TreeSortableClass self =>
    self -> Maybe (TreeIter -> TreeIter -> IO Ordering) -> IO ()
    treeSortableSetDefaultSortFunc self
    self Maybe (TreeIter -> TreeIter -> IO Ordering)
    Nothing = do
      (\(TreeSortable ForeignPtr TreeSortable
    arg1) TreeIterCompareFunc
    arg2 Ptr ()
    arg3 FunPtr (Ptr () -> IO ())
    arg4 -> ForeignPtr TreeSortable -> (Ptr TreeSortable -> IO ()) -> IO ()
    forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
    withForeignPtr ForeignPtr TreeSortable
    arg1 ((Ptr TreeSortable -> IO ()) -> IO ())
    -> (Ptr TreeSortable -> IO ()) -> IO ()
    forall a b. (a -> b) -> a -> b
    $ \Ptr TreeSortable
    argPtr1 ->Ptr TreeSortable
    -> TreeIterCompareFunc
    -> Ptr ()
    -> FunPtr (Ptr () -> IO ())
    -> IO ()
    gtk_tree_sortable_set_default_sort_func Ptr TreeSortable
    argPtr1 TreeIterCompareFunc
    arg2 Ptr ()
    arg3 FunPtr (Ptr () -> IO ())
    arg4)
    {-# LINE 194 "./Graphics/UI/Gtk/ModelView/TreeSortable.chs" #-}
        (toTreeSortable self)
        TreeIterCompareFunc
    forall a. FunPtr a
    nullFunPtr Ptr ()
    forall a. Ptr a
    nullPtr FunPtr (Ptr () -> IO ())
    forall a. FunPtr a
    nullFunPtr
    treeSortableSetDefaultSortFunc self
    self (Just TreeIter -> TreeIter -> IO Ordering
    sortFunc) = do
      fPtr <- (Ptr TreeModel
     -> Ptr TreeIter -> Ptr TreeIter -> Ptr () -> IO CInt)
    -> IO TreeIterCompareFunc
    mkTreeIterCompareFunc (\Ptr TreeModel
    _ Ptr TreeIter
    iter1Ptr Ptr TreeIter
    iter2Ptr Ptr ()
    _ -> do
        iter1 <- Ptr TreeIter -> IO TreeIter
    forall a. Storable a => Ptr a -> IO a
    peek Ptr TreeIter
    iter1Ptr
        iter2 <- peek iter2Ptr
        liftM orderToGInt $ sortFunc iter1 iter2)
      (\(TreeSortable ForeignPtr TreeSortable
    arg1) TreeIterCompareFunc
    arg2 Ptr ()
    arg3 FunPtr (Ptr () -> IO ())
    arg4 -> ForeignPtr TreeSortable -> (Ptr TreeSortable -> IO ()) -> IO ()
    forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
    withForeignPtr ForeignPtr TreeSortable
    arg1 ((Ptr TreeSortable -> IO ()) -> IO ())
    -> (Ptr TreeSortable -> IO ()) -> IO ()
    forall a b. (a -> b) -> a -> b
    $ \Ptr TreeSortable
    argPtr1 ->Ptr TreeSortable
    -> TreeIterCompareFunc
    -> Ptr ()
    -> FunPtr (Ptr () -> IO ())
    -> IO ()
    gtk_tree_sortable_set_default_sort_func Ptr TreeSortable
    argPtr1 TreeIterCompareFunc
    arg2 Ptr ()
    arg3 FunPtr (Ptr () -> IO ())
    arg4)
    {-# LINE 202 "./Graphics/UI/Gtk/ModelView/TreeSortable.chs" #-}
        (toTreeSortable self)
        fPtr (castFunPtrToPtr fPtr) destroyFunPtr

    -- %hash c:78ec d:d949
    -- | Emits a 'sortColumnChanged' signal on the model.
    --
    treeSortableSortColumnChanged :: TreeSortableClass self => self -> IO ()
    treeSortableSortColumnChanged :: forall self. TreeSortableClass self => self -> IO ()
    treeSortableSortColumnChanged self
    self =
      (\(TreeSortable ForeignPtr TreeSortable
    arg1) -> ForeignPtr TreeSortable -> (Ptr TreeSortable -> IO ()) -> IO ()
    forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
    withForeignPtr ForeignPtr TreeSortable
    arg1 ((Ptr TreeSortable -> IO ()) -> IO ())
    -> (Ptr TreeSortable -> IO ()) -> IO ()
    forall a b. (a -> b) -> a -> b
    $ \Ptr TreeSortable
    argPtr1 ->Ptr TreeSortable -> IO ()
    gtk_tree_sortable_sort_column_changed Ptr TreeSortable
    argPtr1)
    {-# LINE 211 "./Graphics/UI/Gtk/ModelView/TreeSortable.chs" #-}
        (toTreeSortable self)

    -- %hash c:4a10 d:f107
    -- | Returns @True@ if the model has a default sort function. This is used
    -- primarily by 'Graphics.UI.Gtk.ModelView.TreeViewColumn's in order to
    -- determine if a model has a default ordering or if the entries are
    -- retrieved in the sequence in which they are stored in the model.
    --
    treeSortableHasDefaultSortFunc :: TreeSortableClass self => self
     -> IO Bool -- ^ returns @True@, if the model has a default sort function
    treeSortableHasDefaultSortFunc :: forall self. TreeSortableClass self => self -> IO Bool
    treeSortableHasDefaultSortFunc self
    self =
      (CInt -> Bool) -> IO CInt -> IO Bool
    forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
    liftM CInt -> Bool
    forall a. (Eq a, Num a) => a -> Bool
    toBool (IO CInt -> IO Bool) -> IO CInt -> IO Bool
    forall a b. (a -> b) -> a -> b
    $
      (\(TreeSortable ForeignPtr TreeSortable
    arg1) -> ForeignPtr TreeSortable -> (Ptr TreeSortable -> IO CInt) -> IO CInt
    forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
    withForeignPtr ForeignPtr TreeSortable
    arg1 ((Ptr TreeSortable -> IO CInt) -> IO CInt)
    -> (Ptr TreeSortable -> IO CInt) -> IO CInt
    forall a b. (a -> b) -> a -> b
    $ \Ptr TreeSortable
    argPtr1 ->Ptr TreeSortable -> IO CInt
    gtk_tree_sortable_has_default_sort_func Ptr TreeSortable
    argPtr1)
    {-# LINE 224 "./Graphics/UI/Gtk/ModelView/TreeSortable.chs" #-}
        (toTreeSortable self)

    --------------------
    -- Signals

    -- %hash c:c461 d:af3f
    -- |
    --
    sortColumnChanged :: TreeSortableClass self => Signal self (IO ())
    sortColumnChanged :: forall self. TreeSortableClass self => Signal self (IO ())
    sortColumnChanged = (Bool -> self -> IO () -> IO (ConnectId self))
    -> Signal self (IO ())
    forall object handler.
    (Bool -> object -> handler -> IO (ConnectId object))
    -> Signal object handler
    Signal (SignalName -> Bool -> self -> IO () -> IO (ConnectId self)
    forall obj.
    GObjectClass obj =>
    SignalName -> Bool -> obj -> IO () -> IO (ConnectId obj)
    connect_NONE__NONE SignalName
    "sort-column-changed")

    foreign import ccall unsafe "gtk_tree_sortable_get_sort_column_id"
      gtk_tree_sortable_get_sort_column_id :: ((Ptr TreeSortable) -> ((Ptr CInt) -> ((Ptr CInt) -> (IO CInt))))

    foreign import ccall safe "gtk_tree_sortable_set_sort_column_id"
      gtk_tree_sortable_set_sort_column_id :: ((Ptr TreeSortable) -> (CInt -> (CInt -> (IO ()))))

    foreign import ccall safe "gtk_tree_sortable_set_sort_func"
      gtk_tree_sortable_set_sort_func :: ((Ptr TreeSortable) -> (CInt -> ((FunPtr ((Ptr TreeModel) -> ((Ptr TreeIter) -> ((Ptr TreeIter) -> ((Ptr ()) -> (IO CInt)))))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO ()))))))

    foreign import ccall safe "gtk_tree_sortable_set_default_sort_func"
      gtk_tree_sortable_set_default_sort_func :: ((Ptr TreeSortable) -> ((FunPtr ((Ptr TreeModel) -> ((Ptr TreeIter) -> ((Ptr TreeIter) -> ((Ptr ()) -> (IO CInt)))))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO ())))))

    foreign import ccall safe "gtk_tree_sortable_sort_column_changed"
      gtk_tree_sortable_sort_column_changed :: ((Ptr TreeSortable) -> (IO ()))

    foreign import ccall safe "gtk_tree_sortable_has_default_sort_func"
      gtk_tree_sortable_has_default_sort_func :: ((Ptr TreeSortable) -> (IO CInt))