head 1.2; access; symbols; locks; strict; comment @ * @; 1.2 date 2005.08.12.07.24.27; author behdad; state Exp; branches; next 1.1; commitid 75b042fc4e5b4567; 1.1 date 2004.11.11.19.45.53; author pippin; state Exp; branches; next ; desc @@ 1.2 log @2005-08-12 Behdad Esfahbod * hello.c, mouse_events.c: updated to latest cairo+gtk api. * README: noted the change from gtkcairo to cairo+gtk. @ text @/* alpha blended rubber band using canvas, demonstrating mouse intercation * * only things that are new compared to hello.c are commented */ #include #include #define DEFAULT_WIDTH 400 #define DEFAULT_HEIGHT 200 /* a structure keeping information about * the selection */ typedef struct { gboolean active; /* whether the selection is active or not */ gdouble x, y; gdouble w, h; } SelectionInfo; static void paint (GtkWidget *widget, GdkEventExpose *eev, SelectionInfo *sel_info); /* forward definitions of handler for mouse events */ static gboolean event_press (GtkWidget *widget, GdkEventButton *bev, SelectionInfo *sel_info); static gboolean event_release (GtkWidget *widget, GdkEventButton *bev, SelectionInfo *sel_info); static gboolean event_motion (GtkWidget *widget, GdkEventMotion *mev, SelectionInfo *sel_info); gint main (gint argc, gchar **argv) { GtkWidget *window; GtkWidget *canvas; SelectionInfo selection = {FALSE, 0, 0, 0, 0}; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK (gtk_main_quit), NULL); canvas = gtk_drawing_area_new (); gtk_widget_set_size_request (canvas, DEFAULT_WIDTH, DEFAULT_HEIGHT); /* connect our drawing method to the "expose" signal * passing in a pointer to the selection structure as the user data */ g_signal_connect (G_OBJECT (canvas), "expose-event", G_CALLBACK (paint), &selection ); /* add additional events the canvas widget will listen for */ gtk_widget_add_events (canvas, GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK); /* connect our rubber banding callbacks, like the paint callback * we pass the selection as userdata */ g_signal_connect (G_OBJECT (canvas), "button_press_event", G_CALLBACK (event_press), &selection ); g_signal_connect (G_OBJECT (canvas), "button_release_event", G_CALLBACK (event_release), &selection ); g_signal_connect (G_OBJECT (canvas), "motion_notify_event", G_CALLBACK (event_motion), &selection ); gtk_container_add (GTK_CONTAINER (window), canvas); gtk_widget_show_all (window); gtk_main (); return 0; } /* function to draw the rectangular selection */ static void paint_selection (cairo_t *cr, SelectionInfo *sel_info) { if (!sel_info->active) return; cairo_save (cr); cairo_rectangle (cr, sel_info->x, sel_info->y, sel_info->w, sel_info->h); cairo_set_source_rgba (cr, 0, 0, 1, 0.2); cairo_fill_preserve (cr); cairo_set_source_rgba (cr, 0, 0, 0, 0.5); cairo_stroke (cr); cairo_restore (cr); } static void paint (GtkWidget *widget, GdkEventExpose *eev, SelectionInfo *sel_info) { gint width, height; gint i; cairo_t *cr; width = widget->allocation.width; height = widget->allocation.height; cr = gdk_cairo_create (widget->window); cairo_set_source_rgb (cr, 1,1,1); cairo_paint (cr); cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); cairo_save (cr); cairo_set_font_size (cr, 20); cairo_move_to (cr, 40, 60); cairo_set_source_rgb (cr, 0,0,0); cairo_show_text (cr, "Drag your mouse here"); cairo_restore (cr); cairo_set_source_rgb (cr, 1,0,0); cairo_set_font_size (cr, 15); cairo_move_to (cr, 50, 100); cairo_show_text (cr, "(and see alpha blended selection)"); cairo_set_source_rgb (cr, 0,0,1); cairo_move_to (cr, 0, 150); for (i=0; i< width/10; i++) { cairo_rel_line_to (cr, 5, 10); cairo_rel_line_to (cr, 5, -10); } cairo_stroke (cr); /* paint the selection */ paint_selection (cr, sel_info); cairo_destroy (cr); } static gboolean event_press (GtkWidget *widget, GdkEventButton *bev, SelectionInfo *sel_info) { sel_info->active = TRUE; sel_info->x = bev->x; sel_info->y = bev->y; sel_info->w = 0; sel_info->h = 0; /* tell the canvas widget that it needs to redraw itself */ gtk_widget_queue_draw (widget); return TRUE; } static gboolean event_motion (GtkWidget *widget, GdkEventMotion *mev, SelectionInfo *sel_info) { sel_info->w = mev->x - sel_info->x; sel_info->h = mev->y - sel_info->y; /* tell the canvas widget that it needs to redraw itself */ gtk_widget_queue_draw (widget); return TRUE; } static gboolean event_release (GtkWidget *widget, GdkEventButton *bev, SelectionInfo *sel_info) { sel_info->active = FALSE; /* tell the canvas widget that it needs to redraw itself */ gtk_widget_queue_draw (widget); return TRUE; } @ 1.1 log @added mouse event example @ text @d1 1 a1 1 /* alpha blended rubber band using gtkcairo, demonstrating mouse intercation d7 1 a7 1 #include d22 3 a24 3 static void paint (GtkWidget *widget, cairo_t *cr, SelectionInfo *sel_info); d45 1 a45 1 GtkWidget *gtkcairo; d54 2 a55 2 gtkcairo = gtk_cairo_new (); gtk_widget_set_size_request (gtkcairo, DEFAULT_WIDTH, DEFAULT_HEIGHT); d57 1 a57 1 /* connect our drawing method to the "paint" signal d60 1 a60 1 g_signal_connect (G_OBJECT (gtkcairo), "paint", d65 1 a65 1 /* add additional events the gtkcairo widget will listen for d67 1 a67 1 gtk_widget_add_events (gtkcairo, d76 1 a76 1 g_signal_connect (G_OBJECT (gtkcairo), "button_press_event", d80 1 a80 1 g_signal_connect (G_OBJECT (gtkcairo), "button_release_event", d84 1 a84 1 g_signal_connect (G_OBJECT (gtkcairo), "motion_notify_event", d89 1 a89 1 gtk_container_add (GTK_CONTAINER (window), gtkcairo); d105 1 a108 5 cairo_save (cr); cairo_set_rgb_color (cr, 0, 0, 1); cairo_set_alpha (cr, 0.2); cairo_fill (cr); cairo_restore (cr); d110 4 a113 2 cairo_set_rgb_color (cr, 0, 0, 0); cairo_set_alpha (cr, 0.5); d120 3 a122 3 paint (GtkWidget *widget, cairo_t *cr, SelectionInfo *sel_info) d126 1 d131 1 a131 1 cairo_save (cr); d133 2 a134 3 cairo_rectangle (cr, 0, 0, width, height); cairo_set_rgb_color (cr, 1,1,1); cairo_fill (cr); d136 1 a136 1 cairo_select_font (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, d139 1 a139 1 cairo_scale_font (cr, 20); d141 1 a141 1 cairo_set_rgb_color (cr, 0,0,0); d145 2 a146 2 cairo_set_rgb_color (cr, 1,0,0); cairo_scale_font (cr, 15); d150 1 a150 1 cairo_set_rgb_color (cr, 0,0,1); d164 1 a164 1 cairo_restore (cr); d180 1 a180 1 /* tell the gtkcairo widget that it needs to redraw itself */ d195 1 a195 1 /* tell the gtkcairo widget that it needs to redraw itself */ d207 1 a207 1 /* tell the gtkcairo widget that it needs to redraw itself */ @