2020-08-26 06:39:20 +08:00
|
|
|
// Copyright 2020 The TCell Authors
|
2015-09-27 14:37:54 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the license at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package tcell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EventMouse is a mouse event. It is sent on either mouse up or mouse down
|
2015-10-03 12:49:13 +08:00
|
|
|
// events. It is also sent on mouse motion events - if the terminal supports
|
|
|
|
// it. We make every effort to ensure that mouse release events are delivered.
|
|
|
|
// Hence, click drag can be identified by a motion event with the mouse down,
|
2015-10-08 11:15:33 +08:00
|
|
|
// without any intervening button release. On some terminals only the initiating
|
|
|
|
// press and terminating release event will be delivered.
|
2015-10-03 12:49:13 +08:00
|
|
|
//
|
|
|
|
// Mouse wheel events, when reported, may appear on their own as individual
|
2015-10-06 06:46:51 +08:00
|
|
|
// impulses; that is, there will normally not be a release event delivered
|
|
|
|
// for mouse wheel movements.
|
2015-10-03 12:49:13 +08:00
|
|
|
//
|
2015-09-27 14:37:54 +08:00
|
|
|
// Most terminals cannot report the state of more than one button at a time --
|
2015-10-08 11:15:33 +08:00
|
|
|
// and some cannot report motion events unless a button is pressed.
|
2015-10-03 12:49:13 +08:00
|
|
|
//
|
2015-10-08 11:15:33 +08:00
|
|
|
// Applications can inspect the time between events to resolve double or
|
|
|
|
// triple clicks.
|
2015-09-27 14:37:54 +08:00
|
|
|
type EventMouse struct {
|
2015-09-29 13:28:41 +08:00
|
|
|
t time.Time
|
|
|
|
btn ButtonMask
|
|
|
|
mod ModMask
|
|
|
|
x int
|
|
|
|
y int
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
2015-10-08 11:15:33 +08:00
|
|
|
// When returns the time when this EventMouse was created.
|
2015-09-27 14:37:54 +08:00
|
|
|
func (ev *EventMouse) When() time.Time {
|
|
|
|
return ev.t
|
|
|
|
}
|
|
|
|
|
2015-10-08 11:15:33 +08:00
|
|
|
// Buttons returns the list of buttons that were pressed or wheel motions.
|
2015-09-27 14:37:54 +08:00
|
|
|
func (ev *EventMouse) Buttons() ButtonMask {
|
|
|
|
return ev.btn
|
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Modifiers returns a list of keyboard modifiers that were pressed
|
|
|
|
// with the mouse button(s).
|
2015-09-27 14:37:54 +08:00
|
|
|
func (ev *EventMouse) Modifiers() ModMask {
|
|
|
|
return ev.mod
|
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Position returns the mouse position in character cells. The origin
|
|
|
|
// 0, 0 is at the upper left corner.
|
2015-09-27 14:37:54 +08:00
|
|
|
func (ev *EventMouse) Position() (int, int) {
|
|
|
|
return ev.x, ev.y
|
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// NewEventMouse is used to create a new mouse event. Applications
|
|
|
|
// shouldn't need to use this; its mostly for screen implementors.
|
2015-09-27 14:37:54 +08:00
|
|
|
func NewEventMouse(x, y int, btn ButtonMask, mod ModMask) *EventMouse {
|
|
|
|
return &EventMouse{t: time.Now(), x: x, y: y, btn: btn, mod: mod}
|
|
|
|
}
|
|
|
|
|
2015-10-08 11:15:33 +08:00
|
|
|
// ButtonMask is a mask of mouse buttons and wheel events. Mouse button presses
|
|
|
|
// are normally delivered as both press and release events. Mouse wheel events
|
|
|
|
// are normally just single impulse events. Windows supports up to eight
|
|
|
|
// separate buttons plus all four wheel directions, but XTerm can only support
|
|
|
|
// mouse buttons 1-3 and wheel up/down. Its not unheard of for terminals
|
|
|
|
// to support only one or two buttons (think Macs). Old terminals, and true
|
|
|
|
// emulations (such as vt100) won't support mice at all, of course.
|
2015-09-27 14:37:54 +08:00
|
|
|
type ButtonMask int16
|
|
|
|
|
2020-08-26 06:39:20 +08:00
|
|
|
// These are the actual button values. Note that tcell version 1.x reversed buttons
|
|
|
|
// two and three on *nix based terminals. We use button 1 as the primary, and
|
|
|
|
// button 2 as the secondary, and button 3 (which is often missing) as the middle.
|
2015-09-27 14:37:54 +08:00
|
|
|
const (
|
2020-08-26 06:39:20 +08:00
|
|
|
Button1 ButtonMask = 1 << iota // Usually the left (primary) mouse button.
|
|
|
|
Button2 // Usually the right (secondary) mouse button.
|
|
|
|
Button3 // Usually the middle mouse button.
|
2015-10-08 11:15:33 +08:00
|
|
|
Button4 // Often a side button (thumb/next).
|
|
|
|
Button5 // Often a side button (thumb/prev).
|
2015-09-27 14:37:54 +08:00
|
|
|
Button6
|
|
|
|
Button7
|
|
|
|
Button8
|
2015-10-08 11:15:33 +08:00
|
|
|
WheelUp // Wheel motion up/away from user.
|
|
|
|
WheelDown // Wheel motion down/towards user.
|
|
|
|
WheelLeft // Wheel motion to left.
|
|
|
|
WheelRight // Wheel motion to right.
|
|
|
|
ButtonNone ButtonMask = 0 // No button or wheel events.
|
2020-08-26 06:39:20 +08:00
|
|
|
|
|
|
|
ButtonPrimary = Button1
|
|
|
|
ButtonSecondary = Button2
|
|
|
|
ButtonMiddle = Button3
|
2015-09-27 14:37:54 +08:00
|
|
|
)
|