RichTextBox.cs 41 KB

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