2
0

TextBoxBase.cs 51 KB

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