如何在工具栏中为GtkToolItem创建监听器?

时间:2012-12-16 11:50:25

标签: c gtk2

我创建了用C编码的GTK应用程序。

我添加了工具栏:

enter image description here

这是相关的代码片段:

GtkWidget *toolbar;
GtkToolItem *new;
GtkToolItem *open;
GtkToolItem *save;
GtkToolItem *sep;
GtkToolItem *exit;





...
toolbar = gtk_toolbar_new();    
gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);

gtk_container_set_border_width(GTK_CONTAINER(toolbar), 2);

new = gtk_tool_button_new_from_stock(GTK_STOCK_NEW);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), new, -1);

open = gtk_tool_button_new_from_stock(GTK_STOCK_OPEN);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), open, -1);

save = gtk_tool_button_new_from_stock(GTK_STOCK_SAVE);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), save, -1);

sep = gtk_separator_tool_item_new();
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), sep, -1); 

exit = gtk_tool_button_new_from_stock(GTK_STOCK_QUIT);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), exit, -1); 


....



gtk_widget_show_all (window);

问题是,当我按下退出/打开时如何将监听器(回调)添加到工具栏...

对于单个按钮,我有类似的东西:

g_signal_connect (G_OBJECT (_button), "clicked", G_CALLBACK (on_button_clicked), NULL);

void on_button_clicked(){...}

我知道通过gtk_ui_manager做到这一点的方法,但它太复杂了。

请帮助

1 个答案:

答案 0 :(得分:1)

它在主窗口?

g_signal_connect(G_OBJECT(exit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

更多信息:http://zetcode.com/tutorials/gtktutorial/menusandtoolbars/