1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
{-# LANGUAGE CPP #-}
-- -*-haskell-*-
-- GIMP Toolkit (GTK) TreeModelSort
--
-- Author : Duncan Coutts
--
-- Created: 4 August 2004
--
-- Copyright (C) 2004-2005 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)
--
-- A 'TreeModel' which makes an underlying tree model sortable
--
module Graphics.UI.Gtk.ModelView.TreeModelSort (
-- * Detail
--
-- | The 'TreeModelSort' is a model which implements the 'TreeSortable'
-- interface. It does not hold any data itself, but rather is created with a
-- child model and proxies its data. It has identical rows to its
-- child model, and the changes in the child are propagated. The primary
-- purpose of this model is to provide a way to sort a model without
-- modifying it.
-- * Class Hierarchy
-- |
-- @
-- | 'GObject'
-- | +----TreeModelSort
-- @
-- * Types
TreeModelSort,
TreeModelSortClass,
castToTreeModelSort, gTypeTreeModelSort,
toTreeModelSort,
TypedTreeModelSort,
-- * Constructors
treeModelSortNewWithModel,
-- * Methods
treeModelSortGetModel,
treeModelSortConvertChildPathToPath,
treeModelSortConvertPathToChildPath,
treeModelSortConvertChildIterToIter,
treeModelSortConvertIterToChildIter,
treeModelSortResetDefaultSortFunc,
treeModelSortClearCache,
#if GTK_CHECK_VERSION(2,2,0)
treeModelSortIterIsValid,
#endif
) where
import Control.Monad (liftM)
import System.Glib.FFI
import System.Glib.GObject (constructNewGObject,
makeNewGObject)
{#import Graphics.UI.Gtk.Types#}
{#import Graphics.UI.Gtk.ModelView.TreeModel#}
{#import Graphics.UI.Gtk.ModelView.Types#}
{# context lib="gtk" prefix="gtk" #}
instance TreeModelClass (TypedTreeModelSort a)
instance TreeModelSortClass (TypedTreeModelSort a)
instance GObjectClass (TypedTreeModelSort a) where
toGObject (TypedTreeModelSort tm) = GObject (castForeignPtr tm)
unsafeCastGObject = TypedTreeModelSort . castForeignPtr . unGObject
instance TreeSortableClass TreeModelSort
instance TreeSortableClass (TypedTreeModelSort row)
--------------------
-- Constructors
-- | Creates a new 'TreeModelSort', that will be a sorted view of the given
-- model.
--
treeModelSortNewWithModel :: (TreeModelClass (childModel row),
TypedTreeModelClass childModel) =>
childModel row -> IO (TypedTreeModelSort row)
treeModelSortNewWithModel childModel = liftM unsafeTreeModelSortToGeneric $
constructNewGObject mkTreeModelSort $
liftM (castPtr :: Ptr TreeModel -> Ptr TreeModelSort) $
{# call tree_model_sort_new_with_model #}
(toTreeModel childModel)
--------------------
-- Methods
-- | Returns the underlying model the 'TreeModelSort' is sorting.
--
treeModelSortGetModel :: TreeModelSortClass self => self -> IO TreeModel
treeModelSortGetModel self =
makeNewGObject mkTreeModel $
{# call tree_model_sort_get_model #}
(toTreeModelSort self)
-- | Converts the given path to a path relative to the given sorted model.
--
-- * The given path points to a row in the child model. The returned path will
-- point to the same row in the sorted model.
--
treeModelSortConvertChildPathToPath :: TreeModelSortClass self => self
-> TreePath
-> IO TreePath
treeModelSortConvertChildPathToPath self [] = return []
treeModelSortConvertChildPathToPath self childPath =
withTreePath childPath $ \childPath ->
{# call tree_model_sort_convert_child_path_to_path #}
(toTreeModelSort self)
childPath
>>= fromTreePath
-- | Converts path in the sorted model to a path on the unsorted model on which
-- the given 'TreeModelSort' is based. That is, the given path points to a
-- location in the given 'TreeModelSort'. The returned path will point to the
-- same location in the underlying unsorted model.
--
treeModelSortConvertPathToChildPath :: TreeModelSortClass self => self
-> TreePath
-> IO TreePath
treeModelSortConvertPathToChildPath self [] = return []
treeModelSortConvertPathToChildPath self sortedPath =
withTreePath sortedPath $ \sortedPath ->
{# call tree_model_sort_convert_path_to_child_path #}
(toTreeModelSort self)
sortedPath
>>= fromTreePath
-- | Return an iterator in the sorted model that points to the row pointed to
-- by the given iter from the unsorted model.
--
treeModelSortConvertChildIterToIter :: TreeModelSortClass self => self
-> TreeIter
-> IO TreeIter
treeModelSortConvertChildIterToIter self childIter =
with childIter $ \childIterPtr ->
alloca $ \sortIterPtr -> do
{# call tree_model_sort_convert_child_iter_to_iter #}
(toTreeModelSort self)
sortIterPtr
childIterPtr
peek sortIterPtr
-- | Return an iterator in the unsorted model that points to the row pointed to
-- by the given iter from the sorted model.
--
treeModelSortConvertIterToChildIter :: TreeModelSortClass self => self
-> TreeIter
-> IO TreeIter
treeModelSortConvertIterToChildIter self sortedIter =
with sortedIter $ \sortedIterPtr ->
alloca $ \childIterPtr -> do
{# call tree_model_sort_convert_iter_to_child_iter #}
(toTreeModelSort self)
childIterPtr
sortedIterPtr
peek childIterPtr
-- | This resets the default sort function. As a consequence, the order of
-- this model will be the same order as that of the child model.
--
treeModelSortResetDefaultSortFunc :: TreeModelSortClass self => self -> IO ()
treeModelSortResetDefaultSortFunc self =
{# call tree_model_sort_reset_default_sort_func #}
(toTreeModelSort self)
-- | Clear the cache of unref'd iterators.
--
-- * This function should almost never be called. It clears the
-- 'TreeModelSort' of any cached iterators that haven't been reffed with
-- 'treeModelRefNode'. This might be useful if the child model being sorted is
-- static (and doesn't change often) and there has been a lot of unreffed
-- access to nodes. As a side effect of this function, all unreffed iters will
-- be invalid.
--
treeModelSortClearCache :: TreeModelSortClass self => self -> IO ()
treeModelSortClearCache self =
{# call gtk_tree_model_sort_clear_cache #}
(toTreeModelSort self)
#if GTK_CHECK_VERSION(2,2,0)
-- | Checks if the given iter is a valid iter for this 'TreeModelSort'.
--
-- * WARNING: This function is slow. Only use it for debugging and\/or testing
-- purposes.
--
-- * Available since Gtk+ version 2.2
--
treeModelSortIterIsValid :: TreeModelSortClass self => self
-> TreeIter -- ^ @iter@ - A 'TreeIter'.
-> IO Bool -- ^ returns @True@ if the iter is valid, @False@ if the iter is
-- invalid.
treeModelSortIterIsValid self iter =
liftM toBool $
with iter $ \iterPtr ->
{# call gtk_tree_model_sort_iter_is_valid #}
(toTreeModelSort self)
iterPtr
#endif
|