TextBoxBase.cs 51 KB

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