Wrapping text in the GTK3 tree

I'm having trouble getting TreeView in GTK3 to properly wrap text.

I installed it like this:

    Gtk::TreeViewColumn* pColumn = mTreeView.get_column(2);
    static_cast<Gtk::CellRendererText *>(pColumn->get_first_cell())
        ->property_wrap_mode().set_value(Pango::WRAP_WORD_CHAR);
    static_cast<Gtk::CellRendererText *>(pColumn->get_first_cell())
        ->property_wrap_width().set_value(200);

This works, the text is wrapped, but when I resize the window and make it larger, there is a lot of ugly white space above and below the cell with long text. GTK seems to reserve height for a cell based on packing width. Which doesn't make any sense to me.

I tried to get around the setup needed in signal_check_resize, with calculating the required width as follows:

        Gtk::TreeViewColumn* pColumn = mTreeView.get_column(2);
        auto width = this->get_allocated_width()
            - mTreeView.get_column(0)->get_width()
            - mTreeView.get_column(1)->get_width();
        static_cast<Gtk::CellRendererText *>(pColumn->get_first_cell())
            ->property_wrap_width().set_value(width-100);
        this->forceRecreateModel = true; //Needed to work

But that allows me to make the window bigger. It cannot be compressed after resizing.

The question is, how is this done correctly?

I am using gtk3.20.3-1 and gtkmm3.20.1-1 on Arch linux.

EDIT: fixed typo in title ...

+4
1

, .

( ) AUTOSIZE, .

//Last Column setup
{
    mTreeView.append_column("Translation", mColumns.mEnglish);
    Gtk::TreeViewColumn* pColumn = mTreeView.get_column(2);
    pColumn->set_sizing(Gtk::TreeViewColumnSizing::TREE_VIEW_COLUMN_AUTOSIZE);
    static_cast<Gtk::CellRendererText *>(pColumn->get_first_cell())
        ->property_wrap_mode().set_value(Pango::WRAP_WORD_CHAR);
}

. , wrap_width ( , ).

.

this->signal_check_resize().connect([this]()
{
    //calculate remaining size
    Gtk::TreeViewColumn* pColumn = mTreeView.get_column(2);
    auto width = this->get_allocated_width()
        - mTreeView.get_column(0)->get_width()
        - mTreeView.get_column(1)->get_width()-30;

    //minimum reasonable size for column
    if(width < 150)
        width = 150;

    static_cast<Gtk::CellRendererText *>(pColumn->get_first_cell())
        ->property_wrap_width().set_value(width);

    //debounce
    static auto oldsize = 0;
    {
        oldsize = width;

        //trigger redraw of mTreeView (by clearing and refilling Model,
        //it is done in 100ms pulse)
        this->mRedrawNeeded = true;
    }
});

, , , mTreeView, Gtk:: ScrolledWindow. , , .:)

//in class is: Gtk::ScrolledWindow mScrollForResults;

//scrolling area
mGrid.attach(mScrollForResults, 0,2,10,1);
mScrollForResults.set_hexpand();
mScrollForResults.set_policy(Gtk::PolicyType::POLICY_AUTOMATIC,
                             Gtk::PolicyType::POLICY_ALWAYS);
mScrollForResults.set_margin_top(10);
mScrollForResults.set_min_content_width(400);
mScrollForResults.set_min_content_height(200);
mScrollForResults.add(mTreeView);

//results treeView
mRefListStore = Gtk::ListStore::create(mColumns);
mTreeView.set_model(mRefListStore);
mTreeView.set_hexpand();
mTreeView.set_vexpand();
+4

All Articles