TextBoxBase.cs 58 KB

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