private void CopyAsRTF() { TextStyle[] styles = this.Document.Parser.Language.Styles; this.Document.ParseAll(true); int r1 = Selection.LogicalBounds.FirstRow; int r2 = Selection.LogicalBounds.LastRow; int c1 = Selection.LogicalBounds.FirstColumn; int c2 = Selection.LogicalBounds.LastColumn; StringBuilder sb = new StringBuilder(); sb.Append(@"{\rtf1\ansi\ansicpg1252\deff0\deflang1053{\fonttbl{\f0\fmodern\fprq1\fcharset0 " + this.FontName + @";}}"); sb.Append(@"{\colortbl ;"); foreach (TextStyle ts in styles) { sb.AppendFormat("\\red{0}\\green{1}\\blue{2};", ts.ForeColor.R, ts.ForeColor.G, ts.ForeColor.B); sb.AppendFormat("\\red{0}\\green{1}\\blue{2};", ts.BackColor.R, ts.BackColor.G, ts.BackColor.B); } sb.Append(@";}"); sb.Append(@"\viewkind4\uc1\pard\f0\fs20"); bool Done = false; for (int i = r1; i <= r2; i++) { Row row = this.Document[i]; foreach (Word w in row) { if (i == r1 && w.Column + w.Text.Length < c1) continue; bool IsFirst = (i == r1 && w.Column <= c1 && w.Column + w.Text.Length > c1); bool IsLast = (i == r2 && w.Column < c2 && w.Column + w.Text.Length > c2); if (w.Type == WordType.xtWord && w.Style != null) { int clrindex = Array.IndexOf(styles, w.Style); clrindex *= 2; clrindex ++; sb.Append("{\\cf" + clrindex.ToString()); if (!w.Style.Transparent) { sb.Append("\\highlight" + (clrindex + 1).ToString()); } sb.Append(" "); } if (w.Style != null) { if (w.Style.Bold) sb.Append(@"\b "); if (w.Style.Underline) sb.Append(@"\ul "); if (w.Style.Italic) sb.Append(@"\i "); } string wordtext = w.Text; if (IsLast) wordtext = wordtext.Substring(0, c2 - w.Column); if (IsFirst) wordtext = wordtext.Substring(c1 - w.Column); wordtext = wordtext.Replace(@"\", @"\\").Replace(@"}", @"\}").Replace(@"{", @"\{"); sb.Append(wordtext); if (w.Style != null) { if (w.Style.Bold) sb.Append(@"\b0 "); if (w.Style.Underline) sb.Append(@"\ul0 "); if (w.Style.Italic) sb.Append(@"\i0 "); } if (w.Type == WordType.xtWord && w.Style != null) { sb.Append("}"); } if (IsLast) { Done = true; break; } } if (Done) break; sb.Append(@"\par"); } DataObject da; da = new DataObject(); da.SetData(DataFormats.Rtf, sb.ToString()); string s = this.Selection.Text; da.SetData(DataFormats.Text, s); Clipboard.SetDataObject(da); CopyEventArgs ea = new CopyEventArgs(); ea.Text = s; OnClipboardUpdated(ea); }
private void OnClipboardUpdated(CopyEventArgs e) { if (ClipboardUpdated != null) ClipboardUpdated(this, e); }
private void View_ClipboardUpdated(object sender, CopyEventArgs e) { this.OnClipboardUpdated(e); }
private void CopyText() { //no freaking vs.net copy empty selection if (!Selection.IsValid) return; if (this._CodeEditor.CopyAsRTF) { this.CopyAsRTF(); } else { //try //{ try { if (this.Selection != null) { string t = Selection.Text; Clipboard.SetDataObject(t, true); CopyEventArgs ea = new CopyEventArgs(); ea.Text = t; OnClipboardUpdated(ea); } } catch { } //} //catch //{ // try // { // string t = Selection.Text; // Clipboard.SetDataObject(t, true); // CopyEventArgs ea = new CopyEventArgs(); // ea.Text = t; // OnClipboardUpdated(ea); // } // catch // { // } //} } }
protected virtual void OnClipboardUpdated(CopyEventArgs e) { if (ClipboardUpdated != null) ClipboardUpdated(this, e); }