TextBoxBase.cs 52 KB

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