[cairo] cairo_copy / cairo_rectangle

Lars Fomsgaard lars.fomsgaard at gmail.com
Thu Sep 10 11:17:07 PDT 2009


Hi

I am trying to make a program (gtk) for displaying some waveforms,
where i would like to use cairo for the drawing. I was hoping, that i
could start by drawing the waveforms to one big surface and then use
funtions like cairo_scale and cairo_rectangle to handle things like
zooming and redrawing when the window has been hidden.

My problem is that when i draw directly to the "cairo_t", that is
connected to the window, the waves display nicely, but when i draw to
my backup surface and use cairo_copy i end up with a blanc window.
Does anyone have a piece of advise for a cairo-beginner. I have pasted
in a small testprogram below.

Thanks
Lars

/* Start of source code */
#include <cairo.h>
#include <gtk/gtk.h>

static gboolean
on_expose_event(GtkWidget *widget,
   GdkEventExpose *event,
   gpointer data)
{
  static cairo_surface_t *draw_surface;
  static cairo_t *draw_cr;
  static cairo_t *disp_cr;
  static int WavesAlreadyLoaded=0;

  g_print("Entering on_expose_event\n");
  disp_cr = gdk_cairo_create(widget->window);

  /* Initialize master surface */
  if (WavesAlreadyLoaded!=1){
    g_print("Initializing drawing_surface\n");
    draw_surface=cairo_surface_create_similar(
        cairo_get_target(disp_cr),
        cairo_surface_get_type(cairo_get_target(disp_cr)),
        300,
        200);
    draw_cr = cairo_create(draw_surface);
    cairo_set_line_width (draw_cr, 1);
    cairo_set_source_rgb(draw_cr, 0, 0, 0.6);
    cairo_move_to(draw_cr, 30, 40);
    cairo_line_to(draw_cr, 40, 40);
    cairo_line_to(draw_cr, 45, 30);
    cairo_line_to(draw_cr, 55, 30);
    cairo_line_to(draw_cr, 60, 40);
    cairo_stroke(draw_cr);
    cairo_move_to(draw_cr, 60, 40);
    cairo_set_source_rgb(draw_cr, 0.8, 0, 0);
    cairo_move_to(draw_cr, 60, 40);
    cairo_line_to(draw_cr, 70, 40);
    cairo_line_to(draw_cr, 75, 30);
    cairo_line_to(draw_cr, 85, 30);
    cairo_line_to(draw_cr, 90, 40);
    cairo_stroke(draw_cr);
    WavesAlreadyLoaded=1;
  }

  cairo_set_source_surface(disp_cr, cairo_get_target(draw_cr), 0, 0);
  /*cairo_rectangle (disp_cr, 50, 50, 200, 100);
  cairo_fill (disp_cr);*/
  cairo_paint(disp_cr);
  cairo_stroke(disp_cr);
  cairo_destroy(disp_cr);

  return FALSE;
}


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

  GtkWidget *window;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK);

  g_signal_connect(window, "expose-event",
      G_CALLBACK(on_expose_event), NULL);
  g_signal_connect(window, "destroy",
      G_CALLBACK(gtk_main_quit), NULL);

  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_title(GTK_WINDOW(window), "CairoCopy");
  gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
  gtk_widget_set_app_paintable(window, TRUE);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}
/* End of source code */
>


More information about the cairo mailing list