Documentation
¶
Overview ¶
Package gtk4 binds the parts of GTK4 a windowing toolkit needs — windows, the common controls, GObject signals and the GLib main loop — from pure Go with CGO_ENABLED=0, over github.com/ebitengine/purego. It is the Linux sibling of github.com/go-macos/objc: the shared native-toolkit bridge a go-widgets host backend embeds real controls through.
It links no cgo: libgtk-4, libgobject-2.0 and libglib-2.0 are dlopen'd by soname and every entry point is resolved with purego.RegisterLibFunc, exactly as the feasibility spike proved on a stock Linux GTK4. A GObject signal reaches Go through a purego.NewCallback, so a control's clicks and edits are ordinary Go closures.
Index ¶
- Constants
- Variables
- func IdleAdd(fn func())
- func Init() (ok bool, err error)
- func KeyvalToUnicode(keyval uint) rune
- func TextureFromRGBA(data []byte, width, height int) uintptr
- type MainLoop
- type Picture
- type Widget
- func BoxNew(horizontal bool) Widget
- func ButtonNewWithLabel(label string) Widget
- func CheckButtonNewWithLabel(label string) Widget
- func ColorNew() Widget
- func ComboNew(items []string) Widget
- func DateNew() Widget
- func DrawingAreaNew() Widget
- func EntryNew() Widget
- func FixedNew() Widget
- func LabelNew(text string) Widget
- func LinkNew(label string) Widget
- func ListBoxNew() Widget
- func PopUpNew(items []string) Widget
- func ProgressNew() Widget
- func SearchNew() Widget
- func SliderNew(min, max, step float64) Widget
- func SpinnerNew() Widget
- func StepperNew(min, max, step float64) Widget
- func TextViewNew() Widget
- func ToggleButtonNewWithLabel(label string) Widget
- func WindowNew() Widget
- func (w Widget) Active() bool
- func (w Widget) AddTickCallback(fn func() bool) uint64
- func (w Widget) Append(child Widget)
- func (w Widget) Buffer() Widget
- func (w Widget) ColorHex() string
- func (w Widget) ComboText() string
- func (w Widget) Connect(signal string, fn func()) uint64
- func (w Widget) ConnectBufferChanged(fn func()) uint64
- func (w Widget) DateISO() string
- func (w Widget) ListBoxAppendText(text string)
- func (f Widget) Move(child Widget, x, y float64)
- func (w Widget) OnActivateLink(fn func()) uint64
- func (w Widget) OnKey(fn func(keyval, keycode, state uint, press bool))
- func (w Widget) OnMotion(fn func(state uint, x, y float64))
- func (w Widget) OnMouseDown(fn func(button int, state uint, x, y float64))
- func (w Widget) OnMouseUp(fn func(button int, state uint, x, y float64))
- func (w Widget) OnScroll(fn func(dx, dy float64, state uint))
- func (w Widget) Present()
- func (f Widget) Put(child Widget, x, y float64)
- func (w Widget) QueueDraw()
- func (w Widget) SelectRow(i int)
- func (w Widget) Selected() int
- func (w Widget) SelectedRow() int
- func (w Widget) SetActive(on bool)
- func (w Widget) SetChild(child Widget)
- func (w Widget) SetColorHex(s string)
- func (w Widget) SetComboText(s string)
- func (w Widget) SetDateISO(s string)
- func (w Widget) SetDefaultSize(width, height int)
- func (w Widget) SetFraction(f float64)
- func (w Widget) SetGroup(group Widget)
- func (w Widget) SetLabel(label string)
- func (w Widget) SetSelected(i int)
- func (w Widget) SetSizeRequest(width, height int)
- func (w Widget) SetSpinValue(v float64)
- func (w Widget) SetText(s string)
- func (w Widget) SetTextViewText(s string)
- func (w Widget) SetTitle(title string)
- func (w Widget) SetValue(v float64)
- func (w Widget) SetVisibility(vis bool)
- func (w Widget) SetVisible(vis bool)
- func (w Widget) SpinValue() float64
- func (w Widget) Start()
- func (w Widget) Stop()
- func (w Widget) Text() string
- func (w Widget) TextViewText() string
- func (w Widget) Unparent()
- func (w Widget) Value() float64
Constants ¶
const ( ModShift uint = 1 << 0 // GDK_SHIFT_MASK ModControl uint = 1 << 2 // GDK_CONTROL_MASK ModAlt uint = 1 << 3 // GDK_ALT_MASK (Mod1) ModSuper uint = 1 << 26 // GDK_SUPER_MASK )
GDK modifier-state bits, as they arrive in a controller's current event state and in a key event's state argument. A host decodes the four the toolkit cares about; the lock and button-mask bits are left alone.
const MemoryR8G8B8A8 = 5
MemoryR8G8B8A8 is GdkMemoryFormat GDK_MEMORY_R8G8B8A8: four bytes per pixel in R, G, B, A order, not premultiplied — the layout a go-widgets pixel buffer uses, so a frame maps to a GdkTexture with no conversion.
Variables ¶
var ErrUnsupported = errors.New("gtk4: only available on Linux")
ErrUnsupported is returned by Init on any platform without GTK4 (everything but Linux). It is defined on every platform so a portable consumer can test for it with errors.Is.
Functions ¶
func IdleAdd ¶
func IdleAdd(fn func())
IdleAdd schedules fn to run once on the main loop and be removed. A host uses it to marshal work onto the GTK thread. fn runs on the main loop thread.
func Init ¶
Init loads GTK4 and initialises it, reporting whether a display could be opened. It is safe to call more than once. An error means the libraries could not be loaded at all (not merely that there is no display).
func KeyvalToUnicode ¶ added in v0.3.0
KeyvalToUnicode returns the Unicode rune a GDK keyval produces, or 0 for a non-printable (named) key such as Return or an arrow. It is gdk_keyval_to_unicode, so a host maps named keys itself and takes the rune for the rest.
func TextureFromRGBA ¶ added in v0.2.0
TextureFromRGBA builds a GdkTexture from width*height*4 bytes of R8G8B8A8. The bytes are copied (g_bytes_new copies), so the caller may reuse its buffer at once. The returned handle is a GdkTexture with one reference the caller owns.
Types ¶
type Picture ¶ added in v0.2.0
type Picture Widget
A Picture is a GtkPicture that shows an RGBA image a host refreshes each frame with Picture.SetRGBA — the widget the GTK4-hosted go-widgets backend puts the toolkit's pixel framebuffer into, with native controls overlaid above it.
func PictureNew ¶ added in v0.2.0
func PictureNew() Picture
PictureNew creates a picture widget. It returns the null Picture if the pixel entry points cannot be loaded.
type Widget ¶
type Widget uintptr
A Widget is a GTK widget — a GObject pointer. The zero value is the null widget. Methods on it are thin wrappers over the C API.
func BoxNew ¶ added in v0.5.0
BoxNew creates a GtkBox with zero spacing, horizontal (0) or vertical (1). It is the container a host composes a segmented control from, appending linked toggle buttons; visual linking (the .linked style class) is the host's job.
func ButtonNewWithLabel ¶
ButtonNewWithLabel creates a push button.
func CheckButtonNewWithLabel ¶
CheckButtonNewWithLabel creates a labelled check button. GTK check buttons that share a group act as radios; a host groups them for a NativeRadio.
func ColorNew ¶ added in v0.5.0
func ColorNew() Widget
ColorNew creates a GtkColorButton. Read and write the colour as "#RRGGBB" with ColorHex/SetColorHex; connect "color-set" for changes.
func ComboNew ¶ added in v0.5.0
ComboNew creates an editable GtkComboBoxText prefilled with items (one append per string). Read and write the shown text with ComboText/SetComboText; connect "changed" for edits.
func DateNew ¶ added in v0.5.0
func DateNew() Widget
DateNew creates a GtkCalendar. Read and write the selected day as "YYYY-MM-DD" with DateISO/SetDateISO; connect "day-selected" for changes.
func DrawingAreaNew ¶ added in v0.2.0
func DrawingAreaNew() Widget
DrawingAreaNew creates a GtkDrawingArea. QueueDraw asks a widget to repaint.
func EntryNew ¶
func EntryNew() Widget
EntryNew creates a single-line text entry. Call SetVisibility(false) for a secure (password) entry.
func FixedNew ¶
func FixedNew() Widget
FixedNew creates a GtkFixed — the container a host overlays native controls in at absolute positions, over the pixel drawing area.
func LinkNew ¶ added in v0.5.0
LinkNew creates a GtkLinkButton showing label with an empty URI, so GTK opens nothing on its own. Wire OnActivateLink for a plain activation.
func ListBoxNew ¶ added in v0.6.0
func ListBoxNew() Widget
ListBoxNew creates a GtkListBox — a single-column selectable list. Append rows with ListBoxAppendText, read/write the selection with SelectedRow/SelectRow, and connect "row-selected" for changes.
func PopUpNew ¶ added in v0.4.0
PopUpNew creates a GtkDropDown listing items. The items are copied into a GtkStringList (one append per string, so no C string-array marshalling is needed). Read and write the selection index with Selected/SetSelected; connect "notify::selected" for changes.
func ProgressNew ¶ added in v0.5.0
func ProgressNew() Widget
ProgressNew creates a GtkProgressBar. It is read-only: set its position with SetFraction (0..1); there is no change signal (a host drives it).
func SearchNew ¶ added in v0.5.0
func SearchNew() Widget
SearchNew creates a GtkSearchEntry. It implements GtkEditable, so Text/SetText work on it directly; connect "search-changed" for edits.
func SliderNew ¶ added in v0.4.0
SliderNew creates a horizontal GtkScale over [min,max] with the given step. Read and write its value with Value/SetValue; connect "value-changed" for edits.
func SpinnerNew ¶ added in v0.5.0
func SpinnerNew() Widget
SpinnerNew creates a GtkSpinner — an indeterminate activity indicator. Start and Stop animate it; it has no value.
func StepperNew ¶ added in v0.5.0
StepperNew creates a GtkSpinButton over [min,max] with the given step. Read and write its value with SpinValue/SetSpinValue; connect "value-changed" for edits.
func TextViewNew ¶ added in v0.5.0
func TextViewNew() Widget
TextViewNew creates a multi-line GtkTextView. Read and write its whole contents with TextViewText/SetTextViewText. The change signal lives on the underlying GtkTextBuffer, not the view: connect it with ConnectBufferChanged, or reach the buffer with Buffer and connect "changed" yourself.
func ToggleButtonNewWithLabel ¶ added in v0.5.0
ToggleButtonNewWithLabel creates a labelled GtkToggleButton. Group several with SetGroup for a mutually-exclusive (segmented) set; read and write pressed state with the shared Active/SetActive, and connect "toggled" for changes.
func (Widget) Active ¶
Active and SetActive read and write the pressed state of a check button OR a toggle button — GtkCheckButton and GtkToggleButton are distinct types in GTK4 with separate accessors, so the method dispatches on the widget's runtime type (GTK_IS_TOGGLE_BUTTON). This lets both the check/radio controls and the segmented toggles share one Active/SetActive pair.
func (Widget) AddTickCallback ¶ added in v0.2.4
AddTickCallback registers fn to run once per frame, driven by this widget's GdkFrameClock, for as long as fn returns true. It is the GTK-native animation tick: aligned to the display's refresh, and quiescent while the widget is unmapped (the frame clock does not run then), so an idle hidden window costs nothing. A host that renders its own pixels into a Picture uses it to present a fresh frame each vsync — unlike a one-shot IdleAdd, which the frame clock would fire before the window is even mapped. fn runs on the main-loop thread; returning false removes the callback. The callback is retained for the process life, like Widget.Connect's.
func (Widget) Buffer ¶ added in v0.5.0
Buffer returns the view's GtkTextBuffer (as a Widget handle — it is a GObject a host connects signals on, not a displayable widget).
func (Widget) ColorHex ¶ added in v0.5.0
ColorHex returns the chosen colour as "#RRGGBB". GdkRGBA is four C floats (red,green,blue,alpha), 16 bytes; alpha is dropped.
func (Widget) ComboText ¶ added in v0.5.0
ComboText returns the combo's active text — for an editable combo, the text in its entry. gtk_combo_box_text_get_active_text returns a freshly g_malloc'd string that purego copies into Go; the C copy is not freed, a small acceptable per-call leak (no free plumbing, as agreed).
func (Widget) Connect ¶
Connect wires a GObject signal (e.g. "clicked", "changed") to a Go func. The callback keeps the widget's value reachable through its own accessors, so the zero-argument closure is enough for the control signals a host cares about. The returned handler id is GObject's; a host rarely needs it.
The callback is retained for the process life (like the go-macos target classes): GTK holds a C pointer to it, and letting Go collect it would leave a dangling call.
func (Widget) ConnectBufferChanged ¶ added in v0.5.0
ConnectBufferChanged connects fn to the view's buffer "changed" signal — fired on every edit. It resolves the buffer for the host.
func (Widget) DateISO ¶ added in v0.5.0
DateISO returns the selected day as "YYYY-MM-DD". gtk_calendar_get_date hands back a GDateTime we own and unref.
func (Widget) ListBoxAppendText ¶ added in v0.6.0
ListBoxAppendText appends a row holding a left-aligned label with text.
func (Widget) OnActivateLink ¶ added in v0.5.0
OnActivateLink connects fn to "activate-link", whose handler returns gboolean. The bound callback returns TRUE, which suppresses GTK's own URI-open so the link behaves as a plain clickable activation.
func (Widget) OnKey ¶ added in v0.3.0
OnKey fires on a key press and release that reaches w — that is, one no focused child (a native entry) consumed first. It gives the GDK keyval and keycode, the modifier state, and whether this is a press. Use KeyvalToUnicode for the rune and a keyval table for named keys.
func (Widget) OnMotion ¶ added in v0.3.0
OnMotion fires when the pointer moves over w, with the modifier state (a button mask distinguishes a drag) and the widget-local point.
func (Widget) OnMouseDown ¶ added in v0.3.0
OnMouseDown fires on a press of any mouse button over w, with the button (1 left, 2 middle, 3 right), the GDK modifier state, and the widget-local point.
func (Widget) OnScroll ¶ added in v0.3.0
OnScroll fires on a wheel or trackpad scroll over w, with the delta in both axes (units are wheel notches / trackpad steps) and the modifier state.
func (Widget) SelectRow ¶ added in v0.6.0
SelectRow selects the row at index i (a no-op if i is out of range).
func (Widget) Selected ¶ added in v0.4.0
Selected returns the selected index, or -1 when nothing is selected (GTK_INVALID_LIST_POSITION). SetSelected selects by index.
func (Widget) SelectedRow ¶ added in v0.6.0
SelectedRow returns the selected row's index, or -1 when nothing is selected.
func (Widget) SetColorHex ¶ added in v0.5.0
SetColorHex sets the colour from an "#RRGGBB" string (alpha forced opaque). A malformed string is ignored.
func (Widget) SetComboText ¶ added in v0.5.0
SetComboText sets the editable combo's text by writing into its child entry (gtk_combo_box_get_child → gtk_editable_set_text).
func (Widget) SetDateISO ¶ added in v0.5.0
SetDateISO selects the day named by an "YYYY-MM-DD" string. A malformed string or an impossible date is ignored.
func (Widget) SetDefaultSize ¶
func (Widget) SetFraction ¶ added in v0.5.0
SetFraction sets a progress bar's fill, clamped by GTK to [0,1].
func (Widget) SetGroup ¶ added in v0.5.0
SetGroup joins this toggle button to group's exclusive set. Pass the zero Widget to start a fresh group (become the group's first member).
func (Widget) SetSelected ¶ added in v0.4.0
func (Widget) SetSizeRequest ¶
SetSizeRequest fixes a widget's size (a host sizes controls to the region it laid out).
func (Widget) SetSpinValue ¶ added in v0.5.0
func (Widget) SetTextViewText ¶ added in v0.5.0
SetTextViewText replaces the whole buffer contents.
func (Widget) SetVisibility ¶
func (Widget) SetVisible ¶
SetVisible shows or hides a widget in place.
func (Widget) Text ¶
Text and SetText read and write an editable's text (entry, label via editable where applicable), through the GtkEditable interface.
func (Widget) TextViewText ¶ added in v0.5.0
TextViewText returns the whole buffer contents. A GtkTextIter is an opaque fixed-size struct filled in place; a 128-byte buffer is comfortably larger than its ~80 bytes. gtk_text_buffer_get_text returns a freshly g_malloc'd string that purego copies into Go; the C copy is not freed, a small acceptable per-call leak.