TextBoxBase.cs 43 KB

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