TextBoxBase.cs 35 KB

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