我試圖顯示一個上下文菜單,當一個GtkTreeView項目收到右擊。當我右鍵單擊該項目菜單顯示,但在我的GtkTreeView中的選擇消失,並且我希望顯示GtkMenu時我右鍵單擊選中的項目。gtk_menu_popup()刪除treeview選擇
菜單是GtkBuilt:
...
<object class="GtkMenu" id="menu_nf">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="abrir_nfe">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Abrir</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="emitir_nfe">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Emitir</property>
<property name="use_underline">True</property>
</object>
</child>
</object>
...
而這些是我使用的顯示菜單中的相關功能:
static gint popup_menu_nfe(GtkTreeView *t, GdkEventButton *e, gpointer *win){
gtk_menu_popup((LIVRENFE_WINDOW(win))->menu_nf, NULL,
NULL, NULL, NULL, e->button, e->time);
}
static gint nfe_context_menu_show(GtkTreeView *t, GdkEventButton *e,
gpointer win){
if(e->type == GDK_BUTTON_PRESS){
if(e->button == GDK_BUTTON_SECONDARY){
GtkTreeSelection *s;
GtkTreePath *p;
s = gtk_tree_view_get_selection(t);
if(gtk_tree_view_get_path_at_pos(t, e->x, e->y, &p,
NULL, NULL, NULL)){
gtk_tree_selection_unselect_all(s);
gtk_tree_selection_select_path(s, p);
gtk_tree_path_free(p);
}
popup_menu_nfe(t, e, win);
return TRUE;
}
}
return FALSE;
}
static gint nfe_on_popup(GtkTreeView *t, gpointer win){
popup_menu_nfe(t, NULL, win);
return TRUE;
}
static void livrenfe_window_init(LivrenfeWindow *win){
gtk_widget_init_template(GTK_WIDGET(win));
...
g_signal_connect((LIVRENFE_WINDOW(win))->treeview, "button-press-event",
G_CALLBACK(nfe_context_menu_show), win);
g_signal_connect((LIVRENFE_WINDOW(win))->treeview, "popup-menu",
G_CALLBACK(nfe_on_popup), win);
...
}
全部代碼是在這裏:https://github.com/pablogallardo/livrenfe/blob/master/src/lnfe_window.c
我在這裏跟着例子https://en.wikibooks.org/wiki/GTK%2B_By_Example/Tree_View/Events#Context_Menus_on_Right_Click
如果我將gtk_menu_popup()
呼叫註釋掉,當我右鍵單擊它們時,項目會被選中,但當我撥打gtk_menu_popup()
時,似乎我做錯了什麼。我也試着在之前撥打gtk_menu_popup()
我做了GtkTreeSelection部分,它不起作用。
有人能幫我嗎?
謝謝!