using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Windows.Forms; using System.Xml; using Uestc.Auto6.Dso.Core; using Uestc.Auto6.Dso.ComModel; using Uestc.Auto6.Dso.MathExt; using Uni_Trend.MSO7000X.UserControls; using Uestc.Auto6.Dso.Core.Tools; using System.Windows.Forms.VisualStyles; namespace Uestc.Auto6.Dso.U2 { public partial class CustomFormulaForm : FloatForm, IChnlView { private sealed record FuncInfo(String Id, String Symbol, String Expression, String ImageKey, String WeakTipWrapper); private readonly Dictionary _Functions = new(); private String _Formula; private MathCustomArg _CustomArg; public MathPrsnt Presenter { get; set; } IBadge IView.Presenter { get => Presenter; set => Presenter = (MathPrsnt)value; } public CustomFormulaForm() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); _CustomArg = (MathCustomArg)Presenter.GetOrMakeArg(MathType.Custom); if (LoadCustomFormula("")) { LvSelection.Items.Clear(); foreach (var item in _Functions) { var lvi = new ListViewItem(item.Key, item.Value.ImageKey) { ToolTipText = item.Value.WeakTipWrapper.Replace("\\n", Environment.NewLine) }; LvSelection.Items.Add(lvi); } } UpdateFormulaText(); //rtbxFormulaEditor.Focus(); //rtbxFormulaEditor.Select(rtbxFormulaEditor.Text.Length, 0); LvSelection.Focus(); //Presenter.SetFormula(MathType.Custom, _CustomArg.Formula); _CustomArg.Formula = _Formula; //_CustomArg.UpdateFormula(); //风格调整 Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControlRecursion(this); } private Boolean LoadCustomFormula(String path) { XmlReader reader; //Load CustomFormula.xml try { if (File.Exists(path)) reader = new XmlTextReader(path); else { Type type = typeof(CustomFormulaForm); Stream sm = type.Assembly.GetManifestResourceStream(type.Namespace + ".Resources.CustomFormula.xml"); reader = XmlReader.Create(sm); } reader.ReadToFollowing("ExpItems"); XmlReader subreader = reader.ReadSubtree(); while (subreader.Read()) { if (subreader.NodeType == XmlNodeType.Element && subreader.Name == "Item") _Functions.Add(subreader.GetAttribute("name"), new FuncInfo( subreader.GetAttribute("id"), subreader.GetAttribute("symbol"), subreader.GetAttribute("expression"), subreader.GetAttribute("image"), subreader.GetAttribute("info"))); } } catch { Logger.Error($"The load of CustomFormula.xml fails or its format is wrong!"); #if DEBUG throw; #else return false; #endif } return true; } private void UpdateFormulaText() { StringBuilder vsb = new(); StringBuilder expsb = new(); foreach (var item in _CustomArg.Token) { vsb.Append(_Functions[item].Symbol); expsb.Append(_Functions[item].Expression); } RtbxEditor.Text = vsb.ToString(); _Formula = expsb.ToString(); } private void BtnClear_Click(object sender, EventArgs e) { _CustomArg.Token.Clear(); _Formula = ""; TbxDescription.Text = ""; RtbxEditor.Text = ""; } private void BtnBackspace_Click(object sender, EventArgs e) { if (_CustomArg.Token.Count != 0) { _CustomArg.Token.RemoveAt(_CustomArg.Token.Count - 1); UpdateFormulaText(); } } private void LvSelection_Click(object sender, EventArgs e) { if (LvSelection.SelectedItems.Count > 0) { foreach (ListViewItem item in LvSelection.SelectedItems) { Int32 id = Int32.Parse(_Functions[item.Text].Id); //if it is function, add description. if (id > 20 && id < 60) TbxDescription.Text = _Functions[item.Text].WeakTipWrapper.Replace("\\n", Environment.NewLine); _CustomArg.Token.Add(item.Text); UpdateFormulaText(); } } } private void BtnCancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } private void BtnAccept_Click(object sender, EventArgs e) { var res = DynamicExecute.Evaluate("Custom", _Formula, out String errmsg); if (res) { this.DialogResult = DialogResult.OK; //Presenter.SetFormula(Presenter.CalcType, _Formula); //_CustomArg.UpdateFormula(); _CustomArg.Expression = RtbxEditor.Text; _CustomArg.Formula = _Formula; this.Close(); } else { WeakTip.Default.Write("Formula Editor", MsgTipId.InputFormatError); } } private void TbxDescription_MouseDown(object sender, MouseEventArgs e) { NativeMethods.HideCaret((sender as TextBox).Handle); LvSelection.Focus(); } private void RtbxEditor_MouseDown(object sender, MouseEventArgs e) { NativeMethods.HideCaret((sender as RichTextBox).Handle); LvSelection.Focus(); } public void UpdateView(string propertyName) { } protected override void OnFormClosed(FormClosedEventArgs e) { base.OnFormClosed(e); } } }