Trabajando con la familia GtkTreeView |
---|
A continuación, un ejemplo completo del uso de GtkTreeStore. Con este ejemplo podrá insertar, modificar y eliminar filas. La línea de compilación es:
gcc -o ejemplo `pkg-config --libs --cflags gtk+-2.0` gtktreeview.c
#include (gtk/gtk.h) GdkPixbuf *folder; enum { COLUMN_MOVE, COLUMN_COMMENTS, NUM_COLUMNS }; /* crea el modelo tipo arbol */ GtkTreeModel * create_model_treeview(void) { GtkTreeStore *model_store; /* creamos el modelo con 2 tipos de columnas: imagen y texto */ model_store = gtk_tree_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING); return GTK_TREE_MODEL(model_store); } /* agrega una fila al arbol */ static void add_row_tree (gpointer data, GtkTreeView *treeview) { GtkTreeModel *model; GtkTreeStore *model_store; GtkTreeIter iter1, iter2; GtkTreeSelection *select; /* obtiene el modelo tipo arbol */ model_store = (GtkTreeStore *) gtk_tree_view_get_model(treeview); select = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)); /* hay una fila seleccionada? */ if (gtk_tree_selection_get_selected (select, &model, &iter1)) { gtk_tree_store_append(model_store, &iter2, &iter1); gtk_tree_store_set(model_store, &iter2, COLUMN_MOVE,folder, COLUMN_COMMENTS, "fila", -1); }else{ gtk_tree_store_append(model_store, &iter2, NULL); gtk_tree_store_set(model_store, &iter2, COLUMN_MOVE,folder, COLUMN_COMMENTS, "Arbol", -1); } g_print("add new row....\n"); } /* modifica el contenido de una fila del arbol */ static void mod_row_tree (gpointer data, GtkTreeView *treeview) { GtkTreeModel *model; GtkTreeStore *model_store; GtkTreeSelection *select; GtkTreeIter iter; /* obtiene el modelo tipo arbol */ model_store = (GtkTreeStore *) gtk_tree_view_get_model(treeview); select = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)); if (gtk_tree_selection_get_selected (select, &model, &iter)) { gtk_tree_store_set(model_store, &iter, COLUMN_MOVE,folder, COLUMN_COMMENTS, "modificado", -1); } g_print("mod row....\n"); } /* elimina una fila del arbol */ static void del_row_tree (gpointer data, GtkTreeView *treeview) { GtkTreeModel *model; GtkTreeStore *model_store; GtkTreeSelection *select; GtkTreeIter iter; /* obtiene el modelo tipo arbol */ model_store = (GtkTreeStore *) gtk_tree_view_get_model(treeview); select = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)); /* hay una fila seleccionada? */ if (gtk_tree_selection_get_selected (select, &model, &iter)) { gtk_tree_store_remove (model_store,&iter); } g_print("delete row....\n"); } /* agrega las columnas al arbol */ static void add_column_tree(GtkTreeView *treeview) { GtkCellRenderer *renderer; GtkTreeViewColumn *column; /* crea una columna */ column = gtk_tree_view_column_new(); /* coloca el nombre a la columna */ gtk_tree_view_column_set_title(column, "Titulo Columna"); /* crea un render de tipo pixbuf */ 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); /* crea un render tipo texto */ renderer = gtk_cell_renderer_text_new (); gtk_tree_view_column_pack_start (column, renderer, FALSE); gtk_tree_view_column_set_attributes (column,renderer,"text",1,NULL); /* agrega la columna al arbol */ gtk_tree_view_append_column (treeview, column); } /* principal */ int main (int argc, char * argv[]) { GtkWidget *window, *scroll; GtkWidget *treeview; GtkWidget *box, *vbox; GtkTreeModel *model; GtkWidget *button_del; GtkWidget *button_add; GtkWidget *button_mod; gtk_init(&argc, &argv); /* genera imagen (carpeta)*/ folder = gdk_pixbuf_new_from_file ("folder.png", NULL); /* crear los widgets */ window = gtk_window_new(GTK_WINDOW_TOPLEVEL); /* coloca titulo a la ventana */ gtk_window_set_title(GTK_WINDOW(window), "Ejemplo GtkTreeView"); /* define tamaños ventana */ gtk_window_set_default_size(GTK_WINDOW(window), 300,400); /* define ancho del borde */ gtk_container_set_border_width(GTK_CONTAINER(window), 10); /* crea un scroll */ scroll = gtk_scrolled_window_new(NULL, NULL); /* crea el modelo del arbol */ model = create_model_treeview(); /* crea un nuevo widget gtktreeview */ treeview = gtk_tree_view_new(); /* agrega columnas al modelo del arbol */ add_column_tree(GTK_TREE_VIEW(treeview)); /* asocia el modelo al gtkteeview */ gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), model); /* agrega una fila */ add_row_tree(NULL, GTK_TREE_VIEW(treeview)); g_object_unref(G_OBJECT(model)); /* empaquetador */ box = gtk_hbox_new(TRUE, 10); vbox = gtk_vbox_new(FALSE, 5); gtk_widget_set_name(box, "Box"); gtk_container_add(GTK_CONTAINER(scroll), treeview); gtk_box_pack_start(GTK_BOX(box), scroll, TRUE, TRUE, 2); /* crea botone */ button_del = gtk_button_new_with_label ("Eliminar"); button_add = gtk_button_new_with_label ("Agregar"); button_mod = gtk_button_new_with_label ("Modificar"); gtk_box_pack_start(GTK_BOX(vbox), box, TRUE, TRUE, 2); gtk_box_pack_start(GTK_BOX(vbox), button_del, FALSE, TRUE, 2); gtk_box_pack_start(GTK_BOX(vbox), button_add, FALSE, TRUE, 2); gtk_box_pack_start(GTK_BOX(vbox), button_mod, FALSE, TRUE, 2); gtk_container_add(GTK_CONTAINER(window), vbox); /* conectando las señales */ g_signal_connect(G_OBJECT(window), "destroy", gtk_main_quit, NULL); g_signal_connect(G_OBJECT(button_add), "clicked", G_CALLBACK(add_row_tree), treeview); g_signal_connect(G_OBJECT(button_del), "clicked", G_CALLBACK(del_row_tree), treeview); g_signal_connect(G_OBJECT(button_mod), "clicked", G_CALLBACK(mod_row_tree), treeview); gtk_widget_show_all(window); gtk_main(); return 0; }
<< El modelo GtkTreeStore |