TextBoxBase.cs 53 KB

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