using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.IO;
using BobBuilder.Command;
using System.Text.RegularExpressions;
using System.Threading;
using System.Diagnostics;
namespace BobBuilder
{
internal partial class Browser : ContentBase
{
public Browser()
{
InitializeComponent();
_Control.Navigating += HandleNavigating;
_Control.DocumentCompleted += HandleDocumentCompleted;
}
public Browser( Document doc )
: this()
{
Document = doc;
Reload();
}
protected override void HandleTextChanged( object sender, Command.TextChanged command )
{
if ( Document != null && Document.Id == command.Document.Id ) Reload();
}
protected override void HandleRefreshAll( object sender, RefreshAll command )
{
Reload();
}
protected override void HandleGetTableOfContents( object sender, GetTableOfContents command )
{
if ( Document == null || Document.Id != command.Document.Id ) return;
if ( command.OK ) return;
if ( _Loading )
{
command.IsLoading = true;
return;
}
command.TOC = "<h2>Table of Contents</h2>";
command.OK = true;
}
protected override string TabTextDecoration
{
get
{
return ( Loading ? " ( loading )" : String.Empty );
}
}
Point _ScrollPosition = new Point( 0, 0 );
Point ScrollPosition { get { return _ScrollPosition; } }
int ScrollPositionX { get { return _ScrollPosition.X; } set { _ScrollPosition.X = value; } }
int ScrollPositionY { get { return _ScrollPosition.Y; } set { _ScrollPosition.Y = value; } }
bool _Loading = false;
bool Loading { get { return _Loading; } set { _Loading = value; UpdateTabText(); } }
//static Regex _Img1 = new Regex( @"(?<src>\<\s*img\s*[^(src)]*\s+src\s*=\s*"")(?<path>[^:""]*)""", RegexOptions.Compiled | RegexOptions.IgnoreCase );
//static Regex _Img2 = new Regex( @"(?<src>\<\s*img\s*[^(src)]*\s+src\s*=\s*)(?<path>[^:\s]*)\s", RegexOptions.Compiled | RegexOptions.IgnoreCase );
static Regex _Head = new Regex( @"(?<head><\s*head[^>]*>)", RegexOptions.Compiled | RegexOptions.IgnoreCase );
private void Reload()
{
SaveScrollPosition();
string html = Document.Text;
if ( !String.IsNullOrEmpty( Document.Filepath ) )
{
string dire = Path.GetDirectoryName( Document.Filepath );
//html = _Img1.Replace( html, @"${src}file://" + dire + @"\${path}""" );
//html = _Img2.Replace( html, @"${src}file://" + dire + @"\${path} " );
html = _Head.Replace( html, @"${head}" + '\n' + @"<base href=""file://" + dire + @"\"" />" + '\n' );
}
if ( ScrollPositionX != 0 || ScrollPositionY != 0 )
{
string script =
"\n\n<script type=\"text/javascript\">\n" +
"function SetScrollPostition() {\nwindow.scrollTo( " + ScrollPositionX + ", " + ScrollPositionY + " );\n}\n" +
"window.onload = SetScrollPostition;\n" +
"</script>\n\n";
html = html.Replace( "</body>", script + "</body>" );
}
Loading = true;
_Control.DocumentText = html;
}
private void SaveScrollPosition()
{
if ( Loading ) return;
var document = _Control.Document;
if ( document == null ) return;
var body = document.Body;
if ( body == null ) return;
if ( body.ScrollLeft != 0 || body.ScrollTop != 0 )
{
ScrollPositionX = body.ScrollLeft;
ScrollPositionY = body.ScrollTop;
return;
}
// for html with a DOC tag
var htmls = document.GetElementsByTagName( "HTML" );
if ( htmls == null ) return;
if ( htmls.Count == 0 ) return;
var html = htmls[ 0 ];
if ( html == null ) return;
if ( html.ScrollLeft != 0 || html.ScrollTop != 0 )
{
ScrollPositionX = html.ScrollLeft;
ScrollPositionY = html.ScrollTop;
return;
}
}
void HandleNavigating( object sender, WebBrowserNavigatingEventArgs e )
{
}
void HandleDocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e )
{
if ( ScrollPositionX != 0 || ScrollPositionY != 0 )
_Control.Document.Window.ScrollTo( ScrollPosition );
Loading = _Control.IsBusy;
}
[Conditional( "NoSmileys" )]
void NoSmileys()
{
string html = null;
html = html.Replace( ":((", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_cry.gif' />" );
html = html.Replace( ":)", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_smile.gif' />" );
html = html.Replace( ":-)", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_smile.gif' />" );
html = html.Replace( ":(", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_frown.gif' />" );
html = html.Replace( ":-(", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_frown.gif' />" );
html = html.Replace( ":-O", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_redface.gif' />" );
html = html.Replace( ":-o", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_redface.gif' />" );
html = html.Replace( ":-0", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_redface.gif' />" );
html = html.Replace( ":-D", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_biggrin.gif' />" );
html = html.Replace( ";)", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_wink.gif' />" );
html = html.Replace( ";-)", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_wink.gif' />" );
html = html.Replace( ":|", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_line.gif' />" );
html = html.Replace( ":laugh:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_laugh.gif' />" );
html = html.Replace( ":wtf:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_wtf.gif' />" );
html = html.Replace( ":rose:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/rose.gif' />" );
//html = html.Replace( ";p", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_tongue.gif' />" );
html = html.Replace( ";P", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_tongue.gif' />" );
html = html.Replace( ";-P", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_tongue.gif' />" );
html = html.Replace( ":-P", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_tongue.gif' />" );
html = html.Replace( ":~", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_squeamish.gif' />" );
html = html.Replace( ":cool:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_cool.gif' />" );
html = html.Replace( ":rolleyes:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_rolleyes.gif' />" );
html = html.Replace( ":mad:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_mad.gif' />" );
html = html.Replace( ":eek:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_eek.gif' />" );
html = html.Replace( ":confused:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_confused.gif' />" );
html = html.Replace( ":suss:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_suss.gif' />" );
html = html.Replace( " X| ", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_dead.gif' />" );
html = html.Replace( ":zzz:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_snore.gif' />" );
html = html.Replace( ":omg:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_omg.gif' />" );
html = html.Replace( ":beer:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/beer.gif' />" );
html = html.Replace( ":jig:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/jig.gif' />" );
html = html.Replace( ":^)", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_sniff.gif' />" );
html = html.Replace( ":sigh:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_sigh.gif' />" );
html = html.Replace( ":doh:", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_doh.gif' />" );
html = html.Replace( ":-\\", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/smiley_shucks.gif' />" );
//html = html.Replace( "?", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/coffee.gif' />" );
//html = html.Replace( "?", "<img align='absmiddle' src='http://www.codeproject.com/script/Forums/Images/sheep.gif' />" );
}
}
}