2011-02-05 06:24:21 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2022-10-08 00:59:10 +00:00
|
|
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
|
|
|
|
2011-02-05 06:24:21 +00:00
|
|
|
static gboolean
|
|
|
|
clicked_icon (GtkTreeView *tv,
|
2020-07-24 13:54:49 +00:00
|
|
|
int x,
|
|
|
|
int y,
|
2011-02-05 06:24:21 +00:00
|
|
|
GtkTreePath **path)
|
|
|
|
{
|
|
|
|
GtkTreeViewColumn *col;
|
2020-07-24 13:54:49 +00:00
|
|
|
int cell_x, cell_y;
|
|
|
|
int cell_pos, cell_width;
|
2011-02-05 06:24:21 +00:00
|
|
|
GList *cells, *l;
|
2020-07-24 13:54:49 +00:00
|
|
|
int depth;
|
|
|
|
int level_indentation;
|
|
|
|
int expander_size;
|
|
|
|
int indent;
|
2011-02-05 06:24:21 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
2011-02-05 07:17:00 +00:00
|
|
|
#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);
|
2017-06-18 11:17:00 +00:00
|
|
|
expander_size = 16; /* Hardcoded in gtktreeview.c */
|
2011-02-05 07:17:00 +00:00
|
|
|
expander_size += 4;
|
|
|
|
indent = (depth - 1) * level_indentation + depth * expander_size;
|
|
|
|
#else
|
|
|
|
indent = 0;
|
|
|
|
#endif
|
|
|
|
|
2011-02-05 06:24:21 +00:00
|
|
|
for (l = cells; l; l = l->next)
|
|
|
|
{
|
|
|
|
gtk_tree_view_column_cell_get_position (col, l->data, &cell_pos, &cell_width);
|
2011-02-05 07:17:00 +00:00
|
|
|
if (cell_pos + indent <= cell_x && cell_x <= cell_pos + indent + cell_width)
|
2011-02-05 06:24:21 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-06-26 22:03:34 +00:00
|
|
|
static void
|
2019-05-29 17:10:46 +00:00
|
|
|
release_event (GtkGestureClick *gesture,
|
|
|
|
guint n_press,
|
2020-07-24 20:32:16 +00:00
|
|
|
double x,
|
|
|
|
double y,
|
2019-05-29 17:10:46 +00:00
|
|
|
GtkTreeView *tv)
|
2011-02-05 06:24:21 +00:00
|
|
|
{
|
|
|
|
GtkTreePath *path;
|
2020-07-24 13:54:49 +00:00
|
|
|
int tx, ty;
|
2011-02-05 06:24:21 +00:00
|
|
|
|
2018-06-26 22:03:34 +00:00
|
|
|
gtk_tree_view_convert_widget_to_tree_coords (tv, x, y, &tx, &ty);
|
2011-02-05 06:24:21 +00:00
|
|
|
|
2018-06-26 22:03:34 +00:00
|
|
|
if (clicked_icon (tv, tx, ty, &path))
|
2011-02-05 06:24:21 +00:00
|
|
|
{
|
|
|
|
GtkTreeModel *model;
|
|
|
|
GtkTreeIter iter;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *text;
|
2011-02-05 06:24:21 +00:00
|
|
|
|
|
|
|
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;
|
2018-06-26 22:03:34 +00:00
|
|
|
GtkGesture *gesture;
|
2011-02-05 06:24:21 +00:00
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
gtk_init ();
|
2011-02-05 06:24:21 +00:00
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2020-06-24 15:25:09 +00:00
|
|
|
sw = gtk_scrolled_window_new ();
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), sw);
|
2011-02-05 06:24:21 +00:00
|
|
|
tv = gtk_tree_view_new ();
|
2020-05-02 04:51:20 +00:00
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), tv);
|
2011-02-05 06:24:21 +00:00
|
|
|
|
|
|
|
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);
|
2013-06-24 21:31:22 +00:00
|
|
|
gtk_tree_view_column_add_attribute (col, cell, "icon-name", 2);
|
2011-02-05 06:24:21 +00:00
|
|
|
|
|
|
|
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);
|
2013-06-24 21:31:22 +00:00
|
|
|
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);
|
2011-02-05 06:24:21 +00:00
|
|
|
|
|
|
|
gtk_tree_view_set_model (GTK_TREE_VIEW (tv), GTK_TREE_MODEL (store));
|
|
|
|
|
2019-05-29 17:10:46 +00:00
|
|
|
gesture = gtk_gesture_click_new ();
|
2018-06-26 22:03:34 +00:00
|
|
|
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));
|
2011-02-05 06:24:21 +00:00
|
|
|
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2011-02-05 06:24:21 +00:00
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
while (TRUE)
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
2011-02-05 06:24:21 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|