More migration to traditional JS events.
[guacamole-common-js.git] / src / main / resources / 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     var guac_mouse = this;
24
25     var mouseLeftButton   = 0;
26     var mouseMiddleButton = 0;
27     var mouseRightButton  = 0;
28
29     var mouseX = 0;
30     var mouseY = 0;
31
32     var absoluteMouseX = 0;
33     var absoluteMouseY = 0;
34
35
36     function getMouseState(up, down) {
37         var mouseState = new MouseEvent(mouseX, mouseY,
38                 mouseLeftButton, mouseMiddleButton, mouseRightButton, up, down);
39
40         return mouseState;
41     }
42
43
44     // Block context menu so right-click gets sent properly
45     element.oncontextmenu = function(e) {return false;};
46
47     element.onmousemove = function(e) {
48
49         e.stopPropagation();
50
51         absoluteMouseX = e.pageX;
52         absoluteMouseY = e.pageY;
53
54         mouseX = absoluteMouseX - element.offsetLeft;
55         mouseY = absoluteMouseY - element.offsetTop;
56
57         // This is all JUST so we can get the mouse position within the element
58         var parent = element.offsetParent;
59         while (parent) {
60             if (parent.offsetLeft && parent.offsetTop) {
61                 mouseX -= parent.offsetLeft;
62                 mouseY -= parent.offsetTop;
63             }
64             parent = parent.offsetParent;
65         }
66
67         if (guac_mouse.onmousemove)
68             guac_mouse.onmousemove(getMouseState(0, 0));
69     };
70
71
72     element.onmousedown = function(e) {
73
74         e.stopPropagation();
75
76         switch (e.button) {
77             case 0:
78                 mouseLeftButton = 1;
79                 break;
80             case 1:
81                 mouseMiddleButton = 1;
82                 break;
83             case 2:
84                 mouseRightButton = 1;
85                 break;
86         }
87
88         if (guac_mouse.onmousedown)
89             guac_mouse.onmousedown(getMouseState(0, 0));
90     };
91
92
93     element.onmouseup = function(e) {
94
95         e.stopPropagation();
96
97         switch (e.button) {
98             case 0:
99                 mouseLeftButton = 0;
100                 break;
101             case 1:
102                 mouseMiddleButton = 0;
103                 break;
104             case 2:
105                 mouseRightButton = 0;
106                 break;
107         }
108
109         if (guac_mouse.onmouseup)
110             guac_mouse.onmouseup(getMouseState(0, 0));
111     };
112
113     element.onmouseout = function(e) {
114
115         e.stopPropagation();
116
117         // Release all buttons
118         if (mouseLeftButton || mouseMiddleButton || mouseRightButton) {
119             mouseLeftButton = 0;
120             mouseMiddleButton = 0;
121             mouseRightButton = 0;
122
123             if (guac_mouse.onmouseup)
124                 guac_mouse.onmouseup(getMouseState(0, 0));
125         }
126
127     };
128
129     // Override selection on mouse event element.
130     element.onselectstart = function() {
131         return false;
132     };
133
134     // Scroll wheel support
135     function handleScroll(e) {
136
137         var delta = 0;
138         if (e.detail)
139             delta = e.detail;
140         else if (e.wheelDelta)
141             delta = -event.wheelDelta;
142
143         // Up
144         if (delta < 0) {
145             if (guac_mouse.onmousedown)
146                 guac_mouse.onmousedown(getMouseState(1, 0));
147
148             if (guac_mouse.onmouseup)
149                 guac_mouse.onmouseup(getMouseState(0, 0));
150         }
151
152         // Down
153         if (delta > 0) {
154             if (guac_mouse.onmousedown)
155                 guac_mouse.onmousedown(getMouseState(0, 1));
156             
157             if (guac_mouse.onmouseup)
158                 guac_mouse.onmouseup(getMouseState(0, 0));
159         }
160
161         if (e.preventDefault)
162             e.preventDefault();
163
164         e.returnValue = false;
165     }
166
167     element.addEventListener('DOMMouseScroll', handleScroll, false);
168
169     element.onmousewheel = function(e) {
170         handleScroll(e);
171     }
172
173         guac_mouse.onmousedown = null;
174         guac_mouse.onmouseup = null;
175         guac_mouse.onmousemove = null;
176
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;};
182
183 }
184
185 function MouseEvent(x, y, left, middle, right, up, down) {
186
187     this.getX = function() {
188         return x;
189     };
190
191     this.getY = function() {
192         return y;
193     };
194
195     this.getLeft = function() {
196         return left;
197     };
198
199     this.getMiddle = function() {
200         return middle;
201     };
202
203     this.getRight = function() {
204         return right;
205     };
206
207     this.getUp = function() {
208         return up;
209     };
210
211     this.getDown = function() {
212         return down;
213     };
214
215 }