1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #include <gtk/gtk.h>
//treeview.c
/* Compile with:
* gcc -std=c11 -Wall -fmax-errors=10 -Wextra treeview.c -o treeview `pkg-config --cflags --libs gtk+-3.0 `
*/
enum{
colonne_nom = 0,
colonne_prenom,
colonne_pays,
n_colonnes
};
void model_data_new(GtkTreeModel* store,const gchar* nom, const gchar* prenom, const gchar* pays)
{
GtkTreeIter iter;
gtk_list_store_append(GTK_LIST_STORE(store), &iter);
gtk_list_store_set(GTK_LIST_STORE(store), &iter,
colonne_nom, nom,
colonne_prenom, prenom,
colonne_pays, pays,
-1);
}
GtkTreeModel* create_model() {
GtkListStore *store;
store = gtk_list_store_new (n_colonnes, G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING);
return GTK_TREE_MODEL(store);
}
void arrange_tree_view(GtkWidget* view)
{
GtkCellRenderer* renderer;
// colonne 1: nom
renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_insert_column_with_attributes(
GTK_TREE_VIEW(view), -1, "nom", renderer, "text", colonne_nom, NULL);
// colonne 2: prenom
gtk_tree_view_insert_column_with_attributes(
GTK_TREE_VIEW(view), -1, "prenom", renderer, "text", colonne_prenom, NULL);
// colonne 3: pays
gtk_tree_view_insert_column_with_attributes(
GTK_TREE_VIEW(view), -1, "pays", renderer, "text", colonne_pays, NULL);
}
void view_onRowActivated (GtkTreeView *treeview,
GtkTreePath *path,
GtkTreeViewColumn *col,
gpointer userdata)
{
GtkTreeModel *model;
GtkTreeIter iter;
g_print ("A row has been double-clicked!\n");
model = gtk_tree_view_get_model(treeview);
if (gtk_tree_model_get_iter(model, &iter, path))
{
gchar *name;
gtk_tree_model_get(model, &iter, colonne_nom, &name, -1);
g_print ("Double-clicked row contains name %s\n", name);
g_free(name);
}
}
int main( int argc, char *argv[] ) {
GtkWidget * mafenetre;
/* Ceci est appelé dans toutes les applications GTK. Les arguments sont analysés
À partir de la ligne de commande et sont retournés à l'application. */
gtk_init ( &argc, &argv );
mafenetre=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(mafenetre), "Samomoi");
gtk_window_set_position(GTK_WINDOW(mafenetre), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(mafenetre), 400, 100);
gtk_window_set_icon_from_file(GTK_WINDOW(mafenetre), "gtk-logo-rgb.gif", NULL);
g_signal_connect( G_OBJECT( mafenetre ), "destroy", gtk_main_quit, NULL);
GtkWidget* view;
view = gtk_tree_view_new();
gtk_container_add( GTK_CONTAINER(mafenetre), view);
// Orgganisation du vue
arrange_tree_view(view);
// fixation de modèle
GtkTreeModel* store = create_model();
gtk_tree_view_set_model ( GTK_TREE_VIEW(view), store);
model_data_new(store, "Karihila", "Ali", "Comores");
model_data_new(store, "Hassane", "Yousouf", "Maroc");
model_data_new(store, "Abdoulkassim", "dajaber", "Algerie");
model_data_new(store, "Bakayoko", "Justin", "Cote D'ivoire");
model_data_new(store, "Dubois", "Josephine", "France");
g_object_unref( store );
g_signal_connect(view, "row-activated", (GCallback) view_onRowActivated, NULL);
gtk_widget_show_all ( mafenetre );
gtk_main ();
return 0;
} |
Partager