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/>.
20 // Guacamole namespace
21 var Guacamole = Guacamole || {};
23 Guacamole.Mouse = function(element) {
25 var guac_mouse = this;
27 var mouseLeftButton = 0;
28 var mouseMiddleButton = 0;
29 var mouseRightButton = 0;
34 var absoluteMouseX = 0;
35 var absoluteMouseY = 0;
38 function getMouseState(up, down) {
39 var mouseState = new MouseEvent(mouseX, mouseY,
40 mouseLeftButton, mouseMiddleButton, mouseRightButton, up, down);
46 // Block context menu so right-click gets sent properly
47 element.oncontextmenu = function(e) {return false;};
49 element.onmousemove = function(e) {
53 absoluteMouseX = e.pageX;
54 absoluteMouseY = e.pageY;
56 mouseX = absoluteMouseX - element.offsetLeft;
57 mouseY = absoluteMouseY - element.offsetTop;
59 // This is all JUST so we can get the mouse position within the element
60 var parent = element.offsetParent;
62 if (parent.offsetLeft && parent.offsetTop) {
63 mouseX -= parent.offsetLeft;
64 mouseY -= parent.offsetTop;
66 parent = parent.offsetParent;
69 if (guac_mouse.onmousemove)
70 guac_mouse.onmousemove(getMouseState(0, 0));
74 element.onmousedown = function(e) {
83 mouseMiddleButton = 1;
90 if (guac_mouse.onmousedown)
91 guac_mouse.onmousedown(getMouseState(0, 0));
95 element.onmouseup = function(e) {
104 mouseMiddleButton = 0;
107 mouseRightButton = 0;
111 if (guac_mouse.onmouseup)
112 guac_mouse.onmouseup(getMouseState(0, 0));
115 element.onmouseout = function(e) {
119 // Release all buttons
120 if (mouseLeftButton || mouseMiddleButton || mouseRightButton) {
122 mouseMiddleButton = 0;
123 mouseRightButton = 0;
125 if (guac_mouse.onmouseup)
126 guac_mouse.onmouseup(getMouseState(0, 0));
131 // Override selection on mouse event element.
132 element.onselectstart = function() {
136 // Scroll wheel support
137 function handleScroll(e) {
142 else if (e.wheelDelta)
143 delta = -event.wheelDelta;
147 if (guac_mouse.onmousedown)
148 guac_mouse.onmousedown(getMouseState(1, 0));
150 if (guac_mouse.onmouseup)
151 guac_mouse.onmouseup(getMouseState(0, 0));
156 if (guac_mouse.onmousedown)
157 guac_mouse.onmousedown(getMouseState(0, 1));
159 if (guac_mouse.onmouseup)
160 guac_mouse.onmouseup(getMouseState(0, 0));
163 if (e.preventDefault)
166 e.returnValue = false;
169 element.addEventListener('DOMMouseScroll', handleScroll, false);
171 element.onmousewheel = function(e) {
175 guac_mouse.onmousedown = null;
176 guac_mouse.onmouseup = null;
177 guac_mouse.onmousemove = null;
179 guac_mouse.getX = function() {return mouseX;};
180 guac_mouse.getY = function() {return mouseY;};
181 guac_mouse.getLeftButton = function() {return mouseLeftButton;};
182 guac_mouse.getMiddleButton = function() {return mouseMiddleButton;};
183 guac_mouse.getRightButton = function() {return mouseRightButton;};
187 function MouseEvent(x, y, left, middle, right, up, down) {
189 this.getX = function() {
193 this.getY = function() {
197 this.getLeft = function() {
201 this.getMiddle = function() {
205 this.getRight = function() {
209 this.getUp = function() {
213 this.getDown = function() {