/*  treeview.c: Ejemplo de uso de GtkTreeView para unir Pixbuf y 
    text en una misma columna.

    (c) 2003. German Poo Caaman~o <gpoo@ubiobio.cl>
    
    Compilacion:
    $ gcc `pkg-config --cflags --libs gtk+-2.0` -Wall -g treeview.c -o treeview
*/

#include <gtk/gtk.h>

GtkWidget *create_window ();
static GtkTreeModel *create_model ();
static void add_columns (GtkTreeView * treeview);

static char *rightarrow_xpm[] = {
    "20 16 27 1",
    "   c None",
    ".  c #000000",
    "+  c #00FFFF",
    "@  c #00E7E7",
    "#  c #00F5F5",
    "$  c #00C4C4",
    "%  c #009595",
    "&  c #00FDFD",
    "*  c #00F9F9",
    "=  c #00CCCC",
    "-  c #00A2A2",
    ";  c #00EFEF",
    ">  c #00C7C7",
    ",  c #00A8A8",
    "'  c #00DDDD",
    ")  c #00A4A4",
    "!  c #009696",
    "~  c #008E8E",
    "{  c #008F8F",
    "]  c #00A5A5",
    "^  c #00C3C3",
    "/  c #007878",
    "(  c #009E9E",
    "_  c #007777",
    ":  c #00A7A7",
    "<  c #006969",
    "[  c #007676",
    "                    ",
    "                    ",
    "             ..     ",
    "             .+.    ",
    "             .+@.   ",
    "   ...........#$%.  ",
    "   .&*+++++++#=$=-. ",
    "   .;>=======$$==,,.",
    "   .')!~~~~~{]^=~/. ",
    "   ...........({_.  ",
    "             .:<.   ",
    "             .[.    ",
    "             ..     ",
    "                    ",
    "                    ",
    "                    "
};



static char *leftarrow_xpm[] = {
    "20 16 29 1",
    "   c None",
    ".  c #000000",
    "+  c #EE0000",
    "@  c #D90000",
    "#  c #CE0000",
    "$  c #C00000",
    "%  c #EA0000",
    "&  c #D70000",
    "*  c #C60000",
    "=  c #C10000",
    "-  c #FF0000",
    ";  c #F40000",
    ">  c #C40000",
    ",  c #CF0000",
    "'  c #BE0000",
    ")  c #C30000",
    "!  c #CD0000",
    "~  c #C50000",
    "{  c #CC0000",
    "]  c #AA0000",
    "^  c #BC0000",
    "/  c #B50000",
    "(  c #AC0000",
    "_  c #A70000",
    ":  c #8E0000",
    "<  c #B20000",
    "[  c #9A0000",
    "}  c #A20000",
    "|  c #930000",
    "                    ",
    "                    ",
    "     ..             ",
    "    .+.             ",
    "   .+@.             ",
    "  .+#$...........   ",
    " .%&*=@-------;>.   ",
    ".+,'=)!~{{{{{{*].   ",
    " .]$^/(_]]]]]]]:.   ",
    "  .]<[...........   ",
    "   .}|.             ",
    "    .:.             ",
    "     ..             ",
    "                    ",
    "                    ",
    "                    "
};

int
main (int argc, char *argv[])
{
    GtkWidget *window;

    gtk_init (&argc, &argv);

    window = create_window ();
    gtk_widget_show_all (window);

    gtk_main ();
    return 0;
}

GtkWidget *
create_window ()
{
    GtkWidget *window;
    GtkWidget *vbox_main;
    GtkWidget *scrolledwindow;
    GtkWidget *treeview;
    GtkTreeModel *model;

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "GtkTreeView sample");
    gtk_window_set_default_size (GTK_WINDOW (window), 400, 500);

    vbox_main = gtk_vbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (window), vbox_main);

    scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
    gtk_box_pack_start (GTK_BOX (vbox_main), scrolledwindow, TRUE,
                TRUE, 0);

    model = create_model ();

    treeview = gtk_tree_view_new ();
    add_columns (GTK_TREE_VIEW (treeview));

    gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), model);

    g_object_unref (G_OBJECT (model));

    gtk_container_add (GTK_CONTAINER (scrolledwindow), treeview);

    g_signal_connect ((gpointer) window, "delete_event",
              G_CALLBACK (gtk_main_quit), NULL);

    return window;
}

static GtkTreeModel *
create_model (void)
{
    GtkListStore *model;
    GdkPixbuf *left_pixbuf;
    GdkPixbuf *right_pixbuf;
    GtkTreeIter iter;

    model = gtk_list_store_new (2, GDK_TYPE_PIXBUF, G_TYPE_STRING);

    left_pixbuf =
        gdk_pixbuf_new_from_xpm_data ((const char **) leftarrow_xpm);
    right_pixbuf =
        gdk_pixbuf_new_from_xpm_data ((const char **) rightarrow_xpm);

    gtk_list_store_append (model, &iter);
    gtk_list_store_set (model, &iter, 0, left_pixbuf, 1, "izquierda",
                -1);
    gtk_list_store_append (model, &iter);
    gtk_list_store_set (model, &iter, 0, right_pixbuf, 1, "derecha",
                -1);

    return GTK_TREE_MODEL (model);
}

static void
add_columns (GtkTreeView * treeview)
{
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;

    column = gtk_tree_view_column_new ();
    gtk_tree_view_column_set_title (column, "Test");

    renderer = gtk_cell_renderer_pixbuf_new ();
    gtk_tree_view_column_pack_start (column, renderer, FALSE);
    gtk_tree_view_column_set_attributes (column, renderer,
                         "pixbuf", 0, NULL);

    renderer = gtk_cell_renderer_text_new ();
    gtk_tree_view_column_pack_start (column, renderer, TRUE);
    gtk_tree_view_column_set_attributes (column, renderer,
                         "text", 1, NULL);

    gtk_tree_view_append_column (treeview, column);
}