module Graphics.UI.Gtk.Generics.ListStore where
	
import Graphics.UI.Gtk
import Control.Monad( forM_ )

{-
	Used for repopulating a ListStore with new elements.
	
	NB: All previous elements will be lost.
-}

listStoreRepopulate :: ListStore a -> [a] -> IO ( )
listStoreRepopulate store elements = do
        listStoreClear store
        forM_ elements $ \row -> listStoreAppend store row

{-
	Convenience function for getting a single selection from a
	ListStore using a TreePath. Gets the element from the ListStore
	corresponding to the first index in the TreePath.
-}

listStoreGetValueAtPath :: TreePath -> ListStore a -> IO a
listStoreGetValueAtPath (x:_) store = listStoreGetValue store x

{-
	Convenience function for prepending a list to an existing
	ListStore.
-}

listStorePrependList :: ListStore a -> [a] -> IO ( )
listStorePrependList store list = ( listStoreToList store >>= (\l ->
	listStoreRepopulate store ( list ++ l ) ) )

{-
	Convenience function for appending a list to an existing 
	ListStore.
-}

listStoreAppendList :: ListStore a -> [a] -> IO ( )
listStoreAppendList store list = ( listStoreToList store >>= (\l -> 
	listStoreRepopulate store ( l ++ list ) ) )