// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.Versioning;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using Common;
using Common.Utilities;
using Microsoft.PowerToys.Telemetry;
using PreviewHandlerCommon;
using SvgPreviewHandler.Telemetry.Events;
namespace SvgPreviewHandler
{
///
/// Implementation of Control for Svg Preview Handler.
///
public class SvgPreviewControl : FormHandlerControl
{
///
/// Extended Browser Control to display Svg.
///
private WebBrowserExt browser;
///
/// Text box to display the information about blocked elements from Svg.
///
private RichTextBox textBox;
///
/// Represent if an text box info bar is added for showing message.
///
private bool infoBarAdded = false;
///
/// Start the preview on the Control.
///
/// Stream reference to access source file.
public override void DoPreview(T dataSource)
{
this.InvokeOnControlThread(() =>
{
try
{
this.infoBarAdded = false;
string svgData = null;
using (var stream = new StreamWrapper(dataSource as IStream))
{
using (var reader = new StreamReader(stream))
{
svgData = reader.ReadToEnd();
}
}
// Add a info bar on top of the Preview if any blocked element is present.
if (SvgPreviewHandlerHelper.CheckBlockedElements(svgData))
{
this.infoBarAdded = true;
this.AddTextBoxControl(Resource.BlockedElementInfoText);
}
this.AddBrowserControl(svgData);
this.Resize += this.FormResized;
base.DoPreview(dataSource);
PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewed());
}
catch (Exception ex)
{
PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewError { Message = ex.Message });
this.Controls.Clear();
this.infoBarAdded = true;
this.AddTextBoxControl(Resource.SvgNotPreviewedError);
base.DoPreview(dataSource);
}
});
}
///
/// Occurs when RichtextBox is resized.
///
/// Reference to resized control.
/// Provides data for the ContentsResized event.
private void RTBContentsResized(object sender, ContentsResizedEventArgs e)
{
var richTextBox = sender as RichTextBox;
richTextBox.Height = e.NewRectangle.Height + 5;
}
///
/// Occurs when form is resized.
///
/// Reference to resized control.
/// Provides data for the resize event.
private void FormResized(object sender, EventArgs e)
{
if (this.infoBarAdded)
{
this.textBox.Width = this.Width;
}
}
///
/// Adds a Web Browser Control to Control Collection.
///
/// Svg to display on Browser Control.
private void AddBrowserControl(string svgData)
{
this.browser = new WebBrowserExt();
this.browser.DocumentText = svgData;
this.browser.Dock = DockStyle.Fill;
this.browser.IsWebBrowserContextMenuEnabled = false;
this.browser.ScriptErrorsSuppressed = true;
this.browser.ScrollBarsEnabled = true;
this.browser.AllowNavigation = false;
this.Controls.Add(this.browser);
}
///
/// Adds a Text Box in Controls for showing information about blocked elements.
///
/// Message to be displayed in textbox.
private void AddTextBoxControl(string message)
{
this.textBox = new RichTextBox();
this.textBox.Text = message;
this.textBox.BackColor = Color.LightYellow;
this.textBox.Multiline = true;
this.textBox.Dock = DockStyle.Top;
this.textBox.ReadOnly = true;
this.textBox.ContentsResized += this.RTBContentsResized;
this.textBox.ScrollBars = RichTextBoxScrollBars.None;
this.textBox.BorderStyle = BorderStyle.None;
this.Controls.Add(this.textBox);
}
}
}