2

I don't seem to be able to write working code that creates and populates a TreeView using the gtk 3 bindings of gi-gtk (specifically version 3.0.32). All examples I could find online are for different (and older) bindings that do not apply to gi-gtk. The documentation itself is particularly useless in this case.

I have tried replicating a simplified version (one row, one column) of this example, as follows

import qualified GI.Gtk as Gtk
import Data.GI.Base.GType (gtypeString)
import Data.GI.Base.GValue (IsGValue(..))

main :: IO ()
main = do
  Gtk.init Nothing
  w <- Gtk.windowNew Gtk.WindowTypeToplevel
  treeView <- Gtk.treeViewNew
  store <- Gtk.listStoreNew [gtypeString]
  Gtk.treeViewSetModel treeView (Just store)

  iter <- Gtk.listStoreAppend store
  str <- toGValue (Just "foo")
  Gtk.listStoreSetValue store iter 0 str

  Gtk.treeViewColumnNew >>= Gtk.treeViewAppendColumn treeView

  Gtk.containerAdd w treeView
  #showAll w

  Gtk.main

The result is obviously wrong, and looks like a partially drawn treeview, no text shown. I have no idea what I'm doing wrong, so I would appreciate if somebody could show how to fix the code or point me to a working example.

fsestini
  • 121
  • 6
  • 2
    I don't know how it's done in haskell, but if it was C, I'd say that your `TreeViewColumn` 1) doesn't have a GtkCellRenderer. 2) doesn't know, where to get data from. Take a look at [this](https://developer.gnome.org/gtk3/stable/GtkTreeViewColumn.html#gtk-tree-view-column-new-with-attributes) method. maybe it'll help you. – Alexander Dmitriev Sep 16 '19 at 14:20

1 Answers1

0

Here's my working example :

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Main where

import Data.GI.Base
import Control.Monad 
import Data.Text (Text, pack)
import qualified GI.Gtk as Gtk
import qualified GI.Pango as Pango

demoList::[Text]
demoList = ["1 James","2 John","3 Robert","4 Michael","5 William","6 David","7 Richard","8 Joseph","9 Thomas","10 Charles"]
 
setValuesToListStore :: Gtk.ListStore ->  [Text] -> Int -> IO Gtk.TreeIter
setValuesToListStore lsts artx counter = do
             let stT::Text = artx !! counter
             gv <- toGValue (Just stT)
             if (counter == (length artx)-1) then Gtk.listStoreInsertWithValuesv lsts (-1) [0] [gv]              
             else do               
               m <- (Gtk.listStoreInsertWithValuesv lsts (-1) [0] [gv])
               setValuesToListStore lsts artx (counter + 1)

main :: IO ()
main = do
    void $ Gtk.init Nothing

    window <- new Gtk.Window []
    on window #destroy Gtk.mainQuit

    column <- new Gtk.TreeViewColumn [ #title := "Column name" ]
    render <- new Gtk.CellRendererText [ #ellipsize :=  Pango.EllipsizeModeEnd
                                       , #editable := False ]
    #packStart column render True
    #addAttribute column render "text" 0

    mlistStore <- Gtk.listStoreNew [gtypeString]
   
    setValuesToListStore mlistStore demoList 0 
    
    view <- new Gtk.TreeView[#enableTreeLines := True, #headersVisible := True]     
    lk <- (Gtk.treeViewSetModel view (Just mlistStore))
    
    #appendColumn view column

    #expandAll view
    #add window view
    #showAll window

    Gtk.main