'Draw a GtkImage with Cairo
I tried the solutions proposed in similary notes (How to create a gtkimage from a cairo context, How do I create a gtkimage from a cairo surface) but they didn't work for me.
In my actual program, a background task calculates successive states of a physics simulation, and each iteration should add a point to a diagram. If I used a GtkDrawingArea, I would have to store all these points (not knowing in advance how many there will be), and draw everything upon each draw signal. So my idea is to use a GtkImage, where I have to draw only the one new point in each iteration.
In the following test program something should be drawn upon a button click, but nothing happens. Can somebody please indicate the error?
/* Test for drawing an image with Cairo. To be compiled with:
gcc -Wall -g gtk_img.c -o gtk_img `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0`
*/
#include <stdio.h>
#include <gdk/gdk.h>
#include <cairo.h>
#include <gtk/gtk.h>
#define BORDER 6
#define WIDTH 100
#define HEIGHT 100
cairo_surface_t *surf;
cairo_t *cr;
cairo_pattern_t *back, *fore;
/* Event Handlers
==============
*/
static gboolean Delete_Event
(GtkWidget *widget, GdkEvent *event, gpointer data)
{
return FALSE;
}
static void Destroy (GtkWidget *widget, gpointer data)
{
gtk_main_quit ();
}
static void Draw_Now (GtkWidget *widget, gpointer data)
{
printf("Draw button clicked\n");
cr = cairo_create (surf);
cairo_set_source (cr, back);
cairo_paint (cr);
cairo_set_source (cr, fore);
cairo_move_to (cr, 0.0, 0.0);
cairo_line_to (cr, 20.0, 20.0);
cairo_stroke (cr);
}
/* Main Program
============
*/
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkImage *img;
GtkBox *vbox;
GtkButton *button;
// init GTK, make window with vbox
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (Delete_Event), NULL);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (Destroy), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), BORDER);
vbox = (GtkBox*)gtk_box_new (GTK_ORIENTATION_VERTICAL, BORDER);
gtk_container_add (GTK_CONTAINER(window), (GtkWidget*)vbox);
// make image
surf = cairo_image_surface_create (CAIRO_FORMAT_RGB24, WIDTH, HEIGHT);
img = (GtkImage*)gtk_image_new_from_surface (surf);
gtk_image_clear (img);
back = cairo_pattern_create_rgb (0.9, 0.9, 0.9);
fore = cairo_pattern_create_rgb (0.1, 0.1, 0.1);
gtk_box_pack_start (vbox, (GtkWidget*)img, TRUE, TRUE, FALSE);
// button
button = (GtkButton*)gtk_button_new_with_label ("Draw");
g_signal_connect
(G_OBJECT (button), "clicked", G_CALLBACK (Draw_Now), NULL);
gtk_box_pack_start (vbox, (GtkWidget*)button, FALSE, FALSE, FALSE);
// start GTK
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
Solution 1:[1]
You are drawing to the cairo surface, but "nothing knows", so no redraw is triggered.
A random idea to trigger a redraw would be to call gtk_image_clear()
and then gtk_image_set_from_surface()
. I bet there are better ways to do that, but I don't really know GTK, sorry.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Uli Schlachter |