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
// OnEvent process a particular event on the main window.
func (w *MainWindow) OnEvent(e interface{}) {
	switch e.(type) {
	case sparta.CloseEvent:
		if sparta.IsBlock() {
			if !sparta.IsBlocker(w) {
				return
			}
		}
		if w.closeFn != nil {
			if w.closeFn(w, e) {
				return
			}
		}
		for _, c := range w.childs {
			c.OnEvent(e)
		}
		w.win.Close()
	case sparta.CommandEvent:
		if w.commFn != nil {
			w.commFn(w, e)
		}
	case sparta.ConfigureEvent:
		w.geometry = e.(sparta.ConfigureEvent).Rect
		if w.configFn != nil {
			w.configFn(w, e)
		}
	case sparta.ExposeEvent:
		if w.exposeFn != nil {
			w.exposeFn(w, e)
		}
	case sparta.KeyEvent:
		if w.keyFn != nil {
			w.keyFn(w, e)
		}
	case sparta.MouseEvent:
		if w.mouseFn != nil {
			w.mouseFn(w, e)
		}
	}
}