TextBoxBase.cs 50 KB

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