Ejemplo n.º 1
0
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
	}
}
Ejemplo n.º 2
0
func main() {
	var dpy *C.Display
	var attr C.XWindowAttributes
	var start C.XButtonEvent
	var ev C.XEvent
	var ch *C.char

	if dpy != C.XOpenDisplay(ch) {
		return
	}

	C.XGrabKey(
		dpy,
		C.int(C.XKeysymToKeycode(dpy, C.XStringToKeysym(C.CString("F1")))),
		C.Mod1Mask,
		C.XDefaultRootWindow(dpy),
		1,
		C.GrabModeAsync,
		C.GrabModeAsync,
	)

	C.XGrabButton(
		dpy,
		1,
		C.Mod1Mask,
		C.XDefaultRootWindow(dpy),
		1,
		C.ButtonPressMask|C.ButtonReleaseMask|C.PointerMotionMask,
		C.GrabModeAsync,
		C.GrabModeAsync,
		C.None,
		C.None,
	)

	C.XGrabButton(
		dpy,
		3,
		C.Mod1Mask,
		C.XDefaultRootWindow(dpy),
		1,
		C.ButtonPressMask|C.ButtonReleaseMask|C.PointerMotionMask,
		C.GrabModeAsync,
		C.GrabModeAsync,
		C.None,
		C.None,
	)

	start.subwindow = C.None

	for {
		C.XNextEvent(dpy, &ev)
		if unionToInt(ev) == C.KeyPress && unionToXKeyEvent(ev).subwindow != C.None {
			C.XRaiseWindow(dpy, unionToXKeyEvent(ev).subwindow)
		} else if unionToInt(ev) == C.ButtonPress && unionToXButtonEvent(ev).subwindow != C.None {
			C.XGetWindowAttributes(dpy, unionToXButtonEvent(ev).subwindow, &attr)
			start = *unionToXButtonEvent(ev)
		} else if unionToInt(ev) == C.MotionNotify && start.subwindow != C.None {
			xdiff := unionToXButtonEvent(ev).x_root - start.x_root
			ydiff := unionToXButtonEvent(ev).y_root - start.y_root

			var toDiffX C.int
			var toDiffY C.int

			if start.button == 1 {
				toDiffX = xdiff
				toDiffY = ydiff
			}

			var toWidth C.int
			var toHeight C.int

			if start.button == 3 {
				toWidth = xdiff
				toHeight = ydiff
			}

			C.XMoveResizeWindow(
				dpy,
				start.subwindow,
				attr.x+toDiffX,
				attr.y+toDiffY,
				max(1, attr.width+toWidth),
				max(1, attr.height+toHeight))
		} else if unionToInt(ev) == C.ButtonRelease {
			start.subwindow = C.None
		}
	}
}