TextBoxBase.cs 25 KB

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