- 论坛徽章:
- 0
|
#define MINWIDTH 320
#define MINHEIGHT 200
GtkWidget *board =0;
GdkPixbuf * tilepixbuf = NULL;
GtkWidget *mainWin =0;
static void expose_board (GtkWidget *w, GdkEventExpose *e, gpointer data)
{
int width = gdk_pixbuf_get_width (tilepixbuf);
int height = gdk_pixbuf_get_height (tilepixbuf);
gdk_draw_pixbuf(board->window,board->style->fg_gc[GTK_STATE_NORMAL],tilepixbuf,
0,0,0,0,width,height,GDK_RGB_DITHER_NORMAL,0,0);
// gtk_widget_queue_draw (board);
}
static void configure_board (GtkWidget *w, GdkEventConfigure *e, gpointer data)
{
if(board->allocation.width <10 || board->allocation.height <10)
return;
printf("tilepixbuf->ref_count =%d\n",((GObject*)tilepixbuf)->ref_count);
//while(((GObject*)tilepixbuf)->ref_count > 0)
// g_object_unref(tilepixbuf);
g_object_unref(tilepixbuf);//这里想释放tilepixbuf
printf("tilepixbuf->ref_count =%d\n",((GObject*)tilepixbuf)->ref_count);
tilepixbuf = gdk_pixbuf_new_from_file_at_size("postmodern.svg",board->allocation.width,board->allocation.height,NULL);
int width = gdk_pixbuf_get_width (tilepixbuf);
int height = gdk_pixbuf_get_height (tilepixbuf);
gdk_draw_pixbuf(board->window,board->style->fg_gc[GTK_STATE_NORMAL],tilepixbuf,
0,0,0,0,width,height,GDK_RGB_DITHER_NORMAL,0,0);
gtk_widget_queue_draw (board);
printf("configure_board , width =%d,height =%d\n",board->allocation.width,board->allocation.height);
}
gboolean load_images (gchar * file)
{
mainWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (mainWin), "test");
gtk_window_set_default_size (GTK_WINDOW (mainWin), 800, 600);
board = gtk_drawing_area_new ();
gtk_widget_show(board);
gtk_container_add(mainWin,board);
gtk_widget_show(mainWin);
int width =0;
int height =0;
GError ge;
memset(&ge,0,sizeof(GError));
tilepixbuf = gdk_pixbuf_new_from_file_at_size(file,board->allocation.width,board->allocation.height,&ge);
printf("tilepixbuf->ref_count =%d,GError=%s\n",((GObject*)tilepixbuf)->ref_count,ge.message);//这里奇怪,引用计数远大于1
// tilepixbuf = gdk_pixbuf_new_from_file (file, NULL);
//recreate_tile_images ();
// gtk_widget_set_size_request (board, MINWIDTH, MINHEIGHT);
width = gdk_pixbuf_get_width (tilepixbuf);
height = gdk_pixbuf_get_height (tilepixbuf);
gdk_draw_pixbuf(board->window,board->style->fg_gc[GTK_STATE_NORMAL],tilepixbuf,
0,0,0,0,width,height,GDK_RGB_DITHER_NORMAL,0,0);
// gtk_widget_add_events (board, GDK_BUTTON_PRESS_MASK);
g_signal_connect (G_OBJECT (board), "expose_event",
G_CALLBACK (expose_board), NULL);
g_signal_connect (G_OBJECT (board), "configure_event",
G_CALLBACK (configure_board), NULL);
// g_signal_connect (G_OBJECT (board), "button_press_event",
// G_CALLBACK (board_click), NULL);
return TRUE;
}
int main(int argc,char** argv)
{
// gtk_set_locale ();
gtk_init (&argc, &argv);
// if(!g_thread_supported())
// {
// g_thread_init(NULL);
// }
load_images("ppp.svg");
// gdk_threads_enter();
gtk_main();
// gdk_threads_leave();
return 0;
} |
|