GTK程序demo,带工具栏和状态栏

main.c源码:

#include <gtk/gtk.h>

/*
 *@Description:从一个图片文件获取信息得到pixbuf
 *@param: gchar filename
 */

GdkPixbuf *create_pixbuf(const gchar* filename)
{
	GdkPixbuf *pixbuf;
	GError *error = NULL;
	pixbuf = gdk_pixbuf_new_from_file(filename,&error);
	if(!pixbuf){
		fprintf(stderr,"%s\n",error->message);
		g_error_free(error);
	}
	return pixbuf;
}

void menu_pressed(GtkWidget *widget,gpointer window)
{
	gchar *str;
	str = g_strdup_printf("you clicked one menu item");
	gtk_statusbar_push(GTK_STATUSBAR(window),gtk_statusbar_get_context_id(GTK_STATUSBAR(window),str),str);
	g_free(str);
}

//根据菜单栏的选择(ckeck) 控制一些构建的显示和隐藏
void toggle_display(GtkWidget *widget,gpointer oneofwidget)
{
	if(gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))){
		gtk_widget_show(oneofwidget);
	}else{
		gtk_widget_hide(oneofwidget);
	}
}

/*选择(Check)菜单选项(menu item)*/
int main(int argc,char *argv[])
{
	GtkWidget *window;
	GtkWidget *vbox;	//盒装容器
	GtkWidget *menubar;	//菜单栏
	GtkWidget *menutoggle,*menu_tog_toggle,*menu_tog_toolbar,*menu_tog_statusbar;	//界面开关菜单
	GtkWidget *toolbar;	//工具栏
	GtkToolItem *tool_exit,*tool_sep,*tool_about;

	GtkWidget *statusbar;	//状态栏

	gtk_init(&argc,&argv);
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(window),"一步一步学习GTK+ DEMO");
	gtk_window_set_default_size(GTK_WINDOW(window),500,400);
	gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
	gtk_window_set_icon(GTK_WINDOW(window),create_pixbuf("/usr/share/icons/mate/48x48/apps/web-browser.png"));

	vbox = gtk_vbox_new(FALSE,0);
	gtk_container_add(GTK_CONTAINER(window),vbox);

	menubar = gtk_menu_bar_new();	//代表整个菜单,是一个menu shell
	menutoggle = gtk_menu_new();	//这里代表一列菜单toglle,也是一个menu shell
	menu_tog_toggle = gtk_menu_item_new_with_label("View");
//	menu_tog_toolbar = gtk_menu_item_new_with_label("show Toolbar");
//	menu_tog_statusbar = gtk_menu_item_new_with_label("show Statusbar");

	menu_tog_toolbar = gtk_check_menu_item_new_with_label("show Toolbar");
	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_tog_toolbar),TRUE);
	menu_tog_statusbar = gtk_check_menu_item_new_with_label("show Statusbar");
	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_tog_statusbar),TRUE);

	gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_tog_toggle),menutoggle);	//View菜单加入 menutoggle menu shell
	gtk_menu_shell_append(GTK_MENU_SHELL(menutoggle),menu_tog_toolbar);	//View菜单加入 menutoggle menu shell
	gtk_menu_shell_append(GTK_MENU_SHELL(menutoggle),menu_tog_statusbar);	//View菜单加入 menutoggle menu shell
	gtk_menu_shell_append(GTK_MENU_SHELL(menubar),menu_tog_toggle);

	//创建工具栏
	toolbar = gtk_toolbar_new();
	gtk_toolbar_set_style(GTK_TOOLBAR(toolbar),GTK_TOOLBAR_ICONS);//设置工具栏样式为图标
	gtk_container_set_border_width(GTK_CONTAINER(toolbar),0);	//工具栏边框大小
	tool_exit = gtk_tool_button_new_from_stock(GTK_STOCK_QUIT);	//工具栏中的"退出"按钮
	gtk_toolbar_insert(GTK_TOOLBAR(toolbar),tool_exit,-1);
	tool_sep = gtk_separator_tool_item_new();					//工具栏中的分割线
	gtk_toolbar_insert(GTK_TOOLBAR(toolbar),tool_sep,-1);
	tool_about = gtk_tool_button_new_from_stock(GTK_STOCK_HELP);//工具栏中的"关于"按钮
	gtk_toolbar_insert(GTK_TOOLBAR(toolbar),tool_about,-1);

	statusbar = gtk_statusbar_new();
	gtk_box_pack_start(GTK_BOX(vbox),menubar,FALSE,FALSE,1);
	//把工具栏加入盒子容器
	gtk_box_pack_start(GTK_BOX(vbox),toolbar,FALSE,FALSE,0);
	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(menu_tog_toolbar),"activate",G_CALLBACK(menu_pressed),G_OBJECT(statusbar));
	g_signal_connect(G_OBJECT(menu_tog_statusbar),"activate",G_CALLBACK(menu_pressed),G_OBJECT(statusbar));
	g_signal_connect(G_OBJECT(tool_exit),"clicked",G_CALLBACK(gtk_main_quit),NULL);
	g_signal_connect(G_OBJECT(menu_tog_toolbar),"activate",G_CALLBACK(toggle_display),toolbar);
	g_signal_connect(G_OBJECT(menu_tog_statusbar),"activate",G_CALLBACK(toggle_display),statusbar);

	gtk_widget_show_all(window);
	gtk_main();
	return 0;
}

Makefile的内容:

all:demo7

OBJECTS:=gtk.o
TARGET:=demo7
CFLAGS:=$(shell pkg-config --cflags --libs gtk+-2.0)

$(TARGET):$(OBJECTS)
	gcc $^ -o $@ $(CFLAGS)
	
clean:
	rm $(OBJECTS) $(TARGET)
阅读更多
个人分类: 软件配置
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

GTK程序demo,带工具栏和状态栏

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