Added FontButton.

This commit is contained in:
Pietro Gagliardi 2018-08-19 17:22:12 -04:00
parent bd0dbfb686
commit 8281a89465
2 changed files with 81 additions and 13 deletions

69
fontbutton.go Normal file
View File

@ -0,0 +1,69 @@
// 12 december 2015
package ui
import (
"unsafe"
)
// #include <stdlib.h>
// #include "ui.h"
// #include "util.h"
// extern void doFontButtonOnChanged(uiFontButton *, void *);
// // see golang/go#19835
// typedef void (*fontButtonCallback)(uiFontButton *, void *);
// static inline uiFontDescriptor *pkguiNewFontDescriptor(void)
// {
// return (uiFontDescriptor *) pkguiAlloc(sizeof (uiFontDescriptor));
// }
// static inline void pkguiFreeFontDescriptor(uiFontDescriptor *fd)
// {
// free(fd);
// }
import "C"
// FontButton is a Control that represents a button that the user can
// click to select a font.
type FontButton struct {
ControlBase
b *C.uiFontButton
onChanged func(*FontButton)
}
// NewFontButton creates a new FontButton.
func NewFontButton() *FontButton {
b := new(FontButton)
b.b = C.uiNewFontButton()
C.uiFontButtonOnChanged(b.b, C.fontButtonCallback(C.doFontButtonOnChanged), nil)
b.ControlBase = NewControlBase(b, uintptr(unsafe.Pointer(b.b)))
return b
}
// Font returns the font currently selected in the FontButton.
func (b *FontButton) Font() *FontDescriptor {
cfd := C.pkguiNewFontDescriptor()
defer C.pkguiFreeFontDescriptor(cfd)
C.uiFontButtonFont(b.b, cfd)
defer C.uiFreeFontButtonFont(cfd)
fd := &FontDescriptor{}
fd.fromLibui(cfd)
return fd
}
// OnChanged registers f to be run when the user changes the
// currently selected font in the FontButton. Only one function can
// be registered at a time.
func (b *FontButton) OnChanged(f func(*FontButton)) {
b.onChanged = f
}
//export doFontButtonOnChanged
func doFontButtonOnChanged(bb *C.uiFontButton, data unsafe.Pointer) {
b := ControlFromLibui(uintptr(unsafe.Pointer(bb))).(*FontButton)
if b.onChanged != nil {
b.onChanged(b)
}
}

View File

@ -8,6 +8,8 @@ import (
"github.com/andlabs/ui" "github.com/andlabs/ui"
) )
var fontButton *ui.FontButton
var attrstr *ui.AttributedString var attrstr *ui.AttributedString
func appendWithAttributes(what string, attrs ...ui.Attribute) { func appendWithAttributes(what string, attrs ...ui.Attribute) {
@ -78,13 +80,7 @@ type areaHandler struct{}
func (areaHandler) Draw(a *ui.Area, p *ui.AreaDrawParams) { func (areaHandler) Draw(a *ui.Area, p *ui.AreaDrawParams) {
tl := ui.DrawNewTextLayout(&ui.DrawTextLayoutParams{ tl := ui.DrawNewTextLayout(&ui.DrawTextLayoutParams{
String: attrstr, String: attrstr,
DefaultFont: &ui.FontDescriptor{ DefaultFont: fontButton.Font(),
Family: "Helvetica",
Size: 12,
Weight: ui.TextWeightNormal,
Italic: ui.TextItalicNormal,
Stretch: ui.TextStretchNormal,
},
Width: p.AreaWidth, Width: p.AreaWidth,
Align: ui.DrawTextAlignLeft, Align: ui.DrawTextAlignLeft,
}) })
@ -130,13 +126,17 @@ func setupUI() {
vbox := ui.NewVerticalBox() vbox := ui.NewVerticalBox()
vbox.SetPadded(true) vbox.SetPadded(true)
// hbox.Append(vbox, false) hbox.Append(vbox, false)
area := ui.NewArea(areaHandler{})
fontButton = ui.NewFontButton()
fontButton.OnChanged(func(*ui.FontButton) {
area.QueueRedrawAll()
})
vbox.Append(fontButton, false)
/* /*
fontButton = uiNewFontButton();
uiFontButtonOnChanged(fontButton, onFontChanged, NULL);
uiBoxAppend(vbox, uiControl(fontButton), 0);
form = uiNewForm(); form = uiNewForm();
uiFormSetPadded(form, 1); uiFormSetPadded(form, 1);
// TODO on OS X if this is set to 1 then the window can't resize; does the form not have the concept of stretchy trailing space? // TODO on OS X if this is set to 1 then the window can't resize; does the form not have the concept of stretchy trailing space?
@ -152,7 +152,6 @@ func setupUI() {
uiFormAppend(form, "Alignment", uiControl(alignment), 0); uiFormAppend(form, "Alignment", uiControl(alignment), 0);
*/ */
area := ui.NewArea(areaHandler{})
hbox.Append(area, true) hbox.Append(area, true)
mainwin.Show() mainwin.Show()