#include<gtk/gtk.h>
static GtkWidget *entry;
void clocker_begin ()
{
time_t now;
struct tm *l_time;
gchar buf[100];
now=time((time_t *)NULL);
l_time=localtime(&now);
sprintf(buf,"%d:%d:%d",l_time->tm_hour,l_time->tm_min,l_time->tm_sec);
gtk_entry_set_text(GTK_ENTRY(entry),buf);
}
void on_menu_activate (GtkMenuItem* item,gpointer data)
{
g_print("menuitem %s is pressed",(gchar*)data);
}
int main(int argc,char *argv[])
{
GtkWidget *window;
GtkWidget *box;
GtkWidget *menubar;
GtkWidget *text;
GtkWidget *scrolledwin;
GtkWidget *toolbar;
GtkWidget *label;
GtkWidget *menu;
GtkWidget *editmenu;
GtkWidget *helpmenu;
GtkWidget *rootmenu;
GtkWidget *menuitem;
GtkAccelGroup *accel_group;
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"菜单测试程序");
gtk_container_set_border_width(GTK_CONTAINER(window),0);
gtk_window_set_default_size(GTK_WINDOW(window),500,400);
g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
box=gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window),box);
gtk_widget_show(box);
menu=gtk_menu_new();//文件菜单
menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_NEW,accel_group);
gtk_menu_shell_append(GTK_MENU_SHELL(menu),menuitem);
g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)("new"));
menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_OPEN,accel_group);
gtk_menu_shell_append(GTK_MENU_SHELL(menu),menuitem);
g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)("open"));
menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE,accel_group);
gtk_menu_shell_append(GTK_MENU_SHELL(menu),menuitem);
g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)("SAVE"));
menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE_AS,accel_group);
gtk_menu_shell_append(GTK_MENU_SHELL(menu),menuitem);
g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)("另存为"));
menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT,accel_group);
gtk_menu_shell_append(GTK_MENU_SHELL(menu),menuitem);
g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)("QUIT"));
rootmenu=gtk_menu_item_new_with_label("文件");
gtk_menu_item_set_submenu(GTK_MENU_ITEM(rootmenu),menu);
menubar=gtk_menu_bar_new();
gtk_menu_shell_append(GTK_MENU_SHELL(menubar),rootmenu);
editmenu=gtk_menu_new();//编辑菜单
menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_CUT,accel_group);
gtk_menu_shell_append(GTK_MENU_SHELL(editmenu),menuitem);
g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)("CUT"));
menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY,accel_group);
gtk_menu_shell_append(GTK_MENU_SHELL(editmenu),menuitem);
g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)("COPY"));
menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE,accel_group);
gtk_menu_shell_append(GTK_MENU_SHELL(editmenu),menuitem);
g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)("paste"));
menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_FIND,accel_group);
gtk_menu_shell_append(GTK_MENU_SHELL(editmenu),menuitem);
g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)("查找"));
rootmenu=gtk_menu_item_new_with_label("编辑");
gtk_menu_item_set_submenu(GTK_MENU_ITEM(rootmenu),editmenu);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar),rootmenu);
helpmenu=gtk_menu_new();//帮助菜单
menuitem=gtk_menu_item_new_with_label("关于");
gtk_menu_shell_append(GTK_MENU_SHELL(helpmenu),menuitem);
g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)("关于"));
rootmenu=gtk_menu_item_new_with_label("帮助");
gtk_menu_item_set_submenu(GTK_MENU_ITEM(rootmenu),helpmenu);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar),rootmenu);
gtk_box_pack_start(GTK_BOX(box),menubar,FALSE,FALSE,0);
rootmenu=gtk_menu_item_new_with_label("当前时间");
gtk_menu_shell_append(GTK_MENU_SHELL(menubar),rootmenu);
g_signal_connect(G_OBJECT(rootmenu),"activate",G_CALLBACK(clocker_begin),NULL);
// text=gtk_text_view_new();
//scrolledwin=gtk_scrolled_window_new(NULL,NULL);
//gtk_box_pack_start(GTK_BOX(box),scrolledwin,TRUE,TRUE,0);
//gtk_container_add(GTK_CONTAINER(scrolledwin),text);
//gtk_text_view_set_editable(GTK_TEXT_VIEW(text),TRUE);
toolbar=gtk_toolbar_new();
gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar),GTK_STOCK_NEW,"新建一个文件夹","新建",GTK_SIGNAL_FUNC(on_menu_activate),(gpointer)("新建"),-1);
gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar),GTK_STOCK_OPEN,"打开一个文件夹","打开",GTK_SIGNAL_FUNC(on_menu_activate),(gpointer)("打开"),-1);
gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar),GTK_STOCK_SAVE,"保存一个文件","保存",GTK_SIGNAL_FUNC(on_menu_activate),(gpointer)("保存"),-1);
gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
label=gtk_label_new("文件");
entry=gtk_entry_new();
gtk_toolbar_append_widget(GTK_TOOLBAR(toolbar),entry,"这是一个录入条","录入");
gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar),GTK_STOCK_CUT,"剪切","剪切",GTK_SIGNAL_FUNC(on_menu_activate),(gpointer)("剪切"),-1);
gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar),GTK_STOCK_COPY,"复制","复制",GTK_SIGNAL_FUNC(on_menu_activate),(gpointer)("复制"),-1);
gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar),GTK_STOCK_PASTE," 粘贴","粘贴",GTK_SIGNAL_FUNC(on_menu_activate),(gpointer)("粘贴"),-1);
gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar),GTK_STOCK_QUIT,"QUIT","QUIT",GTK_SIGNAL_FUNC(gtk_main_quit),(gpointer)("QUIT"),-1);
gtk_toolbar_set_style(GTK_TOOLBAR(toolbar),GTK_TOOLBAR_ICONS);
gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar),GTK_ICON_SIZE_SMALL_TOOLBAR);
gtk_box_pack_start(GTK_BOX(box),toolbar,FALSE,FALSE,0);
text=gtk_text_view_new();
scrolledwin=gtk_scrolled_window_new(NULL,NULL);
gtk_box_pack_start(GTK_BOX(box),scrolledwin,TRUE,TRUE,0);
gtk_container_add(GTK_CONTAINER(scrolledwin),text);
gtk_text_view_set_editable(GTK_TEXT_VIEW(text),TRUE);
gtk_widget_show_all(window);
gtk_main();
return FALSE;
}
图
2011年4月13日 11:21
哈哈哈哈哈