Hello Xgb
A new "Hello World!" program. This time, it is an X application, which uses the xgb Go package.
// xh.go JGL (yiyus). 2010
// xgb Hello World
// 8g xh.go && 8l -o xh xh.8 && ./xh
package main
import (
"fmt"
"os"
"xgb"
)
func hello(c *xgb.Conn, win, gc xgb.Id) {
c.ImageText8(win, gc, 50, 50, []byte("Hello World!"))
}
func main() {
c, err := xgb.Dial("")
if err != nil {
fmt.Fprintf(os.Stderr, "cannot connect: %v\n", err)
os.Exit(1)
}
screen := c.DefaultScreen()
win := c.NewId()
c.CreateWindow(
screen.RootDepth, win, screen.Root,
0, 0, 300, 200, 2,
xgb.WindowClassCopyFromParent,
xgb.WindowClassCopyFromParent,
xgb.CWBackPixel|xgb.CWEventMask,
[]uint32{screen.BlackPixel, xgb.EventMaskExposure | xgb.EventMaskButtonPress},
)
gc := c.NewId()
font := c.NewId()
c.OpenFont(font, "7x13")
c.CreateGC(gc, win,
xgb.GCBackground|xgb.GCForeground|xgb.GCFont,
[]uint32{screen.WhitePixel, screen.BlackPixel, uint32(font)},
)
c.CloseFont(font)
r := xgb.Rectangle{0, 0, 300, 200}
c.PolyFillRectangle(win, gc, []xgb.Rectangle{r})
hello(c, win, gc)
c.MapWindow(win)
Loop: for {
reply, err := c.WaitForEvent()
switch {
case err != nil:
fmt.Printf("error: %v\n", err)
os.Exit(1)
}
switch event := reply.(type) {
case xgb.ExposeEvent:
hello(c, win, gc)
case xgb.ButtonPressEvent:
fmt.Println("Button", event.Detail, "pressed. Bye!")
break Loop
}
}
c.Close()
}