Exemplo n.º 1
0
// OnEvent process a particularevent on the button.
func (b *Button) OnEvent(e interface{}) {
	switch e.(type) {
	case sparta.CloseEvent:
		if b.closeFn != nil {
			b.closeFn(b, e)
		}
	case sparta.ConfigureEvent:
		b.geometry = e.(sparta.ConfigureEvent).Rect
		if b.configFn != nil {
			b.configFn(b, e)
		}
	case sparta.CommandEvent:
		if b.commFn != nil {
			if b.commFn(b, e) {
				return
			}
		}
		b.parent.OnEvent(e)
	case sparta.ExposeEvent:
		if b.exposeFn != nil {
			b.exposeFn(b, e)
		}
		b.win.SetColor(sparta.Foreground, foreColor)
		if len(b.caption) > 0 {
			x := (b.geometry.Dx() - (len(b.caption) * sparta.WidthUnit)) / 2
			y := (b.geometry.Dy() - sparta.HeightUnit) / 2
			b.win.Text(image.Pt(x, y), b.caption)
		}
		rect := image.Rect(0, 0, b.geometry.Dx()-1, b.geometry.Dy()-1)
		b.win.Rectangle(rect, false)
	case sparta.KeyEvent:
		if sparta.IsBlock() {
			if !sparta.IsBlocker(b) {
				return
			}
		}
		if b.keyFn != nil {
			if b.keyFn(b, e) {
				return
			}
		}
	case sparta.MouseEvent:
		if sparta.IsBlock() {
			if !sparta.IsBlocker(b) {
				return
			}
		}
		if b.mouseFn != nil {
			if b.mouseFn(b, e) {
				return
			}
		}
		ev := e.(sparta.MouseEvent)
		if ev.Button == sparta.MouseLeft {
			sparta.SendEvent(b.target, sparta.CommandEvent{Source: b, Value: b.value})
		}
	}
}
Exemplo n.º 2
0
func main() {
	rand.Seed(time.Now().Unix())

	// The main window.
	m := widget.NewMainWindow("main", "Event Window")

	// Functions to capture the events.
	//
	// Capture close event.This event is produced when attepting to
	// close a rootless widget (such as MainWindow), or when a child
	// is closed.
	m.Capture(sparta.CloseEv, closeFunc)
	// Capture command event. This event is produced when another
	// process, or a widget send a message to another widget.
	m.Capture(sparta.Command, commandFunc)
	// Capture configure event. This event is produced when a widget
	// changes its size.
	m.Capture(sparta.Configure, configureFunc)
	// Capture Expose event. This event is produced when a previously
	// obscured part of a widget is exposed (in some backends, other
	// backends save obscured part of the widget and simply copy them
	// when the widget is exposed. Expose event are also produced when
	// the widget is resized (configure), and when an explicit update
	// method is called.
	m.Capture(sparta.Expose, exposeFunc)
	// Capture Key event. This event is produced when a key in the
	// keyboard is pressed or released.
	m.Capture(sparta.KeyEv, keyFunc)
	// Capture Mouse event. This event is produced when the mouse is
	// moved, a button is pressed or released.
	m.Capture(sparta.Mouse, mouseFunc)

	// this helper function is used to send command events to the window.
	// it will send a random number each second.
	go func() {
		for {
			time.Sleep(10 * time.Second)
			sparta.SendEvent(m, sparta.CommandEvent{Value: rand.Intn(255)})
		}
	}()

	sparta.Run()
}
Exemplo n.º 3
0
// OnEvent process a particular event on the list.
func (l *List) OnEvent(e interface{}) {
	switch e.(type) {
	case sparta.CloseEvent:
		if l.closeFn != nil {
			l.closeFn(l, e)
		}
		l.scroll.OnEvent(e)
	case sparta.ConfigureEvent:
		rect := e.(sparta.ConfigureEvent).Rect
		l.geometry = rect
		if l.configFn != nil {
			l.configFn(l, e)
		}
		l.scroll.SetProperty(sparta.Geometry, image.Rect(rect.Dx()-10, 0, rect.Dx(), rect.Dy()))
		l.scroll.SetProperty(ScrollPage, l.geometry.Dy()/sparta.HeightUnit)
	case sparta.CommandEvent:
		if l.commFn != nil {
			if l.commFn(l, e) {
				return
			}
		}
		ev := e.(sparta.CommandEvent)
		if ev.Source == l.scroll {
			l.Update()
			return
		}
		l.parent.OnEvent(e)
	case sparta.ExposeEvent:
		if l.exposeFn != nil {
			l.exposeFn(l, e)
		}
		l.win.SetColor(sparta.Foreground, foreColor)
		if (l.list != nil) && (l.list.Len() > 0) {
			pos := l.scroll.Property(ScrollPos).(int)
			if pos < 0 {
				pos = 0
			}
			page := l.scroll.Property(ScrollPage).(int)
			for i := 0; i <= page; i++ {
				j := i + pos
				if j >= l.list.Len() {
					break
				}
				if l.list.IsSel(j) {
					y := (i * sparta.HeightUnit) + 2
					l.win.Text(image.Pt(2, y), ">")
				}
				l.win.Text(image.Pt(2+sparta.WidthUnit, (i*sparta.HeightUnit)+2), l.list.Item(j))
			}
		}
		rect := image.Rect(0, 0, l.geometry.Dx()-1, l.geometry.Dy()-1)
		l.win.Rectangle(rect, false)
	case sparta.KeyEvent:
		if s.keyFn != nil {
			if s.keyFn(s, e) {
				return
			}
		}
		pos := l.scroll.Property(ScrollPos).(int)
		page := l.scroll.Property(ScrollPage).(int)
		ev := e.(sparta.KeyEvent)
		switch ev.Key {
		case sparta.KeyDown:
			l.scroll.SetProperty(ScrollPos, pos+1)
		case sparta.KeyUp:
			l.scroll.SetProperty(ScrollPos, pos-1)
		case sparta.KeyPageUp:
			l.scroll.SetProperty(ScrollPos, pos-page)
		case sparta.KeyPageDown:
			l.scroll.SetProperty(ScrollPos, pos+page)
		case sparta.KeyHome:
			l.scroll.SetProperty(ScrollPos, 0)
		case sparta.KeyEnd:
			s.SetProperty(ScrollPos, l.list.Len())
		default:
			l.parent.OnEvent(e)
		}
	case sparta.MouseEvent:
		if l.mouseFn != nil {
			if l.mouseFn(l, e) {
				return
			}
		}
		pos := l.scroll.Property(ScrollPos).(int)
		ev := e.(sparta.MouseEvent)
		switch ev.Button {
		case -sparta.MouseWheel:
			l.scroll.SetProperty(ScrollPos, pos+1)
		case sparta.MouseWheel:
			l.scroll.SetProperty(ScrollPos, pos-1)
		case sparta.MouseLeft:
			p := ((ev.Loc.Y - 2) / sparta.HeightUnit) + pos
			sparta.SendEvent(l.target, sparta.CommandEvent{Source: l, Value: p})
		case sparta.MouseRight:
			p := ((ev.Loc.Y - 2) / sparta.HeightUnit) + pos
			sparta.SendEvent(l.target, sparta.CommandEvent{Source: l, Value: -(p + 1)})
		}
	}
}
Exemplo n.º 4
0
// SetProperty sets a property of the scroll.
func (s *Scroll) SetProperty(p sparta.Property, v interface{}) {
	switch p {
	case sparta.Data:
		s.data = v
	case sparta.Geometry:
		val := v.(image.Rectangle)
		if !s.geometry.Eq(val) {
			s.win.SetProperty(sparta.Geometry, val)
		}
	case sparta.Parent:
		if v == nil {
			s.parent = nil
		}
	case sparta.Name:
		val := v.(string)
		if s.name != val {
			s.name = val
		}
	case sparta.Foreground:
		val := v.(color.RGBA)
		if s.fore != val {
			s.fore = val
			s.win.SetProperty(sparta.Foreground, val)
		}
	case sparta.Background:
		val := v.(color.RGBA)
		if s.back != val {
			s.back = val
			s.win.SetProperty(sparta.Background, val)
		}
	case sparta.Target:
		val := v.(sparta.Widget)
		if val == nil {
			val = s.parent
		}
		if s.target == val {
			break
		}
		s.target = val
	case ScrollPage:
		val := v.(int)
		if val < 0 {
			break
		}
		if s.page == val {
			break
		}
		s.page = val
		if ((s.size - s.page) > 0) && (s.pos > (s.size - s.page)) {
			s.pos = s.size - s.page
			sparta.SendEvent(s.target, sparta.CommandEvent{Source: s, Value: s.pos})
		}
		s.Update()
	case ScrollPos:
		val := v.(int)
		if val < 0 {
			val = 0
		} else if val > (s.size - s.page) {
			val = s.size - s.page
		}
		if s.pos == val {
			break
		}
		s.pos = val
		sparta.SendEvent(s.target, sparta.CommandEvent{Source: s, Value: s.pos})
		s.Update()
	case ScrollSize:
		val := v.(int)
		if val < 0 {
			break
		}
		if s.size == val {
			break
		}
		s.size = val
		if s.size == 0 {
			s.page = 0
			s.pos = 0
		} else if (s.page < s.size) && (s.pos > (s.size - s.page)) {
			s.pos = s.size - s.page
			sparta.SendEvent(s.target, sparta.CommandEvent{Source: s, Value: s.pos})
		}
		s.Update()
	}
}