TextBoxBase.cs 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:c
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004-2006 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. //
  26. // NOT COMPLETE
  27. #undef Debug
  28. #undef DebugClick
  29. using System.ComponentModel;
  30. using System.ComponentModel.Design;
  31. using System.Drawing;
  32. using System.Drawing.Text;
  33. using System.Text;
  34. using System.Runtime.InteropServices;
  35. using System.Collections;
  36. namespace System.Windows.Forms {
  37. [DefaultEvent("TextChanged")]
  38. [Designer("System.Windows.Forms.Design.TextBoxBaseDesigner, " + Consts.AssemblySystem_Design)]
  39. public abstract class TextBoxBase : Control {
  40. #region Local Variables
  41. internal HorizontalAlignment alignment;
  42. internal bool accepts_tab;
  43. internal bool accepts_return;
  44. internal bool auto_size;
  45. internal bool backcolor_set;
  46. internal CharacterCasing character_casing;
  47. internal bool hide_selection;
  48. internal int max_length;
  49. internal bool modified;
  50. internal char password_char;
  51. internal bool read_only;
  52. internal bool word_wrap;
  53. internal Document document;
  54. internal LineTag caret_tag; // tag our cursor is in
  55. internal int caret_pos; // position on the line our cursor is in (can be 0 = beginning of line)
  56. internal ImplicitHScrollBar hscroll;
  57. internal ImplicitVScrollBar vscroll;
  58. internal RichTextBoxScrollBars scrollbars;
  59. internal Timer scroll_timer;
  60. internal bool richtext;
  61. internal bool show_selection; // set to true to always show selection, even if no focus is set
  62. internal bool has_been_focused;
  63. internal int selection_length = -1; // set to the user-specified selection length, or -1 if none
  64. internal bool show_caret_w_selection; // TextBox shows the caret when the selection is visible
  65. internal int requested_height;
  66. internal int canvas_width;
  67. internal int canvas_height;
  68. static internal int track_width = 2; //
  69. static internal int track_border = 5; //
  70. internal DateTime click_last;
  71. internal int click_point_x;
  72. internal int click_point_y;
  73. internal CaretSelection click_mode;
  74. internal Bitmap bmp;
  75. internal BorderStyle actual_border_style;
  76. internal bool shortcuts_enabled = true;
  77. #if Debug
  78. internal static bool draw_lines = false;
  79. #endif
  80. #endregion // Local Variables
  81. #region Internal Constructor
  82. // Constructor will go when complete, only for testing - pdb
  83. internal TextBoxBase() {
  84. alignment = HorizontalAlignment.Left;
  85. accepts_return = false;
  86. accepts_tab = false;
  87. auto_size = true;
  88. InternalBorderStyle = BorderStyle.Fixed3D;
  89. actual_border_style = BorderStyle.Fixed3D;
  90. character_casing = CharacterCasing.Normal;
  91. hide_selection = true;
  92. max_length = 32767;
  93. modified = true;
  94. password_char = '\0';
  95. read_only = false;
  96. word_wrap = true;
  97. richtext = false;
  98. show_selection = false;
  99. show_caret_w_selection = (this is TextBox);
  100. document = new Document(this);
  101. document.WidthChanged += new EventHandler(document_WidthChanged);
  102. document.HeightChanged += new EventHandler(document_HeightChanged);
  103. //document.CaretMoved += new EventHandler(CaretMoved);
  104. document.Wrap = false;
  105. requested_height = -1;
  106. click_last = DateTime.Now;
  107. click_mode = CaretSelection.Position;
  108. bmp = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  109. MouseDown += new MouseEventHandler(TextBoxBase_MouseDown);
  110. MouseUp += new MouseEventHandler(TextBoxBase_MouseUp);
  111. MouseMove += new MouseEventHandler(TextBoxBase_MouseMove);
  112. SizeChanged += new EventHandler(TextBoxBase_SizeChanged);
  113. FontChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
  114. ForeColorChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
  115. MouseWheel += new MouseEventHandler(TextBoxBase_MouseWheel);
  116. scrollbars = RichTextBoxScrollBars.None;
  117. hscroll = new ImplicitHScrollBar();
  118. hscroll.ValueChanged += new EventHandler(hscroll_ValueChanged);
  119. hscroll.SetStyle (ControlStyles.Selectable, false);
  120. hscroll.Enabled = false;
  121. hscroll.Visible = false;
  122. hscroll.Maximum = Int32.MaxValue;
  123. vscroll = new ImplicitVScrollBar();
  124. vscroll.ValueChanged += new EventHandler(vscroll_ValueChanged);
  125. vscroll.SetStyle (ControlStyles.Selectable, false);
  126. vscroll.Enabled = false;
  127. vscroll.Visible = false;
  128. vscroll.Maximum = Int32.MaxValue;
  129. SuspendLayout ();
  130. this.Controls.AddImplicit (hscroll);
  131. this.Controls.AddImplicit (vscroll);
  132. ResumeLayout ();
  133. SetStyle(ControlStyles.UserPaint | ControlStyles.StandardClick, false);
  134. #if NET_2_0
  135. SetStyle(ControlStyles.UseTextForAccessibility, false);
  136. #endif
  137. canvas_width = ClientSize.Width;
  138. canvas_height = ClientSize.Height;
  139. document.ViewPortWidth = canvas_width;
  140. document.ViewPortHeight = canvas_height;
  141. Cursor = Cursors.IBeam;
  142. 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. FixupHeight ();
  712. }
  713. protected override void OnHandleDestroyed(EventArgs e) {
  714. base.OnHandleDestroyed (e);
  715. }
  716. protected virtual void OnHideSelectionChanged(EventArgs e) {
  717. EventHandler eh = (EventHandler)(Events [HideSelectionChangedEvent]);
  718. if (eh != null)
  719. eh (this, e);
  720. }
  721. protected virtual void OnModifiedChanged(EventArgs e) {
  722. EventHandler eh = (EventHandler)(Events [ModifiedChangedEvent]);
  723. if (eh != null)
  724. eh (this, e);
  725. }
  726. protected virtual void OnMultilineChanged(EventArgs e) {
  727. EventHandler eh = (EventHandler)(Events [MultilineChangedEvent]);
  728. if (eh != null)
  729. eh (this, e);
  730. }
  731. protected virtual void OnReadOnlyChanged(EventArgs e) {
  732. EventHandler eh = (EventHandler)(Events [ReadOnlyChangedEvent]);
  733. if (eh != null)
  734. eh (this, e);
  735. }
  736. protected override bool ProcessDialogKey(Keys keyData) {
  737. return base.ProcessDialogKey(keyData);
  738. }
  739. private bool ProcessKey(Keys keyData) {
  740. bool control;
  741. bool shift;
  742. control = (Control.ModifierKeys & Keys.Control) != 0;
  743. shift = (Control.ModifierKeys & Keys.Shift) != 0;
  744. if (shortcuts_enabled) {
  745. switch (keyData & Keys.KeyCode) {
  746. case Keys.X:
  747. if (control) {
  748. Cut();
  749. return true;
  750. }
  751. return false;
  752. case Keys.C:
  753. if (control) {
  754. Copy();
  755. return true;
  756. }
  757. return false;
  758. case Keys.V:
  759. if (control) {
  760. return Paste(Clipboard.GetDataObject(), null, true);
  761. }
  762. return false;
  763. case Keys.Z:
  764. if (control) {
  765. Undo();
  766. return true;
  767. }
  768. return false;
  769. case Keys.A:
  770. if (control) {
  771. SelectAll();
  772. return true;
  773. }
  774. return false;
  775. case Keys.Insert:
  776. if (shift) {
  777. Paste(Clipboard.GetDataObject(), null, true);
  778. return true;
  779. }
  780. if (control) {
  781. Copy();
  782. return true;
  783. }
  784. return false;
  785. case Keys.Delete:
  786. if (shift) {
  787. Cut();
  788. return true;
  789. }
  790. if (read_only)
  791. break;
  792. if (document.selection_visible) {
  793. document.ReplaceSelection("", false);
  794. } else {
  795. // DeleteChar only deletes on the line, doesn't do the combine
  796. if (document.CaretPosition >= document.CaretLine.TextLengthWithoutEnding ()) {
  797. if (document.CaretLine.LineNo < document.Lines) {
  798. Line line;
  799. line = document.GetLine(document.CaretLine.LineNo + 1);
  800. // this line needs to be invalidated before it is combined
  801. // because once it is combined, all it's coordinates will
  802. // have changed
  803. document.Invalidate (line, 0, line, line.text.Length);
  804. document.Combine(document.CaretLine, line);
  805. document.UpdateView(document.CaretLine,
  806. document.Lines, 0);
  807. }
  808. } else {
  809. if (!control) {
  810. document.DeleteChar(document.CaretTag, document.CaretPosition, true);
  811. } else {
  812. int end_pos;
  813. end_pos = document.CaretPosition;
  814. while ((end_pos < document.CaretLine.Text.Length) && !Document.IsWordSeparator(document.CaretLine.Text[end_pos])) {
  815. end_pos++;
  816. }
  817. if (end_pos < document.CaretLine.Text.Length) {
  818. end_pos++;
  819. }
  820. document.DeleteChars(document.CaretTag, document.CaretPosition, end_pos - document.CaretPosition);
  821. }
  822. }
  823. }
  824. OnTextChanged(EventArgs.Empty);
  825. document.AlignCaret();
  826. document.UpdateCaret();
  827. CaretMoved(this, null);
  828. return true;
  829. }
  830. }
  831. switch (keyData & Keys.KeyCode) {
  832. case Keys.Left: {
  833. if (control) {
  834. document.MoveCaret(CaretDirection.WordBack);
  835. } else {
  836. if (!document.selection_visible || shift) {
  837. document.MoveCaret(CaretDirection.CharBack);
  838. } else {
  839. document.MoveCaret(CaretDirection.SelectionStart);
  840. }
  841. }
  842. if (!shift) {
  843. document.SetSelectionToCaret(true);
  844. } else {
  845. document.SetSelectionToCaret(false);
  846. }
  847. CaretMoved(this, null);
  848. return true;
  849. }
  850. case Keys.Right: {
  851. if (control) {
  852. document.MoveCaret(CaretDirection.WordForward);
  853. } else {
  854. if (!document.selection_visible || shift) {
  855. document.MoveCaret(CaretDirection.CharForward);
  856. } else {
  857. document.MoveCaret(CaretDirection.SelectionEnd);
  858. }
  859. }
  860. if (!shift) {
  861. document.SetSelectionToCaret(true);
  862. } else {
  863. document.SetSelectionToCaret(false);
  864. }
  865. CaretMoved(this, null);
  866. return true;
  867. }
  868. case Keys.Up: {
  869. if (control) {
  870. if (document.CaretPosition == 0) {
  871. document.MoveCaret(CaretDirection.LineUp);
  872. } else {
  873. document.MoveCaret(CaretDirection.Home);
  874. }
  875. } else {
  876. document.MoveCaret(CaretDirection.LineUp);
  877. }
  878. if ((Control.ModifierKeys & Keys.Shift) == 0) {
  879. document.SetSelectionToCaret(true);
  880. } else {
  881. document.SetSelectionToCaret(false);
  882. }
  883. CaretMoved(this, null);
  884. return true;
  885. }
  886. case Keys.Down: {
  887. if (control) {
  888. if (document.CaretPosition == document.CaretLine.Text.Length) {
  889. document.MoveCaret(CaretDirection.LineDown);
  890. } else {
  891. document.MoveCaret(CaretDirection.End);
  892. }
  893. } else {
  894. document.MoveCaret(CaretDirection.LineDown);
  895. }
  896. if ((Control.ModifierKeys & Keys.Shift) == 0) {
  897. document.SetSelectionToCaret(true);
  898. } else {
  899. document.SetSelectionToCaret(false);
  900. }
  901. CaretMoved(this, null);
  902. return true;
  903. }
  904. case Keys.Home: {
  905. if ((Control.ModifierKeys & Keys.Control) != 0) {
  906. document.MoveCaret(CaretDirection.CtrlHome);
  907. } else {
  908. document.MoveCaret(CaretDirection.Home);
  909. }
  910. if ((Control.ModifierKeys & Keys.Shift) == 0) {
  911. document.SetSelectionToCaret(true);
  912. } else {
  913. document.SetSelectionToCaret(false);
  914. }
  915. CaretMoved(this, null);
  916. return true;
  917. }
  918. case Keys.End: {
  919. if ((Control.ModifierKeys & Keys.Control) != 0) {
  920. document.MoveCaret(CaretDirection.CtrlEnd);
  921. } else {
  922. document.MoveCaret(CaretDirection.End);
  923. }
  924. if ((Control.ModifierKeys & Keys.Shift) == 0) {
  925. document.SetSelectionToCaret(true);
  926. } else {
  927. document.SetSelectionToCaret(false);
  928. }
  929. CaretMoved(this, null);
  930. return true;
  931. }
  932. case Keys.Enter: {
  933. // ignoring accepts_return, fixes bug #76355
  934. if (!read_only && document.multiline && (accepts_return || (FindForm() != null && FindForm().AcceptButton == null) || ((Control.ModifierKeys & Keys.Control) != 0))) {
  935. Line line;
  936. if (document.selection_visible) {
  937. document.ReplaceSelection("\n", false);
  938. }
  939. line = document.CaretLine;
  940. document.Split (document.CaretLine, document.CaretTag, document.CaretPosition);
  941. line.ending = LineEnding.Rich;
  942. document.InsertString (line, line.text.Length,
  943. document.LineEndingToString (line.ending));
  944. OnTextChanged(EventArgs.Empty);
  945. document.UpdateView (line, document.Lines - line.line_no, 0);
  946. CaretMoved(this, null);
  947. return true;
  948. }
  949. break;
  950. }
  951. case Keys.Tab: {
  952. if (!read_only && accepts_tab && document.multiline) {
  953. document.InsertChar(document.CaretLine, document.CaretPosition, '\t');
  954. if (document.selection_visible) {
  955. document.ReplaceSelection("", false);
  956. }
  957. document.SetSelectionToCaret(true);
  958. OnTextChanged(EventArgs.Empty);
  959. CaretMoved(this, null);
  960. return true;
  961. }
  962. break;
  963. }
  964. case Keys.PageUp: {
  965. if ((Control.ModifierKeys & Keys.Control) != 0) {
  966. document.MoveCaret(CaretDirection.CtrlPgUp);
  967. } else {
  968. document.MoveCaret(CaretDirection.PgUp);
  969. }
  970. document.DisplayCaret ();
  971. return true;
  972. }
  973. case Keys.PageDown: {
  974. if ((Control.ModifierKeys & Keys.Control) != 0) {
  975. document.MoveCaret(CaretDirection.CtrlPgDn);
  976. } else {
  977. document.MoveCaret(CaretDirection.PgDn);
  978. }
  979. document.DisplayCaret ();
  980. return true;
  981. }
  982. }
  983. return false;
  984. }
  985. private void HandleBackspace(bool control) {
  986. bool fire_changed;
  987. fire_changed = false;
  988. // delete only deletes on the line, doesn't do the combine
  989. if (document.selection_visible) {
  990. document.undo.BeginUserAction (Locale.GetText ("Delete"));
  991. document.ReplaceSelection("", false);
  992. document.undo.EndUserAction ();
  993. fire_changed = true;
  994. }
  995. document.SetSelectionToCaret(true);
  996. if (document.CaretPosition == 0) {
  997. if (document.CaretLine.LineNo > 1) {
  998. Line line;
  999. int new_caret_pos;
  1000. line = document.GetLine(document.CaretLine.LineNo - 1);
  1001. new_caret_pos = line.TextLengthWithoutEnding ();
  1002. // Invalidate the old line position before we do the combine
  1003. document.Invalidate (line, 0, line, line.text.Length);
  1004. document.Combine(line, document.CaretLine);
  1005. document.UpdateView(line, document.Lines - line.LineNo, 0);
  1006. document.PositionCaret(line, new_caret_pos);
  1007. document.SetSelectionToCaret (true);
  1008. document.UpdateCaret();
  1009. fire_changed = true;
  1010. }
  1011. } else {
  1012. if (!control || document.CaretPosition == 0) {
  1013. // Move before we delete because the delete will change positions around
  1014. // if we cross a wrap border
  1015. LineTag tag = document.CaretTag;
  1016. int pos = document.CaretPosition;
  1017. document.MoveCaret (CaretDirection.CharBack);
  1018. document.DeleteChar (tag, pos, false);
  1019. document.SetSelectionToCaret (true);
  1020. } else {
  1021. int start_pos;
  1022. start_pos = document.CaretPosition - 1;
  1023. while ((start_pos > 0) && !Document.IsWordSeparator(document.CaretLine.Text[start_pos - 1])) {
  1024. start_pos--;
  1025. }
  1026. document.undo.BeginUserAction (Locale.GetText ("Delete"));
  1027. document.DeleteChars(document.CaretTag, start_pos, document.CaretPosition - start_pos);
  1028. document.undo.EndUserAction ();
  1029. document.PositionCaret(document.CaretLine, start_pos);
  1030. document.SetSelectionToCaret (true);
  1031. }
  1032. document.UpdateCaret();
  1033. fire_changed = true;
  1034. }
  1035. if (fire_changed) {
  1036. OnTextChanged(EventArgs.Empty);
  1037. }
  1038. CaretMoved(this, null);
  1039. }
  1040. protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
  1041. // Make sure we don't get sized bigger than we want to be
  1042. if (!richtext) {
  1043. if (!document.multiline) {
  1044. if (height != PreferredHeight) {
  1045. requested_height = height;
  1046. height = PreferredHeight;
  1047. specified |= BoundsSpecified.Height;
  1048. }
  1049. }
  1050. }
  1051. base.SetBoundsCore (x, y, width, height, specified);
  1052. }
  1053. protected override void WndProc(ref Message m) {
  1054. switch ((Msg)m.Msg) {
  1055. case Msg.WM_KEYDOWN: {
  1056. if (ProcessKeyMessage(ref m) || ProcessKey((Keys)m.WParam.ToInt32() | XplatUI.State.ModifierKeys)) {
  1057. m.Result = IntPtr.Zero;
  1058. return;
  1059. }
  1060. DefWndProc (ref m);
  1061. return;
  1062. }
  1063. case Msg.WM_CHAR: {
  1064. int ch;
  1065. if (ProcessKeyMessage(ref m)) {
  1066. m.Result = IntPtr.Zero;
  1067. return;
  1068. }
  1069. if (read_only) {
  1070. return;
  1071. }
  1072. m.Result = IntPtr.Zero;
  1073. ch = m.WParam.ToInt32();
  1074. if (ch == 127) {
  1075. HandleBackspace(true);
  1076. } else if (ch >= 32) {
  1077. if (document.selection_visible) {
  1078. document.ReplaceSelection("", false);
  1079. }
  1080. char c = (char)m.WParam;
  1081. switch (character_casing) {
  1082. case CharacterCasing.Upper:
  1083. c = Char.ToUpper((char) m.WParam);
  1084. break;
  1085. case CharacterCasing.Lower:
  1086. c = Char.ToLower((char) m.WParam);
  1087. break;
  1088. }
  1089. if (document.Length < max_length) {
  1090. document.InsertCharAtCaret(c, true);
  1091. #if NET_2_0
  1092. OnTextUpdate ();
  1093. #endif
  1094. OnTextChanged(EventArgs.Empty);
  1095. CaretMoved(this, null);
  1096. } else {
  1097. XplatUI.AudibleAlert();
  1098. }
  1099. return;
  1100. } else if (ch == 8) {
  1101. HandleBackspace(false);
  1102. }
  1103. return;
  1104. }
  1105. case Msg.WM_SETFOCUS:
  1106. base.WndProc(ref m);
  1107. document.CaretHasFocus ();
  1108. break;
  1109. case Msg.WM_KILLFOCUS:
  1110. base.WndProc(ref m);
  1111. document.CaretLostFocus ();
  1112. break;
  1113. default:
  1114. base.WndProc(ref m);
  1115. return;
  1116. }
  1117. }
  1118. #endregion // Protected Instance Methods
  1119. #region Events
  1120. static object AcceptsTabChangedEvent = new object ();
  1121. static object AutoSizeChangedEvent = new object ();
  1122. static object BorderStyleChangedEvent = new object ();
  1123. static object HideSelectionChangedEvent = new object ();
  1124. static object ModifiedChangedEvent = new object ();
  1125. static object MultilineChangedEvent = new object ();
  1126. static object ReadOnlyChangedEvent = new object ();
  1127. static object HScrolledEvent = new object ();
  1128. static object VScrolledEvent = new object ();
  1129. public event EventHandler AcceptsTabChanged {
  1130. add { Events.AddHandler (AcceptsTabChangedEvent, value); }
  1131. remove { Events.RemoveHandler (AcceptsTabChangedEvent, value); }
  1132. }
  1133. public new event EventHandler AutoSizeChanged {
  1134. add { Events.AddHandler (AutoSizeChangedEvent, value); }
  1135. remove { Events.RemoveHandler (AutoSizeChangedEvent, value); }
  1136. }
  1137. public event EventHandler BorderStyleChanged {
  1138. add { Events.AddHandler (BorderStyleChangedEvent, value); }
  1139. remove { Events.RemoveHandler (BorderStyleChangedEvent, value); }
  1140. }
  1141. public event EventHandler HideSelectionChanged {
  1142. add { Events.AddHandler (HideSelectionChangedEvent, value); }
  1143. remove { Events.RemoveHandler (HideSelectionChangedEvent, value); }
  1144. }
  1145. public event EventHandler ModifiedChanged {
  1146. add { Events.AddHandler (ModifiedChangedEvent, value); }
  1147. remove { Events.RemoveHandler (ModifiedChangedEvent, value); }
  1148. }
  1149. public event EventHandler MultilineChanged {
  1150. add { Events.AddHandler (MultilineChangedEvent, value); }
  1151. remove { Events.RemoveHandler (MultilineChangedEvent, value); }
  1152. }
  1153. public event EventHandler ReadOnlyChanged {
  1154. add { Events.AddHandler (ReadOnlyChangedEvent, value); }
  1155. remove { Events.RemoveHandler (ReadOnlyChangedEvent, value); }
  1156. }
  1157. internal event EventHandler HScrolled {
  1158. add { Events.AddHandler (HScrolledEvent, value); }
  1159. remove { Events.RemoveHandler (HScrolledEvent, value); }
  1160. }
  1161. internal event EventHandler VScrolled {
  1162. add { Events.AddHandler (VScrolledEvent, value); }
  1163. remove { Events.RemoveHandler (VScrolledEvent, value); }
  1164. }
  1165. [Browsable(false)]
  1166. [EditorBrowsable(EditorBrowsableState.Never)]
  1167. public new event EventHandler BackgroundImageChanged {
  1168. add { base.BackgroundImageChanged += value; }
  1169. remove { base.BackgroundImageChanged -= value; }
  1170. }
  1171. [Browsable(false)]
  1172. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1173. public new event EventHandler Click {
  1174. add { base.Click += value; }
  1175. remove { base.Click -= value; }
  1176. }
  1177. // XXX should this not manipulate base.Paint?
  1178. [Browsable(false)]
  1179. [EditorBrowsable(EditorBrowsableState.Never)]
  1180. public new event PaintEventHandler Paint;
  1181. #endregion // Events
  1182. #region Private Methods
  1183. internal Document Document {
  1184. get {
  1185. return document;
  1186. }
  1187. set {
  1188. document = value;
  1189. }
  1190. }
  1191. internal bool ShowSelection {
  1192. get {
  1193. if (show_selection || !hide_selection) {
  1194. return true;
  1195. }
  1196. return has_focus;
  1197. }
  1198. set {
  1199. if (show_selection == value)
  1200. return;
  1201. show_selection = value;
  1202. // Currently InvalidateSelectionArea is commented out so do a full invalidate
  1203. document.InvalidateSelectionArea();
  1204. }
  1205. }
  1206. internal Graphics CreateGraphicsInternal() {
  1207. if (IsHandleCreated) {
  1208. return base.CreateGraphics();
  1209. }
  1210. return Graphics.FromImage(bmp);
  1211. }
  1212. internal override void OnPaintInternal (PaintEventArgs pevent) {
  1213. // Fill background
  1214. if (backcolor_set || (Enabled && !read_only)) {
  1215. pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), pevent.ClipRectangle);
  1216. } else {
  1217. pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorControl), pevent.ClipRectangle);
  1218. }
  1219. // Draw the viewable document
  1220. document.Draw(pevent.Graphics, pevent.ClipRectangle);
  1221. //
  1222. // OnPaint does not get raised on MS (see bug #80639)
  1223. //
  1224. pevent.Handled = true;
  1225. }
  1226. private void FixupHeight ()
  1227. {
  1228. if (!richtext) {
  1229. if (!document.multiline) {
  1230. if (actual_border_style == BorderStyle.None && PreferredHeight != ClientSize.Height) {
  1231. ClientSize = new Size (ClientSize.Width, PreferredHeight);
  1232. }
  1233. }
  1234. }
  1235. }
  1236. private bool IsDoubleClick (MouseEventArgs e)
  1237. {
  1238. TimeSpan interval = DateTime.Now - click_last;
  1239. if (interval.TotalMilliseconds > SystemInformation.DoubleClickTime)
  1240. return false;
  1241. Size dcs = SystemInformation.DoubleClickSize;
  1242. if (e.X < click_point_x - dcs.Width / 2 || e.X > click_point_x + dcs.Width / 2)
  1243. return false;
  1244. if (e.Y < click_point_y - dcs.Height / 2 || e.Y > click_point_y + dcs.Height / 2)
  1245. return false;
  1246. return true;
  1247. }
  1248. private void TextBoxBase_MouseDown (object sender, MouseEventArgs e)
  1249. {
  1250. if (e.Button == MouseButtons.Left) {
  1251. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  1252. if (IsDoubleClick (e)) {
  1253. switch (click_mode) {
  1254. case CaretSelection.Position:
  1255. SelectWord ();
  1256. click_mode = CaretSelection.Word;
  1257. break;
  1258. case CaretSelection.Word:
  1259. if (this is TextBox) {
  1260. document.SetSelectionToCaret (true);
  1261. click_mode = CaretSelection.Position;
  1262. break;
  1263. }
  1264. document.ExpandSelection (CaretSelection.Line, false);
  1265. click_mode = CaretSelection.Line;
  1266. break;
  1267. case CaretSelection.Line:
  1268. // Gotta do this first because Exanding to a word
  1269. // from a line doesn't really work
  1270. document.SetSelectionToCaret (true);
  1271. SelectWord ();
  1272. click_mode = CaretSelection.Word;
  1273. break;
  1274. }
  1275. } else {
  1276. document.SetSelectionToCaret (true);
  1277. click_mode = CaretSelection.Position;
  1278. }
  1279. click_point_x = e.X;
  1280. click_point_y = e.Y;
  1281. click_last = DateTime.Now;
  1282. }
  1283. if ((e.Button == MouseButtons.Middle) && (((int)Environment.OSVersion.Platform == 4) || ((int)Environment.OSVersion.Platform == 128))) {
  1284. Document.Marker marker;
  1285. marker.tag = document.FindCursor(e.X + document.ViewPortX, e.Y + document.ViewPortY, out marker.pos);
  1286. marker.line = marker.tag.line;
  1287. marker.height = marker.tag.height;
  1288. document.SetSelection(marker.line, marker.pos, marker.line, marker.pos);
  1289. Paste (Clipboard.GetDataObject (true), null, true);
  1290. }
  1291. }
  1292. private void TextBoxBase_MouseUp(object sender, MouseEventArgs e) {
  1293. if (e.Button == MouseButtons.Left) {
  1294. if (click_mode == CaretSelection.Position) {
  1295. document.SetSelectionToCaret(false);
  1296. document.DisplayCaret();
  1297. }
  1298. if (scroll_timer != null) {
  1299. scroll_timer.Enabled = false;
  1300. }
  1301. return;
  1302. }
  1303. }
  1304. private void PositionControls ()
  1305. {
  1306. if (hscroll.Visible) {
  1307. //vscroll.Maximum += hscroll.Height;
  1308. canvas_height = ClientSize.Height - hscroll.Height;
  1309. } else {
  1310. canvas_height = ClientSize.Height;
  1311. }
  1312. if (vscroll.Visible) {
  1313. //hscroll.Maximum += vscroll.Width;
  1314. canvas_width = ClientSize.Width - vscroll.Width;
  1315. } else {
  1316. canvas_width = ClientSize.Width;
  1317. }
  1318. document.ViewPortWidth = canvas_width;
  1319. document.ViewPortHeight = canvas_height;
  1320. if (canvas_height < 1 || canvas_width < 1)
  1321. return;
  1322. // We always move them, they just might not be displayed
  1323. hscroll.Bounds = new Rectangle (ClientRectangle.Left,
  1324. Math.Max (0, ClientRectangle.Height - hscroll.Height),
  1325. Math.Max (0, ClientSize.Width - (vscroll.Visible ? vscroll.Width : 0)),
  1326. hscroll.Height);
  1327. vscroll.Bounds = new Rectangle (Math.Max (0, ClientRectangle.Right - vscroll.Width),
  1328. ClientRectangle.Top, vscroll.Width,
  1329. Math.Max (0, ClientSize.Height - (hscroll.Visible ? hscroll.Height : 0)));
  1330. }
  1331. private void TextBoxBase_SizeChanged(object sender, EventArgs e) {
  1332. if (IsHandleCreated)
  1333. CalculateDocument ();
  1334. }
  1335. private void TextBoxBase_MouseWheel(object sender, MouseEventArgs e) {
  1336. if (!vscroll.Enabled) {
  1337. return;
  1338. }
  1339. if (e.Delta < 0)
  1340. vscroll.Value = Math.Min (vscroll.Value + SystemInformation.MouseWheelScrollLines * 5,
  1341. Math.Max (0, vscroll.Maximum - document.ViewPortHeight + 1));
  1342. else
  1343. vscroll.Value = Math.Max (0, vscroll.Value - SystemInformation.MouseWheelScrollLines * 5);
  1344. }
  1345. internal virtual void SelectWord ()
  1346. {
  1347. StringBuilder s = document.caret.line.text;
  1348. int start = document.caret.pos;
  1349. int end = document.caret.pos;
  1350. if (s.Length < 1) {
  1351. if (document.caret.line.line_no >= document.Lines)
  1352. return;
  1353. Line line = document.GetLine (document.caret.line.line_no + 1);
  1354. document.PositionCaret (line, 0);
  1355. return;
  1356. }
  1357. if (start > 0) {
  1358. start--;
  1359. end--;
  1360. }
  1361. // skip whitespace until we hit a word
  1362. while (start > 0 && s [start] == ' ')
  1363. start--;
  1364. if (start > 0) {
  1365. while (start > 0 && (s [start] != ' '))
  1366. start--;
  1367. if (s [start] == ' ')
  1368. start++;
  1369. }
  1370. if (s [end] == ' ') {
  1371. while (end < s.Length && s [end] == ' ')
  1372. end++;
  1373. } else {
  1374. while (end < s.Length && s [end] != ' ')
  1375. end++;
  1376. while (end < s.Length && s [end] == ' ')
  1377. end++;
  1378. }
  1379. document.SetSelection (document.caret.line, start, document.caret.line, end);
  1380. document.PositionCaret (document.selection_end.line, document.selection_end.pos);
  1381. document.DisplayCaret();
  1382. }
  1383. internal void CalculateDocument()
  1384. {
  1385. CalculateScrollBars ();
  1386. document.RecalculateDocument (CreateGraphicsInternal ());
  1387. if (document.caret.line != null && document.caret.line.Y < document.ViewPortHeight) {
  1388. // The window has probably been resized, making the entire thing visible, so
  1389. // we need to set the scroll position back to zero.
  1390. vscroll.Value = 0;
  1391. }
  1392. Invalidate();
  1393. }
  1394. internal void CalculateScrollBars () {
  1395. // FIXME - need separate calculations for center and right alignment
  1396. //
  1397. if (document.Width >= document.ViewPortWidth) {
  1398. hscroll.SetValues (0, Math.Max (1, document.Width), -1,
  1399. document.ViewPortWidth < 0 ? 0 : document.ViewPortWidth);
  1400. if (document.multiline)
  1401. hscroll.Enabled = true;
  1402. } else {
  1403. hscroll.Enabled = false;
  1404. hscroll.Maximum = document.ViewPortWidth;
  1405. }
  1406. if (document.Height >= document.ViewPortHeight) {
  1407. vscroll.SetValues (0, Math.Max (1, document.Height), -1,
  1408. document.ViewPortHeight < 0 ? 0 : document.ViewPortHeight);
  1409. if (document.multiline)
  1410. vscroll.Enabled = true;
  1411. } else {
  1412. vscroll.Enabled = false;
  1413. vscroll.Maximum = document.ViewPortHeight;
  1414. }
  1415. if (!WordWrap) {
  1416. if ((scrollbars & RichTextBoxScrollBars.Horizontal) != 0) {
  1417. if (((scrollbars & RichTextBoxScrollBars.ForcedHorizontal) != 0) || hscroll.Enabled) {
  1418. hscroll.Visible = true;
  1419. } else {
  1420. hscroll.Visible = false;
  1421. }
  1422. } else {
  1423. hscroll.Visible = false;
  1424. }
  1425. }
  1426. if ((scrollbars & RichTextBoxScrollBars.Vertical) != 0) {
  1427. if (((scrollbars & RichTextBoxScrollBars.ForcedVertical) != 0) || vscroll.Enabled) {
  1428. vscroll.Visible = true;
  1429. } else {
  1430. vscroll.Visible = false;
  1431. }
  1432. } else {
  1433. vscroll.Visible = false;
  1434. }
  1435. PositionControls ();
  1436. }
  1437. private void document_WidthChanged(object sender, EventArgs e) {
  1438. CalculateScrollBars();
  1439. }
  1440. private void document_HeightChanged(object sender, EventArgs e) {
  1441. CalculateScrollBars();
  1442. }
  1443. private void hscroll_ValueChanged(object sender, EventArgs e) {
  1444. int old_viewport_x;
  1445. old_viewport_x = document.ViewPortX;
  1446. document.ViewPortX = this.hscroll.Value;
  1447. //
  1448. // Before scrolling we want to destroy the caret, then draw a new one after the scroll
  1449. // the reason for this is that scrolling changes the coordinates of the caret, and we
  1450. // will get tracers if we don't
  1451. //
  1452. if (Focused)
  1453. document.CaretLostFocus ();
  1454. if (vscroll.Visible) {
  1455. XplatUI.ScrollWindow(this.Handle, new Rectangle(0, 0, ClientSize.Width - vscroll.Width, ClientSize.Height), old_viewport_x - this.hscroll.Value, 0, false);
  1456. } else {
  1457. XplatUI.ScrollWindow(this.Handle, ClientRectangle, old_viewport_x - this.hscroll.Value, 0, false);
  1458. }
  1459. if (Focused)
  1460. document.CaretHasFocus ();
  1461. EventHandler eh = (EventHandler)(Events [HScrolledEvent]);
  1462. if (eh != null)
  1463. eh (this, EventArgs.Empty);
  1464. }
  1465. private void vscroll_ValueChanged(object sender, EventArgs e) {
  1466. int old_viewport_y;
  1467. old_viewport_y = document.ViewPortY;
  1468. document.ViewPortY = this.vscroll.Value;
  1469. //
  1470. // Before scrolling we want to destroy the caret, then draw a new one after the scroll
  1471. // the reason for this is that scrolling changes the coordinates of the caret, and we
  1472. // will get tracers if we don't
  1473. //
  1474. if (Focused)
  1475. document.CaretLostFocus ();
  1476. if (hscroll.Visible) {
  1477. XplatUI.ScrollWindow(this.Handle, new Rectangle(0, 0, ClientSize.Width, ClientSize.Height - hscroll.Height), 0, old_viewport_y - this.vscroll.Value, false);
  1478. } else {
  1479. XplatUI.ScrollWindow(this.Handle, ClientRectangle, 0, old_viewport_y - this.vscroll.Value, false);
  1480. }
  1481. if (Focused)
  1482. document.CaretHasFocus ();
  1483. EventHandler eh = (EventHandler)(Events [VScrolledEvent]);
  1484. if (eh != null)
  1485. eh (this, EventArgs.Empty);
  1486. }
  1487. private void TextBoxBase_MouseMove(object sender, MouseEventArgs e) {
  1488. // FIXME - handle auto-scrolling if mouse is to the right/left of the window
  1489. if (e.Button == MouseButtons.Left && Capture) {
  1490. if (!ClientRectangle.Contains (e.X, e.Y)) {
  1491. if (scroll_timer == null) {
  1492. scroll_timer = new Timer ();
  1493. scroll_timer.Interval = 100;
  1494. scroll_timer.Tick += new EventHandler (ScrollTimerTickHandler);
  1495. }
  1496. if (!scroll_timer.Enabled) {
  1497. scroll_timer.Start ();
  1498. // Force the first tick
  1499. ScrollTimerTickHandler (null, EventArgs.Empty);
  1500. }
  1501. }
  1502. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  1503. if (click_mode == CaretSelection.Position) {
  1504. document.SetSelectionToCaret(false);
  1505. document.DisplayCaret();
  1506. }
  1507. }
  1508. }
  1509. private void TextBoxBase_FontOrColorChanged(object sender, EventArgs e) {
  1510. if (!richtext) {
  1511. Line line;
  1512. document.SuspendRecalc ();
  1513. // Font changes apply to the whole document
  1514. for (int i = 1; i <= document.Lines; i++) {
  1515. line = document.GetLine(i);
  1516. LineTag.FormatText(line, 1, line.text.Length, Font,
  1517. ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor),
  1518. null, FormatSpecified.Font | FormatSpecified.Color);
  1519. }
  1520. document.ResumeRecalc (false);
  1521. // Make sure the caret height is matching the new font height
  1522. document.AlignCaret();
  1523. }
  1524. }
  1525. private void ScrollTimerTickHandler (object sender, EventArgs e)
  1526. {
  1527. Point pt = Cursor.Position;
  1528. pt = PointToClient (pt);
  1529. if (pt.X < ClientRectangle.Left) {
  1530. document.MoveCaret(CaretDirection.CharBackNoWrap);
  1531. document.SetSelectionToCaret(false);
  1532. CaretMoved(this, null);
  1533. } else if (pt.X > ClientRectangle.Right) {
  1534. document.MoveCaret(CaretDirection.CharForwardNoWrap);
  1535. document.SetSelectionToCaret(false);
  1536. CaretMoved(this, null);
  1537. } else if (pt.Y > ClientRectangle.Bottom) {
  1538. document.MoveCaret(CaretDirection.LineDown);
  1539. document.SetSelectionToCaret(false);
  1540. CaretMoved(this, null);
  1541. } else if (pt.Y < ClientRectangle.Top) {
  1542. document.MoveCaret(CaretDirection.LineUp);
  1543. document.SetSelectionToCaret(false);
  1544. CaretMoved(this, null);
  1545. }
  1546. }
  1547. /// <summary>Ensure the caret is always visible</summary>
  1548. internal void CaretMoved(object sender, EventArgs e) {
  1549. Point pos;
  1550. int height;
  1551. if (!IsHandleCreated || canvas_width < 1 || canvas_height < 1)
  1552. return;
  1553. document.MoveCaretToTextTag ();
  1554. pos = document.Caret;
  1555. //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);
  1556. // Horizontal scrolling:
  1557. // If the caret moves to the left outside the visible area, we jump the document into view, not just one
  1558. // character, but 1/3 of the width of the document
  1559. // If the caret moves to the right outside the visible area, we scroll just enough to keep the caret visible
  1560. // Handle horizontal scrolling
  1561. if (document.CaretLine.alignment == HorizontalAlignment.Left) {
  1562. // Check if we moved out of view to the left
  1563. if (pos.X < (document.ViewPortX)) {
  1564. do {
  1565. if ((hscroll.Value - document.ViewPortWidth / 3) >= hscroll.Minimum) {
  1566. hscroll.Value -= document.ViewPortWidth / 3;
  1567. } else {
  1568. hscroll.Value = hscroll.Minimum;
  1569. }
  1570. } while (hscroll.Value > pos.X);
  1571. }
  1572. // Check if we moved out of view to the right
  1573. if ((pos.X >= (document.ViewPortWidth + document.ViewPortX)) && (hscroll.Value != hscroll.Maximum)) {
  1574. if ((pos.X - document.ViewPortWidth + 1) <= hscroll.Maximum) {
  1575. if (pos.X - document.ViewPortWidth >= 0) {
  1576. hscroll.Value = pos.X - document.ViewPortWidth + 1;
  1577. } else {
  1578. hscroll.Value = 0;
  1579. }
  1580. } else {
  1581. hscroll.Value = hscroll.Maximum;
  1582. }
  1583. }
  1584. } else if (document.CaretLine.alignment == HorizontalAlignment.Right) {
  1585. // hscroll.Value = pos.X;
  1586. // if ((pos.X > (this.canvas_width + document.ViewPortX)) && (hscroll.Enabled && (hscroll.Value != hscroll.Maximum))) {
  1587. // hscroll.Value = hscroll.Maximum;
  1588. // }
  1589. } else {
  1590. // FIXME - implement center cursor alignment
  1591. }
  1592. if (!document.multiline) {
  1593. return;
  1594. }
  1595. // Handle vertical scrolling
  1596. height = document.CaretLine.Height + 1;
  1597. if (pos.Y < document.ViewPortY) {
  1598. vscroll.Value = pos.Y;
  1599. }
  1600. if ((pos.Y + height) > (document.ViewPortY + canvas_height)) {
  1601. vscroll.Value = Math.Min (vscroll.Maximum, pos.Y - canvas_height + height);
  1602. }
  1603. }
  1604. internal bool Paste(IDataObject clip, DataFormats.Format format, bool obey_length) {
  1605. string s;
  1606. if (clip == null)
  1607. return false;
  1608. if (format == null) {
  1609. if ((this is RichTextBox) && clip.GetDataPresent(DataFormats.Rtf)) {
  1610. format = DataFormats.GetFormat(DataFormats.Rtf);
  1611. } else if ((this is RichTextBox) && clip.GetDataPresent (DataFormats.Bitmap)) {
  1612. format = DataFormats.GetFormat (DataFormats.Bitmap);
  1613. } else if (clip.GetDataPresent(DataFormats.UnicodeText)) {
  1614. format = DataFormats.GetFormat(DataFormats.UnicodeText);
  1615. } else if (clip.GetDataPresent(DataFormats.Text)) {
  1616. format = DataFormats.GetFormat(DataFormats.Text);
  1617. } else {
  1618. return false;
  1619. }
  1620. } else {
  1621. if ((format.Name == DataFormats.Rtf) && !(this is RichTextBox)) {
  1622. return false;
  1623. }
  1624. if (!clip.GetDataPresent(format.Name)) {
  1625. return false;
  1626. }
  1627. }
  1628. if (format.Name == DataFormats.Rtf) {
  1629. document.undo.BeginUserAction (Locale.GetText ("Paste"));
  1630. ((RichTextBox)this).SelectedRtf = (string)clip.GetData(DataFormats.Rtf);
  1631. document.undo.EndUserAction ();
  1632. return true;
  1633. } else if (format.Name == DataFormats.Bitmap) {
  1634. document.undo.BeginUserAction (Locale.GetText ("Paste"));
  1635. // document.InsertImage (document.caret.line, document.caret.pos, (Image) clip.GetData (DataFormats.Bitmap));
  1636. document.MoveCaret (CaretDirection.CharForward);
  1637. document.undo.EndUserAction ();
  1638. return true;
  1639. } else if (format.Name == DataFormats.UnicodeText) {
  1640. s = (string)clip.GetData(DataFormats.UnicodeText);
  1641. } else if (format.Name == DataFormats.Text) {
  1642. s = (string)clip.GetData(DataFormats.Text);
  1643. } else {
  1644. return false;
  1645. }
  1646. if (!obey_length) {
  1647. document.undo.BeginUserAction (Locale.GetText ("Paste"));
  1648. this.SelectedText = s;
  1649. document.undo.EndUserAction ();
  1650. } else {
  1651. if ((s.Length + document.Length) < max_length) {
  1652. document.undo.BeginUserAction (Locale.GetText ("Paste"));
  1653. this.SelectedText = s;
  1654. document.undo.EndUserAction ();
  1655. } else if (document.Length < max_length) {
  1656. document.undo.BeginUserAction (Locale.GetText ("Paste"));
  1657. this.SelectedText = s.Substring (0, max_length - document.Length);
  1658. document.undo.EndUserAction ();
  1659. }
  1660. }
  1661. return true;
  1662. }
  1663. internal abstract Color ChangeBackColor (Color backColor);
  1664. #endregion // Private Methods
  1665. #if NET_2_0
  1666. // This is called just before OnTextChanged is called.
  1667. internal virtual void OnTextUpdate ()
  1668. {
  1669. }
  1670. protected override void OnTextChanged (EventArgs e)
  1671. {
  1672. base.OnTextChanged (e);
  1673. }
  1674. public virtual int GetLineFromCharIndex (int index)
  1675. {
  1676. Line line_out;
  1677. LineTag tag_out;
  1678. int pos;
  1679. document.CharIndexToLineTag (index, out line_out, out tag_out, out pos);
  1680. return line_out.LineNo;
  1681. }
  1682. protected override void OnMouseUp (MouseEventArgs e)
  1683. {
  1684. base.OnMouseUp (e);
  1685. }
  1686. #endif
  1687. }
  1688. }