]> Pileus Git - ~andy/gtk/blob - tests/testoffscreenwindow.c
GtkOffscreenWindow implementation for #604901
[~andy/gtk] / tests / testoffscreenwindow.c
1 #include <gtk/gtk.h>
2
3 static gboolean
4 da_expose (GtkWidget *widget,
5            GdkEventExpose *event,
6            gpointer user_data)
7 {
8   GtkOffscreenWindow *offscreen = (GtkOffscreenWindow *)user_data;
9   GdkPixmap *pixmap;
10   cairo_t *cr;
11
12   if (GTK_WIDGET_DRAWABLE (widget))
13     {
14       pixmap = gdk_offscreen_window_get_pixmap (GTK_WIDGET (offscreen)->window);
15
16       cr = gdk_cairo_create (widget->window);
17       gdk_cairo_set_source_pixmap (cr, pixmap, 50, 50);
18       cairo_paint (cr);
19       cairo_destroy (cr);
20     }
21
22   return FALSE;
23 }
24
25 static gboolean
26 offscreen_damage (GtkWidget      *widget,
27                   GdkEventExpose *event,
28                   GtkWidget      *da)
29 {
30   gtk_widget_queue_draw (da);
31
32   return TRUE;
33 }
34
35 static gboolean
36 da_button_press (GtkWidget *area, GdkEventButton *event, GtkWidget *button)
37 {
38   gtk_widget_set_size_request (button, 150, 60);
39   return TRUE;
40 }
41
42 int
43 main (int argc, char **argv)
44 {
45   GtkWidget *window;
46   GtkWidget *button;
47   GtkWidget *offscreen;
48   GtkWidget *da;
49
50   gtk_init (&argc, &argv);
51
52   offscreen = gtk_offscreen_window_new ();
53
54   button = gtk_button_new_with_label ("Test");
55   gtk_widget_set_size_request (button, 50, 50);
56   gtk_container_add (GTK_CONTAINER (offscreen), button);
57   gtk_widget_show (button);
58
59   gtk_widget_show (offscreen);
60
61   /* Queue exposures and ensure they are handled so
62    * that the result is uptodate for the first
63    * expose of the window. If you want to get further
64    * changes, also track damage on the offscreen
65    * as done above.
66    */
67   gtk_widget_draw (offscreen, NULL);
68
69   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
70   da = gtk_drawing_area_new ();
71   gtk_container_add (GTK_CONTAINER (window), da);
72
73   g_signal_connect (da,
74                     "expose-event",
75                     G_CALLBACK (da_expose),
76                     offscreen);
77
78   g_signal_connect (offscreen,
79                     "damage-event",
80                     G_CALLBACK (offscreen_damage),
81                     da);
82
83   gtk_widget_add_events (da, GDK_BUTTON_PRESS_MASK);
84   g_signal_connect (da, "button_press_event", G_CALLBACK (da_button_press),
85                     button);
86
87   gtk_widget_show_all (window);
88
89   gtk_main();
90
91   return 0;
92 }