Search code examples
haskellgtkgladegtktreeview

How to populate a new entry in a GtkTreeview (using Haskell & Glade)


this is how I currently initialize the Tree view from the glade file (Removed the uninteresting parts)

tree_view <- xmlGetWidget xml castToTreeView "tree_view"

To make it easy, how could I add a new entry to the tree_view using the text from an already filled text field?

text <- get text_field entryText

May someone help me?

Thanks


Solution

  • From your description, I assume that you have a tree view, but haven't set it up properly. A GTK tree view is worth nothing if you don't connect it to several other widgets.

    • A tree model, which holds the data to be displayed in the tree view. The tree model can either be a list store, which stores data as a list, or a tree store, which stores the data as a tree, with nodes containing children. In your case, you'd probably need to create a ListStore:

      store <- listStoreNew String
      

      Tree models can store data of any type, including complex custom data types.

    • A tree view column for each piece of data to be shown. In this case, we just need one column because we want to display one string per row:

      column <- treeViewColumnNew
      
    • The tree view column must know what data to show and how to display it (as text, as a tick box, as a picture...) So we need a cell renderer. The cell renderer will get a piece of data from each row in the tree model and show it in the given column. For displaying text, a CellRendererText is needed. There are other types of cell renderers for showing data in other forms (for instance CellRendererToggle for True/False values).

      -- Create a cell renderer for displaying text
      cell <- cellRendererTextNew
      -- Add the cell renderer to the column. A column may display one or more
      -- pieces of data; just pack the cell renderers into the column
      cellLayoutPackStart column cell True
      -- Let the cell renderer know where it has to get the data from
      -- (in this case, the only element of each row). Note that we
      -- want to display some text
      cellLayoutSetAttributes column cell store $ \row -> [ cellText := row ]
      

    Next, insert the column into the tree view. If you don't do this, the column won't be shown!

        -- Insert the column into the tree view
        _ <- treeViewAppendColumn tree_view column
        -- Optional: Give a header/title to the column
        treeViewColumnSetTitle column "My data"
    

    And last but not least: connect the tree view to the tree model:

        treeViewSetModel tree_view store
    

    Now you should see an empty column in your tree view. See the documentation of the gtk package at http://hackage.haskell.org/package/gtk to learn how to insert, remove and get data to/from a tree model (listStoreAppend, listStoreRemove, listStoreGetValue etc.)

    Note that every tree model has a type and can only work with data of that type (but you can use any data type, even your own, so you can indeed store and show rows of complex data provided you set up every tree view column correctly). Otherwise you will get a type error when compiling the program.