RichTextBox.cs 47 KB

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