Im trying to get a image at random location to rotate. I was looking at another similar post(rotate image). But I couldn't get things to work.
#include <gtk/gtk.h>
#include <cairo.h>
GtkWidget *window;
static void rotate_cb()
{
gtk_widget_queue_draw(window);
}
static gboolean on_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
GtkWidget *img = (GtkWidget *)(data);
gint w = gtk_widget_get_allocated_width (img);
gint h = gtk_widget_get_allocated_height (img);
gtk_widget_realize(img);
cairo_surface_t *surface = gdk_window_create_similar_surface(gtk_widget_get_window (img), CAIRO_CONTENT_COLOR, w, h);
cairo_t *cr = cairo_create (surface);
cairo_translate(cr, w/2, h/2);
cairo_rotate(cr, 2);
cairo_set_source_surface(cr, surface, -w/2, -h/2);
cairo_paint(cr);
cairo_destroy(cr);
cairo_surface_destroy(surface);
return FALSE;
}
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), 600, 600);
gtk_widget_set_app_paintable(window, TRUE);
GtkWidget *l = gtk_layout_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(window), l);
GtkWidget *img = gtk_image_new_from_file("example.png");
gtk_layout_put(GTK_LAYOUT(l), img, 300, 300);
g_signal_connect(window, "draw", G_CALLBACK (on_expose_event), img);
g_signal_connect(window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
GtkWidget *button = gtk_button_new_with_label("button");
g_signal_connect(button, "clicked", G_CALLBACK(rotate_cb), NULL);
gtk_layout_put(GTK_LAYOUT(l), button, 0, 0);
gtk_widget_show_all(window);
gtk_main();
}
The window does received the draw signal, but I didn't know how to connect gtkwidget and cairo_surface_t.
Or maybe there are better ways of doing this(without cairo). I prefer all kinds of ideas! Thank you!