TextBoxBase.cs 56 KB

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