Cecylia
Cecylia

Reputation: 361

Context Menus on Right Click in GtkTreeView (only for specified column)

I want to show a popup menu if 'right click' in a row from GtkTreeView. It is possible that the popup menu shows up only if 'right click' in first column (or specificate column)?

I have use code but this sown up menu for entire row.

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;
}

I have GtkTreeView with 3 columns and I want to show up menu only for first column

Upvotes: 1

Views: 943

Answers (1)

user4815162342
user4815162342

Reputation: 154886

You are already calling gtk_tree_view_get_path_at_pos. This function can also obtain the treeview column under the mouse. Instead of passing NULL for the column argument, make sure to get the column and compare it to your desired column:

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;
}

Upvotes: 1

Related Questions