- Forked from pwiecz/go-fltk with commit hash
5313f8a5a643c8b4f71dabd084cefb9437daa8a7
rebased - A simple wrapper around the FLTK 1.4 library, a lightweight GUI library that allows creating small, standalone and fast GUI applications.
-
To build
fltk_go
, in addition to theGolang compiler
, you also need aC++11 compiler
, -
GCC
orClang
onLinux
-
MinGW64
onWindows
-
XCode
onMacOS
. -
fltk_go
comes with prebuiltFLTK
libraries for some architectures (linux/amd64
,windows/amd64
), but you can easily rebuild them yourself, or build them for other architectures. To build theFLTK
library for your platform, just run go generate from the root of thefltk_go
source tree. -
To run programs built with fltk_go, you will need some system libraries that are typically available on operating systems with a graphical user interface:
-
Windows: no external dependencies except
mingw64
(msys2's mingw64 is recommended) -
MacOS: no external dependencies
-
Linux (and other untested Unix systems): you will need:
-
X11
-
Xrender
-
Xcursor
-
Xfixes
-
Xext
-
Xft
-
Xinerama
-
OpenGL
- You can use the
fltk_go.New<WidgetType>
function to create widgets and make modifications to the widget you are instantiating. Function and method names are similar to the original C++ names, but follow the Go language's PascalCase naming convention.
Setter methods are prefixed with Set
.
package main
import "github.com/george012/fltk_go"
func main() {
win := fltk_go.NewWindow(400, 300)
win.SetLabel("Main Window")
btn := fltk_go.NewButton(160, 200, 80, 30, "Click")
btn.SetCallback(func() {
btn.SetLabel("Clicked")
})
win.End()
win.Show()
fltk_go.Run()
}
FLTK provides 4 built-in styles:
- base (default)
- gtk+
- gleam
- plastic
For example, you can use
fltk_go.SetScheme("gtk+")
to set these styles.
FLTK Also allows customizing the style of the widget:
package main
import (
"strconv"
"github.com/george012/fltk_go"
)
// FLTK uses RGBI color representation, where I is an index into the FLTK color table
// Passing 00 as I will use RGB values
const GRAY = 0x75757500
const LIGHT_GRAY = 0xeeeeee00
const BLUE = 0x42A5F500
const SEL_BLUE = 0x2196F300
const WIDTH = 600
const HEIGHT = 400
func main() {
curr := 0
fltk_go.InitStyles()
win := fltk_go.NewWindow(WIDTH, HEIGHT)
win.SetLabel("Flutter-like")
win.SetColor(fltk_go.WHITE)
bar := fltk_go.NewBox(fltk_go.FLAT_BOX, 0, 0, WIDTH, 60, " FLTK App!")
bar.SetDrawHandler(func() { // Shadow under the bar
fltk_go.DrawBox(fltk_go.FLAT_BOX, 0, 0, WIDTH, 63, LIGHT_GRAY)
})
bar.SetAlign(fltk_go.ALIGN_INSIDE | fltk_go.ALIGN_LEFT)
bar.SetLabelColor(255) // this uses the index into the color map, here it's white
bar.SetColor(BLUE)
bar.SetLabelSize(22)
text := fltk_go.NewBox(fltk_go.NO_BOX, 250, 180, 100, 40, "You have pushed the button this many times:")
text.SetLabelSize(18)
text.SetLabelFont(fltk_go.TIMES)
count := fltk_go.NewBox(fltk_go.NO_BOX, 250, 180+40, 100, 40, "0")
count.SetLabelSize(36)
count.SetLabelColor(GRAY)
btn := fltk_go.NewButton(WIDTH-100, HEIGHT-100, 60, 60, "@+6plus") // This translates to a plus sign
btn.SetColor(BLUE)
btn.SetSelectionColor(SEL_BLUE)
btn.SetLabelColor(255)
btn.SetBox(fltk_go.OFLAT_BOX)
btn.ClearVisibleFocus()
btn.SetCallback(func() {
curr += 1
count.SetLabel(strconv.Itoa(curr))
})
win.End()
win.Show()
fltk_go.Run()
}
Label properties can be viewed here
FLTK supports both vector and raster graphics, through several image types:
- SvgImage
- RgbImage
- JpegImage
- PngImage
- BmpImage
- SharedImage
Some of these can be instantiated from image files or data:
package main
import (
"fmt"
"github.com/george012/fltk_go"
func main() {
win := fltk_go.NewWindow(400, 300)
box := fltk_go.NewBox(fltk_go.FLAT_BOX, 0, 0, 400, 300, "")
image, err := fltk_go.NewJpegImageLoad("image.jpg")
if err != nil {
fmt.Printf("An error occurred: %s\n", err)
} else {
box.SetImage(image)
}
win.End()
win.Show()
fltk_go.Run()
}