TextBoxBase.cs 52 KB

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