summaryrefslogtreecommitdiff
path: root/button.go
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2014-02-12 13:52:34 -0500
committerPietro Gagliardi <[email protected]>2014-02-12 13:52:34 -0500
commita272436ee6aadb67cb9f4d54633981631ba261f3 (patch)
treefe4f4a6cb136e471059f42054ef2ee013289f05f /button.go
parentda92dd55bdf20d75ffa20b42c6c6d6db0d582aeb (diff)
Whoops, forgot to add button.go itself :|
Diffstat (limited to 'button.go')
-rw-r--r--button.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/button.go b/button.go
new file mode 100644
index 0000000..58e864d
--- /dev/null
+++ b/button.go
@@ -0,0 +1,74 @@
+// 12 february 2014
+package main
+
+import (
+ "fmt"
+ "sync"
+)
+
+// A Button represents a clickable button with some text.
+type Button struct {
+ // This channel gets a message when the button is clicked. Unlike other channels in this package, this channel is initialized to non-nil when creating a new button, and cannot be set to nil later.
+ Clicked chan struct{}
+
+ lock sync.Mutex
+ created bool
+ parent Control
+ pWin *Window
+ sysData *sysData
+ initText string
+}
+
+// NewButton creates a new button with the specified text.
+func NewButton(text string) (b *Button) {
+ return &Button{
+ sysData: &sysData{
+ cSysData: cSysData{
+ ctype: c_button,
+ },
+ },
+ initText: text,
+ Clicked: make(chan struct{}),
+ }
+}
+
+// SetText sets the button's text.
+func (b *Button) SetText(text string) (err error) {
+ b.lock.Lock()
+ defer b.lock.Unlock()
+
+ if b.pWin != nil && b.pWin.created {
+ panic("TODO")
+ }
+ b.initText = text
+ return nil
+}
+
+func (b *Button) apply() error {
+ b.lock.Lock()
+ defer b.lock.Unlock()
+
+ if b.pWin == nil {
+ panic(fmt.Sprintf("button (initial text: %q) without parent window", b.initText))
+ }
+ if !b.pWin.created {
+ b.sysData.clicked = b.Clicked
+ b.sysData.parentWindow = b.pWin.sysData
+ return b.sysData.make(b.initText, 300, 300)
+ // TODO size to parent size
+ }
+ return b.sysData.show()
+}
+
+func (b *Button) setParent(c Control) {
+ b.parent = c
+ if w, ok := b.parent.(*Window); ok {
+ b.pWin = w
+ } else {
+ b.pWin = c.parentWindow()
+ }
+}
+
+func (b *Button) parentWindow() *Window {
+ return b.pWin
+}