TextBoxBase.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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 = 0; i < l; i++) {
  206. lines[i] = 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 bool WordWrap {
  367. get {
  368. return word_wrap;
  369. }
  370. set {
  371. if (value != word_wrap) {
  372. word_wrap = value;
  373. }
  374. }
  375. }
  376. #endregion // Public Instance Properties
  377. #region Protected Instance Properties
  378. protected override CreateParams CreateParams {
  379. get {
  380. return base.CreateParams;
  381. }
  382. }
  383. protected override System.Drawing.Size DefaultSize {
  384. get {
  385. return base.DefaultSize;
  386. }
  387. }
  388. #endregion // Protected Instance Properties
  389. #region Public Instance Methods
  390. public void AppendText(string text) {
  391. // FIXME
  392. throw new NotImplementedException();
  393. }
  394. public void Clear() {
  395. // FIXME
  396. throw new NotImplementedException();
  397. }
  398. public void ClearUndo() {
  399. // FIXME
  400. throw new NotImplementedException();
  401. }
  402. public void Copy() {
  403. // FIXME
  404. throw new NotImplementedException();
  405. }
  406. public void Cut() {
  407. // FIXME
  408. throw new NotImplementedException();
  409. }
  410. public void Paste() {
  411. // FIXME
  412. throw new NotImplementedException();
  413. }
  414. public void ScrollToCaret() {
  415. // FIXME
  416. throw new NotImplementedException();
  417. }
  418. public void Select(int start, int length) {
  419. // FIXME
  420. throw new NotImplementedException();
  421. }
  422. public void SelectAll() {
  423. Line last;
  424. last = document.GetLine(document.Lines);
  425. document.SetSelectionStart(document.GetLine(1), 0);
  426. document.SetSelectionEnd(last, last.text.Length);
  427. }
  428. public override string ToString() {
  429. StringBuilder sb;
  430. int i;
  431. int end;
  432. sb = new StringBuilder();
  433. end = document.Lines;
  434. for (i = 1; i < end; i++) {
  435. sb.Append(document.GetLine(i).text.ToString() + "\n");
  436. }
  437. return sb.ToString();
  438. }
  439. public void Undo() {
  440. return;
  441. }
  442. #endregion // Public Instance Methods
  443. #region Protected Instance Methods
  444. protected override void CreateHandle() {
  445. base.CreateHandle ();
  446. }
  447. protected override bool IsInputKey(Keys keyData) {
  448. switch (keyData) {
  449. case Keys.Enter: {
  450. if (multiline && (accepts_return || ((keyData & Keys.Control) != 0))) {
  451. return true;
  452. }
  453. return false;
  454. }
  455. case Keys.Tab: {
  456. if (accepts_tab) {
  457. return true;
  458. }
  459. return false;
  460. }
  461. }
  462. return false;
  463. }
  464. protected virtual void OnAcceptsTabChanged(EventArgs e) {
  465. if (AcceptsTabChanged != null) {
  466. AcceptsTabChanged(this, e);
  467. }
  468. }
  469. protected virtual void OnAutoSizeChanged(EventArgs e) {
  470. if (AutoSizeChanged != null) {
  471. AutoSizeChanged(this, e);
  472. }
  473. }
  474. protected virtual void OnBorderStyleChanged(EventArgs e) {
  475. if (BorderStyleChanged != null) {
  476. BorderStyleChanged(this, e);
  477. }
  478. }
  479. protected override void OnFontChanged(EventArgs e) {
  480. base.OnFontChanged (e);
  481. if (auto_size) {
  482. if (PreferredHeight != Height) {
  483. Height = PreferredHeight;
  484. }
  485. }
  486. }
  487. protected override void OnHandleCreated(EventArgs e) {
  488. base.OnHandleCreated (e);
  489. }
  490. protected override void OnHandleDestroyed(EventArgs e) {
  491. base.OnHandleDestroyed (e);
  492. }
  493. protected virtual void OnHideSelectionChanged(EventArgs e) {
  494. if (HideSelectionChanged != null) {
  495. HideSelectionChanged(this, e);
  496. }
  497. }
  498. protected virtual void OnModifiedChanged(EventArgs e) {
  499. if (ModifiedChanged != null) {
  500. ModifiedChanged(this, e);
  501. }
  502. }
  503. protected virtual void OnMultilineChanged(EventArgs e) {
  504. if (MultilineChanged != null) {
  505. MultilineChanged(this, e);
  506. }
  507. }
  508. protected virtual void OnReadOnlyChanged(EventArgs e) {
  509. if (ReadOnlyChanged != null) {
  510. ReadOnlyChanged(this, e);
  511. }
  512. }
  513. protected override bool ProcessDialogKey(Keys keyData) {
  514. return base.ProcessDialogKey (keyData);
  515. }
  516. protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
  517. // Make sure we don't get sized bigger than we want to be
  518. if (!richtext) {
  519. if (!multiline) {
  520. if (height > PreferredHeight) {
  521. text_bounds = new Rectangle(x, y, width, height);
  522. height = PreferredHeight;
  523. }
  524. }
  525. }
  526. base.SetBoundsCore (x, y, width, height, specified);
  527. }
  528. protected override void WndProc(ref Message m) {
  529. switch ((Msg)m.Msg) {
  530. case Msg.WM_PAINT: {
  531. PaintEventArgs paint_event;
  532. #if !__MonoCS__
  533. XplatUIWin32.Win32SetFocus(Handle);
  534. #endif
  535. paint_event = XplatUI.PaintEventStart(Handle);
  536. PaintControl(paint_event);
  537. XplatUI.PaintEventEnd(Handle);
  538. DefWndProc(ref m);
  539. return;
  540. }
  541. case Msg.WM_SETFOCUS: {
  542. // Set caret
  543. document.CaretHasFocus();
  544. Console.WriteLine("Creating caret");
  545. base.WndProc(ref m);
  546. return;
  547. }
  548. case Msg.WM_KILLFOCUS: {
  549. // Kill caret
  550. document.CaretLostFocus();
  551. Console.WriteLine("Destroying caret");
  552. base.WndProc(ref m);
  553. return;
  554. }
  555. case Msg.WM_KEYDOWN: {
  556. switch ((Keys)(m.WParam.ToInt32())) {
  557. case Keys.Left: {
  558. if ((Control.ModifierKeys & Keys.Control) != 0) {
  559. document.MoveCaret(CaretDirection.WordBack);
  560. } else {
  561. document.MoveCaret(CaretDirection.CharBack);
  562. }
  563. return;
  564. }
  565. case Keys.Right: {
  566. if ((Control.ModifierKeys & Keys.Control) != 0) {
  567. document.MoveCaret(CaretDirection.WordForward);
  568. } else {
  569. document.MoveCaret(CaretDirection.CharForward);
  570. }
  571. return;
  572. }
  573. case Keys.Up: {
  574. document.MoveCaret(CaretDirection.LineUp);
  575. return;
  576. }
  577. case Keys.Down: {
  578. document.DumpTree(document.Root, true);
  579. document.MoveCaret(CaretDirection.LineDown);
  580. return;
  581. }
  582. case Keys.Home: {
  583. if ((Control.ModifierKeys & Keys.Control) != 0) {
  584. document.MoveCaret(CaretDirection.CtrlHome);
  585. } else {
  586. document.MoveCaret(CaretDirection.Home);
  587. }
  588. return;
  589. }
  590. case Keys.End: {
  591. if ((Control.ModifierKeys & Keys.Control) != 0) {
  592. document.MoveCaret(CaretDirection.CtrlEnd);
  593. } else {
  594. document.MoveCaret(CaretDirection.End);
  595. }
  596. return;
  597. }
  598. case Keys.Enter: {
  599. if (multiline && (accepts_return || ((Control.ModifierKeys & Keys.Control) != 0))) {
  600. document.Split(document.CaretLine, document.CaretTag, document.CaretPosition);
  601. OnTextChanged(EventArgs.Empty);
  602. document.UpdateView(document.CaretLine, 2, 0);
  603. document.MoveCaret(CaretDirection.CharForward);
  604. }
  605. return;
  606. }
  607. case Keys.Tab: {
  608. if (accepts_tab) {
  609. document.InsertChar(document.CaretLine, document.CaretPosition, '\t');
  610. OnTextChanged(EventArgs.Empty);
  611. }
  612. return;
  613. }
  614. case Keys.Back: {
  615. // delete only deletes on the line, doesn't do the combine
  616. if (document.CaretPosition == 0) {
  617. if (document.CaretLine.LineNo > 1) {
  618. Line line;
  619. int new_caret_pos;
  620. line = document.GetLine(document.CaretLine.LineNo - 1);
  621. new_caret_pos = line.text.Length;
  622. document.Combine(line, document.CaretLine);
  623. document.UpdateView(line, 1, 0);
  624. document.PositionCaret(line, new_caret_pos);
  625. document.UpdateCaret();
  626. OnTextChanged(EventArgs.Empty);
  627. }
  628. } else {
  629. document.DeleteChar(document.CaretTag, document.CaretPosition, false);
  630. document.MoveCaret(CaretDirection.CharBack);
  631. OnTextChanged(EventArgs.Empty);
  632. }
  633. return;
  634. }
  635. case Keys.Delete: {
  636. // delete only deletes on the line, doesn't do the combine
  637. if (document.CaretPosition == document.CaretLine.text.Length) {
  638. if (document.CaretLine.LineNo < document.Lines) {
  639. Line line;
  640. line = document.GetLine(document.CaretLine.LineNo + 1);
  641. document.Combine(document.CaretLine, line);
  642. document.UpdateView(document.CaretLine, 2, 0);
  643. OnTextChanged(EventArgs.Empty);
  644. #if Debug
  645. Line check_first;
  646. Line check_second;
  647. check_first = document.GetLine(document.CaretLine.LineNo);
  648. check_second = document.GetLine(check_first.line_no + 1);
  649. Console.WriteLine("Post-UpdateView: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
  650. #endif
  651. // Caret doesn't move
  652. }
  653. } else {
  654. document.DeleteChar(document.CaretTag, document.CaretPosition, true);
  655. OnTextChanged(EventArgs.Empty);
  656. }
  657. return;
  658. }
  659. }
  660. return;
  661. }
  662. case Msg.WM_CHAR: {
  663. if (m.WParam.ToInt32() >= 32) { // FIXME, tabs should probably go through
  664. switch (character_casing) {
  665. case CharacterCasing.Normal: {
  666. document.InsertCharAtCaret((char)m.WParam, true);
  667. OnTextChanged(EventArgs.Empty);
  668. return;
  669. }
  670. case CharacterCasing.Lower: {
  671. document.InsertCharAtCaret(Char.ToLower((char)m.WParam), true);
  672. OnTextChanged(EventArgs.Empty);
  673. return;
  674. }
  675. case CharacterCasing.Upper: {
  676. document.InsertCharAtCaret(Char.ToUpper((char)m.WParam), true);
  677. OnTextChanged(EventArgs.Empty);
  678. return;
  679. }
  680. }
  681. }
  682. return;
  683. }
  684. default: {
  685. base.WndProc(ref m);
  686. return;
  687. }
  688. }
  689. }
  690. #endregion // Protected Instance Methods
  691. #region Events
  692. public event EventHandler AcceptsTabChanged;
  693. public event EventHandler AutoSizeChanged;
  694. public event EventHandler BorderStyleChanged;
  695. public event EventHandler Click;
  696. public event EventHandler HideSelectionChanged;
  697. public event EventHandler ModifiedChanged;
  698. public event EventHandler MultilineChanged;
  699. public event PaintEventHandler Paint;
  700. public event EventHandler ReadOnlyChanged;
  701. #endregion // Events
  702. #region Private Methods
  703. internal Document Document {
  704. get {
  705. return document;
  706. }
  707. set {
  708. document = value;
  709. }
  710. }
  711. static int current;
  712. private void PaintControl(PaintEventArgs pevent) {
  713. // Fill background
  714. pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), pevent.ClipRectangle);
  715. pevent.Graphics.TextRenderingHint=TextRenderingHint.AntiAlias;
  716. // Draw the viewable document
  717. document.Draw(pevent.Graphics, pevent.ClipRectangle);
  718. // Set the scrollbar
  719. switch (scrollbars) {
  720. case ScrollBars.Both: {
  721. break;
  722. }
  723. case ScrollBars.Vertical: {
  724. break;
  725. }
  726. case ScrollBars.Horizontal: {
  727. hscroll.Minimum = 0;
  728. hscroll.Maximum = document.Width - this.Width;
  729. break;
  730. }
  731. }
  732. #if Debug
  733. int start;
  734. int end;
  735. Line line;
  736. int line_no;
  737. Pen p;
  738. p = new Pen(Color.Red, 1);
  739. // First, figure out from what line to what line we need to draw
  740. start = document.GetLineByPixel(pevent.ClipRectangle.Top - viewport_y, false).line_no;
  741. end = document.GetLineByPixel(pevent.ClipRectangle.Bottom - viewport_y, false).line_no;
  742. //Console.WriteLine("Starting drawing on line '{0}'", document.GetLine(start));
  743. //Console.WriteLine("Ending drawing on line '{0}'", document.GetLine(end));
  744. line_no = start;
  745. while (line_no <= end) {
  746. line = document.GetLine(line_no);
  747. if (draw_lines) {
  748. for (int i = 0; i < line.text.Length; i++) {
  749. 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);
  750. }
  751. }
  752. line_no++;
  753. }
  754. #endif
  755. }
  756. private void TextBoxBase_MouseDown(object sender, MouseEventArgs e) {
  757. LineTag tag;
  758. Line line;
  759. int pos;
  760. if (e.Button == MouseButtons.Left) {
  761. document.PositionCaret(e.X, e.Y);
  762. document.SetSelectionToCaret(true);
  763. this.grabbed = true;
  764. this.Capture = true;
  765. return;
  766. }
  767. #if Debug
  768. if (e.Button == MouseButtons.Right) {
  769. draw_lines = !draw_lines;
  770. this.Invalidate();
  771. Console.WriteLine("SelectedText: {0}, length {1}", this.SelectedText, this.SelectionLength);
  772. Console.WriteLine("Selection start: {0}", this.SelectionStart);
  773. this.SelectionStart = 10;
  774. this.SelectionLength = 5;
  775. return;
  776. }
  777. tag = document.FindTag(e.X, e.Y, out pos, false);
  778. Console.WriteLine("Click found tag {0}, character {1}", tag, pos);
  779. line = tag.line;
  780. switch(current) {
  781. 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;
  782. 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;
  783. 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;
  784. 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;
  785. 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;
  786. 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;
  787. }
  788. current++;
  789. if (current==6) {
  790. current=0;
  791. }
  792. // Update/Recalculate what we see
  793. document.UpdateView(line, 0);
  794. // Make sure our caret is properly positioned and sized
  795. document.AlignCaret();
  796. #endif
  797. }
  798. private void TextBoxBase_MouseUp(object sender, MouseEventArgs e) {
  799. this.Capture = false;
  800. this.grabbed = false;
  801. if (e.Button == MouseButtons.Left) {
  802. document.PositionCaret(e.X + viewport_x, e.Y + viewport_y);
  803. document.SetSelectionToCaret(false);
  804. document.DisplayCaret();
  805. return;
  806. }
  807. }
  808. #endregion // Private Methods
  809. private void TextBoxBase_SizeChanged(object sender, EventArgs e) {
  810. // First, check which scrollbars we need
  811. hscroll.Bounds = new Rectangle (ClientRectangle.Left, ClientRectangle.Bottom - hscroll.Height, Width, hscroll.Height);
  812. }
  813. private void hscroll_ValueChanged(object sender, EventArgs e) {
  814. XplatUI.ScrollWindow(this.Handle, document.ViewPortX-this.hscroll.Value, 0, true);
  815. document.ViewPortX = this.hscroll.Value;
  816. document.UpdateCaret();
  817. Console.WriteLine("Dude scrolled");
  818. }
  819. private void TextBoxBase_MouseMove(object sender, MouseEventArgs e) {
  820. // FIXME - handle auto-scrolling if mouse is to the right/left of the window
  821. if (grabbed) {
  822. document.PositionCaret(e.X + viewport_x, e.Y + viewport_y);
  823. document.SetSelectionToCaret(false);
  824. document.DisplayCaret();
  825. }
  826. }
  827. private void TextBoxBase_FontOrColorChanged(object sender, EventArgs e) {
  828. if (!richtext) {
  829. Line line;
  830. // Font changes apply to the whole document
  831. for (int i = 1; i <= document.Lines; i++) {
  832. line = document.GetLine(i);
  833. LineTag.FormatText(line, 1, line.text.Length, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
  834. document.UpdateView(line, 0);
  835. }
  836. // Make sure the caret height is matching the new font height
  837. document.AlignCaret();
  838. }
  839. }
  840. }
  841. }