Skip to content

Commit

Permalink
A new example that shows how to make a simple form and set the applic…
Browse files Browse the repository at this point in the history
…ation's scale and theme.
  • Loading branch information
mark-summerfield committed Apr 26, 2023
1 parent bbadd89 commit d4af4da
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions examples/config/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
"strings"

"github.com/pwiecz/go-fltk"
)

const (
pad = 6
rowHeight = 32
colWidth = 60
)

func main() {
window := makeWindow()
window.Show()
fltk.Run()
}

func makeWindow() *fltk.Window {
width := 200
height := 80
window := fltk.NewWindow(width, height)
window.SetLabel("Config")
makeWidgets(width, height)
window.End()
return window
}

func makeWidgets(width, height int) {
colFlex := fltk.NewFlex(0, 0, width, height)
colFlex.SetSpacing(pad)
rowFlex := makeScaleRow(width, rowHeight)
colFlex.Fixed(rowFlex, rowHeight)
rowFlex = makeThemeRow(rowHeight, rowHeight)
colFlex.Fixed(rowFlex, rowHeight)
colFlex.End()
}

func makeScaleRow(width, height int) *fltk.Flex {
rowFlex := fltk.NewFlex(0, 0, width, height)
rowFlex.SetType(fltk.ROW)
rowFlex.SetSpacing(pad)
scaleLabel := makeAccelLabel(colWidth, rowHeight, "&Scale")
scaleSpinner := makeScaleSpinner()
scaleLabel.SetCallback(func() { scaleSpinner.TakeFocus() })
rowFlex.Fixed(scaleLabel, colWidth)
rowFlex.End()
scaleSpinner.TakeFocus()
return rowFlex
}

func makeScaleSpinner() *fltk.Spinner {
spinner := fltk.NewSpinner(0, 0, colWidth, rowHeight)
spinner.SetTooltip("Sets the application's scale.")
spinner.SetType(fltk.SPINNER_FLOAT_INPUT)
spinner.SetMinimum(0.5)
spinner.SetMaximum(3.5)
spinner.SetStep(0.1)
spinner.SetValue(1.0)
spinner.SetCallback(func() {
fltk.SetScreenScale(0, float32(spinner.Value()))
})
return spinner
}

func makeThemeRow(width, height int) *fltk.Flex {
rowFlex := fltk.NewFlex(0, 0, width, height)
rowFlex.SetType(fltk.ROW)
rowFlex.SetSpacing(pad)
themeLabel := makeAccelLabel(colWidth, rowHeight, "&Theme")
themeChoice := makeThemeChoice()
themeLabel.SetCallback(func() { themeChoice.TakeFocus() })
rowFlex.Fixed(themeLabel, colWidth)
rowFlex.End()
return rowFlex
}

func makeThemeChoice() *fltk.Choice {
choice := fltk.NewChoice(0, 0, colWidth, rowHeight)
choice.SetTooltip("Sets the application's theme.")
for i, name := range []string{"&Base", "&Gleam", "G&tk", "&Oxy", "&Plastic"} {
theme := strings.ReplaceAll(name, "&", "")
if theme == "Oxy" {
choice.SetValue(i)
fltk.SetScheme(theme)
}
choice.Add(name, func() { fltk.SetScheme(theme) })
}
return choice
}

func makeAccelLabel(width, height int, label string) *fltk.Button {
button := fltk.NewButton(0, 0, width, height, label)
button.SetAlign(fltk.ALIGN_INSIDE | fltk.ALIGN_LEFT)
button.SetBox(fltk.NO_BOX)
button.ClearVisibleFocus()
return button
}

0 comments on commit d4af4da

Please sign in to comment.