]> Pileus Git - ~andy/gtk/blob - tests/testtreepos.c
Add a testcase for cell positions
[~andy/gtk] / tests / testtreepos.c
1 #include <gtk/gtk.h>
2
3 static gboolean
4 clicked_icon (GtkTreeView  *tv,
5               gint          x,
6               gint          y,
7               GtkTreePath **path)
8 {
9   GtkTreeViewColumn *col;
10   gint cell_x, cell_y;
11   gint cell_pos, cell_width;
12   GList *cells, *l;
13
14   if (gtk_tree_view_get_path_at_pos (tv, x, y, path, &col, &cell_x, &cell_y))
15     {
16       cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (col));
17
18       for (l = cells; l; l = l->next)
19         {
20           gtk_tree_view_column_cell_get_position (col, l->data, &cell_pos, &cell_width);
21           if (cell_pos <= cell_x && cell_x <= cell_pos + cell_width)
22             {
23               g_print ("clicked in %s\n", g_type_name_from_instance (l->data));
24               if (GTK_IS_CELL_RENDERER_PIXBUF (l->data))
25                 {
26                   g_list_free (cells);
27                   return TRUE;
28                 }
29             }
30         }
31
32       g_list_free (cells);
33     }
34
35   return FALSE;
36 }
37
38 static gboolean
39 release_event (GtkTreeView    *tv,
40                GdkEventButton *event)
41 {
42   GtkTreePath *path;
43
44   if (event->type != GDK_BUTTON_RELEASE)
45     return TRUE;
46
47   if (clicked_icon (tv, event->x, event->y, &path))
48     {
49       GtkTreeModel *model;
50       GtkTreeIter iter;
51       gchar *text;
52
53       model = gtk_tree_view_get_model (tv);
54       gtk_tree_model_get_iter (model, &iter, path);
55       gtk_tree_model_get (model, &iter, 0, &text, -1);
56
57       g_print ("text was: %s\n", text);
58       g_free (text);
59       gtk_tree_path_free (path);
60
61       return TRUE;
62     }
63
64   return FALSE;
65 }
66
67 int main (int argc, char *argv[])
68 {
69   GtkWidget *window;
70   GtkWidget *sw;
71   GtkWidget *tv;
72   GtkTreeViewColumn *col;
73   GtkCellRenderer *cell;
74   GtkTreeStore *store;
75   GtkTreeIter iter;
76
77   gtk_init (&argc, &argv);
78
79   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
80   sw = gtk_scrolled_window_new (NULL, NULL);
81   gtk_container_add (GTK_CONTAINER (window), sw);
82   tv = gtk_tree_view_new ();
83   gtk_container_add (GTK_CONTAINER (sw), tv);
84
85   col = gtk_tree_view_column_new ();
86   cell = gtk_cell_renderer_text_new ();
87   gtk_tree_view_column_pack_start (col, cell, TRUE);
88   gtk_tree_view_column_add_attribute (col, cell, "text", 0);
89
90   cell = gtk_cell_renderer_toggle_new ();
91   gtk_tree_view_column_pack_start (col, cell, FALSE);
92   gtk_tree_view_column_add_attribute (col, cell, "active", 1);
93
94   cell = gtk_cell_renderer_text_new ();
95   gtk_tree_view_column_pack_start (col, cell, TRUE);
96   gtk_tree_view_column_add_attribute (col, cell, "text", 0);
97
98   cell = gtk_cell_renderer_pixbuf_new ();
99   gtk_tree_view_column_pack_start (col, cell, FALSE);
100   gtk_tree_view_column_add_attribute (col, cell, "stock-id", 2);
101
102   cell = gtk_cell_renderer_toggle_new ();
103   gtk_tree_view_column_pack_start (col, cell, FALSE);
104   gtk_tree_view_column_add_attribute (col, cell, "active", 1);
105
106   gtk_tree_view_append_column (GTK_TREE_VIEW (tv), col);
107
108   store = gtk_tree_store_new (3, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_STRING);
109   gtk_tree_store_insert_with_values (store, NULL, NULL, 0, 0, "One row", 1, FALSE, 2, "gtk-open", -1);
110   gtk_tree_store_insert_with_values (store, &iter, NULL, 1, 0, "Two row", 1, FALSE, 2, "gtk-file", -1);
111   gtk_tree_store_insert_with_values (store, NULL, &iter, 0, 0, "Three row", 1, FALSE, 2, "gtk-file", -1);
112
113   gtk_tree_view_set_model (GTK_TREE_VIEW (tv), GTK_TREE_MODEL (store));
114
115   g_signal_connect (tv, "button-release-event",
116                     G_CALLBACK (release_event), NULL);
117
118   gtk_widget_show_all (window);
119
120   gtk_main ();
121
122   return 0;
123 }