RichTextBox.cs 42 KB

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