当前位置: 技术问答>linux和unix
GtkToggleToolButton 如何响应右键事件?
来源: 互联网 发布时间:2016-05-18
本文导语: 想弹出个右键菜单, toggled 消息能响应, button_release_event不响应, 请问是怎么回事呢?谢谢 | 你为什么不用GtkNotebook来做阿,监控父控件的button-press然后判断widget是否是这个toolbutton. 搞的我...
想弹出个右键菜单,
toggled 消息能响应,
button_release_event不响应,
请问是怎么回事呢?谢谢
toggled 消息能响应,
button_release_event不响应,
请问是怎么回事呢?谢谢
|
你为什么不用GtkNotebook来做阿,监控父控件的button-press然后判断widget是否是这个toolbutton.
搞的我还得帮你调试好!!!
因为toolbutton是属于GtkBin,所有我们可以取得它的唯一的子控件,toolbutton响应不了右键,
但它的子键盘就应该能了。
搞的我还得帮你调试好!!!
因为toolbutton是属于GtkBin,所有我们可以取得它的唯一的子控件,toolbutton响应不了右键,
但它的子键盘就应该能了。
#include
#include
#include
GtkWidget* create_window1 (void);
gboolean
on_toggletoolbutton1_button_press_event
(GtkWidget *widget,
GdkEventButton *event,
gpointer user_data);
//main.c
int main (int argc, char *argv[])
{
GtkWidget *window1;
gchar *pixmap_dir;
#ifdef G_OS_WIN32
package_prefix = g_win32_get_package_installation_directory (NULL, NULL);
package_data_dir = g_build_filename (package_prefix, "share", NULL);
package_locale_dir = g_build_filename (package_prefix, "share", "locale", NULL);
#endif
#ifdef ENABLE_NLS
bindtextdomain (GETTEXT_PACKAGE, package_locale_dir);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
#endif
gtk_set_locale ();
gtk_init (&argc, &argv);
pixmap_dir = g_build_filename ("/usr/share", "pixmaps", NULL);
// add_pixmap_directory (pixmap_dir);
g_free (pixmap_dir);
/*
* The following code was added by Glade to create one of each component
* (except popup menus), just so that you see something after building
* the project. Delete any components that you don't want shown initially.
*/
window1 = create_window1 ();
gtk_widget_show (window1);
g_signal_connect ((gpointer) window1, "destroy", G_CALLBACK(gtk_main_quit),
NULL);
gtk_main ();
#ifdef G_OS_WIN32
g_free (package_prefix);
g_free (package_data_dir);
g_free (package_locale_dir);
#endif
return 0;
}
//interface.c
GtkWidget*
create_window1 (void)
{
GtkWidget *window1;
GtkWidget *vbox1;
GtkWidget *toolbar1;
GtkIconSize tmp_toolbar_icon_size;
GtkWidget *toggletoolbutton1;
window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window1), ("window1"));
vbox1 = gtk_vbox_new (FALSE, 0);
gtk_widget_show (vbox1);
gtk_container_add (GTK_CONTAINER (window1), vbox1);
toolbar1 = gtk_toolbar_new ();
gtk_widget_show (toolbar1);
gtk_box_pack_start (GTK_BOX (vbox1), toolbar1, FALSE, FALSE, 0);
gtk_toolbar_set_style (GTK_TOOLBAR (toolbar1), GTK_TOOLBAR_BOTH);
tmp_toolbar_icon_size = gtk_toolbar_get_icon_size (GTK_TOOLBAR (toolbar1));
toggletoolbutton1 = (GtkWidget*) gtk_toggle_tool_button_new ();
gtk_tool_button_set_label (GTK_TOOL_BUTTON (toggletoolbutton1), ("toggle1"));
gtk_widget_show (toggletoolbutton1);
gtk_container_add (GTK_CONTAINER (toolbar1), toggletoolbutton1);
GtkWidget *test1;
test1 = gtk_bin_get_child(toggletoolbutton1);
g_signal_connect ((gpointer) test1, "button_press_event",
G_CALLBACK (on_toggletoolbutton1_button_press_event),
NULL);
/* Store pointers to all widgets, for use by lookup_widget(). */
//GLADE_HOOKUP_OBJECT_NO_REF (window1, window1, "window1");
//GLADE_HOOKUP_OBJECT (window1, vbox1, "vbox1");
//GLADE_HOOKUP_OBJECT (window1, toolbar1, "toolbar1");
//GLADE_HOOKUP_OBJECT (window1, toggletoolbutton1, "toggletoolbutton1");
return window1;
}
//callback.c
gboolean
on_toggletoolbutton1_button_press_event
(GtkWidget *widget,
GdkEventButton *event,
gpointer user_data)
{
//响应右键菜单
if(event->type == GDK_BUTTON_PRESS && event->button == 3){ //鼠标右键
printf("button pressn");
GtkItemFactoryEntry menu_items[] = {
{"/menu1",NULL,NULL,0,NULL},
{"/menu2",NULL,NULL,0,NULL},
{"/menu3",NULL,NULL,0,NULL},
{"/menu4",NULL,NULL,0,NULL},
{"/menu5",NULL,NULL,0,NULL},
};
int nmenu_items;
GtkItemFactory *item_factory;
nmenu_items = sizeof(menu_items)/sizeof(menu_items[0]);
item_factory = gtk_item_factory_new(GTK_TYPE_MENU,"",NULL);
//设置CALLBACK类型为GtkItemFactoryCallback2
gtk_item_factory_create_items_ac(item_factory,nmenu_items,menu_items,NULL,2);
gtk_item_factory_popup(item_factory,event->x,event->y,event->button,event->time);
}
return FALSE;
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。