clui/label.go

59 lines
1.1 KiB
Go
Raw Normal View History

2015-09-22 11:54:39 +08:00
package clui
2015-10-17 01:27:43 +08:00
import (
_ "fmt"
term "github.com/nsf/termbox-go"
)
2015-09-22 11:54:39 +08:00
2015-10-17 01:27:43 +08:00
type Label struct {
ControlBase
direction Direction
2015-09-22 11:54:39 +08:00
}
2015-10-17 01:27:43 +08:00
func NewLabel(view View, parent Control, w, h int, title string, scale int) *Label {
c := new(Label)
2015-09-22 11:54:39 +08:00
2015-10-17 01:27:43 +08:00
c.view = view
c.parent = parent
c.minW, c.minH = 1, 1
2015-09-22 11:54:39 +08:00
2015-10-17 01:27:43 +08:00
c.SetTitle(title)
c.SetSize(w, h)
c.SetConstraints(w, h)
c.tabSkip = true
2015-09-22 11:54:39 +08:00
2015-10-17 01:27:43 +08:00
if parent != nil {
parent.AddChild(c, scale)
2015-09-22 11:54:39 +08:00
}
2015-10-17 01:27:43 +08:00
return c
2015-09-22 11:54:39 +08:00
}
func (l *Label) Direction() Direction {
return l.direction
}
func (l *Label) SetDirection(dir Direction) {
l.direction = dir
}
2015-10-17 01:27:43 +08:00
func (l *Label) Repaint() {
canvas := l.view.Canvas()
tm := l.view.Screen().Theme()
2015-10-20 03:05:43 +08:00
2015-10-17 01:27:43 +08:00
fg, bg := RealColor(tm, l.fg, ColorText), RealColor(tm, l.bg, ColorBack)
2015-10-20 03:05:43 +08:00
if !l.Enabled() {
fg = RealColor(tm, l.fg, ColorDisabledText)
}
2015-09-22 11:54:39 +08:00
2015-10-17 01:27:43 +08:00
canvas.FillRect(l.x, l.y, l.width, l.height, term.Cell{Ch: ' ', Fg: fg, Bg: bg})
2015-09-22 11:54:39 +08:00
if l.direction == Horizontal {
shift, text := AlignText(l.title, l.width, l.align)
canvas.PutText(l.x+shift, l.y, text, fg, bg)
} else {
shift, text := AlignText(l.title, l.height, l.align)
canvas.PutVerticalText(l.x, l.y+shift, text, fg, bg)
}
2015-09-22 11:54:39 +08:00
}