Search code examples
gtkgtkmm

Gtk (mm) limit width of combobox


Because I use Comboboxes that may contain text entries of very long size, which leads to the combobox increasing its width far beyond reasonable size, I am trying to give a maximum width to the combobox.

If I am doing this like this:

class MyCombo : public Gtk::ComboBox {
    private:
        CellRendererText render;
    public:
        MyCombo() {
            render.property_width_chars() = 10;
            render.property_ellipsize() = Pango::ELLIPSIZE_END;
            pack_start(render, true);
        }
};

The result will be an empty cell of the desired width, which seems logical since I did not specify which column to show. But how can I do this with that attempt? Using pack_start will just bypass the renderer...

Another approach is this one:

class MyCombo : public Gtk::ComboBox {
    private:
        CellRendererText render;
    public:
        MyCombo() {
            pack_start(render, true);
            set_cell_data_func(render, sigc::mem_fun(*this, &MyCombo::render_iter));
        }

        void render_iter(const TreeModel::const_iterator& iter) {
            Glib::ustring data = get_string_from_iter(iter);
            int desired_width_chars = 10; //for example
            render.property_text() = ellipsize_string(data, desired_width_chars);
        }
};

Using that approach, it works, but the text in the popup (what opens up when u click the combobox) is also shortened which is not what I want (obviously the user should be able to read the whole string and I dont care about the popup widht.)

Can you please help me with this? I would be happy for any advice/alternative solutions.

Regards tagelicht


Solution

  • NOTE: set_wrap_width is a function that wraps the total number of entries in the combo box over a number of columns specified; it does not answer the question.

    Using set_wrap_width(1) | Using set_wrap_width(5)


    Following Noup's answer as a guide I managed to get the below code; which directly answers the question and its requirements (C++/Gtkmm).

        // Get the first cell renderer of the ComboBox. 
        auto v_cellRenderer = (Gtk::CellRendererText*)v_comboBox.get_first_cell();
    
        // Probably obsolete; Sets character width to 1.
        v_cellRenderer->property_width_chars() = 1;
    
        // Sets the ellipses ("...") to be at the end, where text overflows.
        // See Pango::ELLIPSIZE enum for other values.
        v_cellRenderer->property_ellipsize() = Pango::ELLIPSIZE_END;
    
        // Sets the size of the box, change this to suit your needs. 
        // -1 sets it to automatic scaling: (width, height).
        v_cellRenderer->set_fixed_size(200, -1);
    

    Result (image): Result of code

    BE AWARE: Depending on where you perform the above code; either all the cells will be the same size, or just the box itself (intended). From experimenting, I've found:

    • In the parent object constructor: All cell sizes are the same.
    • In a separate function: Only the first cell (the box) is affected.

    I'd recommend you put the code in a function that's connected to the comboBox's changed signal, such as:

        v_comboBox.signal_changed().connect(sigc::mem_fun(*this, &YourClass::comboBox_changedFunction));