GTK4 and mouse click position in a GtkTreeView

Hello world,
still in the process of migrating my app from GTK3 to GTK4, I am facing some issue with the management of mouse click event over a GtkTreeView.
I want to find the point where the mouse click happened, you will here the code I wrote:

void measure_tree_button_event (GtkWidget * widget, double event_x, double event_y, guint event_button, gpointer data)
{
  if (event_button == 1)
  {
    GtkTreeModel * measure_model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
    GtkTreePath * path;
    GtkTreeViewColumn * column;
    int i, j;
    if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW(widget), event_x, event_y, & path, & column, & i,  & j))
    {
      GtkTreeIter row;
      if  (gtk_tree_model_get_iter (measure_model, & row, path))
      {
        gtk_tree_model_get (measure_model, & row, 0, & j, -1);
        // Then do something      
      }
    }
  }
}

#ifdef GTK3
G_MODULE_EXPORT gboolean measure_tree_selection_event (GtkWidget * widget, GdkEventButton * event, gpointer data)
{
  if (event -> type == GDK_BUTTON_PRESS)
  {
    GdkEventButton * bevent = (GdkEventButton*)event;
    measure_tree_button_event (widget, bevent -> x, bevent -> y, bevent -> button, data);
  }
  return FALSE;
}
#else
G_MODULE_EXPORT void measure_tree_button_pressed (GtkGesture * gesture, int n_press, double x, double y, gpointer data)
{
  measure_tree_button_event (gtk_event_controller_get_widget ((GtkEventController*)gesture), x, y, gtk_gesture_single_get_current_button ((GtkGestureSingle * )gesture), data);
}
#endif

GtkWidget * creating_tree_view (GtkTreeModel * model, gpointer data)
{
  GtkWidget * selection_tree = gtk_tree_view_new_with_model(model);
#ifdef GTK4
  gesture = gtk_gesture_click_new ();
  gtk_event_controller_set_name (GTK_EVENT_CONTROLLER (gesture), "clicked");
  gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (gesture), 0);
  g_signal_connect (gesture, "pressed", G_CALLBACK(measure_tree_button_pressed), data);
  gtk_widget_add_controller (widget, GTK_EVENT_CONTROLLER (gesture));
#else
  g_signal_connect (G_OBJECT(selection_tree), "button_press_event", G_CALLBACK(measure_tree_selection_event), data);
#endif
  }
  return selection_tree;
}

With GTK3 no issue the mouse click return the proper position in the GtkTreeView, with GTK4 however the position in wrong and is down one more row than it should … so far I was not able to understand what is wrong if anything.
Any idea would be appreciated !

Thanks in advance for your lights :wink:

PS: is there any way to highlight code in the post so that it can be read more easily, sometimes it seems to work that way but some other it does not …

1 Like

Dear all,
an up on this issue, got an update on the situation today and I want to share that with you.
So the thing is that I can pick and select line in the treeview, but when I click on a line it always select the line
bellow it (I click on the first line, then it pick the second line, and so on) it behave like this up to the last line, then no line can be selected (out of bounds for the treeview) and the first line can not be selected because there is no line before … at least that what I though … I noticed that the header of the treeview is actually clickable, and that If I click on the headers it will actually find the first line in my treeview.
But the thing is that the headers is actually not clickable if I check that in the code using:

gtk_tree_view_get_headers_clickable (GTK_TREE_VIEW(selection_tree));

Then I got a false in return … so I think there is a bug here, and somehow I even tried to enforce
the header not to be clickable using:

gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(selection_tree), FALSE);

but it does not work, and I think this is why I have some troubles with my treeview selection.
Can anyone tell me where I could report this bug ?

Thanks in advance.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.