TextBoxBase.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  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) 2004-2006 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. //
  26. // NOT COMPLETE
  27. #undef Debug
  28. #undef DebugClick
  29. using System.ComponentModel;
  30. using System.ComponentModel.Design;
  31. using System.Drawing;
  32. using System.Drawing.Text;
  33. using System.Text;
  34. using System.Runtime.InteropServices;
  35. namespace System.Windows.Forms {
  36. [DefaultEvent("TextChanged")]
  37. [Designer("System.Windows.Forms.Design.TextBoxBaseDesigner, " + Consts.AssemblySystem_Design)]
  38. public abstract class TextBoxBase : Control {
  39. #region Local Variables
  40. internal HorizontalAlignment alignment;
  41. internal bool accepts_tab;
  42. internal bool accepts_return;
  43. internal bool auto_size;
  44. internal bool backcolor_set;
  45. internal CharacterCasing character_casing;
  46. internal bool undo;
  47. internal bool hide_selection;
  48. internal int max_length;
  49. internal bool modified;
  50. internal bool multiline;
  51. internal char password_char;
  52. internal bool read_only;
  53. internal bool word_wrap;
  54. internal Document document;
  55. internal LineTag caret_tag; // tag our cursor is in
  56. internal int caret_pos; // position on the line our cursor is in (can be 0 = beginning of line)
  57. internal ImplicitHScrollBar hscroll;
  58. internal ImplicitVScrollBar vscroll;
  59. internal RichTextBoxScrollBars scrollbars;
  60. internal bool richtext;
  61. internal bool show_selection; // set to true to always show selection, even if no focus is set
  62. internal int requested_height;
  63. internal int canvas_width;
  64. internal int canvas_height;
  65. static internal int track_width = 2; //
  66. static internal int track_border = 5; //
  67. internal DateTime click_last;
  68. internal CaretSelection click_mode;
  69. internal Bitmap bmp;
  70. #if Debug
  71. internal static bool draw_lines = false;
  72. #endif
  73. #endregion // Local Variables
  74. #region Internal Constructor
  75. // Constructor will go when complete, only for testing - pdb
  76. internal TextBoxBase() {
  77. alignment = HorizontalAlignment.Left;
  78. accepts_return = false;
  79. accepts_tab = false;
  80. auto_size = true;
  81. border_style = BorderStyle.Fixed3D;
  82. character_casing = CharacterCasing.Normal;
  83. undo = false;
  84. hide_selection = true;
  85. max_length = 32767;
  86. modified = false;
  87. multiline = false;
  88. password_char = '\0';
  89. read_only = false;
  90. word_wrap = true;
  91. richtext = false;
  92. show_selection = true;
  93. document = new Document(this);
  94. document.WidthChanged += new EventHandler(document_WidthChanged);
  95. document.HeightChanged += new EventHandler(document_HeightChanged);
  96. //document.CaretMoved += new EventHandler(CaretMoved);
  97. document.Wrap = false;
  98. requested_height = -1;
  99. click_last = DateTime.Now;
  100. click_mode = CaretSelection.Position;
  101. bmp = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  102. MouseDown += new MouseEventHandler(TextBoxBase_MouseDown);
  103. MouseUp += new MouseEventHandler(TextBoxBase_MouseUp);
  104. MouseMove += new MouseEventHandler(TextBoxBase_MouseMove);
  105. SizeChanged += new EventHandler(TextBoxBase_SizeChanged);
  106. FontChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
  107. ForeColorChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
  108. MouseWheel += new MouseEventHandler(TextBoxBase_MouseWheel);
  109. scrollbars = RichTextBoxScrollBars.None;
  110. hscroll = new ImplicitHScrollBar();
  111. hscroll.ValueChanged += new EventHandler(hscroll_ValueChanged);
  112. hscroll.control_style &= ~ControlStyles.Selectable;
  113. hscroll.Enabled = false;
  114. hscroll.Visible = false;
  115. vscroll = new ImplicitVScrollBar();
  116. vscroll.ValueChanged += new EventHandler(vscroll_ValueChanged);
  117. vscroll.control_style &= ~ControlStyles.Selectable;
  118. vscroll.Enabled = false;
  119. vscroll.Visible = false;
  120. SuspendLayout ();
  121. this.Controls.AddImplicit (hscroll);
  122. this.Controls.AddImplicit (vscroll);
  123. ResumeLayout ();
  124. //SetStyle(ControlStyles.ResizeRedraw, true);
  125. SetStyle(ControlStyles.UserPaint | ControlStyles.StandardClick, false);
  126. canvas_width = ClientSize.Width;
  127. canvas_height = ClientSize.Height;
  128. document.ViewPortWidth = canvas_width;
  129. document.ViewPortHeight = canvas_height;
  130. Cursor = Cursors.IBeam;
  131. CalculateScrollBars();
  132. }
  133. #endregion // Internal Constructor
  134. #region Private and Internal Methods
  135. internal string CaseAdjust(string s) {
  136. if (character_casing == CharacterCasing.Normal) {
  137. return s;
  138. }
  139. if (character_casing == CharacterCasing.Lower) {
  140. return s.ToLower();
  141. } else {
  142. return s.ToUpper();
  143. }
  144. }
  145. #endregion // Private and Internal Methods
  146. #region Public Instance Properties
  147. [DefaultValue(false)]
  148. [MWFCategory("Behavior")]
  149. public bool AcceptsTab {
  150. get {
  151. return accepts_tab;
  152. }
  153. set {
  154. if (value != accepts_tab) {
  155. accepts_tab = value;
  156. OnAcceptsTabChanged(EventArgs.Empty);
  157. }
  158. }
  159. }
  160. [DefaultValue(true)]
  161. [Localizable(true)]
  162. [RefreshProperties(RefreshProperties.Repaint)]
  163. [MWFCategory("Behavior")]
  164. public virtual bool AutoSize {
  165. get {
  166. return auto_size;
  167. }
  168. set {
  169. if (value != auto_size) {
  170. auto_size = value;
  171. if (auto_size) {
  172. if (PreferredHeight != ClientSize.Height) {
  173. ClientSize = new Size(ClientSize.Width, PreferredHeight);
  174. }
  175. }
  176. OnAutoSizeChanged(EventArgs.Empty);
  177. }
  178. }
  179. }
  180. [DispId(-501)]
  181. public override System.Drawing.Color BackColor {
  182. get {
  183. return base.BackColor;
  184. }
  185. set {
  186. if (value != ThemeEngine.Current.ColorWindow) {
  187. backcolor_set = true;
  188. } else {
  189. backcolor_set = false;
  190. }
  191. base.BackColor = value;
  192. }
  193. }
  194. [Browsable(false)]
  195. [EditorBrowsable(EditorBrowsableState.Never)]
  196. public override System.Drawing.Image BackgroundImage {
  197. get {
  198. return base.BackgroundImage;
  199. }
  200. set {
  201. base.BackgroundImage = value;
  202. }
  203. }
  204. [DefaultValue(BorderStyle.Fixed3D)]
  205. [DispId(-504)]
  206. [MWFCategory("Appearance")]
  207. public BorderStyle BorderStyle {
  208. get { return InternalBorderStyle; }
  209. set {
  210. InternalBorderStyle = value;
  211. OnBorderStyleChanged(EventArgs.Empty);
  212. }
  213. }
  214. [Browsable(false)]
  215. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  216. public bool CanUndo {
  217. get {
  218. return undo;
  219. }
  220. }
  221. [DispId(-513)]
  222. public override System.Drawing.Color ForeColor {
  223. get {
  224. return base.ForeColor;
  225. }
  226. set {
  227. base.ForeColor = value;
  228. }
  229. }
  230. [DefaultValue(true)]
  231. [MWFCategory("Behavior")]
  232. public bool HideSelection {
  233. get {
  234. return hide_selection;
  235. }
  236. set {
  237. if (value != hide_selection) {
  238. hide_selection = value;
  239. OnHideSelectionChanged(EventArgs.Empty);
  240. }
  241. if (hide_selection) {
  242. document.selection_visible = false;
  243. } else {
  244. document.selection_visible = true;
  245. }
  246. document.InvalidateSelectionArea();
  247. }
  248. }
  249. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  250. [Editor("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
  251. [Localizable(true)]
  252. [MWFCategory("Layout")]
  253. public string[] Lines {
  254. get {
  255. string[] lines;
  256. int i;
  257. int l;
  258. l = document.Lines;
  259. lines = new string[l];
  260. for (i = 1; i <= l; i++) {
  261. lines[i - 1] = document.GetLine(i).text.ToString();
  262. }
  263. return lines;
  264. }
  265. set {
  266. int i;
  267. int l;
  268. Brush brush;
  269. document.Empty();
  270. l = value.Length;
  271. brush = ThemeEngine.Current.ResPool.GetSolidBrush(this.ForeColor);
  272. for (i = 0; i < l; i++) {
  273. document.Add(i+1, CaseAdjust(value[i]), alignment, Font, brush);
  274. }
  275. CalculateDocument();
  276. OnTextChanged(EventArgs.Empty);
  277. }
  278. }
  279. [DefaultValue(32767)]
  280. [Localizable(true)]
  281. [MWFCategory("Behavior")]
  282. public virtual int MaxLength {
  283. get {
  284. if (max_length == 2147483646) { // We don't distinguish between single and multi-line limits
  285. return 0;
  286. }
  287. return max_length;
  288. }
  289. set {
  290. if (value != max_length) {
  291. max_length = value;
  292. }
  293. }
  294. }
  295. [Browsable(false)]
  296. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  297. public bool Modified {
  298. get {
  299. return modified;
  300. }
  301. set {
  302. if (value != modified) {
  303. modified = value;
  304. OnModifiedChanged(EventArgs.Empty);
  305. }
  306. }
  307. }
  308. [DefaultValue(false)]
  309. [Localizable(true)]
  310. [RefreshProperties(RefreshProperties.All)]
  311. [MWFCategory("Behavior")]
  312. public virtual bool Multiline {
  313. get {
  314. return multiline;
  315. }
  316. set {
  317. if (value != multiline) {
  318. multiline = value;
  319. // Make sure we update our size; the user may have already set the size before going to multiline
  320. if (multiline && requested_height != -1) {
  321. Height = requested_height;
  322. requested_height = -1;
  323. }
  324. OnMultilineChanged(EventArgs.Empty);
  325. }
  326. document.multiline = multiline;
  327. if (multiline) {
  328. document.Wrap = word_wrap;
  329. document.PasswordChar = "";
  330. } else {
  331. document.Wrap = false;
  332. if (this.password_char != '\0') {
  333. document.PasswordChar = password_char.ToString();
  334. } else {
  335. document.PasswordChar = "";
  336. }
  337. }
  338. }
  339. }
  340. [Browsable(false)]
  341. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  342. [EditorBrowsable(EditorBrowsableState.Advanced)]
  343. public int PreferredHeight {
  344. get {
  345. return this.Font.Height + 7; // FIXME - consider border style as well
  346. }
  347. }
  348. [DefaultValue(false)]
  349. [MWFCategory("Behavior")]
  350. public bool ReadOnly {
  351. get {
  352. return read_only;
  353. }
  354. set {
  355. if (value != read_only) {
  356. read_only = value;
  357. OnReadOnlyChanged(EventArgs.Empty);
  358. }
  359. }
  360. }
  361. [Browsable(false)]
  362. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  363. public virtual string SelectedText {
  364. get {
  365. return document.GetSelection();
  366. }
  367. set {
  368. if (!read_only) {
  369. document.ReplaceSelection(CaseAdjust(value));
  370. ScrollToCaret();
  371. OnTextChanged(EventArgs.Empty);
  372. }
  373. }
  374. }
  375. [Browsable(false)]
  376. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  377. public virtual int SelectionLength {
  378. get {
  379. return document.SelectionLength();
  380. }
  381. set {
  382. if (value != 0) {
  383. int start;
  384. Line line;
  385. LineTag tag;
  386. int pos;
  387. start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
  388. document.CharIndexToLineTag(start + value, out line, out tag, out pos);
  389. document.SetSelectionEnd(line, pos);
  390. document.PositionCaret(line, pos);
  391. } else {
  392. document.SetSelectionEnd(document.selection_start.line, document.selection_start.pos);
  393. document.PositionCaret(document.selection_start.line, document.selection_start.pos);
  394. }
  395. }
  396. }
  397. [Browsable(false)]
  398. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  399. public int SelectionStart {
  400. get {
  401. int index;
  402. index = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
  403. return index;
  404. }
  405. set {
  406. document.SetSelectionStart(value);
  407. ScrollToCaret();
  408. }
  409. }
  410. [Localizable(true)]
  411. public override string Text {
  412. get {
  413. if (document == null || document.Root == null || document.Root.text == null) {
  414. return string.Empty;
  415. }
  416. if (!multiline) {
  417. return document.Root.text.ToString();
  418. } else {
  419. StringBuilder sb;
  420. int i;
  421. sb = new StringBuilder();
  422. for (i = 1; i < document.Lines; i++) {
  423. if (i > 1)
  424. sb.Append (Environment.NewLine);
  425. sb.Append(document.GetLine(i).text.ToString());
  426. }
  427. sb.Append(document.GetLine(document.Lines).text.ToString());
  428. return sb.ToString();
  429. }
  430. }
  431. set {
  432. if ((value != null) && (value != "")) {
  433. Line line;
  434. if (multiline) {
  435. string[] lines;
  436. lines = value.Split(new char[] {'\n'});
  437. for (int i = 0; i < lines.Length; i++) {
  438. if (lines[i].EndsWith("\r")) {
  439. lines[i] = lines[i].Substring(0, lines[i].Length - 1);
  440. }
  441. }
  442. this.Lines = lines;
  443. line = document.GetLine(1);
  444. if (!Focused) {
  445. document.SetSelectionStart(line, 0);
  446. line = document.GetLine(document.Lines);
  447. document.SetSelectionEnd(line, line.text.Length);
  448. document.PositionCaret(line, line.text.Length);
  449. } else {
  450. document.SetSelectionStart(line, 0);
  451. document.SetSelectionEnd(line, 0);
  452. document.PositionCaret(line, 0);
  453. }
  454. } else {
  455. document.Clear();
  456. document.Add(1, CaseAdjust(value), alignment, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
  457. CalculateDocument();
  458. line = document.GetLine(1);
  459. if (!Focused) {
  460. document.SetSelectionStart(line, 0);
  461. document.SetSelectionEnd(line, value.Length);
  462. document.PositionCaret(line, value.Length);
  463. } else {
  464. document.SetSelectionStart(line, 0);
  465. document.SetSelectionEnd(line, 0);
  466. document.PositionCaret(line, 0);
  467. }
  468. }
  469. } else {
  470. document.Empty();
  471. CalculateDocument();
  472. }
  473. base.Text = value;
  474. // Not needed, base.Text already fires it
  475. // OnTextChanged(EventArgs.Empty);
  476. }
  477. }
  478. [Browsable(false)]
  479. public virtual int TextLength {
  480. get {
  481. if (document == null || document.Root == null || document.Root.text == null) {
  482. return 0;
  483. }
  484. if (!multiline) {
  485. return document.Root.text.Length;
  486. } else {
  487. int total;
  488. int i;
  489. total = 0;
  490. for (i = 1; i < document.Lines; i++) {
  491. total += document.GetLine(i).text.Length + Environment.NewLine.Length;
  492. }
  493. total += document.GetLine(i).text.Length;
  494. return total;
  495. }
  496. }
  497. }
  498. [DefaultValue(true)]
  499. [Localizable(true)]
  500. [MWFCategory("Behavior")]
  501. public bool WordWrap {
  502. get {
  503. return word_wrap;
  504. }
  505. set {
  506. if (value != word_wrap) {
  507. if (multiline) {
  508. word_wrap = value;
  509. document.Wrap = value;
  510. }
  511. }
  512. }
  513. }
  514. #endregion // Public Instance Properties
  515. #region Protected Instance Properties
  516. protected override CreateParams CreateParams {
  517. get {
  518. return base.CreateParams;
  519. }
  520. }
  521. protected override System.Drawing.Size DefaultSize {
  522. get {
  523. return new Size(100, 20);
  524. }
  525. }
  526. #endregion // Protected Instance Properties
  527. #region Public Instance Methods
  528. public void AppendText(string text) {
  529. if (multiline) {
  530. // Grab the formatting for the last element
  531. document.MoveCaret(CaretDirection.CtrlEnd);
  532. // grab the end tag
  533. if (document.CaretTag.next != null) {
  534. document.CaretTag = document.CaretTag.next;
  535. }
  536. document.Insert(document.CaretLine, document.CaretTag, document.CaretPosition, false, text);
  537. CalculateDocument();
  538. } else {
  539. document.MoveCaret(CaretDirection.CtrlEnd);
  540. document.InsertStringAtCaret(text, true);
  541. Invalidate();
  542. }
  543. document.MoveCaret(CaretDirection.CtrlEnd);
  544. document.SetSelectionStart(document.CaretLine, document.CaretPosition);
  545. document.SetSelectionEnd(document.CaretLine, document.CaretPosition);
  546. OnTextChanged(EventArgs.Empty);
  547. }
  548. public void Clear() {
  549. Text = null;
  550. }
  551. public void ClearUndo() {
  552. document.undo.Clear();
  553. }
  554. public void Copy() {
  555. DataObject o;
  556. o = new DataObject(DataFormats.Text, SelectedText);
  557. if (this is RichTextBox) {
  558. o.SetData(DataFormats.Rtf, ((RichTextBox)this).SelectedRtf);
  559. }
  560. Clipboard.SetDataObject(o);
  561. }
  562. public void Cut() {
  563. DataObject o;
  564. o = new DataObject(DataFormats.Text, SelectedText);
  565. if (this is RichTextBox) {
  566. o.SetData(DataFormats.Rtf, ((RichTextBox)this).SelectedRtf);
  567. }
  568. Clipboard.SetDataObject(o);
  569. document.ReplaceSelection("");
  570. }
  571. public void Paste() {
  572. Paste(null, false);
  573. }
  574. public void ScrollToCaret() {
  575. this.CaretMoved(this, EventArgs.Empty);
  576. }
  577. public void Select(int start, int length) {
  578. SelectionStart = start;
  579. SelectionLength = length;
  580. }
  581. public void SelectAll() {
  582. Line last;
  583. last = document.GetLine(document.Lines);
  584. document.SetSelectionStart(document.GetLine(1), 0);
  585. document.SetSelectionEnd(last, last.text.Length);
  586. }
  587. public override string ToString() {
  588. return String.Concat (base.ToString (), ", Text: ", Text);
  589. }
  590. public void Undo() {
  591. document.undo.Undo();
  592. }
  593. #endregion // Public Instance Methods
  594. #region Protected Instance Methods
  595. protected override void CreateHandle() {
  596. base.CreateHandle ();
  597. }
  598. protected override bool IsInputKey(Keys keyData) {
  599. if ((keyData & Keys.Alt) != 0) {
  600. return base.IsInputKey(keyData);
  601. }
  602. switch (keyData & Keys.KeyCode) {
  603. case Keys.Enter: {
  604. if (multiline && accepts_return) {
  605. return true;
  606. }
  607. return false;
  608. }
  609. case Keys.Tab: {
  610. if (accepts_tab && multiline) {
  611. if ((keyData & Keys.Control) == 0) {
  612. return true;
  613. }
  614. }
  615. return false;
  616. }
  617. case Keys.Left:
  618. case Keys.Right:
  619. case Keys.Up:
  620. case Keys.Down:
  621. case Keys.PageUp:
  622. case Keys.PageDown:
  623. case Keys.Home:
  624. case Keys.End: {
  625. return true;
  626. }
  627. }
  628. return false;
  629. }
  630. protected virtual void OnAcceptsTabChanged(EventArgs e) {
  631. if (AcceptsTabChanged != null) {
  632. AcceptsTabChanged(this, e);
  633. }
  634. }
  635. protected virtual void OnAutoSizeChanged(EventArgs e) {
  636. if (AutoSizeChanged != null) {
  637. AutoSizeChanged(this, e);
  638. }
  639. }
  640. protected virtual void OnBorderStyleChanged(EventArgs e) {
  641. if (BorderStyleChanged != null) {
  642. BorderStyleChanged(this, e);
  643. }
  644. }
  645. protected override void OnFontChanged(EventArgs e) {
  646. base.OnFontChanged (e);
  647. if (auto_size) {
  648. if (PreferredHeight != ClientSize.Height) {
  649. Height = PreferredHeight;
  650. }
  651. }
  652. }
  653. protected override void OnHandleCreated(EventArgs e) {
  654. base.OnHandleCreated (e);
  655. }
  656. protected override void OnHandleDestroyed(EventArgs e) {
  657. base.OnHandleDestroyed (e);
  658. }
  659. protected virtual void OnHideSelectionChanged(EventArgs e) {
  660. if (HideSelectionChanged != null) {
  661. HideSelectionChanged(this, e);
  662. }
  663. }
  664. protected virtual void OnModifiedChanged(EventArgs e) {
  665. if (ModifiedChanged != null) {
  666. ModifiedChanged(this, e);
  667. }
  668. }
  669. protected virtual void OnMultilineChanged(EventArgs e) {
  670. if (MultilineChanged != null) {
  671. MultilineChanged(this, e);
  672. }
  673. }
  674. protected virtual void OnReadOnlyChanged(EventArgs e) {
  675. if (ReadOnlyChanged != null) {
  676. ReadOnlyChanged(this, e);
  677. }
  678. }
  679. protected override bool ProcessDialogKey(Keys keyData) {
  680. return base.ProcessDialogKey(keyData);
  681. }
  682. private bool ProcessKey(Keys keyData) {
  683. bool control;
  684. bool shift;
  685. control = (Control.ModifierKeys & Keys.Control) != 0;
  686. shift = (Control.ModifierKeys & Keys.Shift) != 0;
  687. switch (keyData & Keys.KeyCode) {
  688. case Keys.X: { // Cut (Ctrl-X)
  689. if (control) {
  690. Cut();
  691. return true;
  692. }
  693. return false;
  694. }
  695. case Keys.C: { // Copy (Ctrl-C)
  696. if (control) {
  697. Copy();
  698. return true;
  699. }
  700. return false;
  701. }
  702. case Keys.V: { // Paste (Ctrl-V)
  703. if (control) {
  704. return Paste(null, true);
  705. }
  706. return false;
  707. }
  708. case Keys.Z: { // Undo (Ctrl-Z)
  709. if (control) {
  710. Undo();
  711. return true;
  712. }
  713. return false;
  714. }
  715. case Keys.A: { // Select All (Ctrl-A)
  716. if (control) {
  717. SelectAll();
  718. return true;
  719. }
  720. return false;
  721. }
  722. case Keys.Left: {
  723. if (control) {
  724. document.MoveCaret(CaretDirection.WordBack);
  725. } else {
  726. if (!document.selection_visible || shift) {
  727. document.MoveCaret(CaretDirection.CharBack);
  728. } else {
  729. document.MoveCaret(CaretDirection.SelectionStart);
  730. }
  731. }
  732. if (!shift) {
  733. document.SetSelectionToCaret(true);
  734. } else {
  735. document.SetSelectionToCaret(false);
  736. }
  737. CaretMoved(this, null);
  738. return true;
  739. }
  740. case Keys.Right: {
  741. if (control) {
  742. document.MoveCaret(CaretDirection.WordForward);
  743. } else {
  744. if (!document.selection_visible || shift) {
  745. document.MoveCaret(CaretDirection.CharForward);
  746. } else {
  747. document.MoveCaret(CaretDirection.SelectionEnd);
  748. }
  749. }
  750. if (!shift) {
  751. document.SetSelectionToCaret(true);
  752. } else {
  753. document.SetSelectionToCaret(false);
  754. }
  755. CaretMoved(this, null);
  756. return true;
  757. }
  758. case Keys.Up: {
  759. if (control) {
  760. if (document.CaretPosition == 0) {
  761. document.MoveCaret(CaretDirection.LineUp);
  762. } else {
  763. document.MoveCaret(CaretDirection.Home);
  764. }
  765. } else {
  766. document.MoveCaret(CaretDirection.LineUp);
  767. }
  768. if ((Control.ModifierKeys & Keys.Shift) == 0) {
  769. document.SetSelectionToCaret(true);
  770. } else {
  771. document.SetSelectionToCaret(false);
  772. }
  773. CaretMoved(this, null);
  774. return true;
  775. }
  776. case Keys.Down: {
  777. if (control) {
  778. if (document.CaretPosition == document.CaretLine.Text.Length) {
  779. document.MoveCaret(CaretDirection.LineDown);
  780. } else {
  781. document.MoveCaret(CaretDirection.End);
  782. }
  783. } else {
  784. document.MoveCaret(CaretDirection.LineDown);
  785. }
  786. if ((Control.ModifierKeys & Keys.Shift) == 0) {
  787. document.SetSelectionToCaret(true);
  788. } else {
  789. document.SetSelectionToCaret(false);
  790. }
  791. CaretMoved(this, null);
  792. return true;
  793. }
  794. case Keys.Home: {
  795. if ((Control.ModifierKeys & Keys.Control) != 0) {
  796. document.MoveCaret(CaretDirection.CtrlHome);
  797. } else {
  798. document.MoveCaret(CaretDirection.Home);
  799. }
  800. if ((Control.ModifierKeys & Keys.Shift) == 0) {
  801. document.SetSelectionToCaret(true);
  802. } else {
  803. document.SetSelectionToCaret(false);
  804. }
  805. CaretMoved(this, null);
  806. return true;
  807. }
  808. case Keys.End: {
  809. if ((Control.ModifierKeys & Keys.Control) != 0) {
  810. document.MoveCaret(CaretDirection.CtrlEnd);
  811. } else {
  812. document.MoveCaret(CaretDirection.End);
  813. }
  814. if ((Control.ModifierKeys & Keys.Shift) == 0) {
  815. document.SetSelectionToCaret(true);
  816. } else {
  817. document.SetSelectionToCaret(false);
  818. }
  819. CaretMoved(this, null);
  820. return true;
  821. }
  822. case Keys.Enter: {
  823. // ignoring accepts_return, fixes bug #76355
  824. if (!read_only && multiline && (accepts_return || (FindForm() != null && FindForm().AcceptButton == null) || ((Control.ModifierKeys & Keys.Control) != 0))) {
  825. Line line;
  826. if (document.selection_visible) {
  827. document.ReplaceSelection("");
  828. }
  829. document.SetSelectionToCaret(true);
  830. line = document.CaretLine;
  831. document.Split(document.CaretLine, document.CaretTag, document.CaretPosition, false);
  832. OnTextChanged(EventArgs.Empty);
  833. document.UpdateView(line, 2, 0);
  834. document.MoveCaret(CaretDirection.CharForward);
  835. CaretMoved(this, null);
  836. return true;
  837. }
  838. break;
  839. }
  840. case Keys.Tab: {
  841. if (!read_only && accepts_tab && multiline) {
  842. document.InsertChar(document.CaretLine, document.CaretPosition, '\t');
  843. if (document.selection_visible) {
  844. document.ReplaceSelection("");
  845. }
  846. document.SetSelectionToCaret(true);
  847. OnTextChanged(EventArgs.Empty);
  848. CaretMoved(this, null);
  849. return true;
  850. }
  851. break;
  852. }
  853. case Keys.Insert: {
  854. if (shift) {
  855. Paste(null, true);
  856. return true;
  857. }
  858. if (control) {
  859. Copy();
  860. return true;
  861. }
  862. // FIXME - need overwrite/insert toggle?
  863. return false;
  864. }
  865. case Keys.PageUp: {
  866. if ((Control.ModifierKeys & Keys.Control) != 0) {
  867. document.MoveCaret(CaretDirection.CtrlPgUp);
  868. } else {
  869. document.MoveCaret(CaretDirection.PgUp);
  870. }
  871. return true;
  872. }
  873. case Keys.PageDown: {
  874. if ((Control.ModifierKeys & Keys.Control) != 0) {
  875. document.MoveCaret(CaretDirection.CtrlPgDn);
  876. } else {
  877. document.MoveCaret(CaretDirection.PgDn);
  878. }
  879. return true;
  880. }
  881. case Keys.Delete: {
  882. if (shift) {
  883. Cut();
  884. return true;
  885. }
  886. if (read_only) {
  887. break;
  888. }
  889. if (document.selection_visible) {
  890. document.ReplaceSelection("");
  891. } else {
  892. // DeleteChar only deletes on the line, doesn't do the combine
  893. if (document.CaretPosition == document.CaretLine.Text.Length) {
  894. if (document.CaretLine.LineNo < document.Lines) {
  895. Line line;
  896. line = document.GetLine(document.CaretLine.LineNo + 1);
  897. document.Combine(document.CaretLine, line);
  898. document.UpdateView(document.CaretLine, 2, 0);
  899. #if not_Debug
  900. Line check_first;
  901. Line check_second;
  902. check_first = document.GetLine(document.CaretLine.LineNo);
  903. check_second = document.GetLine(check_first.line_no + 1);
  904. Console.WriteLine("Post-UpdateView: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
  905. #endif
  906. // Caret doesn't move
  907. }
  908. } else {
  909. if (!control) {
  910. document.DeleteChar(document.CaretTag, document.CaretPosition, true);
  911. } else {
  912. int end_pos;
  913. end_pos = document.CaretPosition;
  914. while ((end_pos < document.CaretLine.Text.Length) && !Document.IsWordSeparator(document.CaretLine.Text[end_pos])) {
  915. end_pos++;
  916. }
  917. if (end_pos < document.CaretLine.Text.Length) {
  918. end_pos++;
  919. }
  920. document.DeleteChars(document.CaretTag, document.CaretPosition, end_pos - document.CaretPosition);
  921. }
  922. }
  923. }
  924. OnTextChanged(EventArgs.Empty);
  925. document.AlignCaret();
  926. document.UpdateCaret();
  927. CaretMoved(this, null);
  928. return true;
  929. }
  930. }
  931. return false;
  932. }
  933. private void HandleBackspace(bool control) {
  934. bool fire_changed;
  935. fire_changed = false;
  936. // delete only deletes on the line, doesn't do the combine
  937. if (document.selection_visible) {
  938. document.ReplaceSelection("");
  939. fire_changed = true;
  940. }
  941. document.SetSelectionToCaret(true);
  942. if (document.CaretPosition == 0) {
  943. if (document.CaretLine.LineNo > 1) {
  944. Line line;
  945. int new_caret_pos;
  946. line = document.GetLine(document.CaretLine.LineNo - 1);
  947. new_caret_pos = line.text.Length;
  948. document.Combine(line, document.CaretLine);
  949. document.UpdateView(line, 1, 0);
  950. document.PositionCaret(line, new_caret_pos);
  951. //document.MoveCaret(CaretDirection.CharForward);
  952. document.UpdateCaret();
  953. fire_changed = true;
  954. }
  955. } else {
  956. if (!control || document.CaretPosition == 0) {
  957. document.DeleteChar(document.CaretTag, document.CaretPosition, false);
  958. document.MoveCaret(CaretDirection.CharBack);
  959. } else {
  960. int start_pos;
  961. start_pos = document.CaretPosition - 1;
  962. while ((start_pos > 0) && !Document.IsWordSeparator(document.CaretLine.Text[start_pos - 1])) {
  963. start_pos--;
  964. }
  965. document.DeleteChars(document.CaretTag, start_pos, document.CaretPosition - start_pos);
  966. document.PositionCaret(document.CaretLine, start_pos);
  967. }
  968. document.UpdateCaret();
  969. fire_changed = true;
  970. }
  971. if (fire_changed) {
  972. OnTextChanged(EventArgs.Empty);
  973. }
  974. CaretMoved(this, null);
  975. }
  976. protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
  977. // Make sure we don't get sized bigger than we want to be
  978. if (!richtext) {
  979. if (!multiline) {
  980. if (height != PreferredHeight) {
  981. requested_height = height;
  982. height = PreferredHeight;
  983. specified |= BoundsSpecified.Height;
  984. }
  985. }
  986. }
  987. base.SetBoundsCore (x, y, width, height, specified);
  988. TextBoxBase_SizeChanged(this, EventArgs.Empty);
  989. CalculateDocument();
  990. }
  991. protected override void WndProc(ref Message m) {
  992. switch ((Msg)m.Msg) {
  993. case Msg.WM_KEYDOWN: {
  994. if (ProcessKeyMessage(ref m) || ProcessKey((Keys)m.WParam.ToInt32() | XplatUI.State.ModifierKeys)) {
  995. m.Result = IntPtr.Zero;
  996. return;
  997. }
  998. DefWndProc (ref m);
  999. return;
  1000. }
  1001. case Msg.WM_CHAR: {
  1002. int ch;
  1003. if (ProcessKeyMessage(ref m)) {
  1004. m.Result = IntPtr.Zero;
  1005. return;
  1006. }
  1007. if (read_only) {
  1008. return;
  1009. }
  1010. m.Result = IntPtr.Zero;
  1011. ch = m.WParam.ToInt32();
  1012. if (ch == 127) {
  1013. HandleBackspace(true);
  1014. } else if (ch >= 32) {
  1015. if (document.selection_visible) {
  1016. document.ReplaceSelection("");
  1017. }
  1018. char c = (char)m.WParam;
  1019. switch (character_casing) {
  1020. case CharacterCasing.Upper:
  1021. c = Char.ToUpper((char) m.WParam);
  1022. break;
  1023. case CharacterCasing.Lower:
  1024. c = Char.ToLower((char) m.WParam);
  1025. break;
  1026. }
  1027. if (document.Length < max_length) {
  1028. document.InsertCharAtCaret(c, true);
  1029. OnTextChanged(EventArgs.Empty);
  1030. CaretMoved(this, null);
  1031. } else {
  1032. XplatUI.AudibleAlert();
  1033. }
  1034. return;
  1035. } else if (ch == 8) {
  1036. HandleBackspace(false);
  1037. }
  1038. return;
  1039. }
  1040. default: {
  1041. base.WndProc(ref m);
  1042. return;
  1043. }
  1044. }
  1045. }
  1046. #endregion // Protected Instance Methods
  1047. #region Events
  1048. public event EventHandler AcceptsTabChanged;
  1049. public event EventHandler AutoSizeChanged;
  1050. [Browsable(false)]
  1051. [EditorBrowsable(EditorBrowsableState.Never)]
  1052. public new event EventHandler BackgroundImageChanged {
  1053. add { base.BackgroundImageChanged += value; }
  1054. remove { base.BackgroundImageChanged -= value; }
  1055. }
  1056. public event EventHandler BorderStyleChanged;
  1057. [Browsable(false)]
  1058. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1059. public event EventHandler Click;
  1060. public event EventHandler HideSelectionChanged;
  1061. public event EventHandler ModifiedChanged;
  1062. public event EventHandler MultilineChanged;
  1063. [Browsable(false)]
  1064. [EditorBrowsable(EditorBrowsableState.Never)]
  1065. public event PaintEventHandler Paint;
  1066. public event EventHandler ReadOnlyChanged;
  1067. internal event EventHandler HScrolled;
  1068. internal event EventHandler VScrolled;
  1069. #endregion // Events
  1070. #region Private Methods
  1071. internal Document Document {
  1072. get {
  1073. return document;
  1074. }
  1075. set {
  1076. document = value;
  1077. }
  1078. }
  1079. internal bool ShowSelection {
  1080. get {
  1081. if (show_selection) {
  1082. return true;
  1083. }
  1084. return has_focus;
  1085. }
  1086. set {
  1087. if (show_selection == value)
  1088. return;
  1089. show_selection = value;
  1090. // Currently InvalidateSelectionArea is commented out so do a full invalidate
  1091. document.InvalidateSelectionArea();
  1092. }
  1093. }
  1094. internal Graphics CreateGraphicsInternal() {
  1095. if (IsHandleCreated) {
  1096. return base.CreateGraphics();
  1097. }
  1098. return Graphics.FromImage(bmp);
  1099. }
  1100. #if Debug
  1101. static int current;
  1102. #endif
  1103. internal override void OnPaintInternal (PaintEventArgs pevent) {
  1104. // Fill background
  1105. if (backcolor_set || (Enabled && !read_only)) {
  1106. pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), pevent.ClipRectangle);
  1107. } else {
  1108. pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorControl), pevent.ClipRectangle);
  1109. }
  1110. pevent.Graphics.TextRenderingHint=TextRenderingHint.AntiAlias;
  1111. // Draw the viewable document
  1112. document.Draw(pevent.Graphics, pevent.ClipRectangle);
  1113. Rectangle rect = ClientRectangle;
  1114. rect.Width--;
  1115. rect.Height--;
  1116. //pevent.Graphics.DrawRectangle(ThemeEngine.Current.ResPool.GetPen(ThemeEngine.Current.ColorControlDark), rect);
  1117. #if Debug
  1118. int start;
  1119. int end;
  1120. Line line;
  1121. int line_no;
  1122. Pen p;
  1123. p = new Pen(Color.Red, 1);
  1124. // First, figure out from what line to what line we need to draw
  1125. start = document.GetLineByPixel(pevent.ClipRectangle.Top - document.ViewPortY, false).line_no;
  1126. end = document.GetLineByPixel(pevent.ClipRectangle.Bottom - document.ViewPortY, false).line_no;
  1127. //Console.WriteLine("Starting drawing on line '{0}'", document.GetLine(start));
  1128. //Console.WriteLine("Ending drawing on line '{0}'", document.GetLine(end));
  1129. line_no = start;
  1130. while (line_no <= end) {
  1131. line = document.GetLine(line_no);
  1132. if (draw_lines) {
  1133. for (int i = 0; i < line.text.Length; i++) {
  1134. pevent.Graphics.DrawLine(p, (int)line.widths[i] - document.ViewPortX, line.Y - document.ViewPortY, (int)line.widths[i] - document.ViewPortX, line.Y + line.height - document.ViewPortY);
  1135. }
  1136. }
  1137. line_no++;
  1138. }
  1139. #endif
  1140. }
  1141. internal override void OnGotFocusInternal (EventArgs e)
  1142. {
  1143. document.CaretHasFocus ();
  1144. base.OnGotFocusInternal (e);
  1145. }
  1146. internal override void OnLostFocusInternal (EventArgs e)
  1147. {
  1148. document.CaretLostFocus ();
  1149. base.OnLostFocusInternal (e);
  1150. }
  1151. private void TextBoxBase_MouseDown(object sender, MouseEventArgs e) {
  1152. if (e.Button == MouseButtons.Left) {
  1153. TimeSpan interval;
  1154. interval = DateTime.Now - click_last;
  1155. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  1156. // Handle place caret/select word/select line behaviour
  1157. if (e.Clicks == 1) {
  1158. if (SystemInformation.DoubleClickTime < interval.TotalMilliseconds) {
  1159. #if DebugClick
  1160. Console.WriteLine("Single Click Invalidating from char {0} to char {1} ({2})", document.selection_start.pos, document.selection_end.pos, document.selection_start.line.text.ToString(document.selection_start.pos, document.selection_end.pos - document.selection_start.pos));
  1161. #endif
  1162. document.SetSelectionToCaret(true);
  1163. click_mode = CaretSelection.Position;
  1164. } else {
  1165. #if DebugClick
  1166. Console.WriteLine("Tripple Click Selecting line");
  1167. #endif
  1168. document.ExpandSelection(CaretSelection.Line, false);
  1169. click_mode = CaretSelection.Line;
  1170. }
  1171. } else {
  1172. // We select the line if the word is already selected, and vice versa
  1173. if (click_mode != CaretSelection.Word) {
  1174. if (click_mode == CaretSelection.Line) {
  1175. document.Invalidate(document.selection_start.line, 0, document.selection_start.line, document.selection_start.line.text.Length);
  1176. }
  1177. click_mode = CaretSelection.Word;
  1178. document.ExpandSelection(CaretSelection.Word, false); // Setting initial selection
  1179. } else {
  1180. click_mode = CaretSelection.Line;
  1181. document.ExpandSelection(CaretSelection.Line, false); // Setting initial selection
  1182. }
  1183. }
  1184. // Reset
  1185. click_last = DateTime.Now;
  1186. return;
  1187. }
  1188. if ((e.Button == MouseButtons.Middle) && (((int)Environment.OSVersion.Platform == 4) || ((int)Environment.OSVersion.Platform == 128))) {
  1189. Document.Marker marker;
  1190. marker.tag = document.FindCursor(e.X + document.ViewPortX, e.Y + document.ViewPortY, out marker.pos);
  1191. marker.line = marker.tag.line;
  1192. marker.height = marker.tag.height;
  1193. document.SetSelection(marker.line, marker.pos, marker.line, marker.pos);
  1194. Paste();
  1195. }
  1196. #if Debug
  1197. LineTag tag;
  1198. Line line;
  1199. int pos;
  1200. if (e.Button == MouseButtons.Right) {
  1201. draw_lines = !draw_lines;
  1202. this.Invalidate();
  1203. Console.WriteLine("SelectedText: {0}, length {1}", this.SelectedText, this.SelectionLength);
  1204. Console.WriteLine("Selection start: {0}", this.SelectionStart);
  1205. this.SelectionStart = 10;
  1206. this.SelectionLength = 5;
  1207. return;
  1208. }
  1209. tag = document.FindTag(e.X + document.ViewPortX, e.Y + document.ViewPortY, out pos, false);
  1210. Console.WriteLine("Click found tag {0}, character {1}", tag, pos);
  1211. line = tag.line;
  1212. switch(current) {
  1213. case 4: LineTag.FormatText(tag.line, pos, (pos+10)<line.Text.Length ? 10 : line.Text.Length - pos+1, new Font("impact", 20, FontStyle.Bold, GraphicsUnit.Pixel), ThemeEngine.Current.ResPool.GetSolidBrush(Color.Red)); break;
  1214. case 1: LineTag.FormatText(tag.line, pos, (pos+10)<line.Text.Length ? 10 : line.Text.Length - pos+1, new Font("arial unicode ms", 24, FontStyle.Italic, GraphicsUnit.Pixel), ThemeEngine.Current.ResPool.GetSolidBrush(Color.DarkGoldenrod)); break;
  1215. case 2: LineTag.FormatText(tag.line, pos, (pos+10)<line.Text.Length ? 10 : line.Text.Length - pos+1, new Font("arial", 10, FontStyle.Regular, GraphicsUnit.Pixel), ThemeEngine.Current.ResPool.GetSolidBrush(Color.Aquamarine)); break;
  1216. case 3: LineTag.FormatText(tag.line, pos, (pos+10)<line.Text.Length ? 10 : line.Text.Length - pos+1, new Font("times roman", 16, FontStyle.Underline, GraphicsUnit.Pixel), ThemeEngine.Current.ResPool.GetSolidBrush(Color.Turquoise)); break;
  1217. case 0: LineTag.FormatText(tag.line, pos, (pos+10)<line.Text.Length ? 10 : line.Text.Length - pos+1, new Font("times roman", 64, FontStyle.Italic | FontStyle.Bold, GraphicsUnit.Pixel), ThemeEngine.Current.ResPool.GetSolidBrush(Color.LightSeaGreen)); break;
  1218. case 5: LineTag.FormatText(tag.line, pos, (pos+10)<line.Text.Length ? 10 : line.Text.Length - pos+1, ((TextBoxBase)sender).Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor)); break;
  1219. }
  1220. current++;
  1221. if (current==6) {
  1222. current=0;
  1223. }
  1224. // Update/Recalculate what we see
  1225. document.UpdateView(line, 0);
  1226. // Make sure our caret is properly positioned and sized
  1227. document.AlignCaret();
  1228. #endif
  1229. }
  1230. private void TextBoxBase_MouseUp(object sender, MouseEventArgs e) {
  1231. if (e.Button == MouseButtons.Left) {
  1232. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  1233. if (click_mode == CaretSelection.Position) {
  1234. document.SetSelectionToCaret(false);
  1235. document.DisplayCaret();
  1236. } else {
  1237. document.ExpandSelection(click_mode, true);
  1238. }
  1239. return;
  1240. }
  1241. }
  1242. private void TextBoxBase_SizeChanged(object sender, EventArgs e) {
  1243. if (hscroll.Visible) {
  1244. //vscroll.Maximum += hscroll.Height;
  1245. canvas_height = ClientSize.Height - hscroll.Height;
  1246. } else {
  1247. canvas_height = ClientSize.Height;
  1248. }
  1249. if (vscroll.Visible) {
  1250. //hscroll.Maximum += vscroll.Width;
  1251. canvas_width = ClientSize.Width - vscroll.Width;
  1252. } else {
  1253. canvas_width = ClientSize.Width;
  1254. }
  1255. document.ViewPortWidth = canvas_width;
  1256. document.ViewPortHeight = canvas_height;
  1257. // We always move them, they just might not be displayed
  1258. hscroll.Bounds = new Rectangle (ClientRectangle.Left, ClientRectangle.Height - hscroll.Height, ClientSize.Width - (vscroll.Visible ? vscroll.Width : 0), hscroll.Height);
  1259. vscroll.Bounds = new Rectangle (ClientRectangle.Right - vscroll.Width, ClientRectangle.Top, vscroll.Width, ClientSize.Height - (hscroll.Visible ? hscroll.Height : 0));
  1260. }
  1261. private void TextBoxBase_MouseWheel(object sender, MouseEventArgs e) {
  1262. Line line;
  1263. int line_no;
  1264. if (!vscroll.Enabled) {
  1265. return;
  1266. }
  1267. if (e.Delta < 0) {
  1268. line_no = document.GetLineByPixel(document.ViewPortY, false).line_no + SystemInformation.MouseWheelScrollLines;
  1269. if (line_no > document.Lines) {
  1270. line_no = document.Lines;
  1271. }
  1272. } else {
  1273. line_no = document.GetLineByPixel(document.ViewPortY, false).line_no - SystemInformation.MouseWheelScrollLines;
  1274. if (line_no < 1) {
  1275. line_no = 1;
  1276. }
  1277. }
  1278. line = document.GetLine(line_no);
  1279. if (line.Y < vscroll.Maximum) {
  1280. vscroll.Value = line.Y;
  1281. } else {
  1282. vscroll.Value = vscroll.Maximum;
  1283. }
  1284. }
  1285. internal void CalculateDocument() {
  1286. if (!IsHandleCreated) {
  1287. return;
  1288. }
  1289. document.RecalculateDocument(CreateGraphicsInternal());
  1290. CalculateScrollBars();
  1291. Invalidate();
  1292. }
  1293. internal void CalculateScrollBars() {
  1294. // FIXME - need separate calculations for center and right alignment
  1295. // No scrollbars for a single line
  1296. if (document.Width >= document.ViewPortWidth) {
  1297. hscroll.Enabled = true;
  1298. hscroll.Minimum = 0;
  1299. hscroll.LargeChange = document.ViewPortWidth;
  1300. hscroll.Maximum = document.Width;
  1301. } else {
  1302. hscroll.Maximum = document.ViewPortWidth;
  1303. hscroll.Enabled = false;
  1304. }
  1305. if (document.Height >= document.ViewPortHeight) {
  1306. vscroll.Enabled = true;
  1307. vscroll.Minimum = 0;
  1308. vscroll.Maximum = document.Height - ClientSize.Height + 1;
  1309. } else {
  1310. vscroll.Maximum = document.ViewPortHeight;
  1311. vscroll.Enabled = false;
  1312. }
  1313. if (!multiline) {
  1314. return;
  1315. }
  1316. if (!WordWrap) {
  1317. if ((scrollbars & RichTextBoxScrollBars.Horizontal) != 0) {
  1318. if (((scrollbars & RichTextBoxScrollBars.ForcedHorizontal) != 0) || hscroll.Enabled) {
  1319. hscroll.Visible = true;
  1320. } else {
  1321. hscroll.Visible = false;
  1322. }
  1323. } else {
  1324. hscroll.Visible = false;
  1325. }
  1326. }
  1327. if ((scrollbars & RichTextBoxScrollBars.Vertical) != 0) {
  1328. if (((scrollbars & RichTextBoxScrollBars.ForcedVertical) != 0) || vscroll.Enabled) {
  1329. vscroll.Visible = true;
  1330. } else {
  1331. vscroll.Visible = false;
  1332. }
  1333. } else {
  1334. vscroll.Visible = false;
  1335. }
  1336. TextBoxBase_SizeChanged(this, EventArgs.Empty);
  1337. }
  1338. private void document_WidthChanged(object sender, EventArgs e) {
  1339. CalculateScrollBars();
  1340. }
  1341. private void document_HeightChanged(object sender, EventArgs e) {
  1342. CalculateScrollBars();
  1343. }
  1344. private void hscroll_ValueChanged(object sender, EventArgs e) {
  1345. int old_viewport_x;
  1346. old_viewport_x = document.ViewPortX;
  1347. document.ViewPortX = this.hscroll.Value;
  1348. if (vscroll.Visible) {
  1349. XplatUI.ScrollWindow(this.Handle, new Rectangle(0, 0, ClientSize.Width - vscroll.Width, ClientSize.Height), old_viewport_x - this.hscroll.Value, 0, false);
  1350. } else {
  1351. XplatUI.ScrollWindow(this.Handle, ClientRectangle, old_viewport_x - this.hscroll.Value, 0, false);
  1352. }
  1353. document.UpdateCaret();
  1354. if (HScrolled != null) {
  1355. HScrolled(this, EventArgs.Empty);
  1356. }
  1357. }
  1358. private void vscroll_ValueChanged(object sender, EventArgs e) {
  1359. int old_viewport_y;
  1360. old_viewport_y = document.ViewPortY;
  1361. document.ViewPortY = this.vscroll.Value;
  1362. if (hscroll.Visible) {
  1363. XplatUI.ScrollWindow(this.Handle, new Rectangle(0, 0, ClientSize.Width, ClientSize.Height - hscroll.Height), 0, old_viewport_y - this.vscroll.Value, false);
  1364. } else {
  1365. XplatUI.ScrollWindow(this.Handle, ClientRectangle, 0, old_viewport_y - this.vscroll.Value, false);
  1366. }
  1367. document.UpdateCaret();
  1368. if (VScrolled != null) {
  1369. VScrolled(this, EventArgs.Empty);
  1370. }
  1371. }
  1372. private void TextBoxBase_MouseMove(object sender, MouseEventArgs e) {
  1373. // FIXME - handle auto-scrolling if mouse is to the right/left of the window
  1374. if (Capture) {
  1375. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  1376. if (click_mode == CaretSelection.Position) {
  1377. document.SetSelectionToCaret(false);
  1378. document.DisplayCaret();
  1379. } else {
  1380. document.ExpandSelection(click_mode, true);
  1381. }
  1382. }
  1383. }
  1384. private void TextBoxBase_FontOrColorChanged(object sender, EventArgs e) {
  1385. if (!richtext) {
  1386. Line line;
  1387. // Font changes apply to the whole document
  1388. for (int i = 1; i <= document.Lines; i++) {
  1389. line = document.GetLine(i);
  1390. LineTag.FormatText(line, 1, line.text.Length, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
  1391. document.UpdateView(line, 0);
  1392. }
  1393. // Make sure the caret height is matching the new font height
  1394. document.AlignCaret();
  1395. }
  1396. }
  1397. /// <summary>Ensure the caret is always visible</summary>
  1398. internal void CaretMoved(object sender, EventArgs e) {
  1399. Point pos;
  1400. int height;
  1401. pos = document.Caret;
  1402. //Console.WriteLine("Caret now at {0} (Thumb: {1}x{2}, Canvas: {3}x{4}, Document {5}x{6})", pos, hscroll.Value, vscroll.Value, canvas_width, canvas_height, document.Width, document.Height);
  1403. // Horizontal scrolling:
  1404. // If the caret moves to the left outside the visible area, we jump the document into view, not just one
  1405. // character, but 1/3 of the width of the document
  1406. // If the caret moves to the right outside the visible area, we scroll just enough to keep the caret visible
  1407. // Handle horizontal scrolling
  1408. if (document.CaretLine.alignment == HorizontalAlignment.Left) {
  1409. // Check if we moved out of view to the left
  1410. if (pos.X < (document.ViewPortX)) {
  1411. do {
  1412. if ((hscroll.Value - document.ViewPortWidth / 3) >= hscroll.Minimum) {
  1413. hscroll.Value -= document.ViewPortWidth / 3;
  1414. } else {
  1415. hscroll.Value = hscroll.Minimum;
  1416. }
  1417. } while (hscroll.Value > pos.X);
  1418. }
  1419. // Check if we moved out of view to the right
  1420. if ((pos.X >= (document.ViewPortWidth + document.ViewPortX)) && (hscroll.Enabled && (hscroll.Value != hscroll.Maximum))) {
  1421. if ((pos.X - document.ViewPortWidth + 1) <= hscroll.Maximum) {
  1422. if (pos.X - document.ViewPortWidth >= 0) {
  1423. hscroll.Value = pos.X - document.ViewPortWidth + 1;
  1424. } else {
  1425. hscroll.Value = 0;
  1426. }
  1427. } else {
  1428. hscroll.Value = hscroll.Maximum;
  1429. }
  1430. }
  1431. } else if (document.CaretLine.alignment == HorizontalAlignment.Right) {
  1432. // hscroll.Value = pos.X;
  1433. // if ((pos.X > (this.canvas_width + document.ViewPortX)) && (hscroll.Enabled && (hscroll.Value != hscroll.Maximum))) {
  1434. // hscroll.Value = hscroll.Maximum;
  1435. // }
  1436. } else {
  1437. // FIXME - implement center cursor alignment
  1438. }
  1439. if (!multiline) {
  1440. return;
  1441. }
  1442. // Handle vertical scrolling
  1443. height = document.CaretLine.Height + 1;
  1444. if (pos.Y < document.ViewPortY) {
  1445. vscroll.Value = pos.Y;
  1446. }
  1447. if ((pos.Y + height) > (document.ViewPortY + canvas_height)) {
  1448. vscroll.Value = pos.Y - canvas_height + height;
  1449. }
  1450. }
  1451. internal bool Paste(DataFormats.Format format, bool obey_length) {
  1452. IDataObject clip;
  1453. string s;
  1454. clip = Clipboard.GetDataObject();
  1455. if (clip == null)
  1456. return false;
  1457. if (format == null) {
  1458. if ((this is RichTextBox) && clip.GetDataPresent(DataFormats.Rtf)) {
  1459. format = DataFormats.GetFormat(DataFormats.Rtf);
  1460. } else if (clip.GetDataPresent(DataFormats.UnicodeText)) {
  1461. format = DataFormats.GetFormat(DataFormats.UnicodeText);
  1462. } else if (clip.GetDataPresent(DataFormats.Text)) {
  1463. format = DataFormats.GetFormat(DataFormats.Text);
  1464. } else {
  1465. return false;
  1466. }
  1467. } else {
  1468. if ((format.Name == DataFormats.Rtf) && !(this is RichTextBox)) {
  1469. return false;
  1470. }
  1471. if (!clip.GetDataPresent(format.Name)) {
  1472. return false;
  1473. }
  1474. }
  1475. if (format.Name == DataFormats.Rtf) {
  1476. ((RichTextBox)this).SelectedRtf = (string)clip.GetData(DataFormats.Rtf);
  1477. return true;
  1478. } else if (format.Name == DataFormats.UnicodeText) {
  1479. s = (string)clip.GetData(DataFormats.UnicodeText);
  1480. } else if (format.Name == DataFormats.Text) {
  1481. s = (string)clip.GetData(DataFormats.Text);
  1482. } else {
  1483. return false;
  1484. }
  1485. if (!obey_length) {
  1486. this.SelectedText = s;
  1487. } else {
  1488. if ((s.Length + document.Length) < max_length) {
  1489. this.SelectedText = s;
  1490. } else if (document.Length < max_length) {
  1491. this.SelectedText = s.Substring(0, max_length - document.Length);
  1492. }
  1493. }
  1494. return true;
  1495. }
  1496. #endregion // Private Methods
  1497. }
  1498. }