incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From purplecabb...@apache.org
Subject [4/4] wp7 commit: added mouse support
Date Sat, 21 Apr 2012 01:17:27 GMT
added mouse support


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/commit/9bbb5083
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/tree/9bbb5083
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/diff/9bbb5083

Branch: refs/heads/master
Commit: 9bbb5083363ed1fd63becca77b36589285506634
Parents: 3a5cba8
Author: Jesse MacFadyen <purplecabbage@gmail.com>
Authored: Fri Apr 20 14:46:08 2012 -0700
Committer: Jesse MacFadyen <purplecabbage@gmail.com>
Committed: Fri Apr 20 14:46:08 2012 -0700

----------------------------------------------------------------------
 framework/Cordova/BrowserMouseHelper.cs |  214 ++++++++++++++++++++++++++
 framework/CordovaView.xaml.cs           |    5 +
 framework/WP7CordovaClassLib.csproj     |    1 +
 3 files changed, 220 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/9bbb5083/framework/Cordova/BrowserMouseHelper.cs
----------------------------------------------------------------------
diff --git a/framework/Cordova/BrowserMouseHelper.cs b/framework/Cordova/BrowserMouseHelper.cs
new file mode 100644
index 0000000..3e02971
--- /dev/null
+++ b/framework/Cordova/BrowserMouseHelper.cs
@@ -0,0 +1,214 @@
+´╗┐using System.Linq;
+using System.Windows;
+using System.Windows.Controls;
+using Microsoft.Phone.Controls;
+using System.Windows.Input;
+using System.Diagnostics;
+using System.Windows.Media;
+using System;
+
+namespace WP7CordovaClassLib
+{
+
+    /// <summary>
+    /// Suppresses pinch zoom and optionally scrolling of the WebBrowser control
+    /// </summary>
+    public class BrowserMouseHelper
+    {
+
+        /**
+         * 
+         * Full Script below, in use it is minified.
+        */
+
+        private static string mouseScript =
+        @"(function(win,doc){
+            Object.defineProperty( MouseEvent.prototype, 'pageX', {
+               configurable: true,
+               get: function(){ return this.clientX }
+            });
+            Object.defineProperty( MouseEvent.prototype, 'pageY', {
+               configurable: true,
+               get: function(){ return this.clientY }
+            });
+
+            win.onNativeMouseEvent = function(type,x,y){
+                try {
+                    var xMod = screen.logicalXDPI / screen.deviceXDPI;
+                    var yMod = screen.logicalYDPI / screen.deviceYDPI;
+                    var evt = doc.createEvent('MouseEvents');
+                    var xPos =  doc.body.scrollLeft + Math.round(xMod * x);
+                    var yPos =  doc.body.scrollTop + Math.round(yMod * y);
+                    var element = doc.elementFromPoint(xPos,yPos);
+                    evt.initMouseEvent(type, true, true, win, 1, xPos, yPos, xPos, yPos,
false, false, false, false, 0, element);
+                    var canceled = element ? !element.dispatchEvent(evt) : !doc.dispatchEvent(evt);
+                    return canceled ? 'true' : 'false';
+                }
+                catch(e) { return e;}
+            }
+        })(window,document);";
+
+
+        private static string MinifiedMouseScript = "(function(f,a){Object.defineProperty(MouseEvent.prototype,'pageX',{configurable:!0,get:function(){return
this.clientX}});Object.defineProperty(MouseEvent.prototype,'pageY',{configurable:!0,get:function(){return
this.clientY}});f.onNativeMouseEvent=function(g,h,i)" 
+        + "{try{var j=screen.logicalXDPI/screen.deviceXDPI,k=screen.logicalYDPI/screen.deviceYDPI,b=a.createEvent('MouseEvents'),c=a.body.scrollLeft+Math.round(j*h),d=a.body.scrollTop+Math.round(k*i),e=a.elementFromPoint(c,d);b.initMouseEvent(g,!0,!0,"
+        + "f,1,c,d,c,d,!1,!1,!1,!1,0,e);return(e?!e.dispatchEvent(b):!a.dispatchEvent(b))?'true':'false'}catch(l){return
l}}})(window,document);";
+
+
+        private WebBrowser _browser;
+
+        /// <summary>
+        /// Gets or sets whether to suppress the scrolling of
+        /// the WebBrowser control;
+        /// </summary>
+        public bool ScrollDisabled { get; set; }
+        public bool ZoomDisabled { get; set; }
+        protected Border border;
+
+        public BrowserMouseHelper(WebBrowser browser)
+        {
+            _browser = browser;
+            browser.Loaded += new RoutedEventHandler(browser_Loaded);
+        }
+
+        private void browser_Loaded(object sender, RoutedEventArgs e)
+        {
+            var border0 = VisualTreeHelper.GetChild(_browser, 0);
+            var border1 = VisualTreeHelper.GetChild(border0, 0);
+            var panZoom = VisualTreeHelper.GetChild(border1, 0);
+            var grid = VisualTreeHelper.GetChild(panZoom, 0);
+            border = VisualTreeHelper.GetChild(grid, 0) as Border;
+
+
+            if (border != null)
+            {
+                border.ManipulationStarted += Border_ManipulationStarted;
+                border.ManipulationDelta += Border_ManipulationDelta;
+                border.ManipulationCompleted += Border_ManipulationCompleted;
+                border.DoubleTap += Border_DoubleTap;
+                border.Hold += Border_Hold;
+                border.MouseLeftButtonDown += Border_MouseLeftButtonDown;
+            }
+
+            _browser.LoadCompleted += Browser_LoadCompleted;
+
+        }
+
+        void Browser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs
e)
+        {
+            try
+            {
+                _browser.InvokeScript("execScript", MinifiedMouseScript);
+            }
+            catch (Exception)
+            {
+                Debug.WriteLine("BrowserHelper Failed to install mouse script in WebBrowser");
+            }
+
+        }
+
+        bool InvokeSimulatedMouseEvent(string eventName, Point pos)
+        {
+            bool bCancelled = false;
+            try
+            {
+                string strCancelled = _browser.InvokeScript("onNativeMouseEvent", new string[]
{ eventName, pos.X.ToString(), pos.Y.ToString() }) as string;                
+                if (bool.TryParse(strCancelled, out bCancelled))
+                {
+                    return bCancelled;
+                }
+
+            }
+            catch (Exception)
+            {
+                // script error
+            }
+
+            return bCancelled;
+        }
+
+        void Border_Hold(object sender, GestureEventArgs e)
+        {
+            e.Handled = true;
+        }
+
+        void Border_DoubleTap(object sender, GestureEventArgs e)
+        {
+            e.Handled = true;
+        }
+
+        void Border_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
+        {
+            if (ScrollDisabled)
+            {
+                e.Handled = true;
+                e.Complete();
+            }
+        }
+
+        void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
+        {
+            border.MouseMove += new MouseEventHandler(Border_MouseMove);
+            border.MouseLeftButtonUp += new MouseButtonEventHandler(Border_MouseLeftButtonUp);
+
+            Point pos = e.GetPosition(_browser);
+
+            bool bCancelled = InvokeSimulatedMouseEvent("mousedown", pos);
+            e.Handled = bCancelled;
+            ScrollDisabled = bCancelled;
+        }
+
+        void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
+        {
+            border.MouseMove -= new MouseEventHandler(Border_MouseMove);
+            border.MouseLeftButtonUp -= new MouseButtonEventHandler(Border_MouseLeftButtonUp);
+            Point pos = e.GetPosition(_browser);
+
+            bool bCancelled = InvokeSimulatedMouseEvent("mouseup", pos);
+            e.Handled = bCancelled;
+            ScrollDisabled = false;
+        }
+
+
+        void Border_MouseMove(object sender, MouseEventArgs e)
+        {
+            //Debug.WriteLine("Border_MouseMove");
+            Point pos = e.GetPosition(_browser);
+
+            bool bCancelled = InvokeSimulatedMouseEvent("mousemove", pos);
+            //ScrollDisabled = bCancelled;
+
+        }
+
+        private void Border_ManipulationCompleted(object sender, ManipulationCompletedEventArgs
e)
+        {
+            
+            // suppress zoom
+            if (e.FinalVelocities != null)
+            {
+                if (e.FinalVelocities.ExpansionVelocity.X != 0.0 ||
+                   e.FinalVelocities.ExpansionVelocity.Y != 0.0)
+                {
+                    e.Handled = true;
+                }
+            }
+        }
+
+        private void Border_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
+        {
+
+            // optionally suppress zoom
+            if (ZoomDisabled && (e.DeltaManipulation.Scale.X != 0.0 || e.DeltaManipulation.Scale.Y
!= 0.0) )
+            {
+                e.Handled = true;
+                e.Complete();
+            }
+            // optionally suppress scrolling
+            if (ScrollDisabled && (e.DeltaManipulation.Translation.X != 0.0 || e.DeltaManipulation.Translation.Y
!= 0.0) )
+            {
+                e.Handled = true;
+                e.Complete();
+            }
+        }
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/9bbb5083/framework/CordovaView.xaml.cs
----------------------------------------------------------------------
diff --git a/framework/CordovaView.xaml.cs b/framework/CordovaView.xaml.cs
index 334ed6c..d399a16 100644
--- a/framework/CordovaView.xaml.cs
+++ b/framework/CordovaView.xaml.cs
@@ -71,6 +71,8 @@ namespace WP7CordovaClassLib
         /// </summary>
         private NativeExecution nativeExecution;
 
