I'm going out of my mind a bit here.
I'm trying to draw some simple graphics on my GTK form using cairo.
#include <stdio.h>
#include <gtk/gtk.h>
#include <cairo.h>
GtkWidget* window;
GtkWidget* darea;
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 390, 240);
darea = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(window), darea);
cairo_t *cr;
cr = gdk_cairo_create(darea->window);
cairo_rectangle(cr, 0, 0, 100, 100);
cairo_fill(cr);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
This compiles, but gives me
Gdk-CRITICAL **: IA__gdk_cairo_create: assertion `GDK_IS_DRAWABLE (drawable)' failed
followed by a segfault.
I've been looking at the tutorial here
So I changed my code as follows, made the cairo calls occur inside the expose event.
#include <stdio.h>
#include <gtk/gtk.h>
#include <cairo.h>
GtkWidget* window;
GtkWidget* darea;
static gboolean
on_expose_event(GtkWidget *widget,
GdkEventExpose *event,
gpointer data)
{
cairo_t *cr;
cr = gdk_cairo_create(darea->window);
cairo_rectangle(cr, 0, 0, 100, 100);
cairo_fill(cr);
}
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 390, 240);
darea = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(window), darea);
g_signal_connect(darea, "expose-event",
G_CALLBACK(on_expose_event), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
Why does this fix it? My understanding re: the expose is: the
g_signal_connect(darea, "expose-event", G_GCALLBACK(on_expose_event), NULL);
tells the program, 'when an expose event happens to darea, then call on_expose_event'. The null is where you can pass in a pointer to a struct of additional information for the function to use.
and
static gboolean
on_expose_event(GtkWidget *widget,
GdkEventExpose *event,
gpointer data)
{
means that on_expose_event is passed a pointer to the widget that the event happened to, andin this case because it's an expose event, a pointer to a struct containing information about the expose event, and a pointer to a struct to any other information you might like to add.