diff --git a/fontbutton.go b/fontbutton.go new file mode 100644 index 0000000..c228151 --- /dev/null +++ b/fontbutton.go @@ -0,0 +1,69 @@ +// 12 december 2015 + +package ui + +import ( + "unsafe" +) + +// #include +// #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) + } +} diff --git a/zz_drawtext.go b/zz_drawtext.go index b2c6f59..c56cf02 100644 --- a/zz_drawtext.go +++ b/zz_drawtext.go @@ -8,6 +8,8 @@ import ( "github.com/andlabs/ui" ) +var fontButton *ui.FontButton + var attrstr *ui.AttributedString func appendWithAttributes(what string, attrs ...ui.Attribute) { @@ -78,13 +80,7 @@ type areaHandler struct{} func (areaHandler) Draw(a *ui.Area, p *ui.AreaDrawParams) { tl := ui.DrawNewTextLayout(&ui.DrawTextLayoutParams{ String: attrstr, - DefaultFont: &ui.FontDescriptor{ - Family: "Helvetica", - Size: 12, - Weight: ui.TextWeightNormal, - Italic: ui.TextItalicNormal, - Stretch: ui.TextStretchNormal, - }, + DefaultFont: fontButton.Font(), Width: p.AreaWidth, Align: ui.DrawTextAlignLeft, }) @@ -130,13 +126,17 @@ func setupUI() { vbox := ui.NewVerticalBox() 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(); 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? @@ -152,7 +152,6 @@ func setupUI() { uiFormAppend(form, "Alignment", uiControl(alignment), 0); */ - area := ui.NewArea(areaHandler{}) hbox.Append(area, true) mainwin.Show()