TextBoxBase.cs 52 KB

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