Chinaunix首页 | 论坛 | 博客
  • 博客访问: 191069
  • 博文数量: 49
  • 博客积分: 2065
  • 博客等级: 大尉
  • 技术积分: 413
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-08 17:04
文章分类

全部博文(49)

文章存档

2012年(2)

2010年(17)

2009年(30)

我的朋友

分类:

2009-10-27 14:10:35

#include

#define FILENAME "test.png"

GtkWidget *create_view( void )
{

    GtkTreeModel *model;
    GtkWidget *view;
    GtkCellRenderer *cell;

    GtkTreeIter iter;

    model = GTK_TREE_MODEL(gtk_list_store_new( 1 , G_TYPE_STRING));

    view = gtk_tree_view_new_with_model(model);
    gtk_tree_view_set_reorderable(GTK_TREE_VIEW(view), TRUE );

    cell = gtk_cell_renderer_text_new();
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), - 1 , "String" , cell, "text" , 0 , NULL );

    gtk_list_store_append(GTK_LIST_STORE(model), &iter);
    gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0 , "text" , - 1 );

    return view;
}

gboolean expose_ev(GtkWidget *wi, GdkEventExpose *ev, gpointer user_data)
{

    GdkPixbuf *pbuf;
    GdkPixbuf *treebuf;
    GdkPixbuf *temp;
    GError *err;
    gint w, h;
    GdkWindow *binwin;

    /* the bin window is the part that shows the list */
    binwin = gtk_tree_view_get_bin_window(GTK_TREE_VIEW(wi));

    err = NULL ;

    /* open our pixbuf */
    pbuf = gdk_pixbuf_new_from_file(FILENAME, &err);
    if (err != NULL )
    {

        g_warning(err->message);
        g_error_free(err);
        pbuf = NULL ;
        return FALSE ;
    }

    /* get the width and height of the bin window */
    gdk_window_get_size(binwin, &w, &h);

    /* get a pixbuf of what the binwin looks like right now */
    treebuf = gdk_pixbuf_get_from_drawable( NULL , binwin, NULL , 0 , 0 , 0 , 0 , w, h);
    /* create a temporary pixbuf so that we can free it */
    temp = pbuf;
    /* scale our pixbuf to the size of the binwin */
    pbuf = gdk_pixbuf_scale_simple(pbuf, w, h, GDK_INTERP_BILINEAR);
    /* free the old pixbuf */
    g_object_unref(temp);
    /* composite the pbuf and treebuf together and store it in treebuf, 40 is the alpha */
    gdk_pixbuf_composite(pbuf, treebuf, 0 , 0 , w, h, 0 , 0 , 1 , 1 , GDK_INTERP_NEAREST, 40 );
    /* free pbuf */
    g_object_unref(pbuf);

    /* now update binwin with the new pixbuf */
    gdk_draw_pixbuf(binwin, NULL , treebuf, 0 , 0 , 0 , 0 , - 1 , - 1 , GDK_RGB_DITHER_NONE, 0 , 0 );

    /* free treebuf */
    g_object_unref(treebuf);

    return FALSE ;
}

int main( int argc, char **argv)
{

    GtkWidget *win;
    GtkWidget *view;

    gtk_init(&argc, &argv);

    win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(win, "delete-event" , G_CALLBACK(gtk_main_quit), NULL );

    view = create_view();
    /* note that we need to connect the signal after the default handler */
    g_signal_connect_after(view, "expose-event" , G_CALLBACK(expose_ev), NULL );
    gtk_container_add(GTK_CONTAINER(win), view);
    gtk_widget_show(view);

    gtk_widget_show(win);

    gtk_main();

    return 0 ;
}
阅读(1508) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~