Example #1
0
func main() {
	runtime.GOMAXPROCS(10)
	glib.ThreadInit(nil)
	gdk.ThreadsInit()
	gdk.ThreadsEnter()
	gtk.Init(nil)
	window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
	window.Connect("destroy", gtk.MainQuit)

	vbox := gtk.NewVBox(false, 1)

	label1 := gtk.NewLabel("")
	vbox.Add(label1)
	label2 := gtk.NewLabel("")
	vbox.Add(label2)

	window.Add(vbox)

	window.SetSizeRequest(100, 100)
	window.ShowAll()
	time.Sleep(1000 * 1000 * 100)
	go (func() {
		for i := 0; i < 300000; i++ {
			gdk.ThreadsEnter()
			label1.SetLabel(strconv.Itoa(i))
			gdk.ThreadsLeave()
		}
		gtk.MainQuit()
	})()
	go (func() {
		for i := 300000; i >= 0; i-- {
			gdk.ThreadsEnter()
			label2.SetLabel(strconv.Itoa(i))
			gdk.ThreadsLeave()
		}
		gtk.MainQuit()
	})()
	gtk.Main()
}
Example #2
0
func main() {
	gtk.Init(&os.Args)
	gdk.ThreadsInit()
	window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
	window.SetTitle("Twitter!")
	window.Connect("destroy", gtk.MainQuit)

	vbox := gtk.NewVBox(false, 1)

	scrolledwin := gtk.NewScrolledWindow(nil, nil)
	textview := gtk.NewTextView()
	textview.SetEditable(false)
	textview.SetCursorVisible(false)
	scrolledwin.Add(textview)
	vbox.Add(scrolledwin)

	buffer := textview.GetBuffer()

	tag := buffer.CreateTag("blue", map[string]string{
		"foreground": "#0000FF", "weight": "700"})
	button := gtk.NewButtonWithLabel("Update Timeline")
	button.SetTooltipMarkup("update <b>public timeline</b>")
	button.Clicked(func() {
		go func() {
			gdk.ThreadsEnter()
			button.SetSensitive(false)
			gdk.ThreadsLeave()
			r, err := http.Get("https://api.twitter.com/1/statuses/user_timeline.json?screen_name=go_nuts")
			if err == nil {
				var b []byte
				if r.ContentLength == -1 {
					b, err = ioutil.ReadAll(r.Body)
				} else {
					b = make([]byte, r.ContentLength)
					_, err = io.ReadFull(r.Body, b)
				}
				if err != nil {
					fmt.Println(err)
					return
				}
				var j interface{}
				err = json.NewDecoder(bytes.NewBuffer(b)).Decode(&j)
				if err != nil {
					fmt.Println(err)
					return
				}
				arr := j.([]interface{})
				for i := 0; i < len(arr); i++ {
					data := arr[i].(map[string]interface{})
					icon := data["user"].(map[string]interface{})["profile_image_url"].(string)
					var iter gtk.TextIter
					pixbufbytes, resp := readURL(icon)
					gdk.ThreadsEnter()
					buffer.GetEndIter(&iter)
					if resp != nil {
						buffer.InsertPixbuf(&iter, bytes2pixbuf(pixbufbytes, resp.Header.Get("Content-Type")))
					}
					gdk.ThreadsLeave()
					name := data["user"].(map[string]interface{})["screen_name"].(string)
					text := data["text"].(string)
					gdk.ThreadsEnter()
					buffer.Insert(&iter, " ")
					buffer.InsertWithTag(&iter, name, tag)
					buffer.Insert(&iter, ":"+text+"\n")
					gtk.MainIterationDo(false)
					gdk.ThreadsLeave()
				}
			} else {
				fmt.Println(err)
			}
			button.SetSensitive(true)
		}()
	})
	vbox.PackEnd(button, false, false, 0)

	window.Add(vbox)
	window.SetSizeRequest(800, 500)
	window.ShowAll()
	gdk.ThreadsEnter()
	gtk.Main()
	gdk.ThreadsLeave()
}
Example #3
0
func main() {
	gtk.Init(&os.Args)
	gdk.ThreadsInit()
	window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
	window.SetTitle("Twitter!")
	window.Connect("destroy", gtk.MainQuit)

	vbox := gtk.NewVBox(false, 1)

	scrolledwin := gtk.NewScrolledWindow(nil, nil)
	textview := gtk.NewTextView()
	textview.SetEditable(false)
	textview.SetCursorVisible(false)
	scrolledwin.Add(textview)
	vbox.Add(scrolledwin)

	buffer := textview.GetBuffer()

	tag := buffer.CreateTag("blue", map[string]string{
		"foreground": "#0000FF", "weight": "700"})
	button := gtk.NewButtonWithLabel("Update Timeline")
	button.SetTooltipMarkup("update <b>public timeline</b>")
	button.Clicked(func() {
		b, err := ioutil.ReadFile("settings.json")
		if err != nil {
			fmt.Println(`"settings.json" not found: `, err)
			return
		}
		var config map[string]string
		err = json.Unmarshal(b, &config)
		if err != nil {
			fmt.Println(`can't read "settings.json": `, err)
			return
		}
		client := &oauth.Client{
			Credentials: oauth.Credentials{
				config["ClientToken"], config["ClientSecret"]}}
		cred := &oauth.Credentials{
			config["AccessToken"], config["AccessSecret"]}

		gdk.ThreadsEnter()
		button.SetSensitive(false)
		gdk.ThreadsLeave()
		go func() {
			ts, err := twitterstream.Open(client, cred,
				"https://stream.twitter.com/1/statuses/filter.json",
				url.Values{"track": {"picplz,instagr"}})
			if err != nil {
				println(err.Error())
				return
			}
			for ts.Err() == nil {
				t := tweet{}
				if err := ts.UnmarshalNext(&t); err != nil {
					fmt.Println("error reading tweet: ", err)
					continue
				}
				var iter gtk.TextIter
				pixbufbytes, resp := readURL(t.User.ProfileImageUrl)
				gdk.ThreadsEnter()
				buffer.GetStartIter(&iter)
				if resp != nil {
					buffer.InsertPixbuf(&iter, bytes2pixbuf(pixbufbytes, resp.Header.Get("Content-Type")))
				}
				gdk.ThreadsLeave()
				gdk.ThreadsEnter()
				buffer.Insert(&iter, " ")
				buffer.InsertWithTag(&iter, t.User.ScreenName, tag)
				buffer.Insert(&iter, ":"+t.Text+"\n")
				gtk.MainIterationDo(false)
				gdk.ThreadsLeave()
			}
		}()
	})
	vbox.PackEnd(button, false, false, 0)

	window.Add(vbox)
	window.SetSizeRequest(800, 500)
	window.ShowAll()
	gdk.ThreadsEnter()
	gtk.Main()
	gdk.ThreadsLeave()
}