TextBoxBase.cs 51 KB

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