用户名: 密码: 忘记密码? 注册
收藏此问题 发表新评论

关于gtk和glade,请高手指点

#include <gtk/gtk.h>
#include <glib.h>

int flag=1;
typedef struct
{
guint no;
gchar packtype[10];
gchar srcmac[100];
gchar srcip[100];
guint srcport;
gchar destmac[100];
gchar destip[100];
guint destport;
guint datalength;
}Pack;

enum
{
COLUMN_NO,
COLUMN_PACKTYPE,
COLUMN_SRCMAC,
COLUMN_SRCIP,
COLUMN_SRCPORT,
COLUMN_DESTMAC,
COLUMN_DESTIP,
COLUMN_DESTPORT,
COLUMN_DATALENGTH,
NUM_COLUMNS
};


static GtkWidget* treeview;
static Pack pack={2,"UDP", "S3-JS-3J-4G-43-34","123.342.342.343",324324,"F3-DS-32-4G-43-34","234.432.53.324", 435, 34};


static void init_list(GtkTreeView* treeview)
{
GtkCellRenderer* cell_renderer;
GtkTreeViewColumn* column;
GtkListStore *store;

/*在treeview中添加一新列,并设置其属性*/
cell_renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes( "NO",
cell_renderer,
"text",
COLUMN_NO,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

cell_renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes( "PACK TYPE",
cell_renderer,
"text",
COLUMN_PACKTYPE,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

cell_renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes( " SOURCE MAC",
cell_renderer,
"text",
COLUMN_SRCMAC,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

cell_renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes( " SOURCE IP",
cell_renderer,
"text",
COLUMN_SRCIP,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

cell_renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes( "SRCPORT",
cell_renderer,
"text",
COLUMN_SRCPORT,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

cell_renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes( " DEST MAC",
cell_renderer,
"text",
COLUMN_DESTMAC,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

cell_renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes( " DEST IP",
cell_renderer,
"text",
COLUMN_DESTIP,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

cell_renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes( "DESTPORT",
cell_renderer,
"text",
COLUMN_DESTPORT,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

cell_renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes( "DATASIZE",
cell_renderer,
"text",
COLUMN_DATALENGTH,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

/*创建list store*/
store = gtk_list_store_new (NUM_COLUMNS,
G_TYPE_UINT,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_UINT,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_UINT,
G_TYPE_UINT);

gtk_tree_view_set_model(GTK_TREE_VIEW (treeview), GTK_TREE_MODEL(store));
g_object_unref(store);
return;
}

static void add_to_list(GtkTreeView* treeview, Pack data)
{
GtkListStore *store;
GtkTreeIter iter;
store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(treeview)));
gtk_list_store_append(store, &iter);
gtk_list_store_set (store, &iter,
COLUMN_NO, data.no,
COLUMN_PACKTYPE, data.packtype,
COLUMN_SRCMAC, data.srcmac,
COLUMN_SRCIP, data.srcip,
COLUMN_SRCPORT, data.srcport,
COLUMN_DESTMAC, data.destmac,
COLUMN_DESTIP, data.destip,
COLUMN_DESTPORT,data.destport,
COLUMN_DATALENGTH,data.datalength,
-1);
}




static void start(GtkWidget *widget,gpointer data)
{
g_print("Start\n");
flag=0;
while(flag==0)
{
add_to_list(GTK_TREE_VIEW (treeview),pack);
}

}

static void stop(GtkWidget *widget,gpointer data)
{
g_print("Stop\n");
flag=1;
}


int main (int argc, char* argv[])
{
GtkWidget* window;
GtkWidget* vbox,*hbox;
GtkWidget* button;
GtkWidget* scrolledwindow;
//static GtkWidget* treeview;

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Sniffer");

vbox = gtk_vbox_new (FALSE, 5);
gtk_widget_show (vbox);
gtk_container_add (GTK_CONTAINER (window), vbox);

hbox=gtk_hbox_new(FALSE,30);
//gtk_container_add(GTK_CONTAINER(vbox),hbox);
gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,TRUE,0);

button=gtk_button_new_with_label("Start");
gtk_signal_connect(GTK_OBJECT(button),"clicked",G_CALLBACK(start),NULL);
gtk_box_pack_start(GTK_BOX(hbox),button,FALSE,TRUE,0);

button=gtk_button_new_with_label("Stop");
gtk_signal_connect(GTK_OBJECT(button),"clicked",G_CALLBACK(stop),NULL);
gtk_box_pack_start(GTK_BOX(hbox),button,FALSE,TRUE,0);

scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_show (scrolledwindow);
gtk_box_pack_start (GTK_BOX (vbox),scrolledwindow, TRUE, TRUE, 0);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow), GTK_SHADOW_IN);

treeview = gtk_tree_view_new ();
//gtk_widget_show (treeview);
gtk_container_add (GTK_CONTAINER (scrolledwindow), treeview);

/*设置为FALSE,headers隐藏*/
gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), TRUE);

init_list(GTK_TREE_VIEW (treeview));

/*treeview连接model,绑定model和view*/

gtk_window_set_default_size (GTK_WINDOW (window), 850, 400);
gtk_widget_show_all(window);

gtk_main ();
return 0;
}
上面是我写源程序,本来想实现功能:按buttonstart会不停的在显示框里显示数据pack,按button stop会停止显示。其中button start回调函数如下,
static void start(GtkWidget *widget,gpointer data)
{
g_print("Start\n");
flag=0;
while(flag==0)
{
add_to_list(GTK_TREE_VIEW (treeview),pack);
}

}使用while就是不在显示框里面显示数据,程序就好象死了一样,第一次学习写的GTK,不知道哪里出问题了,难道是要用多线程?
求高手指导。。
昵称: leebug  时间: 2011-06-07 00:39:00
发表评论
昵称:
内容:
验证: