注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

随机过程

http://superware.blog.163.com

 
 
 

日志

 
 

一个简单的 GTK 框架  

2014-07-23 01:30:36|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

#include <gtk/gtk.h>

typedef struct
{
GtkWidget *menu;
GtkWidget *file;
GtkWidget *save;
GtkWidget *open;
GtkWidget *quit;
} FileMenuWidget;

typedef struct
{
GtkWidget *menu;
GtkWidget *edit;
GtkWidget *copy;
GtkWidget *paste;
GtkWidget *remove;
} EditMenuWidget;

typedef struct
{
FileMenuWidget *file;
EditMenuWidget *edit;
} MenuWidget;

typedef struct
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *menubar;
GtkWidget *toolbar;

MenuWidget *menu;

GtkWidget *scrwin;
GtkWidget *view;

GtkTextBuffer *buffer;
} MyEditorWin;

void myeditor_init_main_widget(MyEditorWin *mywin)
{
mywin->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(mywin->window), "Text Views");
gtk_container_border_width(GTK_CONTAINER(mywin->window), 0);
gtk_widget_set_size_request(mywin->window, 400, 320);
}

void myeditor_create_vbox(MyEditorWin *mywin)
{
mywin->vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(mywin->window), mywin->vbox);
}

void myeditor_create_menubar(MyEditorWin *mywin)
{
mywin->menubar = gtk_menu_bar_new();
gtk_box_pack_start(GTK_BOX(mywin->vbox), mywin->menubar, FALSE, FALSE, 0);
}

void myeditor_create_scrwin(MyEditorWin *mywin)
{
mywin->scrwin = gtk_scrolled_window_new(NULL, NULL);
gtk_box_pack_start(GTK_BOX(mywin->vbox), mywin->scrwin, TRUE, TRUE, 0);
}

void myeditor_create_view(MyEditorWin *mywin)
{
mywin->view = gtk_text_view_new();
gtk_container_add(GTK_CONTAINER(mywin->scrwin), mywin->view);
}

void myeditor_create_buffer(MyEditorWin *mywin)
{
mywin->buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(mywin->view));
gtk_text_buffer_set_text(mywin->buffer, "Cold was my soul\nUntold was the pain\nCold was my soul\n", -1);
}

void myeditor_create_other_widgets(MyEditorWin *mywin)
{
myeditor_create_vbox(mywin);
myeditor_create_menubar(mywin);
myeditor_create_scrwin(mywin);
myeditor_create_view(mywin);
myeditor_create_buffer(mywin);
}

void myeditor_create_file_menu_widgets(MyEditorWin *mywin)
{
mywin->menu->file->file = gtk_menu_item_new_with_label("File");
gtk_menu_shell_append(GTK_MENU_SHELL(mywin->menubar), mywin->menu->file->file);
mywin->menu->file->menu = gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(mywin->menu->file->file), mywin->menu->file->menu);
mywin->menu->file->save = gtk_menu_item_new_with_label("Save");
gtk_menu_shell_append(GTK_MENU_SHELL(mywin->menu->file->menu), mywin->menu->file->save);
mywin->menu->file->open = gtk_menu_item_new_with_label("Open");
gtk_menu_shell_append(GTK_MENU_SHELL(mywin->menu->file->menu), mywin->menu->file->open);
mywin->menu->file->quit = gtk_menu_item_new_with_label("Quit");
gtk_menu_shell_append(GTK_MENU_SHELL(mywin->menu->file->menu), mywin->menu->file->quit);
}

void myeditor_create_edit_menu_widgets(MyEditorWin *mywin)
{
mywin->menu->edit->edit = gtk_menu_item_new_with_label("Edit");
gtk_menu_shell_append(GTK_MENU_SHELL(mywin->menubar), mywin->menu->edit->edit);
mywin->menu->edit->menu = gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(mywin->menu->edit->edit), mywin->menu->edit->menu);
mywin->menu->edit->copy = gtk_menu_item_new_with_label("Copy");
gtk_menu_shell_append(GTK_MENU_SHELL(mywin->menu->edit->menu), mywin->menu->edit->copy);
mywin->menu->edit->remove = gtk_menu_item_new_with_label("Remove");
gtk_menu_shell_append(GTK_MENU_SHELL(mywin->menu->edit->menu), mywin->menu->edit->remove);
mywin->menu->edit->paste = gtk_menu_item_new_with_label("Paste");
gtk_menu_shell_append(GTK_MENU_SHELL(mywin->menu->edit->menu), mywin->menu->edit->paste);
}

void myeditor_create_menu_widgets(MyEditorWin *mywin)
{
myeditor_create_file_menu_widgets(mywin);
myeditor_create_edit_menu_widgets(mywin);
}

void myeditor_show_main_window(MyEditorWin *mywin)
{
gtk_widget_show_all(mywin->window);
}

void myeditor_create_callbacks(MyEditorWin *mywin)
{
g_signal_connect(G_OBJECT(mywin->menu->file->quit), "activate", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(mywin->window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
}

void myeditor_create_main_windows(MyEditorWin *mywin)
{
myeditor_init_main_widget(mywin);
myeditor_create_other_widgets(mywin);
myeditor_create_menu_widgets(mywin);
myeditor_create_callbacks(mywin);
myeditor_show_main_window(mywin);
}

int main (int argc, char *argv[])
{
MyEditorWin *mywin;
mywin = (MyEditorWin *)g_malloc(sizeof(MyEditorWin));
mywin->menu = (MenuWidget *)g_malloc(sizeof(MenuWidget));
mywin->menu->file = (FileMenuWidget *)g_malloc(sizeof(FileMenuWidget));
mywin->menu->edit = (EditMenuWidget *)g_malloc(sizeof(FileMenuWidget));
gtk_init(&argc, &argv);
myeditor_create_main_windows(mywin);
gtk_main();
g_free(mywin->menu->edit);
g_free(mywin->menu->file);
g_free(mywin->menu);
g_free(mywin);
return 0;
}

  评论这张
 
阅读(92)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017