1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2005-2006 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok <pbartok@novell.com>
30 using System.Collections;
31 using System.ComponentModel;
33 using System.Drawing.Imaging;
36 using System.Runtime.InteropServices;
37 using RTF=System.Windows.Forms.RTF;
39 namespace System.Windows.Forms {
41 [ClassInterface (ClassInterfaceType.AutoDispatch)]
42 [Docking (DockingBehavior.Ask)]
44 [Designer ("System.Windows.Forms.Design.RichTextBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
46 public class RichTextBox : TextBoxBase {
47 #region Local Variables
48 internal bool auto_word_select;
49 internal int bullet_indent;
50 internal bool detect_urls;
51 private bool reuse_line; // Sometimes we are loading text with already available lines
52 internal int margin_right;
54 private StringBuilder rtf_line;
56 private RtfSectionStyle rtf_style; // Replaces individual style
57 // properties so we can revert
58 private Stack rtf_section_stack;
60 private RTF.TextMap rtf_text_map;
61 private int rtf_skip_count;
62 private int rtf_cursor_x;
63 private int rtf_cursor_y;
64 private int rtf_chars;
67 private bool enable_auto_drag_drop;
68 private RichTextBoxLanguageOptions language_option;
69 private bool rich_text_shortcuts_enabled;
70 private Color selection_back_color;
72 #endregion // Local Variables
74 #region Public Constructors
75 public RichTextBox() {
76 accepts_return = true;
78 auto_word_select = false;
80 base.MaxLength = Int32.MaxValue;
83 base.Multiline = true;
84 document.CRLFSize = 1;
85 shortcuts_enabled = true;
86 base.EnableLinks = true;
89 rtf_style = new RtfSectionStyle ();
90 rtf_section_stack = null;
92 scrollbars = RichTextBoxScrollBars.Both;
93 alignment = HorizontalAlignment.Left;
94 LostFocus += new EventHandler(RichTextBox_LostFocus);
95 GotFocus += new EventHandler(RichTextBox_GotFocus);
96 BackColor = ThemeEngine.Current.ColorWindow;
98 backcolor_set = false;
99 language_option = RichTextBoxLanguageOptions.AutoFontSizeAdjust;
100 rich_text_shortcuts_enabled = true;
101 selection_back_color = DefaultBackColor;
103 ForeColor = ThemeEngine.Current.ColorWindowText;
105 base.HScrolled += new EventHandler(RichTextBox_HScrolled);
106 base.VScrolled += new EventHandler(RichTextBox_VScrolled);
109 SetStyle (ControlStyles.StandardDoubleClick, false);
112 #endregion // Public Constructors
114 #region Private & Internal Methods
116 internal override void HandleLinkClicked (LinkRectangle link)
118 OnLinkClicked (new LinkClickedEventArgs (link.LinkTag.LinkText));
121 internal override Color ChangeBackColor (Color backColor)
123 if (backColor == Color.Empty) {
125 backcolor_set = false;
127 backColor = SystemColors.Window;
130 backColor = SystemColors.Window;
136 internal override void RaiseSelectionChanged()
138 OnSelectionChanged (EventArgs.Empty);
141 private void RichTextBox_LostFocus(object sender, EventArgs e) {
145 private void RichTextBox_GotFocus(object sender, EventArgs e) {
148 #endregion // Private & Internal Methods
150 #region Public Instance Properties
154 public override bool AllowDrop {
156 return base.AllowDrop;
160 base.AllowDrop = value;
164 [DefaultValue(false)]
166 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
167 [RefreshProperties (RefreshProperties.Repaint)]
168 [EditorBrowsable (EditorBrowsableState.Never)]
173 public override bool AutoSize {
179 base.AutoSize = value;
183 [MonoTODO ("Value not respected, always true")]
184 [DefaultValue(false)]
185 public bool AutoWordSelection {
186 get { return auto_word_select; }
187 set { auto_word_select = value; }
191 [EditorBrowsable(EditorBrowsableState.Never)]
192 public override System.Drawing.Image BackgroundImage {
193 get { return base.BackgroundImage; }
194 set { base.BackgroundImage = value; }
199 [EditorBrowsable (EditorBrowsableState.Never)]
200 public override ImageLayout BackgroundImageLayout {
201 get { return base.BackgroundImageLayout; }
202 set { base.BackgroundImageLayout = value; }
208 public int BulletIndent {
210 return bullet_indent;
214 bullet_indent = value;
219 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
220 public bool CanRedo {
222 return document.undo.CanRedo;
227 public bool DetectUrls {
228 get { return base.EnableLinks; }
229 set { base.EnableLinks = value; }
233 [MonoTODO ("Stub, does nothing")]
234 [DefaultValue (false)]
235 public bool EnableAutoDragDrop {
236 get { return enable_auto_drag_drop; }
237 set { enable_auto_drag_drop = value; }
241 public override Font Font {
252 if (PreferredHeight != Height) {
253 Height = PreferredHeight;
259 // Font changes always set the whole doc to that font
260 start = document.GetLine(1);
261 end = document.GetLine(document.Lines);
262 document.FormatText(start, 1, end, end.text.Length + 1, base.Font, Color.Empty, Color.Empty, FormatSpecified.Font);
267 public override Color ForeColor {
269 return base.ForeColor;
273 base.ForeColor = value;
278 [MonoTODO ("Stub, does nothing")]
280 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
281 public RichTextBoxLanguageOptions LanguageOption {
282 get { return language_option; }
283 set { language_option = value; }
287 [DefaultValue(Int32.MaxValue)]
288 public override int MaxLength {
289 get { return base.MaxLength; }
290 set { base.MaxLength = value; }
294 public override bool Multiline {
296 return base.Multiline;
300 base.Multiline = value;
305 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
306 public string RedoActionName {
308 return document.undo.RedoActionName;
313 [MonoTODO ("Stub, does nothing")]
315 [DefaultValue (true)]
316 [EditorBrowsable (EditorBrowsableState.Never)]
317 public bool RichTextShortcutsEnabled {
318 get { return rich_text_shortcuts_enabled; }
319 set { rich_text_shortcuts_enabled = value; }
325 [MonoTODO ("Stub, does nothing")]
326 [MonoInternalNote ("Teach TextControl.RecalculateLine to consider the right margin as well")]
327 public int RightMargin {
333 margin_right = value;
338 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
340 [RefreshProperties (RefreshProperties.All)]
349 start_line = document.GetLine(1);
350 end_line = document.GetLine(document.Lines);
351 return GenerateRTF(start_line, 0, end_line, end_line.text.Length).ToString();
358 data = new MemoryStream(Encoding.ASCII.GetBytes(value), false);
360 InsertRTFFromStream(data, 0, 1);
368 [DefaultValue(RichTextBoxScrollBars.Both)]
370 public RichTextBoxScrollBars ScrollBars {
376 if (!Enum.IsDefined (typeof (RichTextBoxScrollBars), value))
377 throw new InvalidEnumArgumentException ("value", (int) value,
378 typeof (RichTextBoxScrollBars));
380 if (value != scrollbars) {
382 CalculateDocument ();
389 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
390 public string SelectedRtf {
392 return GenerateRTF(document.selection_start.line, document.selection_start.pos, document.selection_end.line, document.selection_end.pos).ToString();
404 if (document.selection_visible) {
405 document.ReplaceSelection("", false);
408 sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
410 data = new MemoryStream(Encoding.ASCII.GetBytes(value), false);
411 int cursor_x = document.selection_start.pos;
412 int cursor_y = document.selection_start.line.line_no;
414 // If we have an empty just added line, tell our code to re-use it
415 // Hackish, but works without touching the heart of the buggy parser.
416 if (cursor_x == 0 && document.GetLine (cursor_y).text.Length == 0)
419 InsertRTFFromStream(data, cursor_x, cursor_y, out x, out y, out chars);
422 document.CharIndexToLineTag(sel_start + chars + (y - document.selection_start.line.line_no) * 2, out line, out tag, out sel_start);
423 document.SetSelection(line, sel_start);
424 document.PositionCaret(line, sel_start);
425 document.DisplayCaret();
427 OnTextChanged(EventArgs.Empty);
433 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
434 public override string SelectedText {
436 return base.SelectedText;
440 base.SelectedText = value;
445 [DefaultValue(HorizontalAlignment.Left)]
446 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
447 public HorizontalAlignment SelectionAlignment {
449 HorizontalAlignment align;
454 start = document.ParagraphStart(document.selection_start.line);
455 align = start.alignment;
457 end = document.ParagraphEnd(document.selection_end.line);
462 if (line.alignment != align) {
463 return HorizontalAlignment.Left;
469 line = document.GetLine(line.line_no + 1);
480 start = document.ParagraphStart(document.selection_start.line);
482 end = document.ParagraphEnd(document.selection_end.line);
487 line.alignment = value;
492 line = document.GetLine(line.line_no + 1);
494 this.CalculateDocument();
499 [MonoTODO ("Stub, does nothing")]
501 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
502 public Color SelectionBackColor {
503 get { return selection_back_color; }
504 set { selection_back_color = value; }
509 [DefaultValue(false)]
510 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
511 [MonoTODO ("Stub, does nothing")]
512 public bool SelectionBullet {
523 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
524 [MonoTODO ("Stub, does nothing")]
525 public int SelectionCharOffset {
535 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
536 public Color SelectionColor {
543 if (selection_length > 0) {
544 start = document.selection_start.line.FindTag (document.selection_start.pos + 1);
545 end = document.selection_start.line.FindTag (document.selection_end.pos);
547 start = document.selection_start.line.FindTag (document.selection_start.pos);
554 while (tag != null) {
556 if (!color.Equals (tag.Color))
562 tag = document.NextTag (tag);
569 if (value == Color.Empty)
570 value = DefaultForeColor;
575 sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
576 sel_end = document.LineTagToCharIndex(document.selection_end.line, document.selection_end.pos);
578 document.FormatText (document.selection_start.line, document.selection_start.pos + 1,
579 document.selection_end.line, document.selection_end.pos + 1, null,
580 value, Color.Empty, FormatSpecified.Color);
582 document.CharIndexToLineTag(sel_start, out document.selection_start.line, out document.selection_start.tag, out document.selection_start.pos);
583 document.CharIndexToLineTag(sel_end, out document.selection_end.line, out document.selection_end.tag, out document.selection_end.pos);
585 document.UpdateView(document.selection_start.line, 0);
587 //Re-Align the caret in case its changed size or position
588 //probably not necessary here
589 document.AlignCaret(false);
594 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
595 public Font SelectionFont {
602 if (selection_length > 0) {
603 start = document.selection_start.line.FindTag (document.selection_start.pos + 1);
604 end = document.selection_start.line.FindTag (document.selection_end.pos);
606 start = document.selection_start.line.FindTag (document.selection_start.pos);
612 if (selection_length > 1) {
614 while (tag != null) {
616 if (!font.Equals(tag.Font))
622 tag = document.NextTag (tag);
633 sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
634 sel_end = document.LineTagToCharIndex(document.selection_end.line, document.selection_end.pos);
636 document.FormatText (document.selection_start.line, document.selection_start.pos + 1,
637 document.selection_end.line, document.selection_end.pos + 1, value,
638 Color.Empty, Color.Empty, FormatSpecified.Font);
640 document.CharIndexToLineTag(sel_start, out document.selection_start.line, out document.selection_start.tag, out document.selection_start.pos);
641 document.CharIndexToLineTag(sel_end, out document.selection_end.line, out document.selection_end.tag, out document.selection_end.pos);
643 document.UpdateView(document.selection_start.line, 0);
644 //Re-Align the caret in case its changed size or position
645 Document.AlignCaret (false);
652 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
653 [MonoTODO ("Stub, does nothing")]
654 public int SelectionHangingIndent {
665 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
666 [MonoTODO ("Stub, does nothing")]
667 public int SelectionIndent {
677 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
678 public override int SelectionLength {
680 return base.SelectionLength;
684 base.SelectionLength = value;
689 [DefaultValue(false)]
690 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
691 [MonoTODO ("Stub, does nothing")]
692 public bool SelectionProtected {
703 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
704 [MonoTODO ("Stub, does nothing")]
705 public int SelectionRightIndent {
715 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
716 [MonoTODO ("Stub, does nothing")]
717 public int[] SelectionTabs {
727 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
728 public RichTextBoxSelectionTypes SelectionType {
730 if (document.selection_start == document.selection_end) {
731 return RichTextBoxSelectionTypes.Empty;
735 if (SelectedText.Length > 1) {
736 return RichTextBoxSelectionTypes.MultiChar | RichTextBoxSelectionTypes.Text;
739 return RichTextBoxSelectionTypes.Text;
743 [DefaultValue(false)]
744 [MonoTODO ("Stub, does nothing")]
745 public bool ShowSelectionMargin {
756 [RefreshProperties (RefreshProperties.All)]
758 public override string Text {
769 public override int TextLength {
771 return base.TextLength;
776 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
777 public string UndoActionName {
779 return document.undo.UndoActionName;
785 public float ZoomFactor {
794 #endregion // Public Instance Properties
796 #region Protected Instance Properties
797 protected override CreateParams CreateParams {
799 return base.CreateParams;
803 protected override Size DefaultSize {
805 return new Size(100, 96);
808 #endregion // Protected Instance Properties
810 #region Public Instance Methods
811 public bool CanPaste(DataFormats.Format clipFormat) {
812 if ((clipFormat.Name == DataFormats.Rtf) ||
813 (clipFormat.Name == DataFormats.Text) ||
814 (clipFormat.Name == DataFormats.UnicodeText)) {
820 public int Find(char[] characterSet) {
821 return Find(characterSet, -1, -1);
824 public int Find(char[] characterSet, int start) {
825 return Find(characterSet, start, -1);
828 public int Find(char[] characterSet, int start, int end) {
829 Document.Marker start_mark;
830 Document.Marker end_mark;
831 Document.Marker result;
834 document.GetMarker(out start_mark, true);
840 start_mark = new Document.Marker();
842 document.CharIndexToLineTag(start, out line, out tag, out pos);
843 start_mark.line = line;
844 start_mark.tag = tag;
845 start_mark.pos = pos;
849 document.GetMarker(out end_mark, false);
855 end_mark = new Document.Marker();
857 document.CharIndexToLineTag(end, out line, out tag, out pos);
858 end_mark.line = line;
863 if (document.FindChars(characterSet, start_mark, end_mark, out result)) {
864 return document.LineTagToCharIndex(result.line, result.pos);
870 public int Find(string str) {
871 return Find(str, -1, -1, RichTextBoxFinds.None);
874 public int Find(string str, int start, int end, RichTextBoxFinds options) {
875 Document.Marker start_mark;
876 Document.Marker end_mark;
877 Document.Marker result;
880 document.GetMarker(out start_mark, true);
886 start_mark = new Document.Marker();
888 document.CharIndexToLineTag(start, out line, out tag, out pos);
890 start_mark.line = line;
891 start_mark.tag = tag;
892 start_mark.pos = pos;
896 document.GetMarker(out end_mark, false);
902 end_mark = new Document.Marker();
904 document.CharIndexToLineTag(end, out line, out tag, out pos);
906 end_mark.line = line;
911 if (document.Find(str, start_mark, end_mark, out result, options)) {
912 return document.LineTagToCharIndex(result.line, result.pos);
918 public int Find(string str, int start, RichTextBoxFinds options) {
919 return Find(str, start, -1, options);
922 public int Find(string str, RichTextBoxFinds options) {
923 return Find(str, -1, -1, options);
928 public char GetCharFromPosition(Point pt) {
932 PointToTagPos(pt, out tag, out pos);
934 if (pos >= tag.Line.text.Length) {
938 return tag.Line.text[pos];
941 internal override char GetCharFromPositionInternal (Point p)
946 PointToTagPos (p, out tag, out pos);
948 if (pos >= tag.Line.text.Length)
951 return tag.Line.text[pos];
959 int GetCharIndexFromPosition(Point pt) {
963 PointToTagPos(pt, out tag, out pos);
965 return document.LineTagToCharIndex(tag.Line, pos);
972 int GetLineFromCharIndex(int index) {
977 document.CharIndexToLineTag(index, out line, out tag, out pos);
979 return line.LineNo - 1;
986 Point GetPositionFromCharIndex(int index) {
991 document.CharIndexToLineTag(index, out line, out tag, out pos);
992 return new Point(line.X + (int)line.widths[pos] + document.OffsetX - document.ViewPortX,
993 line.Y + document.OffsetY - document.ViewPortY);
996 public void LoadFile(System.IO.Stream data, RichTextBoxStreamType fileType) {
1000 // FIXME - ignoring unicode
1001 if (fileType == RichTextBoxStreamType.PlainText) {
1006 sb = new StringBuilder ((int) data.Length);
1007 buffer = new char [1024];
1009 throw new IOException("Not enough memory to load document");
1012 StreamReader sr = new StreamReader (data, Encoding.Default, true);
1013 int charsRead = sr.Read (buffer, 0, buffer.Length);
1014 while (charsRead > 0) {
1015 sb.Append (buffer, 0, charsRead);
1016 charsRead = sr.Read (buffer, 0, buffer.Length);
1019 // Remove the EOF converted to an extra EOL by the StreamReader
1020 if (sb.Length > 0 && sb [sb.Length - 1] == '\n')
1021 sb.Remove (sb.Length - 1, 1);
1023 base.Text = sb.ToString();
1027 InsertRTFFromStream(data, 0, 1);
1029 document.PositionCaret (document.GetLine (1), 0);
1030 document.SetSelectionToCaret (true);
1034 public void LoadFile(string path) {
1035 LoadFile (path, RichTextBoxStreamType.RichText);
1038 public void LoadFile(string path, RichTextBoxStreamType fileType) {
1045 data = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1024);
1047 LoadFile(data, fileType);
1050 catch (Exception ex) {
1051 throw new IOException("Could not open file " + path, ex);
1061 public void Paste(DataFormats.Format clipFormat) {
1062 base.Paste(Clipboard.GetDataObject(), clipFormat, false);
1067 if (document.undo.Redo ())
1068 OnTextChanged (EventArgs.Empty);
1071 public void SaveFile(Stream data, RichTextBoxStreamType fileType) {
1077 if (fileType == RichTextBoxStreamType.UnicodePlainText) {
1078 encoding = Encoding.Unicode;
1080 encoding = Encoding.ASCII;
1084 case RichTextBoxStreamType.PlainText:
1085 case RichTextBoxStreamType.TextTextOleObjs:
1086 case RichTextBoxStreamType.UnicodePlainText: {
1088 bytes = encoding.GetBytes(document.Root.text.ToString());
1089 data.Write(bytes, 0, bytes.Length);
1093 for (i = 1; i < document.Lines; i++) {
1094 // Normalize the new lines to the system ones
1095 string line_text = document.GetLine (i).TextWithoutEnding () + Environment.NewLine;
1096 bytes = encoding.GetBytes(line_text);
1097 data.Write(bytes, 0, bytes.Length);
1099 bytes = encoding.GetBytes(document.GetLine(document.Lines).text.ToString());
1100 data.Write(bytes, 0, bytes.Length);
1105 // If we're here we're saving RTF
1112 start_line = document.GetLine(1);
1113 end_line = document.GetLine(document.Lines);
1114 rtf = GenerateRTF(start_line, 0, end_line, end_line.text.Length);
1116 bytes = new Byte[4096];
1118 // Let's chunk it so we don't use up all memory...
1119 for (i = 0; i < total; i += 1024) {
1120 if ((i + 1024) < total) {
1121 current = encoding.GetBytes(rtf.ToString(i, 1024), 0, 1024, bytes, 0);
1123 current = total - i;
1124 current = encoding.GetBytes(rtf.ToString(i, current), 0, current, bytes, 0);
1126 data.Write(bytes, 0, current);
1130 public void SaveFile(string path) {
1131 if (path.EndsWith(".rtf")) {
1132 SaveFile(path, RichTextBoxStreamType.RichText);
1134 SaveFile(path, RichTextBoxStreamType.PlainText);
1138 public void SaveFile(string path, RichTextBoxStreamType fileType) {
1144 data = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, 1024, false);
1145 SaveFile(data, fileType);
1149 // throw new IOException("Could not write document to file " + path);
1160 [EditorBrowsable (EditorBrowsableState.Never)]
1161 public new void DrawToBitmap (Bitmap bitmap, Rectangle targetBounds)
1163 Graphics dc = Graphics.FromImage (bitmap);
1165 Draw (dc, targetBounds);
1169 #endregion // Public Instance Methods
1171 #region Protected Instance Methods
1172 protected virtual object CreateRichEditOleCallback() {
1173 throw new NotImplementedException();
1176 protected override void OnBackColorChanged(EventArgs e) {
1177 base.OnBackColorChanged (e);
1180 protected virtual void OnContentsResized(ContentsResizedEventArgs e) {
1181 ContentsResizedEventHandler eh = (ContentsResizedEventHandler)(Events [ContentsResizedEvent]);
1186 protected override void OnContextMenuChanged(EventArgs e) {
1187 base.OnContextMenuChanged (e);
1190 protected override void OnHandleCreated(EventArgs e) {
1191 base.OnHandleCreated (e);
1194 protected override void OnHandleDestroyed(EventArgs e) {
1195 base.OnHandleDestroyed (e);
1198 protected virtual void OnHScroll(EventArgs e) {
1199 EventHandler eh = (EventHandler)(Events [HScrollEvent]);
1204 [MonoTODO ("Stub, never called")]
1205 protected virtual void OnImeChange(EventArgs e) {
1206 EventHandler eh = (EventHandler)(Events [ImeChangeEvent]);
1211 protected virtual void OnLinkClicked(LinkClickedEventArgs e) {
1212 LinkClickedEventHandler eh = (LinkClickedEventHandler)(Events [LinkClickedEvent]);
1217 protected virtual void OnProtected(EventArgs e) {
1218 EventHandler eh = (EventHandler)(Events [ProtectedEvent]);
1223 protected override void OnRightToLeftChanged(EventArgs e) {
1224 base.OnRightToLeftChanged (e);
1227 protected virtual void OnSelectionChanged(EventArgs e) {
1228 EventHandler eh = (EventHandler)(Events [SelectionChangedEvent]);
1234 protected override void OnSystemColorsChanged(EventArgs e) {
1235 base.OnSystemColorsChanged (e);
1238 protected override void OnTextChanged(EventArgs e) {
1239 base.OnTextChanged (e);
1243 protected virtual void OnVScroll(EventArgs e) {
1244 EventHandler eh = (EventHandler)(Events [VScrollEvent]);
1249 protected override void WndProc(ref Message m) {
1250 base.WndProc (ref m);
1254 protected override bool ProcessCmdKey (ref Message m, Keys keyData)
1256 return base.ProcessCmdKey (ref m, keyData);
1259 #endregion // Protected Instance Methods
1262 static object ContentsResizedEvent = new object ();
1263 static object HScrollEvent = new object ();
1264 static object ImeChangeEvent = new object ();
1265 static object LinkClickedEvent = new object ();
1266 static object ProtectedEvent = new object ();
1267 static object SelectionChangedEvent = new object ();
1268 static object VScrollEvent = new object ();
1271 [EditorBrowsable(EditorBrowsableState.Never)]
1272 public new event EventHandler BackgroundImageChanged {
1273 add { base.BackgroundImageChanged += value; }
1274 remove { base.BackgroundImageChanged -= value; }
1279 [EditorBrowsable (EditorBrowsableState.Never)]
1280 public new event EventHandler BackgroundImageLayoutChanged {
1281 add { base.BackgroundImageLayoutChanged += value; }
1282 remove { base.BackgroundImageLayoutChanged -= value; }
1286 public event ContentsResizedEventHandler ContentsResized {
1287 add { Events.AddHandler (ContentsResizedEvent, value); }
1288 remove { Events.RemoveHandler (ContentsResizedEvent, value); }
1293 [EditorBrowsable(EditorBrowsableState.Never)]
1294 public new event EventHandler DoubleClick {
1295 add { base.DoubleClick += value; }
1296 remove { base.DoubleClick -= value; }
1302 [EditorBrowsable(EditorBrowsableState.Never)]
1304 public new event DragEventHandler DragDrop {
1305 add { base.DragDrop += value; }
1306 remove { base.DragDrop -= value; }
1311 [EditorBrowsable(EditorBrowsableState.Never)]
1313 public new event DragEventHandler DragEnter {
1314 add { base.DragEnter += value; }
1315 remove { base.DragEnter -= value; }
1319 [EditorBrowsable(EditorBrowsableState.Never)]
1320 public new event EventHandler DragLeave {
1321 add { base.DragLeave += value; }
1322 remove { base.DragLeave -= value; }
1327 [EditorBrowsable(EditorBrowsableState.Never)]
1328 public new event DragEventHandler DragOver {
1329 add { base.DragOver += value; }
1330 remove { base.DragOver -= value; }
1335 [EditorBrowsable(EditorBrowsableState.Never)]
1336 public new event GiveFeedbackEventHandler GiveFeedback {
1337 add { base.GiveFeedback += value; }
1338 remove { base.GiveFeedback -= value; }
1341 public event EventHandler HScroll {
1342 add { Events.AddHandler (HScrollEvent, value); }
1343 remove { Events.RemoveHandler (HScrollEvent, value); }
1346 public event EventHandler ImeChange {
1347 add { Events.AddHandler (ImeChangeEvent, value); }
1348 remove { Events.RemoveHandler (ImeChangeEvent, value); }
1351 public event LinkClickedEventHandler LinkClicked {
1352 add { Events.AddHandler (LinkClickedEvent, value); }
1353 remove { Events.RemoveHandler (LinkClickedEvent, value); }
1356 public event EventHandler Protected {
1357 add { Events.AddHandler (ProtectedEvent, value); }
1358 remove { Events.RemoveHandler (ProtectedEvent, value); }
1362 [EditorBrowsable(EditorBrowsableState.Never)]
1363 public new event QueryContinueDragEventHandler QueryContinueDrag {
1364 add { base.QueryContinueDrag += value; }
1365 remove { base.QueryContinueDrag -= value; }
1368 [MonoTODO ("Event never raised")]
1369 public event EventHandler SelectionChanged {
1370 add { Events.AddHandler (SelectionChangedEvent, value); }
1371 remove { Events.RemoveHandler (SelectionChangedEvent, value); }
1374 public event EventHandler VScroll {
1375 add { Events.AddHandler (VScrollEvent, value); }
1376 remove { Events.RemoveHandler (VScrollEvent, value); }
1378 #endregion // Events
1380 #region Private Methods
1382 internal override void SelectWord ()
1384 document.ExpandSelection(CaretSelection.Word, false);
1387 private class RtfSectionStyle : ICloneable {
1388 internal Color rtf_color;
1389 internal RTF.Font rtf_rtffont;
1390 internal int rtf_rtffont_size;
1391 internal FontStyle rtf_rtfstyle;
1392 internal HorizontalAlignment rtf_rtfalign;
1393 internal int rtf_par_line_left_indent;
1394 internal bool rtf_visible;
1395 internal int rtf_skip_width;
1397 public object Clone ()
1399 RtfSectionStyle new_style = new RtfSectionStyle ();
1401 new_style.rtf_color = rtf_color;
1402 new_style.rtf_par_line_left_indent = rtf_par_line_left_indent;
1403 new_style.rtf_rtfalign = rtf_rtfalign;
1404 new_style.rtf_rtffont = rtf_rtffont;
1405 new_style.rtf_rtffont_size = rtf_rtffont_size;
1406 new_style.rtf_rtfstyle = rtf_rtfstyle;
1407 new_style.rtf_visible = rtf_visible;
1408 new_style.rtf_skip_width = rtf_skip_width;
1414 // To allow us to keep track of the sections and revert formatting
1415 // as we go in and out of sections of the document.
1416 private void HandleGroup (RTF.RTF rtf)
1418 //start group - save the current formatting on to a stack
1419 //end group - go back to the formatting at the current group
1420 if (rtf_section_stack == null) {
1421 rtf_section_stack = new Stack ();
1424 if (rtf.Major == RTF.Major.BeginGroup) {
1425 rtf_section_stack.Push (rtf_style.Clone ());
1426 //spec specifies resetting unicode ignore at begin group as an attempt at error
1429 } else if (rtf.Major == RTF.Major.EndGroup) {
1430 if (rtf_section_stack.Count > 0) {
1431 FlushText (rtf, false);
1433 rtf_style = (RtfSectionStyle) rtf_section_stack.Pop ();
1438 [MonoInternalNote ("Add QuadJust support for justified alignment")]
1439 private void HandleControl(RTF.RTF rtf) {
1441 case RTF.Major.Unicode: {
1443 case RTF.Minor.UnicodeCharBytes: {
1444 rtf_style.rtf_skip_width = rtf.Param;
1448 case RTF.Minor.UnicodeChar: {
1449 FlushText (rtf, false);
1450 rtf_skip_count += rtf_style.rtf_skip_width;
1451 rtf_line.Append((char)rtf.Param);
1458 case RTF.Major.Destination: {
1459 // Console.Write("[Got Destination control {0}]", rtf.Minor);
1464 case RTF.Major.PictAttr:
1465 if (rtf.Picture != null && rtf.Picture.IsValid ()) {
1466 Line line = document.GetLine (rtf_cursor_y);
1467 document.InsertPicture (line, 0, rtf.Picture);
1470 FlushText (rtf, true);
1475 case RTF.Major.CharAttr: {
1477 case RTF.Minor.ForeColor: {
1478 System.Windows.Forms.RTF.Color color;
1480 color = System.Windows.Forms.RTF.Color.GetColor(rtf, rtf.Param);
1482 if (color != null) {
1483 FlushText(rtf, false);
1484 if (color.Red == -1 && color.Green == -1 && color.Blue == -1) {
1485 this.rtf_style.rtf_color = ForeColor;
1487 this.rtf_style.rtf_color = Color.FromArgb(color.Red, color.Green, color.Blue);
1489 FlushText (rtf, false);
1494 case RTF.Minor.FontSize: {
1495 FlushText(rtf, false);
1496 this.rtf_style.rtf_rtffont_size = rtf.Param / 2;
1500 case RTF.Minor.FontNum: {
1501 System.Windows.Forms.RTF.Font font;
1503 font = System.Windows.Forms.RTF.Font.GetFont(rtf, rtf.Param);
1505 FlushText(rtf, false);
1506 this.rtf_style.rtf_rtffont = font;
1511 case RTF.Minor.Plain: {
1512 FlushText(rtf, false);
1513 rtf_style.rtf_rtfstyle = FontStyle.Regular;
1517 case RTF.Minor.Bold: {
1518 FlushText(rtf, false);
1519 if (rtf.Param == RTF.RTF.NoParam) {
1520 rtf_style.rtf_rtfstyle |= FontStyle.Bold;
1522 rtf_style.rtf_rtfstyle &= ~FontStyle.Bold;
1527 case RTF.Minor.Italic: {
1528 FlushText(rtf, false);
1529 if (rtf.Param == RTF.RTF.NoParam) {
1530 rtf_style.rtf_rtfstyle |= FontStyle.Italic;
1532 rtf_style.rtf_rtfstyle &= ~FontStyle.Italic;
1537 case RTF.Minor.StrikeThru: {
1538 FlushText(rtf, false);
1539 if (rtf.Param == RTF.RTF.NoParam) {
1540 rtf_style.rtf_rtfstyle |= FontStyle.Strikeout;
1542 rtf_style.rtf_rtfstyle &= ~FontStyle.Strikeout;
1547 case RTF.Minor.Underline: {
1548 FlushText(rtf, false);
1549 if (rtf.Param == RTF.RTF.NoParam) {
1550 rtf_style.rtf_rtfstyle |= FontStyle.Underline;
1552 rtf_style.rtf_rtfstyle = rtf_style.rtf_rtfstyle & ~FontStyle.Underline;
1557 case RTF.Minor.Invisible: {
1558 FlushText (rtf, false);
1559 rtf_style.rtf_visible = false;
1563 case RTF.Minor.NoUnderline: {
1564 FlushText(rtf, false);
1565 rtf_style.rtf_rtfstyle &= ~FontStyle.Underline;
1572 case RTF.Major.ParAttr: {
1573 switch (rtf.Minor) {
1575 case RTF.Minor.ParDef:
1576 FlushText (rtf, false);
1577 rtf_style.rtf_par_line_left_indent = 0;
1578 rtf_style.rtf_rtfalign = HorizontalAlignment.Left;
1581 case RTF.Minor.LeftIndent:
1582 rtf_style.rtf_par_line_left_indent = (int) (((float) rtf.Param / 1440.0F) * CreateGraphics ().DpiX + 0.5F);
1585 case RTF.Minor.QuadCenter:
1586 FlushText (rtf, false);
1587 rtf_style.rtf_rtfalign = HorizontalAlignment.Center;
1590 case RTF.Minor.QuadJust:
1591 FlushText (rtf, false);
1592 rtf_style.rtf_rtfalign = HorizontalAlignment.Center;
1595 case RTF.Minor.QuadLeft:
1596 FlushText (rtf, false);
1597 rtf_style.rtf_rtfalign = HorizontalAlignment.Left;
1600 case RTF.Minor.QuadRight:
1601 FlushText (rtf, false);
1602 rtf_style.rtf_rtfalign = HorizontalAlignment.Right;
1608 case RTF.Major.SpecialChar: {
1609 //Console.Write("[Got SpecialChar control {0}]", rtf.Minor);
1616 private void SpecialChar(RTF.RTF rtf) {
1618 case RTF.Minor.Page:
1619 case RTF.Minor.Sect:
1621 case RTF.Minor.Line:
1622 case RTF.Minor.Par: {
1623 FlushText(rtf, true);
1627 case RTF.Minor.Cell: {
1632 case RTF.Minor.NoBrkSpace: {
1637 case RTF.Minor.Tab: {
1638 rtf_line.Append ("\t");
1639 // FlushText (rtf, false);
1643 case RTF.Minor.NoReqHyphen:
1644 case RTF.Minor.NoBrkHyphen: {
1645 rtf_line.Append ("-");
1646 // FlushText (rtf, false);
1650 case RTF.Minor.Bullet: {
1651 Console.WriteLine("*");
1655 case RTF.Minor.WidowCtrl:
1658 case RTF.Minor.EmDash: {
1659 rtf_line.Append ("\u2014");
1663 case RTF.Minor.EnDash: {
1664 rtf_line.Append ("\u2013");
1668 case RTF.Minor.LQuote: {
1669 Console.Write("\u2018");
1673 case RTF.Minor.RQuote: {
1674 Console.Write("\u2019");
1678 case RTF.Minor.LDblQuote: {
1679 Console.Write("\u201C");
1683 case RTF.Minor.RDblQuote: {
1684 Console.Write("\u201D");
1689 // Console.WriteLine ("skipped special char: {0}", rtf.Minor);
1696 private void HandleText(RTF.RTF rtf) {
1697 string str = rtf.EncodedText;
1699 //todo - simplistically skips characters, should skip bytes?
1700 if (rtf_skip_count > 0 && str.Length > 0) {
1701 int iToRemove = Math.Min (rtf_skip_count, str.Length);
1703 str = str.Substring (iToRemove);
1704 rtf_skip_count-=iToRemove;
1708 if ((RTF.StandardCharCode)rtf.Minor != RTF.StandardCharCode.nothing) {
1709 rtf_line.Append(rtf_text_map[(RTF.StandardCharCode)rtf.Minor]);
1711 if ((int)rtf.Major > 31 && (int)rtf.Major < 128) {
1712 rtf_line.Append((char)rtf.Major);
1714 //rtf_line.Append((char)rtf.Major);
1715 Console.Write("[Literal:0x{0:X2}]", (int)rtf.Major);
1720 if (rtf_style.rtf_visible)
1721 rtf_line.Append (str);
1724 private void FlushText(RTF.RTF rtf, bool newline) {
1728 length = rtf_line.Length;
1729 if (!newline && (length == 0)) {
1733 if (rtf_style.rtf_rtffont == null) {
1734 // First font in table is default
1735 rtf_style.rtf_rtffont = System.Windows.Forms.RTF.Font.GetFont (rtf, 0);
1738 font = new Font (rtf_style.rtf_rtffont.Name, rtf_style.rtf_rtffont_size, rtf_style.rtf_rtfstyle);
1740 if (rtf_style.rtf_color == Color.Empty) {
1741 System.Windows.Forms.RTF.Color color;
1743 // First color in table is default
1744 color = System.Windows.Forms.RTF.Color.GetColor (rtf, 0);
1746 if ((color == null) || (color.Red == -1 && color.Green == -1 && color.Blue == -1)) {
1747 rtf_style.rtf_color = ForeColor;
1749 rtf_style.rtf_color = Color.FromArgb (color.Red, color.Green, color.Blue);
1754 rtf_chars += rtf_line.Length;
1756 // Try to re-use if we are told so - this usually happens when we are inserting a flow of rtf text
1757 // with an already alive line.
1758 if (rtf_cursor_x == 0 && !reuse_line) {
1759 if (newline && rtf_line.ToString ().EndsWith (Environment.NewLine) == false)
1760 rtf_line.Append (Environment.NewLine);
1762 document.Add (rtf_cursor_y, rtf_line.ToString (), rtf_style.rtf_rtfalign, font, rtf_style.rtf_color,
1763 newline ? LineEnding.Rich : LineEnding.Wrap);
1764 if (rtf_style.rtf_par_line_left_indent != 0) {
1765 Line line = document.GetLine (rtf_cursor_y);
1766 line.indent = rtf_style.rtf_par_line_left_indent;
1771 line = document.GetLine (rtf_cursor_y);
1772 line.indent = rtf_style.rtf_par_line_left_indent;
1773 if (rtf_line.Length > 0) {
1774 document.InsertString (line, rtf_cursor_x, rtf_line.ToString ());
1775 document.FormatText (line, rtf_cursor_x + 1, line, rtf_cursor_x + 1 + length,
1776 font, rtf_style.rtf_color, Color.Empty,
1777 FormatSpecified.Font | FormatSpecified.Color);
1780 line = document.GetLine (rtf_cursor_y);
1781 line.ending = LineEnding.Rich;
1783 if (line.Text.EndsWith (Environment.NewLine) == false)
1784 line.Text += Environment.NewLine;
1787 reuse_line = false; // sanity assignment - in this case we have already re-used one line.
1794 rtf_cursor_x += length;
1796 rtf_line.Length = 0; // Empty line
1799 private void InsertRTFFromStream(Stream data, int cursor_x, int cursor_y) {
1804 InsertRTFFromStream(data, cursor_x, cursor_y, out x, out y, out chars);
1807 private void InsertRTFFromStream(Stream data, int cursor_x, int cursor_y, out int to_x, out int to_y, out int chars) {
1810 rtf = new RTF.RTF(data);
1813 rtf.ClassCallback[RTF.TokenClass.Text] = new RTF.ClassDelegate(HandleText);
1814 rtf.ClassCallback[RTF.TokenClass.Control] = new RTF.ClassDelegate(HandleControl);
1815 rtf.ClassCallback[RTF.TokenClass.Group] = new RTF.ClassDelegate(HandleGroup);
1818 rtf_line = new StringBuilder();
1819 rtf_style.rtf_color = Color.Empty;
1820 rtf_style.rtf_rtffont_size = (int)this.Font.Size;
1821 rtf_style.rtf_rtfalign = HorizontalAlignment.Left;
1822 rtf_style.rtf_rtfstyle = FontStyle.Regular;
1823 rtf_style.rtf_rtffont = null;
1824 rtf_style.rtf_visible = true;
1825 rtf_style.rtf_skip_width = 1;
1826 rtf_cursor_x = cursor_x;
1827 rtf_cursor_y = cursor_y;
1829 rtf.DefaultFont(this.Font.Name);
1831 rtf_text_map = new RTF.TextMap();
1832 RTF.TextMap.SetupStandardTable(rtf_text_map.Table);
1834 document.SuspendRecalc ();
1837 rtf.Read(); // That's it
1838 FlushText(rtf, false);
1843 catch (RTF.RTFException e) {
1847 // Seems to be plain text or broken RTF
1848 Console.WriteLine("RTF Parsing failure: {0}", e.Message);
1851 to_x = rtf_cursor_x;
1852 to_y = rtf_cursor_y;
1855 // clear the section stack if it was used
1856 if (rtf_section_stack != null)
1857 rtf_section_stack.Clear();
1859 document.RecalculateDocument(CreateGraphicsInternal(), cursor_y, document.Lines, false);
1860 document.ResumeRecalc (true);
1862 document.Invalidate (document.GetLine(cursor_y), 0, document.GetLine(document.Lines), -1);
1865 private void RichTextBox_HScrolled(object sender, EventArgs e) {
1869 private void RichTextBox_VScrolled(object sender, EventArgs e) {
1873 private void PointToTagPos(Point pt, out LineTag tag, out int pos) {
1878 if (p.X >= document.ViewPortWidth) {
1879 p.X = document.ViewPortWidth - 1;
1880 } else if (p.X < 0) {
1884 if (p.Y >= document.ViewPortHeight) {
1885 p.Y = document.ViewPortHeight - 1;
1886 } else if (p.Y < 0) {
1890 tag = document.FindCursor(p.X + document.ViewPortX, p.Y + document.ViewPortY, out pos);
1893 private void EmitRTFFontProperties(StringBuilder rtf, int prev_index, int font_index, Font prev_font, Font font) {
1894 if (prev_index != font_index) {
1895 rtf.Append(String.Format("\\f{0}", font_index)); // Font table entry
1898 if ((prev_font == null) || (prev_font.Size != font.Size)) {
1899 rtf.Append(String.Format("\\fs{0}", (int)(font.Size * 2))); // Font size
1902 if ((prev_font == null) || (font.Bold != prev_font.Bold)) {
1906 if (prev_font != null) {
1912 if ((prev_font == null) || (font.Italic != prev_font.Italic)) {
1916 if (prev_font != null) {
1922 if ((prev_font == null) || (font.Strikeout != prev_font.Strikeout)) {
1923 if (font.Strikeout) {
1924 rtf.Append("\\strike");
1926 if (prev_font != null) {
1927 rtf.Append("\\strike0");
1932 if ((prev_font == null) || (font.Underline != prev_font.Underline)) {
1933 if (font.Underline) {
1936 if (prev_font != null) {
1937 rtf.Append("\\ul0");
1943 [MonoInternalNote ("Emit unicode and other special characters properly")]
1944 private void EmitRTFText(StringBuilder rtf, string text) {
1948 // start_pos and end_pos are 0-based
1949 private StringBuilder GenerateRTF(Line start_line, int start_pos, Line end_line, int end_pos) {
1963 sb = new StringBuilder();
1964 fonts = new ArrayList(10);
1965 colors = new ArrayList(10);
1967 // Two runs, first we parse to determine tables;
1968 // and unlike most of our processing here we work on tags
1971 line_no = start_line.line_no;
1974 // Add default font and color; to optimize document content we don't
1975 // use this.Font and this.ForeColor but the font/color from the first tag
1976 tag = LineTag.FindTag(start_line, pos);
1979 fonts.Add(font.Name);
1982 while (line_no <= end_line.line_no) {
1983 line = document.GetLine(line_no);
1984 tag = LineTag.FindTag(line, pos);
1986 if (line_no != end_line.line_no) {
1987 line_len = line.text.Length;
1992 while (pos < line_len) {
1993 if (tag.Font.Name != font.Name) {
1995 if (!fonts.Contains(font.Name)) {
1996 fonts.Add(font.Name);
2000 if (tag.Color != color) {
2002 if (!colors.Contains(color)) {
2007 pos = tag.Start + tag.Length - 1;
2014 // We have the tables, emit the header
2015 sb.Append("{\\rtf1\\ansi");
2016 sb.Append("\\ansicpg1252"); // FIXME - is this correct?
2019 sb.Append(String.Format("\\deff{0}", fonts.IndexOf(this.Font.Name)));
2022 sb.Append("\\deflang1033" + Environment.NewLine); // FIXME - always 1033?
2024 // Emit the font table
2025 sb.Append("{\\fonttbl");
2026 for (i = 0; i < fonts.Count; i++) {
2027 sb.Append(String.Format("{{\\f{0}", i)); // {Font
2028 sb.Append("\\fnil"); // Family
2029 sb.Append("\\fcharset0 "); // Charset ANSI<space>
2030 sb.Append((string)fonts[i]); // Font name
2031 sb.Append(";}"); // }
2034 sb.Append(Environment.NewLine);
2036 // Emit the color table (if needed)
2037 if ((colors.Count > 1) || ((((Color)colors[0]).R != this.ForeColor.R) || (((Color)colors[0]).G != this.ForeColor.G) || (((Color)colors[0]).B != this.ForeColor.B))) {
2038 sb.Append("{\\colortbl "); // Header and NO! default color
2039 for (i = 0; i < colors.Count; i++) {
2040 sb.Append(String.Format("\\red{0}", ((Color)colors[i]).R));
2041 sb.Append(String.Format("\\green{0}", ((Color)colors[i]).G));
2042 sb.Append(String.Format("\\blue{0}", ((Color)colors[i]).B));
2046 sb.Append(Environment.NewLine);
2049 sb.Append("{\\*\\generator Mono RichTextBox;}");
2050 // Emit initial paragraph settings
2051 tag = LineTag.FindTag(start_line, start_pos);
2052 sb.Append("\\pard"); // Reset to default paragraph properties
2053 EmitRTFFontProperties(sb, -1, fonts.IndexOf(tag.Font.Name), null, tag.Font); // Font properties
2054 sb.Append(" "); // Space separator
2057 color = (Color)colors[0];
2059 line_no = start_line.line_no;
2062 while (line_no <= end_line.line_no) {
2063 line = document.GetLine(line_no);
2064 tag = LineTag.FindTag(line, pos);
2066 if (line_no != end_line.line_no) {
2067 line_len = line.text.Length;
2072 while (pos < line_len) {
2075 if (tag.Font != font) {
2076 EmitRTFFontProperties(sb, fonts.IndexOf(font.Name), fonts.IndexOf(tag.Font.Name), font, tag.Font);
2080 if (tag.Color != color) {
2082 sb.Append(String.Format("\\cf{0}", colors.IndexOf(color)));
2084 if (length != sb.Length) {
2085 sb.Append(" "); // Emit space to separate keywords from text
2088 // Emit the string itself
2089 if (line_no != end_line.line_no) {
2090 EmitRTFText(sb, tag.Line.text.ToString(pos, tag.Start + tag.Length - pos - 1));
2092 if (end_pos < (tag.Start + tag.Length - 1)) {
2093 // Emit partial tag only, end_pos is inside this tag
2094 EmitRTFText(sb, tag.Line.text.ToString(pos, end_pos - pos));
2096 EmitRTFText(sb, tag.Line.text.ToString(pos, tag.Start + tag.Length - pos - 1));
2100 pos = tag.Start + tag.Length - 1;
2103 if (pos >= line.text.Length) {
2104 if (line.ending != LineEnding.Wrap) {
2106 sb.Append(Environment.NewLine);
2114 sb.Append(Environment.NewLine);
2118 #endregion // Private Methods