TextBoxBase.cs 35 KB

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