TextBoxBase.cs 52 KB

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