通过上一小节,基本上掌握了如何向窗口中添加菜单栏以及为menu_item添加快捷键,下面我们通过一个实例来创建选择菜单选项.GtkCHeckMenuItem就是一个可以生成带有选择的菜单选项.
下面看一下代码:
#include <gtk/gtk.h>
void toggle_statusbar(GtkWidget *widget,gpointer statusbar){
if(gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))){
gtk_widget_show(statusbar);
}else{
gtk_widget_hide(statusbar);
}
}
int main(int argc,char *argv[])
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *menubar;
GtkWidget *viewmenu;
GtkWidget *view;
GtkWidget *tog_stat;
GtkWidget *statusbar;
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window),250,200);
gtk_window_set_title(GTK_WINDOW(window),"view status bar");
vbox = gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window),vbox);
menubar = gtk_menu_bar_new();
viewmenu = gtk_menu_new();
view = gtk_menu_item_new_with_label("View");
tog_stat = gtk_check_menu_item_new_with_label("View Statusbar");
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(tog_stat),TRUE);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(view),viewmenu);
gtk_menu_shell_append(GTK_MENU_SHELL(viewmenu),tog_stat);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar),view);
gtk_box_pack_start(GTK_BOX(vbox),menubar,FALSE,FALSE,3);
statusbar = gtk_statusbar_new();
gtk_box_pack_end(GTK_BOX(vbox),statusbar,FALSE,TRUE,1);
g_signal_connect_swapped(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect(G_OBJECT(tog_stat),"activate",G_CALLBACK(toggle_statusbar),statusbar);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
Makefile文件同上面小节.首先,我们先看一下,运行之后的效果:
这个就是带有选项框的菜单项.
我们这个实例的意思是,当选中该菜单项的时候,显示状态栏,如果取消选中,则隐藏状态栏.
函数 gtk_check_menu_item_new_with_label() 可以生成一个新的带有选择框的菜
单选项。
好了,菜单栏的使用大致上就这些了,下面我们了解一下工具栏的使用方法.
#include <gtk/gtk.h>
int main(int argc,char *argv[])
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *toolbar;
GtkToolItem *new;
GtkToolItem *open;
GtkToolItem *save;
GtkToolItem *sep;
GtkToolItem *exit;
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window),250,200);
gtk_window_set_title(GTK_WINDOW(window),"toolbar");
vbox = gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window),vbox);
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_NEW);
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_box_pack_start(GTK_BOX(vbox),toolbar,FALSE,FALSE,5);
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);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
下面是工具栏运行之后的效果:
toolbar = gtk_toolbar_new();
gtk_toolbar_set_style(GTK_TOOLBAR(toolbar),GTK_TOOLBAR_ICONS);
从上面两行代码可以看出,生成一个带有图片显示的工具栏.
new = gtk_tool_button_new_from_stock(GTK_STOCK_NEW);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar),new,-1);
从stock中生成一个新的工具按钮,要想添加到工具栏中,只需要函数gtk_toolbar_insert()就可以.
sep = gtk_separator_tool_item_new();
gtk_toolbar_insert(GTK_TOOLBAR(toolbar),sep,-1);
该代码生成一个分割线把工具栏分开.
这样就生成了我们经常见到的工具栏了.
下面我们学习一个功能失效,也就是使工具栏中的一个按钮失效,也就是对该item无法操作.
下面请看一下代码:
#include <gtk/gtk.h>
#include <string.h>
void undo_redo(GtkWidget *widget,gpointer item){
static int count = 2;
const char *name = gtk_widget_get_name(widget);
if(strcmp(name,"undo")){
count++;
}else{
count--;
}
if(count < 0){
gtk_widget_set_sensitive(widget,FALSE);
gtk_widget_set_sensitive(item,TRUE);
}
if(count > 5){
gtk_widget_set_sensitive(widget,FALSE);
gtk_widget_set_sensitive(item,TRUE);
}
}
int main(int argc,char *argv[]){
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *toolbar;
GtkWidget *undo;
GtkWidget *redo;
GtkWidget *sep;
GtkWidget *exit;
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window),250,200);
gtk_window_set_title(GTK_WINDOW(window),"undoredo");
vbox = gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window),vbox);
toolbar = gtk_toolbar_new();
gtk_toolbar_set_style(GTK_TOOLBAR(toolbar),GTK_TOOLBAR_ICONS);
gtk_container_set_border_width(GTK_CONTAINER(toolbar),2);
undo = gtk_tool_button_new_from_stock(GTK_STOCK_UNDO);
gtk_widget_set_name(GTK_WIDGET(undo),"undo");
gtk_toolbar_insert(GTK_TOOLBAR(toolbar),undo,-1);
redo = gtk_tool_button_new_from_stock(GTK_STOCK_REDO);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar),redo,-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_box_pack_start(GTK_BOX(vbox),toolbar,FALSE,FALSE,5);
g_signal_connect(G_OBJECT(undo),"clicked",G_CALLBACK(undo_redo),redo);
g_signal_connect(G_OBJECT(redo),"clicked",G_CALLBACK(undo_redo),undo);
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);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
下面是运行之后的结果
gtk_widget_set_sensitive() 是被用来告诉计算机是否要击活一个工具栏按钮。
下面我们来看一下这个toolbar的一个官方api:
1): gtk_container_set_border_width(GTK_CONTAINER(toolbar),2);
函数原型:
void
gtk_container_set_border_width (GtkContainer *container,
guint border_width);
根据官方文档介绍:
Sets the border width of the container.
The border width of a container is the amount of space to leave around the outside of the container. The only exception to this is GtkWindow; because toplevel windows can’t leave space outside, they leave the space inside. The border is added on all sides of the container. To add space to only one side, use a specific “margin” property on the child widget, for example “margin-top”.
也就是说,该函数用于设置容器边界宽度.
该函数是设置所有的边界的宽度,如果想要仅仅设置一个边界的话,可以使用margin,例如”margin-top”.
注意:
由于我在学习GTK的时候,是看的别人的书籍,是基于2.0的,但是现在GTK版本已经升级到3.0了,很多方法都已经发生了改变,不过,这些方法仍然是可以使用的.
举个例子:
函数:
gtk_tool_button_new_from_stock ()
官方上是这样说的:
gtk_tool_button_new_from_stock has been deprecated since version 3.10 and should not be used in newly-written code.
Use gtk_tool_button_new() instead.
我们看一下函数gtk_tool_button_new()的使用
其函数原型为:
GtkToolItem *
gtk_tool_button_new (GtkWidget *icon_widget, const gchar *label);
使用icon_widget作为内容和使用label作为标签创建一个新的GtkToolButton
参数:
?label 一个作为标签的字符串或者为NULL
?icon_widget 一个将被用作按钮内容的widget,或者为NULL
返回
?一个新的GtkToolButton
从版本2.4开始使用
原文地址:http://blog.csdn.net/hongbochen1223/article/details/46427513