[Howto] GTK+ and cairo (minimal example)

posted in: computer | 0

minimal working example, which creates a GTK-drawing-area and draws ‘Test.’ inside.
(compile with command: gcc example.c -o example `pkg-config --cflags --libs gtk+-3.0`)

versions: GTK+ 3.24.23-2 and cairo 1.16.0-4

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

//cairo-functions
void draw_cairo(GtkWidget *widget, cairo_t *cr, gpointer user_data) {      
  cairo_set_font_size(cr, 50.0);
  cairo_move_to(cr, 25.0, 50.0);
  cairo_show_text(cr, "TEST.");    
}


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

  //window
  GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_widget_show(window);

  //drawingarea
  GtkWidget *darea = gtk_drawing_area_new();
  gtk_container_add(GTK_CONTAINER(window), darea);
  g_signal_connect(G_OBJECT(darea), "draw", G_CALLBACK(draw_cairo), NULL); 
  gtk_widget_show(darea);

  gtk_main();
}