TextBoxBase.cs 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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 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 = false;
  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. Invalidate();
  501. }
  502. OnTextChanged(EventArgs.Empty);
  503. }
  504. public void Clear() {
  505. Text = null;
  506. }
  507. public void ClearUndo() {
  508. // FIXME
  509. throw new NotImplementedException();
  510. }
  511. public void Copy() {
  512. // FIXME
  513. throw new NotImplementedException();
  514. }
  515. public void Cut() {
  516. // FIXME
  517. throw new NotImplementedException();
  518. }
  519. public void Paste() {
  520. // FIXME
  521. throw new NotImplementedException();
  522. }
  523. public void ScrollToCaret() {
  524. // FIXME
  525. throw new NotImplementedException();
  526. }
  527. public void Select(int start, int length) {
  528. SelectionStart = start;
  529. SelectionLength = length;
  530. }
  531. public void SelectAll() {
  532. Line last;
  533. last = document.GetLine(document.Lines);
  534. document.SetSelectionStart(document.GetLine(1), 0);
  535. document.SetSelectionEnd(last, last.text.Length);
  536. }
  537. public override string ToString() {
  538. StringBuilder sb;
  539. int i;
  540. int end;
  541. if (document == null) {
  542. return String.Empty;
  543. }
  544. sb = new StringBuilder();
  545. end = document.Lines;
  546. for (i = 1; i < end; i++) {
  547. sb.Append(document.GetLine(i).text.ToString() + "\n");
  548. }
  549. return sb.ToString();
  550. }
  551. public void Undo() {
  552. return;
  553. }
  554. #endregion // Public Instance Methods
  555. #region Protected Instance Methods
  556. protected override void CreateHandle() {
  557. base.CreateHandle ();
  558. }
  559. protected override bool IsInputKey(Keys keyData) {
  560. switch (keyData) {
  561. #if not
  562. // We handle Enter in ProcessDialogKey
  563. case Keys.Enter: {
  564. if (multiline && (accepts_return || ((keyData & Keys.Control) != 0))) {
  565. return true;
  566. }
  567. return false;
  568. }
  569. #endif
  570. case Keys.Tab: {
  571. if (accepts_tab) {
  572. return true;
  573. }
  574. return false;
  575. }
  576. }
  577. return false;
  578. }
  579. protected virtual void OnAcceptsTabChanged(EventArgs e) {
  580. if (AcceptsTabChanged != null) {
  581. AcceptsTabChanged(this, e);
  582. }
  583. }
  584. protected virtual void OnAutoSizeChanged(EventArgs e) {
  585. if (AutoSizeChanged != null) {
  586. AutoSizeChanged(this, e);
  587. }
  588. }
  589. protected virtual void OnBorderStyleChanged(EventArgs e) {
  590. if (BorderStyleChanged != null) {
  591. BorderStyleChanged(this, e);
  592. }
  593. }
  594. protected override void OnFontChanged(EventArgs e) {
  595. base.OnFontChanged (e);
  596. if (auto_size) {
  597. if (PreferredHeight != Height) {
  598. Height = PreferredHeight;
  599. }
  600. }
  601. }
  602. protected override void OnHandleCreated(EventArgs e) {
  603. base.OnHandleCreated (e);
  604. }
  605. protected override void OnHandleDestroyed(EventArgs e) {
  606. base.OnHandleDestroyed (e);
  607. }
  608. protected virtual void OnHideSelectionChanged(EventArgs e) {
  609. if (HideSelectionChanged != null) {
  610. HideSelectionChanged(this, e);
  611. }
  612. }
  613. protected virtual void OnModifiedChanged(EventArgs e) {
  614. if (ModifiedChanged != null) {
  615. ModifiedChanged(this, e);
  616. }
  617. }
  618. protected virtual void OnMultilineChanged(EventArgs e) {
  619. if (MultilineChanged != null) {
  620. MultilineChanged(this, e);
  621. }
  622. }
  623. protected virtual void OnReadOnlyChanged(EventArgs e) {
  624. if (ReadOnlyChanged != null) {
  625. ReadOnlyChanged(this, e);
  626. }
  627. }
  628. protected override bool ProcessDialogKey(Keys keyData) {
  629. switch (keyData & Keys.KeyCode) {
  630. case Keys.Left: {
  631. document.SetSelectionToCaret(true);
  632. if ((Control.ModifierKeys & Keys.Control) != 0) {
  633. document.MoveCaret(CaretDirection.WordBack);
  634. } else {
  635. document.MoveCaret(CaretDirection.CharBack);
  636. }
  637. CaretMoved(this, null);
  638. return true;
  639. }
  640. case Keys.Right: {
  641. document.SetSelectionToCaret(true);
  642. if ((Control.ModifierKeys & Keys.Control) != 0) {
  643. document.MoveCaret(CaretDirection.WordForward);
  644. } else {
  645. document.MoveCaret(CaretDirection.CharForward);
  646. }
  647. CaretMoved(this, null);
  648. return true;
  649. }
  650. case Keys.Up: {
  651. document.SetSelectionToCaret(true);
  652. document.MoveCaret(CaretDirection.LineUp);
  653. CaretMoved(this, null);
  654. return true;
  655. }
  656. case Keys.Down: {
  657. document.SetSelectionToCaret(true);
  658. document.MoveCaret(CaretDirection.LineDown);
  659. CaretMoved(this, null);
  660. return true;
  661. }
  662. case Keys.Home: {
  663. document.SetSelectionToCaret(true);
  664. if ((Control.ModifierKeys & Keys.Control) != 0) {
  665. document.MoveCaret(CaretDirection.CtrlHome);
  666. } else {
  667. document.MoveCaret(CaretDirection.Home);
  668. }
  669. CaretMoved(this, null);
  670. return true;
  671. }
  672. case Keys.End: {
  673. document.SetSelectionToCaret(true);
  674. if ((Control.ModifierKeys & Keys.Control) != 0) {
  675. document.MoveCaret(CaretDirection.CtrlEnd);
  676. } else {
  677. document.MoveCaret(CaretDirection.End);
  678. }
  679. CaretMoved(this, null);
  680. return true;
  681. }
  682. case Keys.Enter: {
  683. if (!read_only && multiline && (accepts_return || ((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.UpdateCaret();
  731. OnTextChanged(EventArgs.Empty);
  732. }
  733. } else {
  734. document.DeleteChar(document.CaretTag, document.CaretPosition, false);
  735. document.MoveCaret(CaretDirection.CharBack);
  736. OnTextChanged(EventArgs.Empty);
  737. }
  738. CaretMoved(this, null);
  739. return true;
  740. }
  741. case Keys.Delete: {
  742. if (read_only) {
  743. break;
  744. }
  745. // delete only deletes on the line, doesn't do the combine
  746. if (document.CaretPosition == document.CaretLine.text.Length) {
  747. if (document.CaretLine.LineNo < document.Lines) {
  748. Line line;
  749. line = document.GetLine(document.CaretLine.LineNo + 1);
  750. document.Combine(document.CaretLine, line);
  751. document.UpdateView(document.CaretLine, 2, 0);
  752. OnTextChanged(EventArgs.Empty);
  753. #if Debug
  754. Line check_first;
  755. Line check_second;
  756. check_first = document.GetLine(document.CaretLine.LineNo);
  757. check_second = document.GetLine(check_first.line_no + 1);
  758. Console.WriteLine("Post-UpdateView: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
  759. #endif
  760. // Caret doesn't move
  761. }
  762. } else {
  763. document.DeleteChar(document.CaretTag, document.CaretPosition, true);
  764. OnTextChanged(EventArgs.Empty);
  765. }
  766. document.UpdateCaret();
  767. CaretMoved(this, null);
  768. return true;
  769. }
  770. }
  771. return base.ProcessDialogKey (keyData);
  772. }
  773. protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
  774. // Make sure we don't get sized bigger than we want to be
  775. if (!richtext) {
  776. if (!multiline) {
  777. if (height > PreferredHeight) {
  778. requested_height = height;
  779. height = PreferredHeight;
  780. specified |= BoundsSpecified.Height;
  781. }
  782. }
  783. }
  784. document.ViewPortWidth = width;
  785. document.ViewPortHeight = height;
  786. CalculateDocument();
  787. base.SetBoundsCore (x, y, width, height, specified);
  788. }
  789. protected override void WndProc(ref Message m) {
  790. switch ((Msg)m.Msg) {
  791. case Msg.WM_PAINT: {
  792. PaintEventArgs paint_event;
  793. paint_event = XplatUI.PaintEventStart(Handle);
  794. PaintControl(paint_event);
  795. XplatUI.PaintEventEnd(Handle);
  796. DefWndProc(ref m);
  797. return;
  798. }
  799. case Msg.WM_SETFOCUS: {
  800. // Set caret
  801. document.CaretHasFocus();
  802. Console.WriteLine("Creating caret");
  803. base.WndProc(ref m);
  804. return;
  805. }
  806. case Msg.WM_KILLFOCUS: {
  807. // Kill caret
  808. document.CaretLostFocus();
  809. Console.WriteLine("Destroying caret");
  810. base.WndProc(ref m);
  811. return;
  812. }
  813. case Msg.WM_CHAR: {
  814. if (ProcessKeyEventArgs(ref m)) {
  815. return;
  816. }
  817. if (PreProcessMessage(ref m)) {
  818. return;
  819. }
  820. if (ProcessKeyMessage(ref m)) {
  821. return;
  822. }
  823. if (!read_only && (m.WParam.ToInt32() >= 32)) { // FIXME, tabs should probably go through
  824. if (document.selection_visible) {
  825. document.ReplaceSelection("");
  826. }
  827. switch (character_casing) {
  828. case CharacterCasing.Normal: {
  829. document.InsertCharAtCaret((char)m.WParam, true);
  830. OnTextChanged(EventArgs.Empty);
  831. CaretMoved(this, null);
  832. return;
  833. }
  834. case CharacterCasing.Lower: {
  835. document.InsertCharAtCaret(Char.ToLower((char)m.WParam), true);
  836. OnTextChanged(EventArgs.Empty);
  837. CaretMoved(this, null);
  838. return;
  839. }
  840. case CharacterCasing.Upper: {
  841. document.InsertCharAtCaret(Char.ToUpper((char)m.WParam), true);
  842. OnTextChanged(EventArgs.Empty);
  843. CaretMoved(this, null);
  844. return;
  845. }
  846. }
  847. }
  848. DefWndProc(ref m);
  849. return;
  850. }
  851. default: {
  852. base.WndProc(ref m);
  853. return;
  854. }
  855. }
  856. }
  857. #endregion // Protected Instance Methods
  858. #region Events
  859. public event EventHandler AcceptsTabChanged;
  860. public event EventHandler AutoSizeChanged;
  861. [Browsable(false)]
  862. [EditorBrowsable(EditorBrowsableState.Never)]
  863. public new event EventHandler BackgroundImageChanged {
  864. add { base.BackgroundImageChanged += value; }
  865. remove { base.BackgroundImageChanged -= value; }
  866. }
  867. public event EventHandler BorderStyleChanged;
  868. [Browsable(false)]
  869. [EditorBrowsable(EditorBrowsableState.Advanced)]
  870. public event EventHandler Click;
  871. public event EventHandler HideSelectionChanged;
  872. public event EventHandler ModifiedChanged;
  873. public event EventHandler MultilineChanged;
  874. [Browsable(false)]
  875. [EditorBrowsable(EditorBrowsableState.Never)]
  876. public event PaintEventHandler Paint;
  877. public event EventHandler ReadOnlyChanged;
  878. #endregion // Events
  879. #region Private Methods
  880. internal Document Document {
  881. get {
  882. return document;
  883. }
  884. set {
  885. document = value;
  886. }
  887. }
  888. static int current;
  889. private void PaintControl(PaintEventArgs pevent) {
  890. // Fill background
  891. pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), pevent.ClipRectangle);
  892. pevent.Graphics.TextRenderingHint=TextRenderingHint.AntiAlias;
  893. // Draw the viewable document
  894. document.Draw(pevent.Graphics, pevent.ClipRectangle);
  895. Rectangle rect = ClientRectangle;
  896. rect.Width--;
  897. rect.Height--;
  898. //pevent.Graphics.DrawRectangle(ThemeEngine.Current.ResPool.GetPen(ThemeEngine.Current.ColorButtonShadow), rect);
  899. #if Debug
  900. int start;
  901. int end;
  902. Line line;
  903. int line_no;
  904. Pen p;
  905. p = new Pen(Color.Red, 1);
  906. // First, figure out from what line to what line we need to draw
  907. start = document.GetLineByPixel(pevent.ClipRectangle.Top - document.ViewPortY, false).line_no;
  908. end = document.GetLineByPixel(pevent.ClipRectangle.Bottom - document.ViewPortY, false).line_no;
  909. //Console.WriteLine("Starting drawing on line '{0}'", document.GetLine(start));
  910. //Console.WriteLine("Ending drawing on line '{0}'", document.GetLine(end));
  911. line_no = start;
  912. while (line_no <= end) {
  913. line = document.GetLine(line_no);
  914. if (draw_lines) {
  915. for (int i = 0; i < line.text.Length; i++) {
  916. 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);
  917. }
  918. }
  919. line_no++;
  920. }
  921. #endif
  922. }
  923. private void TextBoxBase_MouseDown(object sender, MouseEventArgs e) {
  924. LineTag tag;
  925. Line line;
  926. int pos;
  927. if (e.Button == MouseButtons.Left) {
  928. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  929. document.SetSelectionToCaret(true);
  930. this.grabbed = true;
  931. this.Capture = true;
  932. return;
  933. }
  934. #if Debug
  935. if (e.Button == MouseButtons.Right) {
  936. draw_lines = !draw_lines;
  937. this.Invalidate();
  938. Console.WriteLine("SelectedText: {0}, length {1}", this.SelectedText, this.SelectionLength);
  939. Console.WriteLine("Selection start: {0}", this.SelectionStart);
  940. this.SelectionStart = 10;
  941. this.SelectionLength = 5;
  942. return;
  943. }
  944. tag = document.FindTag(e.X + document.ViewPortX, e.Y + document.ViewPortY, out pos, false);
  945. Console.WriteLine("Click found tag {0}, character {1}", tag, pos);
  946. line = tag.line;
  947. switch(current) {
  948. 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;
  949. 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;
  950. 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;
  951. 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;
  952. 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;
  953. 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;
  954. }
  955. current++;
  956. if (current==6) {
  957. current=0;
  958. }
  959. // Update/Recalculate what we see
  960. document.UpdateView(line, 0);
  961. // Make sure our caret is properly positioned and sized
  962. document.AlignCaret();
  963. #endif
  964. }
  965. private void TextBoxBase_MouseUp(object sender, MouseEventArgs e) {
  966. this.Capture = false;
  967. this.grabbed = false;
  968. if (e.Button == MouseButtons.Left) {
  969. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  970. document.SetSelectionToCaret(false);
  971. document.DisplayCaret();
  972. return;
  973. }
  974. }
  975. #endregion // Private Methods
  976. private void TextBoxBase_SizeChanged(object sender, EventArgs e) {
  977. canvas_width = this.Width;
  978. canvas_height = this.Height;
  979. // We always move them, they just might not be displayed
  980. hscroll.Bounds = new Rectangle (ClientRectangle.Left, ClientRectangle.Bottom - hscroll.Height, Width, hscroll.Height);
  981. vscroll.Bounds = new Rectangle (ClientRectangle.Right - vscroll.Width, ClientRectangle.Top, vscroll.Width, Height);
  982. }
  983. private void CalculateDocument() {
  984. document.RecalculateDocument(CreateGraphics());
  985. CalculateScrollBars();
  986. Invalidate(); // FIXME - do we need this?
  987. }
  988. protected void CalculateScrollBars() {
  989. // FIXME - need separate calculations for center and right alignment
  990. // No scrollbars for a single line
  991. if (document.Width >= this.Width) {
  992. hscroll.Enabled = true;
  993. hscroll.Minimum = 0;
  994. hscroll.Maximum = document.Width - this.Width;
  995. } else {
  996. hscroll.Maximum = document.ViewPortWidth;
  997. hscroll.Enabled = false;
  998. }
  999. if (document.Height >= this.Height) {
  1000. vscroll.Enabled = true;
  1001. vscroll.Minimum = 0;
  1002. vscroll.Maximum = document.Height - this.Height;
  1003. } else {
  1004. vscroll.Maximum = document.ViewPortHeight;
  1005. vscroll.Enabled = false;
  1006. }
  1007. if (!multiline) {
  1008. return;
  1009. }
  1010. if ((scrollbars & RichTextBoxScrollBars.Horizontal) != 0) {
  1011. if (((scrollbars & RichTextBoxScrollBars.ForcedHorizontal) != 0) || hscroll.Enabled) {
  1012. hscroll.Visible = true;
  1013. }
  1014. }
  1015. if ((scrollbars & RichTextBoxScrollBars.Vertical) != 0) {
  1016. if (((scrollbars & RichTextBoxScrollBars.ForcedVertical) != 0) || vscroll.Enabled) {
  1017. vscroll.Visible = true;
  1018. }
  1019. }
  1020. if (hscroll.Visible) {
  1021. vscroll.Maximum += hscroll.Height;
  1022. canvas_height = this.Height - hscroll.Height;
  1023. }
  1024. if (vscroll.Visible) {
  1025. hscroll.Maximum += vscroll.Width * 2;
  1026. canvas_width = this.Width - vscroll.Width * 2;
  1027. }
  1028. }
  1029. private void document_WidthChanged(object sender, EventArgs e) {
  1030. CalculateScrollBars();
  1031. }
  1032. private void hscroll_ValueChanged(object sender, EventArgs e) {
  1033. XplatUI.ScrollWindow(this.Handle, document.ViewPortX-this.hscroll.Value, 0, false);
  1034. document.ViewPortX = this.hscroll.Value;
  1035. document.UpdateCaret();
  1036. //Console.WriteLine("Dude scrolled horizontal");
  1037. }
  1038. private void vscroll_ValueChanged(object sender, EventArgs e) {
  1039. XplatUI.ScrollWindow(this.Handle, 0, document.ViewPortY-this.vscroll.Value, false);
  1040. document.ViewPortY = this.vscroll.Value;
  1041. document.UpdateCaret();
  1042. //Console.WriteLine("Dude scrolled vertical");
  1043. }
  1044. private void TextBoxBase_MouseMove(object sender, MouseEventArgs e) {
  1045. // FIXME - handle auto-scrolling if mouse is to the right/left of the window
  1046. if (grabbed) {
  1047. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  1048. document.SetSelectionToCaret(false);
  1049. document.DisplayCaret();
  1050. }
  1051. }
  1052. private void TextBoxBase_FontOrColorChanged(object sender, EventArgs e) {
  1053. if (!richtext) {
  1054. Line line;
  1055. // Font changes apply to the whole document
  1056. for (int i = 1; i <= document.Lines; i++) {
  1057. line = document.GetLine(i);
  1058. LineTag.FormatText(line, 1, line.text.Length, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
  1059. document.UpdateView(line, 0);
  1060. }
  1061. // Make sure the caret height is matching the new font height
  1062. document.AlignCaret();
  1063. }
  1064. }
  1065. /// <summary>Ensure the caret is always visible</summary>
  1066. internal void CaretMoved(object sender, EventArgs e) {
  1067. Point pos;
  1068. int height;
  1069. pos = document.Caret;
  1070. //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);
  1071. // Handle horizontal scrolling
  1072. if (document.CaretLine.alignment == HorizontalAlignment.Left) {
  1073. if (pos.X < (document.ViewPortX + track_width)) {
  1074. do {
  1075. if ((hscroll.Value - track_width) >= hscroll.Minimum) {
  1076. hscroll.Value -= track_width;
  1077. } else {
  1078. hscroll.Value = hscroll.Minimum;
  1079. }
  1080. } while (hscroll.Value > pos.X);
  1081. }
  1082. if ((pos.X > (this.canvas_width + document.ViewPortX - track_width)) && (hscroll.Value != hscroll.Maximum)) {
  1083. do {
  1084. if ((hscroll.Value + track_width) <= hscroll.Maximum) {
  1085. hscroll.Value += track_width;
  1086. } else {
  1087. hscroll.Value = hscroll.Maximum;
  1088. }
  1089. } while (pos.X > (hscroll.Value + this.canvas_width));
  1090. }
  1091. } else if (document.CaretLine.alignment == HorizontalAlignment.Right) {
  1092. if (pos.X < document.ViewPortX) {
  1093. if (pos.X > hscroll.Minimum) {
  1094. hscroll.Value = pos.X;
  1095. } else {
  1096. hscroll.Value = hscroll.Minimum;
  1097. }
  1098. }
  1099. if ((pos.X > (this.canvas_width + document.ViewPortX)) && (hscroll.Value != hscroll.Maximum)) {
  1100. hscroll.Value = hscroll.Maximum;
  1101. }
  1102. } else {
  1103. }
  1104. if (!multiline) {
  1105. return;
  1106. }
  1107. // Handle vertical scrolling
  1108. height = document.CaretLine.Height;
  1109. if (pos.Y < document.ViewPortY) {
  1110. vscroll.Value = pos.Y;
  1111. }
  1112. if ((pos.Y + height) > (document.ViewPortY + canvas_height)) {
  1113. vscroll.Value = pos.Y - canvas_height + height;
  1114. }
  1115. }
  1116. }
  1117. }