TextBoxBase.cs 58 KB

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