Column grow preventing



Hello guys,

Crazy to find out how to force the message lines of inside the "Problem Attached Code" wrapping when data in a treeview model becomes longer than the original/previous column size.
Can prevent the column grows each time of model updates ???

Any help/idea ??

*** Problem Attached Code ***

#include <gtkmm.h>
#include <iostream>
#include <vector>

class MyWindow
:
    public Gtk::Window
{
public:

    MyWindow();
    virtual ~MyWindow();
   
private:

    void fill_data();
   
    void on_button_next_clicked();
   
    std::vector<Glib::ustring> messages;
    gint current_idx;
   
    // * * * *

    Gtk::Grid grid;
   
    Gtk::ScrolledWindow scrolled;
    Gtk::TreeView treeview;
   
    Gtk::ButtonBox buttonbox;
    Gtk::Button bt_close;
    Gtk::Button bt_next;
   
    class ModelColumns
    :
        public Gtk::TreeModel::ColumnRecord
    {
    public:
   
        ModelColumns()
        {
            add (id);
            add (message);
        }
       
        Gtk::TreeModelColumn<gint> id;
        Gtk::TreeModelColumn<Glib::ustring> message;
    }
        model_columns;
       
    Gtk::TreeRow row;
       
    Glib::RefPtr<Gtk::ListStore> refModelColumns;
};

MyWindow::MyWindow()
:
    current_idx (0),
   
    messages ({
                "Little message",
                "This is a bit longer message",
                "You have just written a very long, long, long... message"
            }),

    bt_close ("Close"),
    bt_next ("Next"),

    refModelColumns (Gtk::ListStore::create (model_columns))
{
    set_title ("Col Width Test");
    set_border_width (5);
   
    treeview.set_model (refModelColumns);
    treeview.set_hexpand();
    treeview.set_vexpand();
   
    treeview.append_column ("Id", model_columns.id);
    treeview.append_column ("Message", model_columns.message);
   
    scrolled.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
    scrolled.add (treeview);
   
    buttonbox.set_layout (Gtk::BUTTONBOX_END);
    buttonbox.set_spacing (5);
    buttonbox.set_hexpand();
   
    buttonbox.pack_start (bt_close);
    buttonbox.pack_start (bt_next);
   
    bt_close.signal_clicked().connect (
        sigc::mem_fun (*this, &MyWindow::hide));
       
    bt_next.signal_clicked().connect (
        sigc::mem_fun (*this, &MyWindow::on_button_next_clicked));
   
    grid.set_row_spacing (5);
   
    grid.attach (scrolled, 0, 0, 1, 1);
    grid.attach (buttonbox, 0, 1, 1, 1);
   
    add (grid);
   
    fill_data();
   
    show_all_children();
}

void
MyWindow::fill_data()
{
    if (!refModelColumns->children().empty())
        refModelColumns->clear();
       
    if ((current_idx < 0) or
            (current_idx >= messages.size()))
        current_idx = 0;
       
    if (!messages.empty())
    {
        row = *(refModelColumns->append());
       
        row[model_columns.id] = current_idx;
        row[model_columns.message] = messages[current_idx];
    }
}

void
MyWindow::on_button_next_clicked()
{
    current_idx++;
   
    if ((current_idx < 0) or
            (current_idx >= messages.size()))
        current_idx = 0;
       
    fill_data();
}

MyWindow::~MyWindow()
{
}

int main (int argc, char *argv[])
{
    Glib::RefPtr<Gtk::Application> app =
        Gtk::Application::create (argc, argv, "org.gtkmm.example");
       
    MyWindow my_window;
   
    return app->run (my_window);
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]