2011-01-19 16:15:25 +00:00

294 lines
9.0 KiB
C#

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
using VEPROMS.CSLA.Library;
using System.Text.RegularExpressions;
using System.Reflection;
namespace Volian.Controls.Library
{
public partial class StepRTBSimple : RichTextBox, IStepRTB
{
#region Fields
private RTBItem _MyRTBItem;
private IContainer _Container = null;
private ItemInfo _MyItemInfo;
private Size _AdjustSize;
private Rectangle _ContentsRectangle;
private E_EditPrintMode _EpMode;
private string _MyClassName;
private E_ViewMode _VwMode;
#endregion
#region RTB 4.1 Not being used
//[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
//static extern IntPtr LoadLibrary(string lpFileName);
//protected override CreateParams CreateParams
//{
// get
// {
// CreateParams prams = base.CreateParams;
// if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
// {
// //prams.ExStyle |= 0x020; // transparent
// prams.ClassName = "RICHEDIT50W";
// }
// return prams;
// }
//}
//[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
//static extern IntPtr LoadLibrary(string lpFileName);
//protected override CreateParams CreateParams
//{
// get
// {
// CreateParams prams = base.CreateParams;
// if (LoadLibrary("riched20.dll") != IntPtr.Zero)
// {
// //prams.ExStyle |= 0x020; // transparent
// prams.ClassName = "RICHEDIT20W";
// }
// return prams;
// }
//}
#endregion
#region Events
/// <summary>
/// Occurs in response to Link GoTo
/// </summary>
public event StepRTBLinkEvent LinkGoTo;
/// <summary>
/// Checks to see if the LinkGoTo event is handled and launches it.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void OnLinkGoTo(object sender, StepPanelLinkEventArgs args)
{
if (LinkGoTo != null) LinkGoTo(sender, args);
}
/// <summary>
/// Occurs when a content or format change causes the box to get taller or shorter
/// </summary>
public event StepRTBEvent HeightChanged;
/// <summary>
/// Checks to see if the HeightChanged event is handled and launches it
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void OnHeightChanged(object sender, EventArgs args)
{
if (HeightChanged != null) HeightChanged(sender, args);
}
#endregion
#region Properties
/// <summary>
/// ItemInfo object associated with the control
/// </summary>
/// <param name="match"></param>
/// <returns></returns>
public ItemInfo MyItemInfo
{
get { return _MyItemInfo; }
set
{
_MyItemInfo = value;
if (value != null)
{
//// TIMING: DisplayItem.TimeIt("rtbMyItem Start");
//if (value.ItemID == 90)
// if(_MyLog.IsInfoEnabled)_MyLog.InfoFormat(value.MyContent.Text);
string txt = _MyItemInfo.MyContent.Text;
txt = txt.Replace("\n", "\r\n\\par ");
//txt = Regex.Replace(txt, "(.){([0-9]*){([0-9 ]*)}}",FixTransition);
//txt = Regex.Replace(txt, @"(.)\\v TRAN", FixTransition);
MatchCollection mc = Regex.Matches(txt, @"(.)\\v (TRAN|ReferencedObject)\\v0 ([^\\]*)\\v ([^\\]*)\\v0");
//string[] parts = Regex.Split(txt, "(.){{(.*)}{(.*)}}");
if (mc.Count > 0)
{
int lastIndex = 0;
foreach (Match m in mc)
{
if(m.Index > lastIndex) AddRtf(txt.Substring(lastIndex,m.Index - lastIndex));
string linkUrl = "Link:" + ((LinkType)Convert.ToInt32(m.Groups[1].Value[0])).ToString() + ":" + m.Groups[4].Value;
string linkValue = (m.Groups[2].Value == "TRAN" ? FixTransition(m) : m.Groups[3].Value);
if (CreateParams.ClassName == "RICHEDIT50W")
AddLink50(linkUrl, linkValue);
else
AddLink20(linkUrl, linkValue);
//AddRtf(string.Format("{0} \\v ReferencedObject - {1} \\v0",ROValue,ROID));
lastIndex = m.Index + m.Length;
}
AddRtf(txt.Substring(lastIndex, txt.Length - lastIndex));
}
else
{
AddRtf(txt);
}
//// TIMING: DisplayItem.TimeIt("rtbMyItem End");
}
}
}
public RTBItem MyRTBItem
{
get { return _MyRTBItem; }
set { _MyRTBItem = value; }
}
public Size AdjustSize
{
get { return _AdjustSize; }
set { _AdjustSize = value; }
}
public Rectangle ContentsRectangle
{
get { return _ContentsRectangle; }
set { _ContentsRectangle = value; }
}
public E_EditPrintMode EpMode
{
get { return _EpMode; }
set { _EpMode = value; }
}
public string MyClassName
{
get { return _MyClassName; }
set { _MyClassName = value; }
}
public E_ViewMode VwMode
{
get { return _VwMode; }
set { _VwMode = value; }
}
#endregion
#region Private Methods
private string FixTransition(Match match)
{
//int transitionID = Convert.ToInt32(match.Groups[2].Value);
int transitionID = Convert.ToInt32(match.Groups[4].Value.Split(" ".ToCharArray())[1]);
// Find the transition
foreach (TransitionInfo ti in _MyItemInfo.MyContent.ContentTransitions)
{
if (ti.TransitionID == transitionID)
{
string path = ti.PathTo.Replace(" Section PROCEDURE STEPS ", ", ");
path = path.Replace(" Section PROCEDURE STEPS", "");
return path;
}
}
return match.Value;
}
/// <summary>
/// Adds the text and hypertext link and sets the style to link
/// </summary>
/// <param name="linkUrl">URL - Uniform Resource Locator (Address)</param>
/// <param name="linkValue">Text descibing location</param>
private void AddLink20(string linkUrl, string linkValue)
{
this.DetectUrls = false;
RTBAPI.CharFormatTwo charFormat = RTBAPI.GetCharFormat(this, RTBAPI.RTBSelection.SCF_SELECTION);
int position = SelectionStart = this.TextLength;
SelectionLength = 0;
SelectedRtf = @"{\rtf1\ansi " + linkValue + @"\v #" + linkUrl + @"\v0}";
Select(position, linkValue.Length + linkUrl.Length + 1);
// Protect the link text to avoid manual changes
charFormat.dwMask = RTBAPI.CharFormatMasks.CFM_LINK | RTBAPI.CharFormatMasks.CFM_PROTECTED;
charFormat.dwEffects = RTBAPI.CharFormatEffects.CFE_LINK | RTBAPI.CharFormatEffects.CFE_PROTECTED;
RTBAPI.SetCharFormat((RichTextBox)this, RTBAPI.RTBSelection.SCF_SELECTION, charFormat);
this.SelectionStart = this.TextLength;
this.SelectionLength = 0;
}
/// <summary>
/// Adds a hypertext link to the Rich Text
/// </summary>
/// <param name="linkUrl">URL - Uniform Resource Locator (Address)</param>
/// <param name="linkValue">Text descibing location</param>
private void AddLink50(string linkUrl, string linkValue)
{
this.DetectUrls = false;
int position = SelectionStart = this.TextLength;
SelectionLength = 0;
SelectedRtf = string.Format(@"{{\rtf\field{{\*\fldinst{{HYPERLINK ""www.volian.com #{0}"" }}}}{{\fldrslt{{\cf2\ul {1}}}}}}}", linkUrl, linkValue);
this.SelectionStart = this.TextLength;
this.SelectionLength = 0;
}
/// <summary>
/// Adds Rich Text
/// </summary>
/// <param name="rtf">this is any text and can include RTF commands or not</param>
private void AddRtf(string rtf)
{
this.SelectedRtf = @"{\rtf1 " + rtf + "}";
}
/// <summary>
/// Used by the constructors to do any standard configuration
/// </summary>
private void SetUp()
{
BorderStyle = System.Windows.Forms.BorderStyle.None;
this.DetectUrls = true;
//ContextMenuStrip = this.contextMenuStrip1;
//ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None;
//this.TextChanged += new EventHandler(VERichTextBox_TextChanged);
this.ContentsResized += new ContentsResizedEventHandler(StepRTBSimple_ContentsResized);
//if (Text != "") ResizeForText();
this.LinkClicked += new LinkClickedEventHandler(StepRTBSimple_LinkClicked);
}
void StepRTBSimple_LinkClicked(object sender, LinkClickedEventArgs args)
{
OnLinkGoTo(sender, new StepPanelLinkEventArgs(_MyRTBItem, args.LinkText));
}
#endregion
#region Constructors
public StepRTBSimple()
{
InitializeComponent();
SetUp();
}
public StepRTBSimple(IContainer container)
{
container.Add(this);
InitializeComponent();
_Container = container;
SetUp();
}
#endregion
#region Event Handlers
void StepRTBSimple_ContentsResized(object sender, ContentsResizedEventArgs e)
{
this.Height = e.NewRectangle.Height;
OnHeightChanged(sender, new EventArgs());
}
#endregion
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public interface IStepRTB
{
event StepRTBEvent HeightChanged;
ItemInfo MyItemInfo { get; set;}
RTBItem MyRTBItem { get; set;}
Size AdjustSize { get; set; }
Rectangle ContentsRectangle { get; set; }
E_EditPrintMode EpMode { get; set; }
string MyClassName { get; set; }
E_ViewMode VwMode { get; set; }
// In addition to the HeightChanged event and the MyItem property
// the constructors below should be created
// public DisplayRTBSimple()
// public DisplayRTBSimple(IContainer container)
}
public enum LinkType : int
{
ReferencedObject = 21,
Transition = 9516,
TransitionRange = 9574
}
}