TextBoxBase.cs 48 KB

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