TextBoxBase.cs 48 KB

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