TextBoxBase.cs 52 KB

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