/* GtkTreeView row-inserted bug:
 *
 * Tested so far:
 *   Gtk 2.24.19: no problem
 *   Gtk 3.6.2: issue present in fixed height mode
 *   Gtk 3.10.2: issue present in fixed height mode
 *   Gtk 3.11.4: issue present in fixed height mode
 */

#include <gtk/gtk.h>

#define FIXED_HEIGHT_MODE TRUE

void add_row_cb(GtkWidget *button, void *user_data) {
    static int i = 10;
    GtkListStore *store = GTK_LIST_STORE(user_data);
    i -= 1;
    gtk_list_store_insert_with_values(store, NULL, 0, 0, i, -1);
}

GtkListStore *create_model() {
    GtkListStore *list_store;
    list_store = gtk_list_store_new(1, G_TYPE_INT);
    return list_store;
}

GtkWidget *create_tree_view(GtkTreeModel *model) {
    GtkWidget *tree_view = gtk_tree_view_new_with_model(model);

    GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
    GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(
        "Value", renderer, "text", 0, NULL);
    gtk_tree_view_column_set_fixed_width(column, 600);
    gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
    gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);

    gtk_tree_view_set_fixed_height_mode(GTK_TREE_VIEW(tree_view), FIXED_HEIGHT_MODE);

    return tree_view;
}

GtkWidget *create_test_window(GtkListStore *model, GCallback add_row_callback) {
    GtkWidget *box = gtk_hbox_new(FALSE, 4);

    GtkWidget *add_row_button = gtk_button_new_with_label("Add Row");
    g_signal_connect(add_row_button, "clicked", add_row_callback, model);
    gtk_box_pack_start(GTK_BOX(box), add_row_button, TRUE, FALSE, 0);

    GtkWidget *tree_view = create_tree_view(GTK_TREE_MODEL(model));
    GtkWidget *scroll = gtk_scrolled_window_new(NULL, NULL);
    gtk_container_add(GTK_CONTAINER(scroll), tree_view);
    gtk_widget_set_size_request(GTK_WIDGET(scroll), 640, 80);
    gtk_box_pack_start(GTK_BOX(box), scroll, TRUE, TRUE, 0);

    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(window, "delete-event", gtk_main_quit, NULL);
    gtk_container_add(GTK_CONTAINER(window), box);

    return window;
}

int main() {
    gtk_init(0, NULL);

    g_print("Gtk treeview testcase; compiled with Gtk %i.%i.%i.\n",
        GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);

#if GTK_MAJOR_VERSION == 2
    g_print("Running with Gtk %i.%i.%i\n",
        gtk_major_version, gtk_minor_version,  gtk_micro_version);
#else
    g_print("Running with Gtk %i.%i.%i\n",
        gtk_get_major_version(), gtk_get_minor_version(),  gtk_get_micro_version());
#endif

    GtkListStore *model = create_model();
    GtkWidget *window = create_test_window(model, G_CALLBACK(add_row_cb));

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

