diff --git a/event.go b/event.go index 3476f41..38cd54c 100644 --- a/event.go +++ b/event.go @@ -306,6 +306,7 @@ func (window *window) handleMotionNotify ( if window.hasModal { return } motionEvent := window.compressMotionNotify(*event.MotionNotifyEvent) + window.updateMousePosition(motionEvent.EventX, motionEvent.EventY) x := int(motionEvent.EventX) y := int(motionEvent.EventY) diff --git a/textbox.go b/textbox.go index 003ab6d..6a3a2b8 100644 --- a/textbox.go +++ b/textbox.go @@ -3,10 +3,11 @@ package x import "image" import "image/color" import "golang.org/x/image/font" -import "golang.org/x/image/math/fixed" import "git.tebibyte.media/tomo/tomo" +import "golang.org/x/image/math/fixed" import "git.tebibyte.media/tomo/typeset" import "git.tebibyte.media/tomo/tomo/text" +import "git.tebibyte.media/tomo/tomo/input" import "git.tebibyte.media/tomo/tomo/event" import "git.tebibyte.media/tomo/tomo/canvas" @@ -23,9 +24,11 @@ type textBox struct { wrap bool hAlign tomo.Align - selectable bool - dot text.Dot - dotColor color.Color + selectable bool + selecting bool + selectStart int + dot text.Dot + dotColor color.Color drawer typeset.Drawer @@ -228,6 +231,38 @@ func (this *textBox) handleFocusLeave () { this.box.handleFocusLeave() } +func (this *textBox) handleMouseDown (button input.Button) { + if button == input.ButtonLeft { + index := this.runeUnderMouse() + this.selectStart = index + this.selecting = true + this.Select(text.Dot { Start: this.selectStart, End: index }) + } + this.box.handleMouseDown(button) +} + +func (this *textBox) handleMouseUp (button input.Button) { + if button == input.ButtonLeft && this.selecting { + index := this.runeUnderMouse() + this.selecting = false + this.Select(text.Dot { Start: this.selectStart, End: index }) + } + this.box.handleMouseUp(button) +} + +func (this *textBox) handleMouseMove () { + if this.selecting { + index := this.runeUnderMouse() + this.Select(text.Dot { Start: this.selectStart, End: index }) + } + this.box.handleMouseMove() +} + +func (this *textBox) runeUnderMouse () int { + position := this.MousePosition().Sub(this.textOffset()) + return this.drawer.AtPosition(fixPt(position)) +} + func (this *textBox) normalizedLayoutBoundsSpace () image.Rectangle { bounds := this.drawer.LayoutBoundsSpace() return bounds.Sub(bounds.Min) diff --git a/window.go b/window.go index 7a337ef..bfc67bf 100644 --- a/window.go +++ b/window.go @@ -117,8 +117,8 @@ func (backend *Backend) newWindow ( Connect(backend.x, window.xWindow.Id) xevent.ButtonReleaseFun(window.handleButtonRelease). Connect(backend.x, window.xWindow.Id) - // xevent.MotionNotifyFun(window.handleMotionNotify). - // Connect(backend.x, window.xWindow.Id) + xevent.MotionNotifyFun(window.handleMotionNotify). + Connect(backend.x, window.xWindow.Id) // xevent.SelectionNotifyFun(window.handleSelectionNotify). // Connect(backend.x, window.xWindow.Id) // xevent.PropertyNotifyFun(window.handlePropertyNotify).