1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/modules/WindowDraggingUtils.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,110 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifdef XP_WIN 1.9 +#define USE_HITTEST 1.10 +#elifdef MOZ_WIDGET_COCOA 1.11 +#define USE_HITTEST 1.12 +#endif 1.13 + 1.14 +this.EXPORTED_SYMBOLS = [ "WindowDraggingElement" ]; 1.15 + 1.16 +this.WindowDraggingElement = function WindowDraggingElement(elem) { 1.17 + this._elem = elem; 1.18 + this._window = elem.ownerDocument.defaultView; 1.19 +#ifdef USE_HITTEST 1.20 + if (!this.isPanel()) 1.21 + this._elem.addEventListener("MozMouseHittest", this, false); 1.22 + else 1.23 +#endif 1.24 + this._elem.addEventListener("mousedown", this, false); 1.25 +}; 1.26 + 1.27 +WindowDraggingElement.prototype = { 1.28 + mouseDownCheck: function(e) { return true; }, 1.29 + dragTags: ["box", "hbox", "vbox", "spacer", "label", "statusbarpanel", "stack", 1.30 + "toolbaritem", "toolbarseparator", "toolbarspring", "toolbarspacer", 1.31 + "radiogroup", "deck", "scrollbox", "arrowscrollbox", "tabs"], 1.32 + shouldDrag: function(aEvent) { 1.33 + if (aEvent.button != 0 || 1.34 + this._window.fullScreen || 1.35 + !this.mouseDownCheck.call(this._elem, aEvent) || 1.36 + aEvent.defaultPrevented) 1.37 + return false; 1.38 + 1.39 + let target = aEvent.originalTarget, parent = aEvent.originalTarget; 1.40 + 1.41 + // The target may be inside an embedded iframe or browser. (bug 615152) 1.42 + if (target.ownerDocument.defaultView != this._window) 1.43 + return false; 1.44 + 1.45 + while (parent != this._elem) { 1.46 + let mousethrough = parent.getAttribute("mousethrough"); 1.47 + if (mousethrough == "always") 1.48 + target = parent.parentNode; 1.49 + else if (mousethrough == "never") 1.50 + break; 1.51 + parent = parent.parentNode; 1.52 + } 1.53 + while (target != this._elem) { 1.54 + if (this.dragTags.indexOf(target.localName) == -1) 1.55 + return false; 1.56 + target = target.parentNode; 1.57 + } 1.58 + return true; 1.59 + }, 1.60 + isPanel : function() { 1.61 + return this._elem instanceof Components.interfaces.nsIDOMXULElement && 1.62 + this._elem.localName == "panel"; 1.63 + }, 1.64 + handleEvent: function(aEvent) { 1.65 + let isPanel = this.isPanel(); 1.66 +#ifdef USE_HITTEST 1.67 + if (!isPanel) { 1.68 + if (this.shouldDrag(aEvent)) 1.69 + aEvent.preventDefault(); 1.70 + return; 1.71 + } 1.72 +#endif 1.73 + 1.74 + switch (aEvent.type) { 1.75 + case "mousedown": 1.76 + if (!this.shouldDrag(aEvent)) 1.77 + return; 1.78 + 1.79 +#ifdef MOZ_WIDGET_GTK 1.80 + // On GTK, there is a toolkit-level function which handles 1.81 + // window dragging, which must be used. 1.82 + this._window.beginWindowMove(aEvent, isPanel ? this._elem : null); 1.83 +#else 1.84 + if (isPanel) { 1.85 + let screenRect = this._elem.getOuterScreenRect(); 1.86 + this._deltaX = aEvent.screenX - screenRect.left; 1.87 + this._deltaY = aEvent.screenY - screenRect.top; 1.88 + } 1.89 + else { 1.90 + this._deltaX = aEvent.screenX - this._window.screenX; 1.91 + this._deltaY = aEvent.screenY - this._window.screenY; 1.92 + } 1.93 + this._draggingWindow = true; 1.94 + this._window.addEventListener("mousemove", this, false); 1.95 + this._window.addEventListener("mouseup", this, false); 1.96 +#endif 1.97 + break; 1.98 + case "mousemove": 1.99 + if (this._draggingWindow) { 1.100 + let toDrag = this.isPanel() ? this._elem : this._window; 1.101 + toDrag.moveTo(aEvent.screenX - this._deltaX, aEvent.screenY - this._deltaY); 1.102 + } 1.103 + break; 1.104 + case "mouseup": 1.105 + if (this._draggingWindow) { 1.106 + this._draggingWindow = false; 1.107 + this._window.removeEventListener("mousemove", this, false); 1.108 + this._window.removeEventListener("mouseup", this, false); 1.109 + } 1.110 + break; 1.111 + } 1.112 + } 1.113 +}