// perform syntax coloring void UpdateSyntaxColoring(C1TextRange range) { // remove old coloring _rangeStyles.RemoveRange(range); var input = range.Text; // highlight the matches foreach (Match m in Regex.Matches(input, tagPattern)) { // select whole tag, make it dark blue _rangeStyles.Add(new C1RangeStyle(GetRangeAtTextOffset(range.Start, m), brDarkBlue)); // select tag name, make it dark red var tagName = m.Groups["tagName"]; _rangeStyles.Add(new C1RangeStyle(GetRangeAtTextOffset(range.Start, tagName), brDarkRed)); // select attribute names, make them light red var attGroup = m.Groups["attName"]; if (attGroup != null) { var atts = attGroup.Captures; for (int i = 0; i < atts.Count; i++) { var att = atts[i]; _rangeStyles.Add(new C1RangeStyle(GetRangeAtTextOffset(range.Start, att), brLightRed)); } } } }
/// <summary> /// Inserts page break to current selection /// </summary> internal void InsertPageBreak() { C1TextRange range = _editor.Selection; range.Start.MoveTo(range.End); range.XmlText = "<br style=\"page-break-before: always\"/>"; }
// perform search and replace using Selection properties void replaceUsingSelection_Click(object sender, EventArgs e) { MessageBox.Show("Replacing 'work' with 'labor' using Selection."); // re-load document this.c1Editor1.LoadXml(Properties.Resources.tesla, null); // strings to search and replace string search = "work"; string replace = "labor"; // do the work int count = 0; int previousStart = 0; C1TextRange range = this.c1Editor1.CreateRange(); for (int start = 0; ; start += search.Length) { // get text (notice new line handling) string txt = this.c1Editor1.Text; txt = txt.Replace("\r\n", "\n"); // find text, break when done start = txt.IndexOf(search, start, StringComparison.OrdinalIgnoreCase); if (start < 0) { break; } // select match // recommended way (better performance): move range relative to its current position range.Move(start - previousStart, search.Length); range.Select(); // not recommended way (slower): using the Select method //this.c1Editor1.Select(start, search.Length); // tell user we're about to replace string msg = string.Format("About to replace instance {0}.", ++count); if (MessageBox.Show(msg, "Replace", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { break; } // second part of the code moving range range.Text = replace; range.ApplyTag("strong"); previousStart = start; // second part of the code using the Select method //this.c1Editor1.Selection.Text = replace; //this.c1Editor1.Selection.ApplyTag("strong"); } // done MessageBox.Show(string.Format("Done, {0} instances found.", count)); }
/// <summary> /// Get the MarkerStyle of the selection. /// </summary> /// <param name="range"></param> /// <returns></returns> docs.TextMarkerStyle?GetMarkerStyle(C1TextRange range) { var lists = range.Lists.ToList(); docs.TextMarkerStyle?marker = lists.Any() ? new docs.TextMarkerStyle?(lists.First().MarkerStyle) : null; foreach (var list in lists) { if (marker != list.MarkerStyle) { return(null); } } return(marker); }
//------------------------------------------------------------------------------------- #region ** implementation void UpdateSyntax() { // get text string text = c1Editor1.Text; // remove formatting C1TextRange rng = c1Editor1.CreateRange(0, text.Length); rng.RemoveTag("span"); // apply all syntax descriptors foreach (SyntaxDescriptor sd in GetSyntaxDescriptors()) { // initialize int start = 0; rng.Start.Move(MoveUnit.Character, -50000000); rng.End.MoveTo(rng.Start); // if there's a group called 'match', use it; // otherwise, use the first one int gIndex = sd.Regex.GroupNumberFromName("match"); if (gIndex < 0) { gIndex = 0; } // find all matches for this descriptor for (Match match = sd.Regex.Match(text); match.Success; match = match.NextMatch()) { // apply descriptor style for this match Group g = match.Groups[gIndex]; foreach (Capture c in g.Captures) { rng.Move(c.Index - start, c.Length); // avoid nested spans (e.g. reserved words in comments, etc) if (rng.Node.ParentNode.Name != "span") { rng.ApplyClass(sd.ClassName, C1StyleType.Character); } start = c.Index; } } } }
// select an XmlNode in the document void selectXmlNode_Click(object sender, EventArgs e) { MessageBox.Show("Selecting 6th paragraph using Nodes."); // re-load document c1Editor1.LoadXml(Properties.Resources.tesla, null); // find 6th <p> node in document XmlDocument doc = c1Editor1.Document; XmlNodeList nodes = SelectNodes(doc, "/html/body/p"); if (nodes.Count > 5) { XmlNode node = nodes[5]; C1TextRange range = c1Editor1.CreateRange(); range.MoveTo(node); range.Select(); } }
// perform syntax coloring void UpdateSyntaxColoring() { C1TextRange range = c1RichTextBox1.Document.ContentRange; // reset coloring _rangeStyles = new C1RangeStyleCollection(); c1RichTextBox1.StyleOverrides.Clear(); c1RichTextBox1.StyleOverrides.Add(_rangeStyles); var input = range.Text; if (Mode == SyntaxMode.Xaml) { // highlight the matches foreach (Match m in Regex.Matches(input, tagPattern)) { // select whole tag, make it dark blue _rangeStyles.Add(new C1RangeStyle(GetRangeAtTextOffset(range.Start, m), brDarkBlue)); // select tag name, make it dark red var tagName = m.Groups["tagName"]; _rangeStyles.Add(new C1RangeStyle(GetRangeAtTextOffset(range.Start, tagName), brDarkRed)); // select attribute names, make them light red var attGroup = m.Groups["attName"]; if (attGroup != null) { var atts = attGroup.Captures; for (int i = 0; i < atts.Count; i++) { var att = atts[i]; _rangeStyles.Add(new C1RangeStyle(GetRangeAtTextOffset(range.Start, att), brLightRed)); } } } } else if (Mode == SyntaxMode.CSharp) { // highlight reserved words first foreach (string keyword in reservedWords) { foreach (Match m in Regex.Matches(input, keyword + " ")) { // select reserved word, make it blue _rangeStyles.Add(new C1RangeStyle(GetRangeAtTextOffset(range.Start, m), brBlue)); } } // highlight strings foreach (Match m in Regex.Matches(input, stringPattern)) { // select string, make it red _rangeStyles.Add(new C1RangeStyle(GetRangeAtTextOffset(range.Start, m), brDarkRed)); } // highlight comments foreach (Match m in Regex.Matches(input, commentPattern)) { // select comment, make it green _rangeStyles.Add(new C1RangeStyle(GetRangeAtTextOffset(range.Start, m), brGreen)); } } }
/// <summary> /// Get the MarkerStyle of the selection. /// </summary> /// <param name="range"></param> /// <returns></returns> docs.TextMarkerStyle? GetMarkerStyle(C1TextRange range) { var lists = range.Lists.ToList(); docs.TextMarkerStyle? marker = lists.Any() ? new docs.TextMarkerStyle?(lists.First().MarkerStyle) : null; foreach (var list in lists) { if (marker != list.MarkerStyle) return null; } return marker; }