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-07 09:44:46 -05:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
type widgetbase struct {
|
|
|
|
widget *C.GtkWidget
|
2014-07-15 19:48:16 -05:00
|
|
|
parentw *window
|
|
|
|
floating bool
|
2014-07-07 09:44:46 -05:00
|
|
|
}
|
|
|
|
|
2014-07-07 21:46:23 -05:00
|
|
|
func newWidget(w *C.GtkWidget) *widgetbase {
|
|
|
|
return &widgetbase{
|
2014-07-07 09:44:46 -05:00
|
|
|
widget: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:48:16 -05:00
|
|
|
// these few methods are embedded by all the various Controls since they all will do the same thing
|
|
|
|
|
|
|
|
func (w *widgetbase) unparent() {
|
|
|
|
if w.parentw != nil {
|
|
|
|
// add another reference so it doesn't get removed by accident
|
|
|
|
C.g_object_ref(C.gpointer(unsafe.Pointer(w.widget)))
|
|
|
|
// we unref this in parent() below
|
|
|
|
w.floating = true
|
|
|
|
C.gtk_container_remove(w.parentw.layoutc, w.widget)
|
|
|
|
w.parentw = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *widgetbase) parent(win *window) {
|
|
|
|
C.gtk_container_add(win.layoutc, w.widget)
|
|
|
|
w.parentw = win
|
|
|
|
// was previously parented; unref our saved ref
|
|
|
|
if w.floating {
|
|
|
|
C.g_object_unref(C.gpointer(unsafe.Pointer(w.widget)))
|
|
|
|
w.floating = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-07 09:44:46 -05:00
|
|
|
type button struct {
|
|
|
|
*widgetbase
|
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-19 08:44:32 -05:00
|
|
|
func newButton(text string) *button {
|
|
|
|
ctext := togstr(text)
|
|
|
|
defer freegstr(ctext)
|
|
|
|
widget := C.gtk_button_new_with_label(ctext)
|
|
|
|
b := &button{
|
|
|
|
widgetbase: newWidget(widget),
|
|
|
|
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)),
|
|
|
|
"clicked",
|
|
|
|
C.GCallback(C.buttonClicked),
|
|
|
|
C.gpointer(unsafe.Pointer(b)))
|
|
|
|
return b
|
2014-07-07 09:44:46 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|