TextBoxBase.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  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 base.DefaultSize;
  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. document.Insert(document.CaretLine, document.CaretPosition, text);
  498. CalculateDocument();
  499. document.MoveCaret(CaretDirection.CtrlEnd);
  500. } else {
  501. document.MoveCaret(CaretDirection.CtrlEnd);
  502. document.InsertStringAtCaret(text, true);
  503. Invalidate();
  504. }
  505. OnTextChanged(EventArgs.Empty);
  506. }
  507. public void Clear() {
  508. Text = null;
  509. }
  510. public void ClearUndo() {
  511. document.undo.Clear();
  512. }
  513. public void Copy() {
  514. DataObject o;
  515. o = new DataObject(DataFormats.Text, SelectedText);
  516. if (this is RichTextBox) {
  517. o.SetData(DataFormats.Rtf, ((RichTextBox)this).SelectedRtf);
  518. }
  519. Clipboard.SetDataObject(o);
  520. }
  521. public void Cut() {
  522. DataObject o;
  523. o = new DataObject(DataFormats.Text, SelectedText);
  524. if (this is RichTextBox) {
  525. o.SetData(DataFormats.Rtf, ((RichTextBox)this).SelectedRtf);
  526. }
  527. Clipboard.SetDataObject(o);
  528. document.ReplaceSelection("");
  529. }
  530. public void Paste() {
  531. Paste(null, false);
  532. }
  533. public void ScrollToCaret() {
  534. this.CaretMoved(this, EventArgs.Empty);
  535. }
  536. public void Select(int start, int length) {
  537. SelectionStart = start;
  538. SelectionLength = length;
  539. }
  540. public void SelectAll() {
  541. Line last;
  542. last = document.GetLine(document.Lines);
  543. document.SetSelectionStart(document.GetLine(1), 0);
  544. document.SetSelectionEnd(last, last.text.Length);
  545. }
  546. public override string ToString() {
  547. return Text;
  548. }
  549. public void Undo() {
  550. document.undo.Undo();
  551. }
  552. #endregion // Public Instance Methods
  553. #region Protected Instance Methods
  554. protected override void CreateHandle() {
  555. base.CreateHandle ();
  556. }
  557. protected override bool IsInputKey(Keys keyData) {
  558. if ((keyData & Keys.Alt) != 0) {
  559. return base.IsInputKey(keyData);
  560. }
  561. switch (keyData & Keys.KeyCode) {
  562. case Keys.Enter: {
  563. if (multiline && accepts_return) {
  564. return true;
  565. }
  566. return false;
  567. }
  568. case Keys.Tab: {
  569. if (accepts_tab && multiline) {
  570. if ((keyData & Keys.Control) == 0) {
  571. return true;
  572. }
  573. }
  574. return false;
  575. }
  576. case Keys.Left:
  577. case Keys.Right:
  578. case Keys.Up:
  579. case Keys.Down:
  580. case Keys.PageUp:
  581. case Keys.PageDown:
  582. case Keys.Home:
  583. case Keys.End: {
  584. return true;
  585. }
  586. }
  587. return false;
  588. }
  589. protected virtual void OnAcceptsTabChanged(EventArgs e) {
  590. if (AcceptsTabChanged != null) {
  591. AcceptsTabChanged(this, e);
  592. }
  593. }
  594. protected virtual void OnAutoSizeChanged(EventArgs e) {
  595. if (AutoSizeChanged != null) {
  596. AutoSizeChanged(this, e);
  597. }
  598. }
  599. protected virtual void OnBorderStyleChanged(EventArgs e) {
  600. if (BorderStyleChanged != null) {
  601. BorderStyleChanged(this, e);
  602. }
  603. }
  604. protected override void OnFontChanged(EventArgs e) {
  605. base.OnFontChanged (e);
  606. if (auto_size) {
  607. if (PreferredHeight != ClientSize.Height) {
  608. Height = PreferredHeight;
  609. }
  610. }
  611. }
  612. protected override void OnHandleCreated(EventArgs e) {
  613. base.OnHandleCreated (e);
  614. }
  615. protected override void OnHandleDestroyed(EventArgs e) {
  616. base.OnHandleDestroyed (e);
  617. }
  618. protected virtual void OnHideSelectionChanged(EventArgs e) {
  619. if (HideSelectionChanged != null) {
  620. HideSelectionChanged(this, e);
  621. }
  622. }
  623. protected virtual void OnModifiedChanged(EventArgs e) {
  624. if (ModifiedChanged != null) {
  625. ModifiedChanged(this, e);
  626. }
  627. }
  628. protected virtual void OnMultilineChanged(EventArgs e) {
  629. if (MultilineChanged != null) {
  630. MultilineChanged(this, e);
  631. }
  632. }
  633. protected virtual void OnReadOnlyChanged(EventArgs e) {
  634. if (ReadOnlyChanged != null) {
  635. ReadOnlyChanged(this, e);
  636. }
  637. }
  638. protected override bool ProcessDialogKey(Keys keyData) {
  639. return base.ProcessDialogKey(keyData);
  640. }
  641. private bool ProcessKey(Keys keyData) {
  642. bool control;
  643. bool shift;
  644. control = (Control.ModifierKeys & Keys.Control) != 0;
  645. shift = (Control.ModifierKeys & Keys.Shift) != 0;
  646. switch (keyData & Keys.KeyCode) {
  647. case Keys.X: { // Cut (Ctrl-X)
  648. if (control) {
  649. Cut();
  650. return true;
  651. }
  652. return false;
  653. }
  654. case Keys.C: { // Copy (Ctrl-C)
  655. if (control) {
  656. Copy();
  657. return true;
  658. }
  659. return false;
  660. }
  661. case Keys.V: { // Paste (Ctrl-V)
  662. if (control) {
  663. return Paste(null, true);
  664. }
  665. return false;
  666. }
  667. case Keys.Z: { // Undo (Ctrl-Z)
  668. if (control) {
  669. Undo();
  670. return true;
  671. }
  672. return false;
  673. }
  674. case Keys.Left: {
  675. if (control) {
  676. document.MoveCaret(CaretDirection.WordBack);
  677. } else {
  678. if (!document.selection_visible || shift) {
  679. document.MoveCaret(CaretDirection.CharBack);
  680. } else {
  681. document.MoveCaret(CaretDirection.SelectionStart);
  682. }
  683. }
  684. if (!shift) {
  685. document.SetSelectionToCaret(true);
  686. } else {
  687. document.SetSelectionToCaret(false);
  688. }
  689. CaretMoved(this, null);
  690. return true;
  691. }
  692. case Keys.Right: {
  693. if (control) {
  694. document.MoveCaret(CaretDirection.WordForward);
  695. } else {
  696. if (!document.selection_visible || shift) {
  697. document.MoveCaret(CaretDirection.CharForward);
  698. } else {
  699. document.MoveCaret(CaretDirection.SelectionEnd);
  700. }
  701. }
  702. if (!shift) {
  703. document.SetSelectionToCaret(true);
  704. } else {
  705. document.SetSelectionToCaret(false);
  706. }
  707. CaretMoved(this, null);
  708. return true;
  709. }
  710. case Keys.Up: {
  711. if (control) {
  712. if (document.CaretPosition == 0) {
  713. document.MoveCaret(CaretDirection.LineUp);
  714. } else {
  715. document.MoveCaret(CaretDirection.Home);
  716. }
  717. } else {
  718. document.MoveCaret(CaretDirection.LineUp);
  719. }
  720. if ((Control.ModifierKeys & Keys.Shift) == 0) {
  721. document.SetSelectionToCaret(true);
  722. } else {
  723. document.SetSelectionToCaret(false);
  724. }
  725. CaretMoved(this, null);
  726. return true;
  727. }
  728. case Keys.Down: {
  729. if (control) {
  730. if (document.CaretPosition == document.CaretLine.Text.Length) {
  731. document.MoveCaret(CaretDirection.LineDown);
  732. } else {
  733. document.MoveCaret(CaretDirection.End);
  734. }
  735. } else {
  736. document.MoveCaret(CaretDirection.LineDown);
  737. }
  738. if ((Control.ModifierKeys & Keys.Shift) == 0) {
  739. document.SetSelectionToCaret(true);
  740. } else {
  741. document.SetSelectionToCaret(false);
  742. }
  743. CaretMoved(this, null);
  744. return true;
  745. }
  746. case Keys.Home: {
  747. if ((Control.ModifierKeys & Keys.Control) != 0) {
  748. document.MoveCaret(CaretDirection.CtrlHome);
  749. } else {
  750. document.MoveCaret(CaretDirection.Home);
  751. }
  752. if ((Control.ModifierKeys & Keys.Shift) == 0) {
  753. document.SetSelectionToCaret(true);
  754. } else {
  755. document.SetSelectionToCaret(false);
  756. }
  757. CaretMoved(this, null);
  758. return true;
  759. }
  760. case Keys.End: {
  761. if ((Control.ModifierKeys & Keys.Control) != 0) {
  762. document.MoveCaret(CaretDirection.CtrlEnd);
  763. } else {
  764. document.MoveCaret(CaretDirection.End);
  765. }
  766. if ((Control.ModifierKeys & Keys.Shift) == 0) {
  767. document.SetSelectionToCaret(true);
  768. } else {
  769. document.SetSelectionToCaret(false);
  770. }
  771. CaretMoved(this, null);
  772. return true;
  773. }
  774. case Keys.Enter: {
  775. // ignoring accepts_return, fixes bug #76355
  776. if (!read_only && multiline && (accepts_return || (FindForm().AcceptButton == null) || ((Control.ModifierKeys & Keys.Control) != 0))) {
  777. Line line;
  778. if (document.selection_visible) {
  779. document.ReplaceSelection("");
  780. }
  781. document.SetSelectionToCaret(true);
  782. line = document.CaretLine;
  783. document.Split(document.CaretLine, document.CaretTag, document.CaretPosition, false);
  784. OnTextChanged(EventArgs.Empty);
  785. document.UpdateView(line, 2, 0);
  786. document.MoveCaret(CaretDirection.CharForward);
  787. CaretMoved(this, null);
  788. return true;
  789. }
  790. break;
  791. }
  792. case Keys.Tab: {
  793. if (!read_only && accepts_tab && multiline) {
  794. document.InsertChar(document.CaretLine, document.CaretPosition, '\t');
  795. if (document.selection_visible) {
  796. document.ReplaceSelection("");
  797. }
  798. document.SetSelectionToCaret(true);
  799. OnTextChanged(EventArgs.Empty);
  800. CaretMoved(this, null);
  801. return true;
  802. }
  803. break;
  804. }
  805. case Keys.Back: {
  806. if (read_only) {
  807. break;
  808. }
  809. // delete only deletes on the line, doesn't do the combine
  810. if (document.selection_visible) {
  811. document.ReplaceSelection("");
  812. }
  813. document.SetSelectionToCaret(true);
  814. if (document.CaretPosition == 0) {
  815. if (document.CaretLine.LineNo > 1) {
  816. Line line;
  817. int new_caret_pos;
  818. line = document.GetLine(document.CaretLine.LineNo - 1);
  819. new_caret_pos = line.text.Length;
  820. document.Combine(line, document.CaretLine);
  821. document.UpdateView(line, 1, 0);
  822. document.PositionCaret(line, new_caret_pos);
  823. //document.MoveCaret(CaretDirection.CharForward);
  824. document.UpdateCaret();
  825. OnTextChanged(EventArgs.Empty);
  826. }
  827. } else {
  828. if (!control || document.CaretPosition == 0) {
  829. document.DeleteChar(document.CaretTag, document.CaretPosition, false);
  830. document.MoveCaret(CaretDirection.CharBack);
  831. } else {
  832. int start_pos;
  833. start_pos = document.CaretPosition - 1;
  834. while ((start_pos > 0) && !Document.IsWordSeparator(document.CaretLine.Text[start_pos - 1])) {
  835. start_pos--;
  836. }
  837. document.DeleteChars(document.CaretTag, start_pos, document.CaretPosition - start_pos);
  838. document.PositionCaret(document.CaretLine, start_pos);
  839. }
  840. document.UpdateCaret();
  841. OnTextChanged(EventArgs.Empty);
  842. }
  843. CaretMoved(this, null);
  844. return true;
  845. }
  846. case Keys.Insert: {
  847. if (shift) {
  848. Paste(null, true);
  849. return true;
  850. }
  851. if (control) {
  852. Copy();
  853. return true;
  854. }
  855. // FIXME - need overwrite/insert toggle?
  856. return false;
  857. }
  858. case Keys.Delete: {
  859. if (shift) {
  860. Cut();
  861. return true;
  862. }
  863. if (read_only) {
  864. break;
  865. }
  866. if (document.selection_visible) {
  867. document.ReplaceSelection("");
  868. } else {
  869. // DeleteChar only deletes on the line, doesn't do the combine
  870. if (document.CaretPosition == document.CaretLine.Text.Length) {
  871. if (document.CaretLine.LineNo < document.Lines) {
  872. Line line;
  873. line = document.GetLine(document.CaretLine.LineNo + 1);
  874. document.Combine(document.CaretLine, line);
  875. document.UpdateView(document.CaretLine, 2, 0);
  876. #if not_Debug
  877. Line check_first;
  878. Line check_second;
  879. check_first = document.GetLine(document.CaretLine.LineNo);
  880. check_second = document.GetLine(check_first.line_no + 1);
  881. Console.WriteLine("Post-UpdateView: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
  882. #endif
  883. // Caret doesn't move
  884. }
  885. } else {
  886. if (!control) {
  887. document.DeleteChar(document.CaretTag, document.CaretPosition, true);
  888. } else {
  889. int end_pos;
  890. end_pos = document.CaretPosition;
  891. while ((end_pos < document.CaretLine.Text.Length) && !Document.IsWordSeparator(document.CaretLine.Text[end_pos])) {
  892. end_pos++;
  893. }
  894. if (end_pos < document.CaretLine.Text.Length) {
  895. end_pos++;
  896. }
  897. document.DeleteChars(document.CaretTag, document.CaretPosition, end_pos - document.CaretPosition);
  898. }
  899. }
  900. }
  901. OnTextChanged(EventArgs.Empty);
  902. document.AlignCaret();
  903. document.UpdateCaret();
  904. CaretMoved(this, null);
  905. return true;
  906. }
  907. }
  908. return false;
  909. }
  910. protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
  911. // Make sure we don't get sized bigger than we want to be
  912. if (!richtext) {
  913. if (!multiline) {
  914. if (height > PreferredHeight) {
  915. requested_height = height;
  916. height = PreferredHeight;
  917. specified |= BoundsSpecified.Height;
  918. }
  919. }
  920. }
  921. document.ViewPortWidth = width;
  922. document.ViewPortHeight = height;
  923. CalculateDocument();
  924. base.SetBoundsCore (x, y, width, height, specified);
  925. }
  926. protected override void WndProc(ref Message m) {
  927. switch ((Msg)m.Msg) {
  928. case Msg.WM_PAINT: {
  929. PaintEventArgs paint_event;
  930. paint_event = XplatUI.PaintEventStart(Handle, true);
  931. PaintControl(paint_event);
  932. XplatUI.PaintEventEnd(Handle, true);
  933. DefWndProc(ref m);
  934. return;
  935. }
  936. case Msg.WM_SETFOCUS: {
  937. // Set caret
  938. document.CaretHasFocus();
  939. base.WndProc(ref m);
  940. return;
  941. }
  942. case Msg.WM_KILLFOCUS: {
  943. // Kill caret
  944. document.CaretLostFocus();
  945. base.WndProc(ref m);
  946. return;
  947. }
  948. case Msg.WM_KEYDOWN: {
  949. ProcessKey((Keys)m.WParam.ToInt32() | XplatUI.State.ModifierKeys);
  950. base.WndProc(ref m);
  951. return;
  952. }
  953. case Msg.WM_CHAR: {
  954. // Ctrl-Backspace generates a real char, whack it
  955. if (m.WParam.ToInt32() == 127) {
  956. base.WndProc(ref m);
  957. return;
  958. }
  959. base.WndProc(ref m);
  960. if (!read_only && (m.WParam.ToInt32() >= 32)) { // FIXME, tabs should probably go through
  961. if (document.selection_visible) {
  962. document.ReplaceSelection("");
  963. }
  964. switch (character_casing) {
  965. case CharacterCasing.Normal: {
  966. if (document.Length < max_length) {
  967. document.InsertCharAtCaret((char)m.WParam, true);
  968. OnTextChanged(EventArgs.Empty);
  969. CaretMoved(this, null);
  970. } else {
  971. XplatUI.AudibleAlert();
  972. }
  973. return;
  974. }
  975. case CharacterCasing.Lower: {
  976. if (document.Length < max_length) {
  977. document.InsertCharAtCaret(Char.ToLower((char)m.WParam), true);
  978. OnTextChanged(EventArgs.Empty);
  979. CaretMoved(this, null);
  980. } else {
  981. XplatUI.AudibleAlert();
  982. }
  983. return;
  984. }
  985. case CharacterCasing.Upper: {
  986. if (document.Length < max_length) {
  987. document.InsertCharAtCaret(Char.ToUpper((char)m.WParam), true);
  988. OnTextChanged(EventArgs.Empty);
  989. CaretMoved(this, null);
  990. } else {
  991. XplatUI.AudibleAlert();
  992. }
  993. return;
  994. }
  995. }
  996. }
  997. return;
  998. }
  999. default: {
  1000. base.WndProc(ref m);
  1001. return;
  1002. }
  1003. }
  1004. }
  1005. #endregion // Protected Instance Methods
  1006. #region Events
  1007. public event EventHandler AcceptsTabChanged;
  1008. public event EventHandler AutoSizeChanged;
  1009. [Browsable(false)]
  1010. [EditorBrowsable(EditorBrowsableState.Never)]
  1011. public new event EventHandler BackgroundImageChanged {
  1012. add { base.BackgroundImageChanged += value; }
  1013. remove { base.BackgroundImageChanged -= value; }
  1014. }
  1015. public event EventHandler BorderStyleChanged;
  1016. [Browsable(false)]
  1017. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1018. public event EventHandler Click;
  1019. public event EventHandler HideSelectionChanged;
  1020. public event EventHandler ModifiedChanged;
  1021. public event EventHandler MultilineChanged;
  1022. [Browsable(false)]
  1023. [EditorBrowsable(EditorBrowsableState.Never)]
  1024. public event PaintEventHandler Paint;
  1025. public event EventHandler ReadOnlyChanged;
  1026. internal event EventHandler HScrolled;
  1027. internal event EventHandler VScrolled;
  1028. #endregion // Events
  1029. #region Private Methods
  1030. internal Document Document {
  1031. get {
  1032. return document;
  1033. }
  1034. set {
  1035. document = value;
  1036. }
  1037. }
  1038. #if Debug
  1039. static int current;
  1040. #endif
  1041. private void PaintControl(PaintEventArgs pevent) {
  1042. // Fill background
  1043. pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), pevent.ClipRectangle);
  1044. pevent.Graphics.TextRenderingHint=TextRenderingHint.AntiAlias;
  1045. // Draw the viewable document
  1046. document.Draw(pevent.Graphics, pevent.ClipRectangle);
  1047. Rectangle rect = ClientRectangle;
  1048. rect.Width--;
  1049. rect.Height--;
  1050. //pevent.Graphics.DrawRectangle(ThemeEngine.Current.ResPool.GetPen(ThemeEngine.Current.ColorControlDark), rect);
  1051. #if Debug
  1052. int start;
  1053. int end;
  1054. Line line;
  1055. int line_no;
  1056. Pen p;
  1057. p = new Pen(Color.Red, 1);
  1058. // First, figure out from what line to what line we need to draw
  1059. start = document.GetLineByPixel(pevent.ClipRectangle.Top - document.ViewPortY, false).line_no;
  1060. end = document.GetLineByPixel(pevent.ClipRectangle.Bottom - document.ViewPortY, false).line_no;
  1061. //Console.WriteLine("Starting drawing on line '{0}'", document.GetLine(start));
  1062. //Console.WriteLine("Ending drawing on line '{0}'", document.GetLine(end));
  1063. line_no = start;
  1064. while (line_no <= end) {
  1065. line = document.GetLine(line_no);
  1066. if (draw_lines) {
  1067. for (int i = 0; i < line.text.Length; i++) {
  1068. 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);
  1069. }
  1070. }
  1071. line_no++;
  1072. }
  1073. #endif
  1074. }
  1075. private void TextBoxBase_MouseDown(object sender, MouseEventArgs e) {
  1076. if (e.Button == MouseButtons.Left) {
  1077. TimeSpan interval;
  1078. interval = DateTime.Now - click_last;
  1079. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  1080. this.Capture = true;
  1081. // Handle place caret/select word/select line behaviour
  1082. if (e.Clicks == 1) {
  1083. if (SystemInformation.DoubleClickTime < interval.TotalMilliseconds) {
  1084. #if DebugClick
  1085. 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));
  1086. #endif
  1087. document.SetSelectionToCaret(true);
  1088. click_mode = CaretSelection.Position;
  1089. } else {
  1090. #if DebugClick
  1091. Console.WriteLine("Tripple Click Selecting line");
  1092. #endif
  1093. document.ExpandSelection(CaretSelection.Line, false);
  1094. click_mode = CaretSelection.Line;
  1095. }
  1096. } else {
  1097. // We select the line if the word is already selected, and vice versa
  1098. if (click_mode != CaretSelection.Word) {
  1099. if (click_mode == CaretSelection.Line) {
  1100. document.Invalidate(document.selection_start.line, 0, document.selection_start.line, document.selection_start.line.text.Length);
  1101. }
  1102. click_mode = CaretSelection.Word;
  1103. document.ExpandSelection(CaretSelection.Word, false); // Setting initial selection
  1104. } else {
  1105. click_mode = CaretSelection.Line;
  1106. document.ExpandSelection(CaretSelection.Line, false); // Setting initial selection
  1107. }
  1108. }
  1109. // Reset
  1110. click_last = DateTime.Now;
  1111. return;
  1112. }
  1113. #if Debug
  1114. LineTag tag;
  1115. Line line;
  1116. int pos;
  1117. if (e.Button == MouseButtons.Right) {
  1118. draw_lines = !draw_lines;
  1119. this.Invalidate();
  1120. Console.WriteLine("SelectedText: {0}, length {1}", this.SelectedText, this.SelectionLength);
  1121. Console.WriteLine("Selection start: {0}", this.SelectionStart);
  1122. this.SelectionStart = 10;
  1123. this.SelectionLength = 5;
  1124. return;
  1125. }
  1126. tag = document.FindTag(e.X + document.ViewPortX, e.Y + document.ViewPortY, out pos, false);
  1127. Console.WriteLine("Click found tag {0}, character {1}", tag, pos);
  1128. line = tag.line;
  1129. switch(current) {
  1130. 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;
  1131. 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;
  1132. 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;
  1133. 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;
  1134. 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;
  1135. 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;
  1136. }
  1137. current++;
  1138. if (current==6) {
  1139. current=0;
  1140. }
  1141. // Update/Recalculate what we see
  1142. document.UpdateView(line, 0);
  1143. // Make sure our caret is properly positioned and sized
  1144. document.AlignCaret();
  1145. #endif
  1146. }
  1147. private void TextBoxBase_MouseUp(object sender, MouseEventArgs e) {
  1148. this.Capture = false;
  1149. if (e.Button == MouseButtons.Left) {
  1150. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  1151. if (click_mode == CaretSelection.Position) {
  1152. document.SetSelectionToCaret(false);
  1153. document.DisplayCaret();
  1154. } else {
  1155. document.ExpandSelection(click_mode, true);
  1156. }
  1157. return;
  1158. }
  1159. }
  1160. private void TextBoxBase_SizeChanged(object sender, EventArgs e) {
  1161. canvas_width = ClientSize.Width;
  1162. canvas_height = ClientSize.Height;
  1163. // We always move them, they just might not be displayed
  1164. hscroll.Bounds = new Rectangle (ClientRectangle.Left, ClientRectangle.Bottom - hscroll.Height, ClientSize.Width - (vscroll.Visible ? SystemInformation.VerticalScrollBarWidth : 0), hscroll.Height);
  1165. vscroll.Bounds = new Rectangle (ClientRectangle.Right - vscroll.Width, ClientRectangle.Top, vscroll.Width, ClientSize.Height - (hscroll.Visible ? SystemInformation.HorizontalScrollBarHeight : 0));
  1166. }
  1167. private void TextBoxBase_MouseWheel(object sender, MouseEventArgs e) {
  1168. Line line;
  1169. int line_no;
  1170. int target;
  1171. if (!vscroll.Enabled) {
  1172. return;
  1173. }
  1174. if (e.Delta < 0) {
  1175. line_no = document.GetLineByPixel(document.ViewPortY, false).line_no + SystemInformation.MouseWheelScrollLines;
  1176. if (line_no > document.Lines) {
  1177. line_no = document.Lines;
  1178. }
  1179. } else {
  1180. line_no = document.GetLineByPixel(document.ViewPortY, false).line_no - SystemInformation.MouseWheelScrollLines;
  1181. if (line_no < 1) {
  1182. line_no = 1;
  1183. }
  1184. }
  1185. line = document.GetLine(line_no);
  1186. if (line.Y < vscroll.Maximum) {
  1187. vscroll.Value = line.Y;
  1188. } else {
  1189. vscroll.Value = vscroll.Maximum;
  1190. }
  1191. }
  1192. internal void CalculateDocument() {
  1193. if (!IsHandleCreated) {
  1194. return;
  1195. }
  1196. document.RecalculateDocument(CreateGraphics());
  1197. CalculateScrollBars();
  1198. //blah Console.WriteLine("TextBox.cs(1175) Invalidate called in CalculateDocument");
  1199. Invalidate(); // FIXME - do we need this?
  1200. }
  1201. internal void CalculateScrollBars() {
  1202. // FIXME - need separate calculations for center and right alignment
  1203. // No scrollbars for a single line
  1204. if (document.Width >= ClientSize.Width) {
  1205. hscroll.Enabled = true;
  1206. hscroll.Minimum = 0;
  1207. hscroll.Maximum = document.Width - ClientSize.Width;
  1208. } else {
  1209. hscroll.Maximum = document.ViewPortWidth;
  1210. hscroll.Enabled = false;
  1211. }
  1212. if (document.Height >= ClientSize.Height) {
  1213. vscroll.Enabled = true;
  1214. vscroll.Minimum = 0;
  1215. vscroll.Maximum = document.Height - ClientSize.Height;
  1216. } else {
  1217. vscroll.Maximum = document.ViewPortHeight;
  1218. vscroll.Enabled = false;
  1219. }
  1220. if (!multiline) {
  1221. return;
  1222. }
  1223. if (!WordWrap) {
  1224. if ((scrollbars & RichTextBoxScrollBars.Horizontal) != 0) {
  1225. if (((scrollbars & RichTextBoxScrollBars.ForcedHorizontal) != 0) || hscroll.Enabled) {
  1226. hscroll.Visible = true;
  1227. }
  1228. }
  1229. }
  1230. if ((scrollbars & RichTextBoxScrollBars.Vertical) != 0) {
  1231. if (((scrollbars & RichTextBoxScrollBars.ForcedVertical) != 0) || vscroll.Enabled) {
  1232. vscroll.Visible = true;
  1233. }
  1234. }
  1235. if (hscroll.Visible) {
  1236. vscroll.Maximum += hscroll.Height;
  1237. canvas_height = ClientSize.Height - hscroll.Height;
  1238. }
  1239. if (vscroll.Visible) {
  1240. hscroll.Maximum += vscroll.Width * 2;
  1241. canvas_width = ClientSize.Width - vscroll.Width * 2;
  1242. }
  1243. TextBoxBase_SizeChanged(this, EventArgs.Empty);
  1244. }
  1245. private void document_WidthChanged(object sender, EventArgs e) {
  1246. CalculateScrollBars();
  1247. }
  1248. private void document_HeightChanged(object sender, EventArgs e) {
  1249. CalculateScrollBars();
  1250. }
  1251. private void hscroll_ValueChanged(object sender, EventArgs e) {
  1252. XplatUI.ScrollWindow(this.Handle, document.ViewPortX-this.hscroll.Value, 0, false);
  1253. document.ViewPortX = this.hscroll.Value;
  1254. document.UpdateCaret();
  1255. if (HScrolled != null) {
  1256. HScrolled(this, EventArgs.Empty);
  1257. }
  1258. }
  1259. private void vscroll_ValueChanged(object sender, EventArgs e) {
  1260. XplatUI.ScrollWindow(this.Handle, 0, document.ViewPortY-this.vscroll.Value, false);
  1261. document.ViewPortY = this.vscroll.Value;
  1262. document.UpdateCaret();
  1263. if (VScrolled != null) {
  1264. VScrolled(this, EventArgs.Empty);
  1265. }
  1266. }
  1267. private void TextBoxBase_MouseMove(object sender, MouseEventArgs e) {
  1268. // FIXME - handle auto-scrolling if mouse is to the right/left of the window
  1269. if (Capture) {
  1270. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  1271. if (click_mode == CaretSelection.Position) {
  1272. document.SetSelectionToCaret(false);
  1273. document.DisplayCaret();
  1274. } else {
  1275. document.ExpandSelection(click_mode, true);
  1276. }
  1277. }
  1278. }
  1279. private void TextBoxBase_FontOrColorChanged(object sender, EventArgs e) {
  1280. if (!richtext) {
  1281. Line line;
  1282. // Font changes apply to the whole document
  1283. for (int i = 1; i <= document.Lines; i++) {
  1284. line = document.GetLine(i);
  1285. LineTag.FormatText(line, 1, line.text.Length, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
  1286. document.UpdateView(line, 0);
  1287. }
  1288. // Make sure the caret height is matching the new font height
  1289. document.AlignCaret();
  1290. }
  1291. }
  1292. /// <summary>Ensure the caret is always visible</summary>
  1293. internal void CaretMoved(object sender, EventArgs e) {
  1294. Point pos;
  1295. int height;
  1296. pos = document.Caret;
  1297. //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);
  1298. // Handle horizontal scrolling
  1299. if (document.CaretLine.alignment == HorizontalAlignment.Left) {
  1300. if (pos.X < (document.ViewPortX + track_width)) {
  1301. do {
  1302. if ((hscroll.Value - track_width) >= hscroll.Minimum) {
  1303. hscroll.Value -= track_width;
  1304. } else {
  1305. hscroll.Value = hscroll.Minimum;
  1306. }
  1307. } while (hscroll.Value > pos.X);
  1308. }
  1309. if ((pos.X > (this.canvas_width + document.ViewPortX - track_width)) && (hscroll.Value != hscroll.Maximum)) {
  1310. do {
  1311. if ((hscroll.Value + track_width) <= hscroll.Maximum) {
  1312. hscroll.Value += track_width;
  1313. } else {
  1314. hscroll.Value = hscroll.Maximum;
  1315. }
  1316. } while (pos.X > (hscroll.Value + this.canvas_width));
  1317. }
  1318. } else if (document.CaretLine.alignment == HorizontalAlignment.Right) {
  1319. if (pos.X < document.ViewPortX) {
  1320. if (pos.X > hscroll.Minimum) {
  1321. hscroll.Value = pos.X;
  1322. } else {
  1323. hscroll.Value = hscroll.Minimum;
  1324. }
  1325. }
  1326. if ((pos.X > (this.canvas_width + document.ViewPortX)) && (hscroll.Value != hscroll.Maximum)) {
  1327. hscroll.Value = hscroll.Maximum;
  1328. }
  1329. } else {
  1330. }
  1331. if (!multiline) {
  1332. return;
  1333. }
  1334. // Handle vertical scrolling
  1335. height = document.CaretLine.Height;
  1336. if (pos.Y < document.ViewPortY) {
  1337. vscroll.Value = pos.Y;
  1338. }
  1339. if ((pos.Y + height) > (document.ViewPortY + canvas_height)) {
  1340. vscroll.Value = pos.Y - canvas_height + height;
  1341. }
  1342. }
  1343. internal bool Paste(DataFormats.Format format, bool obey_length) {
  1344. IDataObject clip;
  1345. string s;
  1346. clip = Clipboard.GetDataObject();
  1347. if (clip == null)
  1348. return false;
  1349. if (format == null) {
  1350. if ((this is RichTextBox) && clip.GetDataPresent(DataFormats.Rtf)) {
  1351. format = DataFormats.GetFormat(DataFormats.Rtf);
  1352. } else if (clip.GetDataPresent(DataFormats.UnicodeText)) {
  1353. format = DataFormats.GetFormat(DataFormats.UnicodeText);
  1354. } else if (clip.GetDataPresent(DataFormats.Text)) {
  1355. format = DataFormats.GetFormat(DataFormats.Text);
  1356. } else {
  1357. return false;
  1358. }
  1359. } else {
  1360. if ((format.Name == DataFormats.Rtf) && !(this is RichTextBox)) {
  1361. return false;
  1362. }
  1363. if (!clip.GetDataPresent(format.Name)) {
  1364. return false;
  1365. }
  1366. }
  1367. if (format.Name == DataFormats.Rtf) {
  1368. ((RichTextBox)this).SelectedRtf = (string)clip.GetData(DataFormats.Rtf);
  1369. return true;
  1370. } else if (format.Name == DataFormats.UnicodeText) {
  1371. s = (string)clip.GetData(DataFormats.UnicodeText);
  1372. } else if (format.Name == DataFormats.Text) {
  1373. s = (string)clip.GetData(DataFormats.Text);
  1374. } else {
  1375. return false;
  1376. }
  1377. if (!obey_length) {
  1378. this.SelectedText = s;
  1379. } else {
  1380. if ((s.Length + document.Length) < max_length) {
  1381. this.SelectedText = s;
  1382. } else if (document.Length < max_length) {
  1383. this.SelectedText = s.Substring(0, max_length - document.Length);
  1384. }
  1385. }
  1386. return true;
  1387. }
  1388. #endregion // Private Methods
  1389. }
  1390. }