summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Application/Componentes/WebBrowser.cs
blob: 35327bc569538290ac658331dd2a9d8acabd2af4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using Gestor.Application.Properties;
using HtmlAgilityPack;
using mshtml;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Navigation;
using System.Windows.Threading;

namespace Gestor.Application.Componentes
{
	public class WebBrowser : UserControl, IComponentConnector
	{
		public HTMLDocument HtmlDocument;

		public System.Windows.Controls.WebBrowser Browser;

		public readonly static DependencyProperty IsReadOnlyProperty;

		internal Grid GridWebBrowser;

		private bool _contentLoaded;

		public bool IsReadOnly
		{
			get
			{
				return (bool)base.GetValue(Gestor.Application.Componentes.WebBrowser.IsReadOnlyProperty);
			}
			set
			{
				base.SetValue(Gestor.Application.Componentes.WebBrowser.IsReadOnlyProperty, value);
			}
		}

		static WebBrowser()
		{
			Gestor.Application.Componentes.WebBrowser.IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(Gestor.Application.Componentes.WebBrowser), new PropertyMetadata(false));
		}

		public WebBrowser()
		{
			this.InitializeComponent();
			System.Windows.Threading.Dispatcher dispatcher = base.Dispatcher;
			if (dispatcher == null)
			{
				return;
			}
			dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(this.ContentLoad));
		}

		private void Browser_PreviewKeyDown(object sender, KeyEventArgs e)
		{
			if (this.IsReadOnly)
			{
				e.Handled = true;
			}
		}

		private void Completed(object sender, NavigationEventArgs e)
		{
			this.HtmlDocument = this.Browser.Document as HTMLDocument;
			if (this.HtmlDocument == null)
			{
				return;
			}
			this.HtmlDocument.designMode = "On";
		}

		private void ContentLoad()
		{
			base.PreviewKeyDown += new KeyEventHandler(this.Browser_PreviewKeyDown);
		}

		private static void EncodeNode(HtmlNode node)
		{
			if (node.get_HasChildNodes())
			{
				foreach (HtmlNode childNode in node.get_ChildNodes())
				{
					if (childNode.get_NodeType() != 3)
					{
						Gestor.Application.Componentes.WebBrowser.EncodeNode(childNode);
					}
					else
					{
						childNode.set_InnerHtml(WebUtility.HtmlEncode(childNode.get_InnerHtml()));
					}
				}
			}
			else if (node.get_NodeType() == 3)
			{
				node.set_InnerHtml(WebUtility.HtmlEncode(node.get_InnerHtml()));
			}
		}

		private void HideScriptErrors()
		{
			FieldInfo field = typeof(Gestor.Application.Componentes.WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
			if (field == null)
			{
				return;
			}
			object value = field.GetValue(this.Browser);
			if (value != null)
			{
				value.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, value, new object[] { true });
			}
		}

		public static string HtmlEncode(string html)
		{
			if (html == null)
			{
				return null;
			}
			HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
			htmlDocument.LoadHtml(html);
			htmlDocument.OptionWriteEmptyNodes = true;
			Gestor.Application.Componentes.WebBrowser.EncodeNode(htmlDocument.get_DocumentNode());
			return htmlDocument.get_DocumentNode().get_InnerHtml();
		}

		[DebuggerNonUserCode]
		[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			System.Windows.Application.LoadComponent(this, new Uri("/Gestor.Application;component/componentes/webbrowser.xaml", UriKind.Relative));
		}

		public void InstanciateNew(string htmlData = null)
		{
			if (this.Browser != null)
			{
				this.Browser.LoadCompleted -= new LoadCompletedEventHandler(this.Completed);
				this.Browser.Dispose();
				this.GridWebBrowser.Children.Remove(this.Browser);
			}
			HTMLDocument htmlDocument = this.HtmlDocument;
			if (htmlDocument != null)
			{
				htmlDocument.clear();
			}
			else
			{
			}
			this.Browser = new System.Windows.Controls.WebBrowser()
			{
				IsEnabled = true
			};
			this.Browser.LoadCompleted += new LoadCompletedEventHandler(this.Completed);
			this.GridWebBrowser.Children.Add(this.Browser);
			this.HideScriptErrors();
			htmlData = Gestor.Application.Componentes.WebBrowser.HtmlEncode(htmlData);
			this.Browser.NavigateToString((string.IsNullOrEmpty(htmlData) ? Gestor.Application.Properties.Resources.New : htmlData));
			this.HtmlDocument = this.Browser.Document as HTMLDocument;
			if (this.HtmlDocument == null)
			{
				return;
			}
			this.HtmlDocument.charset = "ISO-8859-1";
			this.HtmlDocument.designMode = "On";
		}

		public void ReadOnly(bool isReadOnly)
		{
			this.IsReadOnly = isReadOnly;
		}

		[DebuggerNonUserCode]
		[EditorBrowsable(EditorBrowsableState.Never)]
		[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
		void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target)
		{
			if (connectionId != 1)
			{
				this._contentLoaded = true;
				return;
			}
			this.GridWebBrowser = (Grid)target;
		}
	}
}