1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include <gtk/gtk.h>
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
static gboolean
clicked_icon (GtkTreeView *tv,
int x,
int y,
GtkTreePath **path)
{
GtkTreeViewColumn *col;
int cell_x, cell_y;
int cell_pos, cell_width;
GList *cells, *l;
int depth;
int level_indentation;
int expander_size;
int indent;
if (gtk_tree_view_get_path_at_pos (tv, x, y, path, &col, &cell_x, &cell_y))
{
cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (col));
#if 1
/* ugly workaround to fix the problem:
* manually calculate the indent for the row
*/
depth = gtk_tree_path_get_depth (*path);
level_indentation = gtk_tree_view_get_level_indentation (tv);
expander_size = 16; /* Hardcoded in gtktreeview.c */
expander_size += 4;
indent = (depth - 1) * level_indentation + depth * expander_size;
#else
indent = 0;
#endif
for (l = cells; l; l = l->next)
{
gtk_tree_view_column_cell_get_position (col, l->data, &cell_pos, &cell_width);
if (cell_pos + indent <= cell_x && cell_x <= cell_pos + indent + cell_width)
{
g_print ("clicked in %s\n", g_type_name_from_instance (l->data));
if (GTK_IS_CELL_RENDERER_PIXBUF (l->data))
{
g_list_free (cells);
return TRUE;
}
}
}
g_list_free (cells);
}
return FALSE;
}
static void
release_event (GtkGestureClick *gesture,
guint n_press,
double x,
double y,
GtkTreeView *tv)
{
GtkTreePath *path;
int tx, ty;
gtk_tree_view_convert_widget_to_tree_coords (tv, x, y, &tx, &ty);
if (clicked_icon (tv, tx, ty, &path))
{
GtkTreeModel *model;
GtkTreeIter iter;
char *text;
model = gtk_tree_view_get_model (tv);
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_model_get (model, &iter, 0, &text, -1);
g_print ("text was: %s\n", text);
g_free (text);
gtk_tree_path_free (path);
}
}
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *sw;
GtkWidget *tv;
GtkTreeViewColumn *col;
GtkCellRenderer *cell;
GtkTreeStore *store;
GtkTreeIter iter;
GtkGesture *gesture;
gtk_init ();
window = gtk_window_new ();
sw = gtk_scrolled_window_new ();
gtk_window_set_child (GTK_WINDOW (window), sw);
tv = gtk_tree_view_new ();
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), tv);
col = gtk_tree_view_column_new ();
cell = gtk_cell_renderer_text_new ();
gtk_tree_view_column_pack_start (col, cell, TRUE);
gtk_tree_view_column_add_attribute (col, cell, "text", 0);
cell = gtk_cell_renderer_toggle_new ();
gtk_tree_view_column_pack_start (col, cell, FALSE);
gtk_tree_view_column_add_attribute (col, cell, "active", 1);
cell = gtk_cell_renderer_text_new ();
gtk_tree_view_column_pack_start (col, cell, TRUE);
gtk_tree_view_column_add_attribute (col, cell, "text", 0);
cell = gtk_cell_renderer_pixbuf_new ();
gtk_tree_view_column_pack_start (col, cell, FALSE);
gtk_tree_view_column_add_attribute (col, cell, "icon-name", 2);
cell = gtk_cell_renderer_toggle_new ();
gtk_tree_view_column_pack_start (col, cell, FALSE);
gtk_tree_view_column_add_attribute (col, cell, "active", 1);
gtk_tree_view_append_column (GTK_TREE_VIEW (tv), col);
store = gtk_tree_store_new (3, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_STRING);
gtk_tree_store_insert_with_values (store, NULL, NULL, 0, 0, "One row", 1, FALSE, 2, "document-open", -1);
gtk_tree_store_insert_with_values (store, &iter, NULL, 1, 0, "Two row", 1, FALSE, 2, "dialog-warning", -1);
gtk_tree_store_insert_with_values (store, NULL, &iter, 0, 0, "Three row", 1, FALSE, 2, "dialog-error", -1);
gtk_tree_view_set_model (GTK_TREE_VIEW (tv), GTK_TREE_MODEL (store));
gesture = gtk_gesture_click_new ();
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
GTK_PHASE_CAPTURE);
g_signal_connect (gesture, "released",
G_CALLBACK (release_event), tv);
gtk_widget_add_controller (tv, GTK_EVENT_CONTROLLER (gesture));
gtk_widget_show (window);
while (TRUE)
g_main_context_iteration (NULL, TRUE);
return 0;
}
|