Skip to content

Instantly share code, notes, and snippets.

@JohnCoconut
Last active February 24, 2018 17:10
Show Gist options
  • Save JohnCoconut/bf17df5a3d13c889acd673740c9c2898 to your computer and use it in GitHub Desktop.
Save JohnCoconut/bf17df5a3d13c889acd673740c9c2898 to your computer and use it in GitHub Desktop.
GtkTreeView warnings
#include <gtk/gtk.h>
static void activate_main (GtkApplication *app, gpointer user_data);
static void setup_tree_view (GtkWidget *treeview);
static void populate_tree_store (GtkTreeStore *store);
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new (NULL, G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK (activate_main), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
static void
activate_main (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window, *scrolled_win;
GtkWidget *treeview;
GtkWidget *toolbar;
GtkToolItem *expand_tree_view_button, *collapse_tree_view_button;
GtkWidget *image;
GtkWidget *status_bar;
guint context_id;
GtkWidget *master_grid;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Gtk Tree View");
gtk_widget_set_size_request(window, 1280, 720);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
GtkTreeStore *store;
store = gtk_tree_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
populate_tree_store (store);
treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
g_object_unref (store);
gtk_widget_set_hexpand (treeview, TRUE);
gtk_widget_set_vexpand (treeview, TRUE);
setup_tree_view (treeview);
scrolled_win = gtk_scrolled_window_new (NULL, NULL);
gtk_container_add (GTK_CONTAINER (scrolled_win), treeview);
toolbar = gtk_toolbar_new ();
image = gtk_image_new_from_icon_name ("list-add", GTK_ICON_SIZE_SMALL_TOOLBAR);
expand_tree_view_button = gtk_tool_button_new (image, NULL);
image = gtk_image_new_from_icon_name ("list-remove", GTK_ICON_SIZE_SMALL_TOOLBAR);
collapse_tree_view_button = gtk_tool_button_new (image, NULL);
gtk_toolbar_insert (GTK_TOOLBAR (toolbar), expand_tree_view_button, -1);
gtk_toolbar_insert (GTK_TOOLBAR (toolbar), collapse_tree_view_button, -1);
g_signal_connect_swapped (expand_tree_view_button, "clicked",
G_CALLBACK (gtk_tree_view_expand_all), GTK_TREE_VIEW (treeview));
g_signal_connect_swapped (collapse_tree_view_button, "clicked",
G_CALLBACK (gtk_tree_view_collapse_all), GTK_TREE_VIEW (treeview));
status_bar = gtk_statusbar_new ();
context_id = gtk_statusbar_get_context_id (GTK_STATUSBAR (status_bar), "Status Message");
gtk_statusbar_push (GTK_STATUSBAR (status_bar), context_id, "A status message.");
master_grid = gtk_grid_new ();
gtk_grid_attach (GTK_GRID (master_grid), toolbar, 0, 0, 1, 1);
gtk_grid_attach (GTK_GRID (master_grid), scrolled_win, 0, 1, 1, 1);
gtk_grid_attach (GTK_GRID (master_grid), status_bar, 0, 2, 1, 1);
gtk_container_add (GTK_CONTAINER (window), master_grid);
gtk_widget_show_all (window);
}
static void
populate_tree_store (GtkTreeStore *store)
{
GtkTreeIter item1, item2, item3;
for (int i = 0; i < 100; i++)
{
gtk_tree_store_append (store, &item1, NULL);
gtk_tree_store_set (store, &item1,
0, g_strdup_printf ("Window %d", i + 1),
1, "12345678",
2, "ClassName",
-1);
for (int j = 0; j < 3; j++)
{
gtk_tree_store_append (store, &item2, &item1);
gtk_tree_store_set (store, &item2,
0, g_strdup_printf ("Window %d%d", i + 1, i + 2),
1, "12345678",
2, "ClassName",
-1);
for (int k = 0; k < 5; k++)
{
gtk_tree_store_append (store, &item3, &item2);
gtk_tree_store_set (store, &item3,
0, g_strdup_printf ("Window %d%d%d", i + 1, j + 1, k+ 1),
1, "12345678",
2, "ClassName",
-1);
}
}
}
}
static void
setup_tree_view (GtkWidget *treeview)
{
GtkTreeViewColumn *window_column, *handle_column, *class_column;
GtkCellRenderer *renderer;
renderer = gtk_cell_renderer_text_new ();
window_column = gtk_tree_view_column_new_with_attributes
("Window", renderer, "text", 0, NULL);
gtk_tree_view_column_set_resizable (window_column, TRUE);
renderer = gtk_cell_renderer_text_new ();
handle_column = gtk_tree_view_column_new_with_attributes
("Handle", renderer, "text", 1, NULL);
renderer = gtk_cell_renderer_text_new ();
class_column = gtk_tree_view_column_new_with_attributes
("Class", renderer, "text", 2, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), handle_column);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), window_column);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), class_column);
}
@JohnCoconut
Copy link
Author

Compile it with gcc -o app gtk_tree_view.c $(pkg-config --libs --cflags gtk+-3.0). It shows this warning,

Allocating size to GtkApplicationWindow 0x55c2a71ea2b0 without caling gtk_widget_get_preferred_width/height(). How does the code know the size to allocate?

My Gtk version is 3.22.21, as reported by pkg-config gtk+-3.0 --modversion.

@ivanstepanovftw
Copy link

Problem appears even in GTKmm... Somebody, report bug...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment