TextBoxBase.cs 47 KB

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