1 2 3 4 5 6 7 8 9 10 11 12 13 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 60 61 62 63
| #include <stdlib.h>
#include <gtk/gtk.h>
gboolean expose (GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
{
cairo_t *context = NULL;
cairo_surface_t *surface=NULL;
guchar *buffer = NULL;
gint i = 0;
gint width, height, stride;
// Récupération du cairo context du GtkWidget.
context = gdk_cairo_create (widget->window);
// Création d'une surface image et test de celle-ci avant modification
width = widget->allocation.width;
height = widget->allocation.height;
surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
if (cairo_surface_status (surface) == CAIRO_STATUS_SUCCESS)
{
// Récupération du buffer et de la longueur d'une ligne en octets.
buffer = cairo_image_surface_get_data (surface);
stride = cairo_image_surface_get_stride (surface);
// Exemple de modification directe du buffer image
for (i=0; i < stride*height; i+=4)
buffer[i] = 255;
cairo_set_source_surface (context, surface, 0, 0);
cairo_rectangle (context, 0, 0, width, height);
cairo_clip (context);
cairo_paint(context);
}
cairo_destroy(context);
cairo_surface_destroy(surface);
return FALSE;
}
int main (int argc,char *argv[])
{
GtkWidget *window = NULL;
GtkWidget *drawing = NULL;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
drawing = gtk_drawing_area_new();
gtk_widget_set_size_request(drawing, 300, 600);
gtk_container_add(GTK_CONTAINER(window), drawing);
g_signal_connect(G_OBJECT(drawing), "expose-event", (GCallback)expose, NULL);
g_signal_connect(G_OBJECT(window), "delete-event", gtk_main_quit, NULL);
gtk_widget_show_all(window);
gtk_main();
return EXIT_SUCCESS;
} |
Partager