+        protected BrowserMouseHelper bmHelper;
+
         protected DOMStorageHelper domStorageHelper;
         protected OrientationHelper orientationHelper;
 
@@ -145,6 +147,7 @@ namespace WP7CordovaClassLib
 
             // initializes native execution logic
             this.nativeExecution = new NativeExecution(ref this.CordovaBrowser);
+            this.bmHelper = new BrowserMouseHelper(this.CordovaBrowser);
         }
 
         
@@ -196,6 +199,8 @@ namespace WP7CordovaClassLib
             // prevents refreshing web control to initial state during pages transitions
             if (this.IsBrowserInitialized) return;
 
+            
+
             this.domStorageHelper = new DOMStorageHelper(this.CordovaBrowser);
 
             try

http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/9bbb5083/framework/WP7CordovaClassLib.csproj
----------------------------------------------------------------------
diff --git a/framework/WP7CordovaClassLib.csproj b/framework/WP7CordovaClassLib.csproj
index e5d0126..be20833 100644
--- a/framework/WP7CordovaClassLib.csproj
+++ b/framework/WP7CordovaClassLib.csproj
@@ -59,6 +59,7 @@
     <Reference Include="System.Xml.Linq" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Cordova\BrowserMouseHelper.cs" />
     <Compile Include="Cordova\Commands\Accelerometer.cs" />
     <Compile Include="Cordova\Commands\AudioPlayer.cs">
       <SubType>Code</SubType>


Mime
View raw message