RichTextBox.cs 42 KB

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