0

I have a huge problem with cairo and gtk2. I was following this tutorial: http://zetcode.com/gfx/cairo/cairobackends/ (GTK Window header). Unfortunately nothing appears in the window.

Code from tutorial:

#include <cairo.h>
#include <gtk/gtk.h>

static void do_drawing(cairo_t *);

static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr, 
    gpointer user_data)
{      
  do_drawing(cr);

  return FALSE;
}

static void do_drawing(cairo_t *cr)
{
  cairo_set_source_rgb(cr, 0, 0, 0);
  cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
      CAIRO_FONT_WEIGHT_NORMAL);
  cairo_set_font_size(cr, 40.0);

  cairo_move_to(cr, 10.0, 50.0);
  cairo_show_text(cr, "Disziplin ist Macht.");    
}


int main(int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *darea;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  darea = gtk_drawing_area_new();
  gtk_container_add(GTK_CONTAINER(window), darea);

  g_signal_connect(G_OBJECT(darea), "draw", 
      G_CALLBACK(on_draw_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_default_size(GTK_WINDOW(window), 400, 90); 
  gtk_window_set_title(GTK_WINDOW(window), "GTK window");

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}

I figured out that I should use "expose_event" istead of "draw", but there is still problem.

I found something like this:

gboolean
expose_event_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
  gdk_draw_arc (widget->window,
                widget->style->fg_gc[gtk_widget_get_state (widget)],
                TRUE,
                0, 0, widget->allocation.width, widget->allocation.height,
                0, 64 * 360);
  return TRUE;
}
[...]
  GtkWidget *drawing_area = gtk_drawing_area_new ();
  gtk_widget_set_size_request (drawing_area, 100, 100);
  g_signal_connect (G_OBJECT (drawing_area), "expose_event",
                    G_CALLBACK (expose_event_callback), NULL);

This piece of code works, it draws something. Could you tell me what should I do to have a working functions like in the first example with one where I can put a code I want to draw (something like do_drawing). Thanks in advance.

jjpikoov
  • 119
  • 1
  • 11
  • The `"draw"` event from the code you used is GTK+ 3. This is really all I can say, though, as that's also all I know... – andlabs Jan 24 '15 at 18:07
  • I wrote "I figured out that I should use "expose_event" instead of "draw", but there is still problem." – jjpikoov Jan 24 '15 at 18:11
  • In late 2012 I rewrote the tutorial for GTK3. There are in fact only a few differences. – Jan Bodnar Jul 19 '15 at 10:27

1 Answers1

2

Try to create a new cairo context in the expose event callback with :

gdk_cairo_create (gtk_widget_get_window (widget));

Look this question :

How to create a cairo_t on a Gtk 2 window

I haven't make any tests but I think that it could help you

Community
  • 1
  • 1
cedlemo
  • 3,205
  • 3
  • 32
  • 50