368 lines
No EOL
12 KiB
C#
368 lines
No EOL
12 KiB
C#
/*
|
|
Copyright (c) 2014, Lars Brubaker
|
|
All rights reserved.
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
list of conditions and the following disclaimer.
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
this list of conditions and the following disclaimer in the documentation
|
|
and/or other materials provided with the distribution.
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
The views and conclusions contained in the software and documentation are those
|
|
of the authors and should not be interpreted as representing official policies,
|
|
either expressed or implied, of the FreeBSD Project.
|
|
*/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
using MatterHackers.Agg;
|
|
using MatterHackers.Agg.Font;
|
|
using MatterHackers.Agg.Image;
|
|
using MatterHackers.Agg.UI;
|
|
using MatterHackers.Localizations;
|
|
using MatterHackers.MatterControl.ContactForm;
|
|
using MatterHackers.MatterControl.HtmlParsing;
|
|
using MatterHackers.VectorMath;
|
|
|
|
namespace MatterHackers.MatterControl
|
|
{
|
|
public class HtmlWidget : FlowLayoutWidget
|
|
{
|
|
private LinkButtonFactory linkButtonFactory = new LinkButtonFactory();
|
|
|
|
private Stack<GuiWidget> elementsUnderConstruction = new Stack<GuiWidget>();
|
|
HtmlParser htmlParser = new HtmlParser();
|
|
|
|
public HtmlWidget(string htmlContent, Color aboutTextColor)
|
|
: base(FlowDirection.TopToBottom)
|
|
{
|
|
this.Name = "HtmlWidget";
|
|
elementsUnderConstruction.Push(this);
|
|
linkButtonFactory.fontSize = 12;
|
|
linkButtonFactory.textColor = aboutTextColor;
|
|
|
|
VAnchor = VAnchor.Fit;
|
|
HAnchor = HAnchor.Stretch;
|
|
|
|
htmlContent = htmlContent.Replace("\r", "");
|
|
htmlContent = htmlContent.Replace("\n", "");
|
|
htmlParser.ParseHtml(htmlContent, AddContent, CloseContent);
|
|
}
|
|
|
|
public class WrappingTextWidget : GuiWidget
|
|
{
|
|
private String unwrappedMessage;
|
|
private TextWidget messageContainer;
|
|
|
|
public WrappingTextWidget(string text, double pointSize = 12, Justification justification = Justification.Left, Color textColor = new Color(), bool ellipsisIfClipped = true, bool underline = false, Color backgroundColor = new Color())
|
|
{
|
|
unwrappedMessage = text;
|
|
messageContainer = new TextWidget(text, 0, 0, pointSize, justification, textColor, ellipsisIfClipped, underline);
|
|
this.BackgroundColor = backgroundColor;
|
|
messageContainer.AutoExpandBoundsToText = true;
|
|
messageContainer.HAnchor = HAnchor.Left;
|
|
messageContainer.VAnchor = VAnchor.Bottom;
|
|
this.HAnchor = HAnchor.Stretch;
|
|
this.VAnchor = VAnchor.Fit;
|
|
|
|
AddChild(messageContainer);
|
|
}
|
|
|
|
public override void OnBoundsChanged(EventArgs e)
|
|
{
|
|
AdjustTextWrap();
|
|
base.OnBoundsChanged(e);
|
|
}
|
|
|
|
private void AdjustTextWrap()
|
|
{
|
|
if (messageContainer != null)
|
|
{
|
|
double wrappingSize = this.Width - this.Padding.Width;
|
|
if (wrappingSize > 0)
|
|
{
|
|
EnglishTextWrapping wrapper = new EnglishTextWrapping(messageContainer.Printer.TypeFaceStyle.EmSizeInPoints);
|
|
string wrappedMessage = wrapper.InsertCRs(unwrappedMessage, wrappingSize);
|
|
messageContainer.Text = wrappedMessage;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void OnChildAdded(EventArgs e)
|
|
{
|
|
foreach (var child in Children)
|
|
{
|
|
if (child.VAnchor == VAnchor.Stretch)
|
|
{
|
|
this.VAnchor = VAnchor.Stretch;
|
|
}
|
|
}
|
|
base.OnChildAdded(e);
|
|
}
|
|
|
|
|
|
// Replace multiple white spaces with single whitespace
|
|
private static readonly Regex replaceMultipleWhiteSpacesWithSingleWhitespaceRegex = new Regex(@"\s+", RegexOptions.Compiled);
|
|
|
|
private void AddContent(HtmlParser htmlParser, string htmlContent)
|
|
{
|
|
ElementState elementState = htmlParser.CurrentElementState;
|
|
htmlContent = replaceMultipleWhiteSpacesWithSingleWhitespaceRegex.Replace(htmlContent, " ");
|
|
string decodedHtml = HtmlParser.UrlDecode(htmlContent);
|
|
switch (elementState.TypeName)
|
|
{
|
|
case "a":
|
|
{
|
|
elementsUnderConstruction.Push(new FlowLayoutWidget());
|
|
elementsUnderConstruction.Peek().Name = "a";
|
|
|
|
if (decodedHtml != null && decodedHtml != "")
|
|
{
|
|
Button linkButton = linkButtonFactory.Generate(decodedHtml.Replace("\r\n", "\n"));
|
|
StyledTypeFace styled = new StyledTypeFace(LiberationSansFont.Instance, elementState.PointSize);
|
|
double descentInPixels = styled.DescentInPixels;
|
|
linkButton.OriginRelativeParent = new Vector2((double)linkButton.OriginRelativeParent.X, (double)(linkButton.OriginRelativeParent.Y + descentInPixels));
|
|
linkButton.Click += (sender, mouseEvent) =>
|
|
{
|
|
ApplicationController.Instance.LaunchBrowser(elementState.Href);
|
|
};
|
|
elementsUnderConstruction.Peek().AddChild(linkButton);
|
|
}
|
|
}
|
|
break;
|
|
|
|
case "h1":
|
|
case "p":
|
|
{
|
|
elementsUnderConstruction.Push(new FlowLayoutWidget());
|
|
elementsUnderConstruction.Peek().Name = "p";
|
|
elementsUnderConstruction.Peek().HAnchor = HAnchor.Stretch;
|
|
|
|
if (decodedHtml != null && decodedHtml != "")
|
|
{
|
|
WrappingTextWidget content = new WrappingTextWidget(decodedHtml, pointSize: elementState.PointSize, textColor: ActiveTheme.Instance.PrimaryTextColor);
|
|
elementsUnderConstruction.Peek().AddChild(content);
|
|
}
|
|
}
|
|
break;
|
|
|
|
case "div":
|
|
{
|
|
elementsUnderConstruction.Push(new FlowLayoutWidget());
|
|
elementsUnderConstruction.Peek().Name = "div";
|
|
|
|
if (decodedHtml != null && decodedHtml != "")
|
|
{
|
|
TextWidget content = new TextWidget(decodedHtml, pointSize: elementState.PointSize, textColor: ActiveTheme.Instance.PrimaryTextColor);
|
|
elementsUnderConstruction.Peek().AddChild(content);
|
|
}
|
|
}
|
|
break;
|
|
|
|
case "!DOCTYPE":
|
|
break;
|
|
|
|
case "body":
|
|
break;
|
|
|
|
case "html":
|
|
break;
|
|
|
|
case "img":
|
|
{
|
|
// put the image into the widget when it is done downloading.
|
|
var image = new ImageBuffer(Math.Max(elementState.SizeFixed.x, 1), Math.Max(elementState.SizeFixed.y, 1));
|
|
var imageWidget = new ImageWidget(image);
|
|
imageWidget.Load += (s, e) => ApplicationController.Instance.DownloadToImageAsync(image, elementState.src, elementState.SizeFixed.x != 0);
|
|
|
|
if (elementsUnderConstruction.Peek().Name == "a")
|
|
{
|
|
Button linkButton = new Button(0, 0, imageWidget);
|
|
linkButton.Cursor = Cursors.Hand;
|
|
linkButton.Click += (sender, mouseEvent) =>
|
|
{
|
|
ApplicationController.Instance.LaunchBrowser(elementState.Href);
|
|
};
|
|
elementsUnderConstruction.Peek().AddChild(linkButton);
|
|
}
|
|
else
|
|
{
|
|
elementsUnderConstruction.Peek().AddChild(imageWidget);
|
|
}
|
|
}
|
|
break;
|
|
|
|
case "input":
|
|
break;
|
|
|
|
case "table":
|
|
break;
|
|
|
|
case "td":
|
|
case "span":
|
|
GuiWidget widgetToAdd;
|
|
|
|
if (elementState.Classes.Contains("translate"))
|
|
{
|
|
decodedHtml = decodedHtml.Localize();
|
|
}
|
|
if (elementState.Classes.Contains("toUpper"))
|
|
{
|
|
decodedHtml = decodedHtml.ToUpper();
|
|
}
|
|
if (elementState.Classes.Contains("versionNumber"))
|
|
{
|
|
decodedHtml = VersionInfo.Instance.ReleaseVersion;
|
|
}
|
|
if (elementState.Classes.Contains("buildNumber"))
|
|
{
|
|
decodedHtml = VersionInfo.Instance.BuildVersion;
|
|
}
|
|
|
|
Button createdButton = null;
|
|
if (elementState.Classes.Contains("centeredButton"))
|
|
{
|
|
createdButton = ApplicationController.Instance.Theme.GrayButtonFactory.Generate(decodedHtml);
|
|
widgetToAdd = createdButton;
|
|
}
|
|
else if (elementState.Classes.Contains("linkButton"))
|
|
{
|
|
double oldFontSize = linkButtonFactory.fontSize;
|
|
linkButtonFactory.fontSize = elementState.PointSize;
|
|
createdButton = linkButtonFactory.Generate(decodedHtml);
|
|
StyledTypeFace styled = new StyledTypeFace(LiberationSansFont.Instance, elementState.PointSize);
|
|
double descentInPixels = styled.DescentInPixels;
|
|
createdButton.OriginRelativeParent = new Vector2((double)createdButton.OriginRelativeParent.X, (double)(createdButton.OriginRelativeParent.Y + descentInPixels));
|
|
widgetToAdd = createdButton;
|
|
linkButtonFactory.fontSize = oldFontSize;
|
|
}
|
|
else
|
|
{
|
|
TextWidget content = new TextWidget(decodedHtml, pointSize: elementState.PointSize, textColor: ActiveTheme.Instance.PrimaryTextColor);
|
|
widgetToAdd = content;
|
|
}
|
|
|
|
if (createdButton != null)
|
|
{
|
|
if (elementState.Id == "sendFeedback")
|
|
{
|
|
createdButton.Click += (s, e) => ContactFormWindow.Open();
|
|
}
|
|
else if (elementState.Id == "clearCache")
|
|
{
|
|
createdButton.Click += (s, e) => CacheDirectory.DeleteCacheData(0);
|
|
}
|
|
}
|
|
|
|
if (elementState.VerticalAlignment == ElementState.VerticalAlignType.top)
|
|
{
|
|
widgetToAdd.VAnchor = VAnchor.Top;
|
|
}
|
|
|
|
elementsUnderConstruction.Peek().AddChild(widgetToAdd);
|
|
break;
|
|
|
|
case "tr":
|
|
elementsUnderConstruction.Push(new FlowLayoutWidget());
|
|
elementsUnderConstruction.Peek().Name = "tr";
|
|
if (elementState.SizePercent.y == 100)
|
|
{
|
|
elementsUnderConstruction.Peek().VAnchor = VAnchor.Stretch;
|
|
this.VAnchor = VAnchor.Stretch;
|
|
}
|
|
if (elementState.Alignment == ElementState.AlignType.center)
|
|
{
|
|
elementsUnderConstruction.Peek().HAnchor |= HAnchor.Center;
|
|
}
|
|
break;
|
|
|
|
default:
|
|
throw new NotImplementedException("Don't know what to do with '{0}'".FormatWith(elementState.TypeName));
|
|
}
|
|
}
|
|
|
|
private void CloseContent(HtmlParser htmlParser, string htmlContent)
|
|
{
|
|
ElementState elementState = htmlParser.CurrentElementState;
|
|
switch (elementState.TypeName)
|
|
{
|
|
case "a":
|
|
GuiWidget aWidget = elementsUnderConstruction.Pop();
|
|
if (aWidget.Name != "a")
|
|
{
|
|
throw new Exception("Should have been 'a'.");
|
|
}
|
|
elementsUnderConstruction.Peek().AddChild(aWidget);
|
|
break;
|
|
|
|
case "body":
|
|
break;
|
|
|
|
case "html":
|
|
break;
|
|
|
|
case "h1":
|
|
case "p":
|
|
GuiWidget pWidget = elementsUnderConstruction.Pop();
|
|
if (pWidget.Name != "p")
|
|
{
|
|
throw new Exception("Should have been 'p'.");
|
|
}
|
|
elementsUnderConstruction.Peek().AddChild(pWidget);
|
|
break;
|
|
|
|
case "div":
|
|
GuiWidget divWidget = elementsUnderConstruction.Pop();
|
|
if (divWidget.Name != "div")
|
|
{
|
|
throw new Exception("Should have been 'div'.");
|
|
}
|
|
elementsUnderConstruction.Peek().AddChild(divWidget);
|
|
break;
|
|
|
|
case "input":
|
|
break;
|
|
|
|
case "table":
|
|
break;
|
|
|
|
case "span":
|
|
break;
|
|
|
|
case "tr":
|
|
GuiWidget trWidget = elementsUnderConstruction.Pop();
|
|
if (trWidget.Name != "tr")
|
|
{
|
|
throw new Exception("Should have been 'tr'.");
|
|
}
|
|
elementsUnderConstruction.Peek().AddChild(trWidget);
|
|
break;
|
|
|
|
case "td":
|
|
break;
|
|
|
|
case "img":
|
|
break;
|
|
|
|
default:
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|
|
} |