Question

Sorry for the dump(?) question.How do I add a GTK Combobox to a Toolbar? I have googled it, but did not find an answer. It compiles without error, but when I run the application the following message is printed to the console:

Gtk-CRITICAL **: gtk_toolbar_insert: assertion 'GTK_IS_TOOL_ITEM (item)' failed

Here is an example of the Toolbar+Combobox:

using Gtk;

public class Example : Object  {
    private Window _win;
    private Toolbar _tb;

    public Example() {
        _win = new Window();
        _win.title = "Test";
        _win.window_position = WindowPosition.CENTER;
        _win.set_default_size(800, 600);
        _win.destroy.connect(Gtk.main_quit);

        _tb = new Toolbar();
        var img = new Image.from_icon_name("document-new", Gtk.IconSize.SMALL_TOOLBAR);
        var btn = new ToolButton(img, "New");
        _tb.add(btn);

        add_zoombox();

        var vbox = new Box(Orientation.VERTICAL, 0);
        vbox.pack_start(_tb, false, true, 0);

        _win.add(vbox);
    }

    private void add_zoombox() {
        ListStore list = new ListStore(1, typeof (int));
        for(int i = 25; i<= 400; i*=2) {
            TreeIter iter;
            list.append(out iter);
            list.set(iter, 0, i);
        }

        ComboBox cb = new ComboBox.with_model(list);
        CellRendererText r = new CellRendererText();
        cb.pack_start(r, false);
        cb.set_active(0);
        _tb.add(cb);
        cb.show();
    }

    public void show_window() {
        _win.show_all();
    }


}

public static int main (string[] args) {
    Gtk.init(ref args);
    Example ex = new Example();
    ex.show_window();
    Gtk.main();
    return 0;
}
Was it helpful?

Solution

Solved the problem on my own. After reading the doc again, I found out that a Toolbar can only contain only ToolButtons, ToggleToolButtons and RadioToolButtons. To add a Combobox or any other item to the Toolbar it must be added to a ToolItem first. Here is the code that changed:

ToolItem container = new ToolItem();
_tb.add(container);
container.add(cb);
cb.show();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top