TextBoxBase.cs 51 KB

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