RichTextBox.cs 42 KB

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