| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979 |
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- // Copyright (c) 2004-2006 Novell, Inc. (http://www.novell.com)
- //
- // Authors:
- // Peter Bartok [email protected]
- //
- //
- // NOT COMPLETE
- #undef Debug
- #undef DebugClick
- using System.ComponentModel;
- using System.ComponentModel.Design;
- using System.Drawing;
- using System.Drawing.Text;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Collections;
- namespace System.Windows.Forms {
- [DefaultEvent("TextChanged")]
- [Designer("System.Windows.Forms.Design.TextBoxBaseDesigner, " + Consts.AssemblySystem_Design)]
- public abstract class TextBoxBase : Control {
- #region Local Variables
- internal HorizontalAlignment alignment;
- internal bool accepts_tab;
- internal bool accepts_return;
- internal bool auto_size;
- internal bool backcolor_set;
- internal CharacterCasing character_casing;
- internal bool hide_selection;
- internal int max_length;
- internal bool modified;
- internal char password_char;
- internal bool read_only;
- internal bool word_wrap;
- internal Document document;
- internal LineTag caret_tag; // tag our cursor is in
- internal int caret_pos; // position on the line our cursor is in (can be 0 = beginning of line)
- internal ImplicitHScrollBar hscroll;
- internal ImplicitVScrollBar vscroll;
- internal RichTextBoxScrollBars scrollbars;
- internal Timer scroll_timer;
- internal bool richtext;
- internal bool show_selection; // set to true to always show selection, even if no focus is set
- internal int selection_length = -1; // set to the user-specified selection length, or -1 if none
- internal bool show_caret_w_selection; // TextBox shows the caret when the selection is visible
- internal int requested_height;
- internal int canvas_width;
- internal int canvas_height;
- static internal int track_width = 2; //
- static internal int track_border = 5; //
- internal DateTime click_last;
- internal int click_point_x;
- internal int click_point_y;
- internal CaretSelection click_mode;
- internal Bitmap bmp;
- #if Debug
- internal static bool draw_lines = false;
- #endif
- #endregion // Local Variables
- #region Internal Constructor
- // Constructor will go when complete, only for testing - pdb
- internal TextBoxBase() {
- alignment = HorizontalAlignment.Left;
- accepts_return = false;
- accepts_tab = false;
- auto_size = true;
- border_style = BorderStyle.Fixed3D;
- character_casing = CharacterCasing.Normal;
- hide_selection = true;
- max_length = 32767;
- modified = true;
- password_char = '\0';
- read_only = false;
- word_wrap = true;
- richtext = false;
- show_selection = false;
- show_caret_w_selection = (this is TextBox);
- document = new Document(this);
- document.WidthChanged += new EventHandler(document_WidthChanged);
- document.HeightChanged += new EventHandler(document_HeightChanged);
- //document.CaretMoved += new EventHandler(CaretMoved);
- document.Wrap = false;
- requested_height = -1;
- click_last = DateTime.Now;
- click_mode = CaretSelection.Position;
- bmp = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
- MouseDown += new MouseEventHandler(TextBoxBase_MouseDown);
- MouseUp += new MouseEventHandler(TextBoxBase_MouseUp);
- MouseMove += new MouseEventHandler(TextBoxBase_MouseMove);
- SizeChanged += new EventHandler(TextBoxBase_SizeChanged);
- FontChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
- ForeColorChanged += new EventHandler(TextBoxBase_FontOrColorChanged);
- MouseWheel += new MouseEventHandler(TextBoxBase_MouseWheel);
-
- scrollbars = RichTextBoxScrollBars.None;
- hscroll = new ImplicitHScrollBar();
- hscroll.ValueChanged += new EventHandler(hscroll_ValueChanged);
- hscroll.SetStyle (ControlStyles.Selectable, false);
- hscroll.Enabled = false;
- hscroll.Visible = false;
- hscroll.Maximum = Int32.MaxValue;
- vscroll = new ImplicitVScrollBar();
- vscroll.ValueChanged += new EventHandler(vscroll_ValueChanged);
- vscroll.SetStyle (ControlStyles.Selectable, false);
- vscroll.Enabled = false;
- vscroll.Visible = false;
- vscroll.Maximum = Int32.MaxValue;
- SuspendLayout ();
- this.Controls.AddImplicit (hscroll);
- this.Controls.AddImplicit (vscroll);
- ResumeLayout ();
-
- SetStyle(ControlStyles.UserPaint | ControlStyles.StandardClick, false);
- #if NET_2_0
- SetStyle(ControlStyles.UseTextForAccessibility, false);
- #endif
- canvas_width = ClientSize.Width;
- canvas_height = ClientSize.Height;
- document.ViewPortWidth = canvas_width;
- document.ViewPortHeight = canvas_height;
- Cursor = Cursors.IBeam;
- CalculateScrollBars();
- }
- #endregion // Internal Constructor
- #region Private and Internal Methods
- internal string CaseAdjust(string s) {
- if (character_casing == CharacterCasing.Normal) {
- return s;
- }
- if (character_casing == CharacterCasing.Lower) {
- return s.ToLower();
- } else {
- return s.ToUpper();
- }
- }
- internal override void HandleClick(int clicks, MouseEventArgs me) {
- // MS seems to fire the click event in spite of the styles they set
- bool click_set = GetStyle (ControlStyles.StandardClick);
- bool doubleclick_set = GetStyle (ControlStyles.StandardDoubleClick);
- // so explicitly set them to true first
- SetStyle (ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true);
- base.HandleClick (clicks, me);
- // then revert to our previous state
- if (!click_set)
- SetStyle (ControlStyles.StandardClick, false);
- if (!doubleclick_set)
- SetStyle (ControlStyles.StandardDoubleClick, false);
- }
- #endregion // Private and Internal Methods
- #region Public Instance Properties
- [DefaultValue(false)]
- [MWFCategory("Behavior")]
- public bool AcceptsTab {
- get {
- return accepts_tab;
- }
- set {
- if (value != accepts_tab) {
- accepts_tab = value;
- OnAcceptsTabChanged(EventArgs.Empty);
- }
- }
- }
- [DefaultValue(true)]
- [Localizable(true)]
- [RefreshProperties(RefreshProperties.Repaint)]
- [MWFCategory("Behavior")]
- public
- #if NET_2_0
- override
- #else
- virtual
- #endif
- bool AutoSize {
- get {
- return auto_size;
- }
- set {
- if (value != auto_size) {
- auto_size = value;
- if (auto_size) {
- if (PreferredHeight != ClientSize.Height) {
- ClientSize = new Size(ClientSize.Width, PreferredHeight);
- }
- }
- OnAutoSizeChanged(EventArgs.Empty);
- }
- }
- }
- [DispId(-501)]
- public override System.Drawing.Color BackColor {
- get {
- return base.BackColor;
- }
- set {
- if (value != ThemeEngine.Current.ColorWindow) {
- backcolor_set = true;
- } else {
- backcolor_set = false;
- }
- base.BackColor = value;
- }
- }
- [Browsable(false)]
- [EditorBrowsable(EditorBrowsableState.Never)]
- public override System.Drawing.Image BackgroundImage {
- get {
- return base.BackgroundImage;
- }
- set {
- base.BackgroundImage = value;
- }
- }
- [DefaultValue(BorderStyle.Fixed3D)]
- [DispId(-504)]
- [MWFCategory("Appearance")]
- public BorderStyle BorderStyle {
- get { return InternalBorderStyle; }
- set {
- InternalBorderStyle = value;
- OnBorderStyleChanged(EventArgs.Empty);
- }
- }
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public bool CanUndo {
- get {
- return document.undo.CanUndo;
- }
- }
- [DispId(-513)]
- public override System.Drawing.Color ForeColor {
- get {
- return base.ForeColor;
- }
- set {
- // This is to duplicate a bug on MS (80590)
- // My guess is that they do not create their
- // textdocument until the handle is created,
- // and forecolor is actually a property of the
- // textdocument.
- if (this is TextBox && !IsHandleCreated)
- return;
- base.ForeColor = value;
- }
- }
- [DefaultValue(true)]
- [MWFCategory("Behavior")]
- public bool HideSelection {
- get {
- return hide_selection;
- }
- set {
- if (value != hide_selection) {
- hide_selection = value;
- OnHideSelectionChanged(EventArgs.Empty);
- }
- if (hide_selection) {
- document.selection_visible = false;
- } else {
- document.selection_visible = true;
- }
- document.InvalidateSelectionArea();
- }
- }
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- [Editor("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
- [Localizable(true)]
- [MWFCategory("Appearance")]
- public string[] Lines {
- get {
- int count;
- ArrayList lines;
- count = document.Lines;
- // Handle empty document
- if ((count == 1) && (document.GetLine (1).text.Length == 0)) {
- return new string [0];
- }
- lines = new ArrayList ();
- int i = 1;
- while (i <= count) {
- Line line;
- StringBuilder lt = new StringBuilder ();
- do {
- line = document.GetLine (i++);
- lt.Append (line.text.ToString ());
- } while (line.soft_break && i <= count);
- lines.Add (lt.ToString ());
- }
- return (string []) lines.ToArray (typeof (string));
- }
- set {
- int i;
- int l;
- SolidBrush brush;
- document.Empty();
- l = value.Length;
- brush = ThemeEngine.Current.ResPool.GetSolidBrush(this.ForeColor);
- document.SuspendRecalc ();
- for (i = 0; i < l; i++) {
- // Don't add the last line if it is just an empty line feed
- // the line feed is reflected in the previous line's soft_break = false
- if (i == l - 1 && value [i].Length == 0)
- break;
- bool carriage_return = false;
- if (value [i].EndsWith ("\r")) {
- value [i] = value [i].Substring (0, value [i].Length - 1);
- carriage_return = true;
- }
- document.Add(i+1, CaseAdjust(value[i]), alignment, Font, brush);
- if (carriage_return) {
- Line line = document.GetLine (i + 1);
- line.carriage_return = true;
- }
- }
- document.ResumeRecalc (true);
- // CalculateDocument();
- OnTextChanged(EventArgs.Empty);
- }
- }
- [DefaultValue(32767)]
- [Localizable(true)]
- [MWFCategory("Behavior")]
- public virtual int MaxLength {
- get {
- if (max_length == 2147483646) { // We don't distinguish between single and multi-line limits
- return 0;
- }
- return max_length;
- }
- set {
- if (value != max_length) {
- max_length = value;
- }
- }
- }
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public bool Modified {
- get {
- return modified;
- }
- set {
- if (value != modified) {
- modified = value;
- OnModifiedChanged(EventArgs.Empty);
- }
- }
- }
- [DefaultValue(false)]
- [Localizable(true)]
- [RefreshProperties(RefreshProperties.All)]
- [MWFCategory("Behavior")]
- public virtual bool Multiline {
- get {
- return document.multiline;
- }
- set {
- if (value != document.multiline) {
- document.multiline = value;
- // Make sure we update our size; the user may have already set the size before going to multiline
- if (document.multiline && requested_height != -1) {
- Height = requested_height;
- requested_height = -1;
- }
- if (Parent != null)
- Parent.PerformLayout ();
- OnMultilineChanged(EventArgs.Empty);
- }
- if (document.multiline) {
- document.Wrap = word_wrap;
- document.PasswordChar = "";
- } else {
- document.Wrap = false;
- if (this.password_char != '\0') {
- document.PasswordChar = password_char.ToString();
- } else {
- document.PasswordChar = "";
- }
- }
- CalculateDocument ();
- }
- }
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- [EditorBrowsable(EditorBrowsableState.Advanced)]
- public int PreferredHeight {
- get {
- return Font.Height + (BorderStyle == BorderStyle.None ? 0 : 7);
- }
- }
- [DefaultValue(false)]
- [MWFCategory("Behavior")]
- public bool ReadOnly {
- get {
- return read_only;
- }
- set {
- if (value != read_only) {
- read_only = value;
- OnReadOnlyChanged(EventArgs.Empty);
- }
- }
- }
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public virtual string SelectedText {
- get {
- return document.GetSelection();
- }
- set {
- document.ReplaceSelection(CaseAdjust(value), false);
- ScrollToCaret();
- OnTextChanged(EventArgs.Empty);
- }
- }
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public virtual int SelectionLength {
- get {
- int res = document.SelectionLength ();
- if (res == 0)
- res = -1;
- return res;
- }
- set {
- if (value < 0) {
- throw new ArgumentException(String.Format("{0} is not a valid value", value), "value");
- }
- if (value != 0) {
- int start;
- Line line;
- LineTag tag;
- int pos;
- selection_length = value;
- start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
- document.CharIndexToLineTag(start + value, out line, out tag, out pos);
- document.SetSelectionEnd(line, pos);
- document.PositionCaret(line, pos);
- } else {
- selection_length = -1;
- document.SetSelectionEnd(document.selection_start.line, document.selection_start.pos);
- document.PositionCaret(document.selection_start.line, document.selection_start.pos);
- }
- }
- }
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public int SelectionStart {
- get {
- int index;
- index = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
- return index;
- }
- set {
- document.SetSelectionStart(value);
- if (selection_length > -1 ) {
- document.SetSelectionEnd(value + selection_length);
- } else {
- document.SetSelectionEnd(value);
- }
- document.PositionCaret(document.selection_start.line, document.selection_start.pos);
- ScrollToCaret();
- }
- }
- [Localizable(true)]
- public override string Text {
- get {
- if (document == null || document.Root == null || document.Root.text == null) {
- return string.Empty;
- }
- StringBuilder sb = new StringBuilder();
- Line line = null;
- for (int i = 1; i <= document.Lines; i++) {
- if (line != null && line.carriage_return)
- sb.Append ("\r");
- if (line != null && !line.soft_break)
- sb.Append (Environment.NewLine);
- line = document.GetLine (i);
- sb.Append(line.text.ToString());
- }
- return sb.ToString();
- }
- set {
- if (value == Text)
- return;
- if ((value != null) && (value != "")) {
- document.Empty ();
- document.Insert (document.GetLine (1), 0, false, value);
-
- document.PositionCaret (document.GetLine (1), 0);
- document.SetSelectionToCaret (true);
- ScrollToCaret ();
- } else {
- document.Empty();
- CalculateDocument();
- }
- // set the var so OnModifiedChanged is not raised
- modified = false;
- OnTextChanged(EventArgs.Empty);
- }
- }
- [Browsable(false)]
- public virtual int TextLength {
- get {
- if (document == null || document.Root == null || document.Root.text == null) {
- return 0;
- }
- return Text.Length;
- }
- }
- [DefaultValue(true)]
- [Localizable(true)]
- [MWFCategory("Behavior")]
- public bool WordWrap {
- get {
- return word_wrap;
- }
- set {
- if (value != word_wrap) {
- if (document.multiline) {
- word_wrap = value;
- document.Wrap = value;
- }
- }
- }
- }
- #endregion // Public Instance Properties
- #region Protected Instance Properties
- protected override CreateParams CreateParams {
- get {
- return base.CreateParams;
- }
- }
- protected override System.Drawing.Size DefaultSize {
- get {
- return new Size(100, 20);
- }
- }
- #endregion // Protected Instance Properties
- #region Public Instance Methods
- public void AppendText(string text) {
- document.MoveCaret (CaretDirection.CtrlEnd);
- document.Insert (document.caret.line, document.caret.pos, false, text);
- document.MoveCaret (CaretDirection.CtrlEnd);
- document.SetSelectionToCaret (true);
- OnTextChanged(EventArgs.Empty);
- }
- public void Clear() {
- Text = null;
- }
- public void ClearUndo() {
- document.undo.Clear();
- }
- public void Copy() {
- DataObject o;
- o = new DataObject(DataFormats.Text, SelectedText);
- if (this is RichTextBox) {
- o.SetData(DataFormats.Rtf, ((RichTextBox)this).SelectedRtf);
- }
- Clipboard.SetDataObject(o);
- }
- public void Cut() {
- DataObject o;
- o = new DataObject(DataFormats.Text, SelectedText);
- if (this is RichTextBox) {
- o.SetData(DataFormats.Rtf, ((RichTextBox)this).SelectedRtf);
- }
- Clipboard.SetDataObject (o);
- document.undo.BeginUserAction (Locale.GetText ("Cut"));
- document.ReplaceSelection (String.Empty, false);
- document.undo.EndUserAction ();
- }
- public void Paste() {
- Paste(Clipboard.GetDataObject(), null, false);
- }
- public void ScrollToCaret() {
- if (IsHandleCreated) {
- CaretMoved(this, EventArgs.Empty);
- }
- }
- public void Select(int start, int length) {
- SelectionStart = start;
- SelectionLength = length;
- }
- public void SelectAll() {
- Line last;
- last = document.GetLine(document.Lines);
- document.SetSelectionStart(document.GetLine(1), 0);
- document.SetSelectionEnd(last, last.text.Length);
- document.PositionCaret (document.selection_end.line, document.selection_end.pos);
- selection_length = -1;
- }
- public override string ToString() {
- return String.Concat (base.ToString (), ", Text: ", Text);
- }
- public void Undo() {
- document.undo.Undo();
- }
- #endregion // Public Instance Methods
- #region Protected Instance Methods
- protected override void CreateHandle() {
- base.CreateHandle ();
- document.AlignCaret();
- ScrollToCaret();
- }
- protected override bool IsInputKey(Keys keyData) {
- if ((keyData & Keys.Alt) != 0) {
- return base.IsInputKey(keyData);
- }
- switch (keyData & Keys.KeyCode) {
- case Keys.Enter: {
- if (accepts_return) {
- return true;
- }
- return false;
- }
- case Keys.Tab: {
- if (accepts_tab && document.multiline) {
- if ((keyData & Keys.Control) == 0) {
- return true;
- }
- }
- return false;
- }
- case Keys.Left:
- case Keys.Right:
- case Keys.Up:
- case Keys.Down:
- case Keys.PageUp:
- case Keys.PageDown:
- case Keys.Home:
- case Keys.End: {
- return true;
- }
- }
- return false;
- }
- protected virtual void OnAcceptsTabChanged(EventArgs e) {
- EventHandler eh = (EventHandler)(Events [AcceptsTabChangedEvent]);
- if (eh != null)
- eh (this, e);
- }
- protected virtual void OnAutoSizeChanged(EventArgs e) {
- EventHandler eh = (EventHandler)(Events [AutoSizeChangedEvent]);
- if (eh != null)
- eh (this, e);
- }
- protected virtual void OnBorderStyleChanged(EventArgs e) {
- EventHandler eh = (EventHandler)(Events [BorderStyleChangedEvent]);
- if (eh != null)
- eh (this, e);
- }
- protected override void OnFontChanged(EventArgs e) {
- base.OnFontChanged (e);
- if (auto_size && !document.multiline) {
- if (PreferredHeight != ClientSize.Height) {
- Height = PreferredHeight;
- }
- }
- }
- protected override void OnHandleCreated(EventArgs e) {
- base.OnHandleCreated (e);
- }
- protected override void OnHandleDestroyed(EventArgs e) {
- base.OnHandleDestroyed (e);
- }
- protected virtual void OnHideSelectionChanged(EventArgs e) {
- EventHandler eh = (EventHandler)(Events [HideSelectionChangedEvent]);
- if (eh != null)
- eh (this, e);
- }
- protected virtual void OnModifiedChanged(EventArgs e) {
- EventHandler eh = (EventHandler)(Events [ModifiedChangedEvent]);
- if (eh != null)
- eh (this, e);
- }
- protected virtual void OnMultilineChanged(EventArgs e) {
- EventHandler eh = (EventHandler)(Events [MultilineChangedEvent]);
- if (eh != null)
- eh (this, e);
- }
- protected virtual void OnReadOnlyChanged(EventArgs e) {
- EventHandler eh = (EventHandler)(Events [ReadOnlyChangedEvent]);
- if (eh != null)
- eh (this, e);
- }
- protected override bool ProcessDialogKey(Keys keyData) {
- return base.ProcessDialogKey(keyData);
- }
- private bool ProcessKey(Keys keyData) {
- bool control;
- bool shift;
- control = (Control.ModifierKeys & Keys.Control) != 0;
- shift = (Control.ModifierKeys & Keys.Shift) != 0;
- switch (keyData & Keys.KeyCode) {
- case Keys.X: { // Cut (Ctrl-X)
- if (control) {
- Cut();
- return true;
- }
- return false;
- }
- case Keys.C: { // Copy (Ctrl-C)
- if (control) {
- Copy();
- return true;
- }
- return false;
- }
- case Keys.V: { // Paste (Ctrl-V)
- if (control) {
- return Paste(Clipboard.GetDataObject(), null, true);
- }
- return false;
- }
- case Keys.Z: { // Undo (Ctrl-Z)
- if (control) {
- Undo();
- return true;
- }
- return false;
- }
- case Keys.A: { // Select All (Ctrl-A)
- if (control) {
- SelectAll();
- return true;
- }
- return false;
- }
- case Keys.Left: {
- if (control) {
- document.MoveCaret(CaretDirection.WordBack);
- } else {
- if (!document.selection_visible || shift) {
- document.MoveCaret(CaretDirection.CharBack);
- } else {
- document.MoveCaret(CaretDirection.SelectionStart);
- }
- }
- if (!shift) {
- document.SetSelectionToCaret(true);
- } else {
- document.SetSelectionToCaret(false);
- }
- CaretMoved(this, null);
- return true;
- }
- case Keys.Right: {
- if (control) {
- document.MoveCaret(CaretDirection.WordForward);
- } else {
- if (!document.selection_visible || shift) {
- document.MoveCaret(CaretDirection.CharForward);
- } else {
- document.MoveCaret(CaretDirection.SelectionEnd);
- }
- }
- if (!shift) {
- document.SetSelectionToCaret(true);
- } else {
- document.SetSelectionToCaret(false);
- }
- CaretMoved(this, null);
- return true;
- }
- case Keys.Up: {
- if (control) {
- if (document.CaretPosition == 0) {
- document.MoveCaret(CaretDirection.LineUp);
- } else {
- document.MoveCaret(CaretDirection.Home);
- }
- } else {
- document.MoveCaret(CaretDirection.LineUp);
- }
- if ((Control.ModifierKeys & Keys.Shift) == 0) {
- document.SetSelectionToCaret(true);
- } else {
- document.SetSelectionToCaret(false);
- }
- CaretMoved(this, null);
- return true;
- }
- case Keys.Down: {
- if (control) {
- if (document.CaretPosition == document.CaretLine.Text.Length) {
- document.MoveCaret(CaretDirection.LineDown);
- } else {
- document.MoveCaret(CaretDirection.End);
- }
- } else {
- document.MoveCaret(CaretDirection.LineDown);
- }
- if ((Control.ModifierKeys & Keys.Shift) == 0) {
- document.SetSelectionToCaret(true);
- } else {
- document.SetSelectionToCaret(false);
- }
- CaretMoved(this, null);
- return true;
- }
- case Keys.Home: {
- if ((Control.ModifierKeys & Keys.Control) != 0) {
- document.MoveCaret(CaretDirection.CtrlHome);
- } else {
- document.MoveCaret(CaretDirection.Home);
- }
- if ((Control.ModifierKeys & Keys.Shift) == 0) {
- document.SetSelectionToCaret(true);
- } else {
- document.SetSelectionToCaret(false);
- }
- CaretMoved(this, null);
- return true;
- }
- case Keys.End: {
- if ((Control.ModifierKeys & Keys.Control) != 0) {
- document.MoveCaret(CaretDirection.CtrlEnd);
- } else {
- document.MoveCaret(CaretDirection.End);
- }
- if ((Control.ModifierKeys & Keys.Shift) == 0) {
- document.SetSelectionToCaret(true);
- } else {
- document.SetSelectionToCaret(false);
- }
- CaretMoved(this, null);
- return true;
- }
- case Keys.Enter: {
- // ignoring accepts_return, fixes bug #76355
- if (!read_only && (accepts_return || (FindForm() != null && FindForm().AcceptButton == null) || ((Control.ModifierKeys & Keys.Control) != 0))) {
- Line line;
- if (document.selection_visible) {
- document.ReplaceSelection("", false);
- }
- line = document.CaretLine;
- document.Split (document.CaretLine, document.CaretTag, document.CaretPosition, line.soft_break);
- line.soft_break = false;
- OnTextChanged(EventArgs.Empty);
- document.UpdateView(line, 2, 0);
- CaretMoved(this, null);
- return true;
- }
- break;
- }
- case Keys.Tab: {
- if (!read_only && accepts_tab && document.multiline) {
- document.InsertChar(document.CaretLine, document.CaretPosition, '\t');
- if (document.selection_visible) {
- document.ReplaceSelection("", false);
- }
- document.SetSelectionToCaret(true);
- OnTextChanged(EventArgs.Empty);
- CaretMoved(this, null);
- return true;
- }
- break;
- }
- case Keys.Insert: {
- if (shift) {
- Paste(Clipboard.GetDataObject(), null, true);
- return true;
- }
- if (control) {
- Copy();
- return true;
- }
- // FIXME - need overwrite/insert toggle?
- return false;
- }
- case Keys.PageUp: {
- if ((Control.ModifierKeys & Keys.Control) != 0) {
- document.MoveCaret(CaretDirection.CtrlPgUp);
- } else {
- document.MoveCaret(CaretDirection.PgUp);
- }
- return true;
- }
- case Keys.PageDown: {
- if ((Control.ModifierKeys & Keys.Control) != 0) {
- document.MoveCaret(CaretDirection.CtrlPgDn);
- } else {
- document.MoveCaret(CaretDirection.PgDn);
- }
- return true;
- }
- case Keys.Delete: {
- if (shift) {
- Cut();
- return true;
- }
- if (read_only) {
- break;
- }
- if (document.selection_visible) {
- document.ReplaceSelection("", false);
- } else {
- // DeleteChar only deletes on the line, doesn't do the combine
- if (document.CaretPosition == document.CaretLine.Text.Length) {
- if (document.CaretLine.LineNo < document.Lines) {
- Line line;
- line = document.GetLine(document.CaretLine.LineNo + 1);
- document.Combine(document.CaretLine, line);
- document.UpdateView(document.CaretLine, 2, 0);
- #if not_Debug
- Line check_first;
- Line check_second;
- check_first = document.GetLine(document.CaretLine.LineNo);
- check_second = document.GetLine(check_first.line_no + 1);
- Console.WriteLine("Post-UpdateView: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
- #endif
- // Caret doesn't move
- }
- } else {
- if (!control) {
- document.DeleteChar(document.CaretTag, document.CaretPosition, true);
- } else {
- int end_pos;
- end_pos = document.CaretPosition;
- while ((end_pos < document.CaretLine.Text.Length) && !Document.IsWordSeparator(document.CaretLine.Text[end_pos])) {
- end_pos++;
- }
- if (end_pos < document.CaretLine.Text.Length) {
- end_pos++;
- }
- document.DeleteChars(document.CaretTag, document.CaretPosition, end_pos - document.CaretPosition);
- }
- }
- }
- OnTextChanged(EventArgs.Empty);
- document.AlignCaret();
- document.UpdateCaret();
- CaretMoved(this, null);
- return true;
- }
- }
- return false;
- }
- private void HandleBackspace(bool control) {
- bool fire_changed;
- fire_changed = false;
- // delete only deletes on the line, doesn't do the combine
- if (document.selection_visible) {
- document.undo.BeginUserAction (Locale.GetText ("Delete"));
- document.ReplaceSelection("", false);
- document.undo.EndUserAction ();
- fire_changed = true;
- }
- document.SetSelectionToCaret(true);
- if (document.CaretPosition == 0) {
- if (document.CaretLine.LineNo > 1) {
- Line line;
- int new_caret_pos;
- line = document.GetLine(document.CaretLine.LineNo - 1);
- new_caret_pos = line.text.Length;
- document.Combine(line, document.CaretLine);
- document.UpdateView(line, 1, 0);
- document.PositionCaret(line, new_caret_pos);
- document.SetSelectionToCaret (true);
- document.UpdateCaret();
- fire_changed = true;
- }
- } else {
- if (!control || document.CaretPosition == 0) {
- // Move before we delete because the delete will change positions around
- // if we cross a wrap border
- LineTag tag = document.CaretTag;
- int pos = document.CaretPosition;
- document.MoveCaret (CaretDirection.CharBack);
- document.DeleteChar (tag, pos, false);
- document.SetSelectionToCaret (true);
- } else {
- int start_pos;
-
- start_pos = document.CaretPosition - 1;
- while ((start_pos > 0) && !Document.IsWordSeparator(document.CaretLine.Text[start_pos - 1])) {
- start_pos--;
- }
- document.undo.BeginUserAction (Locale.GetText ("Delete"));
- document.DeleteChars(document.CaretTag, start_pos, document.CaretPosition - start_pos);
- document.undo.EndUserAction ();
- document.PositionCaret(document.CaretLine, start_pos);
- document.SetSelectionToCaret (true);
- }
- document.UpdateCaret();
- fire_changed = true;
- }
- if (fire_changed) {
- OnTextChanged(EventArgs.Empty);
- }
- CaretMoved(this, null);
- }
- protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
- // Make sure we don't get sized bigger than we want to be
- if (!richtext) {
- if (!document.multiline) {
- if (height != PreferredHeight) {
- requested_height = height;
- height = PreferredHeight;
- specified |= BoundsSpecified.Height;
- }
- }
- }
- base.SetBoundsCore (x, y, width, height, specified);
- }
- protected override void WndProc(ref Message m) {
- switch ((Msg)m.Msg) {
- case Msg.WM_KEYDOWN: {
- if (ProcessKeyMessage(ref m) || ProcessKey((Keys)m.WParam.ToInt32() | XplatUI.State.ModifierKeys)) {
- m.Result = IntPtr.Zero;
- return;
- }
- DefWndProc (ref m);
- return;
- }
- case Msg.WM_CHAR: {
- int ch;
- if (ProcessKeyMessage(ref m)) {
- m.Result = IntPtr.Zero;
- return;
- }
- if (read_only) {
- return;
- }
- m.Result = IntPtr.Zero;
- ch = m.WParam.ToInt32();
- if (ch == 127) {
- HandleBackspace(true);
- } else if (ch >= 32) {
- if (document.selection_visible) {
- document.ReplaceSelection("", false);
- }
- char c = (char)m.WParam;
- switch (character_casing) {
- case CharacterCasing.Upper:
- c = Char.ToUpper((char) m.WParam);
- break;
- case CharacterCasing.Lower:
- c = Char.ToLower((char) m.WParam);
- break;
- }
- if (document.Length < max_length) {
- document.InsertCharAtCaret(c, true);
- #if NET_2_0
- OnTextUpdate ();
- #endif
- OnTextChanged(EventArgs.Empty);
- CaretMoved(this, null);
- } else {
- XplatUI.AudibleAlert();
- }
- return;
- } else if (ch == 8) {
- HandleBackspace(false);
- }
- return;
- }
- case Msg.WM_SETFOCUS:
- document.CaretHasFocus ();
- base.WndProc(ref m);
- break;
- case Msg.WM_KILLFOCUS:
- document.CaretLostFocus ();
- base.WndProc(ref m);
- break;
- default:
- base.WndProc(ref m);
- return;
- }
- }
- #endregion // Protected Instance Methods
- #region Events
- static object AcceptsTabChangedEvent = new object ();
- static object AutoSizeChangedEvent = new object ();
- static object BorderStyleChangedEvent = new object ();
- static object HideSelectionChangedEvent = new object ();
- static object ModifiedChangedEvent = new object ();
- static object MultilineChangedEvent = new object ();
- static object ReadOnlyChangedEvent = new object ();
- static object HScrolledEvent = new object ();
- static object VScrolledEvent = new object ();
- public event EventHandler AcceptsTabChanged {
- add { Events.AddHandler (AcceptsTabChangedEvent, value); }
- remove { Events.RemoveHandler (AcceptsTabChangedEvent, value); }
- }
- public new event EventHandler AutoSizeChanged {
- add { Events.AddHandler (AutoSizeChangedEvent, value); }
- remove { Events.RemoveHandler (AutoSizeChangedEvent, value); }
- }
- public event EventHandler BorderStyleChanged {
- add { Events.AddHandler (BorderStyleChangedEvent, value); }
- remove { Events.RemoveHandler (BorderStyleChangedEvent, value); }
- }
- public event EventHandler HideSelectionChanged {
- add { Events.AddHandler (HideSelectionChangedEvent, value); }
- remove { Events.RemoveHandler (HideSelectionChangedEvent, value); }
- }
- public event EventHandler ModifiedChanged {
- add { Events.AddHandler (ModifiedChangedEvent, value); }
- remove { Events.RemoveHandler (ModifiedChangedEvent, value); }
- }
- public event EventHandler MultilineChanged {
- add { Events.AddHandler (MultilineChangedEvent, value); }
- remove { Events.RemoveHandler (MultilineChangedEvent, value); }
- }
- public event EventHandler ReadOnlyChanged {
- add { Events.AddHandler (ReadOnlyChangedEvent, value); }
- remove { Events.RemoveHandler (ReadOnlyChangedEvent, value); }
- }
- internal event EventHandler HScrolled {
- add { Events.AddHandler (HScrolledEvent, value); }
- remove { Events.RemoveHandler (HScrolledEvent, value); }
- }
- internal event EventHandler VScrolled {
- add { Events.AddHandler (VScrolledEvent, value); }
- remove { Events.RemoveHandler (VScrolledEvent, value); }
- }
- [Browsable(false)]
- [EditorBrowsable(EditorBrowsableState.Never)]
- public new event EventHandler BackgroundImageChanged {
- add { base.BackgroundImageChanged += value; }
- remove { base.BackgroundImageChanged -= value; }
- }
- [Browsable(false)]
- [EditorBrowsable(EditorBrowsableState.Advanced)]
- public new event EventHandler Click {
- add { base.Click += value; }
- remove { base.Click -= value; }
- }
- // XXX should this not manipulate base.Paint?
- [Browsable(false)]
- [EditorBrowsable(EditorBrowsableState.Never)]
- public new event PaintEventHandler Paint;
- #endregion // Events
- #region Private Methods
- internal Document Document {
- get {
- return document;
- }
- set {
- document = value;
- }
- }
- internal bool ShowSelection {
- get {
- if (show_selection || !hide_selection) {
- return true;
- }
- return has_focus;
- }
- set {
- if (show_selection == value)
- return;
- show_selection = value;
- // Currently InvalidateSelectionArea is commented out so do a full invalidate
- document.InvalidateSelectionArea();
- }
- }
- internal Graphics CreateGraphicsInternal() {
- if (IsHandleCreated) {
- return base.CreateGraphics();
- }
- return Graphics.FromImage(bmp);
- }
- #if Debug
- static int current;
- #endif
- internal override void OnPaintInternal (PaintEventArgs pevent) {
- // Fill background
- if (backcolor_set || (Enabled && !read_only)) {
- pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), pevent.ClipRectangle);
- } else {
- pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorControl), pevent.ClipRectangle);
- }
-
- // Draw the viewable document
- document.Draw(pevent.Graphics, pevent.ClipRectangle);
- //
- // OnPaint does not get raised on MS (see bug #80639)
- //
- pevent.Handled = true;
- }
- private bool IsDoubleClick (MouseEventArgs e)
- {
- TimeSpan interval = DateTime.Now - click_last;
- if (interval.TotalMilliseconds > SystemInformation.DoubleClickTime)
- return false;
- Size dcs = SystemInformation.DoubleClickSize;
- if (e.X < click_point_x - dcs.Width / 2 || e.X > click_point_x + dcs.Width / 2)
- return false;
- if (e.Y < click_point_y - dcs.Height / 2 || e.Y > click_point_y + dcs.Height / 2)
- return false;
- return true;
- }
- private void TextBoxBase_MouseDown (object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left) {
- document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
- if (IsDoubleClick (e)) {
- switch (click_mode) {
- case CaretSelection.Position:
- SelectWord ();
- click_mode = CaretSelection.Word;
- break;
- case CaretSelection.Word:
- if (this is TextBox) {
- document.SetSelectionToCaret (true);
- click_mode = CaretSelection.Position;
- break;
- }
- document.ExpandSelection (CaretSelection.Line, false);
- click_mode = CaretSelection.Line;
- break;
- case CaretSelection.Line:
- // Gotta do this first because Exanding to a word
- // from a line doesn't really work
- document.SetSelectionToCaret (true);
- SelectWord ();
- click_mode = CaretSelection.Word;
- break;
- }
- } else {
- document.SetSelectionToCaret (true);
- click_mode = CaretSelection.Position;
- }
- click_point_x = e.X;
- click_point_y = e.Y;
- click_last = DateTime.Now;
- }
- if ((e.Button == MouseButtons.Middle) && (((int)Environment.OSVersion.Platform == 4) || ((int)Environment.OSVersion.Platform == 128))) {
- Document.Marker marker;
- marker.tag = document.FindCursor(e.X + document.ViewPortX, e.Y + document.ViewPortY, out marker.pos);
- marker.line = marker.tag.line;
- marker.height = marker.tag.height;
- document.SetSelection(marker.line, marker.pos, marker.line, marker.pos);
- Paste (Clipboard.GetDataObject (true), null, true);
- }
- }
- private void TextBoxBase_MouseUp(object sender, MouseEventArgs e) {
- if (e.Button == MouseButtons.Left) {
- if (click_mode == CaretSelection.Position) {
- document.SetSelectionToCaret(false);
- document.DisplayCaret();
- }
- if (scroll_timer != null) {
- scroll_timer.Enabled = false;
- }
- return;
- }
- }
- private void PositionControls ()
- {
- if (hscroll.Visible) {
- //vscroll.Maximum += hscroll.Height;
- canvas_height = ClientSize.Height - hscroll.Height;
- } else {
- canvas_height = ClientSize.Height;
- }
- if (vscroll.Visible) {
- //hscroll.Maximum += vscroll.Width;
- canvas_width = ClientSize.Width - vscroll.Width;
- } else {
- canvas_width = ClientSize.Width;
- }
- document.ViewPortWidth = canvas_width;
- document.ViewPortHeight = canvas_height;
- if (canvas_height < 1 || canvas_width < 1)
- return;
- // We always move them, they just might not be displayed
- hscroll.Bounds = new Rectangle (ClientRectangle.Left,
- Math.Max (0, ClientRectangle.Height - hscroll.Height),
- Math.Max (0, ClientSize.Width - (vscroll.Visible ? vscroll.Width : 0)),
- hscroll.Height);
- vscroll.Bounds = new Rectangle (Math.Max (0, ClientRectangle.Right - vscroll.Width),
- ClientRectangle.Top, vscroll.Width,
- Math.Max (0, ClientSize.Height - (hscroll.Visible ? hscroll.Height : 0)));
-
- }
- private void TextBoxBase_SizeChanged(object sender, EventArgs e) {
- CalculateDocument();
- }
- private void TextBoxBase_MouseWheel(object sender, MouseEventArgs e) {
- if (!vscroll.Enabled) {
- return;
- }
- if (e.Delta < 0)
- vscroll.Value = Math.Min (vscroll.Value + SystemInformation.MouseWheelScrollLines,
- Math.Max (0, vscroll.Maximum - document.ViewPortHeight + 1));
- else
- vscroll.Value = Math.Max (0, vscroll.Value - SystemInformation.MouseWheelScrollLines);
- }
- internal virtual void SelectWord ()
- {
- StringBuilder s = document.caret.line.text;
- int start = document.caret.pos;
- int end = document.caret.pos;
- if (s.Length < 1) {
- if (document.caret.line.line_no >= document.Lines)
- return;
- Line line = document.GetLine (document.caret.line.line_no + 1);
- document.PositionCaret (line, 0);
- return;
- }
- if (start > 0) {
- start--;
- end--;
- }
- // skip whitespace until we hit a word
- while (start > 0 && s [start] == ' ')
- start--;
- if (start > 0) {
- while (start > 0 && (s [start] != ' '))
- start--;
- if (s [start] == ' ')
- start++;
- }
- if (s [end] == ' ') {
- while (end < s.Length && s [end] == ' ')
- end++;
- } else {
- while (end < s.Length && s [end] != ' ')
- end++;
- while (end < s.Length && s [end] == ' ')
- end++;
- }
- document.SetSelection (document.caret.line, start, document.caret.line, end);
- document.PositionCaret (document.selection_end.line, document.selection_end.pos);
- }
- internal void CalculateDocument() {
- if (!IsHandleCreated) {
- return;
- }
- document.RecalculateDocument(CreateGraphicsInternal());
- CalculateScrollBars();
- if (document.caret.line != null && document.caret.line.Y < document.ViewPortHeight) {
- // The window has probably been resized, making the entire thing visible, so
- // we need to set the scroll position back to zero.
- vscroll.Value = 0;
- }
- Invalidate();
- }
- internal void CalculateScrollBars () {
- // FIXME - need separate calculations for center and right alignment
- if (!document.multiline) {
- PositionControls ();
- return;
- }
- if (document.Width >= document.ViewPortWidth) {
- hscroll.SetValues (0, Math.Max (1, document.Width), -1,
- document.ViewPortWidth < 0 ? 0 : document.ViewPortWidth);
- hscroll.Enabled = true;
- } else {
- hscroll.Enabled = false;
- hscroll.Maximum = document.ViewPortWidth;
- }
- if (document.Height >= document.ViewPortHeight) {
- vscroll.SetValues (0, Math.Max (1, document.Height), -1,
- document.ViewPortHeight < 0 ? 0 : document.ViewPortHeight);
- vscroll.Enabled = true;
- } else {
- vscroll.Enabled = false;
- vscroll.Maximum = document.ViewPortHeight;
- }
- if (!WordWrap) {
- if ((scrollbars & RichTextBoxScrollBars.Horizontal) != 0) {
- if (((scrollbars & RichTextBoxScrollBars.ForcedHorizontal) != 0) || hscroll.Enabled) {
- hscroll.Visible = true;
- } else {
- hscroll.Visible = false;
- }
- } else {
- hscroll.Visible = false;
- }
- }
- if ((scrollbars & RichTextBoxScrollBars.Vertical) != 0) {
- if (((scrollbars & RichTextBoxScrollBars.ForcedVertical) != 0) || vscroll.Enabled) {
- vscroll.Visible = true;
- } else {
- vscroll.Visible = false;
- }
- } else {
- vscroll.Visible = false;
- }
- PositionControls ();
- }
- private void document_WidthChanged(object sender, EventArgs e) {
- CalculateScrollBars();
- }
- private void document_HeightChanged(object sender, EventArgs e) {
- CalculateScrollBars();
- }
- private void hscroll_ValueChanged(object sender, EventArgs e) {
- int old_viewport_x;
- old_viewport_x = document.ViewPortX;
- document.ViewPortX = this.hscroll.Value;
- //
- // Before scrolling we want to destroy the caret, then draw a new one after the scroll
- // the reason for this is that scrolling changes the coordinates of the caret, and we
- // will get tracers if we don't
- //
- if (Focused)
- document.CaretLostFocus ();
- if (vscroll.Visible) {
- XplatUI.ScrollWindow(this.Handle, new Rectangle(0, 0, ClientSize.Width - vscroll.Width, ClientSize.Height), old_viewport_x - this.hscroll.Value, 0, false);
- } else {
- XplatUI.ScrollWindow(this.Handle, ClientRectangle, old_viewport_x - this.hscroll.Value, 0, false);
- }
- if (Focused)
- document.CaretHasFocus ();
- EventHandler eh = (EventHandler)(Events [HScrolledEvent]);
- if (eh != null)
- eh (this, EventArgs.Empty);
- }
- private void vscroll_ValueChanged(object sender, EventArgs e) {
- int old_viewport_y;
- old_viewport_y = document.ViewPortY;
- document.ViewPortY = this.vscroll.Value;
- //
- // Before scrolling we want to destroy the caret, then draw a new one after the scroll
- // the reason for this is that scrolling changes the coordinates of the caret, and we
- // will get tracers if we don't
- //
- if (Focused)
- document.CaretLostFocus ();
- if (hscroll.Visible) {
- XplatUI.ScrollWindow(this.Handle, new Rectangle(0, 0, ClientSize.Width, ClientSize.Height - hscroll.Height), 0, old_viewport_y - this.vscroll.Value, false);
- } else {
- XplatUI.ScrollWindow(this.Handle, ClientRectangle, 0, old_viewport_y - this.vscroll.Value, false);
- }
- if (Focused)
- document.CaretHasFocus ();
- EventHandler eh = (EventHandler)(Events [VScrolledEvent]);
- if (eh != null)
- eh (this, EventArgs.Empty);
- }
- private void TextBoxBase_MouseMove(object sender, MouseEventArgs e) {
- // FIXME - handle auto-scrolling if mouse is to the right/left of the window
- if (e.Button == MouseButtons.Left && Capture) {
- if (!ClientRectangle.Contains (e.X, e.Y)) {
- if (scroll_timer == null) {
- scroll_timer = new Timer ();
- scroll_timer.Interval = 100;
- scroll_timer.Tick += new EventHandler (ScrollTimerTickHandler);
- }
- if (!scroll_timer.Enabled) {
- scroll_timer.Start ();
- // Force the first tick
- ScrollTimerTickHandler (null, EventArgs.Empty);
- }
- return;
- }
- document.PositionCaret(e.X + document.ViewPortX, e.Y + document.ViewPortY);
- if (click_mode == CaretSelection.Position) {
- document.SetSelectionToCaret(false);
- document.DisplayCaret();
- }
- }
- }
-
- private void TextBoxBase_FontOrColorChanged(object sender, EventArgs e) {
- if (!richtext) {
- Line line;
- document.SuspendRecalc ();
- // Font changes apply to the whole document
- for (int i = 1; i <= document.Lines; i++) {
- line = document.GetLine(i);
- LineTag.FormatText(line, 1, line.text.Length, Font,
- ThemeEngine.Current.ResPool.GetSolidBrush(ForeColor),
- null, FormatSpecified.Font | FormatSpecified.Color);
- }
- document.UpdateView (document.GetLine (1), 0);
- document.ResumeRecalc (true);
- // Make sure the caret height is matching the new font height
- document.AlignCaret();
- }
- }
- private void ScrollTimerTickHandler (object sender, EventArgs e)
- {
- Point pt = Cursor.Position;
- pt = PointToClient (pt);
- if (pt.X < ClientRectangle.Left) {
- document.MoveCaret(CaretDirection.CharBackNoWrap);
- document.SetSelectionToCaret(false);
-
- CaretMoved(this, null);
- } else if (pt.X > ClientRectangle.Right) {
- document.MoveCaret(CaretDirection.CharForwardNoWrap);
- document.SetSelectionToCaret(false);
-
- CaretMoved(this, null);
- } else if (pt.Y > ClientRectangle.Bottom) {
- document.MoveCaret(CaretDirection.LineDown);
- document.SetSelectionToCaret(false);
-
- CaretMoved(this, null);
- } else if (pt.Y < ClientRectangle.Top) {
- document.MoveCaret(CaretDirection.LineUp);
- document.SetSelectionToCaret(false);
-
- CaretMoved(this, null);
- }
- }
- /// <summary>Ensure the caret is always visible</summary>
- internal void CaretMoved(object sender, EventArgs e) {
- Point pos;
- int height;
- if (canvas_width < 1 || canvas_height < 1)
- return;
- document.MoveCaretToTextTag ();
- pos = document.Caret;
- //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);
- // Horizontal scrolling:
- // If the caret moves to the left outside the visible area, we jump the document into view, not just one
- // character, but 1/3 of the width of the document
- // If the caret moves to the right outside the visible area, we scroll just enough to keep the caret visible
- // Handle horizontal scrolling
- if (document.CaretLine.alignment == HorizontalAlignment.Left) {
- // Check if we moved out of view to the left
- if (pos.X < (document.ViewPortX)) {
- do {
- if ((hscroll.Value - document.ViewPortWidth / 3) >= hscroll.Minimum) {
- hscroll.Value -= document.ViewPortWidth / 3;
- } else {
- hscroll.Value = hscroll.Minimum;
- }
- } while (hscroll.Value > pos.X);
- }
- // Check if we moved out of view to the right
- if ((pos.X >= (document.ViewPortWidth + document.ViewPortX)) && (hscroll.Value != hscroll.Maximum)) {
- if ((pos.X - document.ViewPortWidth + 1) <= hscroll.Maximum) {
- if (pos.X - document.ViewPortWidth >= 0) {
- hscroll.Value = pos.X - document.ViewPortWidth + 1;
- } else {
- hscroll.Value = 0;
- }
- } else {
- hscroll.Value = hscroll.Maximum;
- }
- }
- } else if (document.CaretLine.alignment == HorizontalAlignment.Right) {
- // hscroll.Value = pos.X;
- // if ((pos.X > (this.canvas_width + document.ViewPortX)) && (hscroll.Enabled && (hscroll.Value != hscroll.Maximum))) {
- // hscroll.Value = hscroll.Maximum;
- // }
- } else {
- // FIXME - implement center cursor alignment
- }
- if (!document.multiline) {
- return;
- }
- // Handle vertical scrolling
- height = document.CaretLine.Height + 1;
- if (pos.Y < document.ViewPortY) {
- vscroll.Value = pos.Y;
- }
- if ((pos.Y + height) > (document.ViewPortY + canvas_height)) {
- vscroll.Value = Math.Max (0, pos.Y - canvas_height + height);
- }
- }
- internal bool Paste(IDataObject clip, DataFormats.Format format, bool obey_length) {
- string s;
- if (clip == null)
- return false;
- if (format == null) {
- if ((this is RichTextBox) && clip.GetDataPresent(DataFormats.Rtf)) {
- format = DataFormats.GetFormat(DataFormats.Rtf);
- } else if ((this is RichTextBox) && clip.GetDataPresent (DataFormats.Bitmap)) {
- format = DataFormats.GetFormat (DataFormats.Bitmap);
- } else if (clip.GetDataPresent(DataFormats.UnicodeText)) {
- format = DataFormats.GetFormat(DataFormats.UnicodeText);
- } else if (clip.GetDataPresent(DataFormats.Text)) {
- format = DataFormats.GetFormat(DataFormats.Text);
- } else {
- return false;
- }
- } else {
- if ((format.Name == DataFormats.Rtf) && !(this is RichTextBox)) {
- return false;
- }
- if (!clip.GetDataPresent(format.Name)) {
- return false;
- }
- }
- if (format.Name == DataFormats.Rtf) {
- document.undo.BeginUserAction (Locale.GetText ("Paste"));
- ((RichTextBox)this).SelectedRtf = (string)clip.GetData(DataFormats.Rtf);
- document.undo.EndUserAction ();
- return true;
- } else if (format.Name == DataFormats.Bitmap) {
- document.undo.BeginUserAction (Locale.GetText ("Paste"));
- document.InsertImage (document.caret.line, document.caret.pos, (Image) clip.GetData (DataFormats.Bitmap));
- document.MoveCaret (CaretDirection.CharForward);
- document.undo.EndUserAction ();
- return true;
- } else if (format.Name == DataFormats.UnicodeText) {
- s = (string)clip.GetData(DataFormats.UnicodeText);
- } else if (format.Name == DataFormats.Text) {
- s = (string)clip.GetData(DataFormats.Text);
- } else {
- return false;
- }
- if (!obey_length) {
- document.undo.BeginUserAction (Locale.GetText ("Paste"));
- this.SelectedText = s;
- document.undo.EndUserAction ();
- } else {
- if ((s.Length + document.Length) < max_length) {
- document.undo.BeginUserAction (Locale.GetText ("Paste"));
- this.SelectedText = s;
- document.undo.EndUserAction ();
- } else if (document.Length < max_length) {
- document.undo.BeginUserAction (Locale.GetText ("Paste"));
- this.SelectedText = s.Substring (0, max_length - document.Length);
- document.undo.EndUserAction ();
- }
- }
- return true;
- }
- #endregion // Private Methods
- #if NET_2_0
- // This is called just before OnTextChanged is called.
- internal virtual void OnTextUpdate ()
- {
- }
-
- protected override void OnTextChanged (EventArgs e)
- {
- base.OnTextChanged (e);
- }
- public virtual int GetLineFromCharIndex (int index)
- {
- Line line_out;
- LineTag tag_out;
- int pos;
-
- document.CharIndexToLineTag (index, out line_out, out tag_out, out pos);
- return line_out.LineNo;
- }
- protected override void OnMouseUp (MouseEventArgs e)
- {
- base.OnMouseUp (e);
- }
- #endif
- }
- }
|