Advertisement
brandizzi

Selecting a line in a Gtk+ tree view - ok to select child ro

Jul 18th, 2012
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 KB | None | 0 0
  1. #include <gtk/gtk.h>
  2. #include <stdbool.h>
  3.  
  4. void on_cursor_changed(GtkTreeView *tree_view, gpointer data);
  5.  
  6. int main(int argc, char *argv[]) {
  7.     gtk_init(&argc, &argv);
  8.  
  9.     // Creating visual elements
  10.     GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL),
  11.         *tree_view = gtk_tree_view_new();
  12.     GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
  13.     GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(
  14.        "title", renderer, "text", 0, NULL);
  15.     gtk_tree_view_insert_column(GTK_TREE_VIEW(tree_view), column, 0);
  16.        
  17.     // Creating model
  18.     GtkTreeStore *model = gtk_tree_store_new(1, G_TYPE_STRING);
  19.    
  20.     // Adding toplevel rows
  21.     GtkTreeIter iter;
  22.     gtk_tree_store_append(model, &iter, NULL);
  23.     gtk_tree_store_set(model, &iter, 0, "line 0", -1);
  24.    
  25.     gtk_tree_store_append(model, &iter, NULL);
  26.     gtk_tree_store_set(model, &iter, 0, "line 1", -1);
  27.    
  28.     gtk_tree_store_append(model, &iter, NULL);
  29.     gtk_tree_store_set(model, &iter, 0, "line 2 with children", -1);
  30.    
  31.     // Adding children rows
  32.     GtkTreeIter child;
  33.     gtk_tree_store_append(model, &child, &iter);
  34.     gtk_tree_store_set(model, &child, 0, "child line 1", -1);
  35.    
  36.     gtk_tree_store_append(model, &child, &iter);
  37.     gtk_tree_store_set(model, &child, 0, "child line 2", -1);
  38.    
  39.     // Binding model to view
  40.     gtk_tree_view_set_model(GTK_TREE_VIEW(tree_view), GTK_TREE_MODEL(model));
  41.     // Expanding third line
  42.     GtkTreePath *path = gtk_tree_path_new_from_string("2");
  43.     gtk_tree_view_expand_row(GTK_TREE_VIEW(tree_view), path, TRUE);
  44.     // Selecting a line
  45.     gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(model), &iter, "2:0");
  46.     GtkTreeSelection *selection =
  47.             gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
  48.     gtk_tree_selection_select_iter(selection, &iter);
  49.  
  50.     // Connecting selection callback to view
  51.     g_signal_connect(G_OBJECT(tree_view), "cursor-changed",
  52.             G_CALLBACK(on_cursor_changed), NULL);
  53.    
  54.     gtk_widget_show(tree_view);
  55.    
  56.     // Gran finale
  57.     gtk_container_add(GTK_CONTAINER(window), tree_view);
  58.     gtk_widget_show(window);
  59.    
  60.     gtk_main();
  61.     return 0;
  62. }
  63.  
  64. void on_cursor_changed(GtkTreeView *tree_view, gpointer data) {
  65.     GtkTreeSelection *selection = gtk_tree_view_get_selection(tree_view);
  66.     GtkTreeIter iter;
  67.     GtkTreeModel *model;
  68.     bool has_selection = gtk_tree_selection_get_selected(
  69.             selection, &model, &iter);
  70.     //if (!has_selection) return;
  71.    
  72.     gchar *path_str = gtk_tree_model_get_string_from_iter(model, &iter);
  73.  
  74.     if (strncmp(path_str, "0", 2) == 0) {
  75.         g_print("1st line selected\n");
  76.     } else if (strncmp(path_str, "1", 2) == 0) {
  77.         g_print("2nd line selected\n");
  78.     } else if (strncmp(path_str, "2", 2) == 0) {
  79.         g_print("3rd line selected\n");
  80.     } else if (strncmp(path_str, "2:", 2) == 0) {
  81.         GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
  82.         gint *indices = gtk_tree_path_get_indices(path);
  83.         g_print("Child #%d from 3rd line selected\n", indices[1]);
  84.     }
  85.    
  86.     g_free(path_str);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement