Merge pull request #341 from sevoku/extend-webview

extend WebView functionality (incl. all backends)
This commit is contained in:
Lluis Sanchez 2014-07-13 18:41:10 +02:00
Родитель 1472bacb89 858c7109e0
Коммит 47d514e14c
13 изменённых файлов: 1344 добавлений и 32 удалений

Просмотреть файл

@ -3,6 +3,7 @@
//
// Author:
// Cody Russell <cody@xamarin.com>
// Vsevolod Kukol <sevo@sevo.org>
//
// Copyright (c) 2014 Xamarin Inc.
//
@ -27,17 +28,114 @@
using System;
using Xwt;
using Xwt.Drawing;
using System.Net;
using System.Timers;
namespace Samples
{
public class WebViewSample : VBox
{
readonly WebView wb = new WebView ();
readonly ProgressBar pg = new ProgressBar ();
public WebViewSample ()
{
var wb = new WebView ("http://www.xamarin.com");
wb.WidthRequest = 600;
wb.HeightRequest = 900;
PackStart (wb, true);
var toolbar = new HBox ();
var back = new Button ("<");
var forward = new Button (">");
var lbl = new Label ("Address:");
var go = new Button ("Go!");
var stop = new Button ("Stop!");
stop.Sensitive = false;
back.Sensitive = false;
forward.Sensitive = false;
var uri = new TextEntry ();
uri.Text = wb.Url;
toolbar.PackStart (back);
toolbar.PackStart (forward);
toolbar.PackStart (lbl);
toolbar.PackStart (uri, true);
toolbar.PackEnd (go);
toolbar.PackEnd (stop);
PackStart (toolbar);
var title = new Label ("Title: ");
PackStart (title);
var wbscroll = new ScrollView (wb);
wbscroll.VerticalScrollPolicy = ScrollPolicy.Automatic;
wbscroll.HorizontalScrollPolicy = ScrollPolicy.Automatic;
PackStart (wbscroll, true);
pg.Fraction = 0.0;
var timer = new Timer (100);
timer.Elapsed += UpdateProgress;
PackStart (pg);
var loadhtml = new Button ("Load sample Html");
PackStart (loadhtml);
wb.TitleChanged += (object sender, EventArgs e) => title.Text = "Title: " + wb.Title;
wb.Loading += delegate(object sender, EventArgs e) {
uri.Text = wb.Url;
pg.Fraction = 0.0;
stop.Sensitive = true;
timer.Start ();
back.Sensitive = wb.CanGoBack;
forward.Sensitive = wb.CanGoForward;
};
wb.Loaded += delegate(object sender, EventArgs e) {
uri.Text = wb.Url;
stop.Sensitive = false;
timer.Stop ();
pg.Fraction = 1.0;
back.Sensitive = wb.CanGoBack;
forward.Sensitive = wb.CanGoForward;
};
wb.NavigateToUrl += delegate(object sender, NavigateToUrlEventArgs e) {
if (e.Uri.Host.EndsWith("facebook.com")) {
e.SetHandled ();
MessageDialog.ShowMessage ("Loading *.facebook.com overriden");
}
else {
}
};
uri.Activated += (sender, e) => wb.Url = uri.Text;
go.Clicked += (sender, e) => wb.Url = uri.Text;
stop.Clicked += (sender, e) => wb.StopLoading ();
back.Clicked += (sender, e) => wb.GoBack ();
forward.Clicked += (sender, e) => wb.GoForward ();
loadhtml.Clicked += LoadHtmlString;
wb.Url = "http://www.xamarin.com";
}
void LoadHtmlString (object sender, EventArgs e)
{
string html =
"<html><head>" +
"<title>Xwt.WebView HTML Test</title>" +
"<style type=\"text/css\">" +
"<!-- h1 {text-align:center;font-family:Arial, Helvetica, Sans-Serif;}-->" +
"</style></head><body>" +
"<h1>Hello, World!</h1>" +
"<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor" +
"invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam" +
"et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est" +
"Lorem ipsum dolor sit amet.</p>" +
"<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor" +
"invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam" +
"et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est" +
"Lorem ipsum dolor sit amet.</p>" +
"</body></html>";
wb.LoadHtml (html, "sample.html");
}
public void UpdateProgress (object sender, ElapsedEventArgs args)
{
Application.Invoke ( () => pg.Fraction = wb.LoadProgress );
}
}
}

Просмотреть файл

@ -3,6 +3,7 @@
//
// Author:
// Lluis Sanchez Gual <lluis@xamarin.com>
// Vsevolod Kukol <sevo@sevo.org>
//
// Copyright (c) 2014 Xamarin, Inc (http://www.xamarin.com)
//
@ -26,6 +27,7 @@
using System;
using Xwt.GtkBackend;
using Xwt.Backends;
using MonoMac.Foundation;
namespace Xwt.Gtk.Mac
{
@ -53,6 +55,122 @@ namespace Xwt.Gtk.Mac
view.MainFrameUrl = value;
}
}
public string Title {
get {
return view.MainFrameTitle;
}
}
public double LoadProgress {
get {
return view.EstimatedProgress;
}
}
public bool CanGoBack {
get {
return view.CanGoBack ();
}
}
public bool CanGoForward {
get {
return view.CanGoForward ();
}
}
public void GoBack ()
{
view.GoBack ();
}
public void GoForward ()
{
view.GoForward ();
}
public void Reload ()
{
view.MainFrame.Reload ();
}
public void StopLoading ()
{
view.MainFrame.StopLoading ();
}
public void LoadHtml (string content, string base_uri)
{
view.MainFrame.LoadHtmlString (content, new NSUrl(base_uri));
}
protected new IWebViewEventSink EventSink {
get { return (IWebViewEventSink)base.EventSink; }
}
public override void EnableEvent (object eventId)
{
base.EnableEvent (eventId);
if (eventId is WebViewEvent) {
switch ((WebViewEvent)eventId) {
case WebViewEvent.NavigateToUrl: view.StartedProvisionalLoad += HandleStartedProvisionalLoad; break;
case WebViewEvent.Loading: view.CommitedLoad += HandleLoadStarted; break;
case WebViewEvent.Loaded: view.FinishedLoad += HandleLoadFinished; break;
case WebViewEvent.TitleChanged: view.ReceivedTitle += HandleTitleChanged; break;
}
}
}
public override void DisableEvent (object eventId)
{
base.DisableEvent (eventId);
if (eventId is WebViewEvent) {
switch ((WebViewEvent)eventId) {
case WebViewEvent.NavigateToUrl: view.StartedProvisionalLoad -= HandleStartedProvisionalLoad; break;
case WebViewEvent.Loading: view.CommitedLoad += HandleLoadStarted; break;
case WebViewEvent.Loaded: view.FinishedLoad -= HandleLoadFinished; break;
case WebViewEvent.TitleChanged: view.ReceivedTitle -= HandleTitleChanged; break;
}
}
}
void HandleStartedProvisionalLoad (object sender, MonoMac.WebKit.WebFrameEventArgs e)
{
var url = String.Empty;
if (e.ForFrame.ProvisionalDataSource.Request.MainDocumentURL != null)
url = e.ForFrame.ProvisionalDataSource.Request.MainDocumentURL.ToString ();
if (String.IsNullOrEmpty (url))
return;
bool cancel = false;
ApplicationContext.InvokeUserCode (delegate {
cancel = EventSink.OnNavigateToUrl(url);
});
if (cancel)
e.ForFrame.StopLoading ();
}
void HandleLoadStarted (object o, EventArgs args)
{
ApplicationContext.InvokeUserCode (delegate {
EventSink.OnLoading ();
});
}
void HandleLoadFinished (object o, EventArgs args)
{
ApplicationContext.InvokeUserCode (delegate {
EventSink.OnLoaded ();
});
}
void HandleTitleChanged (object sender, MonoMac.WebKit.WebFrameTitleEventArgs e)
{
ApplicationContext.InvokeUserCode (delegate {
EventSink.OnTitleChanged ();
});
}
#endregion
}
}

Просмотреть файл

@ -3,6 +3,7 @@
//
// Author:
// Cody Russell <cody@xamarin.com>
// Vsevolod Kukol <sevo@sevo.org>
//
// Copyright (c) 2014 Xamarin Inc.
//
@ -30,7 +31,7 @@ using System.Runtime.InteropServices;
using SWF = System.Windows.Forms;
using Xwt.GtkBackend;
using Xwt.Backends;
using GTK = Gtk;
using Gtk;
namespace Xwt.Gtk.Windows
{
@ -38,7 +39,8 @@ namespace Xwt.Gtk.Windows
{
SWF.WebBrowser view;
string url;
GTK.Socket socket;
Socket socket;
bool enableNavigatingEvent, enableLoadingEvent, enableLoadedEvent, enableTitleChangedEvent;
[DllImportAttribute("user32.dll", EntryPoint = "SetParent")]
internal static extern System.IntPtr SetParent([InAttribute] System.IntPtr hwndChild, [InAttribute] System.IntPtr hwndNewParent);
@ -47,34 +49,195 @@ namespace Xwt.Gtk.Windows
{
base.Initialize ();
socket = new GTK.Socket ();
socket = new Socket ();
Widget = socket;
this.Widget.Realized += delegate
{
var size = new System.Drawing.Size (Widget.WidthRequest, Widget.HeightRequest);
this.Widget.Realized += HandleGtkRealized;
this.Widget.SizeAllocated += HandleGtkSizeAllocated;
view = new SWF.WebBrowser ();
view.Size = size;
var browser_handle = view.Handle;
IntPtr window_handle = (IntPtr)socket.Id;
SetParent (browser_handle, window_handle);
if (url != null)
view.Navigate (url);
//return false;
};
Widget.Show();
}
}
void HandleGtkRealized (object sender, EventArgs e)
{
var size = new System.Drawing.Size (Widget.WidthRequest, Widget.HeightRequest);
view = new SWF.WebBrowser ();
view.ScriptErrorsSuppressed = true;
view.AllowWebBrowserDrop = false;
view.Size = size;
var browser_handle = view.Handle;
IntPtr window_handle = (IntPtr)socket.Id;
SetParent (browser_handle, window_handle);
view.ProgressChanged += HandleProgressChanged;
view.Navigating += HandleNavigating;
view.Navigated += HandleNavigated;
view.DocumentTitleChanged += HandleDocumentTitleChanged;
if (url != null)
view.Navigate (url);
}
void HandleGtkSizeAllocated (object sender, SizeAllocatedArgs e)
{
var size = new System.Drawing.Size(e.Allocation.Width, e.Allocation.Height);
view.Size = size;
}
public string Url {
get { return url; }
get {
if (view != null && !String.IsNullOrEmpty(view.Url.AbsoluteUri))
url = view.Url.AbsoluteUri;
return url;
}
set {
url = value;
if (view != null)
view.Navigate (url);
}
}
double loadProgress;
public double LoadProgress {
get {
return loadProgress;
}
}
public bool CanGoBack {
get {
return view.CanGoBack;
}
}
public bool CanGoForward {
get {
return view.CanGoForward;
}
}
public void GoBack ()
{
view.GoBack ();
}
public void GoForward ()
{
view.GoForward ();
}
public void Reload ()
{
view.Refresh ();
}
public void StopLoading ()
{
view.Stop ();
}
public void LoadHtml (string content, string base_uri)
{
view.DocumentText = content;
}
public string Title {
get {
return view.Document.Title;
}
}
protected new IWebViewEventSink EventSink {
get { return (IWebViewEventSink)base.EventSink; }
}
public override void EnableEvent (object eventId)
{
base.EnableEvent (eventId);
if (eventId is WebViewEvent) {
switch ((WebViewEvent)eventId) {
case WebViewEvent.NavigateToUrl:
enableNavigatingEvent = true;
break;
case WebViewEvent.Loading:
enableLoadingEvent = true;
break;
case WebViewEvent.Loaded:
enableLoadedEvent = true;
break;
case WebViewEvent.TitleChanged:
enableTitleChangedEvent = true;
break;
}
}
}
public override void DisableEvent (object eventId)
{
base.DisableEvent (eventId);
if (eventId is WebViewEvent) {
switch ((WebViewEvent)eventId) {
case WebViewEvent.NavigateToUrl:
enableNavigatingEvent = false;
break;
case WebViewEvent.Loading:
enableLoadingEvent = false;
break;
case WebViewEvent.Loaded:
enableLoadedEvent = false;
break;
case WebViewEvent.TitleChanged:
enableTitleChangedEvent = false;
break;
}
}
}
void HandleProgressChanged (object sender, SWF.WebBrowserProgressChangedEventArgs e)
{
if (e.CurrentProgress == -1) {
loadProgress = 1;
HandleLoaded(view, EventArgs.Empty);
}
else if (e.MaximumProgress == 0)
loadProgress = 1;
else
loadProgress = (double)e.CurrentProgress / (double)e.MaximumProgress;
}
void HandleNavigating (object sender, SWF.WebBrowserNavigatingEventArgs e)
{
if (enableNavigatingEvent) {
var url = e.Url.AbsoluteUri;
ApplicationContext.InvokeUserCode (delegate {
e.Cancel = EventSink.OnNavigateToUrl (url);
});
}
}
void HandleDocumentTitleChanged (object sender, EventArgs e)
{
if (enableTitleChangedEvent)
ApplicationContext.InvokeUserCode (delegate {
EventSink.OnTitleChanged ();
});
}
void HandleNavigated (object sender, SWF.WebBrowserNavigatedEventArgs e)
{
if (enableLoadingEvent)
ApplicationContext.InvokeUserCode (delegate {
EventSink.OnLoading ();
});
}
void HandleLoaded (object sender, EventArgs e)
{
if (enableLoadedEvent)
ApplicationContext.InvokeUserCode (delegate {
EventSink.OnLoaded ();
});
}
}
}

Просмотреть файл

@ -132,6 +132,8 @@
</Compile>
<Compile Include="Xwt.GtkBackend.CellViews\CellViewBackend.cs" />
<Compile Include="Xwt.GtkBackend\SearchTextEntryBackend.cs" />
<Compile Include="Xwt.GtkBackend\WebViewBackend.cs" />
<Compile Include="Xwt.GtkBackend\GtkWebKitMini.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>

Просмотреть файл

@ -108,6 +108,7 @@ namespace Xwt.GtkBackend
RegisterBackend<IPasswordEntryBackend, PasswordEntryBackend> ();
RegisterBackend<KeyboardHandler, GtkKeyboardHandler> ();
RegisterBackend<ISearchTextEntryBackend, SearchTextEntryBackend> ();
RegisterBackend<IWebViewBackend, WebViewBackend> ();
string typeName = null;
string asmName = null;

Просмотреть файл

@ -43,6 +43,7 @@ namespace Xwt.GtkBackend
internal const string LIBPANGOCAIRO = "libpangocairo-1.0-0.dll";
internal const string LIBGTKGLUE = "gtksharpglue-2";
internal const string LIBGLIBGLUE = "glibsharpglue-2";
internal const string LIBWEBKIT = "libwebkitgtk-1.0";
}
/// <summary>

Просмотреть файл

@ -0,0 +1,366 @@
//
// GtkWebKitMini.cs
//
// Author:
// Vsevolod Kukol <sevo@sevo.org>
//
// Copyright (c) 2014 Vsevolod Kukol
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Runtime.InteropServices;
namespace Xwt.GtkBackend.WebKit
{
public class WebView : Gtk.Container
{
public WebView(IntPtr raw) : base(raw)
{
}
public WebView () : base (IntPtr.Zero)
{
Raw = webkit_web_view_new();
}
public void LoadUri(string uri) {
IntPtr native_uri = GLib.Marshaller.StringToPtrGStrdup (uri);
webkit_web_view_load_uri(Handle, native_uri);
GLib.Marshaller.Free (native_uri);
}
public string Uri {
get {
IntPtr raw_ret = webkit_web_view_get_uri(Handle);
string ret = GLib.Marshaller.Utf8PtrToString (raw_ret);
return ret;
}
}
public double LoadProgress {
get {
double ret = webkit_web_view_get_progress(Handle);
return ret;
}
}
public bool FullContentZoom {
get {
bool raw_ret = webkit_web_view_get_full_content_zoom(Handle);
bool ret = raw_ret;
return ret;
}
set {
webkit_web_view_set_full_content_zoom(Handle, value);
}
}
public void StopLoading() {
webkit_web_view_stop_loading(Handle);
}
public void Reload() {
webkit_web_view_reload(Handle);
}
public bool CanGoBack() {
bool raw_ret = webkit_web_view_can_go_back(Handle);
bool ret = raw_ret;
return ret;
}
public void GoBack() {
webkit_web_view_go_back(Handle);
}
public void GoForward() {
webkit_web_view_go_forward(Handle);
}
public bool CanGoForward() {
bool raw_ret = webkit_web_view_can_go_forward(Handle);
bool ret = raw_ret;
return ret;
}
public void LoadHtmlString(string content, string base_uri) {
IntPtr native_content = GLib.Marshaller.StringToPtrGStrdup (content);
IntPtr native_base_uri = GLib.Marshaller.StringToPtrGStrdup (base_uri);
webkit_web_view_load_html_string(Handle, native_content, native_base_uri);
GLib.Marshaller.Free (native_content);
GLib.Marshaller.Free (native_base_uri);
}
public string Title {
get {
IntPtr raw_ret = webkit_web_view_get_title(Handle);
string ret = GLib.Marshaller.Utf8PtrToString (raw_ret);
return ret;
}
}
[GLib.Signal("load-finished")]
public event EventHandler<GLib.SignalArgs> LoadFinished {
add {
this.AddSignalHandler ("load-finished", value, typeof(GLib.SignalArgs));
}
remove {
this.RemoveSignalHandler ("load-finished", value);
}
}
[GLib.Signal("load-started")]
public event EventHandler<GLib.SignalArgs> LoadStarted {
add {
this.AddSignalHandler ("load-started", value, typeof(GLib.SignalArgs));
}
remove {
this.RemoveSignalHandler ("load-started", value);
}
}
[GLib.Signal("navigation-requested")]
public event EventHandler<NavigationRequestedArgs> NavigationRequested {
add {
this.AddSignalHandler ("navigation-requested", value, typeof(NavigationRequestedArgs));
}
remove {
this.RemoveSignalHandler ("navigation-requested", value);
}
}
[GLib.Signal("title-changed")]
public event EventHandler<TitleChangedArgs> TitleChanged {
add {
this.AddSignalHandler ("title-changed", value, typeof(TitleChangedArgs));
}
remove {
this.RemoveSignalHandler ("title-changed", value);
}
}
static WebView ()
{
Initialize ();
}
// static WebView ()
// {
// ObjectManager.Initialize ();
// }
static bool initialized = false;
internal static void Initialize ()
{
if (initialized)
return;
initialized = true;
GLib.GType.Register (WebView.GType, typeof (WebView));
GLib.GType.Register (NetworkRequest.GType, typeof (NetworkRequest));
}
public static new GLib.GType GType {
get {
IntPtr raw_ret = webkit_web_view_get_type();
GLib.GType ret = new GLib.GType(raw_ret);
return ret;
}
}
[DllImport (GtkInterop.LIBWEBKIT)]
static extern IntPtr webkit_web_view_new();
[DllImport (GtkInterop.LIBWEBKIT)]
static extern IntPtr webkit_web_view_get_type();
[DllImport (GtkInterop.LIBWEBKIT)]
static extern void webkit_web_view_load_uri(IntPtr raw, IntPtr uri);
[DllImport (GtkInterop.LIBWEBKIT)]
static extern IntPtr webkit_web_view_get_uri(IntPtr raw);
[DllImport (GtkInterop.LIBWEBKIT)]
static extern bool webkit_web_view_get_full_content_zoom(IntPtr raw);
[DllImport (GtkInterop.LIBWEBKIT)]
static extern void webkit_web_view_set_full_content_zoom(IntPtr raw, bool full_content_zoom);
[DllImport (GtkInterop.LIBWEBKIT)]
static extern void webkit_web_view_stop_loading(IntPtr raw);
[DllImport (GtkInterop.LIBWEBKIT)]
static extern void webkit_web_view_reload(IntPtr raw);
[DllImport (GtkInterop.LIBWEBKIT)]
static extern bool webkit_web_view_can_go_back(IntPtr raw);
[DllImport (GtkInterop.LIBWEBKIT)]
static extern void webkit_web_view_go_back(IntPtr raw);
[DllImport (GtkInterop.LIBWEBKIT)]
static extern bool webkit_web_view_can_go_forward(IntPtr raw);
[DllImport (GtkInterop.LIBWEBKIT)]
static extern void webkit_web_view_go_forward(IntPtr raw);
[DllImport (GtkInterop.LIBWEBKIT)]
static extern void webkit_web_view_load_html_string(IntPtr raw, IntPtr content, IntPtr base_uri);
[DllImport (GtkInterop.LIBWEBKIT)]
static extern IntPtr webkit_web_view_get_title(IntPtr raw);
[DllImport (GtkInterop.LIBWEBKIT)]
static extern double webkit_web_view_get_progress(IntPtr raw);
}
public sealed class NetworkRequest : GLib.Object {
public NetworkRequest(IntPtr raw) : base(raw) {}
public NetworkRequest (string uri) : base (IntPtr.Zero)
{
IntPtr native_uri = GLib.Marshaller.StringToPtrGStrdup (uri);
Raw = webkit_network_request_new(native_uri);
GLib.Marshaller.Free (native_uri);
}
public static new GLib.GType GType {
get {
IntPtr raw_ret = webkit_network_request_get_type();
GLib.GType ret = new GLib.GType(raw_ret);
return ret;
}
}
public string Uri {
get {
IntPtr raw_ret = webkit_network_request_get_uri(Handle);
string ret = GLib.Marshaller.Utf8PtrToString (raw_ret);
return ret;
}
set {
IntPtr native_value = GLib.Marshaller.StringToPtrGStrdup (value);
webkit_network_request_set_uri(Handle, native_value);
GLib.Marshaller.Free (native_value);
}
}
static NetworkRequest ()
{
WebView.Initialize ();
//GtkSharp.WebkitSharp.ObjectManager.Initialize ();
}
[DllImport (GtkInterop.LIBWEBKIT)]
static extern IntPtr webkit_network_request_new(IntPtr uri);
[DllImport (GtkInterop.LIBWEBKIT)]
static extern IntPtr webkit_network_request_get_type();
[DllImport (GtkInterop.LIBWEBKIT)]
static extern IntPtr webkit_network_request_get_uri(IntPtr raw);
[DllImport (GtkInterop.LIBWEBKIT)]
static extern void webkit_network_request_set_uri(IntPtr raw, IntPtr uri);
}
public enum NavigationResponse {
Accept,
Ignore,
Download,
}
public class LoadProgressChangedArgs : GLib.SignalArgs
{
public int Progress
{
get {
return (int)Args [0];
}
}
}
public class NavigationRequestedArgs : GLib.SignalArgs
{
// public WebKit.WebFrame Frame{
// get {
// return (WebKit.WebFrame) Args [0];
// }
// }
public IntPtr Frame
{
get {
return (IntPtr) Args [0];
}
}
public NetworkRequest Request
{
get {
return (NetworkRequest) Args [1];
}
}
}
public class TitleChangedArgs : GLib.SignalArgs
{
public string Title
{
get {
return (string)Args [1];
}
}
}
// public class ObjectManager {
//
// static bool initialized = false;
// // Call this method from the appropriate module init function.
// public static void Initialize ()
// {
// if (initialized)
// return;
//
// initialized = true;
// GLib.GType.Register (WebKit.Download.GType, typeof (WebKit.Download));
// GLib.GType.Register (WebKit.WebFrame.GType, typeof (WebKit.WebFrame));
// GLib.GType.Register (NetworkRequest.GType, typeof (NetworkRequest));
// GLib.GType.Register (WebKit.SecurityOrigin.GType, typeof (WebKit.SecurityOrigin));
// GLib.GType.Register (WebKit.WebHistoryItem.GType, typeof (WebKit.WebHistoryItem));
// GLib.GType.Register (WebKit.WebDatabase.GType, typeof (WebKit.WebDatabase));
// GLib.GType.Register (WebKit.WebPolicyDecision.GType, typeof (WebKit.WebPolicyDecision));
// GLib.GType.Register (WebKit.SoupAuthDialog.GType, typeof (WebKit.SoupAuthDialog));
// GLib.GType.Register (WebKit.WebWindowFeatures.GType, typeof (WebKit.WebWindowFeatures));
// GLib.GType.Register (WebKit.WebDataSource.GType, typeof (WebKit.WebDataSource));
// GLib.GType.Register (WebKit.WebSettings.GType, typeof (WebKit.WebSettings));
// GLib.GType.Register (WebKit.NetworkResponse.GType, typeof (WebKit.NetworkResponse));
// GLib.GType.Register (WebKit.WebResource.GType, typeof (WebKit.WebResource));
// GLib.GType.Register (WebView.GType, typeof (WebView));
// GLib.GType.Register (WebKit.WebInspector.GType, typeof (WebKit.WebInspector));
// GLib.GType.Register (WebKit.WebNavigationAction.GType, typeof (WebKit.WebNavigationAction));
// }
// }
}

Просмотреть файл

@ -1199,6 +1199,18 @@ namespace Xwt.GtkBackend
supportsHiResIcons = false;
return null;
}
public static void AddSignalHandler (this Gtk.Widget widget, string name, Delegate handler, Type args_type)
{
var signal = GLib.Signal.Lookup (widget, name, args_type);
signal.AddDelegate (handler);
}
public static void RemoveSignalHandler (this Gtk.Widget widget, string name, Delegate handler)
{
var signal = GLib.Signal.Lookup (widget, name);
signal.RemoveDelegate (handler);
}
}
public struct KeyboardShortcut : IEquatable<KeyboardShortcut>

Просмотреть файл

@ -0,0 +1,166 @@
//
// WebViewBackend.cs
//
// Author:
// Vsevolod Kukol <sevo@sevo.org>
//
// Copyright (c) 2014 Vsevolod Kukol
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Xwt.Backends;
using System.Runtime.InteropServices;
using Xwt.GtkBackend.WebKit;
namespace Xwt.GtkBackend
{
public class WebViewBackend : WidgetBackend, IWebViewBackend
{
WebKit.WebView view;
public WebViewBackend ()
{
}
public override void Initialize()
{
base.Initialize ();
view = new WebKit.WebView ();
Widget = view;
Widget.Show ();
}
public string Url {
get { return view.Uri; }
set {
view.LoadUri (value);
}
}
public string Title {
get {
return view.Title;
}
}
public double LoadProgress {
get {
return view.LoadProgress;
}
}
public bool CanGoBack {
get {
return view.CanGoBack ();
}
}
public bool CanGoForward {
get {
return view.CanGoForward ();
}
}
public void GoBack ()
{
view.GoBack ();
}
public void GoForward ()
{
view.GoForward ();
}
public void Reload ()
{
view.Reload ();
}
public void StopLoading ()
{
view.StopLoading ();
}
public void LoadHtml (string content, string base_uri)
{
view.LoadHtmlString (content, base_uri);
}
protected new IWebViewEventSink EventSink {
get { return (IWebViewEventSink)base.EventSink; }
}
public override void EnableEvent (object eventId)
{
base.EnableEvent (eventId);
if (eventId is WebViewEvent) {
switch ((WebViewEvent)eventId) {
case WebViewEvent.NavigateToUrl: view.NavigationRequested += HandleNavigationRequested; break;
case WebViewEvent.Loading: view.LoadStarted += HandleLoadStarted; break;
case WebViewEvent.Loaded: view.LoadFinished += HandleLoadFinished; break;
case WebViewEvent.TitleChanged: view.TitleChanged += HandleTitleChanged; break;
}
}
}
public override void DisableEvent (object eventId)
{
base.DisableEvent (eventId);
if (eventId is WebViewEvent) {
switch ((WebViewEvent)eventId) {
case WebViewEvent.NavigateToUrl: view.NavigationRequested -= HandleNavigationRequested; break;
case WebViewEvent.Loading: view.LoadStarted -= HandleLoadStarted; break;
case WebViewEvent.Loaded: view.LoadFinished -= HandleLoadFinished; break;
case WebViewEvent.TitleChanged: view.TitleChanged -= HandleTitleChanged; break;
}
}
}
void HandleNavigationRequested (object sender, WebKit.NavigationRequestedArgs e)
{
ApplicationContext.InvokeUserCode (delegate {
if (EventSink.OnNavigateToUrl (e.Request.Uri))
e.RetVal = NavigationResponse.Ignore;
});
}
void HandleLoadStarted (object o, EventArgs args)
{
ApplicationContext.InvokeUserCode (delegate {
EventSink.OnLoading ();
});
}
void HandleLoadFinished (object o, EventArgs args)
{
ApplicationContext.InvokeUserCode (delegate {
EventSink.OnLoaded ();
});
}
void HandleTitleChanged (object sender, WebKit.TitleChangedArgs e)
{
ApplicationContext.InvokeUserCode (delegate {
EventSink.OnTitleChanged ();
});
}
}
}

Просмотреть файл

@ -3,6 +3,7 @@
//
// Author:
// Cody Russell <cody@xamarin.com>
// Vsevolod Kukol <sevo@sevo.org>
//
// Copyright (c) 2014 Xamarin Inc.
//
@ -30,9 +31,6 @@ using MonoMac.AppKit;
using MonoMac.Foundation;
using MonoMac.WebKit;
using MonoMac.ObjCRuntime;
using Xwt.Drawing;
namespace Xwt.Mac
{
public class WebViewBackend : ViewBackend<MonoMac.WebKit.WebView, IWebViewEventSink>, IWebViewBackend
@ -59,6 +57,122 @@ namespace Xwt.Mac
Widget.MainFrameUrl = value;
}
}
public string Title {
get {
return Widget.MainFrameTitle;
}
}
public double LoadProgress {
get {
return Widget.EstimatedProgress;
}
}
public bool CanGoBack {
get {
return Widget.CanGoBack ();
}
}
public bool CanGoForward {
get {
return Widget.CanGoForward ();
}
}
public void GoBack ()
{
Widget.GoBack ();
}
public void GoForward ()
{
Widget.GoForward ();
}
public void Reload ()
{
Widget.MainFrame.Reload ();
}
public void StopLoading ()
{
Widget.MainFrame.StopLoading ();
}
public void LoadHtml (string content, string base_uri)
{
Widget.MainFrame.LoadHtmlString (content, new NSUrl(base_uri));
}
protected new IWebViewEventSink EventSink {
get { return (IWebViewEventSink)base.EventSink; }
}
public override void EnableEvent (object eventId)
{
base.EnableEvent (eventId);
if (eventId is WebViewEvent) {
switch ((WebViewEvent)eventId) {
case WebViewEvent.NavigateToUrl: Widget.StartedProvisionalLoad += HandleStartedProvisionalLoad; break;
case WebViewEvent.Loading: Widget.CommitedLoad += HandleLoadStarted; break;
case WebViewEvent.Loaded: Widget.FinishedLoad += HandleLoadFinished; break;
case WebViewEvent.TitleChanged: Widget.ReceivedTitle += HandleTitleChanged; break;
}
}
}
public override void DisableEvent (object eventId)
{
base.DisableEvent (eventId);
if (eventId is WebViewEvent) {
switch ((WebViewEvent)eventId) {
case WebViewEvent.NavigateToUrl: Widget.StartedProvisionalLoad -= HandleStartedProvisionalLoad; break;
case WebViewEvent.Loading: Widget.CommitedLoad += HandleLoadStarted; break;
case WebViewEvent.Loaded: Widget.FinishedLoad -= HandleLoadFinished; break;
case WebViewEvent.TitleChanged: Widget.ReceivedTitle -= HandleTitleChanged; break;
}
}
}
void HandleStartedProvisionalLoad (object sender, WebFrameEventArgs e)
{
var url = String.Empty;
if (e.ForFrame.ProvisionalDataSource.Request.MainDocumentURL != null)
url = e.ForFrame.ProvisionalDataSource.Request.MainDocumentURL.ToString ();
if (String.IsNullOrEmpty (url))
return;
bool cancel = false;
ApplicationContext.InvokeUserCode (delegate {
cancel = EventSink.OnNavigateToUrl(url);
});
if (cancel)
e.ForFrame.StopLoading ();
}
void HandleLoadStarted (object o, EventArgs args)
{
ApplicationContext.InvokeUserCode (delegate {
EventSink.OnLoading ();
});
}
void HandleLoadFinished (object o, EventArgs args)
{
ApplicationContext.InvokeUserCode (delegate {
EventSink.OnLoaded ();
});
}
void HandleTitleChanged (object sender, WebFrameTitleEventArgs e)
{
ApplicationContext.InvokeUserCode (delegate {
EventSink.OnTitleChanged ();
});
}
#endregion
}

Просмотреть файл

@ -3,6 +3,7 @@
//
// Author:
// Cody Russell <cody@xamarin.com>
// Vsevolod Kukol <sevo@sevo.org>
//
// Copyright (c) 2014 Xamarin Inc.
//
@ -34,24 +35,149 @@ namespace Xwt.WPFBackend
public class WebViewBackend : WidgetBackend, IWebViewBackend
{
string url;
SWC.WebBrowser view;
bool enableNavigatingEvent, enableLoadingEvent, enableLoadedEvent, enableTitleChangedEvent;
public WebViewBackend ()
public WebViewBackend () : this (new SWC.WebBrowser ())
{
Widget = new SWC.WebBrowser ();
}
internal WebViewBackend (SWC.WebBrowser browser)
{
Widget = browser;
view = browser;
view.Navigating += HandleNavigating;
view.Navigated += HandleNavigated;
view.LoadCompleted += HandleLoadCompleted;
Widget = view;
}
public string Url {
get { return url; }
set {
url = value;
((SWC.WebBrowser)Widget).Navigate (url);
view.Navigate (url);
}
}
public double LoadProgress { get; protected set; }
public bool CanGoBack {
get {
return view.CanGoBack;
}
}
public bool CanGoForward {
get {
return view.CanGoForward;
}
}
public void GoBack ()
{
view.GoBack ();
}
public void GoForward ()
{
view.GoForward ();
}
public void Reload ()
{
view.Refresh ();
}
public void StopLoading ()
{
view.InvokeScript ("eval", "document.execCommand('Stop');");
}
public void LoadHtml (string content, string base_uri)
{
view.NavigateToString (content);
}
string prevTitle = String.Empty;
public string Title { get; private set; }
protected new IWebViewEventSink EventSink {
get { return (IWebViewEventSink)base.EventSink; }
}
public override void EnableEvent (object eventId)
{
base.EnableEvent (eventId);
if (eventId is WebViewEvent) {
switch ((WebViewEvent)eventId) {
case WebViewEvent.NavigateToUrl:
enableNavigatingEvent = true;
break;
case WebViewEvent.Loading:
enableLoadingEvent = true;
break;
case WebViewEvent.Loaded:
enableLoadedEvent = true;
break;
case WebViewEvent.TitleChanged:
enableTitleChangedEvent = true;
break;
}
}
}
public override void DisableEvent (object eventId)
{
base.DisableEvent (eventId);
if (eventId is WebViewEvent) {
switch ((WebViewEvent)eventId) {
case WebViewEvent.NavigateToUrl:
enableNavigatingEvent = false;
break;
case WebViewEvent.Loading:
enableLoadingEvent = false;
break;
case WebViewEvent.Loaded:
enableLoadedEvent = false;
break;
case WebViewEvent.TitleChanged:
enableTitleChangedEvent = false;
break;
}
}
}
void HandleNavigating (object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
{
if (enableNavigatingEvent) {
var url = e.Uri.AbsoluteUri;
Context.InvokeUserCode (delegate {
e.Cancel = EventSink.OnNavigateToUrl (url);
});
}
}
void HandleLoadCompleted (object sender, System.Windows.Navigation.NavigationEventArgs e)
{
LoadProgress = 1;
if (enableLoadedEvent)
Context.InvokeUserCode (EventSink.OnLoaded);
Title = (string)view.InvokeScript("eval", "document.title.toString()");
if (enableTitleChangedEvent && (prevTitle != Title))
Context.InvokeUserCode (EventSink.OnTitleChanged);
prevTitle = Title;
}
void HandleNavigated (object sender, System.Windows.Navigation.NavigationEventArgs e)
{
LoadProgress = 0;
url = e.Uri.AbsoluteUri;
if (enableLoadingEvent)
Context.InvokeUserCode (delegate {
EventSink.OnLoading ();
});
}
}
}

Просмотреть файл

@ -3,6 +3,7 @@
//
// Author:
// Cody Russell <cody@xamarin.com>
// Vsevolod Kukol <sevo@sevo.org>
//
// Copyright (c) 2014 Xamarin Inc.
//
@ -30,15 +31,30 @@ namespace Xwt.Backends
public interface IWebViewBackend : IWidgetBackend
{
string Url { get; set; }
string Title { get; }
double LoadProgress { get; }
bool CanGoBack { get; }
void GoBack ();
bool CanGoForward { get; }
void GoForward ();
void Reload ();
void StopLoading ();
void LoadHtml (string content, string base_uri);
}
public interface IWebViewEventSink : IWidgetEventSink
{
void OnLoaded ();
void OnLoading ();
bool OnNavigateToUrl (string url);
void OnTitleChanged ();
}
public enum WebViewEvent
{
Loaded = 1
Loaded = 1,
Loading = 2,
NavigateToUrl = 3,
TitleChanged = 4,
}
}

Просмотреть файл

@ -3,6 +3,7 @@
//
// Author:
// Cody Russell <cody@xamarin.com>
// Vsevolod Kukol <sevo@sevo.org>
//
// Copyright (c) 2014 Xamarin Inc.
//
@ -34,20 +35,43 @@ namespace Xwt
[BackendType (typeof(IWebViewBackend))]
public class WebView : Widget
{
EventHandler loading;
EventHandler loaded;
string url;
EventHandler<NavigateToUrlEventArgs> navigateToUrl;
EventHandler titleChanged;
string url = String.Empty;
protected new class WidgetBackendHost : Widget.WidgetBackendHost, IWebViewEventSink
{
public bool OnNavigateToUrl (string url)
{
var args = new NavigateToUrlEventArgs (new Uri(url, UriKind.RelativeOrAbsolute));
((WebView)Parent).OnNavigateToUrl (args);
return args.Handled;
}
public void OnLoading ()
{
((WebView)Parent).OnLoading (EventArgs.Empty);
}
public void OnLoaded ()
{
((WebView)Parent).OnLoaded (EventArgs.Empty);
}
public void OnTitleChanged ()
{
((WebView)Parent).OnTitleChanged (EventArgs.Empty);
}
}
static WebView ()
{
MapEvent (WebViewEvent.Loading, typeof(WebView), "OnLoading");
MapEvent (WebViewEvent.Loaded, typeof(WebView), "OnLoaded");
MapEvent (WebViewEvent.NavigateToUrl, typeof(WebView), "OnNavigateToUrl");
MapEvent (WebViewEvent.TitleChanged, typeof(WebView), "OnTitleChanged");
}
public WebView ()
@ -70,15 +94,84 @@ namespace Xwt
[DefaultValue("")]
public string Url {
get { return url ?? ""; }
get {
if (!String.IsNullOrEmpty (Backend.Url))
url = Backend.Url;
return url;
}
set {
url = value;
Backend.Url = url;
}
}
[DefaultValue("")]
public string Title {
get { return Backend.Title ?? ""; }
}
[DefaultValue(0.0)]
public double LoadProgress {
get { return Backend.LoadProgress; }
}
[DefaultValue(false)]
public bool CanGoBack {
get { return Backend.CanGoBack; }
}
[DefaultValue(false)]
public bool CanGoForward {
get { return Backend.CanGoForward; }
}
public void GoBack ()
{
Backend.GoBack ();
}
public void GoForward ()
{
Backend.GoForward ();
}
public void Reload ()
{
Backend.Reload ();
}
public void StopLoading ()
{
Backend.StopLoading ();
}
public void LoadHtml (string content, string base_uri)
{
Backend.LoadHtml (content, base_uri);
}
protected virtual void OnLoading (EventArgs e)
{
if (loading != null)
loading (this, e);
}
public event EventHandler Loading {
add {
BackendHost.OnBeforeEventAdd (WebViewEvent.Loading, loading);
loading += value;
}
remove {
loading -= value;
BackendHost.OnAfterEventRemove (WebViewEvent.Loading, loading);
}
}
protected virtual void OnLoaded (EventArgs e)
{
if (loaded != null)
loaded (this, e);
}
public event EventHandler Loaded {
@ -92,6 +185,42 @@ namespace Xwt
BackendHost.OnAfterEventRemove (WebViewEvent.Loaded, loaded);
}
}
protected virtual void OnNavigateToUrl (NavigateToUrlEventArgs e)
{
if (navigateToUrl != null)
navigateToUrl (this, e);
}
public event EventHandler<NavigateToUrlEventArgs> NavigateToUrl {
add {
BackendHost.OnBeforeEventAdd (WebViewEvent.NavigateToUrl, navigateToUrl);
navigateToUrl += value;
}
remove {
navigateToUrl -= value;
BackendHost.OnAfterEventRemove (WebViewEvent.NavigateToUrl, navigateToUrl);
}
}
protected virtual void OnTitleChanged (EventArgs e)
{
if (titleChanged != null)
titleChanged (this, e);
}
public event EventHandler TitleChanged {
add {
BackendHost.OnBeforeEventAdd (WebViewEvent.TitleChanged, titleChanged);
titleChanged += value;
}
remove {
titleChanged -= value;
BackendHost.OnAfterEventRemove (WebViewEvent.TitleChanged, titleChanged);
}
}
}
}