Web lists-archives.com

Eliminate the warning Allocating size to GtkWindow without calling gtk_widget_get_preferred_size/height




I am seeing this warning when expanding a TreeStore when the expanded height exceeds the size of the window.  I'm attaching a minimal but complete program that exhibits this.

If there's something I'm missing, I'd appreciate anu pointers, but it seems that this is a bug of sorts.  I've Googled this and saw some cases where it was a bug in the application programming, and were corrected, but most of these programs do explicitly allocate the size.

In the attached file, I did nothing to attempt to resize the window (the warning definitely refers to the toplevel window).

This occurs on all the systems on which I've tried it - Windows compiled under MinGW, Debian testing, Arch Linux, Linux Mint, and Ubuntu.  All these systems have some flavor of Gtk 3.22.

Also note that on my tests, if List 2 (the lower iter) is expanded first and the expanded size does not exceed the height of the window, and then List 1 is expanded, the warning is not displayed.

Seemingly, this is only a harmless warning, but it is a bit disconcerting to see it.  FWIW, even Glade (at least v 3.20) emits this warning when the widget hierarchy is expanded.
/* program to test the get_preferred_height warning */
#include <gtk/gtk.h>

GtkWidget *w_main,
          *main_treeview;

char *list1[][2] = {
    {"l1_1a", "l1_1b"},
    {"l1_2a", "l1_2b"},
    {"l1_3a", "l1_3b"},
    {"l1_4a", "l1_4b"},
    {NULL, NULL}
};

char *list2[][2] = {
    {"l2_1a", "l2_1b"},
    {"l2_2a", "l2_2b"},
    {"l2_3a", "l2_3b"},
    {"l2_4a", "l2_4b"},
    {"l2_5a", "l2_5b"},
    {NULL, NULL}
};

void
kill_wmain (GtkWidget *win, GdkEventAny *event)
{
    gtk_main_quit();
}

void
populate_list (char *ttl, char *list[][2])
{
    GtkTreeModel *model_main = gtk_tree_view_get_model (
            GTK_TREE_VIEW(main_treeview));
    GtkTreeIter topitr;
    char **row;
    gtk_tree_store_append (GTK_TREE_STORE(model_main), &topitr, NULL);
    gtk_tree_store_set (GTK_TREE_STORE(model_main), &topitr, 0, ttl, -1);

    while (*(row = *(list++)))
    {
        int c;
        GtkTreeIter childitr;

        gtk_tree_store_append(GTK_TREE_STORE(model_main), &childitr, &topitr);
        gtk_tree_store_set (GTK_TREE_STORE(model_main), &childitr,
                0, row[0], 1, row[1], -1);
    }
}

GtkWidget *
new_treestore (GtkWidget *parent, char *wtype)
{
    GtkTreeStore *model;
    GtkWidget    *treeview;
    char         *titles[] = {"Col1", "Col2"};
    GtkWidget    *wscrolledwindow = gtk_scrolled_window_new (NULL, NULL);
    int           x;

    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(wscrolledwindow),
                GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_box_pack_start (GTK_BOX(parent), wscrolledwindow, FALSE, TRUE, 5);
    // Just use 'tree' for wtype
    model = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
    treeview = gtk_tree_view_new ();
    gtk_tree_view_set_model (GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(model));
    gtk_widget_set_hexpand (treeview, TRUE);
    gtk_widget_set_vexpand (treeview, TRUE);

    for (x = 0; x < 2; x++)
    {
        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(treeview),
                x, titles[x], gtk_cell_renderer_text_new(), "text", x, NULL);
    }

    gtk_container_add (GTK_CONTAINER(wscrolledwindow), treeview);
    return treeview;
}

int
main (int argc, char **argv)
{

    GtkWidget *vbox;
    GtkTreeIter *iter = NULL;

    gtk_init (&argc, &argv);

    w_main = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    printf ("\nw_main: %x\n",w_main);
    g_signal_connect (w_main, "delete-event", G_CALLBACK(kill_wmain), NULL);
    gtk_window_set_default_size (GTK_WINDOW(w_main), 300, 200);
    vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
    main_treeview = new_treestore (vbox, "tree");
    printf ("main_treeview: %x\n",main_treeview);
    gtk_tree_selection_set_mode (gtk_tree_view_get_selection (
                GTK_TREE_VIEW(main_treeview)), GTK_SELECTION_SINGLE);
    populate_list ("List 1", list1);
    populate_list ("List 2", list2);
    gtk_container_add (GTK_CONTAINER(w_main), vbox);
    gtk_widget_show_all (w_main);
    gtk_main ();
    return 0;
}
_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list