func main() { gtk.Init(&os.Args) window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL) window.SetTitle("GTK DrawingArea") window.Connect("destroy", gtk.MainQuit) vbox := gtk.NewVBox(true, 0) vbox.SetBorderWidth(5) targets := []gtk.TargetEntry{ {"text/uri-list", 0, 0}, {"STRING", 0, 1}, {"text/plain", 0, 2}, } dest := gtk.NewLabel("drop me file") dest.DragDestSet( gtk.DEST_DEFAULT_MOTION| gtk.DEST_DEFAULT_HIGHLIGHT| gtk.DEST_DEFAULT_DROP, targets, gdk.ACTION_COPY) dest.DragDestAddUriTargets() dest.Connect("drag-data-received", func(ctx *glib.CallbackContext) { sdata := gtk.SelectionDataFromNative(unsafe.Pointer(ctx.Args(3))) if sdata != nil { a := (*[2000]uint8)(sdata.GetData()) files := strings.Split(string(a[0:sdata.GetLength()-1]), "\n") for i := range files { filename, _, _ := glib.FilenameFromUri(files[i]) files[i] = filename } dialog := gtk.NewMessageDialog( window, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, strings.Join(files, "\n")) dialog.SetTitle("D&D") dialog.Response(func() { dialog.Destroy() }) dialog.Run() } }) vbox.Add(dest) window.Add(vbox) window.SetSizeRequest(300, 100) window.ShowAll() gtk.Main() }
func main() { gtk.Init(&os.Args) window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL) window.SetTitle("We love Expander") window.Connect("destroy", gtk.MainQuit) vbox := gtk.NewVBox(true, 0) vbox.SetBorderWidth(5) expander := gtk.NewExpander("dan the ...") expander.Add(gtk.NewLabel("404 contents not found")) vbox.PackStart(expander, false, false, 0) window.Add(vbox) window.ShowAll() gtk.Main() }
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() }
func main() { gtk.Init(nil) window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL) window.SetPosition(gtk.WIN_POS_CENTER) window.SetTitle("GTK Go!") window.Connect("destroy", func(ctx *glib.CallbackContext) { gtk.MainQuit() }, "") box := gtk.NewHPaned() palette := gtk.NewToolPalette() group := gtk.NewToolItemGroup("Tools") b := gtk.NewToolButtonFromStock(gtk.STOCK_NEW) b.OnClicked(func() { println("You clicked new!") }) group.Insert(b, -1) b = gtk.NewToolButtonFromStock(gtk.STOCK_CLOSE) group.Insert(b, -1) b = gtk.NewToolButtonFromStock(gtk.STOCK_REDO) group.Insert(b, -1) b = gtk.NewToolButtonFromStock(gtk.STOCK_REFRESH) group.Insert(b, -1) b = gtk.NewToolButtonFromStock(gtk.STOCK_QUIT) group.Insert(b, -1) b = gtk.NewToolButtonFromStock(gtk.STOCK_YES) group.Insert(b, -1) b = gtk.NewToolButtonFromStock(gtk.STOCK_NO) group.Insert(b, -1) b = gtk.NewToolButtonFromStock(gtk.STOCK_PRINT) group.Insert(b, -1) b = gtk.NewToolButtonFromStock(gtk.STOCK_NETWORK) group.Insert(b, -1) b = gtk.NewToolButtonFromStock(gtk.STOCK_INFO) group.Insert(b, -1) b = gtk.NewToolButtonFromStock(gtk.STOCK_HOME) group.Insert(b, -1) b = gtk.NewToolButtonFromStock(gtk.STOCK_INDEX) group.Insert(b, -1) b = gtk.NewToolButtonFromStock(gtk.STOCK_FIND) group.Insert(b, -1) b = gtk.NewToolButtonFromStock(gtk.STOCK_FILE) group.Insert(b, -1) b = gtk.NewToolButtonFromStock(gtk.STOCK_EXECUTE) group.Insert(b, -1) palette.Add(group) bcopy := gtk.NewToolButtonFromStock(gtk.STOCK_COPY) bcut := gtk.NewToolButtonFromStock(gtk.STOCK_CUT) bdelete := gtk.NewToolButtonFromStock(gtk.STOCK_DELETE) group = gtk.NewToolItemGroup("Stuff") group.Insert(bcopy, -1) group.Insert(bcut, -1) group.Insert(bdelete, -1) palette.Add(group) frame := gtk.NewVBox(false, 1) align := gtk.NewAlignment(0, 0, 0, 0) image := gtk.NewImageFromFile("./turkey.jpg") align.Add(image) frame.Add(align) box.Pack1(palette, true, false) box.Pack2(frame, false, false) window.Add(box) window.SetSizeRequest(600, 600) window.ShowAll() gtk.Main() }
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() }
func main() { gtk.Init(nil) window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL) window.SetPosition(gtk.WIN_POS_CENTER) window.SetTitle("GTK Go!") window.Connect("destroy", func(ctx *glib.CallbackContext) { gtk.MainQuit() }, "") vbox := gtk.NewVBox(false, 0) toolbar := gtk.NewToolbar() toolbar.SetStyle(gtk.TOOLBAR_ICONS) vbox.PackStart(toolbar, false, false, 5) btnnew := gtk.NewToolButtonFromStock(gtk.STOCK_NEW) btnclose := gtk.NewToolButtonFromStock(gtk.STOCK_CLOSE) separator := gtk.NewSeparatorToolItem() btncustom := gtk.NewToolButton(nil, "Custom") btnmenu := gtk.NewMenuToolButtonFromStock("gtk.STOCK_CLOSE") btnmenu.SetArrowTooltipText("This is a tool tip") btnnew.OnClicked(onToolButtonClicked) btnclose.OnClicked(onToolButtonClicked) btncustom.OnClicked(onToolButtonClicked) toolmenu := gtk.NewMenu() menuitem := gtk.NewMenuItemWithMnemonic("8") menuitem.Show() toolmenu.Append(menuitem) menuitem = gtk.NewMenuItemWithMnemonic("16") menuitem.Show() toolmenu.Append(menuitem) menuitem = gtk.NewMenuItemWithMnemonic("32") menuitem.Show() toolmenu.Append(menuitem) btnmenu.SetMenu(toolmenu) toolbar.Insert(btnnew, -1) toolbar.Insert(btnclose, -1) toolbar.Insert(separator, -1) toolbar.Insert(btncustom, -1) toolbar.Insert(btnmenu, -1) hbox := gtk.NewHBox(false, 0) vbox.PackStart(hbox, true, true, 0) toolbar2 := gtk.NewToolbar() toolbar2.SetOrientation(gtk.ORIENTATION_VERTICAL) hbox.PackStart(toolbar2, false, false, 5) btnhelp := gtk.NewToolButtonFromStock(gtk.STOCK_HELP) btnhelp.OnClicked(onToolButtonClicked) toolbar2.Insert(btnhelp, -1) btntoggle := gtk.NewToggleToolButton() btntoggle2 := gtk.NewToggleToolButtonFromStock(gtk.STOCK_ITALIC) toolbar2.Insert(btntoggle, -1) toolbar2.Insert(btntoggle2, -1) for i := 0; i < toolbar.GetNItems(); i++ { gti := toolbar.GetNthItem(i) switch gti.(type) { case *gtk.ToolButton: fmt.Printf("toolbar[%d] is a *gtk.ToolButton\n", i) w := gti.(*gtk.ToolButton).GetIconWidget() gti.(*gtk.ToolButton).SetIconWidget(w) case *gtk.ToggleToolButton: fmt.Printf("toolbar[%d] is a *gtk.ToggleToolButton\n", i) gti.(*gtk.ToggleToolButton).SetActive(true) case *gtk.SeparatorToolItem: fmt.Printf("toolbar[%d] is a *gtk.SeparatorToolItem\n", i) default: fmt.Printf("toolbar: Item is of unknown type\n") } } for i := 0; i < toolbar2.GetNItems(); i++ { gti := toolbar2.GetNthItem(i) switch gti.(type) { case *gtk.ToolButton: fmt.Printf("toolbar2[%d] is a *gtk.ToolButton\n", i) case *gtk.ToggleToolButton: fmt.Printf("toolbar2[%d] is a *gtk.ToggleToolButton\n", i) gti.(*gtk.ToggleToolButton).SetActive(true) case *gtk.SeparatorToolItem: fmt.Printf("toolbar2[%d] is a *gtk.SeparatorToolItem\n", i) default: fmt.Printf("toolbar2: Item is of unknown type\n") } } window.Add(vbox) window.SetSizeRequest(600, 600) window.ShowAll() gtk.Main() }
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() }
func main() { var menuitem *gtk.MenuItem gtk.Init(nil) window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL) window.SetPosition(gtk.WIN_POS_CENTER) window.SetTitle("GTK Go!") window.SetIconName("gtk-dialog-info") window.Connect("destroy", func(ctx *glib.CallbackContext) { println("got destroy!", ctx.Data().(string)) gtk.MainQuit() }, "foo") //-------------------------------------------------------- // GtkVBox //-------------------------------------------------------- vbox := gtk.NewVBox(false, 1) //-------------------------------------------------------- // GtkMenuBar //-------------------------------------------------------- menubar := gtk.NewMenuBar() vbox.PackStart(menubar, false, false, 0) //-------------------------------------------------------- // GtkVPaned //-------------------------------------------------------- vpaned := gtk.NewVPaned() vbox.Add(vpaned) //-------------------------------------------------------- // GtkFrame //-------------------------------------------------------- frame1 := gtk.NewFrame("Demo") framebox1 := gtk.NewVBox(false, 1) frame1.Add(framebox1) frame2 := gtk.NewFrame("Demo") framebox2 := gtk.NewVBox(false, 1) frame2.Add(framebox2) vpaned.Pack1(frame1, false, false) vpaned.Pack2(frame2, false, false) //-------------------------------------------------------- // GtkImage //-------------------------------------------------------- dir, _ := path.Split(os.Args[0]) imagefile := path.Join(dir, "../../data/go-gtk-logo.png") label := gtk.NewLabel("Go Binding for GTK") label.ModifyFontEasy("DejaVu Serif 15") framebox1.PackStart(label, false, true, 0) //-------------------------------------------------------- // GtkEntry //-------------------------------------------------------- entry := gtk.NewEntry() entry.SetText("Hello world") framebox1.Add(entry) image := gtk.NewImageFromFile(imagefile) framebox1.Add(image) //-------------------------------------------------------- // GtkScale //-------------------------------------------------------- scale := gtk.NewHScaleWithRange(0, 100, 1) scale.Connect("value-changed", func() { println("scale:", int(scale.GetValue())) }) framebox2.Add(scale) //-------------------------------------------------------- // GtkHBox //-------------------------------------------------------- buttons := gtk.NewHBox(false, 1) //-------------------------------------------------------- // GtkButton //-------------------------------------------------------- button := gtk.NewButtonWithLabel("Button with label") button.Clicked(func() { println("button clicked:", button.GetLabel()) messagedialog := gtk.NewMessageDialog( button.GetTopLevelAsWindow(), gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, entry.GetText()) messagedialog.Response(func() { println("Dialog OK!") //-------------------------------------------------------- // GtkFileChooserDialog //-------------------------------------------------------- filechooserdialog := gtk.NewFileChooserDialog( "Choose File...", button.GetTopLevelAsWindow(), gtk.FILE_CHOOSER_ACTION_OPEN, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT) filter := gtk.NewFileFilter() filter.AddPattern("*.go") filechooserdialog.AddFilter(filter) filechooserdialog.Response(func() { println(filechooserdialog.GetFilename()) filechooserdialog.Destroy() }) filechooserdialog.Run() messagedialog.Destroy() }) messagedialog.Run() }) buttons.Add(button) //-------------------------------------------------------- // GtkFontButton //-------------------------------------------------------- fontbutton := gtk.NewFontButton() fontbutton.Connect("font-set", func() { println("title:", fontbutton.GetTitle()) println("fontname:", fontbutton.GetFontName()) println("use_size:", fontbutton.GetUseSize()) println("show_size:", fontbutton.GetShowSize()) }) buttons.Add(fontbutton) framebox2.PackStart(buttons, false, false, 0) buttons = gtk.NewHBox(false, 1) //-------------------------------------------------------- // GtkToggleButton //-------------------------------------------------------- togglebutton := gtk.NewToggleButtonWithLabel("ToggleButton with label") togglebutton.Connect("toggled", func() { if togglebutton.GetActive() { togglebutton.SetLabel("ToggleButton ON!") } else { togglebutton.SetLabel("ToggleButton OFF!") } }) buttons.Add(togglebutton) //-------------------------------------------------------- // GtkCheckButton //-------------------------------------------------------- checkbutton := gtk.NewCheckButtonWithLabel("CheckButton with label") checkbutton.Connect("toggled", func() { if checkbutton.GetActive() { checkbutton.SetLabel("CheckButton CHECKED!") } else { checkbutton.SetLabel("CheckButton UNCHECKED!") } }) buttons.Add(checkbutton) //-------------------------------------------------------- // GtkRadioButton //-------------------------------------------------------- buttonbox := gtk.NewVBox(false, 1) radiofirst := gtk.NewRadioButtonWithLabel(nil, "Radio1") buttonbox.Add(radiofirst) buttonbox.Add(gtk.NewRadioButtonWithLabel(radiofirst.GetGroup(), "Radio2")) buttonbox.Add(gtk.NewRadioButtonWithLabel(radiofirst.GetGroup(), "Radio3")) buttons.Add(buttonbox) //radiobutton.SetMode(false); radiofirst.SetActive(true) framebox2.PackStart(buttons, false, false, 0) //-------------------------------------------------------- // GtkVSeparator //-------------------------------------------------------- vsep := gtk.NewVSeparator() framebox2.PackStart(vsep, false, false, 0) //-------------------------------------------------------- // GtkComboBoxEntry //-------------------------------------------------------- combos := gtk.NewHBox(false, 1) comboboxentry := gtk.NewComboBoxEntryNewText() comboboxentry.AppendText("Monkey") comboboxentry.AppendText("Tiger") comboboxentry.AppendText("Elephant") comboboxentry.Connect("changed", func() { println("value:", comboboxentry.GetActiveText()) }) combos.Add(comboboxentry) //-------------------------------------------------------- // GtkComboBox //-------------------------------------------------------- combobox := gtk.NewComboBoxNewText() combobox.AppendText("Peach") combobox.AppendText("Banana") combobox.AppendText("Apple") combobox.SetActive(1) combobox.Connect("changed", func() { println("value:", combobox.GetActiveText()) }) combos.Add(combobox) framebox2.PackStart(combos, false, false, 0) //-------------------------------------------------------- // GtkTextView //-------------------------------------------------------- swin := gtk.NewScrolledWindow(nil, nil) swin.SetPolicy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) swin.SetShadowType(gtk.SHADOW_IN) textview := gtk.NewTextView() var start, end gtk.TextIter buffer := textview.GetBuffer() buffer.GetStartIter(&start) buffer.Insert(&start, "Hello ") buffer.GetEndIter(&end) buffer.Insert(&end, "World!") tag := buffer.CreateTag("bold", map[string]string{ "background": "#FF0000", "weight": "700"}) buffer.GetStartIter(&start) buffer.GetEndIter(&end) buffer.ApplyTag(tag, &start, &end) swin.Add(textview) framebox2.Add(swin) buffer.Connect("changed", func() { println("changed") }) //-------------------------------------------------------- // GtkMenuItem //-------------------------------------------------------- cascademenu := gtk.NewMenuItemWithMnemonic("_File") menubar.Append(cascademenu) submenu := gtk.NewMenu() cascademenu.SetSubmenu(submenu) menuitem = gtk.NewMenuItemWithMnemonic("E_xit") menuitem.Connect("activate", func() { gtk.MainQuit() }) submenu.Append(menuitem) cascademenu = gtk.NewMenuItemWithMnemonic("_View") menubar.Append(cascademenu) submenu = gtk.NewMenu() cascademenu.SetSubmenu(submenu) checkmenuitem := gtk.NewCheckMenuItemWithMnemonic("_Disable") checkmenuitem.Connect("activate", func() { vpaned.SetSensitive(!checkmenuitem.GetActive()) }) submenu.Append(checkmenuitem) menuitem = gtk.NewMenuItemWithMnemonic("_Font") menuitem.Connect("activate", func() { fsd := gtk.NewFontSelectionDialog("Font") fsd.SetFontName(fontbutton.GetFontName()) fsd.Response(func() { println(fsd.GetFontName()) fontbutton.SetFontName(fsd.GetFontName()) fsd.Destroy() }) fsd.SetTransientFor(window) fsd.Run() }) submenu.Append(menuitem) cascademenu = gtk.NewMenuItemWithMnemonic("_Help") menubar.Append(cascademenu) submenu = gtk.NewMenu() cascademenu.SetSubmenu(submenu) menuitem = gtk.NewMenuItemWithMnemonic("_About") menuitem.Connect("activate", func() { dialog := gtk.NewAboutDialog() dialog.SetName("Go-Gtk Demo!") dialog.SetProgramName("demo") dialog.SetAuthors(authors()) dir, _ := path.Split(os.Args[0]) imagefile := path.Join(dir, "../../data/johnbartholomew-logo.png") pixbuf, _ := gdkpixbuf.NewFromFile(imagefile) dialog.SetLogo(pixbuf) dialog.SetLicense("The library is available under the same terms and conditions as the Go, the BSD style license, and the LGPL (Lesser GNU Public License). The idea is that if you can use Go (and Gtk) in a project, you should also be able to use go-gtk.") dialog.SetWrapLicense(true) dialog.Run() dialog.Destroy() }) submenu.Append(menuitem) //-------------------------------------------------------- // GtkStatusbar //-------------------------------------------------------- statusbar := gtk.NewStatusbar() context_id := statusbar.GetContextId("go-gtk") statusbar.Push(context_id, "GTK binding for Go!") framebox2.PackStart(statusbar, false, false, 0) //-------------------------------------------------------- // Event //-------------------------------------------------------- window.Add(vbox) window.SetSizeRequest(600, 600) window.ShowAll() gtk.Main() }
func main() { gtk.Init(&os.Args) window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL) window.SetTitle("GTK DrawingArea") window.Connect("destroy", gtk.MainQuit) vbox := gtk.NewVBox(true, 0) vbox.SetBorderWidth(5) drawingarea := gtk.NewDrawingArea() var p1, p2 point var gdkwin *gdk.Window var pixmap *gdk.Pixmap var gc *gdk.GC p1.x = -1 p1.y = -1 drawingarea.Connect("configure-event", func() { if pixmap != nil { pixmap.Unref() } var allocation gtk.Allocation drawingarea.GetAllocation(&allocation) pixmap = gdk.NewPixmap(drawingarea.GetWindow().GetDrawable(), allocation.Width, allocation.Height, 24) gc = gdk.NewGC(pixmap.GetDrawable()) gc.SetRgbFgColor(gdk.NewColor("white")) pixmap.GetDrawable().DrawRectangle(gc, true, 0, 0, -1, -1) gc.SetRgbFgColor(gdk.NewColor("black")) gc.SetRgbBgColor(gdk.NewColor("white")) }) drawingarea.Connect("motion-notify-event", func(ctx *glib.CallbackContext) { if gdkwin == nil { gdkwin = drawingarea.GetWindow() } arg := ctx.Args(0) mev := *(**gdk.EventMotion)(unsafe.Pointer(&arg)) var mt gdk.ModifierType if mev.IsHint != 0 { gdkwin.GetPointer(&p2.x, &p2.y, &mt) } else { p2.x, p2.y = int(mev.X), int(mev.Y) } if p1.x != -1 && p2.x != -1 && (gdk.EventMask(mt)&gdk.BUTTON_PRESS_MASK) != 0 { pixmap.GetDrawable().DrawLine(gc, p1.x, p1.y, p2.x, p2.y) drawingarea.GetWindow().Invalidate(nil, false) } p1 = p2 }) drawingarea.Connect("expose-event", func() { if pixmap != nil { drawingarea.GetWindow().GetDrawable().DrawDrawable(gc, pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1) } }) drawingarea.SetEvents(int(gdk.POINTER_MOTION_MASK | gdk.POINTER_MOTION_HINT_MASK | gdk.BUTTON_PRESS_MASK)) vbox.Add(drawingarea) window.Add(vbox) window.SetSizeRequest(400, 400) window.ShowAll() gtk.Main() }