//export our_area_draw_callback func our_area_draw_callback(widget *C.GtkWidget, cr *C.cairo_t, data C.gpointer) C.gboolean { var x0, y0, x1, y1 C.double var maxwid, maxht C.gint s := (*sysData)(unsafe.Pointer(data)) // thanks to desrt in irc.gimp.net/#gtk+ // these are in user coordinates, which match what coordinates we want by default, even out of a draw event handler (thanks johncc3, mclasen, and Company in irc.gimp.net/#gtk+) C.cairo_clip_extents(cr, &x0, &y0, &x1, &y1) // we do not need to clear the cliprect; GtkDrawingArea did it for us beforehand cliprect := image.Rect(int(x0), int(y0), int(x1), int(y1)) // the cliprect can actually fall outside the size of the Area; clip it by intersecting the two rectangles C.gtk_widget_get_size_request(widget, &maxwid, &maxht) cliprect = image.Rect(0, 0, int(maxwid), int(maxht)).Intersect(cliprect) if cliprect.Empty() { // no intersection; nothing to paint return C.FALSE // signals handled without stopping the event chain (thanks to desrt again) } i := s.handler.Paint(cliprect) surface := C.cairo_image_surface_create( C.CAIRO_FORMAT_ARGB32, // alpha-premultiplied; native byte order C.int(i.Rect.Dx()), C.int(i.Rect.Dy())) if status := C.cairo_surface_status(surface); status != C.CAIRO_STATUS_SUCCESS { panic(fmt.Errorf("cairo_create_image_surface() failed: %s\n", C.GoString(C.cairo_status_to_string(status)))) } // the flush and mark_dirty calls are required; see the cairo docs and https://git.gnome.org/browse/gtk+/tree/gdk/gdkcairo.c#n232 (thanks desrt in irc.gimp.net/#gtk+) C.cairo_surface_flush(surface) toARGB(i, uintptr(unsafe.Pointer(C.cairo_image_surface_get_data(surface))), int(C.cairo_image_surface_get_stride(surface))) C.cairo_surface_mark_dirty(surface) C.cairo_set_source_surface(cr, surface, 0, 0) // origin of the surface // that just set the brush that cairo uses: we have to actually draw now // (via https://developer.gnome.org/gtkmm-tutorial/stable/sec-draw-images.html.en) C.cairo_rectangle(cr, x0, y0, x1, y1) // breaking the nrom here since we have the coordinates as a C double already C.cairo_fill(cr) C.cairo_surface_destroy(surface) // free surface return C.FALSE // signals handled without stopping the event chain (thanks to desrt again) }
// shared code for finishing up and sending a mouse event func finishMouseEvent(widget *C.GtkWidget, data C.gpointer, me MouseEvent, mb uint, x C.gdouble, y C.gdouble, state C.guint, gdkwindow *C.GdkWindow) { var areawidth, areaheight C.gint // on GTK+, mouse buttons 4-7 are for scrolling; if we got here, that's a mistake if mb >= 4 && mb <= 7 { return } s := (*sysData)(unsafe.Pointer(data)) state = translateModifiers(state, gdkwindow) me.Modifiers = makeModifiers(state) // the mb != # checks exclude the Up/Down button from Held if mb != 1 && (state&C.GDK_BUTTON1_MASK) != 0 { me.Held = append(me.Held, 1) } if mb != 2 && (state&C.GDK_BUTTON2_MASK) != 0 { me.Held = append(me.Held, 2) } if mb != 3 && (state&C.GDK_BUTTON3_MASK) != 0 { me.Held = append(me.Held, 3) } // don't check GDK_BUTTON4_MASK or GDK_BUTTON5_MASK because those are for the scrolling buttons mentioned above; there doesn't seem to be a way to detect higher buttons... (TODO) me.Pos = image.Pt(int(x), int(y)) C.gtk_widget_get_size_request(widget, &areawidth, &areaheight) if !me.Pos.In(image.Rect(0, 0, int(areawidth), int(areaheight))) { // outside the actual Area; no event return } // and finally, if the button ID >= 8, continue counting from 4, as above and as in the MouseEvent spec if me.Down >= 8 { me.Down -= 4 } if me.Up >= 8 { me.Up -= 4 } repaint := s.handler.Mouse(me) if repaint { C.gtk_widget_queue_draw(widget) } }
// shared code for finishing up and sending a mouse event func finishMouseEvent(widget *C.GtkWidget, data C.gpointer, me MouseEvent, mb uint, x C.gdouble, y C.gdouble, state C.guint, gdkwindow *C.GdkWindow) { var areawidth, areaheight C.gint // on GTK+, mouse buttons 4-7 are for scrolling; if we got here, that's a mistake if mb >= 4 && mb <= 7 { return } a := (*area)(unsafe.Pointer(data)) state = translateModifiers(state, gdkwindow) me.Modifiers = makeModifiers(state) // the mb != # checks exclude the Up/Down button from Held if mb != 1 && (state&C.GDK_BUTTON1_MASK) != 0 { me.Held = append(me.Held, 1) } if mb != 2 && (state&C.GDK_BUTTON2_MASK) != 0 { me.Held = append(me.Held, 2) } if mb != 3 && (state&C.GDK_BUTTON3_MASK) != 0 { me.Held = append(me.Held, 3) } // don't check GDK_BUTTON4_MASK or GDK_BUTTON5_MASK because those are for the scrolling buttons mentioned above // GDK expressly does not support any more buttons in the GdkModifierType; see https://git.gnome.org/browse/gtk+/tree/gdk/x11/gdkdevice-xi2.c#n763 (thanks mclasen in irc.gimp.net/#gtk+) me.Pos = image.Pt(int(x), int(y)) C.gtk_widget_get_size_request(widget, &areawidth, &areaheight) if !me.Pos.In(image.Rect(0, 0, int(areawidth), int(areaheight))) { // outside the actual Area; no event return } // and finally, if the button ID >= 8, continue counting from 4, as above and as in the MouseEvent spec if me.Down >= 8 { me.Down -= 4 } if me.Up >= 8 { me.Up -= 4 } a.handler.Mouse(me) }
// GetSizeRequest is a wrapper around gtk_widget_get_size_request(). func (v *Widget) GetSizeRequest() (width, height int) { var w, h C.gint C.gtk_widget_get_size_request(v.native(), &w, &h) return int(w), int(h) }