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

GtkTreeView performance test

By: a guest on Dec 15th, 2011  |  syntax: C  |  size: 5.06 KB  |  views: 200  |  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. #include <gtk/gtk.h>
  2.  
  3. #define ROWS 100000
  4.  
  5. static GtkTreeModelFlags list_model_get_flags (GtkTreeModel * model)
  6. {
  7.     return GTK_TREE_MODEL_LIST_ONLY;
  8. }
  9.  
  10. static int list_model_get_n_columns (GtkTreeModel * model)
  11. {
  12.     return 3;
  13. }
  14.  
  15. static GType list_model_get_column_type (GtkTreeModel * model, int column)
  16. {
  17.     if (column < 0 || column > 2)
  18.         return G_TYPE_INVALID;
  19.  
  20.     if (column == 0)
  21.         return G_TYPE_INT;
  22.     else
  23.         return G_TYPE_STRING;
  24. }
  25.  
  26. static gboolean list_model_get_iter (GtkTreeModel * model, GtkTreeIter * iter,
  27.  GtkTreePath * path)
  28. {
  29.     int row = gtk_tree_path_get_indices (path)[0];
  30.  
  31.     if (row < 0 || row >= ROWS)
  32.         return FALSE;
  33.  
  34.     iter->user_data = GINT_TO_POINTER (row);
  35.     return TRUE;
  36. }
  37.  
  38. static GtkTreePath * list_model_get_path (GtkTreeModel * model, GtkTreeIter * iter)
  39. {
  40.     int row = GPOINTER_TO_INT (iter->user_data);
  41.  
  42.     if (row < 0 || row >= ROWS)
  43.         return NULL;
  44.  
  45.     return gtk_tree_path_new_from_indices (row, -1);
  46. }
  47.  
  48. static void list_model_get_value (GtkTreeModel * model, GtkTreeIter * iter,
  49.  int column, GValue * value)
  50. {
  51.     int row = GPOINTER_TO_INT (iter->user_data);
  52.  
  53.     if (column < 0 || column > 2 || row < 0 || row >= ROWS)
  54.         return;
  55.  
  56.     if (column == 0)
  57.     {
  58.         g_value_init (value, G_TYPE_INT);
  59.         g_value_set_int (value, 1 + row);
  60.     }
  61.     else
  62.     {
  63.         g_value_init (value, G_TYPE_STRING);
  64.         g_value_set_string (value, (column == 1) ? "Hello, world!" : "Blah blah blah");
  65.     }
  66. }
  67.  
  68. static gboolean list_model_iter_next (GtkTreeModel * model, GtkTreeIter * iter)
  69. {
  70.     int row = GPOINTER_TO_INT (iter->user_data);
  71.  
  72.     if (row < 0 || row >= ROWS - 1)
  73.         return FALSE;
  74.  
  75.     iter->user_data = GINT_TO_POINTER (row + 1);
  76.     return TRUE;
  77. }
  78.  
  79. static gboolean list_model_iter_children (GtkTreeModel * model,
  80.  GtkTreeIter * iter, GtkTreeIter * parent)
  81. {
  82.     if (parent)
  83.         return FALSE;
  84.  
  85.     iter->user_data = GINT_TO_POINTER (0);
  86.     return TRUE;
  87. }
  88.  
  89. static gboolean list_model_iter_has_child (GtkTreeModel * model, GtkTreeIter * iter)
  90. {
  91.     return FALSE;
  92. }
  93.  
  94. static int list_model_iter_n_children (GtkTreeModel * model, GtkTreeIter * iter)
  95. {
  96.     return iter ? 0 : ROWS;
  97. }
  98.  
  99. static gboolean list_model_iter_nth_child (GtkTreeModel * model,
  100.  GtkTreeIter * iter, GtkTreeIter * parent, int n)
  101. {
  102.     if (parent || n < 0 || n >= ROWS)
  103.         return FALSE;
  104.  
  105.     iter->user_data = GINT_TO_POINTER (n);
  106.     return TRUE;
  107. }
  108.  
  109. static gboolean list_model_iter_parent (GtkTreeModel * model,
  110.  GtkTreeIter * iter, GtkTreeIter * child)
  111. {
  112.     return FALSE;
  113. }
  114.  
  115. static void iface_init (void * _iface, void * unused)
  116. {
  117.     GtkTreeModelIface * iface = _iface;
  118.  
  119.     iface->get_flags = list_model_get_flags;
  120.     iface->get_n_columns = list_model_get_n_columns;
  121.     iface->get_column_type = list_model_get_column_type;
  122.     iface->get_iter = list_model_get_iter;
  123.     iface->get_path = list_model_get_path;
  124.     iface->get_value = list_model_get_value;
  125.     iface->iter_next = list_model_iter_next;
  126.     iface->iter_children = list_model_iter_children;
  127.     iface->iter_has_child = list_model_iter_has_child;
  128.     iface->iter_n_children = list_model_iter_n_children;
  129.     iface->iter_nth_child = list_model_iter_nth_child;
  130.     iface->iter_parent = list_model_iter_parent;
  131. }
  132.  
  133. static const GInterfaceInfo iface_info = {
  134.  .interface_init = /* (GInterfaceInitFunc) */ iface_init,
  135.  .interface_finalize = NULL,
  136.  .interface_data = NULL};
  137.  
  138. static GType list_model_get_type (void)
  139. {
  140.     static GType type = G_TYPE_INVALID;
  141.     if (type == G_TYPE_INVALID)
  142.     {
  143.         type = g_type_register_static_simple (G_TYPE_OBJECT, "MyListModel",
  144.          sizeof (GObjectClass), NULL, sizeof (GObject), NULL, 0);
  145.         g_type_add_interface_static (type, GTK_TYPE_TREE_MODEL, & iface_info);
  146.     }
  147.     return type;
  148. }
  149.  
  150. int main (void)
  151. {
  152.     gtk_init (NULL, NULL);
  153.  
  154.     GtkWidget * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  155.     gtk_window_set_default_size ((GtkWindow *) window, 700, 500);
  156.  
  157.     GtkWidget * scrolled = gtk_scrolled_window_new (NULL, NULL);
  158.     gtk_scrolled_window_set_policy ((GtkScrolledWindow *) scrolled,
  159.      GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
  160.  
  161.     GObject * model = g_object_new (list_model_get_type (), NULL);
  162.     GtkWidget * list = gtk_tree_view_new_with_model ((GtkTreeModel *) model);
  163.  
  164.     for (int col = 0; col < 3; col ++)
  165.     {
  166.         GtkCellRenderer * renderer = gtk_cell_renderer_text_new ();
  167.         GtkTreeViewColumn * column = gtk_tree_view_column_new_with_attributes
  168.          (NULL, renderer, "text", col, NULL);
  169.  
  170.         gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
  171.         gtk_tree_view_column_set_fixed_width (column, 100);
  172.  
  173.         gtk_tree_view_append_column ((GtkTreeView *) list, column);
  174.     }
  175.  
  176.     gtk_container_add ((GtkContainer *) scrolled, list);
  177.     gtk_container_add ((GtkContainer *) window, scrolled);
  178.     gtk_widget_show_all (window);
  179.  
  180.     g_signal_connect (window, "destroy", (GCallback) gtk_main_quit, NULL);
  181.  
  182.     gtk_main ();
  183.  
  184.     return 0;
  185. }