3 * Guacamole - Clientless Remote Desktop
4 * Copyright (C) 2010 Michael Jumper
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 function GuacamoleMouse(element) {
23 var guac_mouse = this;
25 var mouseLeftButton = 0;
26 var mouseMiddleButton = 0;
27 var mouseRightButton = 0;
32 var absoluteMouseX = 0;
33 var absoluteMouseY = 0;
36 function getMouseState(up, down) {
37 var mouseState = new MouseEvent(mouseX, mouseY,
38 mouseLeftButton, mouseMiddleButton, mouseRightButton, up, down);
44 // Block context menu so right-click gets sent properly
45 element.oncontextmenu = function(e) {return false;};
47 element.onmousemove = function(e) {
51 absoluteMouseX = e.pageX;
52 absoluteMouseY = e.pageY;
54 mouseX = absoluteMouseX - element.offsetLeft;
55 mouseY = absoluteMouseY - element.offsetTop;
57 // This is all JUST so we can get the mouse position within the element
58 var parent = element.offsetParent;
60 if (parent.offsetLeft && parent.offsetTop) {
61 mouseX -= parent.offsetLeft;
62 mouseY -= parent.offsetTop;
64 parent = parent.offsetParent;
67 if (guac_mouse.onmousemove)
68 guac_mouse.onmousemove(getMouseState(0, 0));
72 element.onmousedown = function(e) {
81 mouseMiddleButton = 1;
88 if (guac_mouse.onmousedown)
89 guac_mouse.onmousedown(getMouseState(0, 0));
93 element.onmouseup = function(e) {
102 mouseMiddleButton = 0;
105 mouseRightButton = 0;
109 if (guac_mouse.onmouseup)
110 guac_mouse.onmouseup(getMouseState(0, 0));
113 element.onmouseout = function(e) {
117 // Release all buttons
118 if (mouseLeftButton || mouseMiddleButton || mouseRightButton) {
120 mouseMiddleButton = 0;
121 mouseRightButton = 0;
123 if (guac_mouse.onmouseup)
124 guac_mouse.onmouseup(getMouseState(0, 0));
129 // Override selection on mouse event element.
130 element.onselectstart = function() {
134 // Scroll wheel support
135 function handleScroll(e) {
140 else if (e.wheelDelta)
141 delta = -event.wheelDelta;
145 if (guac_mouse.onmousedown)
146 guac_mouse.onmousedown(getMouseState(1, 0));
148 if (guac_mouse.onmouseup)
149 guac_mouse.onmouseup(getMouseState(0, 0));
154 if (guac_mouse.onmousedown)
155 guac_mouse.onmousedown(getMouseState(0, 1));
157 if (guac_mouse.onmouseup)
158 guac_mouse.onmouseup(getMouseState(0, 0));
161 if (e.preventDefault)
164 e.returnValue = false;
167 element.addEventListener('DOMMouseScroll', handleScroll, false);
169 element.onmousewheel = function(e) {
173 guac_mouse.onmousedown = null;
174 guac_mouse.onmouseup = null;
175 guac_mouse.onmousemove = null;
177 guac_mouse.getX = function() {return mouseX;};
178 guac_mouse.getY = function() {return mouseY;};
179 guac_mouse.getLeftButton = function() {return mouseLeftButton;};
180 guac_mouse.getMiddleButton = function() {return mouseMiddleButton;};
181 guac_mouse.getRightButton = function() {return mouseRightButton;};
185 function MouseEvent(x, y, left, middle, right, up, down) {
187 this.getX = function() {
191 this.getY = function() {
195 this.getLeft = function() {
199 this.getMiddle = function() {
203 this.getRight = function() {
207 this.getUp = function() {
211 this.getDown = function() {