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