func uiSetup() { var err error cssProvider, err = gtk.CssProviderNew() e.Exit(err) err = cssProvider.LoadFromData(css) e.Exit(err) w.count, err = gtk.SpinButtonNewWithRange(1, 999, 1) e.Exit(err) w.count.SetValue(1) shuffle, err := gtk.ButtonNewWithLabel("Shuffle") e.Exit(err) w.showActionsText, err = gtk.CheckButtonNewWithMnemonic("Show actions _text") e.Exit(err) w.showPermutation, err = gtk.CheckButtonNewWithMnemonic("Show _permutation") e.Exit(err) w.cellSize, err = gtk.SpinButtonNewWithRange(1, 999, 1) e.Exit(err) w.cellSize.SetValue(32) cellSizeLabel, err := gtk.LabelNew("Cell size:") e.Exit(err) w.count.Connect("changed", uiShuffle) w.count.Connect("activate", uiShuffle) shuffle.Connect("clicked", uiShuffle) w.showActionsText.Connect("toggled", uiReset) w.showPermutation.Connect("toggled", uiReset) w.cellSize.Connect("changed", uiReset) panel, err := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0) e.Exit(err) panel.SetSpacing(5) panel.Add(w.count) panel.Add(shuffle) panel.Add(w.showActionsText) panel.Add(w.showPermutation) panel.Add(cellSizeLabel) panel.Add(w.cellSize) w.fieldWindow, err = gtk.ScrolledWindowNew(nil, nil) e.Exit(err) layout, err := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0) e.Exit(err) layout.Add(panel) layout.PackStart(w.fieldWindow, true, true, 0) window, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL) e.Exit(err) window.SetTitle(title) window.Add(layout) window.Connect("destroy", gtk.MainQuit) uiShuffle() window.ShowAll() }
// Setup the TextView, put it in a ScrolledWindow, and add both to box. func setupTextView(box *gtk.Box) *gtk.TextView { sw, _ := gtk.ScrolledWindowNew(nil, nil) tv, _ := gtk.TextViewNew() sw.Add(tv) box.PackStart(sw, true, true, 0) return tv }
func (v *ScrolledView) scrolledViewInit(width, height int) (err error) { v.hadj, err = gtk.AdjustmentNew(0, 0, float64(width), 1, 10, float64(width)) if err != nil { log.Println("Unable to create hadj:", err) return } v.vadj, err = gtk.AdjustmentNew(0, 0, float64(height), 1, 10, float64(height)) if err != nil { log.Println("Unable to create vadj:", err) return } v.scrolled, err = gtk.ScrolledWindowNew(v.hadj, v.vadj) if err != nil { log.Println("Unable to create scrolled:", err) return } v.scrolled.SetHExpand(true) v.scrolled.SetVExpand(true) return }
func windowWidget() *gtk.Widget { grid, err := gtk.GridNew() if err != nil { log.Fatal("Unable to create grid:", err) } grid.SetOrientation(gtk.ORIENTATION_VERTICAL) // Just as a demonstration, we create and destroy a Label without ever // adding it to a container. In native GTK, this would result in a // memory leak, since gtk_widget_destroy() will not deallocate any // memory when passed a GtkWidget with a floating reference. // // gotk3 handles this situation by always sinking floating references // of any struct type embedding a glib.InitiallyUnowned, and by setting // a finalizer to unreference the object when Go has lost scope of the // variable. Due to this design, widgets may be allocated freely // without worrying about handling memory incorrectly. // // The following code is not entirely useful (except to demonstrate // this point), but it is also not "incorrect" as the C equivalent // would be. unused, err := gtk.LabelNew("This label is never used") if err != nil { // Calling Destroy() is also unnecessary in this case. The // memory will still be freed with or without calling it. unused.Destroy() } sw, err := gtk.ScrolledWindowNew(nil, nil) if err != nil { log.Fatal("Unable to create scrolled window:", err) } grid.Attach(sw, 0, 0, 2, 1) sw.SetHExpand(true) sw.SetVExpand(true) labelsGrid, err := gtk.GridNew() if err != nil { log.Fatal("Unable to create grid:", err) } labelsGrid.SetOrientation(gtk.ORIENTATION_VERTICAL) sw.Add(labelsGrid) labelsGrid.SetHExpand(true) insertBtn, err := gtk.ButtonNewWithLabel("Add a label") if err != nil { log.Fatal("Unable to create button:", err) } removeBtn, err := gtk.ButtonNewWithLabel("Remove a label") if err != nil { log.Fatal("Unable to create button:", err) } nLabels := 1 insertBtn.Connect("clicked", func() { var s string if nLabels == 1 { s = fmt.Sprintf("Inserted %d label.", nLabels) } else { s = fmt.Sprintf("Inserted %d labels.", nLabels) } label, err := gtk.LabelNew(s) if err != nil { log.Print("Unable to create label:", err) return } labelList.PushBack(label) labelsGrid.Add(label) label.SetHExpand(true) labelsGrid.ShowAll() nLabels++ }) removeBtn.Connect("clicked", func() { e := labelList.Front() if e == nil { log.Print("Nothing to remove") return } lab, ok := labelList.Remove(e).(*gtk.Label) if !ok { log.Print("Element to remove is not a *gtk.Label") return } // (*Widget).Destroy() breaks this label's reference with all // other objects (in this case, the Grid container it was added // to). lab.Destroy() // At this point, only Go retains a reference to the GtkLabel. // When the lab variable goes out of scope when this function // returns, at the next garbage collector run, a finalizer will // be run to perform the final unreference and free the widget. }) grid.Attach(insertBtn, 0, 1, 1, 1) grid.Attach(removeBtn, 1, 1, 1, 1) return &grid.Container.Widget }
// ScrolledWindow creates a *gtk.ScrolledWindow. func ScrolledWindow(hadjustment, vadjustment *gtk.Adjustment) *gtk.ScrolledWindow { w, _ := gtk.ScrolledWindowNew(hadjustment, vadjustment) return w }