TextBoxBase.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  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-2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. //
  26. // NOT COMPLETE
  27. #define Debug
  28. using System.ComponentModel;
  29. using System.ComponentModel.Design;
  30. using System.Drawing;
  31. using System.Drawing.Text;
  32. using System.Text;
  33. using System.Runtime.InteropServices;
  34. namespace System.Windows.Forms {
  35. [DefaultEvent("TextChanged")]
  36. [Designer("System.Windows.Forms.Design.TextBoxBaseDesigner, " + Consts.AssemblySystem_Design)]
  37. public abstract class TextBoxBase : Control {
  38. #region Local Variables
  39. internal HorizontalAlignment alignment;
  40. internal bool accepts_tab;
  41. internal bool accepts_return;
  42. internal bool auto_size;
  43. internal CharacterCasing character_casing;
  44. internal bool undo;
  45. internal bool hide_selection;
  46. internal int max_length;
  47. internal bool modified;
  48. internal bool multiline;
  49. internal bool read_only;
  50. internal bool word_wrap;
  51. internal Document document;
  52. internal LineTag caret_tag; // tag our cursor is in
  53. internal int caret_pos; // position on the line our cursor is in (can be 0 = beginning of line)
  54. internal HScrollBar hscroll;
  55. internal VScrollBar vscroll;
  56. internal RichTextBoxScrollBars scrollbars;
  57. internal bool grabbed;
  58. internal bool richtext;
  59. internal int requested_height;
  60. internal int canvas_width;
  61. internal int canvas_height;
  62. internal int track_width = 20;
  63. #if Debug
  64. internal static bool draw_lines = false;
  65. #endif
  66. #endregion // Local Variables
  67. #region Internal Constructor
  68. // Constructor will go when complete, only for testing - pdb
  69. internal TextBoxBase() {
  70. alignment = HorizontalAlignment.Left;
  71. accepts_return = false;
  72. accepts_tab = false;
  73. auto_size = true;
  74. border_style = BorderStyle.Fixed3D;
  75. character_casing = CharacterCasing.Normal;
  76. undo = false;
  77. hide_selection = true;
  78. max_length = 32767;
  79. modified = false;
  80. multiline = false;
  81. read_only = false;
  82. word_wrap = true;
  83. richtext = false;
  84. document = new Document(this);
  85. document.WidthChanged += new EventHandler(document_WidthChanged);
  86. //document.CaretMoved += new EventHandler(CaretMoved);
  87. document.Wrap = true;
  88. requested_height = -1;
  89. MouseDown += new MouseEventHandler(TextBoxBase_MouseDown);
  90. MouseUp += new MouseEventHandler(TextBoxBase_MouseUp);
  91. MouseMove += new MouseEventHandler(TextBoxBase_MouseMove);
  92. SizeChanged += new EventHandler(TextBoxBase_SizeChanged);
  93. FontChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
  94. ForeColorChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
  95. scrollbars = RichTextBoxScrollBars.None;
  96. hscroll = new HScrollBar();
  97. hscroll.ValueChanged += new EventHandler(hscroll_ValueChanged);
  98. hscroll.control_style &= ~ControlStyles.Selectable;
  99. hscroll.Enabled = false;
  100. hscroll.Visible = false;
  101. vscroll = new VScrollBar();
  102. vscroll.ValueChanged += new EventHandler(vscroll_ValueChanged);
  103. vscroll.control_style &= ~ControlStyles.Selectable;
  104. vscroll.Enabled = false;
  105. vscroll.Visible = false;
  106. this.Controls.Add(hscroll);
  107. this.Controls.Add(vscroll);
  108. //SetStyle(ControlStyles.ResizeRedraw, true);
  109. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  110. SetStyle(ControlStyles.UserPaint, true);
  111. canvas_width = this.Width;
  112. canvas_height = this.Height;
  113. CalculateScrollBars();
  114. }
  115. #endregion // Internal Constructor
  116. #region Private and Internal Methods
  117. internal string CaseAdjust(string s) {
  118. if (character_casing == CharacterCasing.Normal) {
  119. return s;
  120. }
  121. if (character_casing == CharacterCasing.Lower) {
  122. return s.ToLower();
  123. } else {
  124. return s.ToUpper();
  125. }
  126. }
  127. #endregion // Private and Internal Methods
  128. #region Public Instance Properties
  129. [DefaultValue(false)]
  130. public bool AcceptsTab {
  131. get {
  132. return accepts_tab;
  133. }
  134. set {
  135. if (value != accepts_tab) {
  136. accepts_tab = value;
  137. OnAcceptsTabChanged(EventArgs.Empty);
  138. }
  139. }
  140. }
  141. [DefaultValue(true)]
  142. [Localizable(true)]
  143. [RefreshProperties(RefreshProperties.Repaint)]
  144. public virtual bool AutoSize {
  145. get {
  146. return auto_size;
  147. }
  148. set {
  149. if (value != auto_size) {
  150. auto_size = value;
  151. if (auto_size) {
  152. if (PreferredHeight != Height) {
  153. Height = PreferredHeight;
  154. }
  155. }
  156. OnAutoSizeChanged(EventArgs.Empty);
  157. }
  158. }
  159. }
  160. [DispId(-501)]
  161. public override System.Drawing.Color BackColor {
  162. get {
  163. return base.BackColor;
  164. }
  165. set {
  166. base.BackColor = value;
  167. }
  168. }
  169. [Browsable(false)]
  170. [EditorBrowsable(EditorBrowsableState.Never)]
  171. public override System.Drawing.Image BackgroundImage {
  172. get {
  173. return base.BackgroundImage;
  174. }
  175. set {
  176. base.BackgroundImage = value;
  177. }
  178. }
  179. [DefaultValue(BorderStyle.Fixed3D)]
  180. [DispId(-504)]
  181. public BorderStyle BorderStyle {
  182. get {
  183. return border_style;
  184. }
  185. set {
  186. if (value != border_style) {
  187. border_style = value;
  188. OnBorderStyleChanged(EventArgs.Empty);
  189. }
  190. }
  191. }
  192. [Browsable(false)]
  193. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  194. public bool CanUndo {
  195. get {
  196. return undo;
  197. }
  198. }
  199. [DispId(-513)]
  200. public override System.Drawing.Color ForeColor {
  201. get {
  202. return base.ForeColor;
  203. }
  204. set {
  205. base.ForeColor = value;
  206. }
  207. }
  208. [DefaultValue(true)]
  209. public bool HideSelection {
  210. get {
  211. return hide_selection;
  212. }
  213. set {
  214. if (value != hide_selection) {
  215. hide_selection = value;
  216. OnHideSelectionChanged(EventArgs.Empty);
  217. }
  218. if (hide_selection) {
  219. document.selection_visible = false;
  220. } else {
  221. document.selection_visible = true;
  222. }
  223. document.InvalidateSelectionArea();
  224. }
  225. }
  226. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  227. [Editor("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
  228. [Localizable(true)]
  229. public string[] Lines {
  230. get {
  231. string[] lines;
  232. int i;
  233. int l;
  234. l = document.Lines;
  235. lines = new string[l];
  236. for (i = 1; i <= l; i++) {
  237. lines[i - 1] = document.GetLine(i).text.ToString();
  238. }
  239. return lines;
  240. }
  241. set {
  242. int i;
  243. int l;
  244. Brush brush;
  245. document.Empty();
  246. l = value.Length;
  247. brush = ThemeEngine.Current.ResPool.GetSolidBrush(this.ForeColor);
  248. for (i = 0; i < l; i++) {
  249. document.Add(i+1, CaseAdjust(value[i]), alignment, Font, brush);
  250. }
  251. CalculateDocument();
  252. OnTextChanged(EventArgs.Empty);
  253. }
  254. }
  255. [DefaultValue(32767)]
  256. [Localizable(true)]
  257. public virtual int MaxLength {
  258. get {
  259. return max_length;
  260. }
  261. set {
  262. if (value != max_length) {
  263. max_length = value;
  264. }
  265. }
  266. }
  267. [Browsable(false)]
  268. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  269. public bool Modified {
  270. get {
  271. return modified;
  272. }
  273. set {
  274. if (value != modified) {
  275. modified = value;
  276. OnModifiedChanged(EventArgs.Empty);
  277. }
  278. }
  279. }
  280. [DefaultValue(false)]
  281. [Localizable(true)]
  282. [RefreshProperties(RefreshProperties.All)]
  283. public virtual bool Multiline {
  284. get {
  285. return multiline;
  286. }
  287. set {
  288. if (value != multiline) {
  289. multiline = value;
  290. // Make sure we update our size; the user may have already set the size before going to multiline
  291. if (multiline && requested_height != -1) {
  292. Height = requested_height;
  293. requested_height = -1;
  294. }
  295. OnMultilineChanged(EventArgs.Empty);
  296. }
  297. document.multiline = multiline;
  298. }
  299. }
  300. [Browsable(false)]
  301. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  302. [EditorBrowsable(EditorBrowsableState.Advanced)]
  303. public int PreferredHeight {
  304. get {
  305. return this.Font.Height + 7; // FIXME - consider border style as well
  306. }
  307. }
  308. [DefaultValue(false)]
  309. public bool ReadOnly {
  310. get {
  311. return read_only;
  312. }
  313. set {
  314. if (value != read_only) {
  315. read_only = value;
  316. OnReadOnlyChanged(EventArgs.Empty);
  317. }
  318. }
  319. }
  320. [Browsable(false)]
  321. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  322. public virtual string SelectedText {
  323. get {
  324. return document.GetSelection();
  325. }
  326. set {
  327. if (!read_only) {
  328. document.ReplaceSelection(CaseAdjust(value));
  329. OnTextChanged(EventArgs.Empty);
  330. }
  331. }
  332. }
  333. [Browsable(false)]
  334. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  335. public virtual int SelectionLength {
  336. get {
  337. return document.SelectionLength();
  338. }
  339. set {
  340. if (value != 0) {
  341. int start;
  342. Line line;
  343. LineTag tag;
  344. int pos;
  345. start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
  346. document.CharIndexToLineTag(start + value, out line, out tag, out pos);
  347. document.SetSelectionEnd(line, pos);
  348. document.PositionCaret(line, pos);
  349. } else {
  350. document.SetSelectionEnd(document.selection_start.line, document.selection_start.pos);
  351. document.PositionCaret(document.selection_start.line, document.selection_start.pos);
  352. }
  353. }
  354. }
  355. [Browsable(false)]
  356. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  357. public int SelectionStart {
  358. get {
  359. int index;
  360. index = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
  361. return index;
  362. }
  363. set {
  364. Line line;
  365. LineTag tag;
  366. int pos;
  367. document.CharIndexToLineTag(value, out line, out tag, out pos);
  368. document.SetSelectionStart(line, pos);
  369. }
  370. }
  371. [Localizable(true)]
  372. public override string Text {
  373. get {
  374. if (document == null || document.Root == null || document.Root.text == null) {
  375. return string.Empty;
  376. }
  377. if (!multiline) {
  378. return document.Root.text.ToString();
  379. } else {
  380. StringBuilder sb;
  381. int i;
  382. sb = new StringBuilder();
  383. for (i = 1; i < document.Lines; i++) {
  384. sb.Append(document.GetLine(i).text.ToString() + Environment.NewLine);
  385. }
  386. return sb.ToString();
  387. }
  388. }
  389. set {
  390. if (value == base.Text) {
  391. return;
  392. }
  393. if (value != null) {
  394. Line line;
  395. if (multiline) {
  396. string[] lines;
  397. lines = value.Split(new char[] {'\n'});
  398. for (int i = 0; i < lines.Length; i++) {
  399. if (lines[i].EndsWith("\r")) {
  400. lines[i] = lines[i].Substring(0, lines[i].Length - 1);
  401. }
  402. }
  403. this.Lines = lines;
  404. line = document.GetLine(1);
  405. document.SetSelectionStart(line, 0);
  406. line = document.GetLine(document.Lines);
  407. document.SetSelectionEnd(line, line.text.Length);
  408. document.PositionCaret(line, line.text.Length);
  409. } else {
  410. document.Clear();
  411. document.Add(1, CaseAdjust(value), alignment, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
  412. CalculateDocument();
  413. line = document.GetLine(1);
  414. document.SetSelectionStart(line, 0);
  415. document.SetSelectionEnd(line, value.Length);
  416. document.PositionCaret(line, value.Length);
  417. }
  418. }
  419. base.Text = value;
  420. OnTextChanged(EventArgs.Empty);
  421. }
  422. }
  423. [Browsable(false)]
  424. public virtual int TextLength {
  425. get {
  426. if (document == null || document.Root == null || document.Root.text == null) {
  427. return 0;
  428. }
  429. if (!multiline) {
  430. return document.Root.text.Length;
  431. } else {
  432. int total;
  433. int i;
  434. total = 0;
  435. for (i = 1; i < document.Lines; i++) {
  436. total += document.GetLine(i).text.Length + Environment.NewLine.Length;
  437. }
  438. return total;
  439. }
  440. }
  441. }
  442. [DefaultValue(true)]
  443. [Localizable(true)]
  444. public bool WordWrap {
  445. get {
  446. return word_wrap;
  447. }
  448. set {
  449. if (value != word_wrap) {
  450. word_wrap = value;
  451. document.Wrap = value;
  452. }
  453. }
  454. }
  455. #endregion // Public Instance Properties
  456. #region Protected Instance Properties
  457. protected override CreateParams CreateParams {
  458. get {
  459. return base.CreateParams;
  460. }
  461. }
  462. protected override System.Drawing.Size DefaultSize {
  463. get {
  464. return base.DefaultSize;
  465. }
  466. }
  467. #endregion // Protected Instance Properties
  468. #region Public Instance Methods
  469. public void AppendText(string text) {
  470. if (multiline) {
  471. string[] lines;
  472. int linecount;
  473. // Break the string into separate lines
  474. lines = text.Split(new char[] {'\n'});
  475. linecount = lines.Length;
  476. for (int i = 0; i < linecount; i++) {
  477. if (lines[i].EndsWith("\r")) {
  478. lines[i] = lines[i].Substring(0, lines[i].Length - 1);
  479. }
  480. }
  481. // Grab the formatting for the last element
  482. document.MoveCaret(CaretDirection.CtrlEnd);
  483. // Insert the first line
  484. document.InsertString(document.CaretLine, document.CaretPosition, lines[0]);
  485. for (int i = 1; i < linecount; i++) {
  486. document.Add(document.CaretLine.LineNo+i, CaseAdjust(lines[i]), alignment, document.CaretTag.font, document.CaretTag.color);
  487. }
  488. CalculateDocument();
  489. document.MoveCaret(CaretDirection.CtrlEnd);
  490. } else {
  491. document.MoveCaret(CaretDirection.CtrlEnd);
  492. document.InsertStringAtCaret(text, true);
  493. Invalidate();
  494. }
  495. OnTextChanged(EventArgs.Empty);
  496. }
  497. public void Clear() {
  498. Text = null;
  499. }
  500. public void ClearUndo() {
  501. // FIXME
  502. throw new NotImplementedException();
  503. }
  504. public void Copy() {
  505. // FIXME
  506. throw new NotImplementedException();
  507. }
  508. public void Cut() {
  509. // FIXME
  510. throw new NotImplementedException();
  511. }
  512. public void Paste() {
  513. // FIXME
  514. throw new NotImplementedException();
  515. }
  516. public void ScrollToCaret() {
  517. // FIXME
  518. throw new NotImplementedException();
  519. }
  520. public void Select(int start, int length) {
  521. SelectionStart = start;
  522. SelectionLength = length;
  523. }
  524. public void SelectAll() {
  525. Line last;
  526. last = document.GetLine(document.Lines);
  527. document.SetSelectionStart(document.GetLine(1), 0);
  528. document.SetSelectionEnd(last, last.text.Length);
  529. }
  530. public override string ToString() {
  531. StringBuilder sb;
  532. int i;
  533. int end;
  534. if (document == null) {
  535. return String.Empty;
  536. }
  537. sb = new StringBuilder();
  538. end = document.Lines;
  539. for (i = 1; i < end; i++) {
  540. sb.Append(document.GetLine(i).text.ToString() + "\n");
  541. }
  542. return sb.ToString();
  543. }
  544. public void Undo() {
  545. return;
  546. }
  547. #endregion // Public Instance Methods
  548. #region Protected Instance Methods
  549. protected override void CreateHandle() {
  550. base.CreateHandle ();
  551. }
  552. protected override bool IsInputKey(Keys keyData) {
  553. switch (keyData) {
  554. #if not
  555. // We handle Enter in ProcessDialogKey
  556. case Keys.Enter: {
  557. if (multiline && (accepts_return || ((keyData & Keys.Control) != 0))) {
  558. return true;
  559. }
  560. return false;
  561. }
  562. #endif
  563. case Keys.Tab: {
  564. if (accepts_tab) {
  565. return true;
  566. }
  567. return false;
  568. }
  569. }
  570. return false;
  571. }
  572. protected virtual void OnAcceptsTabChanged(EventArgs e) {
  573. if (AcceptsTabChanged != null) {
  574. AcceptsTabChanged(this, e);
  575. }
  576. }
  577. protected virtual void OnAutoSizeChanged(EventArgs e) {
  578. if (AutoSizeChanged != null) {
  579. AutoSizeChanged(this, e);
  580. }
  581. }
  582. protected virtual void OnBorderStyleChanged(EventArgs e) {
  583. if (BorderStyleChanged != null) {
  584. BorderStyleChanged(this, e);
  585. }
  586. }
  587. protected override void OnFontChanged(EventArgs e) {
  588. base.OnFontChanged (e);
  589. if (auto_size) {
  590. if (PreferredHeight != Height) {
  591. Height = PreferredHeight;
  592. }
  593. }
  594. }
  595. protected override void OnHandleCreated(EventArgs e) {
  596. base.OnHandleCreated (e);
  597. }
  598. protected override void OnHandleDestroyed(EventArgs e) {
  599. base.OnHandleDestroyed (e);
  600. }
  601. protected virtual void OnHideSelectionChanged(EventArgs e) {
  602. if (HideSelectionChanged != null) {
  603. HideSelectionChanged(this, e);
  604. }
  605. }
  606. protected virtual void OnModifiedChanged(EventArgs e) {
  607. if (ModifiedChanged != null) {
  608. ModifiedChanged(this, e);
  609. }
  610. }
  611. protected virtual void OnMultilineChanged(EventArgs e) {
  612. if (MultilineChanged != null) {
  613. MultilineChanged(this, e);
  614. }
  615. }
  616. protected virtual void OnReadOnlyChanged(EventArgs e) {
  617. if (ReadOnlyChanged != null) {
  618. ReadOnlyChanged(this, e);
  619. }
  620. }
  621. protected override bool ProcessDialogKey(Keys keyData) {
  622. switch (keyData & Keys.KeyCode) {
  623. case Keys.Left: {
  624. document.SetSelectionToCaret(true);
  625. if ((Control.ModifierKeys & Keys.Control) != 0) {
  626. document.MoveCaret(CaretDirection.WordBack);
  627. } else {
  628. document.MoveCaret(CaretDirection.CharBack);
  629. }
  630. CaretMoved(this, null);
  631. return true;
  632. }
  633. case Keys.Right: {
  634. document.SetSelectionToCaret(true);
  635. if ((Control.ModifierKeys & Keys.Control) != 0) {
  636. document.MoveCaret(CaretDirection.WordForward);
  637. } else {
  638. document.MoveCaret(CaretDirection.CharForward);
  639. }
  640. CaretMoved(this, null);
  641. return true;
  642. }
  643. case Keys.Up: {
  644. document.SetSelectionToCaret(true);
  645. document.MoveCaret(CaretDirection.LineUp);
  646. CaretMoved(this, null);
  647. return true;
  648. }
  649. case Keys.Down: {
  650. document.SetSelectionToCaret(true);
  651. document.MoveCaret(CaretDirection.LineDown);
  652. CaretMoved(this, null);
  653. return true;
  654. }
  655. case Keys.Home: {
  656. document.SetSelectionToCaret(true);
  657. if ((Control.ModifierKeys & Keys.Control) != 0) {
  658. document.MoveCaret(CaretDirection.CtrlHome);
  659. } else {
  660. document.MoveCaret(CaretDirection.Home);
  661. }
  662. CaretMoved(this, null);
  663. return true;
  664. }
  665. case Keys.End: {
  666. document.SetSelectionToCaret(true);
  667. if ((Control.ModifierKeys & Keys.Control) != 0) {
  668. document.MoveCaret(CaretDirection.CtrlEnd);
  669. } else {
  670. document.MoveCaret(CaretDirection.End);
  671. }
  672. CaretMoved(this, null);
  673. return true;
  674. }
  675. case Keys.Enter: {
  676. if (!read_only && multiline && (accepts_return || ((Control.ModifierKeys & Keys.Control) != 0))) {
  677. Line line;
  678. if (document.selection_visible) {
  679. document.ReplaceSelection("");
  680. }
  681. document.SetSelectionToCaret(true);
  682. line = document.CaretLine;
  683. document.Split(document.CaretLine, document.CaretTag, document.CaretPosition, false);
  684. OnTextChanged(EventArgs.Empty);
  685. document.UpdateView(line, 2, 0);
  686. document.MoveCaret(CaretDirection.CharForward);
  687. CaretMoved(this, null);
  688. return true;
  689. }
  690. break;
  691. }
  692. case Keys.Tab: {
  693. if (!read_only && accepts_tab) {
  694. document.InsertChar(document.CaretLine, document.CaretPosition, '\t');
  695. if (document.selection_visible) {
  696. document.ReplaceSelection("");
  697. }
  698. document.SetSelectionToCaret(true);
  699. OnTextChanged(EventArgs.Empty);
  700. CaretMoved(this, null);
  701. return true;
  702. }
  703. break;
  704. }
  705. case Keys.Back: {
  706. if (read_only) {
  707. break;
  708. }
  709. // delete only deletes on the line, doesn't do the combine
  710. if (document.selection_visible) {
  711. document.ReplaceSelection("");
  712. }
  713. document.SetSelectionToCaret(true);
  714. if (document.CaretPosition == 0) {
  715. if (document.CaretLine.LineNo > 1) {
  716. Line line;
  717. int new_caret_pos;
  718. line = document.GetLine(document.CaretLine.LineNo - 1);
  719. new_caret_pos = line.text.Length;
  720. document.Combine(line, document.CaretLine);
  721. document.UpdateView(line, 1, 0);
  722. document.PositionCaret(line, new_caret_pos);
  723. document.UpdateCaret();
  724. OnTextChanged(EventArgs.Empty);
  725. }
  726. } else {
  727. document.DeleteChar(document.CaretTag, document.CaretPosition, false);
  728. document.MoveCaret(CaretDirection.CharBack);
  729. OnTextChanged(EventArgs.Empty);
  730. }
  731. CaretMoved(this, null);
  732. return true;
  733. }
  734. case Keys.Delete: {
  735. if (read_only) {
  736. break;
  737. }
  738. // delete only deletes on the line, doesn't do the combine
  739. if (document.CaretPosition == document.CaretLine.text.Length) {
  740. if (document.CaretLine.LineNo < document.Lines) {
  741. Line line;
  742. line = document.GetLine(document.CaretLine.LineNo + 1);
  743. document.Combine(document.CaretLine, line);
  744. document.UpdateView(document.CaretLine, 2, 0);
  745. OnTextChanged(EventArgs.Empty);
  746. #if Debug
  747. Line check_first;
  748. Line check_second;
  749. check_first = document.GetLine(document.CaretLine.LineNo);
  750. check_second = document.GetLine(check_first.line_no + 1);
  751. Console.WriteLine("Post-UpdateView: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
  752. #endif
  753. // Caret doesn't move
  754. }
  755. } else {
  756. document.DeleteChar(document.CaretTag, document.CaretPosition, true);
  757. OnTextChanged(EventArgs.Empty);
  758. }
  759. CaretMoved(this, null);
  760. return true;
  761. }
  762. }
  763. return base.ProcessDialogKey (keyData);
  764. }
  765. protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
  766. // Make sure we don't get sized bigger than we want to be
  767. if (!richtext) {
  768. if (!multiline) {
  769. if (height > PreferredHeight) {
  770. requested_height = height;
  771. height = PreferredHeight;
  772. specified |= BoundsSpecified.Height;
  773. }
  774. }
  775. }
  776. document.ViewPortWidth = this.Width;
  777. document.ViewPortHeight = this.Height;
  778. CalculateDocument();
  779. base.SetBoundsCore (x, y, width, height, specified);
  780. }
  781. protected override void WndProc(ref Message m) {
  782. switch ((Msg)m.Msg) {
  783. case Msg.WM_PAINT: {
  784. PaintEventArgs paint_event;
  785. paint_event = XplatUI.PaintEventStart(Handle);
  786. PaintControl(paint_event);
  787. XplatUI.PaintEventEnd(Handle);
  788. DefWndProc(ref m);
  789. return;
  790. }
  791. case Msg.WM_SETFOCUS: {
  792. // Set caret
  793. document.CaretHasFocus();
  794. Console.WriteLine("Creating caret");
  795. base.WndProc(ref m);
  796. return;
  797. }
  798. case Msg.WM_KILLFOCUS: {
  799. // Kill caret
  800. document.CaretLostFocus();
  801. Console.WriteLine("Destroying caret");
  802. base.WndProc(ref m);
  803. return;
  804. }
  805. case Msg.WM_CHAR: {
  806. if (ProcessKeyEventArgs(ref m)) {
  807. return;
  808. }
  809. if (PreProcessMessage(ref m)) {
  810. return;
  811. }
  812. if (ProcessKeyMessage(ref m)) {
  813. return;
  814. }
  815. if (!read_only && (m.WParam.ToInt32() >= 32)) { // FIXME, tabs should probably go through
  816. if (document.selection_visible) {
  817. document.ReplaceSelection("");
  818. }
  819. switch (character_casing) {
  820. case CharacterCasing.Normal: {
  821. document.InsertCharAtCaret((char)m.WParam, true);
  822. OnTextChanged(EventArgs.Empty);
  823. CaretMoved(this, null);
  824. return;
  825. }
  826. case CharacterCasing.Lower: {
  827. document.InsertCharAtCaret(Char.ToLower((char)m.WParam), true);
  828. OnTextChanged(EventArgs.Empty);
  829. CaretMoved(this, null);
  830. return;
  831. }
  832. case CharacterCasing.Upper: {
  833. document.InsertCharAtCaret(Char.ToUpper((char)m.WParam), true);
  834. OnTextChanged(EventArgs.Empty);
  835. CaretMoved(this, null);
  836. return;
  837. }
  838. }
  839. }
  840. DefWndProc(ref m);
  841. return;
  842. }
  843. default: {
  844. base.WndProc(ref m);
  845. return;
  846. }
  847. }
  848. }
  849. #endregion // Protected Instance Methods
  850. #region Events
  851. public event EventHandler AcceptsTabChanged;
  852. public event EventHandler AutoSizeChanged;
  853. [Browsable(false)]
  854. [EditorBrowsable(EditorBrowsableState.Never)]
  855. public new event EventHandler BackgroundImageChanged {
  856. add { base.BackgroundImageChanged += value; }
  857. remove { base.BackgroundImageChanged -= value; }
  858. }
  859. public event EventHandler BorderStyleChanged;
  860. [Browsable(false)]
  861. [EditorBrowsable(EditorBrowsableState.Advanced)]
  862. public event EventHandler Click;
  863. public event EventHandler HideSelectionChanged;
  864. public event EventHandler ModifiedChanged;
  865. public event EventHandler MultilineChanged;
  866. [Browsable(false)]
  867. [EditorBrowsable(EditorBrowsableState.Never)]
  868. public event PaintEventHandler Paint;
  869. public event EventHandler ReadOnlyChanged;
  870. #endregion // Events
  871. #region Private Methods
  872. internal Document Document {
  873. get {
  874. return document;
  875. }
  876. set {
  877. document = value;
  878. }
  879. }
  880. static int current;
  881. private void PaintControl(PaintEventArgs pevent) {
  882. // Fill background
  883. pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), pevent.ClipRectangle);
  884. pevent.Graphics.TextRenderingHint=TextRenderingHint.AntiAlias;
  885. // Draw the viewable document
  886. document.Draw(pevent.Graphics, pevent.ClipRectangle);
  887. Rectangle rect = ClientRectangle;
  888. rect.Width--;
  889. rect.Height--;
  890. //pevent.Graphics.DrawRectangle(ThemeEngine.Current.ResPool.GetPen(ThemeEngine.Current.ColorButtonShadow), rect);
  891. #if Debug
  892. int start;
  893. int end;
  894. Line line;
  895. int line_no;
  896. Pen p;
  897. p = new Pen(Color.Red, 1);
  898. // First, figure out from what line to what line we need to draw
  899. start = document.GetLineByPixel(pevent.ClipRectangle.Top - document.ViewPortY, false).line_no;
  900. end = document.GetLineByPixel(pevent.ClipRectangle.Bottom - document.ViewPortY, false).line_no;
  901. //Console.WriteLine("Starting drawing on line '{0}'", document.GetLine(start));
  902. //Console.WriteLine("Ending drawing on line '{0}'", document.GetLine(end));
  903. line_no = start;
  904. while (line_no <= end) {
  905. line = document.GetLine(line_no);
  906. if (draw_lines) {
  907. for (int i = 0; i < line.text.Length; i++) {
  908. 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);
  909. }
  910. }
  911. line_no++;
  912. }
  913. #endif
  914. }
  915. private void TextBoxBase_MouseDown(object sender, MouseEventArgs e) {
  916. LineTag tag;
  917. Line line;
  918. int pos;
  919. if (e.Button == MouseButtons.Left) {
  920. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  921. document.SetSelectionToCaret(true);
  922. this.grabbed = true;
  923. this.Capture = true;
  924. return;
  925. }
  926. #if Debug
  927. if (e.Button == MouseButtons.Right) {
  928. draw_lines = !draw_lines;
  929. this.Invalidate();
  930. Console.WriteLine("SelectedText: {0}, length {1}", this.SelectedText, this.SelectionLength);
  931. Console.WriteLine("Selection start: {0}", this.SelectionStart);
  932. this.SelectionStart = 10;
  933. this.SelectionLength = 5;
  934. return;
  935. }
  936. tag = document.FindTag(e.X + document.ViewPortX, e.Y + document.ViewPortY, out pos, false);
  937. Console.WriteLine("Click found tag {0}, character {1}", tag, pos);
  938. line = tag.line;
  939. switch(current) {
  940. 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;
  941. 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;
  942. 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;
  943. 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;
  944. 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;
  945. 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;
  946. }
  947. current++;
  948. if (current==6) {
  949. current=0;
  950. }
  951. // Update/Recalculate what we see
  952. document.UpdateView(line, 0);
  953. // Make sure our caret is properly positioned and sized
  954. document.AlignCaret();
  955. #endif
  956. }
  957. private void TextBoxBase_MouseUp(object sender, MouseEventArgs e) {
  958. this.Capture = false;
  959. this.grabbed = false;
  960. if (e.Button == MouseButtons.Left) {
  961. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  962. document.SetSelectionToCaret(false);
  963. document.DisplayCaret();
  964. return;
  965. }
  966. }
  967. #endregion // Private Methods
  968. private void TextBoxBase_SizeChanged(object sender, EventArgs e) {
  969. canvas_width = this.Width;
  970. canvas_height = this.Height;
  971. // We always move them, they just might not be displayed
  972. hscroll.Bounds = new Rectangle (ClientRectangle.Left, ClientRectangle.Bottom - hscroll.Height, Width, hscroll.Height);
  973. vscroll.Bounds = new Rectangle (ClientRectangle.Right - vscroll.Width, ClientRectangle.Top, vscroll.Width, Height);
  974. }
  975. private void CalculateDocument() {
  976. document.RecalculateDocument(CreateGraphics());
  977. CalculateScrollBars();
  978. Invalidate(); // FIXME - do we need this?
  979. }
  980. protected void CalculateScrollBars() {
  981. // No scrollbars for a single line
  982. if (document.Width >= this.Width) {
  983. hscroll.Enabled = true;
  984. hscroll.Minimum = 0;
  985. hscroll.Maximum = document.Width - this.Width;
  986. } else {
  987. hscroll.Enabled = false;
  988. }
  989. if (document.Height >= this.Height) {
  990. vscroll.Enabled = true;
  991. vscroll.Minimum = 0;
  992. vscroll.Maximum = document.Height - this.Height;
  993. } else {
  994. vscroll.Enabled = false;
  995. }
  996. if (!multiline) {
  997. return;
  998. }
  999. if ((scrollbars & RichTextBoxScrollBars.Horizontal) != 0) {
  1000. if (((scrollbars & RichTextBoxScrollBars.ForcedHorizontal) != 0) || hscroll.Enabled) {
  1001. hscroll.Visible = true;
  1002. }
  1003. }
  1004. if ((scrollbars & RichTextBoxScrollBars.Vertical) != 0) {
  1005. if (((scrollbars & RichTextBoxScrollBars.ForcedVertical) != 0) || vscroll.Enabled) {
  1006. vscroll.Visible = true;
  1007. }
  1008. }
  1009. if (hscroll.Visible) {
  1010. vscroll.Maximum += hscroll.Height;
  1011. canvas_height = this.Height - hscroll.Height;
  1012. }
  1013. if (vscroll.Visible) {
  1014. hscroll.Maximum += vscroll.Width * 2;
  1015. canvas_width = this.Width - vscroll.Width * 2;
  1016. }
  1017. }
  1018. private void document_WidthChanged(object sender, EventArgs e) {
  1019. CalculateScrollBars();
  1020. }
  1021. private void hscroll_ValueChanged(object sender, EventArgs e) {
  1022. XplatUI.ScrollWindow(this.Handle, document.ViewPortX-this.hscroll.Value, 0, false);
  1023. document.ViewPortX = this.hscroll.Value;
  1024. document.UpdateCaret();
  1025. //Console.WriteLine("Dude scrolled horizontal");
  1026. }
  1027. private void vscroll_ValueChanged(object sender, EventArgs e) {
  1028. XplatUI.ScrollWindow(this.Handle, 0, document.ViewPortY-this.vscroll.Value, false);
  1029. document.ViewPortY = this.vscroll.Value;
  1030. document.UpdateCaret();
  1031. //Console.WriteLine("Dude scrolled vertical");
  1032. }
  1033. private void TextBoxBase_MouseMove(object sender, MouseEventArgs e) {
  1034. // FIXME - handle auto-scrolling if mouse is to the right/left of the window
  1035. if (grabbed) {
  1036. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  1037. document.SetSelectionToCaret(false);
  1038. document.DisplayCaret();
  1039. }
  1040. }
  1041. private void TextBoxBase_FontOrColorChanged(object sender, EventArgs e) {
  1042. if (!richtext) {
  1043. Line line;
  1044. // Font changes apply to the whole document
  1045. for (int i = 1; i <= document.Lines; i++) {
  1046. line = document.GetLine(i);
  1047. LineTag.FormatText(line, 1, line.text.Length, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
  1048. document.UpdateView(line, 0);
  1049. }
  1050. // Make sure the caret height is matching the new font height
  1051. document.AlignCaret();
  1052. }
  1053. }
  1054. /// <summary>Ensure the caret is always visible</summary>
  1055. internal void CaretMoved(object sender, EventArgs e) {
  1056. Point pos;
  1057. int height;
  1058. pos = document.Caret;
  1059. //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);
  1060. // Handle horizontal scrolling
  1061. if (pos.X < (document.ViewPortX + track_width)) {
  1062. do {
  1063. if ((hscroll.Value - track_width) >= hscroll.Minimum) {
  1064. hscroll.Value -= track_width;
  1065. } else {
  1066. hscroll.Value = hscroll.Minimum;
  1067. }
  1068. } while (hscroll.Value > pos.X);
  1069. }
  1070. if ((pos.X > (this.canvas_width + document.ViewPortX - track_width)) && (hscroll.Value != hscroll.Maximum)) {
  1071. do {
  1072. if ((hscroll.Value + track_width) <= hscroll.Maximum) {
  1073. hscroll.Value += track_width;
  1074. } else {
  1075. hscroll.Value = hscroll.Maximum;
  1076. }
  1077. } while (pos.X > (hscroll.Value + this.canvas_width));
  1078. }
  1079. if (!multiline) {
  1080. return;
  1081. }
  1082. // Handle vertical scrolling
  1083. height = document.CaretLine.Height;
  1084. if (pos.Y < document.ViewPortY) {
  1085. vscroll.Value = pos.Y;
  1086. }
  1087. if ((pos.Y + height) > (document.ViewPortY + canvas_height)) {
  1088. vscroll.Value = pos.Y - canvas_height + height;
  1089. }
  1090. }
  1091. }
  1092. }