TextBoxBase.cs 29 KB

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