func RedrawWindow(W *XWindow, tweetsList []*TweetInfo, mouseClick [2]int) { var Attribs C.XWindowAttributes C.XGetWindowAttributes(W.Display, W.Window, &Attribs) // TODO -- Do this only when resizing? C.cairo_xlib_surface_set_size(W.Surface, Attribs.width, Attribs.height) C.cairo_set_source_rgb(W.Cairo, 0.1, 0.1, 0.1) C.cairo_paint(W.Cairo) var Rect C.PangoRectangle yPos := 10.0 + W.Scroll WindowWidth := Attribs.width maxTweetWidth := PixelsToPango(float64(WindowWidth - 5*UIPadding - UserImageSize)) for i := 0; i < len(tweetsList); i++ { t := tweetsList[i] C.pango_layout_set_width(t.Layout, maxTweetWidth) C.pango_layout_get_extents(t.Layout, nil, &Rect) // Get tweet text size _, ry, _, rh := PangoRectToPixels(&Rect) // Position and add padding ry += yPos if rh < UserImageSize+2*UIPadding-UIPadding { rh = UserImageSize + 2*UIPadding } else { rh += UIPadding } // Draw rectangle around tweet C.cairo_set_source_rgb(W.Cairo, 0.2, 0.2, 0.2) C.cairo_rectangle(W.Cairo, UIPadding, C.double(ry), C.double(WindowWidth-2*UIPadding), C.double(rh)) C.cairo_fill(W.Cairo) if mouseClick[0] >= UIPadding && float64(mouseClick[1]) >= ry && float64(mouseClick[1]) <= ry+rh { fmt.Println("Clicked tweet", t.Text) } // Draw user image userImage := GetCachedImage(W.UserImages, t.UserImage) if userImage == nil || C.cairo_surface_status(userImage) != C.CAIRO_STATUS_SUCCESS { userImage = placeholderImage } C.cairo_set_source_surface(W.Cairo, userImage, 2*UIPadding, C.double(yPos+UIPadding)) C.cairo_paint(W.Cairo) // Draw tweet text C.cairo_move_to(W.Cairo, 63, C.double(yPos+SmallPadding)) C.cairo_set_source_rgb(W.Cairo, 0.95, 0.95, 0.95) C.pango_cairo_show_layout(W.Cairo, t.Layout) yPos += 5 + rh } }
//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) }
func (self *Surface) SetSourceSurface(surface *Surface, x, y float64) { C.cairo_set_source_surface(self.context, surface.surface, C.double(x), C.double(y)) }
// SetSourceSurface is a wrapper around cairo_set_source_surface(). func (v *Context) SetSourceSurface(surface *Surface, x, y float64) { C.cairo_set_source_surface(v.native(), surface.native(), C.double(x), C.double(y)) }
//SetSourceSurface is a convenience function for creating a pattern //from a surface and setting it as the source pattern in c //with SetSource. // //The originDisplacement vector gives the user-space coordinates //at which the surface origin should appear. //The surface origin is its upper-left corner before any transformation //has been applied. //The x and y components of the vector are negated and then set //as the translation values in the pattern matrix. // //Other than the initial pattern matrix, described above, all other //pattern attributes are set to their default values. //The resulting pattern can be retrieved by calling Source. // //Originally cairo_set_source_surface. func (c *Context) SetSourceSurface(s Surface, originDisplacement Point) error { sr := s.XtensionRaw() x, y := originDisplacement.c() C.cairo_set_source_surface(c.c, sr, x, y) return c.Err() }