RichTextBox.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  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 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. int sel_start;
  357. int sel_end;
  358. sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
  359. sel_end = document.LineTagToCharIndex(document.selection_end.line, document.selection_end.pos);
  360. Console.WriteLine("FIXME - SelectionColor should not alter font");
  361. document.FormatText(document.selection_start.line, document.selection_start.pos + 1, document.selection_end.line, document.selection_end.pos, document.selection_start.tag.font, new SolidBrush(value));
  362. document.CharIndexToLineTag(sel_start, out document.selection_start.line, out document.selection_start.tag, out document.selection_start.pos);
  363. document.CharIndexToLineTag(sel_end, out document.selection_end.line, out document.selection_end.tag, out document.selection_end.pos);
  364. document.UpdateView(document.selection_start.line, 0);
  365. document.AlignCaret();
  366. }
  367. }
  368. [Browsable(false)]
  369. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  370. public Font SelectionFont {
  371. get {
  372. Font font;
  373. LineTag start;
  374. LineTag end;
  375. LineTag tag;
  376. start = document.selection_start.tag;
  377. end = document.selection_end.tag;
  378. font = document.selection_start.tag.font;
  379. tag = start;
  380. while (true) {
  381. if (!font.Equals(tag.font)) {
  382. return null;
  383. }
  384. if (tag == end) {
  385. break;
  386. }
  387. tag = document.NextTag(tag);
  388. if (tag == null) {
  389. break;
  390. }
  391. }
  392. return font;
  393. }
  394. set {
  395. int sel_start;
  396. int sel_end;
  397. sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
  398. sel_end = document.LineTagToCharIndex(document.selection_end.line, document.selection_end.pos);
  399. document.FormatText(document.selection_start.line, document.selection_start.pos + 1, document.selection_end.line, document.selection_end.pos + 1, value, document.selection_start.tag.color);
  400. document.CharIndexToLineTag(sel_start, out document.selection_start.line, out document.selection_start.tag, out document.selection_start.pos);
  401. document.CharIndexToLineTag(sel_end, out document.selection_end.line, out document.selection_end.tag, out document.selection_end.pos);
  402. document.UpdateView(document.selection_start.line, 0);
  403. document.AlignCaret();
  404. }
  405. }
  406. [Browsable(false)]
  407. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  408. public override int SelectionLength {
  409. get {
  410. return base.SelectionLength;
  411. }
  412. set {
  413. base.SelectionLength = value;
  414. }
  415. }
  416. [Browsable(false)]
  417. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  418. public RichTextBoxSelectionTypes SelectionType {
  419. get {
  420. if (document.selection_start == document.selection_end) {
  421. return RichTextBoxSelectionTypes.Empty;
  422. }
  423. // Lazy, but works
  424. if (SelectedText.Length > 1) {
  425. return RichTextBoxSelectionTypes.MultiChar | RichTextBoxSelectionTypes.Text;
  426. }
  427. return RichTextBoxSelectionTypes.Text;
  428. }
  429. }
  430. [Localizable(true)]
  431. public override string Text {
  432. get {
  433. return base.Text;
  434. }
  435. set {
  436. base.Text = value;
  437. }
  438. }
  439. [Browsable(false)]
  440. public override int TextLength {
  441. get {
  442. return base.TextLength;
  443. }
  444. }
  445. [Browsable(false)]
  446. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  447. public string UndoActionName {
  448. get {
  449. return document.undo.UndoName;
  450. }
  451. }
  452. [Localizable(true)]
  453. [DefaultValue(1)]
  454. public float ZoomFactor {
  455. get {
  456. return zoom;
  457. }
  458. set {
  459. zoom = value;
  460. }
  461. }
  462. #endregion // Public Instance Properties
  463. #region Protected Instance Properties
  464. protected override CreateParams CreateParams {
  465. get {
  466. return base.CreateParams;
  467. }
  468. }
  469. protected override Size DefaultSize {
  470. get {
  471. return new Size(100, 96);
  472. }
  473. }
  474. #endregion // Protected Instance Properties
  475. #region Public Instance Methods
  476. public bool CanPaste(DataFormats.Format clipFormat) {
  477. if ((clipFormat.Name == DataFormats.Rtf) ||
  478. (clipFormat.Name == DataFormats.Text) ||
  479. (clipFormat.Name == DataFormats.UnicodeText)) {
  480. return true;
  481. }
  482. return false;
  483. }
  484. public int Find(char[] characterSet) {
  485. return Find(characterSet, -1, -1);
  486. }
  487. public int Find(char[] characterSet, int start) {
  488. return Find(characterSet, start, -1);
  489. }
  490. public int Find(char[] characterSet, int start, int end) {
  491. Document.Marker start_mark;
  492. Document.Marker end_mark;
  493. Document.Marker result;
  494. if (start == -1) {
  495. document.GetMarker(out start_mark, true);
  496. } else {
  497. Line line;
  498. LineTag tag;
  499. int pos;
  500. start_mark = new Document.Marker();
  501. document.CharIndexToLineTag(start, out line, out tag, out pos);
  502. start_mark.line = line;
  503. start_mark.tag = tag;
  504. start_mark.pos = pos;
  505. }
  506. if (end == -1) {
  507. document.GetMarker(out end_mark, false);
  508. } else {
  509. Line line;
  510. LineTag tag;
  511. int pos;
  512. end_mark = new Document.Marker();
  513. document.CharIndexToLineTag(end, out line, out tag, out pos);
  514. end_mark.line = line;
  515. end_mark.tag = tag;
  516. end_mark.pos = pos;
  517. }
  518. if (document.FindChars(characterSet, start_mark, end_mark, out result)) {
  519. return document.LineTagToCharIndex(result.line, result.pos);
  520. }
  521. return -1;
  522. }
  523. public int Find(string str) {
  524. return Find(str, -1, -1, RichTextBoxFinds.None);
  525. }
  526. public int Find(string str, int start, int end, RichTextBoxFinds options) {
  527. Document.Marker start_mark;
  528. Document.Marker end_mark;
  529. Document.Marker result;
  530. if (start == -1) {
  531. document.GetMarker(out start_mark, true);
  532. } else {
  533. Line line;
  534. LineTag tag;
  535. int pos;
  536. start_mark = new Document.Marker();
  537. document.CharIndexToLineTag(start, out line, out tag, out pos);
  538. start_mark.line = line;
  539. start_mark.tag = tag;
  540. start_mark.pos = pos;
  541. }
  542. if (end == -1) {
  543. document.GetMarker(out end_mark, false);
  544. } else {
  545. Line line;
  546. LineTag tag;
  547. int pos;
  548. end_mark = new Document.Marker();
  549. document.CharIndexToLineTag(end, out line, out tag, out pos);
  550. end_mark.line = line;
  551. end_mark.tag = tag;
  552. end_mark.pos = pos;
  553. }
  554. if (document.Find(str, start_mark, end_mark, out result, options)) {
  555. return document.LineTagToCharIndex(result.line, result.pos);
  556. }
  557. return -1;
  558. }
  559. public int Find(string str, int start, RichTextBoxFinds options) {
  560. return Find(str, start, -1, options);
  561. }
  562. public int Find(string str, RichTextBoxFinds options) {
  563. return Find(str, -1, -1, options);
  564. }
  565. public char GetCharFromPosition(Point pt) {
  566. LineTag tag;
  567. int pos;
  568. PointToTagPos(pt, out tag, out pos);
  569. if (pos >= tag.line.text.Length) {
  570. return '\n';
  571. }
  572. return tag.line.text[pos];
  573. }
  574. public int GetCharIndexFromPosition(Point pt) {
  575. LineTag tag;
  576. int pos;
  577. PointToTagPos(pt, out tag, out pos);
  578. return document.LineTagToCharIndex(tag.line, pos);
  579. }
  580. public int GetLineFromCharIndex(int index) {
  581. Line line;
  582. LineTag tag;
  583. int pos;
  584. document.CharIndexToLineTag(index, out line, out tag, out pos);
  585. return line.LineNo - 1;
  586. }
  587. public Point GetPositionFromCharIndex(int index) {
  588. Line line;
  589. LineTag tag;
  590. int pos;
  591. document.CharIndexToLineTag(index, out line, out tag, out pos);
  592. return new Point((int)line.widths[pos] + 1, line.Y + 1);
  593. }
  594. public void LoadFile(System.IO.Stream data, RichTextBoxStreamType fileType) {
  595. RTF.RTF rtf; // Not 'using SWF.RTF' to avoid ambiguities with font and color
  596. document.Empty();
  597. // FIXME - ignoring unicode
  598. if (fileType == RichTextBoxStreamType.PlainText) {
  599. StringBuilder sb;
  600. int count;
  601. byte[] buffer;
  602. try {
  603. sb = new StringBuilder((int)data.Length);
  604. buffer = new byte[1024];
  605. }
  606. catch {
  607. throw new IOException("Not enough memory to load document");
  608. }
  609. count = 0;
  610. while (count < data.Length) {
  611. count += data.Read(buffer, count, 1024);
  612. sb.Append(buffer);
  613. }
  614. base.Text = sb.ToString();
  615. return;
  616. }
  617. InsertRTFFromStream(data, 0, 1);
  618. }
  619. [MonoTODO("Make smarter RTF detection?")]
  620. public void LoadFile(string path) {
  621. if (path.EndsWith(".rtf")) {
  622. LoadFile(path, RichTextBoxStreamType.RichText);
  623. } else {
  624. LoadFile(path, RichTextBoxStreamType.PlainText);
  625. }
  626. }
  627. public void LoadFile(string path, RichTextBoxStreamType fileType) {
  628. FileStream data;
  629. data = null;
  630. try {
  631. data = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1024);
  632. LoadFile(data, fileType);
  633. }
  634. catch {
  635. throw new IOException("Could not open file " + path);
  636. }
  637. finally {
  638. if (data != null) {
  639. data.Close();
  640. }
  641. }
  642. }
  643. public void Paste(DataFormats.Format clipFormat) {
  644. base.Paste(clipFormat, false);
  645. }
  646. [MonoTODO()]
  647. public void Redo() {
  648. }
  649. public void SaveFile(Stream data, RichTextBoxStreamType fileType) {
  650. Encoding encoding;
  651. int i;
  652. Byte[] bytes;
  653. if (fileType == RichTextBoxStreamType.UnicodePlainText) {
  654. encoding = Encoding.Unicode;
  655. } else {
  656. encoding = Encoding.ASCII;
  657. }
  658. switch(fileType) {
  659. case RichTextBoxStreamType.PlainText:
  660. case RichTextBoxStreamType.TextTextOleObjs:
  661. case RichTextBoxStreamType.UnicodePlainText: {
  662. if (!multiline) {
  663. bytes = encoding.GetBytes(document.Root.text.ToString());
  664. data.Write(bytes, 0, bytes.Length);
  665. return;
  666. }
  667. for (i = 1; i < document.Lines; i++) {
  668. bytes = encoding.GetBytes(document.GetLine(i).text.ToString() + Environment.NewLine);
  669. data.Write(bytes, 0, bytes.Length);
  670. }
  671. bytes = encoding.GetBytes(document.GetLine(document.Lines).text.ToString());
  672. data.Write(bytes, 0, bytes.Length);
  673. return;
  674. }
  675. }
  676. // If we're here we're saving RTF
  677. Line start_line;
  678. Line end_line;
  679. StringBuilder rtf;
  680. int current;
  681. int total;
  682. start_line = document.GetLine(1);
  683. end_line = document.GetLine(document.Lines);
  684. rtf = GenerateRTF(start_line, 0, end_line, end_line.text.Length);
  685. total = rtf.Length;
  686. bytes = new Byte[4096];
  687. // Let's chunk it so we don't use up all memory...
  688. for (i = 0; i < total; i += 1024) {
  689. if ((i + 1024) < total) {
  690. current = encoding.GetBytes(rtf.ToString(i, 1024), 0, 1024, bytes, 0);
  691. } else {
  692. current = total - i;
  693. current = encoding.GetBytes(rtf.ToString(i, current), 0, current, bytes, 0);
  694. }
  695. data.Write(bytes, 0, current);
  696. }
  697. }
  698. public void SaveFile(string path) {
  699. if (path.EndsWith(".rtf")) {
  700. SaveFile(path, RichTextBoxStreamType.RichText);
  701. } else {
  702. SaveFile(path, RichTextBoxStreamType.PlainText);
  703. }
  704. }
  705. public void SaveFile(string path, RichTextBoxStreamType fileType) {
  706. FileStream data;
  707. data = null;
  708. // try {
  709. data = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, 1024, false);
  710. SaveFile(data, fileType);
  711. // }
  712. // catch {
  713. // throw new IOException("Could not write document to file " + path);
  714. // }
  715. // finally {
  716. if (data != null) {
  717. data.Close();
  718. }
  719. // }
  720. }
  721. #endregion // Public Instance Methods
  722. #region Protected Instance Methods
  723. protected virtual object CreateRichEditOleCallback() {
  724. throw new NotImplementedException();
  725. }
  726. protected override void OnBackColorChanged(EventArgs e) {
  727. base.OnBackColorChanged (e);
  728. }
  729. protected virtual void OnContentsResized(ContentsResizedEventArgs e) {
  730. if (ContentsResized != null) {
  731. ContentsResized(this, e);
  732. }
  733. }
  734. protected override void OnContextMenuChanged(EventArgs e) {
  735. base.OnContextMenuChanged (e);
  736. }
  737. protected override void OnHandleCreated(EventArgs e) {
  738. base.OnHandleCreated (e);
  739. }
  740. protected override void OnHandleDestroyed(EventArgs e) {
  741. base.OnHandleDestroyed (e);
  742. }
  743. protected virtual void OnHScroll(EventArgs e) {
  744. if (HScroll != null) {
  745. HScroll(this, e);
  746. }
  747. }
  748. [MonoTODO("Determine when to call this")]
  749. protected virtual void OnImeChange(EventArgs e) {
  750. if (ImeChange != null) {
  751. ImeChange(this, e);
  752. }
  753. }
  754. protected virtual void OnLinkClicked(LinkClickedEventArgs e) {
  755. if (LinkClicked != null) {
  756. LinkClicked(this, e);
  757. }
  758. }
  759. protected virtual void OnProtected(EventArgs e) {
  760. if (Protected != null) {
  761. Protected(this, e);
  762. }
  763. }
  764. protected override void OnRightToLeftChanged(EventArgs e) {
  765. base.OnRightToLeftChanged (e);
  766. }
  767. protected virtual void OnSelectionChanged(EventArgs e) {
  768. if (SelectionChanged != null) {
  769. SelectionChanged(this, e);
  770. }
  771. }
  772. protected override void OnSystemColorsChanged(EventArgs e) {
  773. base.OnSystemColorsChanged (e);
  774. }
  775. protected override void OnTextChanged(EventArgs e) {
  776. base.OnTextChanged (e);
  777. }
  778. protected virtual void OnVScroll(EventArgs e) {
  779. if (VScroll != null) {
  780. VScroll(this, e);
  781. }
  782. }
  783. protected override void WndProc(ref Message m) {
  784. base.WndProc (ref m);
  785. }
  786. #endregion // Protected Instance Methods
  787. #region Events
  788. [Browsable(false)]
  789. [EditorBrowsable(EditorBrowsableState.Never)]
  790. public event EventHandler BackgroundImageChanged;
  791. public event ContentsResizedEventHandler ContentsResized;
  792. [Browsable(false)]
  793. [EditorBrowsable(EditorBrowsableState.Never)]
  794. public event EventHandler DoubleClick;
  795. [Browsable(false)]
  796. [EditorBrowsable(EditorBrowsableState.Never)]
  797. public event DragEventHandler DragDrop {
  798. add {
  799. base.DragDrop += value;
  800. }
  801. remove {
  802. base.DragDrop -= value;
  803. }
  804. }
  805. [Browsable(false)]
  806. [EditorBrowsable(EditorBrowsableState.Never)]
  807. public event DragEventHandler DragEnter {
  808. add {
  809. base.DragEnter += value;
  810. }
  811. remove {
  812. base.DragEnter -= value;
  813. }
  814. }
  815. [Browsable(false)]
  816. [EditorBrowsable(EditorBrowsableState.Never)]
  817. public event EventHandler DragLeave {
  818. add {
  819. base.DragLeave += value;
  820. }
  821. remove {
  822. base.DragLeave -= value;
  823. }
  824. }
  825. [Browsable(false)]
  826. [EditorBrowsable(EditorBrowsableState.Never)]
  827. public event DragEventHandler DragOver {
  828. add {
  829. base.DragOver += value;
  830. }
  831. remove {
  832. base.DragOver -= value;
  833. }
  834. }
  835. [Browsable(false)]
  836. [EditorBrowsable(EditorBrowsableState.Never)]
  837. public event GiveFeedbackEventHandler GiveFeedback;
  838. public event EventHandler HScroll;
  839. public event EventHandler ImeChange;
  840. public event LinkClickedEventHandler LinkClicked;
  841. public event EventHandler Protected;
  842. [Browsable(false)]
  843. [EditorBrowsable(EditorBrowsableState.Never)]
  844. public event QueryContinueDragEventHandler QueryContinueDrag;
  845. public event EventHandler SelectionChanged;
  846. public event EventHandler VScroll;
  847. #endregion // Events
  848. #region Private Methods
  849. private void HandleControl(RTF.RTF rtf) {
  850. switch(rtf.Major) {
  851. case RTF.Major.Unicode: {
  852. switch(rtf.Minor) {
  853. case Minor.UnicodeCharBytes: {
  854. rtf_skip_width = rtf.Param;
  855. break;
  856. }
  857. case Minor.UnicodeChar: {
  858. rtf_skip_count += rtf_skip_width;
  859. rtf_line.Append((char)rtf.Param);
  860. break;
  861. }
  862. }
  863. break;
  864. }
  865. case RTF.Major.Destination: {
  866. Console.Write("[Got Destination control {0}]", rtf.Minor);
  867. rtf.SkipGroup();
  868. break;
  869. }
  870. case RTF.Major.CharAttr: {
  871. switch(rtf.Minor) {
  872. case Minor.ForeColor: {
  873. System.Windows.Forms.RTF.Color color;
  874. color = System.Windows.Forms.RTF.Color.GetColor(rtf, rtf.Param);
  875. if (color != null) {
  876. FlushText(rtf, false);
  877. if (color.Red == -1 && color.Green == -1 && color.Blue == -1) {
  878. this.rtf_color = new SolidBrush(ForeColor);
  879. } else {
  880. this.rtf_color = new SolidBrush(Color.FromArgb(color.Red, color.Green, color.Blue));
  881. }
  882. }
  883. break;
  884. }
  885. case Minor.FontSize: {
  886. FlushText(rtf, false);
  887. this.rtf_rtffont_size = rtf.Param / 2;
  888. break;
  889. }
  890. case Minor.FontNum: {
  891. System.Windows.Forms.RTF.Font font;
  892. font = System.Windows.Forms.RTF.Font.GetFont(rtf, rtf.Param);
  893. if (font != null) {
  894. FlushText(rtf, false);
  895. this.rtf_rtffont = font;
  896. }
  897. break;
  898. }
  899. case Minor.Plain: {
  900. FlushText(rtf, false);
  901. rtf_rtfstyle = FontStyle.Regular;
  902. break;
  903. }
  904. case Minor.Bold: {
  905. FlushText(rtf, false);
  906. if (rtf.Param == RTF.RTF.NoParam) {
  907. rtf_rtfstyle |= FontStyle.Bold;
  908. } else {
  909. rtf_rtfstyle &= ~FontStyle.Bold;
  910. }
  911. break;
  912. }
  913. case Minor.Italic: {
  914. FlushText(rtf, false);
  915. if (rtf.Param == RTF.RTF.NoParam) {
  916. rtf_rtfstyle |= FontStyle.Italic;
  917. } else {
  918. rtf_rtfstyle &= ~FontStyle.Italic;
  919. }
  920. break;
  921. }
  922. case Minor.StrikeThru: {
  923. FlushText(rtf, false);
  924. if (rtf.Param == RTF.RTF.NoParam) {
  925. rtf_rtfstyle |= FontStyle.Strikeout;
  926. } else {
  927. rtf_rtfstyle &= ~FontStyle.Strikeout;
  928. }
  929. break;
  930. }
  931. case Minor.Underline: {
  932. FlushText(rtf, false);
  933. if (rtf.Param == RTF.RTF.NoParam) {
  934. rtf_rtfstyle |= FontStyle.Underline;
  935. } else {
  936. rtf_rtfstyle = rtf_rtfstyle & ~FontStyle.Underline;
  937. }
  938. break;
  939. }
  940. case Minor.NoUnderline: {
  941. FlushText(rtf, false);
  942. rtf_rtfstyle &= ~FontStyle.Underline;
  943. break;
  944. }
  945. }
  946. break;
  947. }
  948. case RTF.Major.SpecialChar: {
  949. Console.Write("[Got SpecialChar control {0}]", rtf.Minor);
  950. SpecialChar(rtf);
  951. break;
  952. }
  953. }
  954. }
  955. private void SpecialChar(RTF.RTF rtf) {
  956. switch(rtf.Minor) {
  957. case Minor.Page:
  958. case Minor.Sect:
  959. case Minor.Row:
  960. case Minor.Line:
  961. case Minor.Par: {
  962. FlushText(rtf, true);
  963. break;
  964. }
  965. case Minor.Cell: {
  966. Console.Write(" ");
  967. break;
  968. }
  969. case Minor.NoBrkSpace: {
  970. Console.Write(" ");
  971. break;
  972. }
  973. case Minor.Tab: {
  974. Console.Write("\t");
  975. break;
  976. }
  977. case Minor.NoBrkHyphen: {
  978. Console.Write("-");
  979. break;
  980. }
  981. case Minor.Bullet: {
  982. Console.Write("*");
  983. break;
  984. }
  985. case Minor.EmDash: {
  986. Console.Write("—");
  987. break;
  988. }
  989. case Minor.EnDash: {
  990. Console.Write("–");
  991. break;
  992. }
  993. case Minor.LQuote: {
  994. Console.Write("‘");
  995. break;
  996. }
  997. case Minor.RQuote: {
  998. Console.Write("’");
  999. break;
  1000. }
  1001. case Minor.LDblQuote: {
  1002. Console.Write("“");
  1003. break;
  1004. }
  1005. case Minor.RDblQuote: {
  1006. Console.Write("”");
  1007. break;
  1008. }
  1009. default: {
  1010. rtf.SkipGroup();
  1011. break;
  1012. }
  1013. }
  1014. }
  1015. private void HandleText(RTF.RTF rtf) {
  1016. if (rtf_skip_count > 0) {
  1017. rtf_skip_count--;
  1018. return;
  1019. }
  1020. if ((RTF.StandardCharCode)rtf.Minor != RTF.StandardCharCode.nothing) {
  1021. rtf_line.Append(rtf_text_map[(RTF.StandardCharCode)rtf.Minor]);
  1022. } else {
  1023. if ((int)rtf.Major > 31 && (int)rtf.Major < 128) {
  1024. rtf_line.Append((char)rtf.Major);
  1025. } else {
  1026. //rtf_line.Append((char)rtf.Major);
  1027. Console.Write("[Literal:0x{0:X2}]", (int)rtf.Major);
  1028. }
  1029. }
  1030. }
  1031. private void FlushText(RTF.RTF rtf, bool newline) {
  1032. int length;
  1033. Font font;
  1034. length = rtf_line.Length;
  1035. if (length == 0) {
  1036. return;
  1037. }
  1038. if (rtf_rtffont == null) {
  1039. // First font in table is default
  1040. rtf_rtffont = System.Windows.Forms.RTF.Font.GetFont(rtf, 0);
  1041. }
  1042. font = new Font(rtf_rtffont.Name, rtf_rtffont_size, rtf_rtfstyle);
  1043. if (rtf_color == null) {
  1044. System.Windows.Forms.RTF.Color color;
  1045. // First color in table is default
  1046. color = System.Windows.Forms.RTF.Color.GetColor(rtf, 0);
  1047. if ((color == null) || (color.Red == -1 && color.Green == -1 && color.Blue == -1)) {
  1048. rtf_color = new SolidBrush(ForeColor);
  1049. } else {
  1050. rtf_color = new SolidBrush(Color.FromArgb(color.Red, color.Green, color.Blue));
  1051. }
  1052. }
  1053. if (rtf_cursor_x == 0) {
  1054. document.Add(rtf_cursor_y, rtf_line.ToString(), rtf_rtfalign, font, rtf_color);
  1055. } else {
  1056. Line line;
  1057. line = document.GetLine(rtf_cursor_y);
  1058. document.InsertString(line, rtf_cursor_x, rtf_line.ToString());
  1059. document.FormatText(line, rtf_cursor_x + 1, line, rtf_cursor_x + 1 + length, font, rtf_color); // FormatText is 1-based
  1060. }
  1061. if (newline) {
  1062. rtf_cursor_x = 0;
  1063. rtf_cursor_y++;
  1064. } else {
  1065. rtf_cursor_x += length;
  1066. }
  1067. rtf_line.Length = 0; // Empty line
  1068. }
  1069. private void InsertRTFFromStream(Stream data, int cursor_x, int cursor_y) {
  1070. int x;
  1071. int y;
  1072. InsertRTFFromStream(data, cursor_x, cursor_y, out x, out y);
  1073. }
  1074. private void InsertRTFFromStream(Stream data, int cursor_x, int cursor_y, out int to_x, out int to_y) {
  1075. RTF.RTF rtf;
  1076. rtf = new RTF.RTF(data);
  1077. // Prepare
  1078. rtf.ClassCallback[RTF.TokenClass.Text] = new RTF.ClassDelegate(HandleText);
  1079. rtf.ClassCallback[RTF.TokenClass.Control] = new RTF.ClassDelegate(HandleControl);
  1080. rtf_skip_width = 0;
  1081. rtf_skip_count = 0;
  1082. rtf_line = new StringBuilder();
  1083. rtf_color = null;
  1084. rtf_rtffont_size = (int)this.Font.Size;
  1085. rtf_rtfalign = HorizontalAlignment.Left;
  1086. rtf_rtfstyle = FontStyle.Regular;
  1087. rtf_rtffont = null;
  1088. rtf_cursor_x = cursor_x;
  1089. rtf_cursor_y = cursor_y;
  1090. rtf.DefaultFont(this.Font.Name);
  1091. rtf_text_map = new RTF.TextMap();
  1092. RTF.TextMap.SetupStandardTable(rtf_text_map.Table);
  1093. try {
  1094. rtf.Read(); // That's it
  1095. FlushText(rtf, false);
  1096. }
  1097. catch (RTF.RTFException) {
  1098. // Seems to be plain text...
  1099. }
  1100. to_x = rtf_cursor_x;
  1101. to_y = rtf_cursor_y;
  1102. document.RecalculateDocument(CreateGraphics(), cursor_y, document.Lines, false);
  1103. document.Invalidate(document.GetLine(cursor_y), 0, document.GetLine(document.Lines), -1);
  1104. }
  1105. private void RichTextBox_HScrolled(object sender, EventArgs e) {
  1106. OnHScroll(e);
  1107. }
  1108. private void RichTextBox_VScrolled(object sender, EventArgs e) {
  1109. OnVScroll(e);
  1110. }
  1111. private void PointToTagPos(Point pt, out LineTag tag, out int pos) {
  1112. Point p;
  1113. p = pt;
  1114. if (p.X >= document.ViewPortWidth) {
  1115. p.X = document.ViewPortWidth - 1;
  1116. } else if (p.X < 0) {
  1117. p.X = 0;
  1118. }
  1119. if (p.Y >= document.ViewPortHeight) {
  1120. p.Y = document.ViewPortHeight - 1;
  1121. } else if (p.Y < 0) {
  1122. p.Y = 0;
  1123. }
  1124. tag = document.FindCursor(p.X + document.ViewPortX, p.Y + document.ViewPortY, out pos);
  1125. }
  1126. private void EmitRTFFontProperties(StringBuilder rtf, int prev_index, int font_index, Font prev_font, Font font) {
  1127. if (prev_index != font_index) {
  1128. rtf.Append(String.Format("\\f{0}", font_index)); // Font table entry
  1129. }
  1130. if ((prev_font == null) || (prev_font.Size != font.Size)) {
  1131. rtf.Append(String.Format("\\fs{0}", (int)(font.Size * 2))); // Font size
  1132. }
  1133. if ((prev_font == null) || (font.Bold != prev_font.Bold)) {
  1134. if (font.Bold) {
  1135. rtf.Append("\\b");
  1136. } else {
  1137. if (prev_font != null) {
  1138. rtf.Append("\\b0");
  1139. }
  1140. }
  1141. }
  1142. if ((prev_font == null) || (font.Italic != prev_font.Italic)) {
  1143. if (font.Italic) {
  1144. rtf.Append("\\i");
  1145. } else {
  1146. if (prev_font != null) {
  1147. rtf.Append("\\i0");
  1148. }
  1149. }
  1150. }
  1151. if ((prev_font == null) || (font.Strikeout != prev_font.Strikeout)) {
  1152. if (font.Strikeout) {
  1153. rtf.Append("\\strike");
  1154. } else {
  1155. if (prev_font != null) {
  1156. rtf.Append("\\strike0");
  1157. }
  1158. }
  1159. }
  1160. if ((prev_font == null) || (font.Underline != prev_font.Underline)) {
  1161. if (font.Underline) {
  1162. rtf.Append("\\ul");
  1163. } else {
  1164. if (prev_font != null) {
  1165. rtf.Append("\\ul0");
  1166. }
  1167. }
  1168. }
  1169. }
  1170. [MonoTODO("Emit unicode and other special characters properly")]
  1171. private void EmitRTFText(StringBuilder rtf, string text) {
  1172. rtf.Append(text);
  1173. }
  1174. // start_pos and end_pos are 0-based
  1175. private StringBuilder GenerateRTF(Line start_line, int start_pos, Line end_line, int end_pos) {
  1176. StringBuilder sb;
  1177. ArrayList fonts;
  1178. ArrayList colors;
  1179. Color color;
  1180. Font font;
  1181. Line line;
  1182. LineTag tag;
  1183. int pos;
  1184. int line_no;
  1185. int line_len;
  1186. int i;
  1187. int length;
  1188. sb = new StringBuilder();
  1189. fonts = new ArrayList(10);
  1190. colors = new ArrayList(10);
  1191. // Two runs, first we parse to determine tables;
  1192. // and unlike most of our processing here we work on tags
  1193. line = start_line;
  1194. line_no = start_line.line_no;
  1195. pos = start_pos;
  1196. // Add default font and color; to optimize document content we don't
  1197. // use this.Font and this.ForeColor but the font/color from the first tag
  1198. tag = LineTag.FindTag(start_line, pos);
  1199. font = tag.font;
  1200. color = ((SolidBrush)tag.color).Color;
  1201. fonts.Add(font.Name);
  1202. colors.Add(color);
  1203. while (line_no <= end_line.line_no) {
  1204. line = document.GetLine(line_no);
  1205. tag = LineTag.FindTag(line, pos);
  1206. if (line_no != end_line.line_no) {
  1207. line_len = line.text.Length;
  1208. } else {
  1209. line_len = end_pos;
  1210. }
  1211. while (pos < line_len) {
  1212. if (tag.font.Name != font.Name) {
  1213. font = tag.font;
  1214. if (!fonts.Contains(font.Name)) {
  1215. fonts.Add(font.Name);
  1216. }
  1217. }
  1218. if (((SolidBrush)tag.color).Color != color) {
  1219. color = ((SolidBrush)tag.color).Color;
  1220. if (!colors.Contains(color)) {
  1221. colors.Add(color);
  1222. }
  1223. }
  1224. pos = tag.start + tag.length - 1;
  1225. tag = tag.next;
  1226. }
  1227. pos = 0;
  1228. line_no++;
  1229. }
  1230. // We have the tables, emit the header
  1231. sb.Append("{\\rtf1\\ansi");
  1232. sb.Append("\\ansicpg1252"); // FIXME - is this correct?
  1233. // Default Font
  1234. sb.Append(String.Format("\\deff{0}", fonts.IndexOf(this.Font.Name)));
  1235. // Default Language
  1236. sb.Append("\\deflang1033\n"); // FIXME - always 1033?
  1237. // Emit the font table
  1238. sb.Append("{\\fonttbl");
  1239. for (i = 0; i < fonts.Count; i++) {
  1240. sb.Append(String.Format("{{\\f{0}", i)); // {Font
  1241. sb.Append("\\fnil"); // Family
  1242. sb.Append("\\fcharset0 "); // Charset ANSI<space>
  1243. sb.Append((string)fonts[i]); // Font name
  1244. sb.Append(";}"); // }
  1245. }
  1246. sb.Append("}\n");
  1247. // Emit the color table (if needed)
  1248. 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))) {
  1249. sb.Append("{\\colortbl "); // Header and NO! default color
  1250. for (i = 0; i < colors.Count; i++) {
  1251. sb.Append(String.Format("\\red{0}", ((Color)colors[i]).R));
  1252. sb.Append(String.Format("\\green{0}", ((Color)colors[i]).G));
  1253. sb.Append(String.Format("\\blue{0}", ((Color)colors[i]).B));
  1254. sb.Append(";");
  1255. }
  1256. sb.Append("}\n");
  1257. }
  1258. sb.Append("{\\*\\generator Mono RichTextBox;}");
  1259. // Emit initial paragraph settings
  1260. tag = LineTag.FindTag(start_line, start_pos);
  1261. sb.Append("\\pard"); // Reset to default paragraph properties
  1262. EmitRTFFontProperties(sb, -1, fonts.IndexOf(tag.font.Name), null, tag.font); // Font properties
  1263. sb.Append(" "); // Space separator
  1264. font = tag.font;
  1265. color = (Color)colors[0];
  1266. line = start_line;
  1267. line_no = start_line.line_no;
  1268. pos = start_pos;
  1269. while (line_no <= end_line.line_no) {
  1270. line = document.GetLine(line_no);
  1271. tag = LineTag.FindTag(line, pos);
  1272. if (line_no != end_line.line_no) {
  1273. line_len = line.text.Length;
  1274. } else {
  1275. line_len = end_pos;
  1276. }
  1277. while (pos < line_len) {
  1278. length = sb.Length;
  1279. if (tag.font != font) {
  1280. EmitRTFFontProperties(sb, fonts.IndexOf(font.Name), fonts.IndexOf(tag.font.Name), font, tag.font);
  1281. font = tag.font;
  1282. }
  1283. if (((SolidBrush)tag.color).Color != color) {
  1284. color = ((SolidBrush)tag.color).Color;
  1285. sb.Append(String.Format("\\cf{0}", colors.IndexOf(color)));
  1286. }
  1287. if (length != sb.Length) {
  1288. sb.Append(" "); // Emit space to separate keywords from text
  1289. }
  1290. // Emit the string itself
  1291. if (line_no != end_line.line_no) {
  1292. EmitRTFText(sb, tag.line.text.ToString(pos, tag.start + tag.length - pos - 1));
  1293. } else {
  1294. if (end_pos < (tag.start + tag.length - 1)) {
  1295. // Emit partial tag only, end_pos is inside this tag
  1296. EmitRTFText(sb, tag.line.text.ToString(pos, end_pos - pos));
  1297. } else {
  1298. EmitRTFText(sb, tag.line.text.ToString(pos, tag.start + tag.length - pos - 1));
  1299. }
  1300. }
  1301. pos = tag.start + tag.length - 1;
  1302. tag = tag.next;
  1303. }
  1304. if (pos >= line.text.Length) {
  1305. if (!line.soft_break) {
  1306. sb.Append("\\par\n");
  1307. }
  1308. }
  1309. pos = 0;
  1310. line_no++;
  1311. }
  1312. sb.Append("}\n");
  1313. return sb;
  1314. }
  1315. #endregion // Private Methods
  1316. }
  1317. }