如果从GtkTreeView连续“右键单击”,我想显示一个弹出菜单。 只有在第一列(或特定列)中“右键单击”时,弹出菜单才会显示?
我已经使用了代码,但是整个行的播放菜单。
gboolean
on_tree_view_button_pressed(GtkWidget *treeview, GdkEventButton *event, gpointer data)
{
if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
GtkTreePath *path;
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview),
event->x, event->y,
&path, NULL, NULL, NULL)) {
gtk_tree_selection_unselect_all(selection);
gtk_tree_selection_select_path(selection, path);
gtk_tree_path_free(path);
}
do_popup_menu(treeview, event, data);
return TRUE;
}
return FALSE;
}
我有3列的GtkTreeView,我想只显示第一列的菜单
答案 0 :(得分:1)
您已拨打gtk_tree_view_get_path_at_pos
。此功能还可以获取鼠标下的树视图列。不要为column
参数传递NULL,而是确保获取列并将其与所需列进行比较:
if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
GtkTreePath *path;
GtkTreeViewColumn *column;
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview),
event->x, event->y,
&path, &column, NULL, NULL))
// if we can't find path at pos, we surely don't
// want to pop up the menu
return FALSE;
if (column != gtk_tree_view_get_column(GTK_TREE_VIEW(treeview), 0)) {
// wrong column, don't bother
gtk_tree_path_free(path);
return FALSE;
}
gtk_tree_selection_unselect_all(selection);
gtk_tree_selection_select_path(selection, path);
gtk_tree_path_free(path);
do_popup_menu(treeview, event, data);
return TRUE;
}