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 // The RFT parser by default, when finds our x cursor in 0, it thinks if needs to
415 // add a new line; but in *this* scenario the line is already created, so force it to reuse it.
416 // Hackish, but works without touching the heart of the buggy parser.
420 InsertRTFFromStream(data, cursor_x, cursor_y, out x, out y, out chars);
423 int nl_length = document.LineEndingLength (XplatUI.RunningOnUnix ? LineEnding.Rich : LineEnding.Hard);
424 document.CharIndexToLineTag(sel_start + chars + (y - document.selection_start.line.line_no) * nl_length,
425 out line, out tag, out sel_start);
426 if (sel_start >= line.text.Length)
427 sel_start = line.text.Length -1;
429 document.SetSelection(line, sel_start);
430 document.PositionCaret(line, sel_start);
431 document.DisplayCaret();
433 OnTextChanged(EventArgs.Empty);
439 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
440 public override string SelectedText {
442 return base.SelectedText;
446 base.SelectedText = value;
451 [DefaultValue(HorizontalAlignment.Left)]
452 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
453 public HorizontalAlignment SelectionAlignment {
455 HorizontalAlignment align;
460 start = document.ParagraphStart(document.selection_start.line);
461 align = start.alignment;
463 end = document.ParagraphEnd(document.selection_end.line);
468 if (line.alignment != align) {
469 return HorizontalAlignment.Left;
475 line = document.GetLine(line.line_no + 1);
486 start = document.ParagraphStart(document.selection_start.line);
488 end = document.ParagraphEnd(document.selection_end.line);
493 line.alignment = value;
498 line = document.GetLine(line.line_no + 1);
500 this.CalculateDocument();
505 [MonoTODO ("Stub, does nothing")]
507 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
508 public Color SelectionBackColor {
509 get { return selection_back_color; }
510 set { selection_back_color = value; }
515 [DefaultValue(false)]
516 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
517 [MonoTODO ("Stub, does nothing")]
518 public bool SelectionBullet {
529 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
530 [MonoTODO ("Stub, does nothing")]
531 public int SelectionCharOffset {
541 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
542 public Color SelectionColor {
549 if (selection_length > 0) {
550 start = document.selection_start.line.FindTag (document.selection_start.pos + 1);
551 end = document.selection_start.line.FindTag (document.selection_end.pos);
553 start = document.selection_start.line.FindTag (document.selection_start.pos);
560 while (tag != null) {
562 if (!color.Equals (tag.Color))
568 tag = document.NextTag (tag);
575 if (value == Color.Empty)
576 value = DefaultForeColor;
581 sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
582 sel_end = document.LineTagToCharIndex(document.selection_end.line, document.selection_end.pos);
584 document.FormatText (document.selection_start.line, document.selection_start.pos + 1,
585 document.selection_end.line, document.selection_end.pos + 1, null,
586 value, Color.Empty, FormatSpecified.Color);
588 document.CharIndexToLineTag(sel_start, out document.selection_start.line, out document.selection_start.tag, out document.selection_start.pos);
589 document.CharIndexToLineTag(sel_end, out document.selection_end.line, out document.selection_end.tag, out document.selection_end.pos);
591 document.UpdateView(document.selection_start.line, 0);
593 //Re-Align the caret in case its changed size or position
594 //probably not necessary here
595 document.AlignCaret(false);
600 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
601 public Font SelectionFont {
608 if (selection_length > 0) {
609 start = document.selection_start.line.FindTag (document.selection_start.pos + 1);
610 end = document.selection_start.line.FindTag (document.selection_end.pos);
612 start = document.selection_start.line.FindTag (document.selection_start.pos);
618 if (selection_length > 1) {
620 while (tag != null) {
622 if (!font.Equals(tag.Font))
628 tag = document.NextTag (tag);
639 sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
640 sel_end = document.LineTagToCharIndex(document.selection_end.line, document.selection_end.pos);
642 document.FormatText (document.selection_start.line, document.selection_start.pos + 1,
643 document.selection_end.line, document.selection_end.pos + 1, value,
644 Color.Empty, Color.Empty, FormatSpecified.Font);
646 document.CharIndexToLineTag(sel_start, out document.selection_start.line, out document.selection_start.tag, out document.selection_start.pos);
647 document.CharIndexToLineTag(sel_end, out document.selection_end.line, out document.selection_end.tag, out document.selection_end.pos);
649 document.UpdateView(document.selection_start.line, 0);
650 //Re-Align the caret in case its changed size or position
651 Document.AlignCaret (false);
658 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
659 [MonoTODO ("Stub, does nothing")]
660 public int SelectionHangingIndent {
671 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
672 [MonoTODO ("Stub, does nothing")]
673 public int SelectionIndent {
683 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
684 public override int SelectionLength {
686 return base.SelectionLength;
690 base.SelectionLength = value;
695 [DefaultValue(false)]
696 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
697 [MonoTODO ("Stub, does nothing")]
698 public bool SelectionProtected {
709 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
710 [MonoTODO ("Stub, does nothing")]
711 public int SelectionRightIndent {
721 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
722 [MonoTODO ("Stub, does nothing")]
723 public int[] SelectionTabs {
733 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
734 public RichTextBoxSelectionTypes SelectionType {
736 if (document.selection_start == document.selection_end) {
737 return RichTextBoxSelectionTypes.Empty;
741 if (SelectedText.Length > 1) {
742 return RichTextBoxSelectionTypes.MultiChar | RichTextBoxSelectionTypes.Text;
745 return RichTextBoxSelectionTypes.Text;
749 [DefaultValue(false)]
750 [MonoTODO ("Stub, does nothing")]
751 public bool ShowSelectionMargin {
762 [RefreshProperties (RefreshProperties.All)]
764 public override string Text {
775 public override int TextLength {
777 return base.TextLength;
782 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
783 public string UndoActionName {
785 return document.undo.UndoActionName;
791 public float ZoomFactor {
800 #endregion // Public Instance Properties
802 #region Protected Instance Properties
803 protected override CreateParams CreateParams {
805 return base.CreateParams;
809 protected override Size DefaultSize {
811 return new Size(100, 96);
814 #endregion // Protected Instance Properties
816 #region Public Instance Methods
817 public bool CanPaste(DataFormats.Format clipFormat) {
818 if ((clipFormat.Name == DataFormats.Rtf) ||
819 (clipFormat.Name == DataFormats.Text) ||
820 (clipFormat.Name == DataFormats.UnicodeText)) {
826 public int Find(char[] characterSet) {
827 return Find(characterSet, -1, -1);
830 public int Find(char[] characterSet, int start) {
831 return Find(characterSet, start, -1);
834 public int Find(char[] characterSet, int start, int end) {
835 Document.Marker start_mark;
836 Document.Marker end_mark;
837 Document.Marker result;
840 document.GetMarker(out start_mark, true);
846 start_mark = new Document.Marker();
848 document.CharIndexToLineTag(start, out line, out tag, out pos);
849 start_mark.line = line;
850 start_mark.tag = tag;
851 start_mark.pos = pos;
855 document.GetMarker(out end_mark, false);
861 end_mark = new Document.Marker();
863 document.CharIndexToLineTag(end, out line, out tag, out pos);
864 end_mark.line = line;
869 if (document.FindChars(characterSet, start_mark, end_mark, out result)) {
870 return document.LineTagToCharIndex(result.line, result.pos);
876 public int Find(string str) {
877 return Find(str, -1, -1, RichTextBoxFinds.None);
880 public int Find(string str, int start, int end, RichTextBoxFinds options) {
881 Document.Marker start_mark;
882 Document.Marker end_mark;
883 Document.Marker result;
886 document.GetMarker(out start_mark, true);
892 start_mark = new Document.Marker();
894 document.CharIndexToLineTag(start, out line, out tag, out pos);
896 start_mark.line = line;
897 start_mark.tag = tag;
898 start_mark.pos = pos;
902 document.GetMarker(out end_mark, false);
908 end_mark = new Document.Marker();
910 document.CharIndexToLineTag(end, out line, out tag, out pos);
912 end_mark.line = line;
917 if (document.Find(str, start_mark, end_mark, out result, options)) {
918 return document.LineTagToCharIndex(result.line, result.pos);
924 public int Find(string str, int start, RichTextBoxFinds options) {
925 return Find(str, start, -1, options);
928 public int Find(string str, RichTextBoxFinds options) {
929 return Find(str, -1, -1, options);
934 public char GetCharFromPosition(Point pt) {
938 PointToTagPos(pt, out tag, out pos);
940 if (pos >= tag.Line.text.Length) {
944 return tag.Line.text[pos];
947 internal override char GetCharFromPositionInternal (Point p)
952 PointToTagPos (p, out tag, out pos);
954 if (pos >= tag.Line.text.Length)
957 return tag.Line.text[pos];
965 int GetCharIndexFromPosition(Point pt) {
969 PointToTagPos(pt, out tag, out pos);
971 return document.LineTagToCharIndex(tag.Line, pos);
978 int GetLineFromCharIndex(int index) {
983 document.CharIndexToLineTag(index, out line, out tag, out pos);
985 return line.LineNo - 1;
992 Point GetPositionFromCharIndex(int index) {
997 document.CharIndexToLineTag(index, out line, out tag, out pos);
998 return new Point(line.X + (int)line.widths[pos] + document.OffsetX - document.ViewPortX,
999 line.Y + document.OffsetY - document.ViewPortY);
1002 public void LoadFile(System.IO.Stream data, RichTextBoxStreamType fileType) {
1006 // FIXME - ignoring unicode
1007 if (fileType == RichTextBoxStreamType.PlainText) {
1012 sb = new StringBuilder ((int) data.Length);
1013 buffer = new char [1024];
1015 throw new IOException("Not enough memory to load document");
1018 StreamReader sr = new StreamReader (data, Encoding.Default, true);
1019 int charsRead = sr.Read (buffer, 0, buffer.Length);
1020 while (charsRead > 0) {
1021 sb.Append (buffer, 0, charsRead);
1022 charsRead = sr.Read (buffer, 0, buffer.Length);
1025 // Remove the EOF converted to an extra EOL by the StreamReader
1026 if (sb.Length > 0 && sb [sb.Length - 1] == '\n')
1027 sb.Remove (sb.Length - 1, 1);
1029 base.Text = sb.ToString();
1033 InsertRTFFromStream(data, 0, 1);
1035 document.PositionCaret (document.GetLine (1), 0);
1036 document.SetSelectionToCaret (true);
1040 public void LoadFile(string path) {
1041 LoadFile (path, RichTextBoxStreamType.RichText);
1044 public void LoadFile(string path, RichTextBoxStreamType fileType) {
1051 data = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1024);
1053 LoadFile(data, fileType);
1056 catch (Exception ex) {
1057 throw new IOException("Could not open file " + path, ex);
1067 public void Paste(DataFormats.Format clipFormat) {
1068 base.Paste(Clipboard.GetDataObject(), clipFormat, false);
1073 if (document.undo.Redo ())
1074 OnTextChanged (EventArgs.Empty);
1077 public void SaveFile(Stream data, RichTextBoxStreamType fileType) {
1083 if (fileType == RichTextBoxStreamType.UnicodePlainText) {
1084 encoding = Encoding.Unicode;
1086 encoding = Encoding.ASCII;
1090 case RichTextBoxStreamType.PlainText:
1091 case RichTextBoxStreamType.TextTextOleObjs:
1092 case RichTextBoxStreamType.UnicodePlainText: {
1094 bytes = encoding.GetBytes(document.Root.text.ToString());
1095 data.Write(bytes, 0, bytes.Length);
1099 for (i = 1; i < document.Lines; i++) {
1100 // Normalize the new lines to the system ones
1101 string line_text = document.GetLine (i).TextWithoutEnding () + Environment.NewLine;
1102 bytes = encoding.GetBytes(line_text);
1103 data.Write(bytes, 0, bytes.Length);
1105 bytes = encoding.GetBytes(document.GetLine(document.Lines).text.ToString());
1106 data.Write(bytes, 0, bytes.Length);
1111 // If we're here we're saving RTF
1118 start_line = document.GetLine(1);
1119 end_line = document.GetLine(document.Lines);
1120 rtf = GenerateRTF(start_line, 0, end_line, end_line.text.Length);
1122 bytes = new Byte[4096];
1124 // Let's chunk it so we don't use up all memory...
1125 for (i = 0; i < total; i += 1024) {
1126 if ((i + 1024) < total) {
1127 current = encoding.GetBytes(rtf.ToString(i, 1024), 0, 1024, bytes, 0);
1129 current = total - i;
1130 current = encoding.GetBytes(rtf.ToString(i, current), 0, current, bytes, 0);
1132 data.Write(bytes, 0, current);
1136 public void SaveFile(string path) {
1137 if (path.EndsWith(".rtf")) {
1138 SaveFile(path, RichTextBoxStreamType.RichText);
1140 SaveFile(path, RichTextBoxStreamType.PlainText);
1144 public void SaveFile(string path, RichTextBoxStreamType fileType) {
1150 data = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, 1024, false);
1151 SaveFile(data, fileType);
1155 // throw new IOException("Could not write document to file " + path);
1166 [EditorBrowsable (EditorBrowsableState.Never)]
1167 public new void DrawToBitmap (Bitmap bitmap, Rectangle targetBounds)
1169 Graphics dc = Graphics.FromImage (bitmap);
1171 Draw (dc, targetBounds);
1175 #endregion // Public Instance Methods
1177 #region Protected Instance Methods
1178 protected virtual object CreateRichEditOleCallback() {
1179 throw new NotImplementedException();
1182 protected override void OnBackColorChanged(EventArgs e) {
1183 base.OnBackColorChanged (e);
1186 protected virtual void OnContentsResized(ContentsResizedEventArgs e) {
1187 ContentsResizedEventHandler eh = (ContentsResizedEventHandler)(Events [ContentsResizedEvent]);
1192 protected override void OnContextMenuChanged(EventArgs e) {
1193 base.OnContextMenuChanged (e);
1196 protected override void OnHandleCreated(EventArgs e) {
1197 base.OnHandleCreated (e);
1200 protected override void OnHandleDestroyed(EventArgs e) {
1201 base.OnHandleDestroyed (e);
1204 protected virtual void OnHScroll(EventArgs e) {
1205 EventHandler eh = (EventHandler)(Events [HScrollEvent]);
1210 [MonoTODO ("Stub, never called")]
1211 protected virtual void OnImeChange(EventArgs e) {
1212 EventHandler eh = (EventHandler)(Events [ImeChangeEvent]);
1217 protected virtual void OnLinkClicked(LinkClickedEventArgs e) {
1218 LinkClickedEventHandler eh = (LinkClickedEventHandler)(Events [LinkClickedEvent]);
1223 protected virtual void OnProtected(EventArgs e) {
1224 EventHandler eh = (EventHandler)(Events [ProtectedEvent]);
1229 protected override void OnRightToLeftChanged(EventArgs e) {
1230 base.OnRightToLeftChanged (e);
1233 protected virtual void OnSelectionChanged(EventArgs e) {
1234 EventHandler eh = (EventHandler)(Events [SelectionChangedEvent]);
1240 protected override void OnSystemColorsChanged(EventArgs e) {
1241 base.OnSystemColorsChanged (e);
1244 protected override void OnTextChanged(EventArgs e) {
1245 base.OnTextChanged (e);
1249 protected virtual void OnVScroll(EventArgs e) {
1250 EventHandler eh = (EventHandler)(Events [VScrollEvent]);
1255 protected override void WndProc(ref Message m) {
1256 base.WndProc (ref m);
1260 protected override bool ProcessCmdKey (ref Message m, Keys keyData)
1262 return base.ProcessCmdKey (ref m, keyData);
1265 #endregion // Protected Instance Methods
1268 static object ContentsResizedEvent = new object ();
1269 static object HScrollEvent = new object ();
1270 static object ImeChangeEvent = new object ();
1271 static object LinkClickedEvent = new object ();
1272 static object ProtectedEvent = new object ();
1273 static object SelectionChangedEvent = new object ();
1274 static object VScrollEvent = new object ();
1277 [EditorBrowsable(EditorBrowsableState.Never)]
1278 public new event EventHandler BackgroundImageChanged {
1279 add { base.BackgroundImageChanged += value; }
1280 remove { base.BackgroundImageChanged -= value; }
1285 [EditorBrowsable (EditorBrowsableState.Never)]
1286 public new event EventHandler BackgroundImageLayoutChanged {
1287 add { base.BackgroundImageLayoutChanged += value; }
1288 remove { base.BackgroundImageLayoutChanged -= value; }
1292 public event ContentsResizedEventHandler ContentsResized {
1293 add { Events.AddHandler (ContentsResizedEvent, value); }
1294 remove { Events.RemoveHandler (ContentsResizedEvent, value); }
1299 [EditorBrowsable(EditorBrowsableState.Never)]
1300 public new event EventHandler DoubleClick {
1301 add { base.DoubleClick += value; }
1302 remove { base.DoubleClick -= value; }
1308 [EditorBrowsable(EditorBrowsableState.Never)]
1310 public new event DragEventHandler DragDrop {
1311 add { base.DragDrop += value; }
1312 remove { base.DragDrop -= value; }
1317 [EditorBrowsable(EditorBrowsableState.Never)]
1319 public new event DragEventHandler DragEnter {
1320 add { base.DragEnter += value; }
1321 remove { base.DragEnter -= value; }
1325 [EditorBrowsable(EditorBrowsableState.Never)]
1326 public new event EventHandler DragLeave {
1327 add { base.DragLeave += value; }
1328 remove { base.DragLeave -= value; }
1333 [EditorBrowsable(EditorBrowsableState.Never)]
1334 public new event DragEventHandler DragOver {
1335 add { base.DragOver += value; }
1336 remove { base.DragOver -= value; }
1341 [EditorBrowsable(EditorBrowsableState.Never)]
1342 public new event GiveFeedbackEventHandler GiveFeedback {
1343 add { base.GiveFeedback += value; }
1344 remove { base.GiveFeedback -= value; }
1347 public event EventHandler HScroll {
1348 add { Events.AddHandler (HScrollEvent, value); }
1349 remove { Events.RemoveHandler (HScrollEvent, value); }
1352 public event EventHandler ImeChange {
1353 add { Events.AddHandler (ImeChangeEvent, value); }
1354 remove { Events.RemoveHandler (ImeChangeEvent, value); }
1357 public event LinkClickedEventHandler LinkClicked {
1358 add { Events.AddHandler (LinkClickedEvent, value); }
1359 remove { Events.RemoveHandler (LinkClickedEvent, value); }
1362 public event EventHandler Protected {
1363 add { Events.AddHandler (ProtectedEvent, value); }
1364 remove { Events.RemoveHandler (ProtectedEvent, value); }
1368 [EditorBrowsable(EditorBrowsableState.Never)]
1369 public new event QueryContinueDragEventHandler QueryContinueDrag {
1370 add { base.QueryContinueDrag += value; }
1371 remove { base.QueryContinueDrag -= value; }
1374 [MonoTODO ("Event never raised")]
1375 public event EventHandler SelectionChanged {
1376 add { Events.AddHandler (SelectionChangedEvent, value); }
1377 remove { Events.RemoveHandler (SelectionChangedEvent, value); }
1380 public event EventHandler VScroll {
1381 add { Events.AddHandler (VScrollEvent, value); }
1382 remove { Events.RemoveHandler (VScrollEvent, value); }
1384 #endregion // Events
1386 #region Private Methods
1388 internal override void SelectWord ()
1390 document.ExpandSelection(CaretSelection.Word, false);
1393 private class RtfSectionStyle : ICloneable {
1394 internal Color rtf_color;
1395 internal RTF.Font rtf_rtffont;
1396 internal int rtf_rtffont_size;
1397 internal FontStyle rtf_rtfstyle;
1398 internal HorizontalAlignment rtf_rtfalign;
1399 internal int rtf_par_line_left_indent;
1400 internal bool rtf_visible;
1401 internal int rtf_skip_width;
1403 public object Clone ()
1405 RtfSectionStyle new_style = new RtfSectionStyle ();
1407 new_style.rtf_color = rtf_color;
1408 new_style.rtf_par_line_left_indent = rtf_par_line_left_indent;
1409 new_style.rtf_rtfalign = rtf_rtfalign;
1410 new_style.rtf_rtffont = rtf_rtffont;
1411 new_style.rtf_rtffont_size = rtf_rtffont_size;
1412 new_style.rtf_rtfstyle = rtf_rtfstyle;
1413 new_style.rtf_visible = rtf_visible;
1414 new_style.rtf_skip_width = rtf_skip_width;
1420 // To allow us to keep track of the sections and revert formatting
1421 // as we go in and out of sections of the document.
1422 private void HandleGroup (RTF.RTF rtf)
1424 //start group - save the current formatting on to a stack
1425 //end group - go back to the formatting at the current group
1426 if (rtf_section_stack == null) {
1427 rtf_section_stack = new Stack ();
1430 if (rtf.Major == RTF.Major.BeginGroup) {
1431 rtf_section_stack.Push (rtf_style.Clone ());
1432 //spec specifies resetting unicode ignore at begin group as an attempt at error
1435 } else if (rtf.Major == RTF.Major.EndGroup) {
1436 if (rtf_section_stack.Count > 0) {
1437 FlushText (rtf, false);
1439 rtf_style = (RtfSectionStyle) rtf_section_stack.Pop ();
1444 [MonoInternalNote ("Add QuadJust support for justified alignment")]
1445 private void HandleControl(RTF.RTF rtf) {
1447 case RTF.Major.Unicode: {
1449 case RTF.Minor.UnicodeCharBytes: {
1450 rtf_style.rtf_skip_width = rtf.Param;
1454 case RTF.Minor.UnicodeChar: {
1455 FlushText (rtf, false);
1456 rtf_skip_count += rtf_style.rtf_skip_width;
1457 rtf_line.Append((char)rtf.Param);
1464 case RTF.Major.Destination: {
1465 // Console.Write("[Got Destination control {0}]", rtf.Minor);
1470 case RTF.Major.PictAttr:
1471 if (rtf.Picture != null && rtf.Picture.IsValid ()) {
1472 Line line = document.GetLine (rtf_cursor_y);
1473 document.InsertPicture (line, 0, rtf.Picture);
1476 FlushText (rtf, true);
1481 case RTF.Major.CharAttr: {
1483 case RTF.Minor.ForeColor: {
1484 System.Windows.Forms.RTF.Color color;
1486 color = System.Windows.Forms.RTF.Color.GetColor(rtf, rtf.Param);
1488 if (color != null) {
1489 FlushText(rtf, false);
1490 if (color.Red == -1 && color.Green == -1 && color.Blue == -1) {
1491 this.rtf_style.rtf_color = ForeColor;
1493 this.rtf_style.rtf_color = Color.FromArgb(color.Red, color.Green, color.Blue);
1495 FlushText (rtf, false);
1500 case RTF.Minor.FontSize: {
1501 FlushText(rtf, false);
1502 this.rtf_style.rtf_rtffont_size = rtf.Param / 2;
1506 case RTF.Minor.FontNum: {
1507 System.Windows.Forms.RTF.Font font;
1509 font = System.Windows.Forms.RTF.Font.GetFont(rtf, rtf.Param);
1511 FlushText(rtf, false);
1512 this.rtf_style.rtf_rtffont = font;
1517 case RTF.Minor.Plain: {
1518 FlushText(rtf, false);
1519 rtf_style.rtf_rtfstyle = FontStyle.Regular;
1523 case RTF.Minor.Bold: {
1524 FlushText(rtf, false);
1525 if (rtf.Param == RTF.RTF.NoParam) {
1526 rtf_style.rtf_rtfstyle |= FontStyle.Bold;
1528 rtf_style.rtf_rtfstyle &= ~FontStyle.Bold;
1533 case RTF.Minor.Italic: {
1534 FlushText(rtf, false);
1535 if (rtf.Param == RTF.RTF.NoParam) {
1536 rtf_style.rtf_rtfstyle |= FontStyle.Italic;
1538 rtf_style.rtf_rtfstyle &= ~FontStyle.Italic;
1543 case RTF.Minor.StrikeThru: {
1544 FlushText(rtf, false);
1545 if (rtf.Param == RTF.RTF.NoParam) {
1546 rtf_style.rtf_rtfstyle |= FontStyle.Strikeout;
1548 rtf_style.rtf_rtfstyle &= ~FontStyle.Strikeout;
1553 case RTF.Minor.Underline: {
1554 FlushText(rtf, false);
1555 if (rtf.Param == RTF.RTF.NoParam) {
1556 rtf_style.rtf_rtfstyle |= FontStyle.Underline;
1558 rtf_style.rtf_rtfstyle = rtf_style.rtf_rtfstyle & ~FontStyle.Underline;
1563 case RTF.Minor.Invisible: {
1564 FlushText (rtf, false);
1565 rtf_style.rtf_visible = false;
1569 case RTF.Minor.NoUnderline: {
1570 FlushText(rtf, false);
1571 rtf_style.rtf_rtfstyle &= ~FontStyle.Underline;
1578 case RTF.Major.ParAttr: {
1579 switch (rtf.Minor) {
1581 case RTF.Minor.ParDef:
1582 FlushText (rtf, false);
1583 rtf_style.rtf_par_line_left_indent = 0;
1584 rtf_style.rtf_rtfalign = HorizontalAlignment.Left;
1587 case RTF.Minor.LeftIndent:
1588 rtf_style.rtf_par_line_left_indent = (int) (((float) rtf.Param / 1440.0F) * CreateGraphics ().DpiX + 0.5F);
1591 case RTF.Minor.QuadCenter:
1592 FlushText (rtf, false);
1593 rtf_style.rtf_rtfalign = HorizontalAlignment.Center;
1596 case RTF.Minor.QuadJust:
1597 FlushText (rtf, false);
1598 rtf_style.rtf_rtfalign = HorizontalAlignment.Center;
1601 case RTF.Minor.QuadLeft:
1602 FlushText (rtf, false);
1603 rtf_style.rtf_rtfalign = HorizontalAlignment.Left;
1606 case RTF.Minor.QuadRight:
1607 FlushText (rtf, false);
1608 rtf_style.rtf_rtfalign = HorizontalAlignment.Right;
1614 case RTF.Major.SpecialChar: {
1615 //Console.Write("[Got SpecialChar control {0}]", rtf.Minor);
1622 private void SpecialChar(RTF.RTF rtf) {
1624 case RTF.Minor.Page:
1625 case RTF.Minor.Sect:
1627 case RTF.Minor.Line:
1628 case RTF.Minor.Par: {
1629 FlushText(rtf, true);
1633 case RTF.Minor.Cell: {
1638 case RTF.Minor.NoBrkSpace: {
1643 case RTF.Minor.Tab: {
1644 rtf_line.Append ("\t");
1645 // FlushText (rtf, false);
1649 case RTF.Minor.NoReqHyphen:
1650 case RTF.Minor.NoBrkHyphen: {
1651 rtf_line.Append ("-");
1652 // FlushText (rtf, false);
1656 case RTF.Minor.Bullet: {
1657 Console.WriteLine("*");
1661 case RTF.Minor.WidowCtrl:
1664 case RTF.Minor.EmDash: {
1665 rtf_line.Append ("\u2014");
1669 case RTF.Minor.EnDash: {
1670 rtf_line.Append ("\u2013");
1674 case RTF.Minor.LQuote: {
1675 Console.Write("\u2018");
1679 case RTF.Minor.RQuote: {
1680 Console.Write("\u2019");
1684 case RTF.Minor.LDblQuote: {
1685 Console.Write("\u201C");
1689 case RTF.Minor.RDblQuote: {
1690 Console.Write("\u201D");
1695 // Console.WriteLine ("skipped special char: {0}", rtf.Minor);
1702 private void HandleText(RTF.RTF rtf) {
1703 string str = rtf.EncodedText;
1705 //todo - simplistically skips characters, should skip bytes?
1706 if (rtf_skip_count > 0 && str.Length > 0) {
1707 int iToRemove = Math.Min (rtf_skip_count, str.Length);
1709 str = str.Substring (iToRemove);
1710 rtf_skip_count-=iToRemove;
1714 if ((RTF.StandardCharCode)rtf.Minor != RTF.StandardCharCode.nothing) {
1715 rtf_line.Append(rtf_text_map[(RTF.StandardCharCode)rtf.Minor]);
1717 if ((int)rtf.Major > 31 && (int)rtf.Major < 128) {
1718 rtf_line.Append((char)rtf.Major);
1720 //rtf_line.Append((char)rtf.Major);
1721 Console.Write("[Literal:0x{0:X2}]", (int)rtf.Major);
1726 if (rtf_style.rtf_visible)
1727 rtf_line.Append (str);
1730 private void FlushText(RTF.RTF rtf, bool newline) {
1734 length = rtf_line.Length;
1735 if (!newline && (length == 0)) {
1739 if (rtf_style.rtf_rtffont == null) {
1740 // First font in table is default
1741 rtf_style.rtf_rtffont = System.Windows.Forms.RTF.Font.GetFont (rtf, 0);
1744 font = new Font (rtf_style.rtf_rtffont.Name, rtf_style.rtf_rtffont_size, rtf_style.rtf_rtfstyle);
1746 if (rtf_style.rtf_color == Color.Empty) {
1747 System.Windows.Forms.RTF.Color color;
1749 // First color in table is default
1750 color = System.Windows.Forms.RTF.Color.GetColor (rtf, 0);
1752 if ((color == null) || (color.Red == -1 && color.Green == -1 && color.Blue == -1)) {
1753 rtf_style.rtf_color = ForeColor;
1755 rtf_style.rtf_color = Color.FromArgb (color.Red, color.Green, color.Blue);
1760 rtf_chars += rtf_line.Length;
1762 // Try to re-use if we are told so - this usually happens when we are inserting a flow of rtf text
1763 // with an already alive line.
1764 if (rtf_cursor_x == 0 && !reuse_line) {
1765 if (newline && rtf_line.ToString ().EndsWith (Environment.NewLine) == false)
1766 rtf_line.Append (Environment.NewLine);
1768 document.Add (rtf_cursor_y, rtf_line.ToString (), rtf_style.rtf_rtfalign, font, rtf_style.rtf_color,
1769 newline ? LineEnding.Rich : LineEnding.Wrap);
1770 if (rtf_style.rtf_par_line_left_indent != 0) {
1771 Line line = document.GetLine (rtf_cursor_y);
1772 line.indent = rtf_style.rtf_par_line_left_indent;
1777 line = document.GetLine (rtf_cursor_y);
1778 line.indent = rtf_style.rtf_par_line_left_indent;
1779 if (rtf_line.Length > 0) {
1780 document.InsertString (line, rtf_cursor_x, rtf_line.ToString ());
1781 document.FormatText (line, rtf_cursor_x + 1, line, rtf_cursor_x + 1 + length,
1782 font, rtf_style.rtf_color, Color.Empty,
1783 FormatSpecified.Font | FormatSpecified.Color);
1786 line = document.GetLine (rtf_cursor_y);
1787 line.ending = LineEnding.Rich;
1789 if (line.Text.EndsWith (Environment.NewLine) == false)
1790 line.Text += Environment.NewLine;
1793 reuse_line = false; // sanity assignment - in this case we have already re-used one line.
1800 rtf_cursor_x += length;
1802 rtf_line.Length = 0; // Empty line
1805 private void InsertRTFFromStream(Stream data, int cursor_x, int cursor_y) {
1810 InsertRTFFromStream(data, cursor_x, cursor_y, out x, out y, out chars);
1813 private void InsertRTFFromStream(Stream data, int cursor_x, int cursor_y, out int to_x, out int to_y, out int chars) {
1816 rtf = new RTF.RTF(data);
1819 rtf.ClassCallback[RTF.TokenClass.Text] = new RTF.ClassDelegate(HandleText);
1820 rtf.ClassCallback[RTF.TokenClass.Control] = new RTF.ClassDelegate(HandleControl);
1821 rtf.ClassCallback[RTF.TokenClass.Group] = new RTF.ClassDelegate(HandleGroup);
1824 rtf_line = new StringBuilder();
1825 rtf_style.rtf_color = Color.Empty;
1826 rtf_style.rtf_rtffont_size = (int)this.Font.Size;
1827 rtf_style.rtf_rtfalign = HorizontalAlignment.Left;
1828 rtf_style.rtf_rtfstyle = FontStyle.Regular;
1829 rtf_style.rtf_rtffont = null;
1830 rtf_style.rtf_visible = true;
1831 rtf_style.rtf_skip_width = 1;
1832 rtf_cursor_x = cursor_x;
1833 rtf_cursor_y = cursor_y;
1835 rtf.DefaultFont(this.Font.Name);
1837 rtf_text_map = new RTF.TextMap();
1838 RTF.TextMap.SetupStandardTable(rtf_text_map.Table);
1840 document.SuspendRecalc ();
1843 rtf.Read(); // That's it
1844 FlushText(rtf, false);
1849 catch (RTF.RTFException e) {
1853 // Seems to be plain text or broken RTF
1854 Console.WriteLine("RTF Parsing failure: {0}", e.Message);
1857 to_x = rtf_cursor_x;
1858 to_y = rtf_cursor_y;
1861 // clear the section stack if it was used
1862 if (rtf_section_stack != null)
1863 rtf_section_stack.Clear();
1865 document.RecalculateDocument(CreateGraphicsInternal(), cursor_y, document.Lines, false);
1866 document.ResumeRecalc (true);
1868 document.Invalidate (document.GetLine(cursor_y), 0, document.GetLine(document.Lines), -1);
1871 private void RichTextBox_HScrolled(object sender, EventArgs e) {
1875 private void RichTextBox_VScrolled(object sender, EventArgs e) {
1879 private void PointToTagPos(Point pt, out LineTag tag, out int pos) {
1884 if (p.X >= document.ViewPortWidth) {
1885 p.X = document.ViewPortWidth - 1;
1886 } else if (p.X < 0) {
1890 if (p.Y >= document.ViewPortHeight) {
1891 p.Y = document.ViewPortHeight - 1;
1892 } else if (p.Y < 0) {
1896 tag = document.FindCursor(p.X + document.ViewPortX, p.Y + document.ViewPortY, out pos);
1899 private void EmitRTFFontProperties(StringBuilder rtf, int prev_index, int font_index, Font prev_font, Font font) {
1900 if (prev_index != font_index) {
1901 rtf.Append(String.Format("\\f{0}", font_index)); // Font table entry
1904 if ((prev_font == null) || (prev_font.Size != font.Size)) {
1905 rtf.Append(String.Format("\\fs{0}", (int)(font.Size * 2))); // Font size
1908 if ((prev_font == null) || (font.Bold != prev_font.Bold)) {
1912 if (prev_font != null) {
1918 if ((prev_font == null) || (font.Italic != prev_font.Italic)) {
1922 if (prev_font != null) {
1928 if ((prev_font == null) || (font.Strikeout != prev_font.Strikeout)) {
1929 if (font.Strikeout) {
1930 rtf.Append("\\strike");
1932 if (prev_font != null) {
1933 rtf.Append("\\strike0");
1938 if ((prev_font == null) || (font.Underline != prev_font.Underline)) {
1939 if (font.Underline) {
1942 if (prev_font != null) {
1943 rtf.Append("\\ul0");
1949 [MonoInternalNote ("Emit unicode and other special characters properly")]
1950 private void EmitRTFText(StringBuilder rtf, string text) {
1954 // start_pos and end_pos are 0-based
1955 private StringBuilder GenerateRTF(Line start_line, int start_pos, Line end_line, int end_pos) {
1969 sb = new StringBuilder();
1970 fonts = new ArrayList(10);
1971 colors = new ArrayList(10);
1973 // Two runs, first we parse to determine tables;
1974 // and unlike most of our processing here we work on tags
1977 line_no = start_line.line_no;
1980 // Add default font and color; to optimize document content we don't
1981 // use this.Font and this.ForeColor but the font/color from the first tag
1982 tag = LineTag.FindTag(start_line, pos);
1985 fonts.Add(font.Name);
1988 while (line_no <= end_line.line_no) {
1989 line = document.GetLine(line_no);
1990 tag = LineTag.FindTag(line, pos);
1992 if (line_no != end_line.line_no) {
1993 line_len = line.text.Length;
1998 while (pos < line_len) {
1999 if (tag.Font.Name != font.Name) {
2001 if (!fonts.Contains(font.Name)) {
2002 fonts.Add(font.Name);
2006 if (tag.Color != color) {
2008 if (!colors.Contains(color)) {
2013 pos = tag.Start + tag.Length - 1;
2020 // We have the tables, emit the header
2021 sb.Append("{\\rtf1\\ansi");
2022 sb.Append("\\ansicpg1252"); // FIXME - is this correct?
2025 sb.Append(String.Format("\\deff{0}", fonts.IndexOf(this.Font.Name)));
2028 sb.Append("\\deflang1033" + Environment.NewLine); // FIXME - always 1033?
2030 // Emit the font table
2031 sb.Append("{\\fonttbl");
2032 for (i = 0; i < fonts.Count; i++) {
2033 sb.Append(String.Format("{{\\f{0}", i)); // {Font
2034 sb.Append("\\fnil"); // Family
2035 sb.Append("\\fcharset0 "); // Charset ANSI<space>
2036 sb.Append((string)fonts[i]); // Font name
2037 sb.Append(";}"); // }
2040 sb.Append(Environment.NewLine);
2042 // Emit the color table (if needed)
2043 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))) {
2044 sb.Append("{\\colortbl "); // Header and NO! default color
2045 for (i = 0; i < colors.Count; i++) {
2046 sb.Append(String.Format("\\red{0}", ((Color)colors[i]).R));
2047 sb.Append(String.Format("\\green{0}", ((Color)colors[i]).G));
2048 sb.Append(String.Format("\\blue{0}", ((Color)colors[i]).B));
2052 sb.Append(Environment.NewLine);
2055 sb.Append("{\\*\\generator Mono RichTextBox;}");
2056 // Emit initial paragraph settings
2057 tag = LineTag.FindTag(start_line, start_pos);
2058 sb.Append("\\pard"); // Reset to default paragraph properties
2059 EmitRTFFontProperties(sb, -1, fonts.IndexOf(tag.Font.Name), null, tag.Font); // Font properties
2060 sb.Append(" "); // Space separator
2063 color = (Color)colors[0];
2065 line_no = start_line.line_no;
2068 while (line_no <= end_line.line_no) {
2069 line = document.GetLine(line_no);
2070 tag = LineTag.FindTag(line, pos);
2072 if (line_no != end_line.line_no) {
2073 line_len = line.text.Length;
2078 while (pos < line_len) {
2081 if (tag.Font != font) {
2082 EmitRTFFontProperties(sb, fonts.IndexOf(font.Name), fonts.IndexOf(tag.Font.Name), font, tag.Font);
2086 if (tag.Color != color) {
2088 sb.Append(String.Format("\\cf{0}", colors.IndexOf(color)));
2090 if (length != sb.Length) {
2091 sb.Append(" "); // Emit space to separate keywords from text
2094 // Emit the string itself
2095 if (line_no != end_line.line_no) {
2096 EmitRTFText(sb, tag.Line.text.ToString(pos, tag.Start + tag.Length - pos - 1));
2098 if (end_pos < (tag.Start + tag.Length - 1)) {
2099 // Emit partial tag only, end_pos is inside this tag
2100 EmitRTFText(sb, tag.Line.text.ToString(pos, end_pos - pos));
2102 EmitRTFText(sb, tag.Line.text.ToString(pos, tag.Start + tag.Length - pos - 1));
2106 pos = tag.Start + tag.Length - 1;
2109 if (pos >= line.text.Length) {
2110 if (line.ending != LineEnding.Wrap) {
2112 sb.Append(Environment.NewLine);
2120 sb.Append(Environment.NewLine);
2124 #endregion // Private Methods