// Copyright 2005 by Omar Al Zabir. All rights are reserved.
//
// If you like this code then feel free to go ahead and use it.
// The only thing I ask is that you don't remove or alter my copyright notice.
//
// Your use of this software is entirely at your own risk. I make no claims or
// warrantees about the reliability or fitness of this code for any particular purpose.
// If you make changes or additions to this code please mark your code as being yours.
//
// website http://www.oazabir.com, email OmarAlZabir@gmail.com, msn oazabir@hotmail.com
using System;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Text;
using System.Text.RegularExpressions;
using mshtml;
using DHTMLEDLib;
using AxDHTMLEDLib;
using System.Collections;
namespace RSSBlogAPI{
/// <summary>
/// HTML Editor Control
/// </summary>
internal class HtmlEditor : AxDHTMLEdit
{
private ColorDialog _ColorSelectionDialog;
public HtmlEditor()
{
this._ColorSelectionDialog = new ColorDialog();
}
#region Commands
public void Bold()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_BOLD, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void Underline()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_UNDERLINE, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void Italic()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_ITALIC, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void InsertHyperlink( )
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_HYPERLINK, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void InsertImage()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_IMAGE, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void InsertImage( string url )
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_IMAGE, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref nullArg );
}
public void Cut()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_CUT, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void Copy()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_COPY, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void Paste()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_PASTE, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void JustifyLeft()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_JUSTIFYLEFT, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void JustifyCenter()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_JUSTIFYCENTER, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void JustifyRight()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_JUSTIFYRIGHT, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void JustifyFull()
{
string selection = this.SelectedHtml;
Regex rx = new Regex( "<p[^>]*>", RegexOptions.IgnoreCase );
selection = rx.Replace( selection, "<p align=\"justify\">" );
this.SelectedHtml = selection;
}
public void Undo()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_UNDO, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void Redo()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_REDO, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void Indent()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_INDENT, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void Outdent()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_OUTDENT, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void OrderList()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_ORDERLIST, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void UnorderList()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_UNORDERLIST, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void RemoveFormat()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_REMOVEFORMAT, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void RemoveLink()
{
object nullArg = null;
this.ExecCommand( DHTMLEDITCMDID.DECMD_UNLINK, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref nullArg );
}
public void SetColor()
{
if ( DialogResult.OK == this._ColorSelectionDialog.ShowDialog( this ) )
SetColor( ColorTranslator.ToHtml( this._ColorSelectionDialog.Color ) );
}
public void SetColor( string htmlColor )
{
object arg = htmlColor;
this.ExecCommand( DHTMLEDITCMDID.DECMD_SETFORECOLOR, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref arg );
}
public void SetFont( string font )
{
object arg = font;
this.ExecCommand( DHTMLEDITCMDID.DECMD_SETFONTNAME, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref arg );
}
public void SetSize( int size )
{
object arg = size;
this.ExecCommand( DHTMLEDITCMDID.DECMD_SETFONTSIZE, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref arg );
}
#endregion
#region Selection Processing
public string SelectedText
{
get
{
IHTMLSelectionObject selection = this.DOM.selection;
if ( selection.type == "Text" )
{
IHTMLTxtRange range = selection.createRange() as IHTMLTxtRange;
range.select();
return range.text;
}
return string.Empty;
}
set
{
IHTMLSelectionObject selection = this.DOM.selection;
if ( selection.type != "Control" )
{
IHTMLTxtRange range = selection.createRange() as IHTMLTxtRange;
range.select();
range.text = value;
}
else
{
IHTMLControlRange range = selection.createRange() as IHTMLControlRange;
range.item(0).outerText = value;
}
}
}
public string SelectedHtml
{
get
{
IHTMLSelectionObject selection = this.DOM.selection;
if ( selection.type == "Text" || selection.type == "None" )
{
IHTMLTxtRange range = selection.createRange() as IHTMLTxtRange;
range.select();
return range.htmlText;
}
else if ( selection.type == "Control" )
{
IHTMLControlRange range = selection.createRange() as IHTMLControlRange;
range.select();
return range.item(0).outerHTML;
}
return string.Empty;
}
set
{
IHTMLSelectionObject selection = this.DOM.selection;
if ( selection.type != "Control" )
{
IHTMLTxtRange range = selection.createRange() as IHTMLTxtRange;
range.select();
range.pasteHTML( value );
}
else
{
IHTMLControlRange range = selection.createRange() as IHTMLControlRange;
range.item(0).outerHTML = value;
}
}
}
#endregion
}
}
|