testoffscreenwindow.c 2.07 KB
Newer Older
1 2 3
#include <gtk/gtk.h>

static gboolean
4 5 6
da_draw (GtkWidget *widget,
         cairo_t   *cr,
         gpointer   user_data)
7 8 9
{
  GtkOffscreenWindow *offscreen = (GtkOffscreenWindow *)user_data;

10 11 12 13
  cairo_set_source_surface (cr,
                            gtk_offscreen_window_get_surface (offscreen),
                            50, 50);
  cairo_paint (cr);
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

  return FALSE;
}

static gboolean
offscreen_damage (GtkWidget      *widget,
                  GdkEventExpose *event,
                  GtkWidget      *da)
{
  gtk_widget_queue_draw (da);

  return TRUE;
}

static gboolean
da_button_press (GtkWidget *area, GdkEventButton *event, GtkWidget *button)
{
  gtk_widget_set_size_request (button, 150, 60);
  return TRUE;
}

int
main (int argc, char **argv)
{
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *offscreen;
  GtkWidget *da;

  gtk_init (&argc, &argv);

  offscreen = gtk_offscreen_window_new ();

  button = gtk_button_new_with_label ("Test");
  gtk_widget_set_size_request (button, 50, 50);
  gtk_container_add (GTK_CONTAINER (offscreen), button);
  gtk_widget_show (button);

  gtk_widget_show (offscreen);

  /* Queue exposures and ensure they are handled so
   * that the result is uptodate for the first
   * expose of the window. If you want to get further
   * changes, also track damage on the offscreen
   * as done above.
   */
60
  gtk_widget_queue_draw (offscreen);
61 62

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
63 64
  g_signal_connect (window, "delete-event",
                    G_CALLBACK (gtk_main_quit), window);
65 66 67 68
  da = gtk_drawing_area_new ();
  gtk_container_add (GTK_CONTAINER (window), da);

  g_signal_connect (da,
69 70
                    "draw",
                    G_CALLBACK (da_draw),
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
                    offscreen);

  g_signal_connect (offscreen,
                    "damage-event",
                    G_CALLBACK (offscreen_damage),
                    da);

  gtk_widget_add_events (da, GDK_BUTTON_PRESS_MASK);
  g_signal_connect (da, "button_press_event", G_CALLBACK (da_button_press),
                    button);

  gtk_widget_show_all (window);

  gtk_main();

  return 0;
}