C2022-003-Change-Textboxes-to-RTF-3

This commit is contained in:
2025-11-26 14:31:59 -05:00
parent 52583a05f6
commit 9023cc76a0
18 changed files with 2615 additions and 1934 deletions

View File

@@ -200,6 +200,10 @@ using System.Xml.Schema;
using System.Text;
using RODBInterface;
using Org.Mentalis.Files;
using System.Windows;
using System.Text.RegularExpressions;
using Volian.Base.Library;
using System.Linq;
//using IniFileIO;
@@ -311,7 +315,17 @@ namespace ctlXMLEditLib
public string GetParentHTId { get { return parenthtid; } }
public void SetParentHTId(string id) { this.parenthtid = id; }
}
private RichTextBox _TextBoxFocus; // RO Editor add symbols C2025 - 003
public RichTextBox TextBoxFocus
{
get { return _TextBoxFocus; }
set
{
_TextBoxFocus = value;
// C2015-017 hide the label indicating user is working with a duplicate of an exiting RO
//lblDuplicateRO.Visible = value;
}
}
public ctlXMLEdit(VlnXmlElement myelem, XmlSchema myschema, ArrayList reqfields, ArrayList fldsWithApplic, string [] pckids)
{
// This call is required by the Windows.Forms Form Designer.
@@ -363,7 +377,7 @@ namespace ctlXMLEditLib
// set the required fields tag on those fields which are included in the passed
// in required fields list.
private void DoSet(TextBox hwnd, string msg)
private void DoSet(RichTextBox hwnd, string msg) // RO Editor add symbols C2025 - 003
{
if (hwnd == null) return;
if (hwnd.Tag != null)
@@ -397,10 +411,10 @@ namespace ctlXMLEditLib
o = myHT[field+"a"];
if (o != null) // set all combo types required, the checker
{
DoSet((TextBox)o,msg);
DoSet((TextBox)myHT[field+"b"],msg);
DoSet((TextBox)myHT[field+"c"],msg);
DoSet((TextBox)myHT[field+"d"],msg);
DoSet((RichTextBox)o,msg);
DoSet((RichTextBox)myHT[field+"b"],msg);
DoSet((RichTextBox)myHT[field+"c"],msg);
DoSet((RichTextBox)myHT[field+"d"],msg);
}
}
else if (o != null)
@@ -476,8 +490,11 @@ namespace ctlXMLEditLib
if (nd == null || nd.InnerText.Length==0) // B2024-004 use Parent value if Child text length is zero
GetDefaultParentValue(hwnd, node, str); // C2021-026 Parent/Child Field has no value so use parent's value
else
{
hwnd.Text = nd.InnerText; // set the field's text from XML
{
hwnd.Text = Regex.Replace(nd.InnerText, @"\\u([0-9]{1,4})\?", m => Convert.ToChar(int.Parse(m.Groups[1].Value)).ToString()); // set the field's text from XML
//hwnd.Text = nd.InnerText; // set the field's text from XML
Size size = hwnd.Size;
// check if this window has a button name as part of its tag. If
// so it's a combo type & the radio button/visibility may need to
@@ -585,13 +602,13 @@ namespace ctlXMLEditLib
{
dosaveflag=false;
mysavexml=false;
TextBox hwnd;
RichTextBox hwnd;
// first, clear out all of the text boxes.
foreach (string str in myHT.Keys)
{
object o = myHT[str];
hwnd = (TextBox) o;
hwnd = (RichTextBox) o;
hwnd.Text = "";
TextBoxAttrTag tag = (TextBoxAttrTag) hwnd.Tag;
RadioButton radio;
@@ -623,7 +640,7 @@ namespace ctlXMLEditLib
// to the parent's value, then clear the child's textbox so that nothing is saved
// to the database. This allow us to know that a specific value was not set
// for this Parent/Child child
private void RemovePCChildTextIfSameAsParent(XmlNode node, TextBox tb, string chldName)
private void RemovePCChildTextIfSameAsParent(XmlNode node, RichTextBox tb, string chldName)
{
XmlNode parentNode = null;
// if this is a child node get the parent's value
@@ -656,6 +673,9 @@ namespace ctlXMLEditLib
}
}
// SaveData saves the data in the element which had been sent to the control. Return
// true if success, false if fail.
// Note that the Parent and Child XML node variables below are not coding for Parent/Child Applicabily Fields
@@ -663,15 +683,56 @@ namespace ctlXMLEditLib
{
if (mysavexml)
{
TextBox hwnd;
RichTextBox hwnd;
TextBoxAttrTag tag;
string imgdate;
//go thru the hash table to get textboxes. Find the ones that have text.
foreach (string str in myHT.Keys)
foreach (string str in myHT.Keys) // RO Editor add symbols C2025 - 003
{
object o = myHT[str];
hwnd = (TextBox) o;
hwnd = (RichTextBox) o;
string result1 = "";
string Str = "";
char[] chrAry = hwnd.Text.ToCharArray();
foreach (int chr in chrAry)
{
if (chr > 166)
{
//Str = @"\u" + ((int)chr).ToString("X4");
//Str = @"\u" + ((int)chr); //.ToString("X4");
Str = $"\\u{(int)chr}?";
result1 = result1 + Str;
}
else
{
result1 = result1 + (char)chr;
}
}
var pattern = @"\\u([0-9]{1,4})\?"; // RO Editor add symbols C2025 - 003
var results = Regex.Matches(hwnd.Text, pattern);
string num1;
int num2;
foreach (Match result in results)
{
num1 = (result.Groups[1].Value);
string resultString = Regex.Match(num1, @"\d+").Value;
string resultString1 = string.Format("{0:x}", resultString);
num2 = int.Parse(resultString, System.Globalization.NumberStyles.HexNumber);
}
hwnd.Text = result1;
imgdate = null;
// if this is a required field and there is no text, put out an error
// message and get out of here.
@@ -788,6 +849,125 @@ namespace ctlXMLEditLib
return (false);
}
// RO Editor add symbols C2025 - 003
private static Regex sreg1 = new Regex(@"\\par\r\n(?!\\)");
private static Regex sreg2 = new Regex(@"[\r\n]", RegexOptions.Singleline); // Strip Carriage Returns and Newlines
private static Regex sreg3 = new Regex(@"^\{(.*)\}$", RegexOptions.Singleline); // Strip Opening and Closing Braces
private static Regex sreg4 = new Regex(@"\{[^{]*?\}", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces
private static Regex sreg5 = new Regex(@"\{[^{]*?\}", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces
private static Regex sreg6 = new Regex(@"(\\[^' \\?\r\n\t]+)(?=\\)"); // add space after token if followed by token
private static Regex sreg7 = new Regex(@"(\\[^ \\?\r\n\t]+ )"); // take backslash xyz and evaluates them
private static Regex sreg8 = new Regex(@"(\\[^ \\?\r\n\t]+) (?=\\)"); // remove space between tokens
private static Regex sreg9 = new Regex(@"(\\[^ \\?\r\n\t]+) (?=\r\n)"); // remove space before /r/n
public static string StaticStripRtfCommands(string rtf, bool fromTable)
{
// 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(@"\}", @" (!]");
// For hardspaces, the windows richtextbox does some 'quirky' things:
// A unicode representation of \u160? is sent INTO the rtb. Coming out,
// that \u160? was translated to a \~ (by the underlying windows rtb).
// Note that if the \~ is sent to the rtb, it is treated as a regular space,
// i.e. no longer a hardspace, and actually is converted to a regular space.
// SO, on the way out, convert any \~ to \u160?
retval = RTFConvertedSymbolsToUnicode(retval);
// 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 = sreg1.Replace(retval, "\\par ");
retval = retval.Replace("\\v0\r\n", "\\v0 "); // Replace Carriage Return and Newline after comment
retval = sreg2.Replace(retval, ""); // Strip Carriage Returns and Newlines
retval = sreg3.Replace(retval, "$1"); // Strip Opening and Closing Braces
retval = sreg4.Replace(retval, ""); // Strip Clauses - remove anything from curly braces
retval = sreg5.Replace(retval, ""); // Strip Clauses - remove anything from curly braces
retval = sreg6.Replace(retval, "$1 "); // add space after token if followed by token
retval = sreg7.Replace(retval, new MatchEvaluator(StaticReplaceRTFClause)); // take backslash xyz and evaluates them
retval = sreg8.Replace(retval, "$1"); // remove space between tokens
retval = sreg9.Replace(retval, "$1"); // remove space before /r/n
// remove \r\n at end of string if the string has 2 or more characters
if (retval.Length > 1 && retval.Substring(retval.Length - 2, 2) == "\r\n") retval = retval.Remove(retval.Length - 2, 2);
if (retval.Length == 0) return "";
if (retval.Length > 1 && retval.Substring(retval.Length - 2, 2) == @"\v") retval = retval.Remove(retval.Length - 2, 2);
retval = retval.Replace(@" (![", @"\{");
retval = retval.Replace(@" (!]", @"\}");
retval = retval.TrimEnd(' ');
// the indent character was translated in the richtextbox, change it back:
if (retval.IndexOf(@"\'05") > -1) retval = retval.Replace(@"\'05", "\x05");
return retval;
}
// RO Editor add symbols C2025 - 003
public static string RTFConvertedSymbolsToUnicode(string str)
{
string rtnStr = str;
// convert \~ to a hard spece. RTF is automatically converting \u160? to \~ but will then convert
// the \~ to a regular space!
rtnStr = rtnStr.Replace(@"\~", @"\u160?");
rtnStr = rtnStr.Replace(@"\'a0", @"\u160?");
// convert \'99 to \u8482? this is for the trade mark symbol. RTF is automatically
// converting the unicode \u8482? to \'99, but once this is done, PROMS StepRTB (edit windows) does not show it
rtnStr = rtnStr.Replace(@"\'99", @"\u8482?");
// convert \'ae to \u174? this is for the registered symbol. RTF converts the unicode character to \'ae
rtnStr = rtnStr.Replace(@"\'ae", @"\u174?");
// convert \'a9 to \u169? this is for the copyright symbol. RTF converts the unicode character to \'a9
rtnStr = rtnStr.Replace(@"\'a9", @"\u169?");
return rtnStr;
}
public static string StaticReplaceRTFClause(Match m)
{
try
{
string token = m.Groups[1].Value;
switch (token[1])
{
case '_': // B2020-100 New Non-Breaking Hyphen
return token.Trim();
case '\\':
return token;
case '\'': // Special Character
return token;
case '{': // look for escape for curly braces:
return token;
case '}':
return token;
case 'v': // save link hidden info
if (token == "\\viewkind4 ") break;
if (Regex.IsMatch(token, @"^\\v0? ?$"))
return token; // comment part of link
// If possible show what procedure was being processed.
break;
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
// C2020-001: only keep the font size rtf if in a table.
//if (_fromTable && Regex.IsMatch(token, @"^\\fs[-0-9]+ ?$")) return token; // font size
break;
case 'p':
if (Regex.IsMatch(token, @"^\\par ?$")) return "\r\n";
break;
}
}
catch (Exception ex)
{
Console.WriteLine("StaticReplaceRTFClause {0} - {1}", ex.GetType().Name, ex.Message);
}
return "";//Strip All
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
@@ -888,12 +1068,18 @@ namespace ctlXMLEditLib
if (getannot.IndexOf("Lines")>=0)
{
GraphicsHttextbox = mytextbox;
Size size = GraphicsHttextbox.Size;
mytextbox.LostFocus += new System.EventHandler(this.GraphicsHt_lostfocus);
//mytextbox.
}
if (getannot.IndexOf("Character")>=0)
{
GraphicsWdtextbox = mytextbox;
mytextbox.LostFocus += new System.EventHandler(this.GraphicsWd_lostfocus);
}
}
}
@@ -904,13 +1090,16 @@ namespace ctlXMLEditLib
{
// A Schema Type Restriction is used to define single or multi-line boxes.
XmlSchemaSimpleTypeRestriction myRestrictions = (XmlSchemaSimpleTypeRestriction) simpleType.Content;
if (myRestrictions.BaseTypeName.Name == "normalizedString")
if (myRestrictions.BaseTypeName.Name == "normalizedString")
{
mytextbox.Multiline = false;
mytextbox.Size = new Size(300, 40);
}
else
{
mytextbox.Multiline = true;
mytextbox.AcceptsTab = true;
mytextbox.Height = (int) mytextbox.Font.GetHeight() * 4;
mytextbox.Height = (int)mytextbox.Font.GetHeight() * 4;
mytextbox.ScrollBars = RichTextBoxScrollBars.Vertical;
zmtooltip.SetToolTip(mytextbox, "Press Shift F2 To Zoom");
}
@@ -993,6 +1182,7 @@ namespace ctlXMLEditLib
gbox.Controls.Add(radio);
RichTextBox tb = new RichTextBox();
Size size3 = tb.Size;
tb.Location = new Point(boxx+20, y+25);
if (!first) tb.Visible = false;
myHT.Add(element.Name,tb);
@@ -1001,7 +1191,7 @@ namespace ctlXMLEditLib
// the groupbox.
Controls.Add(tb);
tb.TextChanged += new System.EventHandler(this.textbox_change);
tb.GotFocus += new System.EventHandler(this.textbox_zoombtn);
tb.GotFocus += new System.EventHandler(this.currentTextBox);
tb.KeyDown += new KeyEventHandler(MyOnKeyDown);
gbox.Contains(tb);
// the following will set attributes on the text boxes such as maxlength, multiline, etc.
@@ -1146,6 +1336,7 @@ namespace ctlXMLEditLib
{
RichTextBox mytextbox;
mytextbox = new RichTextBox();
Size size3 = mytextbox.Size;
mytextbox.Location = new Point(screenx+indent, screeny);
string tFieldName = (pcChildIdx == 0) ? CvtUserFldToFld(element.Name) : CvtUserFldToFld(pcChildFldName);
mytextbox.Name = tFieldName;
@@ -1155,7 +1346,7 @@ namespace ctlXMLEditLib
screeny = screeny + 10;
mytextbox.TextChanged += new System.EventHandler(this.textbox_change);
mytextbox.Validating += new System.ComponentModel.CancelEventHandler(this.textbox_Validating);
mytextbox.GotFocus += new System.EventHandler(this.textbox_zoombtn);
mytextbox.GotFocus += new System.EventHandler(this.currentTextBox);
mytextbox.KeyDown += new KeyEventHandler(MyOnKeyDown);
if (pcChildIdx > 0)
{
@@ -1192,7 +1383,7 @@ namespace ctlXMLEditLib
// the following is needed to load the control.
private void ctlXMLEdit_Load(object sender, System.EventArgs e)
{
}
// when data in a text box has changed, flag it (except for when this
@@ -1264,7 +1455,7 @@ namespace ctlXMLEditLib
private void radiocheckchg(object sender, System.EventArgs e)
{
RadioButton btnsel = (RadioButton) sender;
TextBox assocbox;
RichTextBox assocbox;
string btntext, str;
if(dosaveflag)mysavexml = true;
@@ -1277,7 +1468,7 @@ namespace ctlXMLEditLib
str = btnsel.Name.Replace(btntext,"");
object o = myHT[str];
if (o == null) return;
assocbox = (TextBox) o;
assocbox = (RichTextBox) o;
// make the text box visible if checked, otherwise, invisible
assocbox.Visible = btnsel.Checked;
// C2021-026 show or hide the Parent/Child appicability group box
@@ -1420,13 +1611,15 @@ namespace ctlXMLEditLib
}
// save which box we're on, for zoom processing.
private void textbox_zoombtn(object sender, System.EventArgs e)
private void currentTextBox(object sender, System.EventArgs e)
{
RichTextBox textbox = (RichTextBox) sender;
if (textbox.Multiline == true || textbox == GraphicsFiletextbox)
zoomtextbox = textbox;
else
zoomtextbox = null;
TextBoxFocus = textbox;
}
private void textbox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
@@ -1932,5 +2125,35 @@ namespace ctlXMLEditLib
return curTZ.GetDaylightChanges(year);
}
}
public static class RichTextBoxExtensions // RO Editor add symbols C2025 - 003
{
public static void InsertSymbol(this RichTextBox richTextBox, int symbcode)
{
int position = richTextBox.SelectionStart;
string sym = string.Format(symbcode < 256 ? "\'{0:X2}" : @"\u{0}", symbcode);
string RtfPrefixForSymbols = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset2 FreeMono; } {\f1\fnil\fcharset0 FreeMono; } } {\colortbl;\red255\green0\blue0;\red0\green0\blue255; } \viewkind4\uc1\pard\fs24 \f1\fs24 ";
richTextBox.SelectedRtf = RtfPrefixForSymbols + sym + @"}";
richTextBox.SelectedRtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033\uc1 }";
richTextBox.Select(position, 1);
richTextBox.Select(position + 1, 0);
//TextPointer caretPos = rtb.CaretPosition;
//if (symbcode < 256)
// AddText(((char)symbcode).ToString());
//else
// AddSymbol(sym); // Adds font commands around symbol, needed for higher codes
}
}
public enum E_FontStyle : byte
{
FS_NONE = 0,
FS_BOLD = 0x01,
FS_UNDERLINE = 0x02,
FS_ITALIC = 0x04,
FS_SUPERSCRIPT = 0x08,
FS_SUBSCRIPT = 0x10
}
}