TextBoxBase.cs 27 KB

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