testpixbuf-drawable.c 3.75 KB
Newer Older
1
#include <config.h>
2
#include <gtk/gtk.h>
3
#ifdef GDK_WINDOWING_X11
4
#include <gdk/x11/gdkx.h>
5 6
#elif defined (GDK_WINDOWING_WIN32)
#include <gdk/win32/gdkwin32.h>
7 8
#elif defined (GDK_WINDOWING_FB)
#include <gdk/linux-fb/gdkfb.h>
9
#endif
10

11
int close_app(GtkWidget *widget, gpointer data)
12 13
{
   gtk_main_quit();
14
   return TRUE;
15 16
}

17
int expose_cb(GtkWidget *drawing_area, GdkEventExpose *evt, gpointer data)
18 19 20 21 22
{
   GdkPixbuf *pixbuf;
         
   pixbuf = (GdkPixbuf *) gtk_object_get_data(GTK_OBJECT(drawing_area),
                                              "pixbuf");
23
   if(gdk_pixbuf_get_has_alpha (pixbuf))
24 25 26 27 28 29 30
   {
      gdk_draw_rgb_32_image(drawing_area->window,
                            drawing_area->style->black_gc,
                            evt->area.x, evt->area.y,
                            evt->area.width,
                            evt->area.height,
                            GDK_RGB_DITHER_MAX,
31 32 33 34
                            gdk_pixbuf_get_pixels (pixbuf) +
			    (evt->area.y * gdk_pixbuf_get_rowstride (pixbuf)) +
			    (evt->area.x * gdk_pixbuf_get_n_channels (pixbuf)),
                            gdk_pixbuf_get_rowstride (pixbuf));
35 36 37 38
   }
   else
   {
      gdk_draw_rgb_image(drawing_area->window, 
39 40 41 42 43
			 drawing_area->style->black_gc, 
			 evt->area.x, evt->area.y,
			 evt->area.width,
			 evt->area.height,  
			 GDK_RGB_DITHER_NORMAL,
44 45 46 47
			 gdk_pixbuf_get_pixels (pixbuf) +
			 (evt->area.y * gdk_pixbuf_get_rowstride (pixbuf)) +
			 (evt->area.x * gdk_pixbuf_get_n_channels (pixbuf)),
			 gdk_pixbuf_get_rowstride (pixbuf));
48
   }
49
   return FALSE;
50
}
51

52
int configure_cb(GtkWidget *drawing_area, GdkEventConfigure *evt, gpointer data)
53
{
54
   GdkPixbuf *pixbuf;
55 56 57 58 59
                           
   pixbuf = (GdkPixbuf *) gtk_object_get_data(GTK_OBJECT(drawing_area),   
                                              "pixbuf");
    
   g_print("X:%d Y:%d\n", evt->width, evt->height);
60
   if(evt->width != gdk_pixbuf_get_width (pixbuf) || evt->height != gdk_pixbuf_get_height (pixbuf))
61 62 63 64 65 66 67 68 69 70 71 72
   {
      GdkWindow *root;
      GdkPixbuf *new_pixbuf;

      root = GDK_ROOT_PARENT();
      new_pixbuf = gdk_pixbuf_get_from_drawable(NULL, root, NULL,
						0, 0, 0, 0, evt->width, evt->height);
      gtk_object_set_data(GTK_OBJECT(drawing_area), "pixbuf", new_pixbuf);
      gdk_pixbuf_unref(pixbuf);
   }

   return FALSE;
73 74
}

Owen Taylor's avatar
Owen Taylor committed
75 76
extern void pixbuf_init();

77 78 79 80 81 82 83 84
int main(int argc, char **argv)
{   
   GdkWindow     *root;
   GtkWidget     *window;
   GtkWidget     *vbox;
   GtkWidget     *drawing_area;
   GdkPixbuf     *pixbuf;    
   
Owen Taylor's avatar
Owen Taylor committed
85 86
   pixbuf_init ();

87 88 89
   gtk_init(&argc, &argv);   
   gdk_rgb_set_verbose(TRUE);

90
   gtk_widget_set_default_colormap(gdk_rgb_get_colormap());
91

92
   root = GDK_ROOT_PARENT();
93 94
   pixbuf = gdk_pixbuf_get_from_drawable(NULL, root, NULL,
					 0, 0, 0, 0, 150, 160);
95 96 97 98 99 100 101 102 103 104 105
   
   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_signal_connect(GTK_OBJECT(window), "delete_event",
                      GTK_SIGNAL_FUNC(close_app), NULL);
   gtk_signal_connect(GTK_OBJECT(window), "destroy",   
                      GTK_SIGNAL_FUNC(close_app), NULL);
   
   vbox = gtk_vbox_new(FALSE, 0);
   gtk_container_add(GTK_CONTAINER(window), vbox);  
   
   drawing_area = gtk_drawing_area_new();
Havoc Pennington's avatar
Havoc Pennington committed
106 107 108
   gtk_widget_set_size_request (GTK_WIDGET(drawing_area),
                                gdk_pixbuf_get_width (pixbuf),
                                gdk_pixbuf_get_height (pixbuf));
109 110 111 112 113 114 115 116 117 118
   gtk_signal_connect(GTK_OBJECT(drawing_area), "expose_event",
                      GTK_SIGNAL_FUNC(expose_cb), NULL);

   gtk_signal_connect(GTK_OBJECT(drawing_area), "configure_event",
                      GTK_SIGNAL_FUNC(configure_cb), NULL);
   gtk_object_set_data(GTK_OBJECT(drawing_area), "pixbuf", pixbuf);
   gtk_box_pack_start(GTK_BOX(vbox), drawing_area, TRUE, TRUE, 0);
   
   gtk_widget_show_all(window);
   gtk_main();
119
   return 0;
120
}