3a1234a0d4edd97b559d307017a58fc612da0d6e
[guacamole.git] / web / guacamole-default-webapp / src / main / webapp / guac-web-lib / javascript / mouse.js
1
2 /*
3  *  Guacamole - Clientless Remote Desktop
4  *  Copyright (C) 2010  Michael Jumper
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20
21 function GuacamoleMouse(element) {
22
23         /*****************************************/
24         /*** Mouse Handler                     ***/
25         /*****************************************/
26
27
28     var mouseIndex = 0;
29
30     var mouseLeftButton   = 0;
31     var mouseMiddleButton = 0;
32     var mouseRightButton  = 0;
33
34     var mouseX = 0;
35     var mouseY = 0;
36
37     var absoluteMouseX = 0;
38     var absoluteMouseY = 0;
39
40
41     function getMouseState(up, down) {
42         var mouseState = new MouseEvent(mouseX, mouseY,
43                 mouseLeftButton, mouseMiddleButton, mouseRightButton, up, down);
44
45         return mouseState;
46     }
47
48
49     // Block context menu so right-click gets sent properly
50     element.oncontextmenu = function(e) {return false;};
51
52     element.onmousemove = function(e) {
53
54         e.stopPropagation();
55
56         absoluteMouseX = e.pageX;
57         absoluteMouseY = e.pageY;
58
59         mouseX = absoluteMouseX - element.offsetLeft;
60         mouseY = absoluteMouseY - element.offsetTop;
61
62         // This is all JUST so we can get the mouse position within the element
63         var parent = element.offsetParent;
64         while (parent) {
65             if (parent.offsetLeft && parent.offsetTop) {
66                 mouseX -= parent.offsetLeft;
67                 mouseY -= parent.offsetTop;
68             }
69             parent = parent.offsetParent;
70         }
71
72         movementHandler(getMouseState(0, 0));
73     };
74
75
76     element.onmousedown = function(e) {
77
78         e.stopPropagation();
79
80         switch (e.button) {
81             case 0:
82                 mouseLeftButton = 1;
83                 break;
84             case 1:
85                 mouseMiddleButton = 1;
86                 break;
87             case 2:
88                 mouseRightButton = 1;
89                 break;
90         }
91
92         buttonPressedHandler(getMouseState(0, 0));
93     };
94
95
96     element.onmouseup = function(e) {
97
98         e.stopPropagation();
99
100         switch (e.button) {
101             case 0:
102                 mouseLeftButton = 0;
103                 break;
104             case 1:
105                 mouseMiddleButton = 0;
106                 break;
107             case 2:
108                 mouseRightButton = 0;
109                 break;
110         }
111
112         buttonReleasedHandler(getMouseState(0, 0));
113     };
114
115     // Override selection on mouse event element.
116     element.onselectstart = function() {
117         return false;
118     };
119
120     // Scroll wheel support
121     function handleScroll(e) {
122
123         var delta = 0;
124         if (e.detail)
125             delta = e.detail;
126         else if (e.wheelDelta)
127             delta = -event.wheelDelta;
128
129         // Up
130         if (delta < 0) {
131             buttonPressedHandler(getMouseState(1, 0));
132             buttonReleasedHandler(getMouseState(0, 0));
133         }
134
135         // Down
136         if (delta > 0) {
137             buttonPressedHandler(getMouseState(0, 1));
138             buttonReleasedHandler(getMouseState(0, 0));
139         }
140
141         if (e.preventDefault)
142             e.preventDefault();
143
144         e.returnValue = false;
145     }
146
147     element.addEventListener('DOMMouseScroll', handleScroll, false);
148
149     element.onmousewheel = function(e) {
150         handleScroll(e);
151     }
152
153     function MouseEvent(x, y, left, middle, right, up, down) {
154
155         this.getX = function() {
156             return x;
157         };
158
159         this.getY = function() {
160             return y;
161         };
162
163         this.getLeft = function() {
164             return left;
165         };
166
167         this.getMiddle = function() {
168             return middle;
169         };
170
171         this.getRight = function() {
172             return right;
173         };
174
175         this.getUp = function() {
176             return up;
177         };
178
179         this.getDown = function() {
180             return down;
181         };
182
183         this.toString = function() {
184             return (mouseIndex++) + "," + x + "," + y + "," + left + "," + middle + "," + right + "," + up + "," + down;
185         };
186
187     }
188
189
190         var buttonPressedHandler = null;
191         var buttonReleasedHandler = null;
192         var movementHandler = null;
193
194         this.setButtonPressedHandler  = function(mh) {buttonPressedHandler = mh;};
195         this.setButtonReleasedHandler = function(mh) {buttonReleasedHandler = mh;};
196         this.setMovementHandler = function(mh) {movementHandler = mh;};
197
198
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;};
204
205 }