Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

GtkListStore selection example

By: Ancurio on Oct 14th, 2012  |  syntax: C  |  size: 3.46 KB  |  views: 50  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. #include <gtk/gtk.h>
  3.  
  4.  
  5. #if GTK_MAJOR_VERSION == 2
  6. static void on_up(GtkWidget *btn, gpointer user_data)
  7. {
  8.         GtkTreeSelection *sel = GTK_TREE_SELECTION(user_data);
  9.         GtkTreeModel *model;
  10.         GtkTreeIter current;
  11.         GtkTreePath *path;
  12.  
  13.         gtk_tree_selection_get_selected(sel, &model, &current);
  14.         path = gtk_tree_model_get_path(model, &current);
  15.         gtk_tree_path_prev(path);
  16.         gtk_tree_selection_select_path(sel, path);
  17.         gtk_tree_path_free(path);
  18. }
  19.  
  20. static void on_down(GtkWidget *btn, gpointer user_data)
  21. {
  22.         GtkTreeSelection *sel = GTK_TREE_SELECTION(user_data);
  23.         GtkTreeModel *model;
  24.         GtkTreeIter current;
  25.         GtkTreePath *path;
  26.  
  27.         gtk_tree_selection_get_selected(sel, &model, &current);
  28.         path = gtk_tree_model_get_path(model, &current);
  29.         gtk_tree_path_next(path);
  30.         gtk_tree_selection_select_path(sel, path);
  31.         gtk_tree_path_free(path);
  32. }
  33.  
  34. #elif GTK_MAJOR_VERSION == 3
  35.  
  36. static void on_up(GtkWidget *btn, gpointer user_data)
  37. {
  38.         GtkTreeSelection *sel = GTK_TREE_SELECTION(user_data);
  39.         GtkTreeModel *model;
  40.         GtkTreeIter current;
  41.  
  42.         gtk_tree_selection_get_selected(sel, &model, &current);
  43.         if (gtk_tree_model_iter_previous(model, &current))
  44.                 gtk_tree_selection_select_iter(sel, &current);
  45. }
  46.  
  47. static void on_down(GtkWidget *btn, gpointer user_data)
  48. {
  49.         GtkTreeSelection *sel = GTK_TREE_SELECTION(user_data);
  50.         GtkTreeModel *model;
  51.         GtkTreeIter current;
  52.  
  53.         gtk_tree_selection_get_selected(sel, &model, &current);
  54.         if (gtk_tree_model_iter_next(model, &current))
  55.                 gtk_tree_selection_select_iter(sel, &current);
  56. }
  57.  
  58. #endif
  59.  
  60.  
  61. int main(int argc, char **argv)
  62. {
  63.         gtk_init(&argc, &argv);
  64.  
  65.         GtkListStore *store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
  66.  
  67.         GtkTreeIter iter;
  68.         gtk_list_store_append(store, &iter);
  69.         gtk_list_store_set(store, &iter, 0, "One item", 1, FALSE, -1);
  70.  
  71.         gtk_list_store_append(store, &iter);
  72.         gtk_list_store_set(store, &iter, 0, "Two items", 1, TRUE, -1);
  73.  
  74.         gtk_list_store_append(store, &iter);
  75.         gtk_list_store_set(store, &iter, 0, "Last item", 1, FALSE, -1);
  76.  
  77.         GtkWidget *view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
  78.         g_object_unref(G_OBJECT(store));
  79.  
  80.         GtkCellRenderer *render = gtk_cell_renderer_text_new();
  81.  
  82.         gtk_tree_view_insert_column_with_attributes
  83.                 (GTK_TREE_VIEW(view), -1, "String", render, "text", 0, NULL);
  84.         gtk_tree_view_insert_column_with_attributes
  85.                 (GTK_TREE_VIEW(view), -1, "Bool", render, "text", 1, NULL);
  86.  
  87. #if GTK_MAJOR_VERSION == 2
  88.         GtkWidget *vbox = gtk_vbox_new(FALSE, 4);
  89.         GtkWidget *hbox = gtk_hbox_new(FALSE, 4);
  90. #else
  91.         GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
  92.         GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
  93. #endif
  94.  
  95.         GtkWidget *btn_up = gtk_button_new_with_label("Up");
  96.         GtkWidget *btn_down = gtk_button_new_with_label("Down");
  97.         gtk_box_pack_start(GTK_BOX(vbox), btn_up, FALSE, FALSE, 4);
  98.         gtk_box_pack_start(GTK_BOX(vbox), btn_down, FALSE, FALSE, 4);
  99.  
  100.         GtkTreeSelection *selection =
  101.                 gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
  102.  
  103.         g_signal_connect
  104.                 (G_OBJECT(btn_up), "clicked", G_CALLBACK(on_up), selection);
  105.  
  106.         g_signal_connect
  107.                 (G_OBJECT(btn_down), "clicked", G_CALLBACK(on_down), selection);
  108.  
  109.         gtk_box_pack_start(GTK_BOX(hbox), view, TRUE, TRUE, 4);
  110.         gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 4);
  111.  
  112.  
  113.         GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  114.         gtk_container_add(GTK_CONTAINER(window), hbox);
  115.         gtk_widget_show_all(window);
  116.         g_signal_connect(window, "delete-event", gtk_main_quit, NULL);
  117.  
  118.         gtk_main();
  119.  
  120. }