]> Pileus Git - ~andy/gtk/blob - tests/testtreepos.c
stylecontext: Do invalidation on first resize container
[~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   gint depth;
14   gint level_indentation;
15   gint expander_size;
16   gint indent;
17
18   if (gtk_tree_view_get_path_at_pos (tv, x, y, path, &col, &cell_x, &cell_y))
19     {
20       cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (col));
21
22 #if 1
23       /* ugly workaround to fix the problem:
24        * manually calculate the indent for the row
25        */
26       depth = gtk_tree_path_get_depth (*path);
27       level_indentation = gtk_tree_view_get_level_indentation (tv);
28       gtk_widget_style_get (GTK_WIDGET (tv), "expander-size", &expander_size, NULL);
29       expander_size += 4;
30       indent = (depth - 1) * level_indentation + depth * expander_size;
31 #else
32       indent = 0;
33 #endif
34
35       for (l = cells; l; l = l->next)
36         {
37           gtk_tree_view_column_cell_get_position (col, l->data, &cell_pos, &cell_width);
38           if (cell_pos + indent <= cell_x && cell_x <= cell_pos + indent + cell_width)
39             {
40               g_print ("clicked in %s\n", g_type_name_from_instance (l->data));
41               if (GTK_IS_CELL_RENDERER_PIXBUF (l->data))
42                 {
43                   g_list_free (cells);
44                   return TRUE;
45                 }
46             }
47         }
48
49       g_list_free (cells);
50     }
51
52   return FALSE;
53 }
54
55 static gboolean
56 release_event (GtkTreeView    *tv,
57                GdkEventButton *event)
58 {
59   GtkTreePath *path;
60
61   if (event->type != GDK_BUTTON_RELEASE)
62     return TRUE;
63
64   if (clicked_icon (tv, event->x, event->y, &path))
65     {
66       GtkTreeModel *model;
67       GtkTreeIter iter;
68       gchar *text;
69
70       model = gtk_tree_view_get_model (tv);
71       gtk_tree_model_get_iter (model, &iter, path);
72       gtk_tree_model_get (model, &iter, 0, &text, -1);
73
74       g_print ("text was: %s\n", text);
75       g_free (text);
76       gtk_tree_path_free (path);
77
78       return TRUE;
79     }
80
81   return FALSE;
82 }
83
84 int main (int argc, char *argv[])
85 {
86   GtkWidget *window;
87   GtkWidget *sw;
88   GtkWidget *tv;
89   GtkTreeViewColumn *col;
90   GtkCellRenderer *cell;
91   GtkTreeStore *store;
92   GtkTreeIter iter;
93
94   gtk_init (&argc, &argv);
95
96   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
97   sw = gtk_scrolled_window_new (NULL, NULL);
98   gtk_container_add (GTK_CONTAINER (window), sw);
99   tv = gtk_tree_view_new ();
100   gtk_container_add (GTK_CONTAINER (sw), tv);
101
102   col = gtk_tree_view_column_new ();
103   cell = gtk_cell_renderer_text_new ();
104   gtk_tree_view_column_pack_start (col, cell, TRUE);
105   gtk_tree_view_column_add_attribute (col, cell, "text", 0);
106
107   cell = gtk_cell_renderer_toggle_new ();
108   gtk_tree_view_column_pack_start (col, cell, FALSE);
109   gtk_tree_view_column_add_attribute (col, cell, "active", 1);
110
111   cell = gtk_cell_renderer_text_new ();
112   gtk_tree_view_column_pack_start (col, cell, TRUE);
113   gtk_tree_view_column_add_attribute (col, cell, "text", 0);
114
115   cell = gtk_cell_renderer_pixbuf_new ();
116   gtk_tree_view_column_pack_start (col, cell, FALSE);
117   gtk_tree_view_column_add_attribute (col, cell, "stock-id", 2);
118
119   cell = gtk_cell_renderer_toggle_new ();
120   gtk_tree_view_column_pack_start (col, cell, FALSE);
121   gtk_tree_view_column_add_attribute (col, cell, "active", 1);
122
123   gtk_tree_view_append_column (GTK_TREE_VIEW (tv), col);
124
125   store = gtk_tree_store_new (3, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_STRING);
126   gtk_tree_store_insert_with_values (store, NULL, NULL, 0, 0, "One row", 1, FALSE, 2, "gtk-open", -1);
127   gtk_tree_store_insert_with_values (store, &iter, NULL, 1, 0, "Two row", 1, FALSE, 2, "gtk-file", -1);
128   gtk_tree_store_insert_with_values (store, NULL, &iter, 0, 0, "Three row", 1, FALSE, 2, "gtk-file", -1);
129
130   gtk_tree_view_set_model (GTK_TREE_VIEW (tv), GTK_TREE_MODEL (store));
131
132   g_signal_connect (tv, "button-release-event",
133                     G_CALLBACK (release_event), NULL);
134
135   gtk_widget_show_all (window);
136
137   gtk_main ();
138
139   return 0;
140 }