200 lines
8.5 KiB
C#
200 lines
8.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using System.Text.RegularExpressions;
|
|
using DevComponents.DotNetBar;
|
|
|
|
namespace Volian.Base.Library
|
|
{
|
|
// this is the form used in the UCF (User Control of Format) feature that displays text and allows the user to modify it to
|
|
// add/remove various text attributes: Underline, bold, italics, super/sub script & Hard space.
|
|
public partial class frmRtfEdit : Form
|
|
{
|
|
public frmRtfEdit()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
private static Regex regHyphen = new Regex(@"(?<!\\)(\\u8208\?|\\u8210\?|\\u8211\?|\\u8212\?|\\u8213\?|\\_|\\endash|\\emdash)");
|
|
// Value is used by the UITypeEditor, RtfEditor, to set/return the text that is being edited.
|
|
public string Value
|
|
{
|
|
get
|
|
{
|
|
string rtbString = regHyphen.Replace(RtfToDbText(rtfBox.Rtf).Replace("<BackSlash>", "\\\\"), @"\u8209?");
|
|
int indx = rtbString.IndexOf('-');
|
|
while (indx > -1)
|
|
{
|
|
if (indx > 2)
|
|
{
|
|
if (rtbString[indx - 1] != 'i' || rtbString[indx - 2] != 'f' || rtbString[indx - 3] != '\\')
|
|
{
|
|
rtbString = rtbString.Remove(indx, 1);
|
|
rtbString = rtbString.Insert(indx, @"\u8209?");
|
|
}
|
|
if (indx + 1 > rtbString.Length) indx = -1;
|
|
else indx = rtbString.IndexOf('-', indx + 1);
|
|
}
|
|
}
|
|
return rtbString;
|
|
}
|
|
set
|
|
{
|
|
string makeRtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}\viewkind4\uc1\pard\f0\fs16 " + value + @"\par}";
|
|
makeRtf = makeRtf.Replace(@"\xa0", "'");
|
|
rtfBox.Rtf = makeRtf;
|
|
}
|
|
}
|
|
// when saving text, keep all of the necessary rtf commands that are used to define the text attributes. The rest are removed
|
|
public static string StaticReplaceRTFClause(Match m)
|
|
{
|
|
try
|
|
{
|
|
string token = m.Groups[1].Value;
|
|
switch (token[1])
|
|
{
|
|
case '\\':
|
|
return token;
|
|
case 'u':
|
|
if (Regex.IsMatch(token, @"^\\u[0-9]+$"))
|
|
return token; // Special Charcaters
|
|
if (Regex.IsMatch(token, @"^\\ulnone ?$"))
|
|
return token;
|
|
if (Regex.IsMatch(token, @"^\\ul.*$"))
|
|
return token; // Underline
|
|
if (Regex.IsMatch(token, @"^\\up[0-9] ?$"))
|
|
return token; // shift up (superscript)
|
|
break;
|
|
case 'd':
|
|
if (Regex.IsMatch(token, @"^\\dn[0-9] ?$"))
|
|
return token; // shift down (subscript)
|
|
break;
|
|
case '\'': // Special Character
|
|
return token;
|
|
case 'b': // Bold
|
|
return token;
|
|
case 'i': // Italics
|
|
return token;
|
|
case '{': // look for escape for curly braces:
|
|
return token;
|
|
case '}':
|
|
return token;
|
|
case 'l':
|
|
if (Regex.IsMatch(token, @"^\\line ?$")) return token;
|
|
if (Regex.IsMatch(token, @"^\\li[-0-9]+ ?$")) return token; // line indent
|
|
break;
|
|
case 'f':
|
|
if (Regex.IsMatch(token, @"^\\fi[-0-9]+ ?$")) return token; // first line indent
|
|
break;
|
|
case 'p':
|
|
if (Regex.IsMatch(token, @"^\\par ?$")) return @"{\par}";
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("StaticReplaceRTFClause {0} - {1}", ex.GetType().Name, ex.Message);
|
|
}
|
|
return "";//Strip All
|
|
}
|
|
// remove all of the rtf commands that are not needed to define the attributes of text that are supported:
|
|
private static Regex reg1 = new Regex(@"\\par\r\n(?!\\)");
|
|
private static Regex reg2 = new Regex(@"[\r\n]", RegexOptions.Singleline); // Strip Carriage Returns and Newlines
|
|
private static Regex reg3 = new Regex(@"^\{(.*)\}$", RegexOptions.Singleline); // Strip Opening and Closing Braces
|
|
private static Regex reg4 = new Regex(@"\{[^{]*?\}", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces
|
|
private static Regex reg5 = new Regex(@"\{[^{]*?\}", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces
|
|
private static Regex reg6 = new Regex(@"(\\[^' \\?\r\n\t]+)(?=\\)"); // add space after token if followed by token
|
|
private static Regex reg7 = new Regex(@"(\\[^' \\?\r\n\t]+ )"); // take backslash xyz and evaluates them
|
|
private static Regex reg8 = new Regex(@"(\\[^' \\?\r\n\t]+) (?=\\)"); // remove space between tokens
|
|
private static Regex reg9 = new Regex(@"(\\[^' \\?\r\n\t]+) (?=\r\n)"); // remove space before /r/n
|
|
private static Regex reg10 = new Regex(@"(\\[0-9a-z]+)$"); // end of text attribute needs a 'space'
|
|
private string RtfToDbText(string rtf)
|
|
{
|
|
// replace \{ & \} with (![ & (!] respectively and then redo at end. The curly braces
|
|
// are rtf so were getting removed and/or not handled correctly.
|
|
string retval = rtf.Replace(@"\{", @" (![");
|
|
retval = retval.Replace(@"\}", @" (!]");
|
|
|
|
// remove carriage return/newlines after \par commands (these are introduced by rtb
|
|
// for hard returns, goes into rtb as \par and comes out as \par\r\n):
|
|
retval = reg1.Replace(retval, "\\par ");
|
|
retval = retval.Replace("\\v0\r\n", "\\v0 "); // Replace Carriage Return and Newline after comment
|
|
retval = reg2.Replace(retval, ""); // Strip Carriage Returns and Newlines
|
|
retval = reg3.Replace(retval, "$1"); // Strip Opening and Closing Braces
|
|
retval = reg4.Replace(retval, ""); // Strip Clauses - remove anything from curly braces
|
|
retval = reg5.Replace(retval, ""); // Strip Clauses - remove anything from curly braces
|
|
retval = reg6.Replace(retval, "$1 "); // add space after token if followed by token
|
|
retval = reg7.Replace(retval, new MatchEvaluator(StaticReplaceRTFClause)); // take backslash xyz and evaluates them
|
|
retval = reg8.Replace(retval, "$1"); // remove space between tokens
|
|
retval = reg9.Replace(retval, "$1"); // remove space before /r/n
|
|
if (retval.EndsWith(@"{\par}")) retval = retval.Remove(retval.Length - 6, 6);
|
|
retval = reg10.Replace(retval, "$1 ");
|
|
// remove \r\n at end of string if the string has 2 or more characters
|
|
if (retval.EndsWith("\r\n")) retval = retval.Remove(retval.Length - 2, 2);
|
|
if (retval.Length == 0) return "";
|
|
if (retval.EndsWith(@"\v")) retval = retval.Remove(retval.Length - 2, 2);
|
|
retval = retval.Replace(@" (![", @"\{");
|
|
retval = retval.Replace(@" (!]", @"\}");
|
|
retval = retval.Replace("`",@"\xA0");
|
|
return retval;
|
|
}
|
|
private void btnBold_Click(object sender, EventArgs e)
|
|
{
|
|
RTBAPI.ToggleBold(!RTBAPI.IsBold(rtfBox), rtfBox, rtfBox.SelectionLength == 0 ? RTBAPI.RTBSelection.SCF_DEFAULT : RTBAPI.RTBSelection.SCF_SELECTION);
|
|
btnBold.Checked = RTBAPI.IsBold(rtfBox);
|
|
}
|
|
private void btnItalics_Click(object sender, EventArgs e)
|
|
{
|
|
RTBAPI.ToggleItalic(!RTBAPI.IsItalic(rtfBox), rtfBox, rtfBox.SelectionLength == 0 ? RTBAPI.RTBSelection.SCF_DEFAULT : RTBAPI.RTBSelection.SCF_SELECTION);
|
|
btnItalics.Checked = RTBAPI.IsItalic(rtfBox);
|
|
}
|
|
private void btnUnderline_Click(object sender, EventArgs e)
|
|
{
|
|
RTBAPI.ToggleUnderline(!RTBAPI.IsUnderline(rtfBox), rtfBox, rtfBox.SelectionLength == 0 ? RTBAPI.RTBSelection.SCF_DEFAULT : RTBAPI.RTBSelection.SCF_SELECTION);
|
|
btnUnderline.Checked = RTBAPI.IsUnderline(rtfBox);
|
|
}
|
|
private void btnSubscript_Click(object sender, EventArgs e)
|
|
{
|
|
RTBAPI.ToggleSubscript(!RTBAPI.IsSubScript(rtfBox), rtfBox, rtfBox.SelectionLength == 0 ? RTBAPI.RTBSelection.SCF_DEFAULT : RTBAPI.RTBSelection.SCF_SELECTION);
|
|
btnSubscript.Checked = RTBAPI.IsSubScript(rtfBox);
|
|
}
|
|
private void btnSuperscript_Click(object sender, EventArgs e)
|
|
{
|
|
RTBAPI.ToggleSuperscript(!RTBAPI.IsSuperScript(rtfBox), rtfBox, rtfBox.SelectionLength == 0 ? RTBAPI.RTBSelection.SCF_DEFAULT : RTBAPI.RTBSelection.SCF_SELECTION);
|
|
btnSuperscript.Checked = RTBAPI.IsSuperScript(rtfBox);
|
|
}
|
|
private void btnOK_Click(object sender, EventArgs e)
|
|
{
|
|
}
|
|
private void btnCancel_Click(object sender, EventArgs e)
|
|
{
|
|
}
|
|
private void rtfBox_SelectionChanged(object sender, EventArgs e)
|
|
{
|
|
btnBold.Checked = RTBAPI.IsBold(rtfBox);
|
|
btnItalics.Checked = RTBAPI.IsItalic(rtfBox);
|
|
btnUnderline.Checked = RTBAPI.IsUnderline(rtfBox);
|
|
}
|
|
private void btnHardspace_Click(object sender, EventArgs e)
|
|
{
|
|
AddText("`");
|
|
}
|
|
private void AddText(string str)
|
|
{
|
|
// See comments in AddRtf(string str) to explain the font style setting
|
|
RTBAPI.E_FontStyle fs = RTBAPI.GetFontStyle(rtfBox);
|
|
int positionStart = rtfBox.SelectionStart;
|
|
rtfBox.SelectedText = str;
|
|
int positionAfter = rtfBox.SelectionStart;
|
|
rtfBox.Select(positionStart, positionAfter - positionStart);
|
|
RTBAPI.SetFontStyle(rtfBox, fs);
|
|
rtfBox.Select(positionAfter, 0);
|
|
}
|
|
}
|
|
}
|
|
|