2014-07-17 22:50:14 -05:00
|
|
|
// +build !windows,!darwin
|
2014-07-08 15:47:28 -05:00
|
|
|
|
2014-07-07 09:44:46 -05:00
|
|
|
// 7 july 2014
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #include "gtk_unix.h"
|
2014-07-17 11:14:38 -05:00
|
|
|
// extern void buttonClicked(GtkButton *, gpointer);
|
2014-07-21 20:07:14 -05:00
|
|
|
// extern void checkboxToggled(GtkToggleButton *, gpointer);
|
2014-07-07 09:44:46 -05:00
|
|
|
import "C"
|
|
|
|
|
2014-07-29 12:48:31 -05:00
|
|
|
// TODOs:
|
|
|
|
// - standalone label on its own: should it be centered or not?
|
|
|
|
|
2014-07-07 09:44:46 -05:00
|
|
|
type button struct {
|
2014-07-30 09:21:23 -05:00
|
|
|
*controlbase
|
2014-07-07 21:46:23 -05:00
|
|
|
button *C.GtkButton
|
2014-07-17 11:14:38 -05:00
|
|
|
clicked *event
|
2014-07-07 09:44:46 -05:00
|
|
|
}
|
|
|
|
|
2014-07-21 20:07:14 -05:00
|
|
|
// shared code for setting up buttons, check boxes, etc.
|
|
|
|
func finishNewButton(widget *C.GtkWidget, event string, handler unsafe.Pointer) *button {
|
2014-07-19 08:44:32 -05:00
|
|
|
b := &button{
|
2014-07-30 09:21:23 -05:00
|
|
|
controlbase: newControl(widget),
|
2014-07-19 08:44:32 -05:00
|
|
|
button: (*C.GtkButton)(unsafe.Pointer(widget)),
|
|
|
|
clicked: newEvent(),
|
2014-07-07 09:44:46 -05:00
|
|
|
}
|
2014-07-19 08:44:32 -05:00
|
|
|
g_signal_connect(
|
|
|
|
C.gpointer(unsafe.Pointer(b.button)),
|
2014-07-21 20:07:14 -05:00
|
|
|
event,
|
|
|
|
C.GCallback(handler),
|
2014-07-19 08:44:32 -05:00
|
|
|
C.gpointer(unsafe.Pointer(b)))
|
|
|
|
return b
|
2014-07-07 09:44:46 -05:00
|
|
|
}
|
|
|
|
|
2014-07-21 20:07:14 -05:00
|
|
|
func newButton(text string) *button {
|
|
|
|
ctext := togstr(text)
|
|
|
|
defer freegstr(ctext)
|
|
|
|
widget := C.gtk_button_new_with_label(ctext)
|
|
|
|
return finishNewButton(widget, "clicked", C.buttonClicked)
|
|
|
|
}
|
|
|
|
|
2014-07-19 08:44:32 -05:00
|
|
|
func (b *button) OnClicked(e func()) {
|
|
|
|
b.clicked.set(e)
|
2014-07-17 11:14:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//export buttonClicked
|
|
|
|
func buttonClicked(bwid *C.GtkButton, data C.gpointer) {
|
|
|
|
b := (*button)(unsafe.Pointer(data))
|
|
|
|
b.clicked.fire()
|
|
|
|
println("button clicked")
|
2014-07-07 15:51:17 -05:00
|
|
|
}
|
2014-07-07 09:44:46 -05:00
|
|
|
|
2014-07-19 08:44:32 -05:00
|
|
|
func (b *button) Text() string {
|
|
|
|
return fromgstr(C.gtk_button_get_label(b.button))
|
2014-07-07 09:44:46 -05:00
|
|
|
}
|
|
|
|
|
2014-07-19 08:44:32 -05:00
|
|
|
func (b *button) SetText(text string) {
|
|
|
|
ctext := togstr(text)
|
|
|
|
defer freegstr(ctext)
|
|
|
|
C.gtk_button_set_label(b.button, ctext)
|
2014-07-07 09:44:46 -05:00
|
|
|
}
|
2014-07-21 20:07:14 -05:00
|
|
|
|
|
|
|
type checkbox struct {
|
|
|
|
// embed button so its methods and events carry over
|
|
|
|
*button
|
|
|
|
toggle *C.GtkToggleButton
|
|
|
|
checkbox *C.GtkCheckButton
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCheckbox(text string) *checkbox {
|
|
|
|
ctext := togstr(text)
|
|
|
|
defer freegstr(ctext)
|
|
|
|
widget := C.gtk_check_button_new_with_label(ctext)
|
|
|
|
return &checkbox{
|
|
|
|
button: finishNewButton(widget, "toggled", C.checkboxToggled),
|
|
|
|
toggle: (*C.GtkToggleButton)(unsafe.Pointer(widget)),
|
|
|
|
checkbox: (*C.GtkCheckButton)(unsafe.Pointer(widget)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//export checkboxToggled
|
|
|
|
func checkboxToggled(bwid *C.GtkToggleButton, data C.gpointer) {
|
|
|
|
// note that the finishNewButton() call uses the embedded *button as data
|
|
|
|
// this is fine because we're only deferring to buttonClicked() anyway
|
|
|
|
buttonClicked(nil, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checkbox) Checked() bool {
|
|
|
|
return fromgbool(C.gtk_toggle_button_get_active(c.toggle))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checkbox) SetChecked(checked bool) {
|
|
|
|
C.gtk_toggle_button_set_active(c.toggle, togbool(checked))
|
|
|
|
}
|
2014-07-26 07:52:33 -05:00
|
|
|
|
|
|
|
type textField struct {
|
2014-07-30 09:21:23 -05:00
|
|
|
*controlbase
|
2014-07-26 07:52:33 -05:00
|
|
|
entry *C.GtkEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
func startNewTextField() *textField {
|
|
|
|
w := C.gtk_entry_new()
|
|
|
|
return &textField{
|
2014-07-30 09:21:23 -05:00
|
|
|
controlbase: newControl(w),
|
2014-07-26 07:52:33 -05:00
|
|
|
entry: (*C.GtkEntry)(unsafe.Pointer(w)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTextField() *textField {
|
|
|
|
return startNewTextField()
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPasswordField() *textField {
|
|
|
|
t := startNewTextField()
|
|
|
|
C.gtk_entry_set_visibility(t.entry, C.FALSE)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *textField) Text() string {
|
|
|
|
return fromgstr(C.gtk_entry_get_text(t.entry))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *textField) SetText(text string) {
|
|
|
|
ctext := togstr(text)
|
|
|
|
defer freegstr(ctext)
|
|
|
|
C.gtk_entry_set_text(t.entry, ctext)
|
|
|
|
}
|
2014-07-29 12:48:31 -05:00
|
|
|
|
|
|
|
type label struct {
|
2014-07-30 09:21:23 -05:00
|
|
|
*controlbase
|
|
|
|
misc *C.GtkMisc
|
|
|
|
label *C.GtkLabel
|
|
|
|
standalone bool
|
|
|
|
supercommitResize func(c *allocation, d *sizing)
|
2014-07-29 12:48:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func finishNewLabel(text string, standalone bool) *label {
|
|
|
|
ctext := togstr(text)
|
|
|
|
defer freegstr(ctext)
|
|
|
|
widget := C.gtk_label_new(ctext)
|
2014-07-30 09:21:23 -05:00
|
|
|
l := &label{
|
|
|
|
controlbase: newControl(widget),
|
2014-07-29 12:48:31 -05:00
|
|
|
misc: (*C.GtkMisc)(unsafe.Pointer(widget)),
|
|
|
|
label: (*C.GtkLabel)(unsafe.Pointer(widget)),
|
|
|
|
standalone: standalone,
|
|
|
|
}
|
2014-07-30 09:21:23 -05:00
|
|
|
l.supercommitResize = l.fcommitResize
|
|
|
|
l.fcommitResize = l.labelcommitResize
|
|
|
|
return l
|
2014-07-29 12:48:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func newLabel(text string) Label {
|
|
|
|
return finishNewLabel(text, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStandaloneLabel(text string) Label {
|
|
|
|
return finishNewLabel(text, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *label) Text() string {
|
|
|
|
return fromgstr(C.gtk_label_get_text(l.label))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *label) SetText(text string) {
|
|
|
|
ctext := togstr(text)
|
|
|
|
defer freegstr(ctext)
|
|
|
|
C.gtk_label_set_text(l.label, ctext)
|
|
|
|
}
|
|
|
|
|
2014-07-30 09:21:23 -05:00
|
|
|
func (l *label) labelcommitResize(c *allocation, d *sizing) {
|
2014-07-29 12:48:31 -05:00
|
|
|
if !l.standalone && c.neighbor != nil {
|
|
|
|
c.neighbor.getAuxResizeInfo(d)
|
|
|
|
if d.shouldVAlignTop {
|
|
|
|
// TODO should it be center-aligned to the first line or not
|
|
|
|
C.gtk_misc_set_alignment(l.misc, 0, 0)
|
|
|
|
} else {
|
|
|
|
C.gtk_misc_set_alignment(l.misc, 0, 0.5)
|
|
|
|
}
|
|
|
|
}
|
2014-07-30 09:21:23 -05:00
|
|
|
l.supercommitResize(c, d)
|
2014-07-29 12:48:31 -05:00
|
|
|
}
|