#include <glib.h>
#include <gtk/gtk.h>

static void
switch_cb (GtkWidget *button, GtkTreeView *view)
{
	GtkTreeSelection *selection;
	GtkTreeModel *model;
	GtkTreeIter iter, iter_prev;
	GtkTreePath *path;

	selection = gtk_tree_view_get_selection (view);

	if (gtk_tree_selection_get_selected (selection, &model, &iter))
	{
		path = gtk_tree_model_get_path (model, &iter);

		if (!gtk_tree_path_prev (path))
		{
			gtk_tree_path_free (path);
			return;
		}

		gtk_tree_model_get_iter (model, &iter_prev, path);

		gtk_list_store_swap (GTK_LIST_STORE (model), &iter_prev, &iter);

		gtk_tree_path_free (path);
	}
}

int main(int argc, char **argv)
{
	GtkWidget *window, *vbox, *view, *button;
	GtkListStore *store;
	GtkTreeIter iter;
	GtkCellRenderer *renderer;
	GValue value = { 0, };

	gtk_init (&argc, &argv);

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	g_signal_connect (window, "delete-event", G_CALLBACK (gtk_main_quit), NULL);
	vbox = gtk_vbox_new (FALSE, 0);

        store = gtk_list_store_new (1, G_TYPE_STRING);
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter, 0, "A", -1);
	gtk_list_store_append (store, &iter);
	gtk_list_store_set (store, &iter, 0, "B", -1);

	view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
        renderer = gtk_cell_renderer_text_new ();
        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(view), 0, "Test", renderer, "text", 0, NULL);

        button = gtk_button_new_with_label ("Move Up");
        g_signal_connect (button, "clicked", G_CALLBACK (switch_cb), view);

	gtk_container_add (GTK_CONTAINER (window), vbox);
	gtk_box_pack_start (GTK_BOX (vbox), view, FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);

	gtk_widget_show_all (window);
	gtk_main ();
}
