public Caret(Caret cpy)
 {
     this.textBox = cpy.textBox;
     this.position = cpy.position;
     this.IsSet = cpy.IsSet;
     this.column = cpy.column;
     this.row = cpy.row;
     this.realcolumn = cpy.realcolumn;
 }
 public Label()
 {
     caret = new Caret(this);
     visualCaret = new Graphics.Interface.Caret();
     AddChild(visualCaret);
     Font = InterfaceScene.DefaultFont;
     Clickable = false;
     readOnly = true;
     selectable = false;
 }
        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            if (selectable)
            {
                var screenPos = new Vector2(e.X, e.Y);
                selectionStart = null;
                Vector2 pos = screenPos - textGraphic.Offset - new Vector2(AbsoluteTranslation.X, AbsoluteTranslation.Y);
                caret.SetFromXY(pos);

                selectionStart = new Caret(caret);
            }

            Focus();
            base.OnMouseDown(e);
        }
        protected List<Glyph> GetSelectionBoxes()
        {
            if (selectionStart == null || selectionStart.Position == caret.Position)
                return null;

            var fi = Scene.View.Content.Acquire<FontImplementation>(Font);

            Caret start = selectionStart.Position < caret.Position ? selectionStart : caret;
            Caret end = selectionStart.Position < caret.Position ? caret : selectionStart;
            Vector3 startPos = start.DocumentPosition;
            Vector3 endPos = end.DocumentPosition;

            List<Glyph> selectionBoxes = new List<Glyph>();
            Vector2 sOffset = new Vector2(0, fi.CharacterHeight - 1);

            Caret it = new Caret(start) { Visible = false };
            for (int i = start.Row; i <= end.Row; i++)
            {
                Vector3 startp = it.DocumentPosition;
                it.MoveToEndOfLine();
                Vector3 endp = it.Position <= end.Position ? it.DocumentPosition : end.DocumentPosition;
                it.Position++;

                Glyph g = new Glyph();
                g.Position = startp;
                g.Size = new Vector2(endp.X - startp.X, endp.Y - startp.Y) + sOffset;
                g.UVMin = Vector2.Zero;
                g.UVMax = new Vector2(1, 1);
                selectionBoxes.Add(g);
            }

            Scene.View.Content.Release(fi);

            return selectionBoxes;
        }
 public void SelectAll()
 {
     selectionStart = new Caret(caret);
     selectionStart.Set(0);
     caret.Set(document.Lines.Last().Length, document.Lines.Count);
 }
 protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
 {
     if (!readOnly)
     {
         e.Handled = true;
         switch (e.KeyCode)
         {
             case Keys.Left:
             case Keys.Right:
             case Keys.Up:
             case Keys.Down:
             case Keys.Home:
             case Keys.End:
                 e.SuppressKeyPress = true;
                 if (e.Shift && selectable)
                 {
                     if (selectionStart == null) selectionStart = new Caret(caret);
                 }
                 else if (selectionStart != null)
                 {
                     selectionStart = null;
                     UpdateSelection();
                 }
                 if (e.KeyCode == Keys.Left)
                 {
                     if (e.Control)
                         caret.MoveToBeginningOfWord();
                     else
                         caret--;
                 }
                 if (e.KeyCode == Keys.Right)
                 {
                     if (e.Control)
                         caret.MoveToEndOfWord();
                     else
                         caret++;
                 }
                 if (e.KeyCode == Keys.Up) caret.Up();
                 if (e.KeyCode == Keys.Down) caret.Down();
                 if (e.KeyCode == Keys.Home) caret.Set(0, caret.Row);
                 if (e.KeyCode == Keys.End) caret.Set(document.Lines[caret.Row].Length - 1, caret.Row);
                 OnCaretChanged();
                 break;
             case Keys.Back:
             case Keys.Delete:
                 if (selectionStart != null)
                 {
                     int selpos = selectionStart.Position;
                     DeleteSelection();
                     caret.Position = Math.Min(caret.Position, selpos);
                     OnTextChanged();
                 }
                 else {
                     if (caret > 0 && e.KeyCode == Keys.Back)
                     {
                         caret--;
                         document.Remove(caret, 1);
                         OnTextChanged();
                     }
                     else if (caret <= document.Length - 1 && e.KeyCode == Keys.Delete)
                     {
                         document.Remove(caret, 1);
                         OnTextChanged();
                     }
                 }
                 e.SuppressKeyPress = true;
                 break;
             case Keys.X:
                 if (e.Control)
                 {
                     if (selectionStart != null)
                     {
                         string s = SelectedText();
                         if (s.Length > 0)
                             Clipboard.SetText(s);
                         DeleteSelection();
                         OnTextChanged();
                     }
                     e.SuppressKeyPress = true;
                 }
                 break;
             case Keys.C:
                 if (e.Control)
                 {
                     if (selectionStart != null)
                     {
                         string s = SelectedText();
                         if (s.Length > 0)
                             Clipboard.SetText(s);
                     }
                     e.SuppressKeyPress = true;
                 }
                 break;
             case Keys.V:
                 if (e.Control)
                 {
                     if (selectionStart != null)
                         DeleteSelection();
                     string cb = Clipboard.GetText();
                     foreach (char c in cb)
                         InsertCharacter(c);
                     e.SuppressKeyPress = true;
                 }
                 break;
             case Keys.A:
                 if (e.Control && selectable)
                 {
                     SelectAll();
                     e.SuppressKeyPress = true;
                 }
                 break;
         }
     }
     base.OnKeyDown(e);
 }
 public void SelectAll()
 {
     selectionStart = new Caret(caret);
     selectionStart.Set(0);
     caret.Set(document.Lines.Last().Length, document.Lines.Count);
 }
 public void Insert(Caret start, char value)
 {
     Insert(start, "" + value);
 }
 public void Set(Caret p)
 {
     position = p.position;
     IsSet = true;
     column = p.column;
     realcolumn = p.realcolumn;
     row = p.row;
     if (Visible)
         textBox.UpdateSelection();
 }
        protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (!readOnly)
            {
                e.Handled = true;
                switch (e.KeyCode)
                {
                case Keys.Left:
                case Keys.Right:
                case Keys.Up:
                case Keys.Down:
                case Keys.Home:
                case Keys.End:
                    e.SuppressKeyPress = true;
                    if (e.Shift && selectable)
                    {
                        if (selectionStart == null)
                        {
                            selectionStart = new Caret(caret);
                        }
                    }
                    else if (selectionStart != null)
                    {
                        selectionStart = null;
                        UpdateSelection();
                    }
                    if (e.KeyCode == Keys.Left)
                    {
                        if (e.Control)
                        {
                            caret.MoveToBeginningOfWord();
                        }
                        else
                        {
                            caret--;
                        }
                    }
                    if (e.KeyCode == Keys.Right)
                    {
                        if (e.Control)
                        {
                            caret.MoveToEndOfWord();
                        }
                        else
                        {
                            caret++;
                        }
                    }
                    if (e.KeyCode == Keys.Up)
                    {
                        caret.Up();
                    }
                    if (e.KeyCode == Keys.Down)
                    {
                        caret.Down();
                    }
                    if (e.KeyCode == Keys.Home)
                    {
                        caret.Set(0, caret.Row);
                    }
                    if (e.KeyCode == Keys.End)
                    {
                        caret.Set(document.Lines[caret.Row].Length - 1, caret.Row);
                    }
                    OnCaretChanged();
                    break;

                case Keys.Back:
                case Keys.Delete:
                    if (selectionStart != null)
                    {
                        int selpos = selectionStart.Position;
                        DeleteSelection();
                        caret.Position = Math.Min(caret.Position, selpos);
                        OnTextChanged();
                    }
                    else
                    {
                        if (caret > 0 && e.KeyCode == Keys.Back)
                        {
                            caret--;
                            document.Remove(caret, 1);
                            OnTextChanged();
                        }
                        else if (caret <= document.Length - 1 && e.KeyCode == Keys.Delete)
                        {
                            document.Remove(caret, 1);
                            OnTextChanged();
                        }
                    }
                    e.SuppressKeyPress = true;
                    break;

                case Keys.X:
                    if (e.Control)
                    {
                        if (selectionStart != null)
                        {
                            string s = SelectedText();
                            if (s.Length > 0)
                            {
                                Clipboard.SetText(s);
                            }
                            DeleteSelection();
                            OnTextChanged();
                        }
                        e.SuppressKeyPress = true;
                    }
                    break;

                case Keys.C:
                    if (e.Control)
                    {
                        if (selectionStart != null)
                        {
                            string s = SelectedText();
                            if (s.Length > 0)
                            {
                                Clipboard.SetText(s);
                            }
                        }
                        e.SuppressKeyPress = true;
                    }
                    break;

                case Keys.V:
                    if (e.Control)
                    {
                        if (selectionStart != null)
                        {
                            DeleteSelection();
                        }
                        string cb = Clipboard.GetText();
                        foreach (char c in cb)
                        {
                            InsertCharacter(c);
                        }
                        e.SuppressKeyPress = true;
                    }
                    break;

                case Keys.A:
                    if (e.Control && selectable)
                    {
                        SelectAll();
                        e.SuppressKeyPress = true;
                    }
                    break;
                }
            }
            base.OnKeyDown(e);
        }
 public void Remove(Caret start, int count)
 {
     String t = Text;
     if(t.Length >= start.Position + count)
         Text = t.Remove(start.Position, count);
 }
 public void Insert(Caret start, String value)
 {
     if (value.Contains('\r') || value.Contains('\n'))
     {
         Text = Text.Insert(start.Position, value.Replace("\r\n", "\n").Replace('\r', '\n'));
     }
     else
     {
         Document.Line line = Lines[start.Row];
         line.Value = line.Value.Insert(start.Column, value);
         CalcLineIndices();
     }
 }
 public void Insert(Caret start, char value)
 {
     Insert(start, "" + value);
 }