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 /*****************************************/
24 /*** Mouse Handler ***/
25 /*****************************************/
30 var mouseLeftButton = 0;
31 var mouseMiddleButton = 0;
32 var mouseRightButton = 0;
37 var absoluteMouseX = 0;
38 var absoluteMouseY = 0;
41 function getMouseState(up, down) {
42 var mouseState = new MouseEvent(mouseX, mouseY,
43 mouseLeftButton, mouseMiddleButton, mouseRightButton, up, down);
49 // Block context menu so right-click gets sent properly
50 element.oncontextmenu = function(e) {return false;};
52 element.onmousemove = function(e) {
56 absoluteMouseX = e.pageX;
57 absoluteMouseY = e.pageY;
59 mouseX = absoluteMouseX - element.offsetLeft;
60 mouseY = absoluteMouseY - element.offsetTop;
62 // This is all JUST so we can get the mouse position within the element
63 var parent = element.offsetParent;
65 if (parent.offsetLeft && parent.offsetTop) {
66 mouseX -= parent.offsetLeft;
67 mouseY -= parent.offsetTop;
69 parent = parent.offsetParent;
72 movementHandler(getMouseState(0, 0));
76 element.onmousedown = function(e) {
85 mouseMiddleButton = 1;
92 buttonPressedHandler(getMouseState(0, 0));
96 element.onmouseup = function(e) {
105 mouseMiddleButton = 0;
108 mouseRightButton = 0;
112 buttonReleasedHandler(getMouseState(0, 0));
115 // Override selection on mouse event element.
116 element.onselectstart = function() {
120 // Scroll wheel support
121 function handleScroll(e) {
126 else if (e.wheelDelta)
127 delta = -event.wheelDelta;
131 buttonPressedHandler(getMouseState(1, 0));
132 buttonReleasedHandler(getMouseState(0, 0));
137 buttonPressedHandler(getMouseState(0, 1));
138 buttonReleasedHandler(getMouseState(0, 0));
141 if (e.preventDefault)
144 e.returnValue = false;
147 element.addEventListener('DOMMouseScroll', handleScroll, false);
149 element.onmousewheel = function(e) {
153 function MouseEvent(x, y, left, middle, right, up, down) {
155 this.getX = function() {
159 this.getY = function() {
163 this.getLeft = function() {
167 this.getMiddle = function() {
171 this.getRight = function() {
175 this.getUp = function() {
179 this.getDown = function() {
183 this.toString = function() {
184 return (mouseIndex++) + "," + x + "," + y + "," + left + "," + middle + "," + right + "," + up + "," + down;
190 var buttonPressedHandler = null;
191 var buttonReleasedHandler = null;
192 var movementHandler = null;
194 this.setButtonPressedHandler = function(mh) {buttonPressedHandler = mh;};
195 this.setButtonReleasedHandler = function(mh) {buttonReleasedHandler = mh;};
196 this.setMovementHandler = function(mh) {movementHandler = mh;};
199 this.getX = function() {return mouseX;};
200 this.getY = function() {return mouseY;};
201 this.getLeftButton = function() {return mouseLeftButton;};
202 this.getMiddleButton = function() {return mouseMiddleButton;};
203 this.getRightButton = function() {return mouseRightButton;};