TextBoxBase.cs 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  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. for (i = 1; i < document.Lines; i++) {
  449. if (i > 1)
  450. sb.Append (Environment.NewLine);
  451. sb.Append(document.GetLine(i).text.ToString());
  452. }
  453. sb.Append(document.GetLine(document.Lines).text.ToString());
  454. return sb.ToString();
  455. }
  456. }
  457. set {
  458. if (value == Text)
  459. return;
  460. if ((value != null) && (value != "")) {
  461. Line line;
  462. if (multiline) {
  463. string[] lines;
  464. lines = value.Split(new char[] {'\n'});
  465. for (int i = 0; i < lines.Length; i++) {
  466. if (lines[i].EndsWith("\r")) {
  467. lines[i] = lines[i].Substring(0, lines[i].Length - 1);
  468. }
  469. }
  470. this.Lines = lines;
  471. line = document.GetLine(1);
  472. if (!Focused) {
  473. document.SetSelectionStart(line, 0);
  474. line = document.GetLine(document.Lines);
  475. document.SetSelectionEnd(line, line.text.Length);
  476. selection_length = -1;
  477. document.PositionCaret(line, line.text.Length);
  478. } else {
  479. document.SetSelectionStart(line, 0);
  480. document.SetSelectionEnd(line, 0);
  481. selection_length = -1;
  482. document.PositionCaret(line, 0);
  483. }
  484. } else {
  485. document.Clear();
  486. document.Add(1, CaseAdjust(value), alignment, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
  487. CalculateDocument();
  488. line = document.GetLine(1);
  489. if (!Focused) {
  490. document.SetSelectionStart(line, 0);
  491. document.SetSelectionEnd(line, value.Length);
  492. selection_length = -1;
  493. document.PositionCaret(line, value.Length);
  494. } else {
  495. document.SetSelectionStart(line, 0);
  496. document.SetSelectionEnd(line, 0);
  497. selection_length = -1;
  498. document.PositionCaret(line, 0);
  499. }
  500. }
  501. } else {
  502. document.Empty();
  503. CalculateDocument();
  504. }
  505. OnTextChanged(EventArgs.Empty);
  506. }
  507. }
  508. [Browsable(false)]
  509. public virtual int TextLength {
  510. get {
  511. if (document == null || document.Root == null || document.Root.text == null) {
  512. return 0;
  513. }
  514. if (!multiline) {
  515. return document.Root.text.Length;
  516. } else {
  517. int total;
  518. int i;
  519. total = 0;
  520. for (i = 1; i < document.Lines; i++) {
  521. total += document.GetLine(i).text.Length + Environment.NewLine.Length;
  522. }
  523. total += document.GetLine(i).text.Length;
  524. return total;
  525. }
  526. }
  527. }
  528. [DefaultValue(true)]
  529. [Localizable(true)]
  530. [MWFCategory("Behavior")]
  531. public bool WordWrap {
  532. get {
  533. return word_wrap;
  534. }
  535. set {
  536. if (value != word_wrap) {
  537. if (multiline) {
  538. word_wrap = value;
  539. document.Wrap = value;
  540. }
  541. }
  542. }
  543. }
  544. #endregion // Public Instance Properties
  545. #region Protected Instance Properties
  546. protected override CreateParams CreateParams {
  547. get {
  548. return base.CreateParams;
  549. }
  550. }
  551. protected override System.Drawing.Size DefaultSize {
  552. get {
  553. return new Size(100, 20);
  554. }
  555. }
  556. #endregion // Protected Instance Properties
  557. #region Public Instance Methods
  558. public void AppendText(string text) {
  559. if (multiline) {
  560. // Grab the formatting for the last element
  561. document.MoveCaret(CaretDirection.CtrlEnd);
  562. // grab the end tag
  563. if (document.CaretTag.next != null) {
  564. document.CaretTag = document.CaretTag.next;
  565. }
  566. document.Insert(document.CaretLine, document.CaretTag, document.CaretPosition, false, text);
  567. CalculateDocument();
  568. } else {
  569. document.MoveCaret(CaretDirection.CtrlEnd);
  570. document.InsertStringAtCaret(text, true);
  571. Invalidate();
  572. }
  573. document.MoveCaret(CaretDirection.CtrlEnd);
  574. document.SetSelectionStart(document.CaretLine, document.CaretPosition);
  575. document.SetSelectionEnd(document.CaretLine, document.CaretPosition);
  576. selection_length = -1;
  577. OnTextChanged(EventArgs.Empty);
  578. }
  579. public void Clear() {
  580. Text = null;
  581. }
  582. public void ClearUndo() {
  583. document.undo.Clear();
  584. }
  585. public void Copy() {
  586. DataObject o;
  587. o = new DataObject(DataFormats.Text, SelectedText);
  588. if (this is RichTextBox) {
  589. o.SetData(DataFormats.Rtf, ((RichTextBox)this).SelectedRtf);
  590. }
  591. Clipboard.SetDataObject(o);
  592. }
  593. public void Cut() {
  594. DataObject o;
  595. o = new DataObject(DataFormats.Text, SelectedText);
  596. if (this is RichTextBox) {
  597. o.SetData(DataFormats.Rtf, ((RichTextBox)this).SelectedRtf);
  598. }
  599. Clipboard.SetDataObject(o);
  600. document.ReplaceSelection("");
  601. }
  602. public void Paste() {
  603. Paste(Clipboard.GetDataObject(), null, false);
  604. }
  605. public void ScrollToCaret() {
  606. if (IsHandleCreated) {
  607. CaretMoved(this, EventArgs.Empty);
  608. }
  609. }
  610. public void Select(int start, int length) {
  611. SelectionStart = start;
  612. SelectionLength = length;
  613. }
  614. public void SelectAll() {
  615. Line last;
  616. last = document.GetLine(document.Lines);
  617. document.SetSelectionStart(document.GetLine(1), 0);
  618. document.SetSelectionEnd(last, last.text.Length);
  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. if (AcceptsTabChanged != null) {
  668. AcceptsTabChanged(this, e);
  669. }
  670. }
  671. protected virtual void OnAutoSizeChanged(EventArgs e) {
  672. if (AutoSizeChanged != null) {
  673. AutoSizeChanged(this, e);
  674. }
  675. }
  676. protected virtual void OnBorderStyleChanged(EventArgs e) {
  677. if (BorderStyleChanged != null) {
  678. BorderStyleChanged(this, e);
  679. }
  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. if (HideSelectionChanged != null) {
  697. HideSelectionChanged(this, e);
  698. }
  699. }
  700. protected virtual void OnModifiedChanged(EventArgs e) {
  701. if (ModifiedChanged != null) {
  702. ModifiedChanged(this, e);
  703. }
  704. }
  705. protected virtual void OnMultilineChanged(EventArgs e) {
  706. if (MultilineChanged != null) {
  707. MultilineChanged(this, e);
  708. }
  709. }
  710. protected virtual void OnReadOnlyChanged(EventArgs e) {
  711. if (ReadOnlyChanged != null) {
  712. ReadOnlyChanged(this, e);
  713. }
  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("");
  864. }
  865. document.SetSelectionToCaret(true);
  866. line = document.CaretLine;
  867. document.Split(document.CaretLine, document.CaretTag, document.CaretPosition, false);
  868. OnTextChanged(EventArgs.Empty);
  869. document.UpdateView(line, 2, 0);
  870. document.MoveCaret(CaretDirection.CharForward);
  871. CaretMoved(this, null);
  872. return true;
  873. }
  874. break;
  875. }
  876. case Keys.Tab: {
  877. if (!read_only && accepts_tab && multiline) {
  878. document.InsertChar(document.CaretLine, document.CaretPosition, '\t');
  879. if (document.selection_visible) {
  880. document.ReplaceSelection("");
  881. }
  882. document.SetSelectionToCaret(true);
  883. OnTextChanged(EventArgs.Empty);
  884. CaretMoved(this, null);
  885. return true;
  886. }
  887. break;
  888. }
  889. case Keys.Insert: {
  890. if (shift) {
  891. Paste(Clipboard.GetDataObject(), null, true);
  892. return true;
  893. }
  894. if (control) {
  895. Copy();
  896. return true;
  897. }
  898. // FIXME - need overwrite/insert toggle?
  899. return false;
  900. }
  901. case Keys.PageUp: {
  902. if ((Control.ModifierKeys & Keys.Control) != 0) {
  903. document.MoveCaret(CaretDirection.CtrlPgUp);
  904. } else {
  905. document.MoveCaret(CaretDirection.PgUp);
  906. }
  907. return true;
  908. }
  909. case Keys.PageDown: {
  910. if ((Control.ModifierKeys & Keys.Control) != 0) {
  911. document.MoveCaret(CaretDirection.CtrlPgDn);
  912. } else {
  913. document.MoveCaret(CaretDirection.PgDn);
  914. }
  915. return true;
  916. }
  917. case Keys.Delete: {
  918. if (shift) {
  919. Cut();
  920. return true;
  921. }
  922. if (read_only) {
  923. break;
  924. }
  925. if (document.selection_visible) {
  926. document.ReplaceSelection("");
  927. } else {
  928. // DeleteChar only deletes on the line, doesn't do the combine
  929. if (document.CaretPosition == document.CaretLine.Text.Length) {
  930. if (document.CaretLine.LineNo < document.Lines) {
  931. Line line;
  932. line = document.GetLine(document.CaretLine.LineNo + 1);
  933. document.Combine(document.CaretLine, line);
  934. document.UpdateView(document.CaretLine, 2, 0);
  935. #if not_Debug
  936. Line check_first;
  937. Line check_second;
  938. check_first = document.GetLine(document.CaretLine.LineNo);
  939. check_second = document.GetLine(check_first.line_no + 1);
  940. Console.WriteLine("Post-UpdateView: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
  941. #endif
  942. // Caret doesn't move
  943. }
  944. } else {
  945. if (!control) {
  946. document.DeleteChar(document.CaretTag, document.CaretPosition, true);
  947. } else {
  948. int end_pos;
  949. end_pos = document.CaretPosition;
  950. while ((end_pos < document.CaretLine.Text.Length) && !Document.IsWordSeparator(document.CaretLine.Text[end_pos])) {
  951. end_pos++;
  952. }
  953. if (end_pos < document.CaretLine.Text.Length) {
  954. end_pos++;
  955. }
  956. document.DeleteChars(document.CaretTag, document.CaretPosition, end_pos - document.CaretPosition);
  957. }
  958. }
  959. }
  960. OnTextChanged(EventArgs.Empty);
  961. document.AlignCaret();
  962. document.UpdateCaret();
  963. CaretMoved(this, null);
  964. return true;
  965. }
  966. }
  967. return false;
  968. }
  969. private void HandleBackspace(bool control) {
  970. bool fire_changed;
  971. fire_changed = false;
  972. // delete only deletes on the line, doesn't do the combine
  973. if (document.selection_visible) {
  974. document.ReplaceSelection("");
  975. fire_changed = true;
  976. }
  977. document.SetSelectionToCaret(true);
  978. if (document.CaretPosition == 0) {
  979. if (document.CaretLine.LineNo > 1) {
  980. Line line;
  981. int new_caret_pos;
  982. line = document.GetLine(document.CaretLine.LineNo - 1);
  983. new_caret_pos = line.text.Length;
  984. document.Combine(line, document.CaretLine);
  985. document.UpdateView(line, 1, 0);
  986. document.PositionCaret(line, new_caret_pos);
  987. //document.MoveCaret(CaretDirection.CharForward);
  988. document.UpdateCaret();
  989. fire_changed = true;
  990. }
  991. } else {
  992. if (!control || document.CaretPosition == 0) {
  993. document.DeleteChar(document.CaretTag, document.CaretPosition, false);
  994. document.MoveCaret(CaretDirection.CharBack);
  995. } else {
  996. int start_pos;
  997. start_pos = document.CaretPosition - 1;
  998. while ((start_pos > 0) && !Document.IsWordSeparator(document.CaretLine.Text[start_pos - 1])) {
  999. start_pos--;
  1000. }
  1001. document.DeleteChars(document.CaretTag, start_pos, document.CaretPosition - start_pos);
  1002. document.PositionCaret(document.CaretLine, start_pos);
  1003. }
  1004. document.UpdateCaret();
  1005. fire_changed = true;
  1006. }
  1007. if (fire_changed) {
  1008. OnTextChanged(EventArgs.Empty);
  1009. }
  1010. CaretMoved(this, null);
  1011. }
  1012. protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
  1013. // Make sure we don't get sized bigger than we want to be
  1014. if (!richtext) {
  1015. if (!multiline) {
  1016. if (height != PreferredHeight) {
  1017. requested_height = height;
  1018. height = PreferredHeight;
  1019. specified |= BoundsSpecified.Height;
  1020. }
  1021. }
  1022. }
  1023. base.SetBoundsCore (x, y, width, height, specified);
  1024. }
  1025. protected override void WndProc(ref Message m) {
  1026. switch ((Msg)m.Msg) {
  1027. case Msg.WM_KEYDOWN: {
  1028. if (ProcessKeyMessage(ref m) || ProcessKey((Keys)m.WParam.ToInt32() | XplatUI.State.ModifierKeys)) {
  1029. m.Result = IntPtr.Zero;
  1030. return;
  1031. }
  1032. DefWndProc (ref m);
  1033. return;
  1034. }
  1035. case Msg.WM_CHAR: {
  1036. int ch;
  1037. if (ProcessKeyMessage(ref m)) {
  1038. m.Result = IntPtr.Zero;
  1039. return;
  1040. }
  1041. if (read_only) {
  1042. return;
  1043. }
  1044. m.Result = IntPtr.Zero;
  1045. ch = m.WParam.ToInt32();
  1046. if (ch == 127) {
  1047. HandleBackspace(true);
  1048. } else if (ch >= 32) {
  1049. if (document.selection_visible) {
  1050. document.ReplaceSelection("");
  1051. }
  1052. char c = (char)m.WParam;
  1053. switch (character_casing) {
  1054. case CharacterCasing.Upper:
  1055. c = Char.ToUpper((char) m.WParam);
  1056. break;
  1057. case CharacterCasing.Lower:
  1058. c = Char.ToLower((char) m.WParam);
  1059. break;
  1060. }
  1061. if (document.Length < max_length) {
  1062. document.InsertCharAtCaret(c, true);
  1063. OnTextChanged(EventArgs.Empty);
  1064. CaretMoved(this, null);
  1065. } else {
  1066. XplatUI.AudibleAlert();
  1067. }
  1068. return;
  1069. } else if (ch == 8) {
  1070. HandleBackspace(false);
  1071. }
  1072. return;
  1073. }
  1074. default: {
  1075. base.WndProc(ref m);
  1076. return;
  1077. }
  1078. }
  1079. }
  1080. #endregion // Protected Instance Methods
  1081. #region Events
  1082. public event EventHandler AcceptsTabChanged;
  1083. public event EventHandler AutoSizeChanged;
  1084. [Browsable(false)]
  1085. [EditorBrowsable(EditorBrowsableState.Never)]
  1086. public new event EventHandler BackgroundImageChanged {
  1087. add { base.BackgroundImageChanged += value; }
  1088. remove { base.BackgroundImageChanged -= value; }
  1089. }
  1090. public event EventHandler BorderStyleChanged;
  1091. [Browsable(false)]
  1092. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1093. public event EventHandler Click {
  1094. add { base.Click += value; }
  1095. remove { base.Click -= value; }
  1096. }
  1097. public event EventHandler HideSelectionChanged;
  1098. public event EventHandler ModifiedChanged;
  1099. public event EventHandler MultilineChanged;
  1100. [Browsable(false)]
  1101. [EditorBrowsable(EditorBrowsableState.Never)]
  1102. public event PaintEventHandler Paint;
  1103. public event EventHandler ReadOnlyChanged;
  1104. internal event EventHandler HScrolled;
  1105. internal event EventHandler VScrolled;
  1106. #endregion // Events
  1107. #region Private Methods
  1108. internal Document Document {
  1109. get {
  1110. return document;
  1111. }
  1112. set {
  1113. document = value;
  1114. }
  1115. }
  1116. internal bool ShowSelection {
  1117. get {
  1118. if (show_selection) {
  1119. return true;
  1120. }
  1121. return has_focus;
  1122. }
  1123. set {
  1124. if (show_selection == value)
  1125. return;
  1126. show_selection = value;
  1127. // Currently InvalidateSelectionArea is commented out so do a full invalidate
  1128. document.InvalidateSelectionArea();
  1129. }
  1130. }
  1131. internal Graphics CreateGraphicsInternal() {
  1132. if (IsHandleCreated) {
  1133. return base.CreateGraphics();
  1134. }
  1135. return Graphics.FromImage(bmp);
  1136. }
  1137. #if Debug
  1138. static int current;
  1139. #endif
  1140. internal override void OnPaintInternal (PaintEventArgs pevent) {
  1141. // Fill background
  1142. if (backcolor_set || (Enabled && !read_only)) {
  1143. pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), pevent.ClipRectangle);
  1144. } else {
  1145. pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorControl), pevent.ClipRectangle);
  1146. }
  1147. pevent.Graphics.TextRenderingHint=TextRenderingHint.AntiAlias;
  1148. // Draw the viewable document
  1149. document.Draw(pevent.Graphics, pevent.ClipRectangle);
  1150. Rectangle rect = ClientRectangle;
  1151. rect.Width--;
  1152. rect.Height--;
  1153. //pevent.Graphics.DrawRectangle(ThemeEngine.Current.ResPool.GetPen(ThemeEngine.Current.ColorControlDark), rect);
  1154. #if Debug
  1155. int start;
  1156. int end;
  1157. Line line;
  1158. int line_no;
  1159. Pen p;
  1160. p = new Pen(Color.Red, 1);
  1161. // First, figure out from what line to what line we need to draw
  1162. start = document.GetLineByPixel(pevent.ClipRectangle.Top - document.ViewPortY, false).line_no;
  1163. end = document.GetLineByPixel(pevent.ClipRectangle.Bottom - document.ViewPortY, false).line_no;
  1164. //Console.WriteLine("Starting drawing on line '{0}'", document.GetLine(start));
  1165. //Console.WriteLine("Ending drawing on line '{0}'", document.GetLine(end));
  1166. line_no = start;
  1167. while (line_no <= end) {
  1168. line = document.GetLine(line_no);
  1169. if (draw_lines) {
  1170. for (int i = 0; i < line.text.Length; i++) {
  1171. 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);
  1172. }
  1173. }
  1174. line_no++;
  1175. }
  1176. #endif
  1177. }
  1178. internal override void OnGotFocusInternal (EventArgs e)
  1179. {
  1180. document.CaretHasFocus ();
  1181. base.OnGotFocusInternal (e);
  1182. }
  1183. internal override void OnLostFocusInternal (EventArgs e)
  1184. {
  1185. document.CaretLostFocus ();
  1186. base.OnLostFocusInternal (e);
  1187. }
  1188. private void TextBoxBase_MouseDown(object sender, MouseEventArgs e) {
  1189. if (e.Button == MouseButtons.Left) {
  1190. TimeSpan interval;
  1191. interval = DateTime.Now - click_last;
  1192. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  1193. // Handle place caret/select word/select line behaviour
  1194. if (e.Clicks == 1) {
  1195. if (SystemInformation.DoubleClickTime < interval.TotalMilliseconds) {
  1196. #if DebugClick
  1197. 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));
  1198. #endif
  1199. document.SetSelectionToCaret(true);
  1200. click_mode = CaretSelection.Position;
  1201. } else {
  1202. #if DebugClick
  1203. Console.WriteLine("Tripple Click Selecting line");
  1204. #endif
  1205. document.ExpandSelection(CaretSelection.Line, false);
  1206. click_mode = CaretSelection.Line;
  1207. }
  1208. } else {
  1209. // We select the line if the word is already selected, and vice versa
  1210. if (click_mode != CaretSelection.Word) {
  1211. if (click_mode == CaretSelection.Line) {
  1212. document.Invalidate(document.selection_start.line, 0, document.selection_start.line, document.selection_start.line.text.Length);
  1213. }
  1214. click_mode = CaretSelection.Word;
  1215. document.ExpandSelection(CaretSelection.Word, false); // Setting initial selection
  1216. } else {
  1217. click_mode = CaretSelection.Line;
  1218. document.ExpandSelection(CaretSelection.Line, false); // Setting initial selection
  1219. }
  1220. }
  1221. // Reset
  1222. click_last = DateTime.Now;
  1223. return;
  1224. }
  1225. if ((e.Button == MouseButtons.Middle) && (((int)Environment.OSVersion.Platform == 4) || ((int)Environment.OSVersion.Platform == 128))) {
  1226. Document.Marker marker;
  1227. marker.tag = document.FindCursor(e.X + document.ViewPortX, e.Y + document.ViewPortY, out marker.pos);
  1228. marker.line = marker.tag.line;
  1229. marker.height = marker.tag.height;
  1230. document.SetSelection(marker.line, marker.pos, marker.line, marker.pos);
  1231. Paste (Clipboard.GetDataObject (true), null, true);
  1232. }
  1233. #if Debug
  1234. LineTag tag;
  1235. Line line;
  1236. int pos;
  1237. if (e.Button == MouseButtons.Right) {
  1238. draw_lines = !draw_lines;
  1239. this.Invalidate();
  1240. Console.WriteLine("SelectedText: {0}, length {1}", this.SelectedText, this.SelectionLength);
  1241. Console.WriteLine("Selection start: {0}", this.SelectionStart);
  1242. this.SelectionStart = 10;
  1243. this.SelectionLength = 5;
  1244. return;
  1245. }
  1246. tag = document.FindTag(e.X + document.ViewPortX, e.Y + document.ViewPortY, out pos, false);
  1247. Console.WriteLine("Click found tag {0}, character {1}", tag, pos);
  1248. line = tag.line;
  1249. switch(current) {
  1250. 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;
  1251. 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;
  1252. 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;
  1253. 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;
  1254. 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;
  1255. 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;
  1256. }
  1257. current++;
  1258. if (current==6) {
  1259. current=0;
  1260. }
  1261. // Update/Recalculate what we see
  1262. document.UpdateView(line, 0);
  1263. // Make sure our caret is properly positioned and sized
  1264. document.AlignCaret();
  1265. #endif
  1266. }
  1267. private void TextBoxBase_MouseUp(object sender, MouseEventArgs e) {
  1268. if (e.Button == MouseButtons.Left) {
  1269. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  1270. if (click_mode == CaretSelection.Position) {
  1271. document.SetSelectionToCaret(false);
  1272. document.DisplayCaret();
  1273. } else {
  1274. document.ExpandSelection(click_mode, true);
  1275. }
  1276. if (scroll_timer != null) {
  1277. scroll_timer.Enabled = false;
  1278. }
  1279. return;
  1280. }
  1281. }
  1282. private void PositionControls ()
  1283. {
  1284. if (hscroll.Visible) {
  1285. //vscroll.Maximum += hscroll.Height;
  1286. canvas_height = ClientSize.Height - hscroll.Height;
  1287. } else {
  1288. canvas_height = ClientSize.Height;
  1289. }
  1290. if (vscroll.Visible) {
  1291. //hscroll.Maximum += vscroll.Width;
  1292. canvas_width = ClientSize.Width - vscroll.Width;
  1293. } else {
  1294. canvas_width = ClientSize.Width;
  1295. }
  1296. document.ViewPortWidth = canvas_width;
  1297. document.ViewPortHeight = canvas_height;
  1298. // We always move them, they just might not be displayed
  1299. hscroll.Bounds = new Rectangle (ClientRectangle.Left, ClientRectangle.Height - hscroll.Height, ClientSize.Width - (vscroll.Visible ? vscroll.Width : 0), hscroll.Height);
  1300. vscroll.Bounds = new Rectangle (ClientRectangle.Right - vscroll.Width, ClientRectangle.Top, vscroll.Width, ClientSize.Height - (hscroll.Visible ? hscroll.Height : 0));
  1301. }
  1302. private void TextBoxBase_SizeChanged(object sender, EventArgs e) {
  1303. CalculateDocument();
  1304. }
  1305. private void TextBoxBase_MouseWheel(object sender, MouseEventArgs e) {
  1306. Line line;
  1307. int line_no;
  1308. if (!vscroll.Enabled) {
  1309. return;
  1310. }
  1311. if (e.Delta < 0) {
  1312. line_no = document.GetLineByPixel(document.ViewPortY, false).line_no + SystemInformation.MouseWheelScrollLines;
  1313. if (line_no > document.Lines) {
  1314. line_no = document.Lines;
  1315. }
  1316. } else {
  1317. line_no = document.GetLineByPixel(document.ViewPortY, false).line_no - SystemInformation.MouseWheelScrollLines;
  1318. if (line_no < 1) {
  1319. line_no = 1;
  1320. }
  1321. }
  1322. line = document.GetLine(line_no);
  1323. if (line.Y < vscroll.Maximum) {
  1324. vscroll.Value = line.Y;
  1325. } else {
  1326. vscroll.Value = vscroll.Maximum;
  1327. }
  1328. }
  1329. internal void CalculateDocument() {
  1330. if (!IsHandleCreated) {
  1331. return;
  1332. }
  1333. document.RecalculateDocument(CreateGraphicsInternal());
  1334. CalculateScrollBars();
  1335. Invalidate();
  1336. }
  1337. internal void CalculateScrollBars() {
  1338. // FIXME - need separate calculations for center and right alignment
  1339. if (!multiline) {
  1340. PositionControls ();
  1341. return;
  1342. }
  1343. if (document.Width >= document.ViewPortWidth) {
  1344. hscroll.Enabled = true;
  1345. hscroll.Minimum = 0;
  1346. hscroll.LargeChange = document.ViewPortWidth < 0 ? 0 : document.ViewPortWidth;
  1347. hscroll.Maximum = document.Width;
  1348. } else {
  1349. hscroll.Maximum = document.ViewPortWidth;
  1350. hscroll.Enabled = false;
  1351. }
  1352. if (document.Height >= document.ViewPortHeight) {
  1353. vscroll.Enabled = true;
  1354. vscroll.Minimum = 0;
  1355. vscroll.LargeChange = document.ViewPortHeight < 0 ? 0 : document.ViewPortHeight;
  1356. vscroll.Maximum = document.Height;
  1357. } else {
  1358. vscroll.Maximum = document.ViewPortHeight;
  1359. vscroll.Enabled = false;
  1360. }
  1361. if (!WordWrap) {
  1362. if ((scrollbars & RichTextBoxScrollBars.Horizontal) != 0) {
  1363. if (((scrollbars & RichTextBoxScrollBars.ForcedHorizontal) != 0) || hscroll.Enabled) {
  1364. hscroll.Visible = true;
  1365. } else {
  1366. hscroll.Visible = false;
  1367. }
  1368. } else {
  1369. hscroll.Visible = false;
  1370. }
  1371. }
  1372. if ((scrollbars & RichTextBoxScrollBars.Vertical) != 0) {
  1373. if (((scrollbars & RichTextBoxScrollBars.ForcedVertical) != 0) || vscroll.Enabled) {
  1374. vscroll.Visible = true;
  1375. } else {
  1376. vscroll.Visible = false;
  1377. }
  1378. } else {
  1379. vscroll.Visible = false;
  1380. }
  1381. PositionControls ();
  1382. }
  1383. private void document_WidthChanged(object sender, EventArgs e) {
  1384. CalculateScrollBars();
  1385. }
  1386. private void document_HeightChanged(object sender, EventArgs e) {
  1387. CalculateScrollBars();
  1388. }
  1389. private void hscroll_ValueChanged(object sender, EventArgs e) {
  1390. int old_viewport_x;
  1391. old_viewport_x = document.ViewPortX;
  1392. document.ViewPortX = this.hscroll.Value;
  1393. if (vscroll.Visible) {
  1394. XplatUI.ScrollWindow(this.Handle, new Rectangle(0, 0, ClientSize.Width - vscroll.Width, ClientSize.Height), old_viewport_x - this.hscroll.Value, 0, false);
  1395. } else {
  1396. XplatUI.ScrollWindow(this.Handle, ClientRectangle, old_viewport_x - this.hscroll.Value, 0, false);
  1397. }
  1398. document.UpdateCaret();
  1399. if (HScrolled != null) {
  1400. HScrolled(this, EventArgs.Empty);
  1401. }
  1402. }
  1403. private void vscroll_ValueChanged(object sender, EventArgs e) {
  1404. int old_viewport_y;
  1405. old_viewport_y = document.ViewPortY;
  1406. document.ViewPortY = this.vscroll.Value;
  1407. if (hscroll.Visible) {
  1408. XplatUI.ScrollWindow(this.Handle, new Rectangle(0, 0, ClientSize.Width, ClientSize.Height - hscroll.Height), 0, old_viewport_y - this.vscroll.Value, false);
  1409. } else {
  1410. XplatUI.ScrollWindow(this.Handle, ClientRectangle, 0, old_viewport_y - this.vscroll.Value, false);
  1411. }
  1412. document.UpdateCaret();
  1413. if (VScrolled != null) {
  1414. VScrolled(this, EventArgs.Empty);
  1415. }
  1416. }
  1417. private void TextBoxBase_MouseMove(object sender, MouseEventArgs e) {
  1418. // FIXME - handle auto-scrolling if mouse is to the right/left of the window
  1419. if (Capture) {
  1420. if (!ClientRectangle.Contains (e.X, e.Y)) {
  1421. if (scroll_timer == null) {
  1422. scroll_timer = new Timer ();
  1423. scroll_timer.Interval = 100;
  1424. scroll_timer.Tick += new EventHandler (ScrollTimerTickHandler);
  1425. }
  1426. if (!scroll_timer.Enabled) {
  1427. scroll_timer.Start ();
  1428. // Force the first tick
  1429. ScrollTimerTickHandler (null, EventArgs.Empty);
  1430. }
  1431. return;
  1432. }
  1433. document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
  1434. if (click_mode == CaretSelection.Position) {
  1435. document.SetSelectionToCaret(false);
  1436. document.DisplayCaret();
  1437. } else {
  1438. document.ExpandSelection(click_mode, true);
  1439. }
  1440. }
  1441. }
  1442. private void TextBoxBase_FontOrColorChanged(object sender, EventArgs e) {
  1443. if (!richtext) {
  1444. Line line;
  1445. // Font changes apply to the whole document
  1446. for (int i = 1; i <= document.Lines; i++) {
  1447. line = document.GetLine(i);
  1448. LineTag.FormatText(line, 1, line.text.Length, Font, ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor));
  1449. document.UpdateView(line, 0);
  1450. }
  1451. // Make sure the caret height is matching the new font height
  1452. document.AlignCaret();
  1453. }
  1454. }
  1455. private void ScrollTimerTickHandler (object sender, EventArgs e)
  1456. {
  1457. Point pt = Cursor.Position;
  1458. pt = PointToClient (pt);
  1459. if (pt.X < ClientRectangle.Left) {
  1460. document.MoveCaret(CaretDirection.CharBackNoWrap);
  1461. document.SetSelectionToCaret(false);
  1462. CaretMoved(this, null);
  1463. } else if (pt.X > ClientRectangle.Right) {
  1464. document.MoveCaret(CaretDirection.CharForwardNoWrap);
  1465. document.SetSelectionToCaret(false);
  1466. CaretMoved(this, null);
  1467. } else if (pt.Y > ClientRectangle.Bottom) {
  1468. document.MoveCaret(CaretDirection.LineDown);
  1469. document.SetSelectionToCaret(false);
  1470. CaretMoved(this, null);
  1471. } else if (pt.Y < ClientRectangle.Top) {
  1472. document.MoveCaret(CaretDirection.LineUp);
  1473. document.SetSelectionToCaret(false);
  1474. CaretMoved(this, null);
  1475. }
  1476. }
  1477. /// <summary>Ensure the caret is always visible</summary>
  1478. internal void CaretMoved(object sender, EventArgs e) {
  1479. Point pos;
  1480. int height;
  1481. pos = document.Caret;
  1482. //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);
  1483. // Horizontal scrolling:
  1484. // If the caret moves to the left outside the visible area, we jump the document into view, not just one
  1485. // character, but 1/3 of the width of the document
  1486. // If the caret moves to the right outside the visible area, we scroll just enough to keep the caret visible
  1487. // Handle horizontal scrolling
  1488. if (document.CaretLine.alignment == HorizontalAlignment.Left) {
  1489. // Check if we moved out of view to the left
  1490. if (pos.X < (document.ViewPortX)) {
  1491. do {
  1492. if ((hscroll.Value - document.ViewPortWidth / 3) >= hscroll.Minimum) {
  1493. hscroll.Value -= document.ViewPortWidth / 3;
  1494. } else {
  1495. hscroll.Value = hscroll.Minimum;
  1496. }
  1497. } while (hscroll.Value > pos.X);
  1498. }
  1499. // Check if we moved out of view to the right
  1500. if ((pos.X >= (document.ViewPortWidth + document.ViewPortX)) && (hscroll.Value != hscroll.Maximum)) {
  1501. if ((pos.X - document.ViewPortWidth + 1) <= hscroll.Maximum) {
  1502. if (pos.X - document.ViewPortWidth >= 0) {
  1503. hscroll.Value = pos.X - document.ViewPortWidth + 1;
  1504. } else {
  1505. hscroll.Value = 0;
  1506. }
  1507. } else {
  1508. hscroll.Value = hscroll.Maximum;
  1509. }
  1510. }
  1511. } else if (document.CaretLine.alignment == HorizontalAlignment.Right) {
  1512. // hscroll.Value = pos.X;
  1513. // if ((pos.X > (this.canvas_width + document.ViewPortX)) && (hscroll.Enabled && (hscroll.Value != hscroll.Maximum))) {
  1514. // hscroll.Value = hscroll.Maximum;
  1515. // }
  1516. } else {
  1517. // FIXME - implement center cursor alignment
  1518. }
  1519. if (!multiline) {
  1520. return;
  1521. }
  1522. // Handle vertical scrolling
  1523. height = document.CaretLine.Height + 1;
  1524. if (pos.Y < document.ViewPortY) {
  1525. vscroll.Value = pos.Y;
  1526. }
  1527. if ((pos.Y + height) > (document.ViewPortY + canvas_height)) {
  1528. vscroll.Value = pos.Y - canvas_height + height;
  1529. }
  1530. }
  1531. internal bool Paste(IDataObject clip, DataFormats.Format format, bool obey_length) {
  1532. string s;
  1533. if (clip == null)
  1534. return false;
  1535. if (format == null) {
  1536. if ((this is RichTextBox) && clip.GetDataPresent(DataFormats.Rtf)) {
  1537. format = DataFormats.GetFormat(DataFormats.Rtf);
  1538. } else if (clip.GetDataPresent(DataFormats.UnicodeText)) {
  1539. format = DataFormats.GetFormat(DataFormats.UnicodeText);
  1540. } else if (clip.GetDataPresent(DataFormats.Text)) {
  1541. format = DataFormats.GetFormat(DataFormats.Text);
  1542. } else {
  1543. return false;
  1544. }
  1545. } else {
  1546. if ((format.Name == DataFormats.Rtf) && !(this is RichTextBox)) {
  1547. return false;
  1548. }
  1549. if (!clip.GetDataPresent(format.Name)) {
  1550. return false;
  1551. }
  1552. }
  1553. if (format.Name == DataFormats.Rtf) {
  1554. ((RichTextBox)this).SelectedRtf = (string)clip.GetData(DataFormats.Rtf);
  1555. return true;
  1556. } else if (format.Name == DataFormats.UnicodeText) {
  1557. s = (string)clip.GetData(DataFormats.UnicodeText);
  1558. } else if (format.Name == DataFormats.Text) {
  1559. s = (string)clip.GetData(DataFormats.Text);
  1560. } else {
  1561. return false;
  1562. }
  1563. if (!obey_length) {
  1564. this.SelectedText = s;
  1565. } else {
  1566. if ((s.Length + document.Length) < max_length) {
  1567. this.SelectedText = s;
  1568. } else if (document.Length < max_length) {
  1569. this.SelectedText = s.Substring(0, max_length - document.Length);
  1570. }
  1571. }
  1572. return true;
  1573. }
  1574. #endregion // Private Methods
  1575. }
  1576. }