TextBoxBase.cs 51 KB

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