TextBoxBase.cs 58 KB

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