RichTextBox.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  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:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  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.
  19. //
  20. // Copyright (c) 2005-2006 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok <[email protected]>
  24. //
  25. //
  26. // NOT COMPLETE
  27. using System;
  28. using System.Collections;
  29. using System.ComponentModel;
  30. using System.Drawing;
  31. using System.IO;
  32. using System.Text;
  33. using RTF=System.Windows.Forms.RTF;
  34. namespace System.Windows.Forms {
  35. public class RichTextBox : TextBoxBase {
  36. #region Local Variables
  37. internal bool auto_word_select;
  38. internal int bullet_indent;
  39. internal bool can_redo;
  40. internal bool detect_urls;
  41. internal string redo_action_name;
  42. internal int margin_right;
  43. internal string undo_action_name;
  44. internal float zoom;
  45. private RTF.TextMap rtf_text_map;
  46. private int rtf_skip_width;
  47. private int rtf_skip_count;
  48. private StringBuilder rtf_line;
  49. private SolidBrush rtf_color;
  50. private RTF.Font rtf_rtffont;
  51. private int rtf_rtffont_size;
  52. private FontStyle rtf_rtfstyle;
  53. private HorizontalAlignment rtf_rtfalign;
  54. private int rtf_cursor_x;
  55. private int rtf_cursor_y;
  56. #endregion // Local Variables
  57. #region Public Constructors
  58. public RichTextBox() {
  59. accepts_return = true;
  60. auto_word_select = false;
  61. bullet_indent = 0;
  62. can_redo = false;
  63. detect_urls = true;
  64. max_length = Int32.MaxValue;
  65. redo_action_name = string.Empty;
  66. margin_right = 0;
  67. undo_action_name = string.Empty;
  68. zoom = 1;
  69. base.Multiline = true;
  70. document.CRLFSize = 1;
  71. scrollbars = RichTextBoxScrollBars.Both;
  72. alignment = HorizontalAlignment.Left;
  73. LostFocus += new EventHandler(RichTextBox_LostFocus);
  74. GotFocus += new EventHandler(RichTextBox_GotFocus);
  75. BackColor = ThemeEngine.Current.ColorWindow;
  76. ForeColor = ThemeEngine.Current.ColorWindowText;
  77. base.HScrolled += new EventHandler(RichTextBox_HScrolled);
  78. base.VScrolled += new EventHandler(RichTextBox_VScrolled);
  79. }
  80. #endregion // Public Constructors
  81. #region Private & Internal Methods
  82. private void RichTextBox_LostFocus(object sender, EventArgs e) {
  83. has_focus = false;
  84. Invalidate();
  85. }
  86. private void RichTextBox_GotFocus(object sender, EventArgs e) {
  87. has_focus = true;
  88. Invalidate();
  89. }
  90. #endregion // Private & Internal Methods
  91. #region Public Instance Properties
  92. public override bool AllowDrop {
  93. get {
  94. return base.AllowDrop;
  95. }
  96. set {
  97. base.AllowDrop = value;
  98. }
  99. }
  100. [DefaultValue(false)]
  101. [Localizable(true)]
  102. public override bool AutoSize {
  103. get {
  104. return auto_size;
  105. }
  106. set {
  107. base.AutoSize = value;
  108. }
  109. }
  110. [DefaultValue(false)]
  111. public bool AutoWordSelection {
  112. get {
  113. return auto_word_select;
  114. }
  115. set {
  116. auto_word_select = true;
  117. }
  118. }
  119. [Browsable(false)]
  120. [EditorBrowsable(EditorBrowsableState.Never)]
  121. public override System.Drawing.Image BackgroundImage {
  122. get {
  123. return background_image;
  124. }
  125. set {
  126. base.BackgroundImage = value;
  127. }
  128. }
  129. [DefaultValue(0)]
  130. [Localizable(true)]
  131. public int BulletIndent {
  132. get {
  133. return bullet_indent;
  134. }
  135. set {
  136. bullet_indent = value;
  137. }
  138. }
  139. [Browsable(false)]
  140. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  141. public bool CanRedo {
  142. get {
  143. return can_redo;
  144. }
  145. }
  146. [DefaultValue(true)]
  147. public bool DetectUrls {
  148. get {
  149. return detect_urls;
  150. }
  151. set {
  152. detect_urls = true;
  153. }
  154. }
  155. public override Font Font {
  156. get {
  157. return base.Font;
  158. }
  159. set {
  160. if (font != value) {
  161. Line start;
  162. Line end;
  163. if (auto_size) {
  164. if (PreferredHeight != Height) {
  165. Height = PreferredHeight;
  166. }
  167. }
  168. base.Font = value;
  169. // Font changes always set the whole doc to that font
  170. start = document.GetLine(1);
  171. end = document.GetLine(document.Lines);
  172. document.FormatText(start, 1, end, end.text.Length + 1, base.Font, new SolidBrush(this.ForeColor));
  173. }
  174. }
  175. }
  176. public override Color ForeColor {
  177. get {
  178. return base.ForeColor;
  179. }
  180. set {
  181. base.ForeColor = value;
  182. }
  183. }
  184. [DefaultValue(Int32.MaxValue)]
  185. public override int MaxLength {
  186. get {
  187. return base.max_length;
  188. }
  189. set {
  190. base.max_length = value;
  191. }
  192. }
  193. [DefaultValue(true)]
  194. public override bool Multiline {
  195. get {
  196. return multiline;
  197. }
  198. set {
  199. base.Multiline = value;
  200. }
  201. }
  202. [Browsable(false)]
  203. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  204. [MonoTODO]
  205. public string RedoActionName {
  206. get {
  207. return redo_action_name;
  208. }
  209. }
  210. [DefaultValue(0)]
  211. [Localizable(true)]
  212. [MonoTODO("Teach TextControl.RecalculateLine to consider the right margin as well")]
  213. public int RightMargin {
  214. get {
  215. return margin_right;
  216. }
  217. set {
  218. margin_right = value;
  219. }
  220. }
  221. [Browsable(false)]
  222. [DefaultValue("")]
  223. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  224. public string Rtf {
  225. get {
  226. Line start_line;
  227. Line end_line;
  228. int current;
  229. int total;
  230. start_line = document.GetLine(1);
  231. end_line = document.GetLine(document.Lines);
  232. return GenerateRTF(start_line, 0, end_line, end_line.text.Length).ToString();
  233. }
  234. set {
  235. MemoryStream data;
  236. document.Empty();
  237. data = new MemoryStream(Encoding.ASCII.GetBytes(value), false);
  238. InsertRTFFromStream(data, 0, 1);
  239. data.Close();
  240. }
  241. }
  242. [DefaultValue(RichTextBoxScrollBars.Both)]
  243. [Localizable(true)]
  244. public RichTextBoxScrollBars ScrollBars {
  245. get {
  246. return scrollbars;
  247. }
  248. set {
  249. scrollbars = value;
  250. }
  251. }
  252. [Browsable(false)]
  253. [DefaultValue("")]
  254. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  255. public string SelectedRtf {
  256. get {
  257. return GenerateRTF(document.selection_start.line, document.selection_start.pos, document.selection_end.line, document.selection_end.pos).ToString();
  258. }
  259. set {
  260. MemoryStream data;
  261. int x;
  262. int y;
  263. Line line;
  264. if (document.selection_visible) {
  265. document.ReplaceSelection("");
  266. }
  267. data = new MemoryStream(Encoding.ASCII.GetBytes(value), false);
  268. InsertRTFFromStream(data, document.selection_start.pos, document.selection_start.line.line_no, out x, out y);
  269. data.Close();
  270. line = document.GetLine(y);
  271. document.SetSelection(document.GetLine(y), x);
  272. document.PositionCaret(line, x);
  273. document.DisplayCaret();
  274. OnTextChanged(EventArgs.Empty);
  275. }
  276. }
  277. [Browsable(false)]
  278. [DefaultValue("")]
  279. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  280. public override string SelectedText {
  281. get {
  282. return base.SelectedText;
  283. }
  284. set {
  285. base.SelectedText = value;
  286. }
  287. }
  288. [Browsable(false)]
  289. [DefaultValue(HorizontalAlignment.Left)]
  290. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  291. public HorizontalAlignment SelectionAlignment {
  292. get {
  293. HorizontalAlignment align;
  294. Line start;
  295. Line end;
  296. Line line;
  297. start = document.ParagraphStart(document.selection_start.line);
  298. align = start.alignment;
  299. end = document.ParagraphEnd(document.selection_end.line);
  300. line = start;
  301. while (true) {
  302. if (line.alignment != align) {
  303. return HorizontalAlignment.Left;
  304. }
  305. if (line == end) {
  306. break;
  307. }
  308. line = document.GetLine(line.line_no + 1);
  309. }
  310. return align;
  311. }
  312. set {
  313. Line start;
  314. Line end;
  315. Line line;
  316. start = document.ParagraphStart(document.selection_start.line);
  317. end = document.ParagraphEnd(document.selection_end.line);
  318. line = start;
  319. while (true) {
  320. line.alignment = value;
  321. if (line == end) {
  322. break;
  323. }
  324. line = document.GetLine(line.line_no + 1);
  325. }
  326. this.CalculateDocument();
  327. }
  328. }
  329. [Browsable(false)]
  330. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  331. public Color SelectionColor {
  332. get {
  333. Color color;
  334. LineTag start;
  335. LineTag end;
  336. LineTag tag;
  337. start = document.selection_start.tag;
  338. end = document.selection_end.tag;
  339. color = ((SolidBrush)document.selection_start.tag.color).Color;
  340. tag = start;
  341. while (true) {
  342. if (!color.Equals(((SolidBrush)tag.color).Color)) {
  343. return Color.Empty;
  344. }
  345. if (tag == end) {
  346. break;
  347. }
  348. tag = document.NextTag(tag);
  349. if (tag == null) {
  350. break;
  351. }
  352. }
  353. return color;
  354. }
  355. set {
  356. FontDefinition attributes;
  357. int sel_start;
  358. int sel_end;
  359. attributes = new FontDefinition();
  360. attributes.color = value;
  361. sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
  362. sel_end = document.LineTagToCharIndex(document.selection_end.line, document.selection_end.pos);
  363. document.FormatText(document.selection_start.line, document.selection_start.pos + 1, document.selection_end.line, document.selection_end.pos + 1, attributes);
  364. document.CharIndexToLineTag(sel_start, out document.selection_start.line, out document.selection_start.tag, out document.selection_start.pos);
  365. document.CharIndexToLineTag(sel_end, out document.selection_end.line, out document.selection_end.tag, out document.selection_end.pos);
  366. document.UpdateView(document.selection_start.line, 0);
  367. document.AlignCaret();
  368. }
  369. }
  370. [Browsable(false)]
  371. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  372. public Font SelectionFont {
  373. get {
  374. Font font;
  375. LineTag start;
  376. LineTag end;
  377. LineTag tag;
  378. start = document.selection_start.tag;
  379. end = document.selection_end.tag;
  380. font = document.selection_start.tag.font;
  381. tag = start;
  382. while (true) {
  383. if (!font.Equals(tag.font)) {
  384. return null;
  385. }
  386. if (tag == end) {
  387. break;
  388. }
  389. tag = document.NextTag(tag);
  390. if (tag == null) {
  391. break;
  392. }
  393. }
  394. return font;
  395. }
  396. set {
  397. FontDefinition attributes;
  398. int sel_start;
  399. int sel_end;
  400. attributes = new FontDefinition();
  401. attributes.font_obj = value;
  402. sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
  403. sel_end = document.LineTagToCharIndex(document.selection_end.line, document.selection_end.pos);
  404. document.FormatText(document.selection_start.line, document.selection_start.pos + 1, document.selection_end.line, document.selection_end.pos + 1, attributes);
  405. document.CharIndexToLineTag(sel_start, out document.selection_start.line, out document.selection_start.tag, out document.selection_start.pos);
  406. document.CharIndexToLineTag(sel_end, out document.selection_end.line, out document.selection_end.tag, out document.selection_end.pos);
  407. document.UpdateView(document.selection_start.line, 0);
  408. document.AlignCaret();
  409. }
  410. }
  411. [Browsable(false)]
  412. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  413. public override int SelectionLength {
  414. get {
  415. return base.SelectionLength;
  416. }
  417. set {
  418. base.SelectionLength = value;
  419. }
  420. }
  421. [Browsable(false)]
  422. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  423. public RichTextBoxSelectionTypes SelectionType {
  424. get {
  425. if (document.selection_start == document.selection_end) {
  426. return RichTextBoxSelectionTypes.Empty;
  427. }
  428. // Lazy, but works
  429. if (SelectedText.Length > 1) {
  430. return RichTextBoxSelectionTypes.MultiChar | RichTextBoxSelectionTypes.Text;
  431. }
  432. return RichTextBoxSelectionTypes.Text;
  433. }
  434. }
  435. [Localizable(true)]
  436. public override string Text {
  437. get {
  438. return base.Text;
  439. }
  440. set {
  441. base.Text = value;
  442. }
  443. }
  444. [Browsable(false)]
  445. public override int TextLength {
  446. get {
  447. return base.TextLength;
  448. }
  449. }
  450. [Browsable(false)]
  451. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  452. public string UndoActionName {
  453. get {
  454. return document.undo.UndoName;
  455. }
  456. }
  457. [Localizable(true)]
  458. [DefaultValue(1)]
  459. public float ZoomFactor {
  460. get {
  461. return zoom;
  462. }
  463. set {
  464. zoom = value;
  465. }
  466. }
  467. #endregion // Public Instance Properties
  468. #region Protected Instance Properties
  469. protected override CreateParams CreateParams {
  470. get {
  471. return base.CreateParams;
  472. }
  473. }
  474. protected override Size DefaultSize {
  475. get {
  476. return new Size(100, 96);
  477. }
  478. }
  479. #endregion // Protected Instance Properties
  480. #region Public Instance Methods
  481. public bool CanPaste(DataFormats.Format clipFormat) {
  482. if ((clipFormat.Name == DataFormats.Rtf) ||
  483. (clipFormat.Name == DataFormats.Text) ||
  484. (clipFormat.Name == DataFormats.UnicodeText)) {
  485. return true;
  486. }
  487. return false;
  488. }
  489. public int Find(char[] characterSet) {
  490. return Find(characterSet, -1, -1);
  491. }
  492. public int Find(char[] characterSet, int start) {
  493. return Find(characterSet, start, -1);
  494. }
  495. public int Find(char[] characterSet, int start, int end) {
  496. Document.Marker start_mark;
  497. Document.Marker end_mark;
  498. Document.Marker result;
  499. if (start == -1) {
  500. document.GetMarker(out start_mark, true);
  501. } else {
  502. Line line;
  503. LineTag tag;
  504. int pos;
  505. start_mark = new Document.Marker();
  506. document.CharIndexToLineTag(start, out line, out tag, out pos);
  507. start_mark.line = line;
  508. start_mark.tag = tag;
  509. start_mark.pos = pos;
  510. }
  511. if (end == -1) {
  512. document.GetMarker(out end_mark, false);
  513. } else {
  514. Line line;
  515. LineTag tag;
  516. int pos;
  517. end_mark = new Document.Marker();
  518. document.CharIndexToLineTag(end, out line, out tag, out pos);
  519. end_mark.line = line;
  520. end_mark.tag = tag;
  521. end_mark.pos = pos;
  522. }
  523. if (document.FindChars(characterSet, start_mark, end_mark, out result)) {
  524. return document.LineTagToCharIndex(result.line, result.pos);
  525. }
  526. return -1;
  527. }
  528. public int Find(string str) {
  529. return Find(str, -1, -1, RichTextBoxFinds.None);
  530. }
  531. public int Find(string str, int start, int end, RichTextBoxFinds options) {
  532. Document.Marker start_mark;
  533. Document.Marker end_mark;
  534. Document.Marker result;
  535. if (start == -1) {
  536. document.GetMarker(out start_mark, true);
  537. } else {
  538. Line line;
  539. LineTag tag;
  540. int pos;
  541. start_mark = new Document.Marker();
  542. document.CharIndexToLineTag(start, out line, out tag, out pos);
  543. start_mark.line = line;
  544. start_mark.tag = tag;
  545. start_mark.pos = pos;
  546. }
  547. if (end == -1) {
  548. document.GetMarker(out end_mark, false);
  549. } else {
  550. Line line;
  551. LineTag tag;
  552. int pos;
  553. end_mark = new Document.Marker();
  554. document.CharIndexToLineTag(end, out line, out tag, out pos);
  555. end_mark.line = line;
  556. end_mark.tag = tag;
  557. end_mark.pos = pos;
  558. }
  559. if (document.Find(str, start_mark, end_mark, out result, options)) {
  560. return document.LineTagToCharIndex(result.line, result.pos);
  561. }
  562. return -1;
  563. }
  564. public int Find(string str, int start, RichTextBoxFinds options) {
  565. return Find(str, start, -1, options);
  566. }
  567. public int Find(string str, RichTextBoxFinds options) {
  568. return Find(str, -1, -1, options);
  569. }
  570. public char GetCharFromPosition(Point pt) {
  571. LineTag tag;
  572. int pos;
  573. PointToTagPos(pt, out tag, out pos);
  574. if (pos >= tag.line.text.Length) {
  575. return '\n';
  576. }
  577. return tag.line.text[pos];
  578. }
  579. public int GetCharIndexFromPosition(Point pt) {
  580. LineTag tag;
  581. int pos;
  582. PointToTagPos(pt, out tag, out pos);
  583. return document.LineTagToCharIndex(tag.line, pos);
  584. }
  585. public int GetLineFromCharIndex(int index) {
  586. Line line;
  587. LineTag tag;
  588. int pos;
  589. document.CharIndexToLineTag(index, out line, out tag, out pos);
  590. return line.LineNo - 1;
  591. }
  592. public Point GetPositionFromCharIndex(int index) {
  593. Line line;
  594. LineTag tag;
  595. int pos;
  596. document.CharIndexToLineTag(index, out line, out tag, out pos);
  597. return new Point((int)line.widths[pos] + 1, line.Y + 1);
  598. }
  599. public void LoadFile(System.IO.Stream data, RichTextBoxStreamType fileType) {
  600. RTF.RTF rtf; // Not 'using SWF.RTF' to avoid ambiguities with font and color
  601. document.Empty();
  602. // FIXME - ignoring unicode
  603. if (fileType == RichTextBoxStreamType.PlainText) {
  604. StringBuilder sb;
  605. int count;
  606. byte[] buffer;
  607. try {
  608. sb = new StringBuilder((int)data.Length);
  609. buffer = new byte[1024];
  610. }
  611. catch {
  612. throw new IOException("Not enough memory to load document");
  613. }
  614. count = 0;
  615. while (count < data.Length) {
  616. count += data.Read(buffer, count, 1024);
  617. sb.Append(buffer);
  618. }
  619. base.Text = sb.ToString();
  620. return;
  621. }
  622. InsertRTFFromStream(data, 0, 1);
  623. }
  624. [MonoTODO("Make smarter RTF detection?")]
  625. public void LoadFile(string path) {
  626. if (path.EndsWith(".rtf")) {
  627. LoadFile(path, RichTextBoxStreamType.RichText);
  628. } else {
  629. LoadFile(path, RichTextBoxStreamType.PlainText);
  630. }
  631. }
  632. public void LoadFile(string path, RichTextBoxStreamType fileType) {
  633. FileStream data;
  634. data = null;
  635. try {
  636. data = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1024);
  637. LoadFile(data, fileType);
  638. }
  639. catch {
  640. throw new IOException("Could not open file " + path);
  641. }
  642. finally {
  643. if (data != null) {
  644. data.Close();
  645. }
  646. }
  647. }
  648. public void Paste(DataFormats.Format clipFormat) {
  649. base.Paste(clipFormat, false);
  650. }
  651. [MonoTODO()]
  652. public void Redo() {
  653. }
  654. public void SaveFile(Stream data, RichTextBoxStreamType fileType) {
  655. Encoding encoding;
  656. int i;
  657. Byte[] bytes;
  658. if (fileType == RichTextBoxStreamType.UnicodePlainText) {
  659. encoding = Encoding.Unicode;
  660. } else {
  661. encoding = Encoding.ASCII;
  662. }
  663. switch(fileType) {
  664. case RichTextBoxStreamType.PlainText:
  665. case RichTextBoxStreamType.TextTextOleObjs:
  666. case RichTextBoxStreamType.UnicodePlainText: {
  667. if (!multiline) {
  668. bytes = encoding.GetBytes(document.Root.text.ToString());
  669. data.Write(bytes, 0, bytes.Length);
  670. return;
  671. }
  672. for (i = 1; i < document.Lines; i++) {
  673. bytes = encoding.GetBytes(document.GetLine(i).text.ToString() + Environment.NewLine);
  674. data.Write(bytes, 0, bytes.Length);
  675. }
  676. bytes = encoding.GetBytes(document.GetLine(document.Lines).text.ToString());
  677. data.Write(bytes, 0, bytes.Length);
  678. return;
  679. }
  680. }
  681. // If we're here we're saving RTF
  682. Line start_line;
  683. Line end_line;
  684. StringBuilder rtf;
  685. int current;
  686. int total;
  687. start_line = document.GetLine(1);
  688. end_line = document.GetLine(document.Lines);
  689. rtf = GenerateRTF(start_line, 0, end_line, end_line.text.Length);
  690. total = rtf.Length;
  691. bytes = new Byte[4096];
  692. // Let's chunk it so we don't use up all memory...
  693. for (i = 0; i < total; i += 1024) {
  694. if ((i + 1024) < total) {
  695. current = encoding.GetBytes(rtf.ToString(i, 1024), 0, 1024, bytes, 0);
  696. } else {
  697. current = total - i;
  698. current = encoding.GetBytes(rtf.ToString(i, current), 0, current, bytes, 0);
  699. }
  700. data.Write(bytes, 0, current);
  701. }
  702. }
  703. public void SaveFile(string path) {
  704. if (path.EndsWith(".rtf")) {
  705. SaveFile(path, RichTextBoxStreamType.RichText);
  706. } else {
  707. SaveFile(path, RichTextBoxStreamType.PlainText);
  708. }
  709. }
  710. public void SaveFile(string path, RichTextBoxStreamType fileType) {
  711. FileStream data;
  712. data = null;
  713. // try {
  714. data = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, 1024, false);
  715. SaveFile(data, fileType);
  716. // }
  717. // catch {
  718. // throw new IOException("Could not write document to file " + path);
  719. // }
  720. // finally {
  721. if (data != null) {
  722. data.Close();
  723. }
  724. // }
  725. }
  726. #endregion // Public Instance Methods
  727. #region Protected Instance Methods
  728. protected virtual object CreateRichEditOleCallback() {
  729. throw new NotImplementedException();
  730. }
  731. protected override void OnBackColorChanged(EventArgs e) {
  732. base.OnBackColorChanged (e);
  733. }
  734. protected virtual void OnContentsResized(ContentsResizedEventArgs e) {
  735. if (ContentsResized != null) {
  736. ContentsResized(this, e);
  737. }
  738. }
  739. protected override void OnContextMenuChanged(EventArgs e) {
  740. base.OnContextMenuChanged (e);
  741. }
  742. protected override void OnHandleCreated(EventArgs e) {
  743. base.OnHandleCreated (e);
  744. }
  745. protected override void OnHandleDestroyed(EventArgs e) {
  746. base.OnHandleDestroyed (e);
  747. }
  748. protected virtual void OnHScroll(EventArgs e) {
  749. if (HScroll != null) {
  750. HScroll(this, e);
  751. }
  752. }
  753. [MonoTODO("Determine when to call this")]
  754. protected virtual void OnImeChange(EventArgs e) {
  755. if (ImeChange != null) {
  756. ImeChange(this, e);
  757. }
  758. }
  759. protected virtual void OnLinkClicked(LinkClickedEventArgs e) {
  760. if (LinkClicked != null) {
  761. LinkClicked(this, e);
  762. }
  763. }
  764. protected virtual void OnProtected(EventArgs e) {
  765. if (Protected != null) {
  766. Protected(this, e);
  767. }
  768. }
  769. protected override void OnRightToLeftChanged(EventArgs e) {
  770. base.OnRightToLeftChanged (e);
  771. }
  772. protected virtual void OnSelectionChanged(EventArgs e) {
  773. if (SelectionChanged != null) {
  774. SelectionChanged(this, e);
  775. }
  776. }
  777. protected override void OnSystemColorsChanged(EventArgs e) {
  778. base.OnSystemColorsChanged (e);
  779. }
  780. protected override void OnTextChanged(EventArgs e) {
  781. base.OnTextChanged (e);
  782. }
  783. protected virtual void OnVScroll(EventArgs e) {
  784. if (VScroll != null) {
  785. VScroll(this, e);
  786. }
  787. }
  788. protected override void WndProc(ref Message m) {
  789. base.WndProc (ref m);
  790. }
  791. #endregion // Protected Instance Methods
  792. #region Events
  793. [Browsable(false)]
  794. [EditorBrowsable(EditorBrowsableState.Never)]
  795. public event EventHandler BackgroundImageChanged;
  796. public event ContentsResizedEventHandler ContentsResized;
  797. [Browsable(false)]
  798. [EditorBrowsable(EditorBrowsableState.Never)]
  799. public event EventHandler DoubleClick;
  800. [Browsable(false)]
  801. [EditorBrowsable(EditorBrowsableState.Never)]
  802. public event DragEventHandler DragDrop {
  803. add {
  804. base.DragDrop += value;
  805. }
  806. remove {
  807. base.DragDrop -= value;
  808. }
  809. }
  810. [Browsable(false)]
  811. [EditorBrowsable(EditorBrowsableState.Never)]
  812. public event DragEventHandler DragEnter {
  813. add {
  814. base.DragEnter += value;
  815. }
  816. remove {
  817. base.DragEnter -= value;
  818. }
  819. }
  820. [Browsable(false)]
  821. [EditorBrowsable(EditorBrowsableState.Never)]
  822. public event EventHandler DragLeave {
  823. add {
  824. base.DragLeave += value;
  825. }
  826. remove {
  827. base.DragLeave -= value;
  828. }
  829. }
  830. [Browsable(false)]
  831. [EditorBrowsable(EditorBrowsableState.Never)]
  832. public event DragEventHandler DragOver {
  833. add {
  834. base.DragOver += value;
  835. }
  836. remove {
  837. base.DragOver -= value;
  838. }
  839. }
  840. [Browsable(false)]
  841. [EditorBrowsable(EditorBrowsableState.Never)]
  842. public event GiveFeedbackEventHandler GiveFeedback;
  843. public event EventHandler HScroll;
  844. public event EventHandler ImeChange;
  845. public event LinkClickedEventHandler LinkClicked;
  846. public event EventHandler Protected;
  847. [Browsable(false)]
  848. [EditorBrowsable(EditorBrowsableState.Never)]
  849. public event QueryContinueDragEventHandler QueryContinueDrag;
  850. public event EventHandler SelectionChanged;
  851. public event EventHandler VScroll;
  852. #endregion // Events
  853. #region Private Methods
  854. private void HandleControl(RTF.RTF rtf) {
  855. switch(rtf.Major) {
  856. case RTF.Major.Unicode: {
  857. switch(rtf.Minor) {
  858. case Minor.UnicodeCharBytes: {
  859. rtf_skip_width = rtf.Param;
  860. break;
  861. }
  862. case Minor.UnicodeChar: {
  863. rtf_skip_count += rtf_skip_width;
  864. rtf_line.Append((char)rtf.Param);
  865. break;
  866. }
  867. }
  868. break;
  869. }
  870. case RTF.Major.Destination: {
  871. Console.Write("[Got Destination control {0}]", rtf.Minor);
  872. rtf.SkipGroup();
  873. break;
  874. }
  875. case RTF.Major.CharAttr: {
  876. switch(rtf.Minor) {
  877. case Minor.ForeColor: {
  878. System.Windows.Forms.RTF.Color color;
  879. color = System.Windows.Forms.RTF.Color.GetColor(rtf, rtf.Param);
  880. if (color != null) {
  881. FlushText(rtf, false);
  882. if (color.Red == -1 && color.Green == -1 && color.Blue == -1) {
  883. this.rtf_color = new SolidBrush(ForeColor);
  884. } else {
  885. this.rtf_color = new SolidBrush(Color.FromArgb(color.Red, color.Green, color.Blue));
  886. }
  887. }
  888. break;
  889. }
  890. case Minor.FontSize: {
  891. FlushText(rtf, false);
  892. this.rtf_rtffont_size = rtf.Param / 2;
  893. break;
  894. }
  895. case Minor.FontNum: {
  896. System.Windows.Forms.RTF.Font font;
  897. font = System.Windows.Forms.RTF.Font.GetFont(rtf, rtf.Param);
  898. if (font != null) {
  899. FlushText(rtf, false);
  900. this.rtf_rtffont = font;
  901. }
  902. break;
  903. }
  904. case Minor.Plain: {
  905. FlushText(rtf, false);
  906. rtf_rtfstyle = FontStyle.Regular;
  907. break;
  908. }
  909. case Minor.Bold: {
  910. FlushText(rtf, false);
  911. if (rtf.Param == RTF.RTF.NoParam) {
  912. rtf_rtfstyle |= FontStyle.Bold;
  913. } else {
  914. rtf_rtfstyle &= ~FontStyle.Bold;
  915. }
  916. break;
  917. }
  918. case Minor.Italic: {
  919. FlushText(rtf, false);
  920. if (rtf.Param == RTF.RTF.NoParam) {
  921. rtf_rtfstyle |= FontStyle.Italic;
  922. } else {
  923. rtf_rtfstyle &= ~FontStyle.Italic;
  924. }
  925. break;
  926. }
  927. case Minor.StrikeThru: {
  928. FlushText(rtf, false);
  929. if (rtf.Param == RTF.RTF.NoParam) {
  930. rtf_rtfstyle |= FontStyle.Strikeout;
  931. } else {
  932. rtf_rtfstyle &= ~FontStyle.Strikeout;
  933. }
  934. break;
  935. }
  936. case Minor.Underline: {
  937. FlushText(rtf, false);
  938. if (rtf.Param == RTF.RTF.NoParam) {
  939. rtf_rtfstyle |= FontStyle.Underline;
  940. } else {
  941. rtf_rtfstyle = rtf_rtfstyle & ~FontStyle.Underline;
  942. }
  943. break;
  944. }
  945. case Minor.NoUnderline: {
  946. FlushText(rtf, false);
  947. rtf_rtfstyle &= ~FontStyle.Underline;
  948. break;
  949. }
  950. }
  951. break;
  952. }
  953. case RTF.Major.SpecialChar: {
  954. Console.Write("[Got SpecialChar control {0}]", rtf.Minor);
  955. SpecialChar(rtf);
  956. break;
  957. }
  958. }
  959. }
  960. private void SpecialChar(RTF.RTF rtf) {
  961. switch(rtf.Minor) {
  962. case Minor.Page:
  963. case Minor.Sect:
  964. case Minor.Row:
  965. case Minor.Line:
  966. case Minor.Par: {
  967. FlushText(rtf, true);
  968. break;
  969. }
  970. case Minor.Cell: {
  971. Console.Write(" ");
  972. break;
  973. }
  974. case Minor.NoBrkSpace: {
  975. Console.Write(" ");
  976. break;
  977. }
  978. case Minor.Tab: {
  979. Console.Write("\t");
  980. break;
  981. }
  982. case Minor.NoBrkHyphen: {
  983. Console.Write("-");
  984. break;
  985. }
  986. case Minor.Bullet: {
  987. Console.Write("*");
  988. break;
  989. }
  990. case Minor.EmDash: {
  991. Console.Write("—");
  992. break;
  993. }
  994. case Minor.EnDash: {
  995. Console.Write("–");
  996. break;
  997. }
  998. case Minor.LQuote: {
  999. Console.Write("‘");
  1000. break;
  1001. }
  1002. case Minor.RQuote: {
  1003. Console.Write("’");
  1004. break;
  1005. }
  1006. case Minor.LDblQuote: {
  1007. Console.Write("“");
  1008. break;
  1009. }
  1010. case Minor.RDblQuote: {
  1011. Console.Write("”");
  1012. break;
  1013. }
  1014. default: {
  1015. rtf.SkipGroup();
  1016. break;
  1017. }
  1018. }
  1019. }
  1020. private void HandleText(RTF.RTF rtf) {
  1021. if (rtf_skip_count > 0) {
  1022. rtf_skip_count--;
  1023. return;
  1024. }
  1025. if ((RTF.StandardCharCode)rtf.Minor != RTF.StandardCharCode.nothing) {
  1026. rtf_line.Append(rtf_text_map[(RTF.StandardCharCode)rtf.Minor]);
  1027. } else {
  1028. if ((int)rtf.Major > 31 && (int)rtf.Major < 128) {
  1029. rtf_line.Append((char)rtf.Major);
  1030. } else {
  1031. //rtf_line.Append((char)rtf.Major);
  1032. Console.Write("[Literal:0x{0:X2}]", (int)rtf.Major);
  1033. }
  1034. }
  1035. }
  1036. private void FlushText(RTF.RTF rtf, bool newline) {
  1037. int length;
  1038. Font font;
  1039. length = rtf_line.Length;
  1040. if (length == 0) {
  1041. return;
  1042. }
  1043. if (rtf_rtffont == null) {
  1044. // First font in table is default
  1045. rtf_rtffont = System.Windows.Forms.RTF.Font.GetFont(rtf, 0);
  1046. }
  1047. font = new Font(rtf_rtffont.Name, rtf_rtffont_size, rtf_rtfstyle);
  1048. if (rtf_color == null) {
  1049. System.Windows.Forms.RTF.Color color;
  1050. // First color in table is default
  1051. color = System.Windows.Forms.RTF.Color.GetColor(rtf, 0);
  1052. if ((color == null) || (color.Red == -1 && color.Green == -1 && color.Blue == -1)) {
  1053. rtf_color = new SolidBrush(ForeColor);
  1054. } else {
  1055. rtf_color = new SolidBrush(Color.FromArgb(color.Red, color.Green, color.Blue));
  1056. }
  1057. }
  1058. if (rtf_cursor_x == 0) {
  1059. document.Add(rtf_cursor_y, rtf_line.ToString(), rtf_rtfalign, font, rtf_color);
  1060. } else {
  1061. Line line;
  1062. line = document.GetLine(rtf_cursor_y);
  1063. document.InsertString(line, rtf_cursor_x, rtf_line.ToString());
  1064. document.FormatText(line, rtf_cursor_x + 1, line, rtf_cursor_x + 1 + length, font, rtf_color); // FormatText is 1-based
  1065. if (newline && (line.text.Length > (rtf_cursor_x + 1 + length))) {
  1066. document.Split(line, rtf_cursor_x + length);
  1067. }
  1068. }
  1069. if (newline) {
  1070. rtf_cursor_x = 0;
  1071. rtf_cursor_y++;
  1072. } else {
  1073. rtf_cursor_x += length;
  1074. }
  1075. rtf_line.Length = 0; // Empty line
  1076. }
  1077. private void InsertRTFFromStream(Stream data, int cursor_x, int cursor_y) {
  1078. int x;
  1079. int y;
  1080. InsertRTFFromStream(data, cursor_x, cursor_y, out x, out y);
  1081. }
  1082. private void InsertRTFFromStream(Stream data, int cursor_x, int cursor_y, out int to_x, out int to_y) {
  1083. RTF.RTF rtf;
  1084. rtf = new RTF.RTF(data);
  1085. // Prepare
  1086. rtf.ClassCallback[RTF.TokenClass.Text] = new RTF.ClassDelegate(HandleText);
  1087. rtf.ClassCallback[RTF.TokenClass.Control] = new RTF.ClassDelegate(HandleControl);
  1088. rtf_skip_width = 0;
  1089. rtf_skip_count = 0;
  1090. rtf_line = new StringBuilder();
  1091. rtf_color = null;
  1092. rtf_rtffont_size = (int)this.Font.Size;
  1093. rtf_rtfalign = HorizontalAlignment.Left;
  1094. rtf_rtfstyle = FontStyle.Regular;
  1095. rtf_rtffont = null;
  1096. rtf_cursor_x = cursor_x;
  1097. rtf_cursor_y = cursor_y;
  1098. rtf.DefaultFont(this.Font.Name);
  1099. rtf_text_map = new RTF.TextMap();
  1100. RTF.TextMap.SetupStandardTable(rtf_text_map.Table);
  1101. try {
  1102. rtf.Read(); // That's it
  1103. FlushText(rtf, false);
  1104. }
  1105. catch (RTF.RTFException) {
  1106. // Seems to be plain text...
  1107. }
  1108. to_x = rtf_cursor_x;
  1109. to_y = rtf_cursor_y;
  1110. document.RecalculateDocument(CreateGraphics(), cursor_y, document.Lines, false);
  1111. document.Invalidate(document.GetLine(cursor_y), 0, document.GetLine(document.Lines), -1);
  1112. }
  1113. private void RichTextBox_HScrolled(object sender, EventArgs e) {
  1114. OnHScroll(e);
  1115. }
  1116. private void RichTextBox_VScrolled(object sender, EventArgs e) {
  1117. OnVScroll(e);
  1118. }
  1119. private void PointToTagPos(Point pt, out LineTag tag, out int pos) {
  1120. Point p;
  1121. p = pt;
  1122. if (p.X >= document.ViewPortWidth) {
  1123. p.X = document.ViewPortWidth - 1;
  1124. } else if (p.X < 0) {
  1125. p.X = 0;
  1126. }
  1127. if (p.Y >= document.ViewPortHeight) {
  1128. p.Y = document.ViewPortHeight - 1;
  1129. } else if (p.Y < 0) {
  1130. p.Y = 0;
  1131. }
  1132. tag = document.FindCursor(p.X + document.ViewPortX, p.Y + document.ViewPortY, out pos);
  1133. }
  1134. private void EmitRTFFontProperties(StringBuilder rtf, int prev_index, int font_index, Font prev_font, Font font) {
  1135. if (prev_index != font_index) {
  1136. rtf.Append(String.Format("\\f{0}", font_index)); // Font table entry
  1137. }
  1138. if ((prev_font == null) || (prev_font.Size != font.Size)) {
  1139. rtf.Append(String.Format("\\fs{0}", (int)(font.Size * 2))); // Font size
  1140. }
  1141. if ((prev_font == null) || (font.Bold != prev_font.Bold)) {
  1142. if (font.Bold) {
  1143. rtf.Append("\\b");
  1144. } else {
  1145. if (prev_font != null) {
  1146. rtf.Append("\\b0");
  1147. }
  1148. }
  1149. }
  1150. if ((prev_font == null) || (font.Italic != prev_font.Italic)) {
  1151. if (font.Italic) {
  1152. rtf.Append("\\i");
  1153. } else {
  1154. if (prev_font != null) {
  1155. rtf.Append("\\i0");
  1156. }
  1157. }
  1158. }
  1159. if ((prev_font == null) || (font.Strikeout != prev_font.Strikeout)) {
  1160. if (font.Strikeout) {
  1161. rtf.Append("\\strike");
  1162. } else {
  1163. if (prev_font != null) {
  1164. rtf.Append("\\strike0");
  1165. }
  1166. }
  1167. }
  1168. if ((prev_font == null) || (font.Underline != prev_font.Underline)) {
  1169. if (font.Underline) {
  1170. rtf.Append("\\ul");
  1171. } else {
  1172. if (prev_font != null) {
  1173. rtf.Append("\\ul0");
  1174. }
  1175. }
  1176. }
  1177. }
  1178. [MonoTODO("Emit unicode and other special characters properly")]
  1179. private void EmitRTFText(StringBuilder rtf, string text) {
  1180. rtf.Append(text);
  1181. }
  1182. // start_pos and end_pos are 0-based
  1183. private StringBuilder GenerateRTF(Line start_line, int start_pos, Line end_line, int end_pos) {
  1184. StringBuilder sb;
  1185. ArrayList fonts;
  1186. ArrayList colors;
  1187. Color color;
  1188. Font font;
  1189. Line line;
  1190. LineTag tag;
  1191. int pos;
  1192. int line_no;
  1193. int line_len;
  1194. int i;
  1195. int length;
  1196. sb = new StringBuilder();
  1197. fonts = new ArrayList(10);
  1198. colors = new ArrayList(10);
  1199. // Two runs, first we parse to determine tables;
  1200. // and unlike most of our processing here we work on tags
  1201. line = start_line;
  1202. line_no = start_line.line_no;
  1203. pos = start_pos;
  1204. // Add default font and color; to optimize document content we don't
  1205. // use this.Font and this.ForeColor but the font/color from the first tag
  1206. tag = LineTag.FindTag(start_line, pos);
  1207. font = tag.font;
  1208. color = ((SolidBrush)tag.color).Color;
  1209. fonts.Add(font.Name);
  1210. colors.Add(color);
  1211. while (line_no <= end_line.line_no) {
  1212. line = document.GetLine(line_no);
  1213. tag = LineTag.FindTag(line, pos);
  1214. if (line_no != end_line.line_no) {
  1215. line_len = line.text.Length;
  1216. } else {
  1217. line_len = end_pos;
  1218. }
  1219. while (pos < line_len) {
  1220. if (tag.font.Name != font.Name) {
  1221. font = tag.font;
  1222. if (!fonts.Contains(font.Name)) {
  1223. fonts.Add(font.Name);
  1224. }
  1225. }
  1226. if (((SolidBrush)tag.color).Color != color) {
  1227. color = ((SolidBrush)tag.color).Color;
  1228. if (!colors.Contains(color)) {
  1229. colors.Add(color);
  1230. }
  1231. }
  1232. pos = tag.start + tag.length - 1;
  1233. tag = tag.next;
  1234. }
  1235. pos = 0;
  1236. line_no++;
  1237. }
  1238. // We have the tables, emit the header
  1239. sb.Append("{\\rtf1\\ansi");
  1240. sb.Append("\\ansicpg1252"); // FIXME - is this correct?
  1241. // Default Font
  1242. sb.Append(String.Format("\\deff{0}", fonts.IndexOf(this.Font.Name)));
  1243. // Default Language
  1244. sb.Append("\\deflang1033\n"); // FIXME - always 1033?
  1245. // Emit the font table
  1246. sb.Append("{\\fonttbl");
  1247. for (i = 0; i < fonts.Count; i++) {
  1248. sb.Append(String.Format("{{\\f{0}", i)); // {Font
  1249. sb.Append("\\fnil"); // Family
  1250. sb.Append("\\fcharset0 "); // Charset ANSI<space>
  1251. sb.Append((string)fonts[i]); // Font name
  1252. sb.Append(";}"); // }
  1253. }
  1254. sb.Append("}\n");
  1255. // Emit the color table (if needed)
  1256. 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))) {
  1257. sb.Append("{\\colortbl "); // Header and NO! default color
  1258. for (i = 0; i < colors.Count; i++) {
  1259. sb.Append(String.Format("\\red{0}", ((Color)colors[i]).R));
  1260. sb.Append(String.Format("\\green{0}", ((Color)colors[i]).G));
  1261. sb.Append(String.Format("\\blue{0}", ((Color)colors[i]).B));
  1262. sb.Append(";");
  1263. }
  1264. sb.Append("}\n");
  1265. }
  1266. sb.Append("{\\*\\generator Mono RichTextBox;}");
  1267. // Emit initial paragraph settings
  1268. tag = LineTag.FindTag(start_line, start_pos);
  1269. sb.Append("\\pard"); // Reset to default paragraph properties
  1270. EmitRTFFontProperties(sb, -1, fonts.IndexOf(tag.font.Name), null, tag.font); // Font properties
  1271. sb.Append(" "); // Space separator
  1272. font = tag.font;
  1273. color = (Color)colors[0];
  1274. line = start_line;
  1275. line_no = start_line.line_no;
  1276. pos = start_pos;
  1277. while (line_no <= end_line.line_no) {
  1278. line = document.GetLine(line_no);
  1279. tag = LineTag.FindTag(line, pos);
  1280. if (line_no != end_line.line_no) {
  1281. line_len = line.text.Length;
  1282. } else {
  1283. line_len = end_pos;
  1284. }
  1285. while (pos < line_len) {
  1286. length = sb.Length;
  1287. if (tag.font != font) {
  1288. EmitRTFFontProperties(sb, fonts.IndexOf(font.Name), fonts.IndexOf(tag.font.Name), font, tag.font);
  1289. font = tag.font;
  1290. }
  1291. if (((SolidBrush)tag.color).Color != color) {
  1292. color = ((SolidBrush)tag.color).Color;
  1293. sb.Append(String.Format("\\cf{0}", colors.IndexOf(color)));
  1294. }
  1295. if (length != sb.Length) {
  1296. sb.Append(" "); // Emit space to separate keywords from text
  1297. }
  1298. // Emit the string itself
  1299. if (line_no != end_line.line_no) {
  1300. EmitRTFText(sb, tag.line.text.ToString(pos, tag.start + tag.length - pos - 1));
  1301. } else {
  1302. if (end_pos < (tag.start + tag.length - 1)) {
  1303. // Emit partial tag only, end_pos is inside this tag
  1304. EmitRTFText(sb, tag.line.text.ToString(pos, end_pos - pos));
  1305. } else {
  1306. EmitRTFText(sb, tag.line.text.ToString(pos, tag.start + tag.length - pos - 1));
  1307. }
  1308. }
  1309. pos = tag.start + tag.length - 1;
  1310. tag = tag.next;
  1311. }
  1312. if (pos >= line.text.Length) {
  1313. if (!line.soft_break) {
  1314. sb.Append("\\par\n");
  1315. }
  1316. }
  1317. pos = 0;
  1318. line_no++;
  1319. }
  1320. sb.Append("}\n");
  1321. return sb;
  1322. }
  1323. #endregion // Private Methods
  1324. }
  1325. }