2016 ソフトウェア設計及び演習用の班Wiki
g01::cairotest.c
- #include <gtk/gtk.h> static int cb_mouse_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data) {
int x = event->x; int y = event->y; GdkWindow *drawable = widget->window; cairo_t *cr; cairo_surface_t *surface; if(x >= 500 && y >=500) { cr = gdk_cairo_create (drawable);
surface = cairo_image_surface_create_from_png ("./kakiku.png"); cairo_set_source_surface (cr, surface, 0.0, 0.0); cairo_paint (cr);
cairo_destroy (cr); return FALSE; } if(x >= 0 && y >=300) { cr = gdk_cairo_create (drawable);
surface = cairo_image_surface_create_from_png ("./birds.png"); cairo_set_source_surface (cr, surface, 0.0, 0.0); cairo_paint (cr);
cairo_destroy (cr); return FALSE; }
}
gboolean cb_expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer user_data) { GdkWindow *drawable = widget->window; cairo_t *cr; cairo_surface_t *surface;
cr = gdk_cairo_create (drawable);
surface = (cairo_surface_t *) user_data; cairo_set_source_surface (cr, surface, 0.0, 0.0); cairo_paint (cr);
cairo_destroy (cr);
return FALSE; }
int main (int argc, char *argv[]) { GtkWidget *window; GtkWidget *canvas; cairo_surface_t *surface, *newsurface;
char *name; name = "./aiu.png"; gtk_init (&argc, &argv);
surface = cairo_image_surface_create_from_png (name);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW(window), "cairo_source_image_surface Sample"); gtk_widget_set_size_request (window, cairo_image_surface_get_width(surface) * 1.0, cairo_image_surface_get_height(surface) * 1.0); g_signal_connect (G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
canvas = gtk_drawing_area_new (); gtk_container_add (GTK_CONTAINER(window), canvas); gtk_widget_set_events(canvas,GDK_BUTTON_PRESS_MASK); g_signal_connect (G_OBJECT(canvas), "expose_event", G_CALLBACK (cb_expose_event), surface); g_signal_connect(G_OBJECT(canvas), "button_press_event", G_CALLBACK(cb_mouse_press),NULL);
gtk_widget_show_all (window); gtk_main(); cairo_surface_destroy (surface);
return 0; }
最終更新日:2016/06/17 10:46:19